1use std::io;
2
3use quik_util::*;
4
5pub struct PacketNumber;
6
7impl PacketNumber {
8 pub fn parse(src: &mut impl Buffer, len: usize) -> Result<u32> {
10 let mut buf = [0u8; 4];
11 src.read_exact(&mut buf[..len])?;
12 Ok(NetworkEndian::read_u32(&buf))
13 }
14}
15
16#[derive(Debug, Clone, Hash, PartialEq, Eq)]
17pub struct VarInt {
18 inner: u64,
19}
20
21impl From<VarInt> for usize {
22 fn from(val: VarInt) -> Self {
23 val.inner as usize
24 }
25}
26
27impl VarInt {
28 pub const ZERO: VarInt = VarInt { inner: 0 };
29
30 pub fn parse(src: &mut impl Buffer) -> Result<Self> {
31 let mut buf = [0u8; 8];
32 src.read_exact(&mut buf[..1])?;
33 let typ = buf[0] >> 6;
34 buf[0] &= 0b0011_1111;
35
36 Ok(Self {
37 inner: match typ {
38 0b00 => buf[0] as u64,
39 0b01 => {
40 src.read_exact(&mut buf[1..2])?;
41 NetworkEndian::read_u16(&buf) as u64
42 }
43 0b10 => {
44 src.read_exact(&mut buf[1..4])?;
45 NetworkEndian::read_u32(&buf) as u64
46 }
47 0b11 => {
48 src.read_exact(&mut buf[1..8])?;
49 NetworkEndian::read_u64(&buf) as u64
50 }
51 _ => unreachable!(),
52 },
53 })
54 }
55}
56
57pub type StreamId = VarInt;
58
59#[derive(Debug, Clone, Hash, PartialEq, Eq)]
61pub struct ConnectionId {
62 pub length: usize,
63 pub buf: [u8; 20],
64}
65
66impl ConnectionId {
67 pub fn parse(src: &mut impl Buffer) -> Result<Self> {
68 let length = src.read_u8()? as usize;
69 let mut buf = [0; 20];
70 let (bufref, _) = buf.split_at_mut_checked(length).ok_or_else(|| {
71 io::Error::new(io::ErrorKind::UnexpectedEof, "length longer than 160 bits")
72 })?;
73 src.read_exact(bufref)?;
74 Ok(Self { length, buf })
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use std::io;
81
82 use super::*;
83
84 #[test]
85 fn varint_parse_no_bytes_fails() {
86 let buf = Vec::<u8>::new();
87 let mut bufref = &buf[..];
88 let res = VarInt::parse(&mut bufref);
89 let kind = res
90 .err()
91 .as_ref()
92 .and_then(|e| e.downcast_ref::<io::Error>())
93 .map(|e| e.kind());
94 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
95 }
96
97 #[test]
98 fn varint_parse_one_byte_succeeds() {
99 let buf = vec![0b0011_0101, 0x34];
100 let mut bufref = &buf[..];
101 let res = VarInt::parse(&mut bufref);
102 assert_eq!(res.ok(), Some(VarInt { inner: 0b0011_0101 }));
103 assert_eq!(bufref, &[0x34]);
104 }
105
106 #[test]
107 fn varint_parse_two_byte_succeeds() {
108 let buf = vec![0b0110_0101, 0x34, 0x12];
109 let mut bufref = &buf[..];
110 let res = VarInt::parse(&mut bufref);
111 assert_eq!(res.ok(), Some(VarInt { inner: 0x2534 }));
112 assert_eq!(bufref, &[0x12]);
113 }
114
115 #[test]
116 fn varint_parse_four_byte_succeeds() {
117 let buf = vec![0b1010_0101, 0x12, 0x34, 0x56, 0x78];
118 let mut bufref = &buf[..];
119 let res = VarInt::parse(&mut bufref);
120 assert_eq!(res.ok(), Some(VarInt { inner: 0x25123456 }));
121 assert_eq!(bufref, &[0x78]);
122 }
123
124 #[test]
125 fn varint_parse_eight_byte_succeeds() {
126 let buf = vec![0b1110_0101, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0xaa];
127 let mut bufref = &buf[..];
128 let res = VarInt::parse(&mut bufref);
129 assert_eq!(
130 res.ok(),
131 Some(VarInt {
132 inner: 0x2512345678901122
133 })
134 );
135 assert_eq!(bufref, &[0xaa]);
136 }
137
138 #[test]
139 fn varint_parse_one_byte_size_fails() {
140 let buf = vec![0b0100_0000];
141 let mut bufref = &buf[..];
142 let res = VarInt::parse(&mut bufref);
143 let kind = res
144 .err()
145 .as_ref()
146 .and_then(|e| e.downcast_ref::<io::Error>())
147 .map(|e| e.kind());
148 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
149
150 let buf = vec![0b1000_0000];
151 let mut bufref = &buf[..];
152 let res = VarInt::parse(&mut bufref);
153 let kind = res
154 .err()
155 .as_ref()
156 .and_then(|e| e.downcast_ref::<io::Error>())
157 .map(|e| e.kind());
158 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
159
160 let buf = vec![0b1100_0000];
161 let mut bufref = &buf[..];
162 let res = VarInt::parse(&mut bufref);
163 let kind = res
164 .err()
165 .as_ref()
166 .and_then(|e| e.downcast_ref::<io::Error>())
167 .map(|e| e.kind());
168 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
169 }
170
171 #[test]
172 fn varint_parse_two_byte_fails() {
173 let buf = vec![0b0110_0101];
174 let mut bufref = &buf[..];
175 let res = VarInt::parse(&mut bufref);
176 let kind = res
177 .err()
178 .as_ref()
179 .and_then(|e| e.downcast_ref::<io::Error>())
180 .map(|e| e.kind());
181 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
182 }
183
184 #[test]
185 fn varint_parse_four_byte_fails() {
186 let buf = vec![0b1010_0101, 0x12, 0x34];
187 let mut bufref = &buf[..];
188 let res = VarInt::parse(&mut bufref);
189 let kind = res
190 .err()
191 .as_ref()
192 .and_then(|e| e.downcast_ref::<io::Error>())
193 .map(|e| e.kind());
194 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
195 }
196
197 #[test]
198 fn varint_parse_eight_byte_fails() {
199 let buf = vec![0b1110_0101, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11];
200 let mut bufref = &buf[..];
201 let res = VarInt::parse(&mut bufref);
202 let kind = res
203 .err()
204 .as_ref()
205 .and_then(|e| e.downcast_ref::<io::Error>())
206 .map(|e| e.kind());
207 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
208 }
209
210 #[test]
211 fn connid_parse_no_bytes_fails() {
212 let buf = Vec::<u8>::new();
213 let mut bufref = &buf[..];
214 let res = ConnectionId::parse(&mut bufref);
215 let kind = res
216 .err()
217 .as_ref()
218 .and_then(|e| e.downcast_ref::<io::Error>())
219 .map(|e| e.kind());
220 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
221 }
222
223 #[test]
224 fn connid_parse_size_only_fails() {
225 let buf = vec![19u8];
226 let mut bufref = &buf[..];
227 let res = ConnectionId::parse(&mut bufref);
228 let kind = res
229 .err()
230 .as_ref()
231 .and_then(|e| e.downcast_ref::<io::Error>())
232 .map(|e| e.kind());
233 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
234 }
235
236 #[test]
237 fn connid_parse_size_big_fails() {
238 let buf = vec![20u8];
239 let mut bufref = &buf[..];
240 let res = ConnectionId::parse(&mut bufref);
241 let kind = res
242 .err()
243 .as_ref()
244 .and_then(|e| e.downcast_ref::<io::Error>())
245 .map(|e| e.kind());
246 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
247 }
248
249 #[test]
250 fn connid_parse_size_very_big_fails() {
251 let buf = vec![0xffu8];
252 let mut bufref = &buf[..];
253 let res = ConnectionId::parse(&mut bufref);
254 let kind = res
255 .err()
256 .as_ref()
257 .and_then(|e| e.downcast_ref::<io::Error>())
258 .map(|e| e.kind());
259 assert_eq!(kind, Some(io::ErrorKind::UnexpectedEof));
260 }
261
262 #[test]
263 fn connid_parse_one_byte_passes() {
264 let buf = vec![1u8, 0x12, 0x34];
265 let mut bufref = &buf[..];
266 let res = ConnectionId::parse(&mut bufref);
267 assert_eq!(
268 res.ok(),
269 Some(ConnectionId {
270 length: 1 as usize,
271 buf: [0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
272 })
273 );
274 assert_eq!(bufref, &[0x34])
275 }
276
277 #[test]
278 fn connid_parse_10_bytes_passes() {
279 let buf = vec![
280 10u8, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12, 0x34,
281 ];
282 let mut bufref = &buf[..];
283 let res = ConnectionId::parse(&mut bufref);
284 assert_eq!(
285 res.ok(),
286 Some(ConnectionId {
287 length: 10 as usize,
288 buf: [
289 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0, 0, 0, 0, 0, 0,
290 0, 0, 0, 0
291 ]
292 })
293 );
294 assert_eq!(bufref, &[0x12, 0x34])
295 }
296
297 #[test]
298 fn connid_parse_max_byte_passes() {
299 let buf = vec![
300 20u8, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x22, 0x11, 0x33,
301 0x55, 0x44, 0x77, 0x66, 0x99, 0x11, 0x20, 0x12, 0x34,
302 ];
303 let mut bufref = &buf[..];
304 let res = ConnectionId::parse(&mut bufref);
305 assert_eq!(
306 res.ok(),
307 Some(ConnectionId {
308 length: 20 as usize,
309 buf: [
310 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x22, 0x11, 0x33,
311 0x55, 0x44, 0x77, 0x66, 0x99, 0x11, 0x20
312 ]
313 })
314 );
315 assert_eq!(bufref, &[0x12, 0x34])
316 }
317}