| | | 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.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Net.Http.HPack |
| | | 7 | | { |
| | | 8 | | internal static class IntegerEncoder |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// The maximum bytes required to encode a 32-bit int, regardless of prefix length. |
| | | 12 | | /// </summary> |
| | | 13 | | public const int MaxInt32EncodedLength = 6; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Encodes an integer into one or more bytes. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="value">The value to encode. Must not be negative.</param> |
| | | 19 | | /// <param name="numBits">The length of the prefix, in bits, to encode <paramref name="value"/> within. Must be |
| | | 20 | | /// <param name="destination">The destination span to encode <paramref name="value"/> to.</param> |
| | | 21 | | /// <param name="bytesWritten">The number of bytes used to encode <paramref name="value"/>.</param> |
| | | 22 | | /// <returns>If <paramref name="destination"/> had enough storage to encode <paramref name="value"/>, true. Othe |
| | | 23 | | public static bool Encode(int value, int numBits, Span<byte> destination, out int bytesWritten) |
| | 196 | 24 | | { |
| | 196 | 25 | | Debug.Assert(value >= 0); |
| | 196 | 26 | | Debug.Assert(numBits >= 1 && numBits <= 8); |
| | | 27 | | |
| | 196 | 28 | | if (destination.Length == 0) |
| | 0 | 29 | | { |
| | 0 | 30 | | bytesWritten = 0; |
| | 0 | 31 | | return false; |
| | | 32 | | } |
| | | 33 | | |
| | 196 | 34 | | destination[0] &= MaskHigh(8 - numBits); |
| | | 35 | | |
| | 196 | 36 | | if (value < (1 << numBits) - 1) |
| | 72 | 37 | | { |
| | 72 | 38 | | destination[0] |= (byte)value; |
| | | 39 | | |
| | 72 | 40 | | bytesWritten = 1; |
| | 72 | 41 | | return true; |
| | | 42 | | } |
| | | 43 | | else |
| | 124 | 44 | | { |
| | 124 | 45 | | destination[0] |= (byte)((1 << numBits) - 1); |
| | | 46 | | |
| | 124 | 47 | | if (1 == destination.Length) |
| | 0 | 48 | | { |
| | 0 | 49 | | bytesWritten = 0; |
| | 0 | 50 | | return false; |
| | | 51 | | } |
| | | 52 | | |
| | 124 | 53 | | value -= ((1 << numBits) - 1); |
| | 124 | 54 | | int i = 1; |
| | | 55 | | |
| | 124 | 56 | | while (value >= 128) |
| | 0 | 57 | | { |
| | 0 | 58 | | destination[i++] = (byte)(value % 128 + 128); |
| | | 59 | | |
| | 0 | 60 | | if (i >= destination.Length) |
| | 0 | 61 | | { |
| | 0 | 62 | | bytesWritten = 0; |
| | 0 | 63 | | return false; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | value /= 128; |
| | 0 | 67 | | } |
| | 124 | 68 | | destination[i++] = (byte)value; |
| | | 69 | | |
| | 124 | 70 | | bytesWritten = i; |
| | 124 | 71 | | return true; |
| | | 72 | | } |
| | 196 | 73 | | } |
| | | 74 | | |
| | 196 | 75 | | private static byte MaskHigh(int n) => (byte)(sbyte.MinValue >> (n - 1)); |
| | | 76 | | } |
| | | 77 | | } |