BufferedDecoder.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders
  5. {
  6. /// <summary>
  7. /// A buffering class to allow translation from one format to another to
  8. /// be done in discrete chunks.
  9. /// </summary>
  10. public class BufferedDecoder
  11. {
  12. internal byte[] buffer;
  13. internal int bufOff;
  14. internal ITranslator translator;
  15. /// <summary>
  16. /// Create a buffered Decoder.
  17. /// </summary>
  18. /// <param name="translator">The translater to use.</param>
  19. /// <param name="bufferSize">The size of the buffer.</param>
  20. public BufferedDecoder(
  21. ITranslator translator,
  22. int bufferSize)
  23. {
  24. this.translator = translator;
  25. if ((bufferSize % translator.GetEncodedBlockSize()) != 0)
  26. {
  27. throw new ArgumentException("buffer size not multiple of input block size");
  28. }
  29. buffer = new byte[bufferSize];
  30. // bufOff = 0;
  31. }
  32. /// <summary>
  33. /// Process one byte of data.
  34. /// </summary>
  35. /// <param name="input">Data in.</param>
  36. /// <param name="output">Byte array for the output.</param>
  37. /// <param name="outOff">The offset in the output byte array to start writing from.</param>
  38. /// <returns>The amount of output bytes.</returns>
  39. public int ProcessByte(
  40. byte input,
  41. byte[] output,
  42. int outOff)
  43. {
  44. int resultLen = 0;
  45. buffer[bufOff++] = input;
  46. if (bufOff == buffer.Length)
  47. {
  48. resultLen = translator.Decode(buffer, 0, buffer.Length, output, outOff);
  49. bufOff = 0;
  50. }
  51. return resultLen;
  52. }
  53. /// <summary>
  54. /// Process data from a byte array.
  55. /// </summary>
  56. /// <param name="input">The input data.</param>
  57. /// <param name="inOff">Start position within input data array.</param>
  58. /// <param name="len">Amount of data to process from input data array.</param>
  59. /// <param name="outBytes">Array to store output.</param>
  60. /// <param name="outOff">Position in output array to start writing from.</param>
  61. /// <returns>The amount of output bytes.</returns>
  62. public int ProcessBytes(
  63. byte[] input,
  64. int inOff,
  65. int len,
  66. byte[] outBytes,
  67. int outOff)
  68. {
  69. if (len < 0)
  70. {
  71. throw new ArgumentException("Can't have a negative input length!");
  72. }
  73. int resultLen = 0;
  74. int gapLen = buffer.Length - bufOff;
  75. if (len > gapLen)
  76. {
  77. Array.Copy(input, inOff, buffer, bufOff, gapLen);
  78. resultLen += translator.Decode(buffer, 0, buffer.Length, outBytes, outOff);
  79. bufOff = 0;
  80. len -= gapLen;
  81. inOff += gapLen;
  82. outOff += resultLen;
  83. int chunkSize = len - (len % buffer.Length);
  84. resultLen += translator.Decode(input, inOff, chunkSize, outBytes, outOff);
  85. len -= chunkSize;
  86. inOff += chunkSize;
  87. }
  88. if (len != 0)
  89. {
  90. Array.Copy(input, inOff, buffer, bufOff, len);
  91. bufOff += len;
  92. }
  93. return resultLen;
  94. }
  95. }
  96. }
  97. #pragma warning restore
  98. #endif