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 "quiche/http2/test_tools/hpack_block_builder.h"
6
7 #include "quiche/http2/hpack/varint/hpack_varint_encoder.h"
8 #include "quiche/common/platform/api/quiche_bug_tracker.h"
9 #include "quiche/common/platform/api/quiche_test.h"
10
11 namespace http2 {
12 namespace test {
13
AppendHighBitsAndVarint(uint8_t high_bits,uint8_t prefix_length,uint64_t varint)14 void HpackBlockBuilder::AppendHighBitsAndVarint(uint8_t high_bits,
15 uint8_t prefix_length,
16 uint64_t varint) {
17 EXPECT_LE(3, prefix_length);
18 EXPECT_LE(prefix_length, 8);
19
20 HpackVarintEncoder::Encode(high_bits, prefix_length, varint, &buffer_);
21 }
22
AppendEntryTypeAndVarint(HpackEntryType entry_type,uint64_t varint)23 void HpackBlockBuilder::AppendEntryTypeAndVarint(HpackEntryType entry_type,
24 uint64_t varint) {
25 uint8_t high_bits;
26 uint8_t prefix_length; // Bits of the varint prefix in the first byte.
27 switch (entry_type) {
28 case HpackEntryType::kIndexedHeader:
29 high_bits = 0x80;
30 prefix_length = 7;
31 break;
32 case HpackEntryType::kDynamicTableSizeUpdate:
33 high_bits = 0x20;
34 prefix_length = 5;
35 break;
36 case HpackEntryType::kIndexedLiteralHeader:
37 high_bits = 0x40;
38 prefix_length = 6;
39 break;
40 case HpackEntryType::kUnindexedLiteralHeader:
41 high_bits = 0x00;
42 prefix_length = 4;
43 break;
44 case HpackEntryType::kNeverIndexedLiteralHeader:
45 high_bits = 0x10;
46 prefix_length = 4;
47 break;
48 default:
49 QUICHE_BUG(http2_bug_110_1) << "Unreached, entry_type=" << entry_type;
50 high_bits = 0;
51 prefix_length = 0;
52 break;
53 }
54 AppendHighBitsAndVarint(high_bits, prefix_length, varint);
55 }
56
AppendString(bool is_huffman_encoded,absl::string_view str)57 void HpackBlockBuilder::AppendString(bool is_huffman_encoded,
58 absl::string_view str) {
59 uint8_t high_bits = is_huffman_encoded ? 0x80 : 0;
60 uint8_t prefix_length = 7;
61 AppendHighBitsAndVarint(high_bits, prefix_length, str.size());
62 buffer_.append(str.data(), str.size());
63 }
64
65 } // namespace test
66 } // namespace http2
67