summary refs log tree commit diff
path: root/crypto/src/asn1/DefiniteLengthInputStream.cs
blob: 5f2ecbabcd4736cddf62451c5d10b50d309bd860 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.IO;

using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Asn1
{
    internal class DefiniteLengthInputStream
        : LimitedInputStream
    {
		private static readonly byte[] EmptyBytes = new byte[0];

		private readonly int _originalLength;
		private int _remaining;

        internal DefiniteLengthInputStream(Stream inStream, int length, int limit)
            : base(inStream, limit)
        {
            if (length <= 0)
            {
                if (length < 0)
                    throw new ArgumentException("negative lengths not allowed", "length");

                SetParentEofDetect();
            }

            this._originalLength = length;
			this._remaining = length;
        }

        internal int Remaining
		{
			get { return _remaining; }
		}

		public override int ReadByte()
        {
            if (_remaining < 2)
            {
                if (_remaining == 0)
                    return -1;

                int b = _in.ReadByte();
                if (b < 0)
                    throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);

                _remaining = 0;
                SetParentEofDetect();

                return b;
            }
            else
            {
                int b = _in.ReadByte();
                if (b < 0)
                    throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);

                --_remaining;
                return b;
            }
        }

		public override int Read(byte[] buf, int off, int len)
		{
            if (_remaining == 0)
                return 0;

            int toRead = System.Math.Min(len, _remaining);
            int numRead = _in.Read(buf, off, toRead);

            if (numRead < 1)
                throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);

            if ((_remaining -= numRead) == 0)
            {
                SetParentEofDetect();
            }

            return numRead;
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public override int Read(Span<byte> buffer)
        {
            if (_remaining == 0)
                return 0;

            int toRead = System.Math.Min(buffer.Length, _remaining);
            int numRead = _in.Read(buffer[..toRead]);

            if (numRead < 1)
                throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);

            if ((_remaining -= numRead) == 0)
            {
                SetParentEofDetect();
            }

            return numRead;
        }
#endif

        internal void ReadAllIntoByteArray(byte[] buf)
        {
            if (_remaining != buf.Length)
                throw new ArgumentException("buffer length not right for data");

            if (_remaining == 0)
                return;

            // make sure it's safe to do this!
            int limit = Limit;
            if (_remaining >= limit)
                throw new IOException("corrupted stream - out of bounds length found: " + _remaining + " >= " + limit);

            if ((_remaining -= Streams.ReadFully(_in, buf, 0, buf.Length)) != 0)
                throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
            SetParentEofDetect();
        }

        internal byte[] ToArray()
		{
			if (_remaining == 0)
				return EmptyBytes;

            // make sure it's safe to do this!
            int limit = Limit;
            if (_remaining >= limit)
                throw new IOException("corrupted stream - out of bounds length found: " + _remaining + " >= " + limit);

            byte[] bytes = new byte[_remaining];
			if ((_remaining -= Streams.ReadFully(_in, bytes, 0, bytes.Length)) != 0)
				throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
			SetParentEofDetect();
			return bytes;
		}
    }
}