Base64Encoder.cs 9.5 KB

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