private unsafe void Decode( char* pChars, char* pCharsEndPos,
byte* pBytes, byte* pBytesEndPos,
out int charsDecoded, out int bytesDecoded ) {
#if DEBUG
Debug.Assert( pCharsEndPos - pChars >= 0 );
Debug.Assert( pBytesEndPos - pBytes >= 0 );
#endif
// walk hex digits pairing them up and shoving the value of each pair into a byte
byte *pByte = pBytes;
char *pChar = pChars;
int b = bits;
int bFilled = bitsFilled;
XmlCharType xmlCharType = XmlCharType.Instance;
while ( pChar < pCharsEndPos && pByte < pBytesEndPos ) {
char ch = *pChar;
// end?
if ( ch == '=' ) {
break;
}
pChar++;
// ignore white space
if ( ( xmlCharType.charProperties[ch] & XmlCharType.fWhitespace ) != 0 ) { // if ( xmlCharType.IsWhiteSpace(ch) ) {
continue;
}
int digit;
if ( ch > 122 || ( digit = MapBase64[ch] ) == Invalid ) {
throw new XmlException(Res.Xml_InvalidBase64Value, new string( pChars, 0, (int)(pCharsEndPos - pChars) ));
}
b = ( b << 6 ) | digit;
bFilled += 6;
if ( bFilled >= 8 ) {
// get top eight valid bits
*pByte++ = (byte)( ( b >> ( bFilled - 8 ) ) & 0xFF );
bFilled -= 8;
if ( pByte == pBytesEndPos ) {
goto Return;
}
}
}
if ( pChar < pCharsEndPos && *pChar == '=' ) {
bFilled = 0;
// ignore padding chars
do {
pChar++;
} while ( pChar < pCharsEndPos && *pChar == '=' );
// ignore whitespace after the padding chars
if ( pChar < pCharsEndPos ) {
do {
if ( !( ( xmlCharType.charProperties[*pChar++] & XmlCharType.fWhitespace ) != 0 ) ) { // if ( !( xmlCharType.IsWhiteSpace( chars[charPos++] ) ) ) {
throw new XmlException(Res.Xml_InvalidBase64Value, new string( pChars, 0, (int)( pCharsEndPos - pChars ) ));
}
} while ( pChar < pCharsEndPos );
}
}
Return:
bits = b;
bitsFilled = bFilled;
bytesDecoded = (int)(pByte - pBytes);
charsDecoded = (int)(pChar - pChars);
}
}