DefiniteLengthInputStream.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  6. namespace BestHTTP.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(true);
  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 == 0)
  33. return -1;
  34. int b = _in.ReadByte();
  35. if (b < 0)
  36. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  37. if (--_remaining == 0)
  38. {
  39. SetParentEofDetect(true);
  40. }
  41. return b;
  42. }
  43. public override int Read(
  44. byte[] buf,
  45. int off,
  46. int len)
  47. {
  48. if (_remaining == 0)
  49. return 0;
  50. int toRead = System.Math.Min(len, _remaining);
  51. int numRead = _in.Read(buf, off, toRead);
  52. if (numRead < 1)
  53. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  54. if ((_remaining -= numRead) == 0)
  55. {
  56. SetParentEofDetect(true);
  57. }
  58. return numRead;
  59. }
  60. internal void ReadAllIntoByteArray(byte[] buf)
  61. {
  62. if (_remaining != buf.Length)
  63. throw new ArgumentException("buffer length not right for data");
  64. if (_remaining == 0)
  65. return;
  66. // make sure it's safe to do this!
  67. int limit = Limit;
  68. if (_remaining >= limit)
  69. throw new IOException("corrupted stream - out of bounds length found: " + _remaining + " >= " + limit);
  70. if ((_remaining -= Streams.ReadFully(_in, buf)) != 0)
  71. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  72. SetParentEofDetect(true);
  73. }
  74. internal byte[] ToArray()
  75. {
  76. if (_remaining == 0)
  77. return EmptyBytes;
  78. // make sure it's safe to do this!
  79. int limit = Limit;
  80. if (_remaining >= limit)
  81. throw new IOException("corrupted stream - out of bounds length found: " + _remaining + " >= " + limit);
  82. byte[] bytes = new byte[_remaining];
  83. if ((_remaining -= Streams.ReadFully(_in, bytes)) != 0)
  84. throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
  85. SetParentEofDetect(true);
  86. return bytes;
  87. }
  88. }
  89. }
  90. #pragma warning restore
  91. #endif