| 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/tools/hpack_block_builder.h" | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 | |
| 9 namespace net { | |
| 10 namespace test { | |
| 11 | |
| 12 void HpackBlockBuilder::AppendHighBitsAndVarint(uint8_t high_bits, | |
| 13 uint8_t prefix_length, | |
| 14 uint64_t varint) { | |
| 15 EXPECT_LE(4, prefix_length); | |
| 16 EXPECT_LE(prefix_length, 7); | |
| 17 | |
| 18 // prefix_mask defines the sequence of low-order bits of the first byte | |
| 19 // that encode the prefix of the value. It is also the marker in those bits | |
| 20 // of the first byte indicating that at least one extension byte is needed. | |
| 21 uint8_t prefix_mask = (1 << prefix_length) - 1; | |
| 22 EXPECT_EQ(0, high_bits & prefix_mask); | |
| 23 | |
| 24 if (varint < prefix_mask) { | |
| 25 uint8_t b = high_bits | static_cast<uint8_t>(varint); | |
| 26 buffer_.push_back(static_cast<char>(b)); | |
| 27 return; | |
| 28 } | |
| 29 | |
| 30 // We need extension bytes. | |
| 31 buffer_.push_back(static_cast<char>(high_bits | prefix_mask)); | |
| 32 varint -= prefix_mask; | |
| 33 while (varint >= 128) { | |
| 34 uint8_t b = static_cast<uint8_t>((varint % 128) + 128); | |
| 35 buffer_.push_back(static_cast<char>(b)); | |
| 36 varint = varint / 128; | |
| 37 } | |
| 38 char c = static_cast<char>(varint); | |
| 39 buffer_.push_back(c); | |
| 40 } | |
| 41 | |
| 42 void HpackBlockBuilder::AppendEntryTypeAndVarint(HpackEntryType entry_type, | |
| 43 uint64_t varint) { | |
| 44 uint8_t high_bits; | |
| 45 uint8_t prefix_length; // Bits of the varint prefix in the first byte. | |
| 46 switch (entry_type) { | |
| 47 case HpackEntryType::kIndexedHeader: | |
| 48 high_bits = 0x80; | |
| 49 prefix_length = 7; | |
| 50 break; | |
| 51 case HpackEntryType::kDynamicTableSizeUpdate: | |
| 52 high_bits = 0x20; | |
| 53 prefix_length = 5; | |
| 54 break; | |
| 55 case HpackEntryType::kIndexedLiteralHeader: | |
| 56 high_bits = 0x40; | |
| 57 prefix_length = 6; | |
| 58 break; | |
| 59 case HpackEntryType::kUnindexedLiteralHeader: | |
| 60 high_bits = 0x00; | |
| 61 prefix_length = 4; | |
| 62 break; | |
| 63 case HpackEntryType::kNeverIndexedLiteralHeader: | |
| 64 high_bits = 0x10; | |
| 65 prefix_length = 4; | |
| 66 break; | |
| 67 default: | |
| 68 NOTREACHED(); | |
| 69 high_bits = 0; | |
| 70 prefix_length = 0; | |
| 71 } | |
| 72 AppendHighBitsAndVarint(high_bits, prefix_length, varint); | |
| 73 } | |
| 74 | |
| 75 void HpackBlockBuilder::AppendString(bool is_huffman_encoded, | |
| 76 base::StringPiece str) { | |
| 77 uint8_t high_bits = is_huffman_encoded ? 0x80 : 0; | |
| 78 uint8_t prefix_length = 7; | |
| 79 AppendHighBitsAndVarint(high_bits, prefix_length, str.size()); | |
| 80 str.AppendToString(&buffer_); | |
| 81 } | |
| 82 | |
| 83 } // namespace test | |
| 84 } // namespace net | |
| OLD | NEW |