summary refs log tree commit diff
path: root/crypto/src/tls/ByteQueue.cs
blob: e06ad6346ae6b897101408e6b3a3128ef436958f (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.IO;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Tls
{
    /// <summary>A queue for bytes. This file could be more optimized.</summary>
    public sealed class ByteQueue
    {
        /// <returns>The smallest number which can be written as 2^x which is bigger than i.</returns>
        private static int GetAllocationSize(int i)
        {
            return Integers.HighestOneBit((256 | i) << 1);
        }

        /// <summary>The buffer where we store our data.</summary>
        private byte[] m_databuf;

        /// <summary>How many bytes at the beginning of the buffer are skipped.</summary>
        private int m_skipped = 0;

        /// <summary>How many bytes in the buffer are valid data.</summary>
        private int m_available = 0;

        private bool m_readOnlyBuf = false;

        public ByteQueue()
            : this(0)
        {
        }

        public ByteQueue(int capacity)
        {
            this.m_databuf = capacity == 0 ? TlsUtilities.EmptyBytes : new byte[capacity];
        }

        public ByteQueue(byte[] buf, int off, int len)
        {
            this.m_databuf = buf;
            this.m_skipped = off;
            this.m_available = len;
            this.m_readOnlyBuf = true;
        }

        /// <summary>Add some data to our buffer.</summary>
        /// <param name="buf">A byte-array to read data from.</param>
        /// <param name="off">How many bytes to skip at the beginning of the array.</param>
        /// <param name="len">How many bytes to read from the array.</param>
        public void AddData(byte[] buf, int off, int len)
        {
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
            AddData(buf.AsSpan(off, len));
#else
            if (m_readOnlyBuf)
                throw new InvalidOperationException("Cannot add data to read-only buffer");

            if (m_available == 0)
            {
                if (len > m_databuf.Length)
                {
                    int desiredSize = GetAllocationSize(len);
                    m_databuf = new byte[desiredSize];
                }
                m_skipped = 0;
            }
            else if ((m_skipped + m_available + len) > m_databuf.Length)
            {
                int desiredSize = GetAllocationSize(m_available + len);
                if (desiredSize > m_databuf.Length)
                {
                    byte[] tmp = new byte[desiredSize];
                    Array.Copy(m_databuf, m_skipped, tmp, 0, m_available);
                    m_databuf = tmp;
                }
                else
                {
                    Array.Copy(m_databuf, m_skipped, m_databuf, 0, m_available);
                }
                m_skipped = 0;
            }

            Array.Copy(buf, off, m_databuf, m_skipped + m_available, len);
            m_available += len;
#endif
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public void AddData(ReadOnlySpan<byte> buffer)
        {
            if (m_readOnlyBuf)
                throw new InvalidOperationException("Cannot add data to read-only buffer");

            int len = buffer.Length;
            if (m_available == 0)
            {
                if (len > m_databuf.Length)
                {
                    int desiredSize = GetAllocationSize(len);
                    m_databuf = new byte[desiredSize];
                }
                m_skipped = 0;
            }
            else if ((m_skipped + m_available + len) > m_databuf.Length)
            {
                int desiredSize = GetAllocationSize(m_available + len);
                if (desiredSize > m_databuf.Length)
                {
                    byte[] tmp = new byte[desiredSize];
                    Array.Copy(m_databuf, m_skipped, tmp, 0, m_available);
                    m_databuf = tmp;
                }
                else
                {
                    Array.Copy(m_databuf, m_skipped, m_databuf, 0, m_available);
                }
                m_skipped = 0;
            }

            buffer.CopyTo(m_databuf.AsSpan(m_skipped + m_available));
            m_available += len;
        }
#endif

        /// <returns>The number of bytes which are available in this buffer.</returns>
        public int Available
        {
            get { return m_available; }
        }

        /// <summary>Copy some bytes from the beginning of the data to the provided <see cref="Stream"/>.</summary>
        /// <param name="output">The <see cref="Stream"/> to copy the bytes to.</param>
        /// <param name="length">How many bytes to copy.</param>
        public void CopyTo(Stream output, int length)
        {
            if (length > m_available)
                throw new InvalidOperationException("Cannot copy " + length + " bytes, only got " + m_available);

            output.Write(m_databuf, m_skipped, length);
        }

        /// <summary>Read data from the buffer.</summary>
        /// <param name="buf">The buffer where the read data will be copied to.</param>
        /// <param name="offset">How many bytes to skip at the beginning of buf.</param>
        /// <param name="len">How many bytes to read at all.</param>
        /// <param name="skip">How many bytes from our data to skip.</param>
        public void Read(byte[] buf, int offset, int len, int skip)
        {
            if ((buf.Length - offset) < len)
            {
                throw new ArgumentException("Buffer size of " + buf.Length
                    + " is too small for a read of " + len + " bytes");
            }
            if ((m_available - skip) < len)
            {
                throw new InvalidOperationException("Not enough data to read");
            }
            Array.Copy(m_databuf, m_skipped + skip, buf, offset, len);
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public void Read(Span<byte> buffer, int skip)
        {
            if ((m_available - skip) < buffer.Length)
                throw new InvalidOperationException("Not enough data to read");

            buffer.CopyFrom(m_databuf.AsSpan(m_skipped + skip));
        }
#endif

        /// <summary>Return a <see cref="HandshakeMessageInput"/> over some bytes at the beginning of the data.
        /// </summary>
        /// <param name="length">How many bytes will be readable.</param>
        /// <returns>A <see cref="HandshakeMessageInput"/> over the data.</returns>
        internal HandshakeMessageInput ReadHandshakeMessage(int length)
        {
            if (length > m_available)
                throw new InvalidOperationException("Cannot read " + length + " bytes, only got " + m_available);

            int position = m_skipped;

            m_available -= length;
            m_skipped += length;

            return new HandshakeMessageInput(m_databuf, position, length);
        }

        public int ReadInt32()
        {
            if (m_available < 4)
                throw new InvalidOperationException("Not enough data to read");

            return TlsUtilities.ReadInt32(m_databuf, m_skipped);
        }

        public int ReadUint16(int skip)
        {
            if (m_available < skip + 2)
                throw new InvalidOperationException("Not enough data to read");

            return TlsUtilities.ReadUint16(m_databuf, m_skipped + skip);
        }

        /// <summary>Remove some bytes from our data from the beginning.</summary>
        /// <param name="i">How many bytes to remove.</param>
        public void RemoveData(int i)
        {
            if (i > m_available)
                throw new InvalidOperationException("Cannot remove " + i + " bytes, only got " + m_available);

            /*
             * Skip the data.
             */
            m_available -= i;
            m_skipped += i;
        }

        /// <summary>Remove data from the buffer.</summary>
        /// <param name="buf">The buffer where the removed data will be copied to.</param>
        /// <param name="off">How many bytes to skip at the beginning of buf.</param>
        /// <param name="len">How many bytes to read at all.</param>
        /// <param name="skip">How many bytes from our data to skip.</param>
        public void RemoveData(byte[] buf, int off, int len, int skip)
        {
            Read(buf, off, len, skip);
            RemoveData(skip + len);
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public void RemoveData(Span<byte> buffer, int skip)
        {
            Read(buffer, skip);
            RemoveData(skip + buffer.Length);
        }
#endif

        public byte[] RemoveData(int len, int skip)
        {
            byte[] buf = new byte[len];
            RemoveData(buf, 0, len, skip);
            return buf;
        }

        public void Shrink()
        {
            if (m_available == 0)
            {
                m_databuf = TlsUtilities.EmptyBytes;
                m_skipped = 0;
            }
            else
            {
                int desiredSize = GetAllocationSize(m_available);
                if (desiredSize < m_databuf.Length)
                {
                    byte[] tmp = new byte[desiredSize];
                    Array.Copy(m_databuf, m_skipped, tmp, 0, m_available);
                    m_databuf = tmp;
                    m_skipped = 0;
                }
            }
        }
    }
}