| | | 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 | | #nullable enable |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Threading; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http.HPack |
| | | 9 | | { |
| | | 10 | | internal static class Huffman |
| | | 11 | | { |
| | | 12 | | // HPack static huffman code. see: https://httpwg.org/specs/rfc7541.html#huffman.code |
| | | 13 | | // Stored into two tables to optimize its initialization and memory consumption. |
| | | 14 | | private static ReadOnlySpan<uint> EncodingTableCodes => // 257 |
| | 0 | 15 | | [ |
| | 0 | 16 | | 0b11111111_11000000_00000000_00000000, |
| | 0 | 17 | | 0b11111111_11111111_10110000_00000000, |
| | 0 | 18 | | 0b11111111_11111111_11111110_00100000, |
| | 0 | 19 | | 0b11111111_11111111_11111110_00110000, |
| | 0 | 20 | | 0b11111111_11111111_11111110_01000000, |
| | 0 | 21 | | 0b11111111_11111111_11111110_01010000, |
| | 0 | 22 | | 0b11111111_11111111_11111110_01100000, |
| | 0 | 23 | | 0b11111111_11111111_11111110_01110000, |
| | 0 | 24 | | 0b11111111_11111111_11111110_10000000, |
| | 0 | 25 | | 0b11111111_11111111_11101010_00000000, |
| | 0 | 26 | | 0b11111111_11111111_11111111_11110000, |
| | 0 | 27 | | 0b11111111_11111111_11111110_10010000, |
| | 0 | 28 | | 0b11111111_11111111_11111110_10100000, |
| | 0 | 29 | | 0b11111111_11111111_11111111_11110100, |
| | 0 | 30 | | 0b11111111_11111111_11111110_10110000, |
| | 0 | 31 | | 0b11111111_11111111_11111110_11000000, |
| | 0 | 32 | | 0b11111111_11111111_11111110_11010000, |
| | 0 | 33 | | 0b11111111_11111111_11111110_11100000, |
| | 0 | 34 | | 0b11111111_11111111_11111110_11110000, |
| | 0 | 35 | | 0b11111111_11111111_11111111_00000000, |
| | 0 | 36 | | 0b11111111_11111111_11111111_00010000, |
| | 0 | 37 | | 0b11111111_11111111_11111111_00100000, |
| | 0 | 38 | | 0b11111111_11111111_11111111_11111000, |
| | 0 | 39 | | 0b11111111_11111111_11111111_00110000, |
| | 0 | 40 | | 0b11111111_11111111_11111111_01000000, |
| | 0 | 41 | | 0b11111111_11111111_11111111_01010000, |
| | 0 | 42 | | 0b11111111_11111111_11111111_01100000, |
| | 0 | 43 | | 0b11111111_11111111_11111111_01110000, |
| | 0 | 44 | | 0b11111111_11111111_11111111_10000000, |
| | 0 | 45 | | 0b11111111_11111111_11111111_10010000, |
| | 0 | 46 | | 0b11111111_11111111_11111111_10100000, |
| | 0 | 47 | | 0b11111111_11111111_11111111_10110000, |
| | 0 | 48 | | 0b01010000_00000000_00000000_00000000, |
| | 0 | 49 | | 0b11111110_00000000_00000000_00000000, |
| | 0 | 50 | | 0b11111110_01000000_00000000_00000000, |
| | 0 | 51 | | 0b11111111_10100000_00000000_00000000, |
| | 0 | 52 | | 0b11111111_11001000_00000000_00000000, |
| | 0 | 53 | | 0b01010100_00000000_00000000_00000000, |
| | 0 | 54 | | 0b11111000_00000000_00000000_00000000, |
| | 0 | 55 | | 0b11111111_01000000_00000000_00000000, |
| | 0 | 56 | | 0b11111110_10000000_00000000_00000000, |
| | 0 | 57 | | 0b11111110_11000000_00000000_00000000, |
| | 0 | 58 | | 0b11111001_00000000_00000000_00000000, |
| | 0 | 59 | | 0b11111111_01100000_00000000_00000000, |
| | 0 | 60 | | 0b11111010_00000000_00000000_00000000, |
| | 0 | 61 | | 0b01011000_00000000_00000000_00000000, |
| | 0 | 62 | | 0b01011100_00000000_00000000_00000000, |
| | 0 | 63 | | 0b01100000_00000000_00000000_00000000, |
| | 0 | 64 | | 0b00000000_00000000_00000000_00000000, |
| | 0 | 65 | | 0b00001000_00000000_00000000_00000000, |
| | 0 | 66 | | 0b00010000_00000000_00000000_00000000, |
| | 0 | 67 | | 0b01100100_00000000_00000000_00000000, |
| | 0 | 68 | | 0b01101000_00000000_00000000_00000000, |
| | 0 | 69 | | 0b01101100_00000000_00000000_00000000, |
| | 0 | 70 | | 0b01110000_00000000_00000000_00000000, |
| | 0 | 71 | | 0b01110100_00000000_00000000_00000000, |
| | 0 | 72 | | 0b01111000_00000000_00000000_00000000, |
| | 0 | 73 | | 0b01111100_00000000_00000000_00000000, |
| | 0 | 74 | | 0b10111000_00000000_00000000_00000000, |
| | 0 | 75 | | 0b11111011_00000000_00000000_00000000, |
| | 0 | 76 | | 0b11111111_11111000_00000000_00000000, |
| | 0 | 77 | | 0b10000000_00000000_00000000_00000000, |
| | 0 | 78 | | 0b11111111_10110000_00000000_00000000, |
| | 0 | 79 | | 0b11111111_00000000_00000000_00000000, |
| | 0 | 80 | | 0b11111111_11010000_00000000_00000000, |
| | 0 | 81 | | 0b10000100_00000000_00000000_00000000, |
| | 0 | 82 | | 0b10111010_00000000_00000000_00000000, |
| | 0 | 83 | | 0b10111100_00000000_00000000_00000000, |
| | 0 | 84 | | 0b10111110_00000000_00000000_00000000, |
| | 0 | 85 | | 0b11000000_00000000_00000000_00000000, |
| | 0 | 86 | | 0b11000010_00000000_00000000_00000000, |
| | 0 | 87 | | 0b11000100_00000000_00000000_00000000, |
| | 0 | 88 | | 0b11000110_00000000_00000000_00000000, |
| | 0 | 89 | | 0b11001000_00000000_00000000_00000000, |
| | 0 | 90 | | 0b11001010_00000000_00000000_00000000, |
| | 0 | 91 | | 0b11001100_00000000_00000000_00000000, |
| | 0 | 92 | | 0b11001110_00000000_00000000_00000000, |
| | 0 | 93 | | 0b11010000_00000000_00000000_00000000, |
| | 0 | 94 | | 0b11010010_00000000_00000000_00000000, |
| | 0 | 95 | | 0b11010100_00000000_00000000_00000000, |
| | 0 | 96 | | 0b11010110_00000000_00000000_00000000, |
| | 0 | 97 | | 0b11011000_00000000_00000000_00000000, |
| | 0 | 98 | | 0b11011010_00000000_00000000_00000000, |
| | 0 | 99 | | 0b11011100_00000000_00000000_00000000, |
| | 0 | 100 | | 0b11011110_00000000_00000000_00000000, |
| | 0 | 101 | | 0b11100000_00000000_00000000_00000000, |
| | 0 | 102 | | 0b11100010_00000000_00000000_00000000, |
| | 0 | 103 | | 0b11100100_00000000_00000000_00000000, |
| | 0 | 104 | | 0b11111100_00000000_00000000_00000000, |
| | 0 | 105 | | 0b11100110_00000000_00000000_00000000, |
| | 0 | 106 | | 0b11111101_00000000_00000000_00000000, |
| | 0 | 107 | | 0b11111111_11011000_00000000_00000000, |
| | 0 | 108 | | 0b11111111_11111110_00000000_00000000, |
| | 0 | 109 | | 0b11111111_11100000_00000000_00000000, |
| | 0 | 110 | | 0b11111111_11110000_00000000_00000000, |
| | 0 | 111 | | 0b10001000_00000000_00000000_00000000, |
| | 0 | 112 | | 0b11111111_11111010_00000000_00000000, |
| | 0 | 113 | | 0b00011000_00000000_00000000_00000000, |
| | 0 | 114 | | 0b10001100_00000000_00000000_00000000, |
| | 0 | 115 | | 0b00100000_00000000_00000000_00000000, |
| | 0 | 116 | | 0b10010000_00000000_00000000_00000000, |
| | 0 | 117 | | 0b00101000_00000000_00000000_00000000, |
| | 0 | 118 | | 0b10010100_00000000_00000000_00000000, |
| | 0 | 119 | | 0b10011000_00000000_00000000_00000000, |
| | 0 | 120 | | 0b10011100_00000000_00000000_00000000, |
| | 0 | 121 | | 0b00110000_00000000_00000000_00000000, |
| | 0 | 122 | | 0b11101000_00000000_00000000_00000000, |
| | 0 | 123 | | 0b11101010_00000000_00000000_00000000, |
| | 0 | 124 | | 0b10100000_00000000_00000000_00000000, |
| | 0 | 125 | | 0b10100100_00000000_00000000_00000000, |
| | 0 | 126 | | 0b10101000_00000000_00000000_00000000, |
| | 0 | 127 | | 0b00111000_00000000_00000000_00000000, |
| | 0 | 128 | | 0b10101100_00000000_00000000_00000000, |
| | 0 | 129 | | 0b11101100_00000000_00000000_00000000, |
| | 0 | 130 | | 0b10110000_00000000_00000000_00000000, |
| | 0 | 131 | | 0b01000000_00000000_00000000_00000000, |
| | 0 | 132 | | 0b01001000_00000000_00000000_00000000, |
| | 0 | 133 | | 0b10110100_00000000_00000000_00000000, |
| | 0 | 134 | | 0b11101110_00000000_00000000_00000000, |
| | 0 | 135 | | 0b11110000_00000000_00000000_00000000, |
| | 0 | 136 | | 0b11110010_00000000_00000000_00000000, |
| | 0 | 137 | | 0b11110100_00000000_00000000_00000000, |
| | 0 | 138 | | 0b11110110_00000000_00000000_00000000, |
| | 0 | 139 | | 0b11111111_11111100_00000000_00000000, |
| | 0 | 140 | | 0b11111111_10000000_00000000_00000000, |
| | 0 | 141 | | 0b11111111_11110100_00000000_00000000, |
| | 0 | 142 | | 0b11111111_11101000_00000000_00000000, |
| | 0 | 143 | | 0b11111111_11111111_11111111_11000000, |
| | 0 | 144 | | 0b11111111_11111110_01100000_00000000, |
| | 0 | 145 | | 0b11111111_11111111_01001000_00000000, |
| | 0 | 146 | | 0b11111111_11111110_01110000_00000000, |
| | 0 | 147 | | 0b11111111_11111110_10000000_00000000, |
| | 0 | 148 | | 0b11111111_11111111_01001100_00000000, |
| | 0 | 149 | | 0b11111111_11111111_01010000_00000000, |
| | 0 | 150 | | 0b11111111_11111111_01010100_00000000, |
| | 0 | 151 | | 0b11111111_11111111_10110010_00000000, |
| | 0 | 152 | | 0b11111111_11111111_01011000_00000000, |
| | 0 | 153 | | 0b11111111_11111111_10110100_00000000, |
| | 0 | 154 | | 0b11111111_11111111_10110110_00000000, |
| | 0 | 155 | | 0b11111111_11111111_10111000_00000000, |
| | 0 | 156 | | 0b11111111_11111111_10111010_00000000, |
| | 0 | 157 | | 0b11111111_11111111_10111100_00000000, |
| | 0 | 158 | | 0b11111111_11111111_11101011_00000000, |
| | 0 | 159 | | 0b11111111_11111111_10111110_00000000, |
| | 0 | 160 | | 0b11111111_11111111_11101100_00000000, |
| | 0 | 161 | | 0b11111111_11111111_11101101_00000000, |
| | 0 | 162 | | 0b11111111_11111111_01011100_00000000, |
| | 0 | 163 | | 0b11111111_11111111_11000000_00000000, |
| | 0 | 164 | | 0b11111111_11111111_11101110_00000000, |
| | 0 | 165 | | 0b11111111_11111111_11000010_00000000, |
| | 0 | 166 | | 0b11111111_11111111_11000100_00000000, |
| | 0 | 167 | | 0b11111111_11111111_11000110_00000000, |
| | 0 | 168 | | 0b11111111_11111111_11001000_00000000, |
| | 0 | 169 | | 0b11111111_11111110_11100000_00000000, |
| | 0 | 170 | | 0b11111111_11111111_01100000_00000000, |
| | 0 | 171 | | 0b11111111_11111111_11001010_00000000, |
| | 0 | 172 | | 0b11111111_11111111_01100100_00000000, |
| | 0 | 173 | | 0b11111111_11111111_11001100_00000000, |
| | 0 | 174 | | 0b11111111_11111111_11001110_00000000, |
| | 0 | 175 | | 0b11111111_11111111_11101111_00000000, |
| | 0 | 176 | | 0b11111111_11111111_01101000_00000000, |
| | 0 | 177 | | 0b11111111_11111110_11101000_00000000, |
| | 0 | 178 | | 0b11111111_11111110_10010000_00000000, |
| | 0 | 179 | | 0b11111111_11111111_01101100_00000000, |
| | 0 | 180 | | 0b11111111_11111111_01110000_00000000, |
| | 0 | 181 | | 0b11111111_11111111_11010000_00000000, |
| | 0 | 182 | | 0b11111111_11111111_11010010_00000000, |
| | 0 | 183 | | 0b11111111_11111110_11110000_00000000, |
| | 0 | 184 | | 0b11111111_11111111_11010100_00000000, |
| | 0 | 185 | | 0b11111111_11111111_01110100_00000000, |
| | 0 | 186 | | 0b11111111_11111111_01111000_00000000, |
| | 0 | 187 | | 0b11111111_11111111_11110000_00000000, |
| | 0 | 188 | | 0b11111111_11111110_11111000_00000000, |
| | 0 | 189 | | 0b11111111_11111111_01111100_00000000, |
| | 0 | 190 | | 0b11111111_11111111_11010110_00000000, |
| | 0 | 191 | | 0b11111111_11111111_11011000_00000000, |
| | 0 | 192 | | 0b11111111_11111111_00000000_00000000, |
| | 0 | 193 | | 0b11111111_11111111_00001000_00000000, |
| | 0 | 194 | | 0b11111111_11111111_10000000_00000000, |
| | 0 | 195 | | 0b11111111_11111111_00010000_00000000, |
| | 0 | 196 | | 0b11111111_11111111_11011010_00000000, |
| | 0 | 197 | | 0b11111111_11111111_10000100_00000000, |
| | 0 | 198 | | 0b11111111_11111111_11011100_00000000, |
| | 0 | 199 | | 0b11111111_11111111_11011110_00000000, |
| | 0 | 200 | | 0b11111111_11111110_10100000_00000000, |
| | 0 | 201 | | 0b11111111_11111111_10001000_00000000, |
| | 0 | 202 | | 0b11111111_11111111_10001100_00000000, |
| | 0 | 203 | | 0b11111111_11111111_10010000_00000000, |
| | 0 | 204 | | 0b11111111_11111111_11100000_00000000, |
| | 0 | 205 | | 0b11111111_11111111_10010100_00000000, |
| | 0 | 206 | | 0b11111111_11111111_10011000_00000000, |
| | 0 | 207 | | 0b11111111_11111111_11100010_00000000, |
| | 0 | 208 | | 0b11111111_11111111_11111000_00000000, |
| | 0 | 209 | | 0b11111111_11111111_11111000_01000000, |
| | 0 | 210 | | 0b11111111_11111110_10110000_00000000, |
| | 0 | 211 | | 0b11111111_11111110_00100000_00000000, |
| | 0 | 212 | | 0b11111111_11111111_10011100_00000000, |
| | 0 | 213 | | 0b11111111_11111111_11100100_00000000, |
| | 0 | 214 | | 0b11111111_11111111_10100000_00000000, |
| | 0 | 215 | | 0b11111111_11111111_11110110_00000000, |
| | 0 | 216 | | 0b11111111_11111111_11111000_10000000, |
| | 0 | 217 | | 0b11111111_11111111_11111000_11000000, |
| | 0 | 218 | | 0b11111111_11111111_11111001_00000000, |
| | 0 | 219 | | 0b11111111_11111111_11111011_11000000, |
| | 0 | 220 | | 0b11111111_11111111_11111011_11100000, |
| | 0 | 221 | | 0b11111111_11111111_11111001_01000000, |
| | 0 | 222 | | 0b11111111_11111111_11110001_00000000, |
| | 0 | 223 | | 0b11111111_11111111_11110110_10000000, |
| | 0 | 224 | | 0b11111111_11111110_01000000_00000000, |
| | 0 | 225 | | 0b11111111_11111111_00011000_00000000, |
| | 0 | 226 | | 0b11111111_11111111_11111001_10000000, |
| | 0 | 227 | | 0b11111111_11111111_11111100_00000000, |
| | 0 | 228 | | 0b11111111_11111111_11111100_00100000, |
| | 0 | 229 | | 0b11111111_11111111_11111001_11000000, |
| | 0 | 230 | | 0b11111111_11111111_11111100_01000000, |
| | 0 | 231 | | 0b11111111_11111111_11110010_00000000, |
| | 0 | 232 | | 0b11111111_11111111_00100000_00000000, |
| | 0 | 233 | | 0b11111111_11111111_00101000_00000000, |
| | 0 | 234 | | 0b11111111_11111111_11111010_00000000, |
| | 0 | 235 | | 0b11111111_11111111_11111010_01000000, |
| | 0 | 236 | | 0b11111111_11111111_11111111_11010000, |
| | 0 | 237 | | 0b11111111_11111111_11111100_01100000, |
| | 0 | 238 | | 0b11111111_11111111_11111100_10000000, |
| | 0 | 239 | | 0b11111111_11111111_11111100_10100000, |
| | 0 | 240 | | 0b11111111_11111110_11000000_00000000, |
| | 0 | 241 | | 0b11111111_11111111_11110011_00000000, |
| | 0 | 242 | | 0b11111111_11111110_11010000_00000000, |
| | 0 | 243 | | 0b11111111_11111111_00110000_00000000, |
| | 0 | 244 | | 0b11111111_11111111_10100100_00000000, |
| | 0 | 245 | | 0b11111111_11111111_00111000_00000000, |
| | 0 | 246 | | 0b11111111_11111111_01000000_00000000, |
| | 0 | 247 | | 0b11111111_11111111_11100110_00000000, |
| | 0 | 248 | | 0b11111111_11111111_10101000_00000000, |
| | 0 | 249 | | 0b11111111_11111111_10101100_00000000, |
| | 0 | 250 | | 0b11111111_11111111_11110111_00000000, |
| | 0 | 251 | | 0b11111111_11111111_11110111_10000000, |
| | 0 | 252 | | 0b11111111_11111111_11110100_00000000, |
| | 0 | 253 | | 0b11111111_11111111_11110101_00000000, |
| | 0 | 254 | | 0b11111111_11111111_11111010_10000000, |
| | 0 | 255 | | 0b11111111_11111111_11101000_00000000, |
| | 0 | 256 | | 0b11111111_11111111_11111010_11000000, |
| | 0 | 257 | | 0b11111111_11111111_11111100_11000000, |
| | 0 | 258 | | 0b11111111_11111111_11111011_00000000, |
| | 0 | 259 | | 0b11111111_11111111_11111011_01000000, |
| | 0 | 260 | | 0b11111111_11111111_11111100_11100000, |
| | 0 | 261 | | 0b11111111_11111111_11111101_00000000, |
| | 0 | 262 | | 0b11111111_11111111_11111101_00100000, |
| | 0 | 263 | | 0b11111111_11111111_11111101_01000000, |
| | 0 | 264 | | 0b11111111_11111111_11111101_01100000, |
| | 0 | 265 | | 0b11111111_11111111_11111111_11100000, |
| | 0 | 266 | | 0b11111111_11111111_11111101_10000000, |
| | 0 | 267 | | 0b11111111_11111111_11111101_10100000, |
| | 0 | 268 | | 0b11111111_11111111_11111101_11000000, |
| | 0 | 269 | | 0b11111111_11111111_11111101_11100000, |
| | 0 | 270 | | 0b11111111_11111111_11111110_00000000, |
| | 0 | 271 | | 0b11111111_11111111_11111011_10000000, |
| | 0 | 272 | | 0b11111111_11111111_11111111_11111100, |
| | 0 | 273 | | ]; |
| | | 274 | | |
| | | 275 | | private static ReadOnlySpan<byte> EncodingTableBitLengths => // 257 |
| | 0 | 276 | | [ |
| | 0 | 277 | | 13, |
| | 0 | 278 | | 23, |
| | 0 | 279 | | 28, |
| | 0 | 280 | | 28, |
| | 0 | 281 | | 28, |
| | 0 | 282 | | 28, |
| | 0 | 283 | | 28, |
| | 0 | 284 | | 28, |
| | 0 | 285 | | 28, |
| | 0 | 286 | | 24, |
| | 0 | 287 | | 30, |
| | 0 | 288 | | 28, |
| | 0 | 289 | | 28, |
| | 0 | 290 | | 30, |
| | 0 | 291 | | 28, |
| | 0 | 292 | | 28, |
| | 0 | 293 | | 28, |
| | 0 | 294 | | 28, |
| | 0 | 295 | | 28, |
| | 0 | 296 | | 28, |
| | 0 | 297 | | 28, |
| | 0 | 298 | | 28, |
| | 0 | 299 | | 30, |
| | 0 | 300 | | 28, |
| | 0 | 301 | | 28, |
| | 0 | 302 | | 28, |
| | 0 | 303 | | 28, |
| | 0 | 304 | | 28, |
| | 0 | 305 | | 28, |
| | 0 | 306 | | 28, |
| | 0 | 307 | | 28, |
| | 0 | 308 | | 28, |
| | 0 | 309 | | 6, |
| | 0 | 310 | | 10, |
| | 0 | 311 | | 10, |
| | 0 | 312 | | 12, |
| | 0 | 313 | | 13, |
| | 0 | 314 | | 6, |
| | 0 | 315 | | 8, |
| | 0 | 316 | | 11, |
| | 0 | 317 | | 10, |
| | 0 | 318 | | 10, |
| | 0 | 319 | | 8, |
| | 0 | 320 | | 11, |
| | 0 | 321 | | 8, |
| | 0 | 322 | | 6, |
| | 0 | 323 | | 6, |
| | 0 | 324 | | 6, |
| | 0 | 325 | | 5, |
| | 0 | 326 | | 5, |
| | 0 | 327 | | 5, |
| | 0 | 328 | | 6, |
| | 0 | 329 | | 6, |
| | 0 | 330 | | 6, |
| | 0 | 331 | | 6, |
| | 0 | 332 | | 6, |
| | 0 | 333 | | 6, |
| | 0 | 334 | | 6, |
| | 0 | 335 | | 7, |
| | 0 | 336 | | 8, |
| | 0 | 337 | | 15, |
| | 0 | 338 | | 6, |
| | 0 | 339 | | 12, |
| | 0 | 340 | | 10, |
| | 0 | 341 | | 13, |
| | 0 | 342 | | 6, |
| | 0 | 343 | | 7, |
| | 0 | 344 | | 7, |
| | 0 | 345 | | 7, |
| | 0 | 346 | | 7, |
| | 0 | 347 | | 7, |
| | 0 | 348 | | 7, |
| | 0 | 349 | | 7, |
| | 0 | 350 | | 7, |
| | 0 | 351 | | 7, |
| | 0 | 352 | | 7, |
| | 0 | 353 | | 7, |
| | 0 | 354 | | 7, |
| | 0 | 355 | | 7, |
| | 0 | 356 | | 7, |
| | 0 | 357 | | 7, |
| | 0 | 358 | | 7, |
| | 0 | 359 | | 7, |
| | 0 | 360 | | 7, |
| | 0 | 361 | | 7, |
| | 0 | 362 | | 7, |
| | 0 | 363 | | 7, |
| | 0 | 364 | | 7, |
| | 0 | 365 | | 8, |
| | 0 | 366 | | 7, |
| | 0 | 367 | | 8, |
| | 0 | 368 | | 13, |
| | 0 | 369 | | 19, |
| | 0 | 370 | | 13, |
| | 0 | 371 | | 14, |
| | 0 | 372 | | 6, |
| | 0 | 373 | | 15, |
| | 0 | 374 | | 5, |
| | 0 | 375 | | 6, |
| | 0 | 376 | | 5, |
| | 0 | 377 | | 6, |
| | 0 | 378 | | 5, |
| | 0 | 379 | | 6, |
| | 0 | 380 | | 6, |
| | 0 | 381 | | 6, |
| | 0 | 382 | | 5, |
| | 0 | 383 | | 7, |
| | 0 | 384 | | 7, |
| | 0 | 385 | | 6, |
| | 0 | 386 | | 6, |
| | 0 | 387 | | 6, |
| | 0 | 388 | | 5, |
| | 0 | 389 | | 6, |
| | 0 | 390 | | 7, |
| | 0 | 391 | | 6, |
| | 0 | 392 | | 5, |
| | 0 | 393 | | 5, |
| | 0 | 394 | | 6, |
| | 0 | 395 | | 7, |
| | 0 | 396 | | 7, |
| | 0 | 397 | | 7, |
| | 0 | 398 | | 7, |
| | 0 | 399 | | 7, |
| | 0 | 400 | | 15, |
| | 0 | 401 | | 11, |
| | 0 | 402 | | 14, |
| | 0 | 403 | | 13, |
| | 0 | 404 | | 28, |
| | 0 | 405 | | 20, |
| | 0 | 406 | | 22, |
| | 0 | 407 | | 20, |
| | 0 | 408 | | 20, |
| | 0 | 409 | | 22, |
| | 0 | 410 | | 22, |
| | 0 | 411 | | 22, |
| | 0 | 412 | | 23, |
| | 0 | 413 | | 22, |
| | 0 | 414 | | 23, |
| | 0 | 415 | | 23, |
| | 0 | 416 | | 23, |
| | 0 | 417 | | 23, |
| | 0 | 418 | | 23, |
| | 0 | 419 | | 24, |
| | 0 | 420 | | 23, |
| | 0 | 421 | | 24, |
| | 0 | 422 | | 24, |
| | 0 | 423 | | 22, |
| | 0 | 424 | | 23, |
| | 0 | 425 | | 24, |
| | 0 | 426 | | 23, |
| | 0 | 427 | | 23, |
| | 0 | 428 | | 23, |
| | 0 | 429 | | 23, |
| | 0 | 430 | | 21, |
| | 0 | 431 | | 22, |
| | 0 | 432 | | 23, |
| | 0 | 433 | | 22, |
| | 0 | 434 | | 23, |
| | 0 | 435 | | 23, |
| | 0 | 436 | | 24, |
| | 0 | 437 | | 22, |
| | 0 | 438 | | 21, |
| | 0 | 439 | | 20, |
| | 0 | 440 | | 22, |
| | 0 | 441 | | 22, |
| | 0 | 442 | | 23, |
| | 0 | 443 | | 23, |
| | 0 | 444 | | 21, |
| | 0 | 445 | | 23, |
| | 0 | 446 | | 22, |
| | 0 | 447 | | 22, |
| | 0 | 448 | | 24, |
| | 0 | 449 | | 21, |
| | 0 | 450 | | 22, |
| | 0 | 451 | | 23, |
| | 0 | 452 | | 23, |
| | 0 | 453 | | 21, |
| | 0 | 454 | | 21, |
| | 0 | 455 | | 22, |
| | 0 | 456 | | 21, |
| | 0 | 457 | | 23, |
| | 0 | 458 | | 22, |
| | 0 | 459 | | 23, |
| | 0 | 460 | | 23, |
| | 0 | 461 | | 20, |
| | 0 | 462 | | 22, |
| | 0 | 463 | | 22, |
| | 0 | 464 | | 22, |
| | 0 | 465 | | 23, |
| | 0 | 466 | | 22, |
| | 0 | 467 | | 22, |
| | 0 | 468 | | 23, |
| | 0 | 469 | | 26, |
| | 0 | 470 | | 26, |
| | 0 | 471 | | 20, |
| | 0 | 472 | | 19, |
| | 0 | 473 | | 22, |
| | 0 | 474 | | 23, |
| | 0 | 475 | | 22, |
| | 0 | 476 | | 25, |
| | 0 | 477 | | 26, |
| | 0 | 478 | | 26, |
| | 0 | 479 | | 26, |
| | 0 | 480 | | 27, |
| | 0 | 481 | | 27, |
| | 0 | 482 | | 26, |
| | 0 | 483 | | 24, |
| | 0 | 484 | | 25, |
| | 0 | 485 | | 19, |
| | 0 | 486 | | 21, |
| | 0 | 487 | | 26, |
| | 0 | 488 | | 27, |
| | 0 | 489 | | 27, |
| | 0 | 490 | | 26, |
| | 0 | 491 | | 27, |
| | 0 | 492 | | 24, |
| | 0 | 493 | | 21, |
| | 0 | 494 | | 21, |
| | 0 | 495 | | 26, |
| | 0 | 496 | | 26, |
| | 0 | 497 | | 28, |
| | 0 | 498 | | 27, |
| | 0 | 499 | | 27, |
| | 0 | 500 | | 27, |
| | 0 | 501 | | 20, |
| | 0 | 502 | | 24, |
| | 0 | 503 | | 20, |
| | 0 | 504 | | 21, |
| | 0 | 505 | | 22, |
| | 0 | 506 | | 21, |
| | 0 | 507 | | 21, |
| | 0 | 508 | | 23, |
| | 0 | 509 | | 22, |
| | 0 | 510 | | 22, |
| | 0 | 511 | | 25, |
| | 0 | 512 | | 25, |
| | 0 | 513 | | 24, |
| | 0 | 514 | | 24, |
| | 0 | 515 | | 26, |
| | 0 | 516 | | 23, |
| | 0 | 517 | | 26, |
| | 0 | 518 | | 27, |
| | 0 | 519 | | 26, |
| | 0 | 520 | | 26, |
| | 0 | 521 | | 27, |
| | 0 | 522 | | 27, |
| | 0 | 523 | | 27, |
| | 0 | 524 | | 27, |
| | 0 | 525 | | 27, |
| | 0 | 526 | | 28, |
| | 0 | 527 | | 27, |
| | 0 | 528 | | 27, |
| | 0 | 529 | | 27, |
| | 0 | 530 | | 27, |
| | 0 | 531 | | 27, |
| | 0 | 532 | | 26, |
| | 0 | 533 | | 30 |
| | 0 | 534 | | ]; |
| | | 535 | | |
| | 0 | 536 | | private static readonly ushort[] s_decodingTree = GenerateDecodingLookupTree(); |
| | | 537 | | |
| | | 538 | | public static (uint encoded, int bitLength) Encode(int data) |
| | | 539 | | { |
| | | 540 | | return (EncodingTableCodes[data], EncodingTableBitLengths[data]); |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | private static ushort[] GenerateDecodingLookupTree() |
| | 0 | 544 | | { |
| | | 545 | | // Decoding lookup tree is a tree of 8 bit lookup tables stored in |
| | | 546 | | // one dimensional array of ushort to reduce allocations. |
| | | 547 | | // First 256 ushort is lookup table with index 0, next 256 ushort is lookup table with index 1, etc... |
| | | 548 | | // lookup_value = [(lookup_table_index << 8) + lookup_index] |
| | | 549 | | |
| | | 550 | | // lookup_index is next 8 bits of huffman code, if there is less than 8 bits in source. |
| | | 551 | | // lookup_index MUST be aligned to 8 bits with LSB bits set to anything (zeros are recommended). |
| | | 552 | | |
| | | 553 | | // Lookup value is encoded in ushort as either. |
| | | 554 | | // ----------------------------------------------------------------- |
| | | 555 | | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| | | 556 | | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| | | 557 | | // | 1 | next_lookup_table_index | not_used | |
| | | 558 | | // +---+---------------------------+-------------------------------+ |
| | | 559 | | // or |
| | | 560 | | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| | | 561 | | // | 0 | number_of_used_bits | octet | |
| | | 562 | | // +---+---------------------------+-------------------------------+ |
| | | 563 | | |
| | | 564 | | // Bit 15 unset indicates a leaf value of decoding tree. |
| | | 565 | | // For example value 0x0241 means that we have reached end of huffman code |
| | | 566 | | // with result byte 0x41 'A' and from lookup bits only rightmost 2 bits were used |
| | | 567 | | // and rest of bits are part of next huffman code. |
| | | 568 | | |
| | | 569 | | // Bit 15 set indicates that code is not yet decoded and next lookup table index shall be used |
| | | 570 | | // for next n bits of huffman code. |
| | | 571 | | // 0 in 'next lookup table index' is considered as decoding error - invalid huffman code |
| | | 572 | | |
| | | 573 | | // Because HPack uses static huffman code defined in RFC https://httpwg.org/specs/rfc7541.html#huffman.code |
| | | 574 | | // it is guaranteed that for this huffman code generated decoding lookup tree MUST consist of exactly 15 loo |
| | 0 | 575 | | var decodingTree = new ushort[15 * 256]; |
| | | 576 | | |
| | 0 | 577 | | ReadOnlySpan<uint> encodingTableCodes = EncodingTableCodes; |
| | 0 | 578 | | ReadOnlySpan<byte> encodingTableBitLengths = EncodingTableBitLengths; |
| | | 579 | | |
| | 0 | 580 | | int allocatedLookupTableIndex = 0; |
| | | 581 | | // Create traverse path for all 0..256 octets, 256 is EOS, see: http://httpwg.org/specs/rfc7541.html#rfc.sec |
| | 0 | 582 | | for (int octet = 0; octet <= 256; octet++) |
| | 0 | 583 | | { |
| | 0 | 584 | | uint code = encodingTableCodes[octet]; |
| | 0 | 585 | | int bitLength = encodingTableBitLengths[octet]; |
| | | 586 | | |
| | 0 | 587 | | int lookupTableIndex = 0; |
| | 0 | 588 | | int bitsLeft = bitLength; |
| | 0 | 589 | | while (bitsLeft > 0) |
| | 0 | 590 | | { |
| | | 591 | | // read next 8 bits from huffman code |
| | 0 | 592 | | int indexInLookupTable = (int)(code >> (32 - 8)); |
| | | 593 | | |
| | 0 | 594 | | if (bitsLeft <= 8) |
| | 0 | 595 | | { |
| | | 596 | | // Reached last lookup table for this huffman code. |
| | | 597 | | |
| | | 598 | | // Identical lookup value has to be stored for every combination of unused bits, |
| | | 599 | | // For example: 12 bit code could be looked up during decoding as this: |
| | | 600 | | // --------------------------------- |
| | | 601 | | // 7 6 5 4 3 2 1 0 |
| | | 602 | | // +---+---+---+---+---+---+---+---+ |
| | | 603 | | // |last_code_bits | next_code_bits| |
| | | 604 | | // +-------------------------------+ |
| | | 605 | | // next_code_bits are 'random' bits of next huffman code, so in order for lookup |
| | | 606 | | // to work, lookup value has to be stored for all 4 unused bits, in this case for suffix 0..15 |
| | 0 | 607 | | int suffixCount = 1 << (8 - bitsLeft); |
| | 0 | 608 | | for (int suffix = 0; suffix < suffixCount; suffix++) |
| | 0 | 609 | | { |
| | 0 | 610 | | if (octet == 256) |
| | 0 | 611 | | { |
| | | 612 | | // EOS (in our case 256) have special meaning in HPack static huffman code |
| | | 613 | | // see: http://httpwg.org/specs/rfc7541.html#rfc.section.5.2 |
| | | 614 | | // > A Huffman-encoded string literal containing the EOS symbol MUST be treated as a dec |
| | | 615 | | // To force decoding error we store 0 as 'next lookup table index' which MUST be treated |
| | | 616 | | |
| | | 617 | | // Invalid huffman code - EOS |
| | | 618 | | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| | | 619 | | // | 1 | 0 0 0 0 0 0 0 | 1 1 1 1 1 1 1 1 | |
| | | 620 | | // +---+---------------------------+-------------------------------+ |
| | 0 | 621 | | decodingTree[(lookupTableIndex << 8) + (indexInLookupTable | suffix)] = 0x80ff; |
| | 0 | 622 | | } |
| | | 623 | | else |
| | 0 | 624 | | { |
| | | 625 | | // Leaf lookup value |
| | | 626 | | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| | | 627 | | // | 0 | number_of_used_bits | code | |
| | | 628 | | // +---+---------------------------+-------------------------------+ |
| | 0 | 629 | | decodingTree[(lookupTableIndex << 8) + (indexInLookupTable | suffix)] = (ushort)((bitsLe |
| | 0 | 630 | | } |
| | 0 | 631 | | } |
| | 0 | 632 | | } |
| | | 633 | | else |
| | 0 | 634 | | { |
| | | 635 | | // More than 8 bits left in huffman code means that we need to traverse to another lookup table |
| | 0 | 636 | | ushort lookupValue = decodingTree[(lookupTableIndex << 8) + indexInLookupTable]; |
| | | 637 | | |
| | | 638 | | // Because next_lookup_table_index can not be 0, as 0 is index of root table, default value of a |
| | | 639 | | // means that we have not initialized it yet => lookup table MUST be allocated and its index ass |
| | | 640 | | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| | | 641 | | // | 1 | next_lookup_table_index | not_used | |
| | | 642 | | // +---+---------------------------+-------------------------------+ |
| | 0 | 643 | | if (lookupValue == 0) |
| | 0 | 644 | | { |
| | 0 | 645 | | ++allocatedLookupTableIndex; |
| | 0 | 646 | | decodingTree[(lookupTableIndex << 8) + indexInLookupTable] = (ushort)((0x80 | allocatedLooku |
| | 0 | 647 | | lookupTableIndex = allocatedLookupTableIndex; |
| | 0 | 648 | | } |
| | | 649 | | else |
| | 0 | 650 | | { |
| | 0 | 651 | | lookupTableIndex = (lookupValue & 0x7f00) >> 8; |
| | 0 | 652 | | } |
| | 0 | 653 | | } |
| | | 654 | | |
| | 0 | 655 | | bitsLeft -= 8; |
| | 0 | 656 | | code <<= 8; |
| | 0 | 657 | | } |
| | 0 | 658 | | } |
| | | 659 | | |
| | 0 | 660 | | return decodingTree; |
| | 0 | 661 | | } |
| | | 662 | | |
| | | 663 | | /// <summary> |
| | | 664 | | /// Decodes a Huffman encoded string from a byte array. |
| | | 665 | | /// </summary> |
| | | 666 | | /// <param name="src">The source byte array containing the encoded data.</param> |
| | | 667 | | /// <param name="dstArray">The destination byte array to store the decoded data. This may grow if its size is i |
| | | 668 | | /// <returns>The number of decoded symbols.</returns> |
| | | 669 | | public static int Decode(ReadOnlySpan<byte> src, ref byte[] dstArray) |
| | 0 | 670 | | { |
| | | 671 | | // The code below implements the decoding logic for an HPack huffman encoded literal values. |
| | | 672 | | // https://httpwg.org/specs/rfc7541.html#string.literal.representation |
| | | 673 | | // |
| | | 674 | | // To decode a symbol, we traverse the decoding lookup table tree by 8 bits for each lookup |
| | | 675 | | // until we found a leaf - which contains decoded symbol (octet) |
| | | 676 | | // |
| | | 677 | | // see comments in GenerateDecodingLookupTree() describing decoding table |
| | | 678 | | |
| | 0 | 679 | | Span<byte> dst = dstArray; |
| | 0 | 680 | | Debug.Assert(dst.Length > 0); |
| | | 681 | | |
| | 0 | 682 | | ushort[] decodingTree = s_decodingTree; |
| | | 683 | | |
| | 0 | 684 | | int lookupTableIndex = 0; |
| | | 685 | | int lookupIndex; |
| | | 686 | | |
| | 0 | 687 | | uint acc = 0; |
| | 0 | 688 | | int bitsInAcc = 0; |
| | | 689 | | |
| | 0 | 690 | | int i = 0; |
| | 0 | 691 | | int j = 0; |
| | 0 | 692 | | while (i < src.Length) |
| | 0 | 693 | | { |
| | | 694 | | // Load next 8 bits into accumulator. |
| | 0 | 695 | | acc <<= 8; |
| | 0 | 696 | | acc |= src[i++]; |
| | 0 | 697 | | bitsInAcc += 8; |
| | | 698 | | |
| | | 699 | | // Decode bits in accumulator. |
| | | 700 | | do |
| | 0 | 701 | | { |
| | 0 | 702 | | lookupIndex = (byte)(acc >> (bitsInAcc - 8)); |
| | | 703 | | |
| | 0 | 704 | | int lookupValue = decodingTree[(lookupTableIndex << 8) + lookupIndex]; |
| | | 705 | | |
| | 0 | 706 | | if (lookupValue < 0x80_00) |
| | 0 | 707 | | { |
| | | 708 | | // Octet found. |
| | | 709 | | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| | | 710 | | // | 0 | number_of_used_bits | octet | |
| | | 711 | | // +---+---------------------------+-------------------------------+ |
| | 0 | 712 | | if (j == dst.Length) |
| | 0 | 713 | | { |
| | 0 | 714 | | Array.Resize(ref dstArray, dst.Length * 2); |
| | 0 | 715 | | dst = dstArray; |
| | 0 | 716 | | } |
| | 0 | 717 | | dst[j++] = (byte)lookupValue; |
| | | 718 | | |
| | | 719 | | // Start lookup of next symbol |
| | 0 | 720 | | lookupTableIndex = 0; |
| | 0 | 721 | | bitsInAcc -= lookupValue >> 8; |
| | 0 | 722 | | } |
| | | 723 | | else |
| | 0 | 724 | | { |
| | | 725 | | // Traverse to next lookup table. |
| | | 726 | | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| | | 727 | | // | 1 | next_lookup_table_index | not_used | |
| | | 728 | | // +---+---------------------------+-------------------------------+ |
| | 0 | 729 | | lookupTableIndex = (lookupValue & 0x7f00) >> 8; |
| | 0 | 730 | | if (lookupTableIndex == 0) |
| | 0 | 731 | | { |
| | | 732 | | // No valid symbol could be decoded or EOS was decoded |
| | 0 | 733 | | throw new HuffmanDecodingException(SR.net_http_hpack_huffman_decode_failed); |
| | | 734 | | } |
| | 0 | 735 | | bitsInAcc -= 8; |
| | 0 | 736 | | } |
| | 0 | 737 | | } while (bitsInAcc >= 8); |
| | 0 | 738 | | } |
| | | 739 | | |
| | | 740 | | // Finish decoding last < 8 bits of src. |
| | | 741 | | // Processing of the last byte has to handle several corner cases |
| | | 742 | | // so it's extracted outside of the main loop for performance reasons. |
| | 0 | 743 | | while (bitsInAcc > 0) |
| | 0 | 744 | | { |
| | 0 | 745 | | Debug.Assert(bitsInAcc < 8); |
| | | 746 | | |
| | | 747 | | // Check for correct EOS, which is padding with ones till end of byte |
| | | 748 | | // when we STARTED new huffman code in last 8 bits (lookupTableIndex was reset to 0 -> root lookup table |
| | 0 | 749 | | if (lookupTableIndex == 0) |
| | 0 | 750 | | { |
| | | 751 | | // Check if all remaining bits are ones. |
| | 0 | 752 | | uint ones = uint.MaxValue >> (32 - bitsInAcc); |
| | 0 | 753 | | if ((acc & ones) == ones) |
| | 0 | 754 | | { |
| | | 755 | | // Is it a EOS. See: http://httpwg.org/specs/rfc7541.html#rfc.section.5.2 |
| | 0 | 756 | | break; |
| | | 757 | | } |
| | 0 | 758 | | } |
| | | 759 | | |
| | | 760 | | // Lookup index has to be 8 bits aligned to MSB |
| | 0 | 761 | | lookupIndex = (byte)(acc << (8 - bitsInAcc)); |
| | | 762 | | |
| | 0 | 763 | | int lookupValue = decodingTree[(lookupTableIndex << 8) + lookupIndex]; |
| | | 764 | | |
| | 0 | 765 | | if (lookupValue < 0x80_00) |
| | 0 | 766 | | { |
| | | 767 | | // Octet found. |
| | | 768 | | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| | | 769 | | // | 0 | number_of_used_bits | octet | |
| | | 770 | | // +---+---------------------------+-------------------------------+ |
| | 0 | 771 | | bitsInAcc -= lookupValue >> 8; |
| | | 772 | | |
| | 0 | 773 | | if (bitsInAcc < 0) |
| | 0 | 774 | | { |
| | | 775 | | // Last looked up code had more bits than was left in accumulator which indicated invalid or inc |
| | 0 | 776 | | throw new HuffmanDecodingException(SR.net_http_hpack_huffman_decode_failed); |
| | | 777 | | } |
| | | 778 | | |
| | 0 | 779 | | if (j == dst.Length) |
| | 0 | 780 | | { |
| | 0 | 781 | | Array.Resize(ref dstArray, dst.Length * 2); |
| | 0 | 782 | | dst = dstArray; |
| | 0 | 783 | | } |
| | 0 | 784 | | dst[j++] = (byte)lookupValue; |
| | | 785 | | |
| | | 786 | | // Set table index to root - start of new huffman code. |
| | 0 | 787 | | lookupTableIndex = 0; |
| | 0 | 788 | | } |
| | | 789 | | else |
| | 0 | 790 | | { |
| | | 791 | | // Src was depleted in middle of lookup tree or EOS was decoded. |
| | 0 | 792 | | throw new HuffmanDecodingException(SR.net_http_hpack_huffman_decode_failed); |
| | | 793 | | } |
| | 0 | 794 | | } |
| | | 795 | | |
| | 0 | 796 | | if (lookupTableIndex != 0) |
| | 0 | 797 | | { |
| | | 798 | | // Finished in middle of traversing - no valid symbol could be decoded |
| | | 799 | | // or too long EOS padding (7 bits plus). See: http://httpwg.org/specs/rfc7541.html#rfc.section.5.2 |
| | 0 | 800 | | throw new HuffmanDecodingException(SR.net_http_hpack_huffman_decode_failed); |
| | | 801 | | } |
| | | 802 | | |
| | 0 | 803 | | return j; |
| | 0 | 804 | | } |
| | | 805 | | } |
| | | 806 | | } |