< Summary

Information
Class: System.Net.Http.HPack.IntegerDecoder
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\Common\src\System\Net\Http\aspnetcore\Http2\Hpack\IntegerDecoder.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 31
Coverable lines: 31
Total lines: 102
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
BeginTryDecode(...)0%440%
TryDecode(...)0%10100%

File(s)

D:\runner\runtime\src\libraries\Common\src\System\Net\Http\aspnetcore\Http2\Hpack\IntegerDecoder.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Numerics;
 6
 7namespace 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)
 038        {
 039            Debug.Assert(prefixLength >= 1 && prefixLength <= 8);
 040            Debug.Assert((b & ~((1 << prefixLength) - 1)) == 0, "bits other than prefix data must be set to 0.");
 41
 042            if (b < ((1 << prefixLength) - 1))
 043            {
 044                result = b;
 045                return true;
 46            }
 47
 048            _i = b;
 049            _m = 0;
 050            result = 0;
 051            return false;
 052        }
 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)
 063        {
 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)
 071            if (BitOperations.LeadingZeroCount((uint)b) <= _m)
 072            {
 073                throw new HPackDecodingException(SR.net_http_hpack_bad_integer);
 74            }
 75
 076            _i += ((b & 0x7f) << _m);
 77
 78            // If the addition overflowed, the result will be negative.
 079            if (_i < 0)
 080            {
 081                throw new HPackDecodingException(SR.net_http_hpack_bad_integer);
 82            }
 83
 084            _m += 7;
 85
 086            if ((b & 128) == 0)
 087            {
 088                if (b == 0 && _m / 7 > 1)
 089                {
 90                    // Do not accept overlong encodings.
 091                    throw new HPackDecodingException(SR.net_http_hpack_bad_integer);
 92                }
 93
 094                result = _i;
 095                return true;
 96            }
 97
 098            result = 0;
 099            return false;
 0100        }
 101    }
 102}