DefiniteLengthInputStream.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. class DefiniteLengthInputStream
  9. : LimitedInputStream
  10. {
  11. private static readonly byte[] EmptyBytes = new byte[0];
  12. private readonly int _originalLength;
  13. private int _remaining;
  14. internal DefiniteLengthInputStream(Stream inStream, int length, int limit)
  15. : base(inStream, limit)
  16. {
  17. if (length <= 0)
  18. {
  19. if (length < 0)
  20. throw new ArgumentException("negative lengths not allowed", "length");
  21. SetParentEofDetect();
  22. }
  23. this._originalLength = length;
  24. this._remaining = length;
  25. }
  26. internal int Remaining
  27. {
  28. get { return _remaining; }
  29. }
  30. public override int ReadByte()
  31. {
  32. if (_remaining < 2)
  33. {
  34. if (_remaining == 0)
  35. return -1;
  36. int b = _in.ReadByte();
  37. if (b < 0)
  38. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  39. _remaining = 0;
  40. SetParentEofDetect();
  41. return b;
  42. }
  43. else
  44. {
  45. int b = _in.ReadByte();
  46. if (b < 0)
  47. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  48. --_remaining;
  49. return b;
  50. }
  51. }
  52. public override int Read(byte[] buf, int off, int len)
  53. {
  54. if (_remaining == 0)
  55. return 0;
  56. int toRead = System.Math.Min(len, _remaining);
  57. int numRead = _in.Read(buf, off, toRead);
  58. if (numRead < 1)
  59. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  60. if ((_remaining -= numRead) == 0)
  61. {
  62. SetParentEofDetect();
  63. }
  64. return numRead;
  65. }
  66. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  67. public override int Read(Span<byte> buffer)
  68. {
  69. if (_remaining == 0)
  70. return 0;
  71. int toRead = System.Math.Min(buffer.Length, _remaining);
  72. int numRead = _in.Read(buffer[..toRead]);
  73. if (numRead < 1)
  74. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  75. if ((_remaining -= numRead) == 0)
  76. {
  77. SetParentEofDetect();
  78. }
  79. return numRead;
  80. }
  81. #endif
  82. internal void ReadAllIntoByteArray(byte[] buf)
  83. {
  84. if (_remaining != buf.Length)
  85. throw new ArgumentException("buffer length not right for data");
  86. if (_remaining == 0)
  87. return;
  88. // make sure it's safe to do this!
  89. int limit = Limit;
  90. if (_remaining >= limit)
  91. throw new IOException("corrupted stream - out of bounds length found: " + _remaining + " >= " + limit);
  92. if ((_remaining -= Streams.ReadFully(_in, buf, 0, buf.Length)) != 0)
  93. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  94. SetParentEofDetect();
  95. }
  96. internal byte[] ToArray()
  97. {
  98. if (_remaining == 0)
  99. return EmptyBytes;
  100. // make sure it's safe to do this!
  101. int limit = Limit;
  102. if (_remaining >= limit)
  103. throw new IOException("corrupted stream - out of bounds length found: " + _remaining + " >= " + limit);
  104. byte[] bytes = new byte[_remaining];
  105. if ((_remaining -= Streams.ReadFully(_in, bytes, 0, bytes.Length)) != 0)
  106. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  107. SetParentEofDetect();
  108. return bytes;
  109. }
  110. }
  111. }
  112. #pragma warning restore
  113. #endif