Debian Codegraph @

third_party/rust/neqo-qpack/src/decoder_instructions.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 use std::{
8 fmt::{self, Display, Formatter},
9 mem,
10 };
11
12 use neqo_common::{qdebug, qtrace};
13 use neqo_transport::StreamId;
14
15 use crate::{
16 prefix::{DECODER_HEADER_ACK, DECODER_INSERT_COUNT_INCREMENT, DECODER_STREAM_CANCELLATION},
17 qpack_send_buf::QpackData,
18 reader::{IntReader, ReadByte},
19 Res,
20 };
21
22 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
23 pub enum DecoderInstruction {
24 InsertCountIncrement { increment: u64 },
25 HeaderAck { stream_id: StreamId },
26 StreamCancellation { stream_id: StreamId },
27 NoInstruction,
28 }
29
30 impl DecoderInstruction {
31 fn get_instruction(b: u8) -> Self {
32 if DECODER_HEADER_ACK.cmp_prefix(b) {
33 Self::HeaderAck {
34 stream_id: StreamId::from(0),
35 }
36 } else if DECODER_STREAM_CANCELLATION.cmp_prefix(b) {
37 Self::StreamCancellation {
38 stream_id: StreamId::from(0),
39 }
40 } else if DECODER_INSERT_COUNT_INCREMENT.cmp_prefix(b) {
41 Self::InsertCountIncrement { increment: 0 }
42 } else {
43 unreachable!();
44 }
45 }
46
47 pub(crate) fn marshal(&self, enc: &mut QpackData) {
48 match self {
49 Self::InsertCountIncrement { increment } => {
50 enc.encode_prefixed_encoded_int(DECODER_INSERT_COUNT_INCREMENT, *increment);
51 }
52 Self::HeaderAck { stream_id } => {
53 enc.encode_prefixed_encoded_int(DECODER_HEADER_ACK, stream_id.as_u64());
54 }
55 Self::StreamCancellation { stream_id } => {
56 enc.encode_prefixed_encoded_int(DECODER_STREAM_CANCELLATION, stream_id.as_u64());
57 }
58 Self::NoInstruction => {}
59 }
60 }
61 }
62
63 #[derive(Debug)]
64 enum DecoderInstructionReaderState {
65 ReadInstruction,
66 ReadInt { reader: IntReader },
67 }
68
69 #[derive(Debug)]
70 pub struct DecoderInstructionReader {
71 state: DecoderInstructionReaderState,
72 instruction: DecoderInstruction,
73 }
74
75 impl Display for DecoderInstructionReader {
76 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
77 write!(f, "InstructionReader")
78 }
79 }
80
81 impl DecoderInstructionReader {
82 pub const fn new() -> Self {
83 Self {
84 state: DecoderInstructionReaderState::ReadInstruction,
85 instruction: DecoderInstruction::NoInstruction,
86 }
87 }
88
89 /// # Errors
90 ///
91 /// 1) `NeedMoreData` if the reader needs more data
92 /// 2) `ClosedCriticalStream`
93 /// 3) other errors will be translated to `DecoderStream` by the caller of this function.
94 pub fn read_instructions<R: ReadByte>(&mut self, recv: &mut R) -> Res<DecoderInstruction> {
95 qdebug!("[{self}] read a new instruction");
96 loop {
97 match &mut self.state {
98 DecoderInstructionReaderState::ReadInstruction => {
99 let b = recv.read_byte()?;
100 self.instruction = DecoderInstruction::get_instruction(b);
101 self.state = DecoderInstructionReaderState::ReadInt {
102 reader: IntReader::make(
103 b,
104 &[
105 DECODER_HEADER_ACK,
106 DECODER_STREAM_CANCELLATION,
107 DECODER_INSERT_COUNT_INCREMENT,
108 ],
109 ),
110 };
111 }
112 DecoderInstructionReaderState::ReadInt { reader } => {
113 let val = reader.read(recv)?;
114 qtrace!("[{self}] varint read {val}");
115 match &mut self.instruction {
116 DecoderInstruction::InsertCountIncrement { increment: v } => {
117 *v = val;
118 self.state = DecoderInstructionReaderState::ReadInstruction;
119 break Ok(mem::replace(
120 &mut self.instruction,
121 DecoderInstruction::NoInstruction,
122 ));
123 }
124 DecoderInstruction::HeaderAck { stream_id: v }
125 | DecoderInstruction::StreamCancellation { stream_id: v } => {
126 *v = StreamId::from(val);
127 self.state = DecoderInstructionReaderState::ReadInstruction;
128 break Ok(mem::replace(
129 &mut self.instruction,
130 DecoderInstruction::NoInstruction,
131 ));
132 }
133 DecoderInstruction::NoInstruction => {
134 unreachable!("This instruction cannot be in this state");
135 }
136 }
137 }
138 }
139 }
140 }
141 }
142
143 #[cfg(test)]
144 mod test {
145
146 use neqo_transport::StreamId;
147
148 use super::{DecoderInstruction, DecoderInstructionReader, QpackData};
149 use crate::{reader::test_receiver::TestReceiver, Error};
150
151 fn test_encoding_decoding(instruction: DecoderInstruction) {
152 let mut buf = QpackData::default();
153 instruction.marshal(&mut buf);
154 let mut test_receiver: TestReceiver = TestReceiver::default();
155 test_receiver.write(&buf);
156 let mut decoder = DecoderInstructionReader::new();
157 assert_eq!(
158 decoder.read_instructions(&mut test_receiver).unwrap(),
159 instruction
160 );
161 }
162
163 #[test]
164 fn encoding_decoding_instructions() {
165 test_encoding_decoding(DecoderInstruction::InsertCountIncrement { increment: 1 });
166 test_encoding_decoding(DecoderInstruction::InsertCountIncrement { increment: 10_000 });
167
168 test_encoding_decoding(DecoderInstruction::HeaderAck {
169 stream_id: StreamId::new(1),
170 });
171 test_encoding_decoding(DecoderInstruction::HeaderAck {
172 stream_id: StreamId::new(10_000),
173 });
174
175 test_encoding_decoding(DecoderInstruction::StreamCancellation {
176 stream_id: StreamId::new(1),
177 });
178 test_encoding_decoding(DecoderInstruction::StreamCancellation {
179 stream_id: StreamId::new(10_000),
180 });
181 }
182
183 fn test_encoding_decoding_slow_reader(instruction: DecoderInstruction) {
184 let mut buf = QpackData::default();
185 instruction.marshal(&mut buf);
186 let mut test_receiver: TestReceiver = TestReceiver::default();
187 let mut decoder = DecoderInstructionReader::new();
188 for i in 0..buf.len() - 1 {
189 test_receiver.write(&buf[i..=i]);
190 assert_eq!(
191 decoder.read_instructions(&mut test_receiver),
192 Err(Error::NeedMoreData)
193 );
194 }
195 test_receiver.write(&buf[buf.len() - 1..buf.len()]);
196 assert_eq!(
197 decoder.read_instructions(&mut test_receiver).unwrap(),
198 instruction
199 );
200 }
201
202 #[test]
203 fn encoding_decoding_instructions_slow_reader() {
204 test_encoding_decoding_slow_reader(DecoderInstruction::InsertCountIncrement {
205 increment: 10_000,
206 });
207 test_encoding_decoding_slow_reader(DecoderInstruction::HeaderAck {
208 stream_id: StreamId::new(10_000),
209 });
210 test_encoding_decoding_slow_reader(DecoderInstruction::StreamCancellation {
211 stream_id: StreamId::new(10_000),
212 });
213 }
214
215 #[test]
216 fn decoding_error() {
217 let mut test_receiver: TestReceiver = TestReceiver::default();
218 // InsertCountIncrement with overflow
219 test_receiver.write(&[
220 0x3f, 0xc1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff, 0x02,
221 ]);
222 let mut decoder = DecoderInstructionReader::new();
223 assert_eq!(
224 decoder.read_instructions(&mut test_receiver),
225 Err(Error::IntegerOverflow)
226 );
227
228 let mut test_receiver: TestReceiver = TestReceiver::default();
229 // StreamCancellation with overflow
230 test_receiver.write(&[
231 0x7f, 0xc1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff, 0x02,
232 ]);
233 let mut decoder = DecoderInstructionReader::new();
234 assert_eq!(
235 decoder.read_instructions(&mut test_receiver),
236 Err(Error::IntegerOverflow)
237 );
238
239 let mut test_receiver: TestReceiver = TestReceiver::default();
240 // HeaderAck with overflow
241 test_receiver.write(&[
242 0x7f, 0xc1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff, 0x02,
243 ]);
244 let mut decoder = DecoderInstructionReader::new();
245 assert_eq!(
246 decoder.read_instructions(&mut test_receiver),
247 Err(Error::IntegerOverflow)
248 );
249 }
250 }