HexEncoder.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  4. using System;
  5. using System.IO;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders
  7. {
  8. public class HexEncoder
  9. : IEncoder
  10. {
  11. protected readonly byte[] encodingTable =
  12. {
  13. (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
  14. (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
  15. };
  16. /*
  17. * set up the decoding table.
  18. */
  19. protected readonly byte[] decodingTable = new byte[128];
  20. protected void InitialiseDecodingTable()
  21. {
  22. Arrays.Fill(decodingTable, (byte)0xff);
  23. for (int i = 0; i < encodingTable.Length; i++)
  24. {
  25. decodingTable[encodingTable[i]] = (byte)i;
  26. }
  27. decodingTable['A'] = decodingTable['a'];
  28. decodingTable['B'] = decodingTable['b'];
  29. decodingTable['C'] = decodingTable['c'];
  30. decodingTable['D'] = decodingTable['d'];
  31. decodingTable['E'] = decodingTable['e'];
  32. decodingTable['F'] = decodingTable['f'];
  33. }
  34. public HexEncoder()
  35. {
  36. InitialiseDecodingTable();
  37. }
  38. public int Encode(byte[] inBuf, int inOff, int inLen, byte[] outBuf, int outOff)
  39. {
  40. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  41. return Encode(inBuf.AsSpan(inOff, inLen), outBuf.AsSpan(outOff));
  42. #else
  43. int inPos = inOff;
  44. int inEnd = inOff + inLen;
  45. int outPos = outOff;
  46. while (inPos < inEnd)
  47. {
  48. uint b = inBuf[inPos++];
  49. outBuf[outPos++] = encodingTable[b >> 4];
  50. outBuf[outPos++] = encodingTable[b & 0xF];
  51. }
  52. return outPos - outOff;
  53. #endif
  54. }
  55. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  56. public int Encode(ReadOnlySpan<byte> input, Span<byte> output)
  57. {
  58. int inPos = 0;
  59. int inEnd = input.Length;
  60. int outPos = 0;
  61. while (inPos < inEnd)
  62. {
  63. uint b = input[inPos++];
  64. output[outPos++] = encodingTable[b >> 4];
  65. output[outPos++] = encodingTable[b & 0xF];
  66. }
  67. return outPos;
  68. }
  69. #endif
  70. /**
  71. * encode the input data producing a Hex output stream.
  72. *
  73. * @return the number of bytes produced.
  74. */
  75. public int Encode(byte[] buf, int off, int len, Stream outStream)
  76. {
  77. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  78. return Encode(buf.AsSpan(off, len), outStream);
  79. #else
  80. if (len < 0)
  81. return 0;
  82. byte[] tmp = new byte[72];
  83. int remaining = len;
  84. while (remaining > 0)
  85. {
  86. int inLen = System.Math.Min(36, remaining);
  87. int outLen = Encode(buf, off, inLen, tmp, 0);
  88. outStream.Write(tmp, 0, outLen);
  89. off += inLen;
  90. remaining -= inLen;
  91. }
  92. return len * 2;
  93. #endif
  94. }
  95. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  96. public int Encode(ReadOnlySpan<byte> data, Stream outStream)
  97. {
  98. Span<byte> tmp = stackalloc byte[72];
  99. int result = data.Length * 2;
  100. while (!data.IsEmpty)
  101. {
  102. int inLen = System.Math.Min(36, data.Length);
  103. int outLen = Encode(data[..inLen], tmp);
  104. outStream.Write(tmp[..outLen]);
  105. data = data[inLen..];
  106. }
  107. return result;
  108. }
  109. #endif
  110. private static bool Ignore(char c)
  111. {
  112. return c == '\n' || c =='\r' || c == '\t' || c == ' ';
  113. }
  114. /**
  115. * decode the Hex encoded byte data writing it to the given output stream,
  116. * whitespace characters will be ignored.
  117. *
  118. * @return the number of bytes produced.
  119. */
  120. public int Decode(byte[] data, int off, int length, Stream outStream)
  121. {
  122. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  123. return Decode(data.AsSpan(off, length), outStream);
  124. #else
  125. byte b1, b2;
  126. int outLen = 0;
  127. byte[] buf = new byte[36];
  128. int bufOff = 0;
  129. int end = off + length;
  130. while (end > off)
  131. {
  132. if (!Ignore((char)data[end - 1]))
  133. break;
  134. end--;
  135. }
  136. int i = off;
  137. while (i < end)
  138. {
  139. while (i < end && Ignore((char)data[i]))
  140. {
  141. i++;
  142. }
  143. b1 = decodingTable[data[i++]];
  144. while (i < end && Ignore((char)data[i]))
  145. {
  146. i++;
  147. }
  148. b2 = decodingTable[data[i++]];
  149. if ((b1 | b2) >= 0x80)
  150. throw new IOException("invalid characters encountered in Hex data");
  151. buf[bufOff++] = (byte)((b1 << 4) | b2);
  152. if (bufOff == buf.Length)
  153. {
  154. outStream.Write(buf, 0, bufOff);
  155. bufOff = 0;
  156. }
  157. outLen++;
  158. }
  159. if (bufOff > 0)
  160. {
  161. outStream.Write(buf, 0, bufOff);
  162. }
  163. return outLen;
  164. #endif
  165. }
  166. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  167. public int Decode(ReadOnlySpan<byte> data, Stream outStream)
  168. {
  169. byte b1, b2;
  170. int outLen = 0;
  171. Span<byte> buf = stackalloc byte[36];
  172. int bufOff = 0;
  173. int end = data.Length;
  174. while (end > 0)
  175. {
  176. if (!Ignore((char)data[end - 1]))
  177. break;
  178. end--;
  179. }
  180. int i = 0;
  181. while (i < end)
  182. {
  183. while (i < end && Ignore((char)data[i]))
  184. {
  185. i++;
  186. }
  187. b1 = decodingTable[data[i++]];
  188. while (i < end && Ignore((char)data[i]))
  189. {
  190. i++;
  191. }
  192. b2 = decodingTable[data[i++]];
  193. if ((b1 | b2) >= 0x80)
  194. throw new IOException("invalid characters encountered in Hex data");
  195. buf[bufOff++] = (byte)((b1 << 4) | b2);
  196. if (bufOff == buf.Length)
  197. {
  198. outStream.Write(buf);
  199. bufOff = 0;
  200. }
  201. outLen++;
  202. }
  203. if (bufOff > 0)
  204. {
  205. outStream.Write(buf[..bufOff]);
  206. }
  207. return outLen;
  208. }
  209. #endif
  210. /**
  211. * decode the Hex encoded string data writing it to the given output stream,
  212. * whitespace characters will be ignored.
  213. *
  214. * @return the number of bytes produced.
  215. */
  216. public int DecodeString(string data, Stream outStream)
  217. {
  218. byte b1, b2;
  219. int length = 0;
  220. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  221. Span<byte> buf = stackalloc byte[36];
  222. #else
  223. byte[] buf = new byte[36];
  224. #endif
  225. int bufOff = 0;
  226. int end = data.Length;
  227. while (end > 0)
  228. {
  229. if (!Ignore(data[end - 1]))
  230. break;
  231. end--;
  232. }
  233. int i = 0;
  234. while (i < end)
  235. {
  236. while (i < end && Ignore(data[i]))
  237. {
  238. i++;
  239. }
  240. b1 = decodingTable[data[i++]];
  241. while (i < end && Ignore(data[i]))
  242. {
  243. i++;
  244. }
  245. b2 = decodingTable[data[i++]];
  246. if ((b1 | b2) >= 0x80)
  247. throw new IOException("invalid characters encountered in Hex data");
  248. buf[bufOff++] = (byte)((b1 << 4) | b2);
  249. if (bufOff == buf.Length)
  250. {
  251. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  252. outStream.Write(buf);
  253. #else
  254. outStream.Write(buf, 0, bufOff);
  255. #endif
  256. bufOff = 0;
  257. }
  258. length++;
  259. }
  260. if (bufOff > 0)
  261. {
  262. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  263. outStream.Write(buf[..bufOff]);
  264. #else
  265. outStream.Write(buf, 0, bufOff);
  266. #endif
  267. }
  268. return length;
  269. }
  270. internal byte[] DecodeStrict(string str, int off, int len)
  271. {
  272. if (null == str)
  273. throw new ArgumentNullException("str");
  274. if (off < 0 || len < 0 || off > (str.Length - len))
  275. throw new IndexOutOfRangeException("invalid offset and/or length specified");
  276. if (0 != (len & 1))
  277. throw new ArgumentException("a hexadecimal encoding must have an even number of characters", "len");
  278. int resultLen = len >> 1;
  279. byte[] result = new byte[resultLen];
  280. int strPos = off;
  281. for (int i = 0; i < resultLen; ++i)
  282. {
  283. byte b1 = decodingTable[str[strPos++]];
  284. byte b2 = decodingTable[str[strPos++]];
  285. if ((b1 | b2) >= 0x80)
  286. throw new IOException("invalid characters encountered in Hex data");
  287. result[i] = (byte)((b1 << 4) | b2);
  288. }
  289. return result;
  290. }
  291. }
  292. }
  293. #pragma warning restore
  294. #endif