BufferedEncoder.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 class that allows encoding of data using a specific encoder to be processed in chunks.
  8. /// </summary>
  9. public class BufferedEncoder
  10. {
  11. internal byte[] Buffer;
  12. internal int bufOff;
  13. internal ITranslator translator;
  14. /// <summary>
  15. /// Create.
  16. /// </summary>
  17. /// <param name="translator">The translator to use.</param>
  18. /// <param name="bufferSize">Size of the chunks.</param>
  19. public BufferedEncoder(
  20. ITranslator translator,
  21. int bufferSize)
  22. {
  23. this.translator = translator;
  24. if ((bufferSize % translator.GetEncodedBlockSize()) != 0)
  25. {
  26. throw new ArgumentException("buffer size not multiple of input block size");
  27. }
  28. Buffer = new byte[bufferSize];
  29. // bufOff = 0;
  30. }
  31. /// <summary>
  32. /// Process one byte of data.
  33. /// </summary>
  34. /// <param name="input">The byte.</param>
  35. /// <param name="outBytes">An array to store output in.</param>
  36. /// <param name="outOff">Offset within output array to start writing from.</param>
  37. /// <returns></returns>
  38. public int ProcessByte(
  39. byte input,
  40. byte[] outBytes,
  41. int outOff)
  42. {
  43. int resultLen = 0;
  44. Buffer[bufOff++] = input;
  45. if (bufOff == Buffer.Length)
  46. {
  47. resultLen = translator.Encode(Buffer, 0, Buffer.Length, outBytes, outOff);
  48. bufOff = 0;
  49. }
  50. return resultLen;
  51. }
  52. /// <summary>
  53. /// Process data from a byte array.
  54. /// </summary>
  55. /// <param name="input">Input data Byte array containing data to be processed.</param>
  56. /// <param name="inOff">Start position within input data array.</param>
  57. /// <param name="len">Amount of input data to be processed.</param>
  58. /// <param name="outBytes">Output data array.</param>
  59. /// <param name="outOff">Offset within output data array to start writing to.</param>
  60. /// <returns>The amount of data written.</returns>
  61. public int ProcessBytes(
  62. byte[] input,
  63. int inOff,
  64. int len,
  65. byte[] outBytes,
  66. int outOff)
  67. {
  68. if (len < 0)
  69. {
  70. throw new ArgumentException("Can't have a negative input length!");
  71. }
  72. int resultLen = 0;
  73. int gapLen = Buffer.Length - bufOff;
  74. if (len > gapLen)
  75. {
  76. Array.Copy(input, inOff, Buffer, bufOff, gapLen);
  77. resultLen += translator.Encode(Buffer, 0, Buffer.Length, outBytes, outOff);
  78. bufOff = 0;
  79. len -= gapLen;
  80. inOff += gapLen;
  81. outOff += resultLen;
  82. int chunkSize = len - (len % Buffer.Length);
  83. resultLen += translator.Encode(input, inOff, chunkSize, outBytes, outOff);
  84. len -= chunkSize;
  85. inOff += chunkSize;
  86. }
  87. if (len != 0)
  88. {
  89. Array.Copy(input, inOff, Buffer, bufOff, len);
  90. bufOff += len;
  91. }
  92. return resultLen;
  93. }
  94. }
  95. }
  96. #pragma warning restore
  97. #endif