summary refs log tree commit diff
path: root/crypto/src/asn1/ConstructedBitStream.cs
blob: f089dac75498fd428c5c22ad0bfefafa94242234 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.IO;

using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Asn1
{
    internal class ConstructedBitStream
        : BaseInputStream
    {
        private readonly Asn1StreamParser m_parser;
        private readonly bool m_octetAligned;

        private bool m_first = true;
        private int m_padBits = 0;

        private Asn1BitStringParser m_currentParser;
        private Stream m_currentStream;

        internal ConstructedBitStream(Asn1StreamParser parser, bool octetAligned)
        {
            m_parser = parser;
            m_octetAligned = octetAligned;
        }

        internal int PadBits
        {
            get { return m_padBits; }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            Streams.ValidateBufferArguments(buffer, offset, count);

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
            return Read(buffer.AsSpan(offset, count));
#else
            if (count < 1)
                return 0;

            if (m_currentStream == null)
            {
                if (!m_first)
                    return 0;

                m_currentParser = GetNextParser();
                if (m_currentParser == null)
                    return 0;

                m_first = false;
                m_currentStream = m_currentParser.GetBitStream();
            }

            int totalRead = 0;

            for (;;)
            {
                int numRead = m_currentStream.Read(buffer, offset + totalRead, count - totalRead);

                if (numRead > 0)
                {
                    totalRead += numRead;

                    if (totalRead == count)
                        return totalRead;
                }
                else
                {
                    m_padBits = m_currentParser.PadBits;
                    m_currentParser = GetNextParser();
                    if (m_currentParser == null)
                    {
                        m_currentStream = null;
                        return totalRead;
                    }

                    m_currentStream = m_currentParser.GetBitStream();
                }
            }
#endif
        }

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

            if (m_currentStream == null)
            {
                if (!m_first)
                    return 0;

                m_currentParser = GetNextParser();
                if (m_currentParser == null)
                    return 0;

                m_first = false;
                m_currentStream = m_currentParser.GetBitStream();
            }

            int totalRead = 0;

            for (;;)
            {
                int numRead = m_currentStream.Read(buffer[totalRead..]);

                if (numRead > 0)
                {
                    totalRead += numRead;

                    if (totalRead == buffer.Length)
                        return totalRead;
                }
                else
                {
                    m_padBits = m_currentParser.PadBits;
                    m_currentParser = GetNextParser();
                    if (m_currentParser == null)
                    {
                        m_currentStream = null;
                        return totalRead;
                    }

                    m_currentStream = m_currentParser.GetBitStream();
                }
            }
        }
#endif

        public override int ReadByte()
        {
            if (m_currentStream == null)
            {
                if (!m_first)
                    return -1;

                m_currentParser = GetNextParser();
                if (m_currentParser == null)
                    return -1;

                m_first = false;
                m_currentStream = m_currentParser.GetBitStream();
            }

            for (;;)
            {
                int b = m_currentStream.ReadByte();

                if (b >= 0)
                    return b;

                m_padBits = m_currentParser.PadBits;
                m_currentParser = GetNextParser();
                if (m_currentParser == null)
                {
                    m_currentStream = null;
                    return -1;
                }

                m_currentStream = m_currentParser.GetBitStream();
            }
        }

        private Asn1BitStringParser GetNextParser()
        {
            IAsn1Convertible asn1Obj = m_parser.ReadObject();
            if (asn1Obj == null)
            {
                if (m_octetAligned && m_padBits != 0)
                    throw new IOException("expected octet-aligned bitstring, but found padBits: " + m_padBits);

                return null;
            }

            if (asn1Obj is Asn1BitStringParser)
            {
                if (m_padBits != 0)
                    throw new IOException("only the last nested bitstring can have padding");

                return (Asn1BitStringParser)asn1Obj;
            }

            throw new IOException("unknown object encountered: " + Platform.GetTypeName(asn1Obj));
        }
    }
}