123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.IO;
- namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
- {
- class IndefiniteLengthInputStream
- : LimitedInputStream
- {
- private int _lookAhead;
- private bool _eofOn00 = true;
- internal IndefiniteLengthInputStream(Stream inStream, int limit)
- : base(inStream, limit)
- {
- _lookAhead = RequireByte();
- if (0 == _lookAhead)
- {
- CheckEndOfContents();
- }
- }
- internal void SetEofOn00(bool eofOn00)
- {
- _eofOn00 = eofOn00;
- if (_eofOn00 && 0 == _lookAhead)
- {
- CheckEndOfContents();
- }
- }
- private void CheckEndOfContents()
- {
- if (0 != RequireByte())
- throw new IOException("malformed end-of-contents marker");
- _lookAhead = -1;
- SetParentEofDetect();
- }
- public override int Read(byte[] buffer, int offset, int count)
- {
- // Only use this optimisation if we aren't checking for 00
- if (_eofOn00 || count <= 1)
- return base.Read(buffer, offset, count);
- if (_lookAhead < 0)
- return 0;
- int numRead = _in.Read(buffer, offset + 1, count - 1);
- if (numRead <= 0)
- throw new EndOfStreamException();
- buffer[offset] = (byte)_lookAhead;
- _lookAhead = RequireByte();
- return numRead + 1;
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- public override int Read(Span<byte> buffer)
- {
- // Only use this optimisation if we aren't checking for 00
- if (_eofOn00 || buffer.Length <= 1)
- return base.Read(buffer);
- if (_lookAhead < 0)
- return 0;
- int numRead = _in.Read(buffer[1..]);
- if (numRead <= 0)
- throw new EndOfStreamException();
- buffer[0] = (byte)_lookAhead;
- _lookAhead = RequireByte();
- return numRead + 1;
- }
- #endif
- public override int ReadByte()
- {
- if (_eofOn00 && _lookAhead <= 0)
- {
- if (0 == _lookAhead)
- {
- CheckEndOfContents();
- }
- return -1;
- }
- int result = _lookAhead;
- _lookAhead = RequireByte();
- return result;
- }
- private int RequireByte()
- {
- int b = _in.ReadByte();
- if (b < 0)
- throw new EndOfStreamException();
- return b;
- }
- }
- }
- #pragma warning restore
- #endif
|