2
0

Base64.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  24. /// <summary>Base-64 encodes the supplied block of data.</summary>
  25. /// <remarks>
  26. /// Base-64 encodes the supplied block of data. Line wrapping is not
  27. /// applied on output.
  28. /// </remarks>
  29. /// <param name="bytes">The block of data that is to be Base-64 encoded.</param>
  30. /// <returns>A <code>String</code> containing the encoded data.</returns>
  31. public static string Encode(byte[] bytes)
  32. {
  33. int length = bytes.Length;
  34. if (length == 0)
  35. {
  36. return string.Empty;
  37. }
  38. StringBuilder buffer = new StringBuilder((int)Math.Ceiling(length / 3d) * 4);
  39. int remainder = length % 3;
  40. length -= remainder;
  41. int block;
  42. int i = 0;
  43. while (i < length)
  44. {
  45. block = ((bytes[i++] & unchecked(0xff)) << 16) | ((bytes[i++] & unchecked(
  46. 0xff)) << 8) | (bytes[i++] & unchecked(0xff));
  47. buffer.Append(Alphabet[(int)(((uint)block) >> 18)]);
  48. buffer.Append(Alphabet[((int)(((uint)block) >> 12)) & unchecked(0x3f)]);
  49. buffer.Append(Alphabet[((int)(((uint)block) >> 6)) & unchecked(0x3f)]);
  50. buffer.Append(Alphabet[block & unchecked(0x3f)]);
  51. }
  52. if (remainder == 0)
  53. {
  54. return buffer.ToString();
  55. }
  56. if (remainder == 1)
  57. {
  58. block = (bytes[i] & unchecked(0xff)) << 4;
  59. buffer.Append(Alphabet[(int)(((uint)block) >> 6)]);
  60. buffer.Append(Alphabet[block & unchecked(0x3f)]);
  61. buffer.Append("==");
  62. return buffer.ToString();
  63. }
  64. block = (((bytes[i++] & unchecked(0xff)) << 8) | ((bytes[i]) & unchecked(0xff))) << 2;
  65. buffer.Append(Alphabet[(int)(((uint)block) >> 12)]);
  66. buffer.Append(Alphabet[((int)(((uint)block) >> 6)) & unchecked(0x3f)]);
  67. buffer.Append(Alphabet[block & unchecked(0x3f)]);
  68. buffer.Append("=");
  69. return buffer.ToString();
  70. }
  71. /// <summary>Decodes the supplied Base-64 encoded string.</summary>
  72. /// <remarks>Decodes the supplied Base-64 encoded string.</remarks>
  73. /// <param name="string">The Base-64 encoded string that is to be decoded.</param>
  74. /// <returns>A <code>byte[]</code> containing the decoded data block.</returns>
  75. public static byte[] Decode(string @string)
  76. {
  77. int length = @string.Length;
  78. if (length == 0)
  79. {
  80. return new byte[0];
  81. }
  82. int pad = (@string[length - 2] == '=') ? 2 : (@string[length - 1] == '=') ? 1 : 0;
  83. int size = length * 3 / 4 - pad;
  84. byte[] buffer = new byte[size];
  85. int block;
  86. int i = 0;
  87. int index = 0;
  88. while (i < length)
  89. {
  90. block = (Alphabet.IndexOf(@string[i++]) & unchecked(0xff)) << 18 | (Alphabet
  91. .IndexOf(@string[i++]) & unchecked(0xff)) << 12 | (Alphabet.IndexOf(@string
  92. [i++]) & unchecked(0xff)) << 6 | (Alphabet.IndexOf(@string[i++]) & unchecked(
  93. 0xff));
  94. buffer[index++] = unchecked((byte)((int)(((uint)block) >> 16)));
  95. if (index < size)
  96. {
  97. buffer[index++] = unchecked((byte)(((int)(((uint)block) >> 8)) & unchecked(0xff)));
  98. }
  99. if (index < size)
  100. {
  101. buffer[index++] = unchecked((byte)(block & unchecked(0xff)));
  102. }
  103. }
  104. return buffer;
  105. }
  106. }
  107. }