1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 // https://developers.google.com/protocol-buffers/
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 //     * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #endregion
32 
33 using System;
34 using System.Buffers;
35 using System.Buffers.Binary;
36 using System.Collections.Generic;
37 using System.IO;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Security;
41 using System.Text;
42 using Google.Protobuf.Collections;
43 
44 namespace Google.Protobuf
45 {
46     /// <summary>
47     /// Fast parsing primitives for wrapper types
48     /// </summary>
49     [SecuritySafeCritical]
50     internal static class ParsingPrimitivesWrappers
51     {
ReadFloatWrapperLittleEndian(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)52         internal static float? ReadFloatWrapperLittleEndian(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
53         {
54             // length:1 + tag:1 + value:4 = 6 bytes
55             if (state.bufferPos + 6 <= state.bufferSize)
56             {
57                 // The entire wrapper message is already contained in `buffer`.
58                 int length = buffer[state.bufferPos];
59                 if (length == 0)
60                 {
61                     state.bufferPos++;
62                     return 0F;
63                 }
64                 // tag:1 + value:4 = length of 5 bytes
65                 // field=1, type=32-bit = tag of 13
66                 if (length != 5 || buffer[state.bufferPos + 1] != 13)
67                 {
68                     return ReadFloatWrapperSlow(ref buffer, ref state);
69                 }
70                 state.bufferPos += 2;
71                 return ParsingPrimitives.ParseFloat(ref buffer, ref state);
72             }
73             else
74             {
75                 return ReadFloatWrapperSlow(ref buffer, ref state);
76             }
77         }
78 
ReadFloatWrapperSlow(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)79         internal static float? ReadFloatWrapperSlow(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
80         {
81             int length = ParsingPrimitives.ParseLength(ref buffer, ref state);
82             if (length == 0)
83             {
84                 return 0F;
85             }
86             int finalBufferPos = state.totalBytesRetired + state.bufferPos + length;
87             float result = 0F;
88             do
89             {
90                 // field=1, type=32-bit = tag of 13
91                 if (ParsingPrimitives.ParseTag(ref buffer, ref state) == 13)
92                 {
93                     result = ParsingPrimitives.ParseFloat(ref buffer, ref state);
94                 }
95                 else
96                 {
97                     ParsingPrimitivesMessages.SkipLastField(ref buffer, ref state);
98                 }
99             }
100             while (state.totalBytesRetired + state.bufferPos < finalBufferPos);
101             return result;
102         }
103 
ReadDoubleWrapperLittleEndian(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)104         internal static double? ReadDoubleWrapperLittleEndian(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
105         {
106             // length:1 + tag:1 + value:8 = 10 bytes
107             if (state.bufferPos + 10 <= state.bufferSize)
108             {
109                 // The entire wrapper message is already contained in `buffer`.
110                 int length = buffer[state.bufferPos];
111                 if (length == 0)
112                 {
113                     state.bufferPos++;
114                     return 0D;
115                 }
116                 // tag:1 + value:8 = length of 9 bytes
117                 // field=1, type=64-bit = tag of 9
118                 if (length != 9 || buffer[state.bufferPos + 1] != 9)
119                 {
120                     return ReadDoubleWrapperSlow(ref buffer, ref state);
121                 }
122                 state.bufferPos += 2;
123                 return ParsingPrimitives.ParseDouble(ref buffer, ref state);
124             }
125             else
126             {
127                 return ReadDoubleWrapperSlow(ref buffer, ref state);
128             }
129         }
130 
ReadDoubleWrapperSlow(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)131         internal static double? ReadDoubleWrapperSlow(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
132         {
133             int length = ParsingPrimitives.ParseLength(ref buffer, ref state);
134             if (length == 0)
135             {
136                 return 0D;
137             }
138             int finalBufferPos = state.totalBytesRetired + state.bufferPos + length;
139             double result = 0D;
140             do
141             {
142                 // field=1, type=64-bit = tag of 9
143                 if (ParsingPrimitives.ParseTag(ref buffer, ref state) == 9)
144                 {
145                     result = ParsingPrimitives.ParseDouble(ref buffer, ref state);
146                 }
147                 else
148                 {
149                     ParsingPrimitivesMessages.SkipLastField(ref buffer, ref state);
150                 }
151             }
152             while (state.totalBytesRetired + state.bufferPos < finalBufferPos);
153             return result;
154         }
155 
ReadBoolWrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)156         internal static bool? ReadBoolWrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
157         {
158             return ReadUInt64Wrapper(ref buffer, ref state) != 0;
159         }
160 
ReadUInt32Wrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)161         internal static uint? ReadUInt32Wrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
162         {
163             // field=1, type=varint = tag of 8
164             const int expectedTag = 8;
165             // length:1 + tag:1 + value:10(varint64-max) = 12 bytes
166             // Value can be 64 bits for negative integers
167             if (state.bufferPos + 12 <= state.bufferSize)
168             {
169                 // The entire wrapper message is already contained in `buffer`.
170                 int pos0 = state.bufferPos;
171                 int length = buffer[state.bufferPos++];
172                 if (length == 0)
173                 {
174                     return 0;
175                 }
176                 // Length will always fit in a single byte.
177                 if (length >= 128)
178                 {
179                     state.bufferPos = pos0;
180                     return ReadUInt32WrapperSlow(ref buffer, ref state);
181                 }
182                 int finalBufferPos = state.bufferPos + length;
183                 if (buffer[state.bufferPos++] != expectedTag)
184                 {
185                     state.bufferPos = pos0;
186                     return ReadUInt32WrapperSlow(ref buffer, ref state);
187                 }
188                 var result = ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
189                 // Verify this message only contained a single field.
190                 if (state.bufferPos != finalBufferPos)
191                 {
192                     state.bufferPos = pos0;
193                     return ReadUInt32WrapperSlow(ref buffer, ref state);
194                 }
195                 return result;
196             }
197             else
198             {
199                 return ReadUInt32WrapperSlow(ref buffer, ref state);
200             }
201         }
202 
ReadUInt32WrapperSlow(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)203         internal static uint? ReadUInt32WrapperSlow(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
204         {
205             int length = ParsingPrimitives.ParseLength(ref buffer, ref state);
206             if (length == 0)
207             {
208                 return 0;
209             }
210             int finalBufferPos = state.totalBytesRetired + state.bufferPos + length;
211             uint result = 0;
212             do
213             {
214                 // field=1, type=varint = tag of 8
215                 if (ParsingPrimitives.ParseTag(ref buffer, ref state) == 8)
216                 {
217                     result = ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
218                 }
219                 else
220                 {
221                     ParsingPrimitivesMessages.SkipLastField(ref buffer, ref state);
222                 }
223             }
224             while (state.totalBytesRetired + state.bufferPos < finalBufferPos);
225             return result;
226         }
227 
ReadInt32Wrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)228         internal static int? ReadInt32Wrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
229         {
230             return (int?)ReadUInt32Wrapper(ref buffer, ref state);
231         }
232 
ReadUInt64Wrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)233         internal static ulong? ReadUInt64Wrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
234         {
235             // field=1, type=varint = tag of 8
236             const int expectedTag = 8;
237             // length:1 + tag:1 + value:10(varint64-max) = 12 bytes
238             if (state.bufferPos + 12 <= state.bufferSize)
239             {
240                 // The entire wrapper message is already contained in `buffer`.
241                 int pos0 = state.bufferPos;
242                 int length = buffer[state.bufferPos++];
243                 if (length == 0)
244                 {
245                     return 0L;
246                 }
247                 // Length will always fit in a single byte.
248                 if (length >= 128)
249                 {
250                     state.bufferPos = pos0;
251                     return ReadUInt64WrapperSlow(ref buffer, ref state);
252                 }
253                 int finalBufferPos = state.bufferPos + length;
254                 if (buffer[state.bufferPos++] != expectedTag)
255                 {
256                     state.bufferPos = pos0;
257                     return ReadUInt64WrapperSlow(ref buffer, ref state);
258                 }
259                 var result = ParsingPrimitives.ParseRawVarint64(ref buffer, ref state);
260                 // Verify this message only contained a single field.
261                 if (state.bufferPos != finalBufferPos)
262                 {
263                     state.bufferPos = pos0;
264                     return ReadUInt64WrapperSlow(ref buffer, ref state);
265                 }
266                 return result;
267             }
268             else
269             {
270                 return ReadUInt64WrapperSlow(ref buffer, ref state);
271             }
272         }
273 
ReadUInt64WrapperSlow(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)274         internal static ulong? ReadUInt64WrapperSlow(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
275         {
276             // field=1, type=varint = tag of 8
277             const int expectedTag = 8;
278             int length = ParsingPrimitives.ParseLength(ref buffer, ref state);
279             if (length == 0)
280             {
281                 return 0L;
282             }
283             int finalBufferPos = state.totalBytesRetired + state.bufferPos + length;
284             ulong result = 0L;
285             do
286             {
287                 if (ParsingPrimitives.ParseTag(ref buffer, ref state) == expectedTag)
288                 {
289                     result = ParsingPrimitives.ParseRawVarint64(ref buffer, ref state);
290                 }
291                 else
292                 {
293                     ParsingPrimitivesMessages.SkipLastField(ref buffer, ref state);
294                 }
295             }
296             while (state.totalBytesRetired + state.bufferPos < finalBufferPos);
297             return result;
298         }
299 
ReadInt64Wrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)300         internal static long? ReadInt64Wrapper(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
301         {
302             return (long?)ReadUInt64Wrapper(ref buffer, ref state);
303         }
304 
ReadFloatWrapperLittleEndian(ref ParseContext ctx)305         internal static float? ReadFloatWrapperLittleEndian(ref ParseContext ctx)
306         {
307             return ParsingPrimitivesWrappers.ReadFloatWrapperLittleEndian(ref ctx.buffer, ref ctx.state);
308         }
309 
ReadFloatWrapperSlow(ref ParseContext ctx)310         internal static float? ReadFloatWrapperSlow(ref ParseContext ctx)
311         {
312             return ParsingPrimitivesWrappers.ReadFloatWrapperSlow(ref ctx.buffer, ref ctx.state);
313         }
314 
ReadDoubleWrapperLittleEndian(ref ParseContext ctx)315         internal static double? ReadDoubleWrapperLittleEndian(ref ParseContext ctx)
316         {
317             return ParsingPrimitivesWrappers.ReadDoubleWrapperLittleEndian(ref ctx.buffer, ref ctx.state);
318         }
319 
ReadDoubleWrapperSlow(ref ParseContext ctx)320         internal static double? ReadDoubleWrapperSlow(ref ParseContext ctx)
321         {
322             return ParsingPrimitivesWrappers.ReadDoubleWrapperSlow(ref ctx.buffer, ref ctx.state);
323         }
324 
ReadBoolWrapper(ref ParseContext ctx)325         internal static bool? ReadBoolWrapper(ref ParseContext ctx)
326         {
327             return ParsingPrimitivesWrappers.ReadBoolWrapper(ref ctx.buffer, ref ctx.state);
328         }
329 
ReadUInt32Wrapper(ref ParseContext ctx)330         internal static uint? ReadUInt32Wrapper(ref ParseContext ctx)
331         {
332             return ParsingPrimitivesWrappers.ReadUInt32Wrapper(ref ctx.buffer, ref ctx.state);
333         }
334 
ReadInt32Wrapper(ref ParseContext ctx)335         internal static int? ReadInt32Wrapper(ref ParseContext ctx)
336         {
337             return ParsingPrimitivesWrappers.ReadInt32Wrapper(ref ctx.buffer, ref ctx.state);
338         }
339 
ReadUInt64Wrapper(ref ParseContext ctx)340         internal static ulong? ReadUInt64Wrapper(ref ParseContext ctx)
341         {
342             return ParsingPrimitivesWrappers.ReadUInt64Wrapper(ref ctx.buffer, ref ctx.state);
343         }
344 
ReadUInt64WrapperSlow(ref ParseContext ctx)345         internal static ulong? ReadUInt64WrapperSlow(ref ParseContext ctx)
346         {
347             return ParsingPrimitivesWrappers.ReadUInt64WrapperSlow(ref ctx.buffer, ref ctx.state);
348         }
349 
ReadInt64Wrapper(ref ParseContext ctx)350         internal static long? ReadInt64Wrapper(ref ParseContext ctx)
351         {
352             return ParsingPrimitivesWrappers.ReadInt64Wrapper(ref ctx.buffer, ref ctx.state);
353         }
354     }
355 }