HexEncoder.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /**
  38. * encode the input data producing a Hex output stream.
  39. *
  40. * @return the number of bytes produced.
  41. */
  42. public int Encode(
  43. byte[] data,
  44. int off,
  45. int length,
  46. Stream outStream)
  47. {
  48. for (int i = off; i < (off + length); i++)
  49. {
  50. int v = data[i];
  51. outStream.WriteByte(encodingTable[v >> 4]);
  52. outStream.WriteByte(encodingTable[v & 0xf]);
  53. }
  54. return length * 2;
  55. }
  56. private static bool Ignore(char c)
  57. {
  58. return c == '\n' || c =='\r' || c == '\t' || c == ' ';
  59. }
  60. /**
  61. * decode the Hex encoded byte data writing it to the given output stream,
  62. * whitespace characters will be ignored.
  63. *
  64. * @return the number of bytes produced.
  65. */
  66. public int Decode(
  67. byte[] data,
  68. int off,
  69. int length,
  70. Stream outStream)
  71. {
  72. byte b1, b2;
  73. int outLen = 0;
  74. int end = off + length;
  75. while (end > off)
  76. {
  77. if (!Ignore((char)data[end - 1]))
  78. {
  79. break;
  80. }
  81. end--;
  82. }
  83. int i = off;
  84. while (i < end)
  85. {
  86. while (i < end && Ignore((char)data[i]))
  87. {
  88. i++;
  89. }
  90. b1 = decodingTable[data[i++]];
  91. while (i < end && Ignore((char)data[i]))
  92. {
  93. i++;
  94. }
  95. b2 = decodingTable[data[i++]];
  96. if ((b1 | b2) >= 0x80)
  97. throw new IOException("invalid characters encountered in Hex data");
  98. outStream.WriteByte((byte)((b1 << 4) | b2));
  99. outLen++;
  100. }
  101. return outLen;
  102. }
  103. /**
  104. * decode the Hex encoded string 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 DecodeString(
  110. string data,
  111. Stream outStream)
  112. {
  113. byte b1, b2;
  114. int length = 0;
  115. int end = data.Length;
  116. while (end > 0)
  117. {
  118. if (!Ignore(data[end - 1]))
  119. {
  120. break;
  121. }
  122. end--;
  123. }
  124. int i = 0;
  125. while (i < end)
  126. {
  127. while (i < end && Ignore(data[i]))
  128. {
  129. i++;
  130. }
  131. b1 = decodingTable[data[i++]];
  132. while (i < end && Ignore(data[i]))
  133. {
  134. i++;
  135. }
  136. b2 = decodingTable[data[i++]];
  137. if ((b1 | b2) >= 0x80)
  138. throw new IOException("invalid characters encountered in Hex data");
  139. outStream.WriteByte((byte)((b1 << 4) | b2));
  140. length++;
  141. }
  142. return length;
  143. }
  144. }
  145. }
  146. #pragma warning restore
  147. #endif