| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Buffers.Binary; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Variable length integer encoding and decoding methods. Based on https://tools.ietf.org/html/draft-ietf-quic-tran |
| | | 12 | | /// A variable-length integer can use 1, 2, 4, or 8 bytes. |
| | | 13 | | /// </summary> |
| | | 14 | | internal static class VariableLengthIntegerHelper |
| | | 15 | | { |
| | | 16 | | public const int MaximumEncodedLength = 8; |
| | | 17 | | |
| | | 18 | | // The high 4 bits indicate the length of the integer. |
| | | 19 | | // 00 = length 1 |
| | | 20 | | // 01 = length 2 |
| | | 21 | | // 10 = length 4 |
| | | 22 | | // 11 = length 8 |
| | | 23 | | private const byte LengthMask = 0xC0; |
| | | 24 | | private const byte InitialOneByteLengthMask = 0x00; |
| | | 25 | | private const byte InitialTwoByteLengthMask = 0x40; |
| | | 26 | | private const byte InitialFourByteLengthMask = 0x80; |
| | | 27 | | private const byte InitialEightByteLengthMask = 0xC0; |
| | | 28 | | |
| | | 29 | | // Bits to subtract to remove the length. |
| | | 30 | | private const uint TwoByteLengthMask = 0x4000; |
| | | 31 | | private const uint FourByteLengthMask = 0x80000000; |
| | | 32 | | private const ulong EightByteLengthMask = 0xC000000000000000; |
| | | 33 | | |
| | | 34 | | // public for internal use in aspnetcore |
| | | 35 | | public const uint OneByteLimit = (1U << 6) - 1; |
| | | 36 | | public const uint TwoByteLimit = (1U << 14) - 1; |
| | | 37 | | public const uint FourByteLimit = (1U << 30) - 1; |
| | | 38 | | public const long EightByteLimit = (1L << 62) - 1; |
| | | 39 | | |
| | | 40 | | public static bool TryRead(ReadOnlySpan<byte> buffer, out long value, out int bytesRead) |
| | 0 | 41 | | { |
| | 0 | 42 | | if (buffer.Length != 0) |
| | 0 | 43 | | { |
| | 0 | 44 | | byte firstByte = buffer[0]; |
| | | 45 | | |
| | 0 | 46 | | switch (firstByte & LengthMask) |
| | | 47 | | { |
| | | 48 | | case InitialOneByteLengthMask: |
| | 0 | 49 | | value = firstByte; |
| | 0 | 50 | | bytesRead = 1; |
| | 0 | 51 | | return true; |
| | | 52 | | case InitialTwoByteLengthMask: |
| | 0 | 53 | | if (BinaryPrimitives.TryReadUInt16BigEndian(buffer, out ushort serializedShort)) |
| | 0 | 54 | | { |
| | 0 | 55 | | value = serializedShort - TwoByteLengthMask; |
| | 0 | 56 | | bytesRead = 2; |
| | 0 | 57 | | return true; |
| | | 58 | | } |
| | 0 | 59 | | break; |
| | | 60 | | case InitialFourByteLengthMask: |
| | 0 | 61 | | if (BinaryPrimitives.TryReadUInt32BigEndian(buffer, out uint serializedInt)) |
| | 0 | 62 | | { |
| | 0 | 63 | | value = serializedInt - FourByteLengthMask; |
| | 0 | 64 | | bytesRead = 4; |
| | 0 | 65 | | return true; |
| | | 66 | | } |
| | 0 | 67 | | break; |
| | | 68 | | default: // InitialEightByteLengthMask |
| | 0 | 69 | | Debug.Assert((firstByte & LengthMask) == InitialEightByteLengthMask); |
| | 0 | 70 | | if (BinaryPrimitives.TryReadUInt64BigEndian(buffer, out ulong serializedLong)) |
| | 0 | 71 | | { |
| | 0 | 72 | | value = (long)(serializedLong - EightByteLengthMask); |
| | 0 | 73 | | Debug.Assert(value >= 0 && value <= EightByteLimit, "Serialized values are within [0, 2^62). |
| | | 74 | | |
| | 0 | 75 | | bytesRead = 8; |
| | 0 | 76 | | return true; |
| | | 77 | | } |
| | 0 | 78 | | break; |
| | | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | value = 0; |
| | 0 | 83 | | bytesRead = 0; |
| | 0 | 84 | | return false; |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | public static bool TryRead(ref SequenceReader<byte> reader, out long value) |
| | | 88 | | { |
| | | 89 | | // Hot path: we probably have the entire integer in one unbroken span. |
| | | 90 | | if (TryRead(reader.UnreadSpan, out value, out int bytesRead)) |
| | | 91 | | { |
| | | 92 | | reader.Advance(bytesRead); |
| | | 93 | | return true; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // Cold path: copy to a temporary buffer before calling span-based read. |
| | | 97 | | return TryReadSlow(ref reader, out value); |
| | | 98 | | |
| | | 99 | | static bool TryReadSlow(ref SequenceReader<byte> reader, out long value) |
| | | 100 | | { |
| | | 101 | | ReadOnlySpan<byte> span = reader.CurrentSpan; |
| | | 102 | | |
| | | 103 | | if (reader.TryPeek(out byte firstByte)) |
| | | 104 | | { |
| | | 105 | | int length = |
| | | 106 | | (firstByte & LengthMask) switch |
| | | 107 | | { |
| | | 108 | | InitialOneByteLengthMask => 1, |
| | | 109 | | InitialTwoByteLengthMask => 2, |
| | | 110 | | InitialFourByteLengthMask => 4, |
| | | 111 | | _ => 8 // LengthEightByte |
| | | 112 | | }; |
| | | 113 | | |
| | | 114 | | Span<byte> temp = (stackalloc byte[8])[..length]; |
| | | 115 | | if (reader.TryCopyTo(temp)) |
| | | 116 | | { |
| | | 117 | | bool result = TryRead(temp, out value, out int bytesRead); |
| | | 118 | | Debug.Assert(result); |
| | | 119 | | Debug.Assert(bytesRead == length); |
| | | 120 | | |
| | | 121 | | reader.Advance(bytesRead); |
| | | 122 | | return true; |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | value = 0; |
| | | 127 | | return false; |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | // If callsite has 'examined', set it to buffer.End if the integer wasn't successfully read, otherwise set exami |
| | | 132 | | public static bool TryGetInteger(in ReadOnlySequence<byte> buffer, out SequencePosition consumed, out long integ |
| | | 133 | | { |
| | | 134 | | var reader = new SequenceReader<byte>(buffer); |
| | | 135 | | if (TryRead(ref reader, out integer)) |
| | | 136 | | { |
| | | 137 | | consumed = buffer.GetPosition(reader.Consumed); |
| | | 138 | | return true; |
| | | 139 | | } |
| | | 140 | | else |
| | | 141 | | { |
| | | 142 | | consumed = buffer.Start; |
| | | 143 | | return false; |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | public static bool TryWrite(Span<byte> buffer, long longToEncode, out int bytesWritten) |
| | 0 | 148 | | { |
| | 0 | 149 | | Debug.Assert(longToEncode >= 0); |
| | 0 | 150 | | Debug.Assert(longToEncode <= EightByteLimit); |
| | | 151 | | |
| | 0 | 152 | | if (longToEncode <= OneByteLimit) |
| | 0 | 153 | | { |
| | 0 | 154 | | if (buffer.Length != 0) |
| | 0 | 155 | | { |
| | 0 | 156 | | buffer[0] = (byte)longToEncode; |
| | 0 | 157 | | bytesWritten = 1; |
| | 0 | 158 | | return true; |
| | | 159 | | } |
| | 0 | 160 | | } |
| | 0 | 161 | | else if (longToEncode <= TwoByteLimit) |
| | 0 | 162 | | { |
| | 0 | 163 | | if (BinaryPrimitives.TryWriteUInt16BigEndian(buffer, (ushort)((uint)longToEncode | TwoByteLengthMask))) |
| | 0 | 164 | | { |
| | 0 | 165 | | bytesWritten = 2; |
| | 0 | 166 | | return true; |
| | | 167 | | } |
| | 0 | 168 | | } |
| | 0 | 169 | | else if (longToEncode <= FourByteLimit) |
| | 0 | 170 | | { |
| | 0 | 171 | | if (BinaryPrimitives.TryWriteUInt32BigEndian(buffer, (uint)longToEncode | FourByteLengthMask)) |
| | 0 | 172 | | { |
| | 0 | 173 | | bytesWritten = 4; |
| | 0 | 174 | | return true; |
| | | 175 | | } |
| | 0 | 176 | | } |
| | | 177 | | else // EightByteLimit |
| | 0 | 178 | | { |
| | 0 | 179 | | if (BinaryPrimitives.TryWriteUInt64BigEndian(buffer, (ulong)longToEncode | EightByteLengthMask)) |
| | 0 | 180 | | { |
| | 0 | 181 | | bytesWritten = 8; |
| | 0 | 182 | | return true; |
| | | 183 | | } |
| | 0 | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | bytesWritten = 0; |
| | 0 | 187 | | return false; |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | public static int WriteInteger(Span<byte> buffer, long longToEncode) |
| | 0 | 191 | | { |
| | 0 | 192 | | bool res = TryWrite(buffer, longToEncode, out int bytesWritten); |
| | 0 | 193 | | Debug.Assert(res); |
| | 0 | 194 | | return bytesWritten; |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | public static int GetByteCount(long value) |
| | 0 | 198 | | { |
| | 0 | 199 | | Debug.Assert(value >= 0); |
| | 0 | 200 | | Debug.Assert(value <= EightByteLimit); |
| | | 201 | | |
| | 0 | 202 | | return |
| | 0 | 203 | | value <= OneByteLimit ? 1 : |
| | 0 | 204 | | value <= TwoByteLimit ? 2 : |
| | 0 | 205 | | value <= FourByteLimit ? 4 : |
| | 0 | 206 | | 8; // EightByteLimit |
| | 0 | 207 | | } |
| | | 208 | | } |
| | | 209 | | } |