Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BinHexDecoder.cs « Xml « System « System.Xml « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0e3e37cbb0451864812644701440d0ffef4e184 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//------------------------------------------------------------------------------
// <copyright file="BinHexDecoder.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;

namespace System.Xml
{
    internal class BinHexDecoder : IncrementalReadDecoder {
//
// Fields
//
        byte[]  buffer;
        int     startIndex;
        int     curIndex;
        int     endIndex;
        bool    hasHalfByteCached;
        byte    cachedHalfByte;

//
// IncrementalReadDecoder interface
//
        internal override int DecodedCount { 
            get { 
                return curIndex - startIndex; 
            } 
        }

        internal override bool IsFull { 
            get { 
                return curIndex == endIndex; 
            }
        }

#if SILVERLIGHT && !SILVERLIGHT_DISABLE_SECURITY
        [System.Security.SecuritySafeCritical]
#endif
        internal override unsafe int Decode(char[] chars, int startPos, int len) {
            if ( chars == null ) {
                throw new ArgumentNullException( "chars" );
            }
            if ( len < 0 ) {
                throw new ArgumentOutOfRangeException( "len" );
            }
            if ( startPos < 0 ) {
                throw new ArgumentOutOfRangeException( "startPos" );
            }
            if ( chars.Length - startPos < len ) {
                throw new ArgumentOutOfRangeException( "len" );
            }

            if ( len == 0 ) {
                return 0;
            }
            int bytesDecoded, charsDecoded;
            fixed ( char* pChars = &chars[startPos] ) {
                fixed ( byte* pBytes = &buffer[curIndex] ) {
                    Decode( pChars, pChars + len, pBytes, pBytes + ( endIndex - curIndex ),  
                            ref this.hasHalfByteCached, ref this.cachedHalfByte, out charsDecoded, out bytesDecoded );
                }
            }
            curIndex += bytesDecoded;
            return charsDecoded;
        }

#if SILVERLIGHT && !SILVERLIGHT_DISABLE_SECURITY
        [System.Security.SecuritySafeCritical]
#endif
        internal override unsafe int Decode(string str, int startPos, int len) {
            if ( str == null ) {
                throw new ArgumentNullException( "str" );
            }
            if ( len < 0 ) {
                throw new ArgumentOutOfRangeException( "len" );
            }
            if ( startPos < 0 ) {
                throw new ArgumentOutOfRangeException( "startPos" );
            }
            if ( str.Length - startPos < len ) {
                throw new ArgumentOutOfRangeException( "len" );
            }

            if ( len == 0 ) {
                return 0;
            }
            int bytesDecoded, charsDecoded;
            fixed ( char* pChars = str ) {
                fixed ( byte* pBytes = &buffer[curIndex] ) {
                    Decode( pChars + startPos, pChars + startPos + len, pBytes, pBytes + ( endIndex - curIndex ),  
                            ref this.hasHalfByteCached, ref this.cachedHalfByte, out charsDecoded, out bytesDecoded );
                }
            }
            curIndex += bytesDecoded;
            return charsDecoded;
        }

        internal override void Reset() {
            this.hasHalfByteCached = false;
            this.cachedHalfByte = 0;
        }

        internal override void SetNextOutputBuffer( Array buffer, int index, int count ) {
            Debug.Assert( buffer != null );
            Debug.Assert( count >= 0 );
            Debug.Assert( index >= 0 );
            Debug.Assert( buffer.Length - index >= count );
            Debug.Assert( ( buffer as byte[] ) != null );

            this.buffer = (byte[])buffer;
            this.startIndex = index;
            this.curIndex = index;
            this.endIndex = index + count;
        }

//
// Static methods
//
#if SILVERLIGHT && !SILVERLIGHT_DISABLE_SECURITY
        [System.Security.SecuritySafeCritical]
#endif
        public static unsafe byte[] Decode(char[] chars, bool allowOddChars) {
            if ( chars == null ) {
                throw new ArgumentNullException( "chars" );
            }
            
            int len = chars.Length;
            if ( len == 0 ) {
                return new byte[0];
            }

            byte[] bytes = new byte[ ( len + 1 ) / 2 ];
            int bytesDecoded, charsDecoded;
            bool hasHalfByteCached = false;
            byte cachedHalfByte = 0;
            
            fixed ( char* pChars = &chars[0] ) {
                fixed ( byte* pBytes = &bytes[0] ) {
                    Decode( pChars, pChars + len, pBytes, pBytes + bytes.Length, ref hasHalfByteCached, ref cachedHalfByte, out charsDecoded, out bytesDecoded );
                }
            }

            if ( hasHalfByteCached && !allowOddChars ) {
                throw new XmlException( Res.Xml_InvalidBinHexValueOddCount, new string( chars ) );
            }

            if ( bytesDecoded < bytes.Length ) {
                byte[] tmp = new byte[ bytesDecoded ];
                Array.Copy( bytes, 0, tmp, 0, bytesDecoded );
                bytes = tmp;
            }

            return bytes;
        }

//
// Private methods
//

#if SILVERLIGHT && !SILVERLIGHT_DISABLE_SECURITY
        [System.Security.SecurityCritical]
#endif
        private static unsafe void Decode(char* pChars, char* pCharsEndPos, 
                                    byte* pBytes, byte* pBytesEndPos, 
                                    ref bool hasHalfByteCached, ref byte cachedHalfByte,
								    out int charsDecoded, out int bytesDecoded ) {
#if DEBUG
            Debug.Assert( pCharsEndPos - pChars >= 0 );
            Debug.Assert( pBytesEndPos - pBytes >= 0 );
#endif

            char* pChar = pChars;
            byte* pByte = pBytes;
            XmlCharType xmlCharType = XmlCharType.Instance;
            while ( pChar < pCharsEndPos && pByte < pBytesEndPos ) {
                byte halfByte;
                char ch = *pChar++;

                if ( ch >= 'a' && ch <= 'f' ) {
                    halfByte = (byte)(ch - 'a' + 10);
                }
                else if ( ch >= 'A' && ch <= 'F' ) {
                    halfByte = (byte)(ch - 'A' + 10);
                }
                else if ( ch >= '0' && ch <= '9' ) {
                    halfByte = (byte)(ch - '0');
                }
                else if ( ( xmlCharType.charProperties[ch] & XmlCharType.fWhitespace ) != 0 ) { // else if ( xmlCharType.IsWhiteSpace( ch ) ) {
                    continue; 
                }
                else {
                    throw new XmlException( Res.Xml_InvalidBinHexValue, new string( pChars, 0, (int)( pCharsEndPos - pChars ) ) );
                }

                if ( hasHalfByteCached ) {
                    *pByte++ = (byte)( ( cachedHalfByte << 4 ) + halfByte );
                    hasHalfByteCached = false;
                }
                else {
                    cachedHalfByte = halfByte;
                    hasHalfByteCached = true;
                }
            }

            bytesDecoded = (int)(pByte - pBytes);
            charsDecoded = (int)(pChar - pChars);
        }
    }
}