| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Buffers; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Text; |
| | | 8 | | using ZeroC.Slice.Codec.Internal; |
| | | 9 | | |
| | | 10 | | namespace ZeroC.Slice.Codec; |
| | | 11 | | |
| | | 12 | | /// <summary>Provides methods to decode data encoded with Slice.</summary> |
| | | 13 | | public ref partial struct SliceDecoder |
| | | 14 | | { |
| | | 15 | | /// <summary>Gets the number of bytes decoded in the underlying buffer.</summary> |
| | 240281 | 16 | | public readonly long Consumed => _reader.Consumed; |
| | | 17 | | |
| | | 18 | | /// <summary>Gets the decoding context.</summary> |
| | | 19 | | /// <remarks>The decoding context is a kind of cookie: the code that creates the decoder can store this context in |
| | | 20 | | /// the decoder for later retrieval.</remarks> |
| | 22 | 21 | | public object? DecodingContext { get; } |
| | | 22 | | |
| | | 23 | | /// <summary>Gets a value indicating whether this decoder has reached the end of its underlying buffer.</summary> |
| | | 24 | | /// <value><see langword="true" /> when this decoder has reached the end of its underlying buffer; otherwise |
| | | 25 | | /// <see langword="false" />.</value> |
| | 7349 | 26 | | public readonly bool End => _reader.End; |
| | | 27 | | |
| | | 28 | | /// <summary>Gets the number of bytes remaining in the underlying buffer.</summary> |
| | | 29 | | /// <value>The number of bytes remaining in the underlying buffer.</value> |
| | 52 | 30 | | public readonly long Remaining => _reader.Remaining; |
| | | 31 | | |
| | | 32 | | private const string EndOfBufferMessage = "Attempting to decode past the end of the Slice decoder buffer."; |
| | | 33 | | |
| | 9 | 34 | | private static readonly UTF8Encoding _utf8 = |
| | 9 | 35 | | new(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); // no BOM |
| | | 36 | | |
| | | 37 | | // The number of bytes already allocated for strings, dictionaries, and sequences. |
| | | 38 | | private int _currentCollectionAllocation; |
| | | 39 | | |
| | | 40 | | // The maximum number of bytes that can be allocated for strings, dictionaries, and sequences. |
| | | 41 | | private readonly int _maxCollectionAllocation; |
| | | 42 | | |
| | | 43 | | // The sequence reader. |
| | | 44 | | private SequenceReader<byte> _reader; |
| | | 45 | | |
| | | 46 | | /// <summary>Constructs a new Slice decoder over a byte buffer.</summary> |
| | | 47 | | /// <param name="buffer">The byte buffer.</param> |
| | | 48 | | /// <param name="decodingContext">The decoding context.</param> |
| | | 49 | | /// <param name="maxCollectionAllocation">The maximum cumulative allocation in bytes when decoding strings, |
| | | 50 | | /// sequences, and dictionaries from this buffer.<c>-1</c> (the default) is equivalent to 8 times the buffer |
| | | 51 | | /// length, clamped to <see cref="int.MaxValue" />.</param> |
| | | 52 | | public SliceDecoder(ReadOnlySequence<byte> buffer, object? decodingContext = null, int maxCollectionAllocation = -1) |
| | 23568 | 53 | | { |
| | 23568 | 54 | | DecodingContext = decodingContext; |
| | | 55 | | |
| | 23568 | 56 | | _currentCollectionAllocation = 0; |
| | 23568 | 57 | | _maxCollectionAllocation = maxCollectionAllocation == -1 ? |
| | 23568 | 58 | | (buffer.Length > int.MaxValue / 8 ? int.MaxValue : (int)(8L * buffer.Length)) : |
| | 23568 | 59 | | (maxCollectionAllocation >= 0 ? maxCollectionAllocation : |
| | 23568 | 60 | | throw new ArgumentException( |
| | 23568 | 61 | | $"The {nameof(maxCollectionAllocation)} argument must be greater than or equal to -1.", |
| | 23568 | 62 | | nameof(maxCollectionAllocation))); |
| | | 63 | | |
| | 23568 | 64 | | _reader = new SequenceReader<byte>(buffer); |
| | 23568 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary>Constructs a new Slice decoder over a byte buffer.</summary> |
| | | 68 | | /// <param name="buffer">The byte buffer.</param> |
| | | 69 | | /// <param name="decodingContext">The decoding context.</param> |
| | | 70 | | /// <param name="maxCollectionAllocation">The maximum cumulative allocation in bytes when decoding strings, |
| | | 71 | | /// sequences, and dictionaries from this buffer.<c>-1</c> (the default) is equivalent to 8 times the buffer |
| | | 72 | | /// length, clamped to <see cref="int.MaxValue" />.</param> |
| | | 73 | | public SliceDecoder(ReadOnlyMemory<byte> buffer, object? decodingContext = null, int maxCollectionAllocation = -1) |
| | 195 | 74 | | : this(new ReadOnlySequence<byte>(buffer), decodingContext, maxCollectionAllocation) |
| | 195 | 75 | | { |
| | 195 | 76 | | } |
| | | 77 | | |
| | | 78 | | // Decode methods for basic types |
| | | 79 | | |
| | | 80 | | /// <summary>Checks if the in memory representation of the bool value is valid according to the Slice encoding.</sum |
| | | 81 | | /// <param name="value">The value to check.</param> |
| | | 82 | | /// <exception cref="InvalidDataException">If the value is out of the bool type accepted range.</exception> |
| | | 83 | | public static void CheckBoolValue(bool value) |
| | 6 | 84 | | { |
| | 6 | 85 | | if (Unsafe.As<bool, byte>(ref value) > 1) |
| | 1 | 86 | | { |
| | 1 | 87 | | throw new InvalidDataException("The value is out of the bool type accepted range."); |
| | | 88 | | } |
| | 5 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary>Decodes a slice bool into a bool.</summary> |
| | | 92 | | /// <returns>The bool decoded by this decoder.</returns> |
| | | 93 | | public bool DecodeBool() |
| | 2138 | 94 | | { |
| | 2138 | 95 | | if (_reader.TryRead(out byte value)) |
| | 2137 | 96 | | { |
| | 2137 | 97 | | return value switch |
| | 2137 | 98 | | { |
| | 1572 | 99 | | 0 => false, |
| | 564 | 100 | | 1 => true, |
| | 1 | 101 | | _ => throw new InvalidDataException("The value is out of the bool type accepted range.") |
| | 2137 | 102 | | }; |
| | | 103 | | } |
| | | 104 | | else |
| | 1 | 105 | | { |
| | 1 | 106 | | throw new InvalidDataException(EndOfBufferMessage); |
| | | 107 | | } |
| | 2136 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary>Decodes a Slice float32 into a float.</summary> |
| | | 111 | | /// <returns>The float decoded by this decoder.</returns> |
| | | 112 | | public float DecodeFloat32() => |
| | 0 | 113 | | SequenceMarshal.TryRead(ref _reader, out float value) ? |
| | 0 | 114 | | value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 115 | | |
| | | 116 | | /// <summary>Decodes a Slice float64 into a double.</summary> |
| | | 117 | | /// <returns>The double decoded by this decoder.</returns> |
| | | 118 | | public double DecodeFloat64() => |
| | 0 | 119 | | SequenceMarshal.TryRead(ref _reader, out double value) ? |
| | 0 | 120 | | value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 121 | | |
| | | 122 | | /// <summary>Decodes a Slice int8 into an sbyte.</summary> |
| | | 123 | | /// <returns>The sbyte decoded by this decoder.</returns> |
| | 2 | 124 | | public sbyte DecodeInt8() => (sbyte)DecodeUInt8(); |
| | | 125 | | |
| | | 126 | | /// <summary>Decodes a Slice int16 into a short.</summary> |
| | | 127 | | /// <returns>The short decoded by this decoder.</returns> |
| | | 128 | | public short DecodeInt16() => |
| | 1067 | 129 | | SequenceMarshal.TryRead(ref _reader, out short value) ? |
| | 1067 | 130 | | value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 131 | | |
| | | 132 | | /// <summary>Decodes a Slice int32 into an int.</summary> |
| | | 133 | | /// <returns>The int decoded by this decoder.</returns> |
| | | 134 | | public int DecodeInt32() => |
| | 138389 | 135 | | SequenceMarshal.TryRead(ref _reader, out int value) ? |
| | 138389 | 136 | | value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 137 | | |
| | | 138 | | /// <summary>Decodes a Slice int64 into a long.</summary> |
| | | 139 | | /// <returns>The long decoded by this decoder.</returns> |
| | | 140 | | public long DecodeInt64() => |
| | 251 | 141 | | SequenceMarshal.TryRead(ref _reader, out long value) ? |
| | 251 | 142 | | value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 143 | | |
| | | 144 | | /// <summary>Decodes a size encoded on a variable number of bytes.</summary> |
| | | 145 | | /// <returns>The size decoded by this decoder.</returns> |
| | | 146 | | public int DecodeSize() |
| | 145270 | 147 | | { |
| | | 148 | | try |
| | 145270 | 149 | | { |
| | 145270 | 150 | | return checked((int)DecodeVarUInt62()); |
| | | 151 | | } |
| | 1 | 152 | | catch (OverflowException exception) |
| | 1 | 153 | | { |
| | 1 | 154 | | throw new InvalidDataException("Cannot decode size larger than int.MaxValue.", exception); |
| | | 155 | | } |
| | 145267 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary>Decodes a Slice string into a string.</summary> |
| | | 159 | | /// <returns>The string decoded by this decoder.</returns> |
| | | 160 | | public string DecodeString() |
| | 139283 | 161 | | { |
| | 139283 | 162 | | int size = DecodeSize(); |
| | 139283 | 163 | | if (size == 0) |
| | 1377 | 164 | | { |
| | 1377 | 165 | | return ""; |
| | | 166 | | } |
| | | 167 | | else |
| | 137906 | 168 | | { |
| | | 169 | | // In the worst-case scenario, each byte becomes a new character. We'll adjust this allocation increase |
| | | 170 | | // after decoding the string. |
| | 137906 | 171 | | IncreaseCollectionAllocation(size, Unsafe.SizeOf<char>()); |
| | | 172 | | |
| | | 173 | | string result; |
| | 137903 | 174 | | if (_reader.UnreadSpan.Length >= size) |
| | 137902 | 175 | | { |
| | | 176 | | try |
| | 137902 | 177 | | { |
| | 137902 | 178 | | result = _utf8.GetString(_reader.UnreadSpan[0..size]); |
| | 137901 | 179 | | } |
| | 1 | 180 | | catch (Exception exception) when (exception is ArgumentException or DecoderFallbackException) |
| | 1 | 181 | | { |
| | | 182 | | // The two exceptions that can be thrown by GetString are ArgumentException and |
| | | 183 | | // DecoderFallbackException. Both of which are a result of malformed data. As such, we can just |
| | | 184 | | // throw an InvalidDataException. |
| | 1 | 185 | | throw new InvalidDataException("Invalid UTF-8 string.", exception); |
| | | 186 | | } |
| | 137901 | 187 | | } |
| | | 188 | | else |
| | 1 | 189 | | { |
| | 1 | 190 | | ReadOnlySequence<byte> bytes = _reader.UnreadSequence; |
| | 1 | 191 | | if (size > bytes.Length) |
| | 0 | 192 | | { |
| | 0 | 193 | | throw new InvalidDataException(EndOfBufferMessage); |
| | | 194 | | } |
| | | 195 | | try |
| | 1 | 196 | | { |
| | 1 | 197 | | result = _utf8.GetString(bytes.Slice(0, size)); |
| | 1 | 198 | | } |
| | 0 | 199 | | catch (Exception exception) when (exception is ArgumentException or DecoderFallbackException) |
| | 0 | 200 | | { |
| | | 201 | | // The two exceptions that can be thrown by GetString are ArgumentException and |
| | | 202 | | // DecoderFallbackException. Both of which are a result of malformed data. As such, we can just |
| | | 203 | | // throw an InvalidDataException. |
| | 0 | 204 | | throw new InvalidDataException("Invalid UTF-8 string.", exception); |
| | | 205 | | } |
| | 1 | 206 | | } |
| | | 207 | | |
| | 137902 | 208 | | _reader.Advance(size); |
| | | 209 | | |
| | | 210 | | // Make the adjustment. The overall increase in allocation is result.Length * SizeOf<char>(). |
| | 137902 | 211 | | DecreaseCollectionAllocation(size - result.Length, Unsafe.SizeOf<char>()); |
| | 137902 | 212 | | return result; |
| | | 213 | | } |
| | 139279 | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary>Decodes a Slice uint8 into a byte.</summary> |
| | | 217 | | /// <returns>The byte decoded by this decoder.</returns> |
| | | 218 | | public byte DecodeUInt8() => |
| | 364 | 219 | | _reader.TryRead(out byte value) ? value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 220 | | |
| | | 221 | | /// <summary>Decodes a Slice uint16 into a ushort.</summary> |
| | | 222 | | /// <returns>The ushort decoded by this decoder.</returns> |
| | | 223 | | public ushort DecodeUInt16() => |
| | 1 | 224 | | SequenceMarshal.TryRead(ref _reader, out ushort value) ? |
| | 1 | 225 | | value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 226 | | |
| | | 227 | | /// <summary>Decodes a Slice uint32 into a uint.</summary> |
| | | 228 | | /// <returns>The uint decoded by this decoder.</returns> |
| | | 229 | | public uint DecodeUInt32() => |
| | 21 | 230 | | SequenceMarshal.TryRead(ref _reader, out uint value) ? |
| | 21 | 231 | | value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 232 | | |
| | | 233 | | /// <summary>Decodes a Slice uint64 into a ulong.</summary> |
| | | 234 | | /// <returns>The ulong decoded by this decoder.</returns> |
| | | 235 | | public ulong DecodeUInt64() => |
| | 0 | 236 | | SequenceMarshal.TryRead(ref _reader, out ulong value) ? |
| | 0 | 237 | | value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 238 | | |
| | | 239 | | /// <summary>Decodes a Slice varint32 into an int.</summary> |
| | | 240 | | /// <returns>The int decoded by this decoder.</returns> |
| | | 241 | | public int DecodeVarInt32() |
| | 324 | 242 | | { |
| | | 243 | | try |
| | 324 | 244 | | { |
| | 324 | 245 | | return checked((int)DecodeVarInt62()); |
| | | 246 | | } |
| | 2 | 247 | | catch (OverflowException exception) |
| | 2 | 248 | | { |
| | 2 | 249 | | throw new InvalidDataException("The value is out of the varint32 accepted range.", exception); |
| | | 250 | | } |
| | 322 | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary>Decodes a Slice varint62 into a long.</summary> |
| | | 254 | | /// <returns>The long decoded by this decoder.</returns> |
| | | 255 | | public long DecodeVarInt62() => |
| | 344 | 256 | | (PeekByte() & 0x03) switch |
| | 344 | 257 | | { |
| | 323 | 258 | | 0 => (sbyte)DecodeUInt8() >> 2, |
| | 2 | 259 | | 1 => DecodeInt16() >> 2, |
| | 11 | 260 | | 2 => DecodeInt32() >> 2, |
| | 8 | 261 | | _ => DecodeInt64() >> 2 |
| | 344 | 262 | | }; |
| | | 263 | | |
| | | 264 | | /// <summary>Decodes a Slice varuint32 into a uint.</summary> |
| | | 265 | | /// <returns>The uint decoded by this decoder.</returns> |
| | | 266 | | public uint DecodeVarUInt32() |
| | 1 | 267 | | { |
| | | 268 | | try |
| | 1 | 269 | | { |
| | 1 | 270 | | return checked((uint)DecodeVarUInt62()); |
| | | 271 | | } |
| | 1 | 272 | | catch (OverflowException exception) |
| | 1 | 273 | | { |
| | 1 | 274 | | throw new InvalidDataException("The value is out of the varuint32 accepted range.", exception); |
| | | 275 | | } |
| | 0 | 276 | | } |
| | | 277 | | |
| | | 278 | | /// <summary>Decodes a Slice varuint62 into a ulong.</summary> |
| | | 279 | | /// <returns>The ulong decoded by this decoder.</returns> |
| | | 280 | | public ulong DecodeVarUInt62() => |
| | 153552 | 281 | | TryDecodeVarUInt62(out ulong value) ? value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 282 | | |
| | | 283 | | /// <summary>Tries to decode a Slice uint8 into a byte.</summary> |
| | | 284 | | /// <param name="value">When this method returns <see langword="true" />, this value is set to the decoded byte. |
| | | 285 | | /// Otherwise, this value is set to its default value.</param> |
| | | 286 | | /// <returns><see langword="true" /> if the decoder is not at the end of the buffer and the decode operation |
| | | 287 | | /// succeeded; otherwise, <see langword="false" />.</returns> |
| | 13407 | 288 | | public bool TryDecodeUInt8(out byte value) => _reader.TryRead(out value); |
| | | 289 | | |
| | | 290 | | /// <summary>Tries to decode a Slice varuint62 into a ulong.</summary> |
| | | 291 | | /// <param name="value">When this method returns <see langword="true" />, this value is set to the decoded ulong. |
| | | 292 | | /// Otherwise, this value is set to its default value.</param> |
| | | 293 | | /// <returns><see langword="true" /> if the decoder is not at the end of the buffer and the decode operation |
| | | 294 | | /// succeeded; otherwise, <see langword="false" />.</returns> |
| | | 295 | | public bool TryDecodeVarUInt62(out ulong value) |
| | 182008 | 296 | | { |
| | 182008 | 297 | | if (_reader.TryPeek(out byte b)) |
| | 182004 | 298 | | { |
| | 182004 | 299 | | switch (b & 0x03) |
| | | 300 | | { |
| | | 301 | | case 0: |
| | 158033 | 302 | | { |
| | 158033 | 303 | | if (_reader.TryRead(out byte byteValue)) |
| | 158033 | 304 | | { |
| | 158033 | 305 | | value = (uint)byteValue >> 2; |
| | 158033 | 306 | | return true; |
| | | 307 | | } |
| | 0 | 308 | | break; |
| | | 309 | | } |
| | | 310 | | case 1: |
| | 11571 | 311 | | { |
| | 11571 | 312 | | if (SequenceMarshal.TryRead(ref _reader, out ushort ushortValue)) |
| | 11569 | 313 | | { |
| | 11569 | 314 | | value = (uint)ushortValue >> 2; |
| | 11569 | 315 | | return true; |
| | | 316 | | } |
| | 2 | 317 | | break; |
| | | 318 | | } |
| | | 319 | | case 2: |
| | 12390 | 320 | | { |
| | 12390 | 321 | | if (SequenceMarshal.TryRead(ref _reader, out uint uintValue)) |
| | 12388 | 322 | | { |
| | 12388 | 323 | | value = uintValue >> 2; |
| | 12388 | 324 | | return true; |
| | | 325 | | } |
| | 2 | 326 | | break; |
| | | 327 | | } |
| | | 328 | | default: |
| | 10 | 329 | | { |
| | 10 | 330 | | if (SequenceMarshal.TryRead(ref _reader, out ulong ulongValue)) |
| | 8 | 331 | | { |
| | 8 | 332 | | value = ulongValue >> 2; |
| | 8 | 333 | | return true; |
| | | 334 | | } |
| | 2 | 335 | | break; |
| | | 336 | | } |
| | | 337 | | } |
| | 6 | 338 | | } |
| | 10 | 339 | | value = 0; |
| | 10 | 340 | | return false; |
| | 182008 | 341 | | } |
| | | 342 | | |
| | | 343 | | // Other methods |
| | | 344 | | |
| | | 345 | | /// <summary>Copy bytes from the underlying reader into the destination to fill completely destination. |
| | | 346 | | /// </summary> |
| | | 347 | | /// <param name="destination">The span to which bytes of this decoder will be copied.</param> |
| | | 348 | | /// <remarks>This method also moves the reader's Consumed property.</remarks> |
| | | 349 | | public void CopyTo(Span<byte> destination) |
| | 3092 | 350 | | { |
| | 3092 | 351 | | if (_reader.TryCopyTo(destination)) |
| | 3092 | 352 | | { |
| | 3092 | 353 | | _reader.Advance(destination.Length); |
| | 3092 | 354 | | } |
| | | 355 | | else |
| | 0 | 356 | | { |
| | 0 | 357 | | throw new InvalidDataException(EndOfBufferMessage); |
| | | 358 | | } |
| | 3092 | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary>Copy all bytes from the underlying reader into the destination buffer writer.</summary> |
| | | 362 | | /// <param name="destination">The destination buffer writer.</param> |
| | | 363 | | /// <remarks>This method also moves the reader's Consumed property.</remarks> |
| | | 364 | | public void CopyTo(IBufferWriter<byte> destination) |
| | 17 | 365 | | { |
| | 17 | 366 | | destination.Write(_reader.UnreadSequence); |
| | 17 | 367 | | _reader.AdvanceToEnd(); |
| | 17 | 368 | | } |
| | | 369 | | |
| | | 370 | | /// <summary>Decodes a tagged field.</summary> |
| | | 371 | | /// <typeparam name="T">The type of the decoded value.</typeparam> |
| | | 372 | | /// <param name="tag">The tag.</param> |
| | | 373 | | /// <param name="decodeFunc">A decode function that decodes the value of this tagged field.</param> |
| | | 374 | | /// <returns>The decoded value of the tagged field, or <see langword="null" /> if not found.</returns> |
| | | 375 | | /// <remarks>We return a T? and not a T to avoid ambiguities in the generated code with nullable reference types |
| | | 376 | | /// such as string?.</remarks> |
| | | 377 | | public T? DecodeTagged<T>(int tag, DecodeFunc<T> decodeFunc) |
| | 59 | 378 | | { |
| | 59 | 379 | | int requestedTag = tag; |
| | | 380 | | |
| | 59 | 381 | | while (true) |
| | 59 | 382 | | { |
| | 59 | 383 | | long startPos = _reader.Consumed; |
| | 59 | 384 | | tag = DecodeVarInt32(); |
| | | 385 | | |
| | 59 | 386 | | if (tag == requestedTag) |
| | 35 | 387 | | { |
| | | 388 | | // Found requested tag, so skip size: |
| | 35 | 389 | | SkipSize(); |
| | 35 | 390 | | return decodeFunc(ref this); |
| | | 391 | | } |
| | 24 | 392 | | else if (tag == SliceDefinitions.TagEndMarker || tag > requestedTag) |
| | 24 | 393 | | { |
| | 24 | 394 | | _reader.Rewind(_reader.Consumed - startPos); // rewind |
| | 24 | 395 | | break; // while |
| | | 396 | | } |
| | | 397 | | else |
| | 0 | 398 | | { |
| | 0 | 399 | | Skip(DecodeSize()); |
| | | 400 | | // and continue while loop |
| | 0 | 401 | | } |
| | 0 | 402 | | } |
| | 24 | 403 | | return default; |
| | 59 | 404 | | } |
| | | 405 | | |
| | | 406 | | /// <summary>Gets a bit sequence reader to read the underlying bit sequence later on.</summary> |
| | | 407 | | /// <param name="bitSequenceSize">The minimum number of bits in the sequence.</param> |
| | | 408 | | /// <returns>A bit sequence reader.</returns> |
| | | 409 | | public BitSequenceReader GetBitSequenceReader(int bitSequenceSize) |
| | 1096 | 410 | | { |
| | 1096 | 411 | | if (bitSequenceSize <= 0) |
| | 0 | 412 | | { |
| | 0 | 413 | | throw new ArgumentOutOfRangeException( |
| | 0 | 414 | | nameof(bitSequenceSize), |
| | 0 | 415 | | $"The {nameof(bitSequenceSize)} argument must be greater than 0."); |
| | | 416 | | } |
| | | 417 | | |
| | 1096 | 418 | | int size = SliceEncoder.GetBitSequenceByteCount(bitSequenceSize); |
| | 1096 | 419 | | if (_reader.Remaining < size) |
| | 1 | 420 | | { |
| | 1 | 421 | | throw new InvalidDataException(EndOfBufferMessage); |
| | | 422 | | } |
| | 1095 | 423 | | ReadOnlySequence<byte> bitSequence = _reader.UnreadSequence.Slice(0, size); |
| | 1095 | 424 | | _reader.Advance(size); |
| | 1095 | 425 | | Debug.Assert(bitSequence.Length == size); |
| | 1095 | 426 | | return new BitSequenceReader(bitSequence); |
| | 1095 | 427 | | } |
| | | 428 | | |
| | | 429 | | /// <summary>Increases the number of bytes in the decoder's collection allocation.</summary> |
| | | 430 | | /// <param name="count">The number of elements.</param> |
| | | 431 | | /// <param name="elementSize">The size of each element in bytes.</param> |
| | | 432 | | /// <seealso cref="SliceDecoder(ReadOnlySequence{byte}, object?, int)" /> |
| | | 433 | | public void IncreaseCollectionAllocation(int count, int elementSize) |
| | 141724 | 434 | | { |
| | 141724 | 435 | | Debug.Assert(count >= 0, $"{nameof(count)} must be greater than or equal to 0."); |
| | 141724 | 436 | | Debug.Assert(elementSize > 0, $"{nameof(elementSize)} must be greater than 0."); |
| | | 437 | | |
| | | 438 | | // Widen count to long to avoid overflow when multiplying by elementSize. |
| | 141724 | 439 | | long byteCount = (long)count * elementSize; |
| | | 440 | | |
| | 141724 | 441 | | int remainingAllocation = _maxCollectionAllocation - _currentCollectionAllocation; |
| | 141724 | 442 | | if (byteCount > remainingAllocation) |
| | 17 | 443 | | { |
| | 17 | 444 | | throw new InvalidDataException( |
| | 17 | 445 | | $"The decoding exceeds the max collection allocation of '{_maxCollectionAllocation}'."); |
| | | 446 | | } |
| | 141707 | 447 | | _currentCollectionAllocation += (int)byteCount; |
| | 141707 | 448 | | } |
| | | 449 | | |
| | | 450 | | /// <summary>Skip the given number of bytes.</summary> |
| | | 451 | | /// <param name="count">The number of bytes to skip.</param> |
| | | 452 | | public void Skip(int count) |
| | 71 | 453 | | { |
| | 71 | 454 | | if (_reader.Remaining >= count) |
| | 71 | 455 | | { |
| | 71 | 456 | | _reader.Advance(count); |
| | 71 | 457 | | } |
| | | 458 | | else |
| | 0 | 459 | | { |
| | 0 | 460 | | throw new InvalidDataException(EndOfBufferMessage); |
| | | 461 | | } |
| | 71 | 462 | | } |
| | | 463 | | |
| | | 464 | | /// <summary>Skips the remaining tagged fields.</summary> |
| | | 465 | | public void SkipTagged() |
| | 187 | 466 | | { |
| | 198 | 467 | | while (true) |
| | 198 | 468 | | { |
| | 198 | 469 | | if (DecodeVarInt32() == SliceDefinitions.TagEndMarker) |
| | 187 | 470 | | { |
| | 187 | 471 | | break; // while |
| | | 472 | | } |
| | | 473 | | |
| | | 474 | | // Skip tagged value |
| | 11 | 475 | | Skip(DecodeSize()); |
| | 11 | 476 | | } |
| | 187 | 477 | | } |
| | | 478 | | |
| | | 479 | | /// <summary>Skip Slice size.</summary> |
| | 41 | 480 | | public void SkipSize() => Skip(DecodeVarInt62Length(PeekByte())); |
| | | 481 | | |
| | | 482 | | // Applies to all var type: varint62, varuint62 etc. |
| | 41 | 483 | | internal static int DecodeVarInt62Length(byte from) => 1 << (from & 0x03); |
| | | 484 | | |
| | | 485 | | /// <summary>Decreases the number of bytes in the decoder's collection allocation.</summary> |
| | | 486 | | /// <param name="count">The number of elements.</param> |
| | | 487 | | /// <param name="elementSize">The size of each element in bytes.</param> |
| | | 488 | | private void DecreaseCollectionAllocation(int count, int elementSize) |
| | 137902 | 489 | | { |
| | 137902 | 490 | | Debug.Assert(count >= 0, $"{nameof(count)} must be greater than or equal to 0."); |
| | 137902 | 491 | | Debug.Assert(elementSize > 0, $"{nameof(elementSize)} must be greater than 0."); |
| | | 492 | | |
| | | 493 | | // Widen count to long to avoid overflow when multiplying by elementSize. |
| | 137902 | 494 | | long byteCount = (long)count * elementSize; |
| | | 495 | | |
| | 137902 | 496 | | Debug.Assert(byteCount <= _currentCollectionAllocation, "Decreasing more than the current collection allocation. |
| | 137902 | 497 | | _currentCollectionAllocation -= (int)byteCount; |
| | 137902 | 498 | | } |
| | | 499 | | |
| | | 500 | | private readonly byte PeekByte() => |
| | 385 | 501 | | _reader.TryPeek(out byte value) ? value : throw new InvalidDataException(EndOfBufferMessage); |
| | | 502 | | } |