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
265
266
267
268
269
270
271
272
273
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#endregion
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using System.Security;
namespace Google.Protobuf
{
/// <summary>
/// Abstraction for reading from a stream / read only sequence.
/// Parsing from the buffer is a loop of reading from current buffer / refreshing the buffer once done.
/// </summary>
[SecuritySafeCritical]
internal struct SegmentedBufferHelper
{
private int? totalLength;
private ReadOnlySequence<byte>.Enumerator readOnlySequenceEnumerator;
private CodedInputStream codedInputStream;
/// <summary>
/// Initialize an instance with a coded input stream.
/// This approach is faster than using a constructor because the instance to initialize is passed by reference
/// and we can write directly into it without copying.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Initialize(CodedInputStream codedInputStream, out SegmentedBufferHelper instance)
{
instance.totalLength = codedInputStream.InternalInputStream == null ? (int?)codedInputStream.InternalBuffer.Length : null;
instance.readOnlySequenceEnumerator = default;
instance.codedInputStream = codedInputStream;
}
/// <summary>
/// Initialize an instance with a read only sequence.
/// This approach is faster than using a constructor because the instance to initialize is passed by reference
/// and we can write directly into it without copying.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Initialize(ReadOnlySequence<byte> sequence, out SegmentedBufferHelper instance, out ReadOnlySpan<byte> firstSpan)
{
instance.codedInputStream = null;
if (sequence.IsSingleSegment)
{
firstSpan = sequence.First.Span;
instance.totalLength = firstSpan.Length;
instance.readOnlySequenceEnumerator = default;
}
else
{
instance.readOnlySequenceEnumerator = sequence.GetEnumerator();
instance.totalLength = (int) sequence.Length;
// set firstSpan to the first segment
instance.readOnlySequenceEnumerator.MoveNext();
firstSpan = instance.readOnlySequenceEnumerator.Current.Span;
}
}
public bool RefillBuffer(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state, bool mustSucceed)
{
if (codedInputStream != null)
{
return RefillFromCodedInputStream(ref buffer, ref state, mustSucceed);
}
else
{
return RefillFromReadOnlySequence(ref buffer, ref state, mustSucceed);
}
}
public int? TotalLength => totalLength;
public CodedInputStream CodedInputStream => codedInputStream;
/// <summary>
/// Sets currentLimit to (current position) + byteLimit. This is called
/// when descending into a length-delimited embedded message. The previous
/// limit is returned.
/// </summary>
/// <returns>The old limit.</returns>
public static int PushLimit(ref ParserInternalState state, int byteLimit)
{
if (byteLimit < 0)
{
throw InvalidProtocolBufferException.NegativeSize();
}
byteLimit += state.totalBytesRetired + state.bufferPos;
int oldLimit = state.currentLimit;
if (byteLimit > oldLimit)
{
throw InvalidProtocolBufferException.TruncatedMessage();
}
state.currentLimit = byteLimit;
RecomputeBufferSizeAfterLimit(ref state);
return oldLimit;
}
/// <summary>
/// Discards the current limit, returning the previous limit.
/// </summary>
public static void PopLimit(ref ParserInternalState state, int oldLimit)
{
state.currentLimit = oldLimit;
RecomputeBufferSizeAfterLimit(ref state);
}
/// <summary>
/// Returns whether or not all the data before the limit has been read.
/// </summary>
/// <returns></returns>
public static bool IsReachedLimit(ref ParserInternalState state)
{
if (state.currentLimit == int.MaxValue)
{
return false;
}
int currentAbsolutePosition = state.totalBytesRetired + state.bufferPos;
return currentAbsolutePosition >= state.currentLimit;
}
/// <summary>
/// Returns true if the stream has reached the end of the input. This is the
/// case if either the end of the underlying input source has been reached or
/// the stream has reached a limit created using PushLimit.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAtEnd(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
{
return state.bufferPos == state.bufferSize && !state.segmentedBufferHelper.RefillBuffer(ref buffer, ref state, false);
}
private bool RefillFromReadOnlySequence(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state, bool mustSucceed)
{
CheckCurrentBufferIsEmpty(ref state);
if (state.totalBytesRetired + state.bufferSize == state.currentLimit)
{
// Oops, we hit a limit.
if (mustSucceed)
{
throw InvalidProtocolBufferException.TruncatedMessage();
}
else
{
return false;
}
}
state.totalBytesRetired += state.bufferSize;
state.bufferPos = 0;
state.bufferSize = 0;
while (readOnlySequenceEnumerator.MoveNext())
{
buffer = readOnlySequenceEnumerator.Current.Span;
state.bufferSize = buffer.Length;
if (buffer.Length != 0)
{
break;
}
}
if (state.bufferSize == 0)
{
if (mustSucceed)
{
throw InvalidProtocolBufferException.TruncatedMessage();
}
else
{
return false;
}
}
else
{
RecomputeBufferSizeAfterLimit(ref state);
int totalBytesRead =
state.totalBytesRetired + state.bufferSize + state.bufferSizeAfterLimit;
if (totalBytesRead < 0 || totalBytesRead > state.sizeLimit)
{
throw InvalidProtocolBufferException.SizeLimitExceeded();
}
return true;
}
}
private bool RefillFromCodedInputStream(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state, bool mustSucceed)
{
CheckCurrentBufferIsEmpty(ref state);
if (state.totalBytesRetired + state.bufferSize == state.currentLimit)
{
// Oops, we hit a limit.
if (mustSucceed)
{
throw InvalidProtocolBufferException.TruncatedMessage();
}
else
{
return false;
}
}
Stream input = codedInputStream.InternalInputStream;
state.totalBytesRetired += state.bufferSize;
state.bufferPos = 0;
state.bufferSize = (input == null) ? 0 : input.Read(codedInputStream.InternalBuffer, 0, buffer.Length);
if (state.bufferSize < 0)
{
throw new InvalidOperationException("Stream.Read returned a negative count");
}
if (state.bufferSize == 0)
{
if (mustSucceed)
{
throw InvalidProtocolBufferException.TruncatedMessage();
}
else
{
return false;
}
}
else
{
RecomputeBufferSizeAfterLimit(ref state);
int totalBytesRead =
state.totalBytesRetired + state.bufferSize + state.bufferSizeAfterLimit;
if (totalBytesRead < 0 || totalBytesRead > state.sizeLimit)
{
throw InvalidProtocolBufferException.SizeLimitExceeded();
}
return true;
}
}
private static void RecomputeBufferSizeAfterLimit(ref ParserInternalState state)
{
state.bufferSize += state.bufferSizeAfterLimit;
int bufferEnd = state.totalBytesRetired + state.bufferSize;
if (bufferEnd > state.currentLimit)
{
// Limit is in current buffer.
state.bufferSizeAfterLimit = bufferEnd - state.currentLimit;
state.bufferSize -= state.bufferSizeAfterLimit;
}
else
{
state.bufferSizeAfterLimit = 0;
}
}
private static void CheckCurrentBufferIsEmpty(ref ParserInternalState state)
{
if (state.bufferPos < state.bufferSize)
{
throw new InvalidOperationException("RefillBuffer() called when buffer wasn't empty.");
}
}
}
}