HexEncoder.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 HexEncoder
  8. : IEncoder
  9. {
  10. protected readonly byte[] encodingTable =
  11. {
  12. (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
  13. (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
  14. };
  15. /*
  16. * set up the decoding table.
  17. */
  18. protected readonly byte[] decodingTable = new byte[128];
  19. protected void InitialiseDecodingTable()
  20. {
  21. Arrays.Fill(decodingTable, (byte)0xff);
  22. for (int i = 0; i < encodingTable.Length; i++)
  23. {
  24. decodingTable[encodingTable[i]] = (byte)i;
  25. }
  26. decodingTable['A'] = decodingTable['a'];
  27. decodingTable['B'] = decodingTable['b'];
  28. decodingTable['C'] = decodingTable['c'];
  29. decodingTable['D'] = decodingTable['d'];
  30. decodingTable['E'] = decodingTable['e'];
  31. decodingTable['F'] = decodingTable['f'];
  32. }
  33. public HexEncoder()
  34. {
  35. InitialiseDecodingTable();
  36. }
  37. public int Encode(byte[] inBuf, int inOff, int inLen, byte[] outBuf, int outOff)
  38. {
  39. int inPos = inOff;
  40. int inEnd = inOff + inLen;
  41. int outPos = outOff;
  42. while (inPos < inEnd)
  43. {
  44. uint b = inBuf[inPos++];
  45. outBuf[outPos++] = encodingTable[b >> 4];
  46. outBuf[outPos++] = encodingTable[b & 0xF];
  47. }
  48. return outPos - outOff;
  49. }
  50. /**
  51. * encode the input data producing a Hex output stream.
  52. *
  53. * @return the number of bytes produced.
  54. */
  55. public int Encode(byte[] buf, int off, int len, Stream outStream)
  56. {
  57. if (len < 0)
  58. return 0;
  59. byte[] tmp = new byte[72];
  60. int remaining = len;
  61. while (remaining > 0)
  62. {
  63. int inLen = System.Math.Min(36, remaining);
  64. int outLen = Encode(buf, off, inLen, tmp, 0);
  65. outStream.Write(tmp, 0, outLen);
  66. off += inLen;
  67. remaining -= inLen;
  68. }
  69. return len * 2;
  70. }
  71. private static bool Ignore(char c)
  72. {
  73. return c == '\n' || c =='\r' || c == '\t' || c == ' ';
  74. }
  75. /**
  76. * decode the Hex encoded byte data writing it to the given output stream,
  77. * whitespace characters will be ignored.
  78. *
  79. * @return the number of bytes produced.
  80. */
  81. public int Decode(
  82. byte[] data,
  83. int off,
  84. int length,
  85. Stream outStream)
  86. {
  87. byte b1, b2;
  88. int outLen = 0;
  89. byte[] buf = new byte[36];
  90. int bufOff = 0;
  91. int end = off + length;
  92. while (end > off)
  93. {
  94. if (!Ignore((char)data[end - 1]))
  95. break;
  96. end--;
  97. }
  98. int i = off;
  99. while (i < end)
  100. {
  101. while (i < end && Ignore((char)data[i]))
  102. {
  103. i++;
  104. }
  105. b1 = decodingTable[data[i++]];
  106. while (i < end && Ignore((char)data[i]))
  107. {
  108. i++;
  109. }
  110. b2 = decodingTable[data[i++]];
  111. if ((b1 | b2) >= 0x80)
  112. throw new IOException("invalid characters encountered in Hex data");
  113. buf[bufOff++] = (byte)((b1 << 4) | b2);
  114. if (bufOff == buf.Length)
  115. {
  116. outStream.Write(buf, 0, bufOff);
  117. bufOff = 0;
  118. }
  119. outLen++;
  120. }
  121. if (bufOff > 0)
  122. {
  123. outStream.Write(buf, 0, bufOff);
  124. }
  125. return outLen;
  126. }
  127. /**
  128. * decode the Hex encoded string data writing it to the given output stream,
  129. * whitespace characters will be ignored.
  130. *
  131. * @return the number of bytes produced.
  132. */
  133. public int DecodeString(
  134. string data,
  135. Stream outStream)
  136. {
  137. byte b1, b2;
  138. int length = 0;
  139. byte[] buf = new byte[36];
  140. int bufOff = 0;
  141. int end = data.Length;
  142. while (end > 0)
  143. {
  144. if (!Ignore(data[end - 1]))
  145. break;
  146. end--;
  147. }
  148. int i = 0;
  149. while (i < end)
  150. {
  151. while (i < end && Ignore(data[i]))
  152. {
  153. i++;
  154. }
  155. b1 = decodingTable[data[i++]];
  156. while (i < end && Ignore(data[i]))
  157. {
  158. i++;
  159. }
  160. b2 = decodingTable[data[i++]];
  161. if ((b1 | b2) >= 0x80)
  162. throw new IOException("invalid characters encountered in Hex data");
  163. buf[bufOff++] = (byte)((b1 << 4) | b2);
  164. if (bufOff == buf.Length)
  165. {
  166. outStream.Write(buf, 0, bufOff);
  167. bufOff = 0;
  168. }
  169. length++;
  170. }
  171. if (bufOff > 0)
  172. {
  173. outStream.Write(buf, 0, bufOff);
  174. }
  175. return length;
  176. }
  177. internal byte[] DecodeStrict(string str, int off, int len)
  178. {
  179. if (null == str)
  180. throw new ArgumentNullException("str");
  181. if (off < 0 || len < 0 || off > (str.Length - len))
  182. throw new IndexOutOfRangeException("invalid offset and/or length specified");
  183. if (0 != (len & 1))
  184. throw new ArgumentException("a hexadecimal encoding must have an even number of characters", "len");
  185. int resultLen = len >> 1;
  186. byte[] result = new byte[resultLen];
  187. int strPos = off;
  188. for (int i = 0; i < resultLen; ++i)
  189. {
  190. byte b1 = decodingTable[str[strPos++]];
  191. byte b2 = decodingTable[str[strPos++]];
  192. if ((b1 | b2) >= 0x80)
  193. throw new IOException("invalid characters encountered in Hex data");
  194. result[i] = (byte)((b1 << 4) | b2);
  195. }
  196. return result;
  197. }
  198. }
  199. }
  200. #pragma warning restore
  201. #endif