| | | 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 | | using System.Numerics; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http.HPack |
| | | 8 | | { |
| | | 9 | | internal struct IntegerDecoder |
| | | 10 | | { |
| | | 11 | | private int _i; |
| | | 12 | | private int _m; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Decodes the first byte of the integer. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="b"> |
| | | 18 | | /// The first byte of the variable-length encoded integer. |
| | | 19 | | /// </param> |
| | | 20 | | /// <param name="prefixLength"> |
| | | 21 | | /// The number of lower bits in this prefix byte that the |
| | | 22 | | /// integer has been encoded into. Must be between 1 and 8. |
| | | 23 | | /// Upper bits must be zero. |
| | | 24 | | /// </param> |
| | | 25 | | /// <param name="result"> |
| | | 26 | | /// If decoded successfully, contains the decoded integer. |
| | | 27 | | /// </param> |
| | | 28 | | /// <returns> |
| | | 29 | | /// If the integer has been fully decoded, true. |
| | | 30 | | /// Otherwise, false -- <see cref="TryDecode(byte, out int)"/> must be called on subsequent bytes. |
| | | 31 | | /// </returns> |
| | | 32 | | /// <remarks> |
| | | 33 | | /// The term "prefix" can be confusing. From the HPACK spec: |
| | | 34 | | /// An integer is represented in two parts: a prefix that fills the current octet and an |
| | | 35 | | /// optional list of octets that are used if the integer value does not fit within the prefix. |
| | | 36 | | /// </remarks> |
| | | 37 | | public bool BeginTryDecode(byte b, int prefixLength, out int result) |
| | 0 | 38 | | { |
| | 0 | 39 | | Debug.Assert(prefixLength >= 1 && prefixLength <= 8); |
| | 0 | 40 | | Debug.Assert((b & ~((1 << prefixLength) - 1)) == 0, "bits other than prefix data must be set to 0."); |
| | | 41 | | |
| | 0 | 42 | | if (b < ((1 << prefixLength) - 1)) |
| | 0 | 43 | | { |
| | 0 | 44 | | result = b; |
| | 0 | 45 | | return true; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | _i = b; |
| | 0 | 49 | | _m = 0; |
| | 0 | 50 | | result = 0; |
| | 0 | 51 | | return false; |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Decodes subsequent bytes of an integer. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="b">The next byte.</param> |
| | | 58 | | /// <param name="result"> |
| | | 59 | | /// If decoded successfully, contains the decoded integer. |
| | | 60 | | /// </param> |
| | | 61 | | /// <returns>If the integer has been fully decoded, true. Otherwise, false -- <see cref="TryDecode(byte, out int |
| | | 62 | | public bool TryDecode(byte b, out int result) |
| | 0 | 63 | | { |
| | | 64 | | // Check if shifting b by _m would result in > 31 bits. |
| | | 65 | | // No masking is required: if the 8th bit is set, it indicates there is a |
| | | 66 | | // bit set in a future byte, so it is fine to check that here as if it were |
| | | 67 | | // bit 0 on the next byte. |
| | | 68 | | // This is a simplified form of: |
| | | 69 | | // int additionalBitsRequired = 32 - BitOperations.LeadingZeroCount((uint)b); |
| | | 70 | | // if (_m + additionalBitsRequired > 31) |
| | 0 | 71 | | if (BitOperations.LeadingZeroCount((uint)b) <= _m) |
| | 0 | 72 | | { |
| | 0 | 73 | | throw new HPackDecodingException(SR.net_http_hpack_bad_integer); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | _i += ((b & 0x7f) << _m); |
| | | 77 | | |
| | | 78 | | // If the addition overflowed, the result will be negative. |
| | 0 | 79 | | if (_i < 0) |
| | 0 | 80 | | { |
| | 0 | 81 | | throw new HPackDecodingException(SR.net_http_hpack_bad_integer); |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | _m += 7; |
| | | 85 | | |
| | 0 | 86 | | if ((b & 128) == 0) |
| | 0 | 87 | | { |
| | 0 | 88 | | if (b == 0 && _m / 7 > 1) |
| | 0 | 89 | | { |
| | | 90 | | // Do not accept overlong encodings. |
| | 0 | 91 | | throw new HPackDecodingException(SR.net_http_hpack_bad_integer); |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | result = _i; |
| | 0 | 95 | | return true; |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | result = 0; |
| | 0 | 99 | | return false; |
| | 0 | 100 | | } |
| | | 101 | | } |
| | | 102 | | } |