Base64Encoder.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders
  6. {
  7. public class Base64Encoder
  8. : IEncoder
  9. {
  10. protected readonly byte[] encodingTable =
  11. {
  12. (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
  13. (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
  14. (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
  15. (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
  16. (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
  17. (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
  18. (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
  19. (byte)'v',
  20. (byte)'w', (byte)'x', (byte)'y', (byte)'z',
  21. (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6',
  22. (byte)'7', (byte)'8', (byte)'9',
  23. (byte)'+', (byte)'/'
  24. };
  25. protected byte padding = (byte)'=';
  26. /*
  27. * set up the decoding table.
  28. */
  29. protected readonly byte[] decodingTable = new byte[128];
  30. protected void InitialiseDecodingTable()
  31. {
  32. Arrays.Fill(decodingTable, (byte)0xff);
  33. for (int i = 0; i < encodingTable.Length; i++)
  34. {
  35. decodingTable[encodingTable[i]] = (byte)i;
  36. }
  37. }
  38. public Base64Encoder()
  39. {
  40. InitialiseDecodingTable();
  41. }
  42. /**
  43. * encode the input data producing a base 64 output stream.
  44. *
  45. * @return the number of bytes produced.
  46. */
  47. public int Encode(
  48. byte[] data,
  49. int off,
  50. int length,
  51. Stream outStream)
  52. {
  53. int modulus = length % 3;
  54. int dataLength = (length - modulus);
  55. int a1, a2, a3;
  56. for (int i = off; i < off + dataLength; i += 3)
  57. {
  58. a1 = data[i] & 0xff;
  59. a2 = data[i + 1] & 0xff;
  60. a3 = data[i + 2] & 0xff;
  61. outStream.WriteByte(encodingTable[(int) ((uint) a1 >> 2) & 0x3f]);
  62. outStream.WriteByte(encodingTable[((a1 << 4) | (int) ((uint) a2 >> 4)) & 0x3f]);
  63. outStream.WriteByte(encodingTable[((a2 << 2) | (int) ((uint) a3 >> 6)) & 0x3f]);
  64. outStream.WriteByte(encodingTable[a3 & 0x3f]);
  65. }
  66. /*
  67. * process the tail end.
  68. */
  69. int b1, b2, b3;
  70. int d1, d2;
  71. switch (modulus)
  72. {
  73. case 0: /* nothing left to do */
  74. break;
  75. case 1:
  76. d1 = data[off + dataLength] & 0xff;
  77. b1 = (d1 >> 2) & 0x3f;
  78. b2 = (d1 << 4) & 0x3f;
  79. outStream.WriteByte(encodingTable[b1]);
  80. outStream.WriteByte(encodingTable[b2]);
  81. outStream.WriteByte(padding);
  82. outStream.WriteByte(padding);
  83. break;
  84. case 2:
  85. d1 = data[off + dataLength] & 0xff;
  86. d2 = data[off + dataLength + 1] & 0xff;
  87. b1 = (d1 >> 2) & 0x3f;
  88. b2 = ((d1 << 4) | (d2 >> 4)) & 0x3f;
  89. b3 = (d2 << 2) & 0x3f;
  90. outStream.WriteByte(encodingTable[b1]);
  91. outStream.WriteByte(encodingTable[b2]);
  92. outStream.WriteByte(encodingTable[b3]);
  93. outStream.WriteByte(padding);
  94. break;
  95. }
  96. return (dataLength / 3) * 4 + ((modulus == 0) ? 0 : 4);
  97. }
  98. private bool ignore(
  99. char c)
  100. {
  101. return (c == '\n' || c =='\r' || c == '\t' || c == ' ');
  102. }
  103. /**
  104. * decode the base 64 encoded byte data writing it to the given output stream,
  105. * whitespace characters will be ignored.
  106. *
  107. * @return the number of bytes produced.
  108. */
  109. public int Decode(
  110. byte[] data,
  111. int off,
  112. int length,
  113. Stream outStream)
  114. {
  115. byte b1, b2, b3, b4;
  116. int outLen = 0;
  117. int end = off + length;
  118. while (end > off)
  119. {
  120. if (!ignore((char)data[end - 1]))
  121. {
  122. break;
  123. }
  124. end--;
  125. }
  126. int i = off;
  127. int finish = end - 4;
  128. i = nextI(data, i, finish);
  129. while (i < finish)
  130. {
  131. b1 = decodingTable[data[i++]];
  132. i = nextI(data, i, finish);
  133. b2 = decodingTable[data[i++]];
  134. i = nextI(data, i, finish);
  135. b3 = decodingTable[data[i++]];
  136. i = nextI(data, i, finish);
  137. b4 = decodingTable[data[i++]];
  138. if ((b1 | b2 | b3 | b4) >= 0x80)
  139. throw new IOException("invalid characters encountered in base64 data");
  140. outStream.WriteByte((byte)((b1 << 2) | (b2 >> 4)));
  141. outStream.WriteByte((byte)((b2 << 4) | (b3 >> 2)));
  142. outStream.WriteByte((byte)((b3 << 6) | b4));
  143. outLen += 3;
  144. i = nextI(data, i, finish);
  145. }
  146. outLen += decodeLastBlock(outStream, (char)data[end - 4], (char)data[end - 3], (char)data[end - 2], (char)data[end - 1]);
  147. return outLen;
  148. }
  149. private int nextI(
  150. byte[] data,
  151. int i,
  152. int finish)
  153. {
  154. while ((i < finish) && ignore((char)data[i]))
  155. {
  156. i++;
  157. }
  158. return i;
  159. }
  160. /**
  161. * decode the base 64 encoded string data writing it to the given output stream,
  162. * whitespace characters will be ignored.
  163. *
  164. * @return the number of bytes produced.
  165. */
  166. public int DecodeString(
  167. string data,
  168. Stream outStream)
  169. {
  170. // Platform Implementation
  171. // byte[] bytes = Convert.FromBase64String(data);
  172. // outStream.Write(bytes, 0, bytes.Length);
  173. // return bytes.Length;
  174. byte b1, b2, b3, b4;
  175. int length = 0;
  176. int end = data.Length;
  177. while (end > 0)
  178. {
  179. if (!ignore(data[end - 1]))
  180. {
  181. break;
  182. }
  183. end--;
  184. }
  185. int i = 0;
  186. int finish = end - 4;
  187. i = nextI(data, i, finish);
  188. while (i < finish)
  189. {
  190. b1 = decodingTable[data[i++]];
  191. i = nextI(data, i, finish);
  192. b2 = decodingTable[data[i++]];
  193. i = nextI(data, i, finish);
  194. b3 = decodingTable[data[i++]];
  195. i = nextI(data, i, finish);
  196. b4 = decodingTable[data[i++]];
  197. if ((b1 | b2 | b3 | b4) >= 0x80)
  198. throw new IOException("invalid characters encountered in base64 data");
  199. outStream.WriteByte((byte)((b1 << 2) | (b2 >> 4)));
  200. outStream.WriteByte((byte)((b2 << 4) | (b3 >> 2)));
  201. outStream.WriteByte((byte)((b3 << 6) | b4));
  202. length += 3;
  203. i = nextI(data, i, finish);
  204. }
  205. length += decodeLastBlock(outStream, data[end - 4], data[end - 3], data[end - 2], data[end - 1]);
  206. return length;
  207. }
  208. private int decodeLastBlock(
  209. Stream outStream,
  210. char c1,
  211. char c2,
  212. char c3,
  213. char c4)
  214. {
  215. if (c3 == padding)
  216. {
  217. if (c4 != padding)
  218. throw new IOException("invalid characters encountered at end of base64 data");
  219. byte b1 = decodingTable[c1];
  220. byte b2 = decodingTable[c2];
  221. if ((b1 | b2) >= 0x80)
  222. throw new IOException("invalid characters encountered at end of base64 data");
  223. outStream.WriteByte((byte)((b1 << 2) | (b2 >> 4)));
  224. return 1;
  225. }
  226. if (c4 == padding)
  227. {
  228. byte b1 = decodingTable[c1];
  229. byte b2 = decodingTable[c2];
  230. byte b3 = decodingTable[c3];
  231. if ((b1 | b2 | b3) >= 0x80)
  232. throw new IOException("invalid characters encountered at end of base64 data");
  233. outStream.WriteByte((byte)((b1 << 2) | (b2 >> 4)));
  234. outStream.WriteByte((byte)((b2 << 4) | (b3 >> 2)));
  235. return 2;
  236. }
  237. {
  238. byte b1 = decodingTable[c1];
  239. byte b2 = decodingTable[c2];
  240. byte b3 = decodingTable[c3];
  241. byte b4 = decodingTable[c4];
  242. if ((b1 | b2 | b3 | b4) >= 0x80)
  243. throw new IOException("invalid characters encountered at end of base64 data");
  244. outStream.WriteByte((byte)((b1 << 2) | (b2 >> 4)));
  245. outStream.WriteByte((byte)((b2 << 4) | (b3 >> 2)));
  246. outStream.WriteByte((byte)((b3 << 6) | b4));
  247. return 3;
  248. }
  249. }
  250. private int nextI(string data, int i, int finish)
  251. {
  252. while ((i < finish) && ignore(data[i]))
  253. {
  254. i++;
  255. }
  256. return i;
  257. }
  258. }
  259. }
  260. #pragma warning restore
  261. #endif