Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Xml / System / Xml / Base64Decoder.cs / 1305376 / Base64Decoder.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics;
namespace System.Xml {
internal class Base64Decoder : IncrementalReadDecoder {
//
// Fields
//
byte[] buffer;
int startIndex;
int curIndex;
int endIndex;
int bits;
int bitsFilled;
private static readonly String CharsBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static readonly byte[] MapBase64 = ConstructMapBase64();
private const int MaxValidChar = (int)'z';
private const byte Invalid = unchecked((byte)-1);
//
// 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 ), 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 ), out charsDecoded, out bytesDecoded );
}
}
curIndex += bytesDecoded;
return charsDecoded;
}
internal override void Reset() {
bitsFilled = 0;
bits = 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;
}
//
// Private methods
//
private static byte[] ConstructMapBase64() {
byte[] mapBase64 = new byte[MaxValidChar + 1];
for ( int i = 0; i < mapBase64.Length; i++ ) {
mapBase64[i]= Invalid;
}
for ( int i = 0; i < CharsBase64.Length; i++ ) {
mapBase64[(int)CharsBase64[i]] = (byte)i;
}
return mapBase64;
}
#if SILVERLIGHT && !SILVERLIGHT_DISABLE_SECURITY
[System.Security.SecurityCritical]
#endif
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);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics;
namespace System.Xml {
internal class Base64Decoder : IncrementalReadDecoder {
//
// Fields
//
byte[] buffer;
int startIndex;
int curIndex;
int endIndex;
int bits;
int bitsFilled;
private static readonly String CharsBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static readonly byte[] MapBase64 = ConstructMapBase64();
private const int MaxValidChar = (int)'z';
private const byte Invalid = unchecked((byte)-1);
//
// 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 ), 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 ), out charsDecoded, out bytesDecoded );
}
}
curIndex += bytesDecoded;
return charsDecoded;
}
internal override void Reset() {
bitsFilled = 0;
bits = 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;
}
//
// Private methods
//
private static byte[] ConstructMapBase64() {
byte[] mapBase64 = new byte[MaxValidChar + 1];
for ( int i = 0; i < mapBase64.Length; i++ ) {
mapBase64[i]= Invalid;
}
for ( int i = 0; i < CharsBase64.Length; i++ ) {
mapBase64[(int)CharsBase64[i]] = (byte)i;
}
return mapBase64;
}
#if SILVERLIGHT && !SILVERLIGHT_DISABLE_SECURITY
[System.Security.SecurityCritical]
#endif
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);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XmlSchemaValidationException.cs
- ChtmlTextWriter.cs
- XmlILTrace.cs
- SqlFacetAttribute.cs
- CustomErrorCollection.cs
- AssemblyHash.cs
- Types.cs
- UnicodeEncoding.cs
- Pair.cs
- SimpleMailWebEventProvider.cs
- latinshape.cs
- SqlComparer.cs
- CFGGrammar.cs
- CodeRegionDirective.cs
- PropertyCollection.cs
- XmlObjectSerializer.cs
- ObjectTypeMapping.cs
- InstancePersistenceEvent.cs
- WebSysDefaultValueAttribute.cs
- ShaderRenderModeValidation.cs
- listitem.cs
- GeneralTransform3D.cs
- BasicSecurityProfileVersion.cs
- SamlEvidence.cs
- MenuItemAutomationPeer.cs
- TextSpanModifier.cs
- XmlTypeAttribute.cs
- DbConnectionClosed.cs
- SecurityHelper.cs
- ClosableStream.cs
- UnionExpr.cs
- SystemDropShadowChrome.cs
- ProfileSettingsCollection.cs
- MultiSelector.cs
- MessageContractImporter.cs
- SqlErrorCollection.cs
- SelectionHighlightInfo.cs
- SqlNotificationRequest.cs
- SiteMapNode.cs
- Parser.cs
- WebPartDescription.cs
- EventSetter.cs
- InputDevice.cs
- MetadataExporter.cs
- PtsHost.cs
- LateBoundBitmapDecoder.cs
- PrivilegeNotHeldException.cs
- SqlCacheDependencyDatabase.cs
- ResourceDisplayNameAttribute.cs
- AssemblyCacheEntry.cs
- MultiSelector.cs
- PreservationFileReader.cs
- TemplateControlCodeDomTreeGenerator.cs
- DataGridViewHitTestInfo.cs
- ProviderManager.cs
- FixedSOMImage.cs
- SystemUnicastIPAddressInformation.cs
- DataPagerFieldCommandEventArgs.cs
- SelectManyQueryOperator.cs
- AuthenticationService.cs
- CapabilitiesAssignment.cs
- DataContractSerializerOperationGenerator.cs
- TypeUsage.cs
- HttpCookiesSection.cs
- WindowsFormsLinkLabel.cs
- ListItemsCollectionEditor.cs
- MouseGestureValueSerializer.cs
- EventLevel.cs
- ThicknessKeyFrameCollection.cs
- SemanticResultValue.cs
- PeerToPeerException.cs
- ApplicationCommands.cs
- SqlIdentifier.cs
- EventMap.cs
- FolderLevelBuildProviderAppliesToAttribute.cs
- FixedSchema.cs
- RSAOAEPKeyExchangeDeformatter.cs
- DataRowIndexBuffer.cs
- CompilationLock.cs
- DrawingAttributeSerializer.cs
- Latin1Encoding.cs
- XmlFormatWriterGenerator.cs
- GPRECTF.cs
- WinFormsSpinner.cs
- Selection.cs
- ProgressChangedEventArgs.cs
- Table.cs
- DynamicEntity.cs
- XmlMapping.cs
- PassportAuthentication.cs
- LinkButton.cs
- TextTrailingCharacterEllipsis.cs
- coordinator.cs
- EntityType.cs
- MinimizableAttributeTypeConverter.cs
- Encoder.cs
- InputLanguageManager.cs
- SignedXml.cs
- QueryParameter.cs
- CodeLinePragma.cs