Base64.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using System.Text;
  19. namespace SharpCifs.Util
  20. {
  21. public class Base64
  22. {
  23. private static readonly string Alphabet
  24. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  25. /// <summary>Base-64 encodes the supplied block of data.</summary>
  26. /// <remarks>
  27. /// Base-64 encodes the supplied block of data. Line wrapping is not
  28. /// applied on output.
  29. /// </remarks>
  30. /// <param name="bytes">The block of data that is to be Base-64 encoded.</param>
  31. /// <returns>A <code>String</code> containing the encoded data.</returns>
  32. public static string Encode(byte[] bytes)
  33. {
  34. int length = bytes.Length;
  35. if (length == 0)
  36. {
  37. return string.Empty;
  38. }
  39. StringBuilder buffer = new StringBuilder((int)Math.Ceiling(length / 3d) * 4);
  40. int remainder = length % 3;
  41. length -= remainder;
  42. int block;
  43. int i = 0;
  44. while (i < length)
  45. {
  46. block = ((bytes[i++] & unchecked(0xff)) << 16) | ((bytes[i++] & unchecked(
  47. 0xff)) << 8) | (bytes[i++] & unchecked(0xff));
  48. buffer.Append(Alphabet[(int)(((uint)block) >> 18)]);
  49. buffer.Append(Alphabet[((int)(((uint)block) >> 12)) & unchecked(0x3f)]);
  50. buffer.Append(Alphabet[((int)(((uint)block) >> 6)) & unchecked(0x3f)]);
  51. buffer.Append(Alphabet[block & unchecked(0x3f)]);
  52. }
  53. if (remainder == 0)
  54. {
  55. return buffer.ToString();
  56. }
  57. if (remainder == 1)
  58. {
  59. block = (bytes[i] & unchecked(0xff)) << 4;
  60. buffer.Append(Alphabet[(int)(((uint)block) >> 6)]);
  61. buffer.Append(Alphabet[block & unchecked(0x3f)]);
  62. buffer.Append("==");
  63. return buffer.ToString();
  64. }
  65. block = (((bytes[i++] & unchecked(0xff)) << 8) | ((bytes[i]) & unchecked(0xff))) << 2;
  66. buffer.Append(Alphabet[(int)(((uint)block) >> 12)]);
  67. buffer.Append(Alphabet[((int)(((uint)block) >> 6)) & unchecked(0x3f)]);
  68. buffer.Append(Alphabet[block & unchecked(0x3f)]);
  69. buffer.Append("=");
  70. return buffer.ToString();
  71. }
  72. /// <summary>Decodes the supplied Base-64 encoded string.</summary>
  73. /// <remarks>Decodes the supplied Base-64 encoded string.</remarks>
  74. /// <param name="string">The Base-64 encoded string that is to be decoded.</param>
  75. /// <returns>A <code>byte[]</code> containing the decoded data block.</returns>
  76. public static byte[] Decode(string @string)
  77. {
  78. int length = @string.Length;
  79. if (length == 0)
  80. {
  81. return new byte[0];
  82. }
  83. int pad = (@string[length - 2] == '=') ? 2 : (@string[length - 1] == '=') ? 1 : 0;
  84. int size = length * 3 / 4 - pad;
  85. byte[] buffer = new byte[size];
  86. int block;
  87. int i = 0;
  88. int index = 0;
  89. while (i < length)
  90. {
  91. block = (Alphabet.IndexOf(@string[i++]) & unchecked(0xff)) << 18
  92. | (Alphabet.IndexOf(@string[i++]) & unchecked(0xff)) << 12
  93. | (Alphabet.IndexOf(@string[i++]) & unchecked(0xff)) << 6
  94. | (Alphabet.IndexOf(@string[i++]) & unchecked(0xff));
  95. buffer[index++] = unchecked((byte)((int)(((uint)block) >> 16)));
  96. if (index < size)
  97. {
  98. buffer[index++] = unchecked(
  99. (byte)(
  100. ((int)(((uint)block) >> 8)) & unchecked(0xff))
  101. );
  102. }
  103. if (index < size)
  104. {
  105. buffer[index++] = unchecked((byte)(block & unchecked(0xff)));
  106. }
  107. }
  108. return buffer;
  109. }
  110. }
  111. }