IndefiniteLengthInputStream.cs 3.8 KB

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