Base64Encoder.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //------------------------------------------------------------------------------
  2. // <copyright file="Base64Encoder.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. //------------------------------------------------------------------------------
  7. using System.Text;
  8. using System.Diagnostics;
  9. namespace System.Xml {
  10. internal abstract partial class Base64Encoder {
  11. byte[] leftOverBytes;
  12. int leftOverBytesCount;
  13. char[] charsLine;
  14. internal const int Base64LineSize = 76;
  15. internal const int LineSizeInBytes = Base64LineSize/4*3;
  16. internal Base64Encoder() {
  17. charsLine = new char[Base64LineSize];
  18. }
  19. internal abstract void WriteChars( char[] chars, int index, int count );
  20. internal void Encode( byte[] buffer, int index, int count ) {
  21. if ( buffer == null ) {
  22. throw new ArgumentNullException( "buffer" );
  23. }
  24. if ( index < 0 ) {
  25. throw new ArgumentOutOfRangeException( "index" );
  26. }
  27. if ( count < 0 ) {
  28. throw new ArgumentOutOfRangeException( "count" );
  29. }
  30. if ( count > buffer.Length - index ) {
  31. throw new ArgumentOutOfRangeException( "count" );
  32. }
  33. // encode left-over buffer
  34. if( leftOverBytesCount > 0 ) {
  35. int i = leftOverBytesCount;
  36. while ( i < 3 && count > 0 ) {
  37. leftOverBytes[i++] = buffer[index++];
  38. count--;
  39. }
  40. // the total number of buffer we have is less than 3 -> return
  41. if ( count == 0 && i < 3 ) {
  42. leftOverBytesCount = i;
  43. return;
  44. }
  45. // encode the left-over buffer and write out
  46. int leftOverChars = Convert.ToBase64CharArray( leftOverBytes, 0, 3, charsLine, 0 );
  47. WriteChars( charsLine, 0, leftOverChars );
  48. }
  49. // store new left-over buffer
  50. leftOverBytesCount = count % 3;
  51. if ( leftOverBytesCount > 0 ) {
  52. count -= leftOverBytesCount;
  53. if ( leftOverBytes == null ) {
  54. leftOverBytes = new byte[3];
  55. }
  56. for( int i = 0; i < leftOverBytesCount; i++ ) {
  57. leftOverBytes[i] = buffer[ index + count + i ];
  58. }
  59. }
  60. // encode buffer in 76 character long chunks
  61. int endIndex = index + count;
  62. int chunkSize = LineSizeInBytes;
  63. while( index < endIndex ) {
  64. if ( index + chunkSize > endIndex ) {
  65. chunkSize = endIndex - index;
  66. }
  67. int charCount = Convert.ToBase64CharArray( buffer, index, chunkSize, charsLine, 0 );
  68. WriteChars( charsLine, 0, charCount );
  69. index += chunkSize;
  70. }
  71. }
  72. internal void Flush() {
  73. if ( leftOverBytesCount > 0 ) {
  74. int leftOverChars = Convert.ToBase64CharArray( leftOverBytes, 0, leftOverBytesCount, charsLine, 0 );
  75. WriteChars( charsLine, 0, leftOverChars );
  76. leftOverBytesCount = 0;
  77. }
  78. }
  79. }
  80. internal partial class XmlRawWriterBase64Encoder : Base64Encoder {
  81. XmlRawWriter rawWriter;
  82. internal XmlRawWriterBase64Encoder( XmlRawWriter rawWriter ) {
  83. this.rawWriter = rawWriter;
  84. }
  85. internal override void WriteChars( char[] chars, int index, int count ) {
  86. rawWriter.WriteRaw( chars, index, count );
  87. }
  88. }
  89. #if !SILVERLIGHT || FEATURE_NETCORE
  90. internal partial class XmlTextWriterBase64Encoder : Base64Encoder {
  91. XmlTextEncoder xmlTextEncoder;
  92. internal XmlTextWriterBase64Encoder( XmlTextEncoder xmlTextEncoder ) {
  93. this.xmlTextEncoder = xmlTextEncoder;
  94. }
  95. internal override void WriteChars( char[] chars, int index, int count ) {
  96. xmlTextEncoder.WriteRaw( chars, index, count );
  97. }
  98. }
  99. #endif
  100. }