OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/http2/hpack/decoder/hpack_entry_type_decoder.h" |
| 6 |
| 7 #include <sstream> |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 namespace net { |
| 12 |
| 13 std::string HpackEntryTypeDecoder::DebugString() const { |
| 14 std::stringstream ss; |
| 15 ss << "HpackEntryTypeDecoder(varint_decoder=" << varint_decoder_.DebugString() |
| 16 << ", entry_type=" << entry_type_ << ")"; |
| 17 return ss.str(); |
| 18 } |
| 19 |
| 20 std::ostream& operator<<(std::ostream& out, const HpackEntryTypeDecoder& v) { |
| 21 return out << v.DebugString(); |
| 22 } |
| 23 |
| 24 // This ridiculous looking function turned out to be the winner in benchmarking |
| 25 // of several very different alternative implementations. It would be even |
| 26 // faster (~7%) if inlined in the header file, but I'm not sure if that is |
| 27 // worth doing... yet. |
| 28 // TODO(jamessynge): Benchmark again at a higher level (e.g. at least at the |
| 29 // full HTTP/2 decoder level, but preferably still higher) to determine if the |
| 30 // alternatives that take less code/data space are preferable in that situation. |
| 31 DecodeStatus HpackEntryTypeDecoder::Start(DecodeBuffer* db) { |
| 32 DCHECK(db != nullptr); |
| 33 DCHECK(db->HasData()); |
| 34 |
| 35 // The high four bits (nibble) of first byte of the entry determine the type |
| 36 // of the entry, and may also be the initial bits of the varint that |
| 37 // represents an index or table size. Note the use of the word 'initial' |
| 38 // rather than 'high'; the HPACK encoding of varints is not in network |
| 39 // order (i.e. not big-endian, the high-order byte isn't first), nor in |
| 40 // little-endian order. See: |
| 41 // http://httpwg.org/specs/rfc7541.html#integer.representation |
| 42 uint8_t byte = db->DecodeUInt8(); |
| 43 switch (byte) { |
| 44 case 0b00000000: |
| 45 case 0b00000001: |
| 46 case 0b00000010: |
| 47 case 0b00000011: |
| 48 case 0b00000100: |
| 49 case 0b00000101: |
| 50 case 0b00000110: |
| 51 case 0b00000111: |
| 52 case 0b00001000: |
| 53 case 0b00001001: |
| 54 case 0b00001010: |
| 55 case 0b00001011: |
| 56 case 0b00001100: |
| 57 case 0b00001101: |
| 58 case 0b00001110: |
| 59 // The low 4 bits of |byte| are the initial bits of the varint. |
| 60 // One of those bits is 0, so the varint is only one byte long. |
| 61 entry_type_ = HpackEntryType::kUnindexedLiteralHeader; |
| 62 varint_decoder_.set_value(byte); |
| 63 return DecodeStatus::kDecodeDone; |
| 64 |
| 65 case 0b00001111: |
| 66 // The low 4 bits of |byte| are the initial bits of the varint. All 4 |
| 67 // are 1, so the varint extends into another byte. |
| 68 entry_type_ = HpackEntryType::kUnindexedLiteralHeader; |
| 69 return varint_decoder_.StartExtended(0x0f, db); |
| 70 |
| 71 case 0b00010000: |
| 72 case 0b00010001: |
| 73 case 0b00010010: |
| 74 case 0b00010011: |
| 75 case 0b00010100: |
| 76 case 0b00010101: |
| 77 case 0b00010110: |
| 78 case 0b00010111: |
| 79 case 0b00011000: |
| 80 case 0b00011001: |
| 81 case 0b00011010: |
| 82 case 0b00011011: |
| 83 case 0b00011100: |
| 84 case 0b00011101: |
| 85 case 0b00011110: |
| 86 // The low 4 bits of |byte| are the initial bits of the varint. |
| 87 // One of those bits is 0, so the varint is only one byte long. |
| 88 entry_type_ = HpackEntryType::kNeverIndexedLiteralHeader; |
| 89 varint_decoder_.set_value(byte & 0x0f); |
| 90 return DecodeStatus::kDecodeDone; |
| 91 |
| 92 case 0b00011111: |
| 93 // The low 4 bits of |byte| are the initial bits of the varint. |
| 94 // All of those bits are 1, so the varint extends into another byte. |
| 95 entry_type_ = HpackEntryType::kNeverIndexedLiteralHeader; |
| 96 return varint_decoder_.StartExtended(0x0f, db); |
| 97 |
| 98 case 0b00100000: |
| 99 case 0b00100001: |
| 100 case 0b00100010: |
| 101 case 0b00100011: |
| 102 case 0b00100100: |
| 103 case 0b00100101: |
| 104 case 0b00100110: |
| 105 case 0b00100111: |
| 106 case 0b00101000: |
| 107 case 0b00101001: |
| 108 case 0b00101010: |
| 109 case 0b00101011: |
| 110 case 0b00101100: |
| 111 case 0b00101101: |
| 112 case 0b00101110: |
| 113 case 0b00101111: |
| 114 case 0b00110000: |
| 115 case 0b00110001: |
| 116 case 0b00110010: |
| 117 case 0b00110011: |
| 118 case 0b00110100: |
| 119 case 0b00110101: |
| 120 case 0b00110110: |
| 121 case 0b00110111: |
| 122 case 0b00111000: |
| 123 case 0b00111001: |
| 124 case 0b00111010: |
| 125 case 0b00111011: |
| 126 case 0b00111100: |
| 127 case 0b00111101: |
| 128 case 0b00111110: |
| 129 entry_type_ = HpackEntryType::kDynamicTableSizeUpdate; |
| 130 // The low 5 bits of |byte| are the initial bits of the varint. |
| 131 // One of those bits is 0, so the varint is only one byte long. |
| 132 varint_decoder_.set_value(byte & 0x01f); |
| 133 return DecodeStatus::kDecodeDone; |
| 134 |
| 135 case 0b00111111: |
| 136 entry_type_ = HpackEntryType::kDynamicTableSizeUpdate; |
| 137 // The low 5 bits of |byte| are the initial bits of the varint. |
| 138 // All of those bits are 1, so the varint extends into another byte. |
| 139 return varint_decoder_.StartExtended(0x1f, db); |
| 140 |
| 141 case 0b01000000: |
| 142 case 0b01000001: |
| 143 case 0b01000010: |
| 144 case 0b01000011: |
| 145 case 0b01000100: |
| 146 case 0b01000101: |
| 147 case 0b01000110: |
| 148 case 0b01000111: |
| 149 case 0b01001000: |
| 150 case 0b01001001: |
| 151 case 0b01001010: |
| 152 case 0b01001011: |
| 153 case 0b01001100: |
| 154 case 0b01001101: |
| 155 case 0b01001110: |
| 156 case 0b01001111: |
| 157 case 0b01010000: |
| 158 case 0b01010001: |
| 159 case 0b01010010: |
| 160 case 0b01010011: |
| 161 case 0b01010100: |
| 162 case 0b01010101: |
| 163 case 0b01010110: |
| 164 case 0b01010111: |
| 165 case 0b01011000: |
| 166 case 0b01011001: |
| 167 case 0b01011010: |
| 168 case 0b01011011: |
| 169 case 0b01011100: |
| 170 case 0b01011101: |
| 171 case 0b01011110: |
| 172 case 0b01011111: |
| 173 case 0b01100000: |
| 174 case 0b01100001: |
| 175 case 0b01100010: |
| 176 case 0b01100011: |
| 177 case 0b01100100: |
| 178 case 0b01100101: |
| 179 case 0b01100110: |
| 180 case 0b01100111: |
| 181 case 0b01101000: |
| 182 case 0b01101001: |
| 183 case 0b01101010: |
| 184 case 0b01101011: |
| 185 case 0b01101100: |
| 186 case 0b01101101: |
| 187 case 0b01101110: |
| 188 case 0b01101111: |
| 189 case 0b01110000: |
| 190 case 0b01110001: |
| 191 case 0b01110010: |
| 192 case 0b01110011: |
| 193 case 0b01110100: |
| 194 case 0b01110101: |
| 195 case 0b01110110: |
| 196 case 0b01110111: |
| 197 case 0b01111000: |
| 198 case 0b01111001: |
| 199 case 0b01111010: |
| 200 case 0b01111011: |
| 201 case 0b01111100: |
| 202 case 0b01111101: |
| 203 case 0b01111110: |
| 204 entry_type_ = HpackEntryType::kIndexedLiteralHeader; |
| 205 // The low 6 bits of |byte| are the initial bits of the varint. |
| 206 // One of those bits is 0, so the varint is only one byte long. |
| 207 varint_decoder_.set_value(byte & 0x03f); |
| 208 return DecodeStatus::kDecodeDone; |
| 209 |
| 210 case 0b01111111: |
| 211 entry_type_ = HpackEntryType::kIndexedLiteralHeader; |
| 212 // The low 6 bits of |byte| are the initial bits of the varint. |
| 213 // All of those bits are 1, so the varint extends into another byte. |
| 214 return varint_decoder_.StartExtended(0x3f, db); |
| 215 |
| 216 case 0b10000000: |
| 217 case 0b10000001: |
| 218 case 0b10000010: |
| 219 case 0b10000011: |
| 220 case 0b10000100: |
| 221 case 0b10000101: |
| 222 case 0b10000110: |
| 223 case 0b10000111: |
| 224 case 0b10001000: |
| 225 case 0b10001001: |
| 226 case 0b10001010: |
| 227 case 0b10001011: |
| 228 case 0b10001100: |
| 229 case 0b10001101: |
| 230 case 0b10001110: |
| 231 case 0b10001111: |
| 232 case 0b10010000: |
| 233 case 0b10010001: |
| 234 case 0b10010010: |
| 235 case 0b10010011: |
| 236 case 0b10010100: |
| 237 case 0b10010101: |
| 238 case 0b10010110: |
| 239 case 0b10010111: |
| 240 case 0b10011000: |
| 241 case 0b10011001: |
| 242 case 0b10011010: |
| 243 case 0b10011011: |
| 244 case 0b10011100: |
| 245 case 0b10011101: |
| 246 case 0b10011110: |
| 247 case 0b10011111: |
| 248 case 0b10100000: |
| 249 case 0b10100001: |
| 250 case 0b10100010: |
| 251 case 0b10100011: |
| 252 case 0b10100100: |
| 253 case 0b10100101: |
| 254 case 0b10100110: |
| 255 case 0b10100111: |
| 256 case 0b10101000: |
| 257 case 0b10101001: |
| 258 case 0b10101010: |
| 259 case 0b10101011: |
| 260 case 0b10101100: |
| 261 case 0b10101101: |
| 262 case 0b10101110: |
| 263 case 0b10101111: |
| 264 case 0b10110000: |
| 265 case 0b10110001: |
| 266 case 0b10110010: |
| 267 case 0b10110011: |
| 268 case 0b10110100: |
| 269 case 0b10110101: |
| 270 case 0b10110110: |
| 271 case 0b10110111: |
| 272 case 0b10111000: |
| 273 case 0b10111001: |
| 274 case 0b10111010: |
| 275 case 0b10111011: |
| 276 case 0b10111100: |
| 277 case 0b10111101: |
| 278 case 0b10111110: |
| 279 case 0b10111111: |
| 280 case 0b11000000: |
| 281 case 0b11000001: |
| 282 case 0b11000010: |
| 283 case 0b11000011: |
| 284 case 0b11000100: |
| 285 case 0b11000101: |
| 286 case 0b11000110: |
| 287 case 0b11000111: |
| 288 case 0b11001000: |
| 289 case 0b11001001: |
| 290 case 0b11001010: |
| 291 case 0b11001011: |
| 292 case 0b11001100: |
| 293 case 0b11001101: |
| 294 case 0b11001110: |
| 295 case 0b11001111: |
| 296 case 0b11010000: |
| 297 case 0b11010001: |
| 298 case 0b11010010: |
| 299 case 0b11010011: |
| 300 case 0b11010100: |
| 301 case 0b11010101: |
| 302 case 0b11010110: |
| 303 case 0b11010111: |
| 304 case 0b11011000: |
| 305 case 0b11011001: |
| 306 case 0b11011010: |
| 307 case 0b11011011: |
| 308 case 0b11011100: |
| 309 case 0b11011101: |
| 310 case 0b11011110: |
| 311 case 0b11011111: |
| 312 case 0b11100000: |
| 313 case 0b11100001: |
| 314 case 0b11100010: |
| 315 case 0b11100011: |
| 316 case 0b11100100: |
| 317 case 0b11100101: |
| 318 case 0b11100110: |
| 319 case 0b11100111: |
| 320 case 0b11101000: |
| 321 case 0b11101001: |
| 322 case 0b11101010: |
| 323 case 0b11101011: |
| 324 case 0b11101100: |
| 325 case 0b11101101: |
| 326 case 0b11101110: |
| 327 case 0b11101111: |
| 328 case 0b11110000: |
| 329 case 0b11110001: |
| 330 case 0b11110010: |
| 331 case 0b11110011: |
| 332 case 0b11110100: |
| 333 case 0b11110101: |
| 334 case 0b11110110: |
| 335 case 0b11110111: |
| 336 case 0b11111000: |
| 337 case 0b11111001: |
| 338 case 0b11111010: |
| 339 case 0b11111011: |
| 340 case 0b11111100: |
| 341 case 0b11111101: |
| 342 case 0b11111110: |
| 343 entry_type_ = HpackEntryType::kIndexedHeader; |
| 344 // The low 7 bits of |byte| are the initial bits of the varint. |
| 345 // One of those bits is 0, so the varint is only one byte long. |
| 346 varint_decoder_.set_value(byte & 0x07f); |
| 347 return DecodeStatus::kDecodeDone; |
| 348 |
| 349 case 0b11111111: |
| 350 entry_type_ = HpackEntryType::kIndexedHeader; |
| 351 // The low 7 bits of |byte| are the initial bits of the varint. |
| 352 // All of those bits are 1, so the varint extends into another byte. |
| 353 return varint_decoder_.StartExtended(0x7f, db); |
| 354 } |
| 355 CHECK(false) << "Unreachable, byte=" << std::hex |
| 356 << static_cast<uint32_t>(byte); |
| 357 return DecodeStatus::kDecodeError; |
| 358 } |
| 359 |
| 360 } // namespace net |
OLD | NEW |