IndefiniteLengthInputStream.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.IO;
  4. namespace Org.BouncyCastle.Asn1
  5. {
  6. class IndefiniteLengthInputStream
  7. : LimitedInputStream
  8. {
  9. private int _lookAhead;
  10. private bool _eofOn00 = true;
  11. internal IndefiniteLengthInputStream(
  12. Stream inStream,
  13. int limit)
  14. : base(inStream, limit)
  15. {
  16. _lookAhead = RequireByte();
  17. CheckForEof();
  18. }
  19. internal void SetEofOn00(
  20. bool eofOn00)
  21. {
  22. _eofOn00 = eofOn00;
  23. if (_eofOn00)
  24. {
  25. CheckForEof();
  26. }
  27. }
  28. private bool CheckForEof()
  29. {
  30. if (_lookAhead == 0x00)
  31. {
  32. int extra = RequireByte();
  33. if (extra != 0)
  34. {
  35. throw new IOException("malformed end-of-contents marker");
  36. }
  37. _lookAhead = -1;
  38. SetParentEofDetect(true);
  39. return true;
  40. }
  41. return _lookAhead < 0;
  42. }
  43. public override int Read(
  44. byte[] buffer,
  45. int offset,
  46. int count)
  47. {
  48. // Only use this optimisation if we aren't checking for 00
  49. if (_eofOn00 || count <= 1)
  50. return base.Read(buffer, offset, count);
  51. if (_lookAhead < 0)
  52. return 0;
  53. int numRead = _in.Read(buffer, offset + 1, count - 1);
  54. if (numRead <= 0)
  55. {
  56. // Corrupted stream
  57. throw new EndOfStreamException();
  58. }
  59. buffer[offset] = (byte)_lookAhead;
  60. _lookAhead = RequireByte();
  61. return numRead + 1;
  62. }
  63. public override int ReadByte()
  64. {
  65. if (_eofOn00 && CheckForEof())
  66. return -1;
  67. int result = _lookAhead;
  68. _lookAhead = RequireByte();
  69. return result;
  70. }
  71. private int RequireByte()
  72. {
  73. int b = _in.ReadByte();
  74. if (b < 0)
  75. {
  76. // Corrupted stream
  77. throw new EndOfStreamException();
  78. }
  79. return b;
  80. }
  81. }
  82. }
  83. //using System;
  84. //using System.IO;
  85. //namespace Org.BouncyCastle.Asn1
  86. //{
  87. // class IndefiniteLengthInputStream
  88. // : LimitedInputStream
  89. // {
  90. // private bool _eofReached = false;
  91. // private bool _eofOn00 = true;
  92. // internal IndefiniteLengthInputStream(
  93. // Stream inStream,
  94. // int limit)
  95. // : base(inStream, limit)
  96. // {
  97. // }
  98. // internal void SetEofOn00(
  99. // bool eofOn00)
  100. // {
  101. // _eofOn00 = eofOn00;
  102. // }
  103. // public override int Read(
  104. // byte[] buffer,
  105. // int offset,
  106. // int count)
  107. // {
  108. // if (_eofReached)
  109. // return 0;
  110. // if (_eofOn00)
  111. // return base.Read(buffer, offset, count);
  112. // int numRead = _in.Read(buffer, offset, count);
  113. // if (numRead <= 0)
  114. // throw new EndOfStreamException();
  115. // return numRead;
  116. // }
  117. // public override int ReadByte()
  118. // {
  119. // if (_eofReached)
  120. // return -1;
  121. // int b1 = _in.ReadByte();
  122. // if (b1 < 0)
  123. // throw new EndOfStreamException();
  124. // if (b1 == 0 && _eofOn00)
  125. // {
  126. // int b2 = _in.ReadByte();
  127. // if (b2 < 0)
  128. // throw new EndOfStreamException();
  129. // if (b2 == 0)
  130. // {
  131. // _eofReached = true;
  132. // SetParentEofDetect(true);
  133. // return -1;
  134. // }
  135. // throw new InvalidDataException();
  136. // }
  137. // return b1;
  138. // }
  139. // }
  140. //}
  141. #endif