Debian Codegraph @

third_party/rust/neqo-qpack/src/prefix.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 #[derive(Copy, Clone, Debug)]
8 pub struct Prefix {
9 #[expect(clippy::struct_field_names, reason = "Still the best name.")]
10 prefix: u8,
11 len: u8,
12 mask: u8,
13 }
14
15 impl Prefix {
16 pub fn new(prefix: u8, len: u8) -> Self {
17 // len should never be larger than 7.
18 // Most of Prefixes are instantiated as consts below. The only place where this
19 // constructor is used is in tests and when literals are encoded and the Huffman
20 // bit is added to one of the consts below. create_prefix guaranty that all const
21 // have len < 7 so we can safely assert that len is <=7.
22 assert!(len <= 7);
23 assert!((len == 0) || (prefix & ((1 << (8 - len)) - 1) == 0));
24 Self {
25 prefix,
26 len,
27 mask: if len == 0 {
28 0xFF
29 } else {
30 ((1 << len) - 1) << (8 - len)
31 },
32 }
33 }
34
35 pub const fn len(self) -> u8 {
36 self.len
37 }
38
39 pub const fn prefix(self) -> u8 {
40 self.prefix
41 }
42
43 pub const fn cmp_prefix(self, b: u8) -> bool {
44 (b & self.mask) == self.prefix
45 }
46 }
47
48 #[macro_export]
49 macro_rules! create_prefix {
50 ($n:ident) => {
51 pub const $n: Prefix = Prefix {
52 prefix: 0x0,
53 len: 0,
54 mask: 0xFF,
55 };
56 };
57 ($n:ident, $v:expr, $l:expr) => {
58 static_assertions::const_assert!($l < 7);
59 static_assertions::const_assert!($v & ((1 << (8 - $l)) - 1) == 0);
60 pub const $n: Prefix = Prefix {
61 prefix: $v,
62 len: $l,
63 mask: ((1 << $l) - 1) << (8 - $l),
64 };
65 };
66 ($n:ident, $v:expr, $l:expr, $m:expr) => {
67 static_assertions::const_assert!($l < 7);
68 static_assertions::const_assert!($v & ((1 << (8 - $l)) - 1) == 0);
69 static_assertions::const_assert!((((1 << $l) - 1) << (8 - $l)) >= $m);
70 pub const $n: Prefix = Prefix {
71 prefix: $v,
72 len: $l,
73 mask: $m,
74 };
75 };
76 }
77
78 create_prefix!(NO_PREFIX);
79 //=====================================================================
80 // Decoder instructions prefix
81 //=====================================================================
82
83 // | 1 | Stream ID (7+) |
84 create_prefix!(DECODER_HEADER_ACK, 0x80, 1);
85
86 // | 0 | 1 | Stream ID (6+) |
87 create_prefix!(DECODER_STREAM_CANCELLATION, 0x40, 2);
88
89 // | 0 | 0 | Increment (6+) |
90 create_prefix!(DECODER_INSERT_COUNT_INCREMENT, 0x00, 2);
91
92 //=====================================================================
93 // Encoder instructions prefix
94 //=====================================================================
95
96 // | 0 | 0 | 1 | Capacity (5+) |
97 create_prefix!(ENCODER_CAPACITY, 0x20, 3);
98
99 // | 1 | T | Name Index (6+) |
100 // T == 1 static
101 // T == 0 dynamic
102 create_prefix!(ENCODER_INSERT_WITH_NAME_REF_STATIC, 0xC0, 2);
103 create_prefix!(ENCODER_INSERT_WITH_NAME_REF_DYNAMIC, 0x80, 2);
104
105 // | 0 | 1 | H | Name Length (5+) |
106 // H is not relevant for decoding this prefix, therefore the mask is 1100 0000 = 0xC0
107 create_prefix!(ENCODER_INSERT_WITH_NAME_LITERAL, 0x40, 2);
108
109 // | 0 | 0 | 0 | Index (5+) |
110 create_prefix!(ENCODER_DUPLICATE, 0x00, 3);
111
112 //=====================================================================
113 // Header block encoding prefixes
114 //=====================================================================
115
116 create_prefix!(BASE_PREFIX_POSITIVE, 0x00, 1);
117 create_prefix!(BASE_PREFIX_NEGATIVE, 0x80, 1);
118
119 // | 1 | T | index(6+) |
120 // T == 1 static
121 // T == 0 dynamic
122 create_prefix!(HEADER_FIELD_INDEX_STATIC, 0xC0, 2);
123 create_prefix!(HEADER_FIELD_INDEX_DYNAMIC, 0x80, 2);
124
125 // | 0 | 0 | 0 | 1 | Index(4+) |
126 create_prefix!(HEADER_FIELD_INDEX_DYNAMIC_POST, 0x10, 4);
127
128 // | 0 | 1 | N | T | Index(4+) |
129 // T == 1 static
130 // T == 0 dynamic
131 // N is ignored, therefore the mask is 1101 0000 = 0xD0
132 create_prefix!(HEADER_FIELD_LITERAL_NAME_REF_STATIC, 0x50, 4, 0xD0);
133 create_prefix!(HEADER_FIELD_LITERAL_NAME_REF_DYNAMIC, 0x40, 4, 0xD0);
134
135 // | 0 | 0 | 0 | 0 | N | Index(3+) |
136 // N is ignored, therefore the mask is 1111 0000 = 0xF0
137 create_prefix!(HEADER_FIELD_LITERAL_NAME_REF_DYNAMIC_POST, 0x00, 5, 0xF0);
138
139 // | 0 | 0 | 1 | N | H | Index(3+) |
140 // N is ignored and H is not relevant for decoding this prefix, therefore the mask is 1110 0000 =
141 // 0xE0
142 create_prefix!(HEADER_FIELD_LITERAL_NAME_LITERAL, 0x20, 4, 0xE0);