net-wire 0.1.0

Allocation-free no_std wire views and codecs for network protocols.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! QPACK field line representations defined by RFC 9204.

mod builder;

use core::fmt;

use crate::qpack::integer::{QpackInteger, QpackIntegerParseError};
use crate::qpack::string::{QpackStringLiteral, QpackStringLiteralParseError};
pub use builder::{
    QpackFieldLineBuildError, QpackIndexedFieldLineBuilder, QpackIndexedPostBaseFieldLineBuilder,
    QpackLiteralNameFieldLineBuilder, QpackLiteralNameReferenceFieldLineBuilder,
    QpackLiteralPostBaseNameReferenceFieldLineBuilder,
};
pub(crate) use builder::{QpackFieldLinePlan, QpackFieldLinePlanInput, plan_field_line};

/// A parsed QPACK field line.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum QpackFieldLine<'a> {
    /// An indexed field line.
    Indexed(QpackIndexedFieldLine<'a>),
    /// An indexed post-base field line.
    IndexedPostBase(QpackIndexedPostBaseFieldLine<'a>),
    /// A literal field line with a referenced name.
    LiteralNameReference(QpackLiteralNameReferenceFieldLine<'a>),
    /// A literal field line with a post-base referenced name.
    LiteralPostBaseNameReference(QpackLiteralPostBaseNameReferenceFieldLine<'a>),
    /// A literal field line containing a literal name.
    LiteralName(QpackLiteralNameFieldLine<'a>),
}

impl<'a> QpackFieldLine<'a> {
    /// Parses exactly one QPACK field line from the start of `bytes`.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, QpackFieldLineParseError> {
        let first = *bytes
            .first()
            .ok_or(QpackFieldLineParseError::DispatchIncomplete)?;

        if first & 0x80 != 0 {
            let index =
                QpackInteger::parse(bytes, 6).map_err(QpackFieldLineParseError::IndexedIndex)?;
            return Ok(Self::Indexed(QpackIndexedFieldLine {
                bytes: index.as_bytes(),
                index,
            }));
        }

        if first & 0xc0 == 0x40 {
            let name_index = QpackInteger::parse(bytes, 4)
                .map_err(QpackFieldLineParseError::LiteralNameReferenceNameIndex)?;
            let name_length = name_index.as_bytes().len();
            let value_bytes = bytes
                .get(name_length..)
                .ok_or(QpackFieldLineParseError::RequiredLengthOverflow)?;
            let value = QpackStringLiteral::parse(value_bytes, 8)
                .map_err(QpackFieldLineParseError::LiteralNameReferenceValue)?;
            let length = name_length
                .checked_add(value.as_bytes().len())
                .ok_or(QpackFieldLineParseError::RequiredLengthOverflow)?;
            let line_bytes = bytes
                .get(..length)
                .ok_or(QpackFieldLineParseError::RequiredLengthOverflow)?;
            return Ok(Self::LiteralNameReference(
                QpackLiteralNameReferenceFieldLine {
                    bytes: line_bytes,
                    name_index,
                    value,
                },
            ));
        }

        if first & 0xe0 == 0x20 {
            let name = QpackStringLiteral::parse(bytes, 4)
                .map_err(QpackFieldLineParseError::LiteralNameName)?;
            let name_length = name.as_bytes().len();
            let value_bytes = bytes
                .get(name_length..)
                .ok_or(QpackFieldLineParseError::RequiredLengthOverflow)?;
            let value = QpackStringLiteral::parse(value_bytes, 8)
                .map_err(QpackFieldLineParseError::LiteralNameValue)?;
            let length = name_length
                .checked_add(value.as_bytes().len())
                .ok_or(QpackFieldLineParseError::RequiredLengthOverflow)?;
            let line_bytes = bytes
                .get(..length)
                .ok_or(QpackFieldLineParseError::RequiredLengthOverflow)?;
            return Ok(Self::LiteralName(QpackLiteralNameFieldLine {
                bytes: line_bytes,
                name,
                value,
            }));
        }

        if first & 0xf0 == 0x10 {
            let index = QpackInteger::parse(bytes, 4)
                .map_err(QpackFieldLineParseError::IndexedPostBaseIndex)?;
            return Ok(Self::IndexedPostBase(QpackIndexedPostBaseFieldLine {
                bytes: index.as_bytes(),
                index,
            }));
        }

        let name_index = QpackInteger::parse(bytes, 3)
            .map_err(QpackFieldLineParseError::LiteralPostBaseNameReferenceNameIndex)?;
        let name_length = name_index.as_bytes().len();
        let value_bytes = bytes
            .get(name_length..)
            .ok_or(QpackFieldLineParseError::RequiredLengthOverflow)?;
        let value = QpackStringLiteral::parse(value_bytes, 8)
            .map_err(QpackFieldLineParseError::LiteralPostBaseNameReferenceValue)?;
        let length = name_length
            .checked_add(value.as_bytes().len())
            .ok_or(QpackFieldLineParseError::RequiredLengthOverflow)?;
        let line_bytes = bytes
            .get(..length)
            .ok_or(QpackFieldLineParseError::RequiredLengthOverflow)?;
        Ok(Self::LiteralPostBaseNameReference(
            QpackLiteralPostBaseNameReferenceFieldLine {
                bytes: line_bytes,
                name_index,
                value,
            },
        ))
    }

    /// Returns the exact encoded bytes occupied by this field line.
    pub const fn as_bytes(self) -> &'a [u8] {
        match self {
            Self::Indexed(line) => line.as_bytes(),
            Self::IndexedPostBase(line) => line.as_bytes(),
            Self::LiteralNameReference(line) => line.as_bytes(),
            Self::LiteralPostBaseNameReference(line) => line.as_bytes(),
            Self::LiteralName(line) => line.as_bytes(),
        }
    }
}

/// An indexed QPACK field line.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QpackIndexedFieldLine<'a> {
    bytes: &'a [u8],
    index: QpackInteger<'a>,
}

impl<'a> QpackIndexedFieldLine<'a> {
    /// Returns the exact encoded bytes occupied by this field line.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }
    /// Returns whether the index refers to the static table.
    pub const fn is_static(self) -> bool {
        self.bytes[0] & 0x40 != 0
    }
    /// Returns the encoded field-line index.
    pub const fn index(self) -> QpackInteger<'a> {
        self.index
    }
}

/// An indexed post-base QPACK field line.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QpackIndexedPostBaseFieldLine<'a> {
    bytes: &'a [u8],
    index: QpackInteger<'a>,
}

impl<'a> QpackIndexedPostBaseFieldLine<'a> {
    /// Returns the exact encoded bytes occupied by this field line.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }
    /// Returns the encoded post-base index.
    pub const fn index(self) -> QpackInteger<'a> {
        self.index
    }
}

/// A literal QPACK field line with a referenced name.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QpackLiteralNameReferenceFieldLine<'a> {
    bytes: &'a [u8],
    name_index: QpackInteger<'a>,
    value: QpackStringLiteral<'a>,
}

impl<'a> QpackLiteralNameReferenceFieldLine<'a> {
    /// Returns the exact encoded bytes occupied by this field line.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }
    /// Returns whether intermediaries must not index this field line.
    pub const fn is_never_indexed(self) -> bool {
        self.bytes[0] & 0x20 != 0
    }
    /// Returns whether the name index refers to the static table.
    pub const fn is_static(self) -> bool {
        self.bytes[0] & 0x10 != 0
    }
    /// Returns the encoded referenced-name index.
    pub const fn name_index(self) -> QpackInteger<'a> {
        self.name_index
    }
    /// Returns the encoded field value.
    pub const fn value(self) -> QpackStringLiteral<'a> {
        self.value
    }
}

/// A literal QPACK field line with a post-base referenced name.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QpackLiteralPostBaseNameReferenceFieldLine<'a> {
    bytes: &'a [u8],
    name_index: QpackInteger<'a>,
    value: QpackStringLiteral<'a>,
}

impl<'a> QpackLiteralPostBaseNameReferenceFieldLine<'a> {
    /// Returns the exact encoded bytes occupied by this field line.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }
    /// Returns whether intermediaries must not index this field line.
    pub const fn is_never_indexed(self) -> bool {
        self.bytes[0] & 0x08 != 0
    }
    /// Returns the encoded post-base name index.
    pub const fn name_index(self) -> QpackInteger<'a> {
        self.name_index
    }
    /// Returns the encoded field value.
    pub const fn value(self) -> QpackStringLiteral<'a> {
        self.value
    }
}

/// A literal QPACK field line containing a literal name.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QpackLiteralNameFieldLine<'a> {
    bytes: &'a [u8],
    name: QpackStringLiteral<'a>,
    value: QpackStringLiteral<'a>,
}

impl<'a> QpackLiteralNameFieldLine<'a> {
    /// Returns the exact encoded bytes occupied by this field line.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }
    /// Returns whether intermediaries must not index this field line.
    pub const fn is_never_indexed(self) -> bool {
        self.bytes[0] & 0x10 != 0
    }
    /// Returns the encoded literal name.
    pub const fn name(self) -> QpackStringLiteral<'a> {
        self.name
    }
    /// Returns the encoded field value.
    pub const fn value(self) -> QpackStringLiteral<'a> {
        self.value
    }
}

/// An error returned while parsing a QPACK field line.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum QpackFieldLineParseError {
    /// The input did not contain a field-line dispatch byte.
    DispatchIncomplete,
    /// The indexed field-line index could not be parsed.
    IndexedIndex(#[doc = "The integer parsing error."] QpackIntegerParseError),
    /// The indexed post-base field-line index could not be parsed.
    IndexedPostBaseIndex(#[doc = "The integer parsing error."] QpackIntegerParseError),
    /// The literal name-reference index could not be parsed.
    LiteralNameReferenceNameIndex(#[doc = "The integer parsing error."] QpackIntegerParseError),
    /// The literal name-reference value could not be parsed.
    LiteralNameReferenceValue(
        #[doc = "The string-literal parsing error."] QpackStringLiteralParseError,
    ),
    /// The literal post-base name-reference index could not be parsed.
    LiteralPostBaseNameReferenceNameIndex(
        #[doc = "The integer parsing error."] QpackIntegerParseError,
    ),
    /// The literal post-base name-reference value could not be parsed.
    LiteralPostBaseNameReferenceValue(
        #[doc = "The string-literal parsing error."] QpackStringLiteralParseError,
    ),
    /// The literal field name could not be parsed.
    LiteralNameName(#[doc = "The string-literal parsing error."] QpackStringLiteralParseError),
    /// The literal field value could not be parsed.
    LiteralNameValue(#[doc = "The string-literal parsing error."] QpackStringLiteralParseError),
    /// The combined component lengths overflowed `usize`.
    RequiredLengthOverflow,
}

impl fmt::Display for QpackFieldLineParseError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DispatchIncomplete => formatter.write_str("QPACK field line is incomplete"),
            Self::IndexedIndex(error) => {
                write!(formatter, "invalid indexed field line index: {error}")
            }
            Self::IndexedPostBaseIndex(error) => write!(
                formatter,
                "invalid indexed post-base field line index: {error}"
            ),
            Self::LiteralNameReferenceNameIndex(error) => {
                write!(formatter, "invalid literal name-reference index: {error}")
            }
            Self::LiteralNameReferenceValue(error) => {
                write!(formatter, "invalid literal name-reference value: {error}")
            }
            Self::LiteralPostBaseNameReferenceNameIndex(error) => write!(
                formatter,
                "invalid literal post-base name-reference index: {error}"
            ),
            Self::LiteralPostBaseNameReferenceValue(error) => write!(
                formatter,
                "invalid literal post-base name-reference value: {error}"
            ),
            Self::LiteralNameName(error) => {
                write!(formatter, "invalid literal field name: {error}")
            }
            Self::LiteralNameValue(error) => {
                write!(formatter, "invalid literal field value: {error}")
            }
            Self::RequiredLengthOverflow => formatter.write_str("QPACK field line length overflow"),
        }
    }
}

/// A validated sequence of QPACK field lines backed by its encoded bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QpackFieldLines<'a> {
    bytes: &'a [u8],
}

impl<'a> QpackFieldLines<'a> {
    /// Validates and borrows every field line in `bytes`.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, QpackFieldLinesParseError> {
        let mut remaining = bytes;
        while !remaining.is_empty() {
            let line = match QpackFieldLine::parse(remaining) {
                Ok(line) => line,
                Err(error) => {
                    return Err(QpackFieldLinesParseError {
                        offset: bytes.len() - remaining.len(),
                        error,
                    });
                }
            };
            remaining = &remaining[line.as_bytes().len()..];
        }
        Ok(Self { bytes })
    }

    /// Returns the complete encoded field-line sequence.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }

    /// Iterates over the field lines in this sequence.
    pub const fn iter(self) -> QpackFieldLineIter<'a> {
        QpackFieldLineIter::new(self.bytes)
    }
}

/// An error encountered while validating a QPACK field-line sequence.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QpackFieldLinesParseError {
    offset: usize,
    error: QpackFieldLineParseError,
}

impl QpackFieldLinesParseError {
    /// Returns the byte offset at which the invalid field line begins.
    pub const fn offset(self) -> usize {
        self.offset
    }

    /// Returns the error reported for the field line at [`Self::offset`].
    pub const fn error(self) -> QpackFieldLineParseError {
        self.error
    }
}

impl fmt::Display for QpackFieldLinesParseError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "QPACK field line at offset {}: {}",
            self.offset, self.error
        )
    }
}

/// An iterator over encoded QPACK field lines.
#[derive(Clone, Debug)]
pub struct QpackFieldLineIter<'a> {
    remaining: &'a [u8],
    failed: bool,
}

impl<'a> QpackFieldLineIter<'a> {
    /// Creates a fallible iterator over raw encoded field-line bytes.
    pub const fn new(bytes: &'a [u8]) -> Self {
        Self {
            remaining: bytes,
            failed: false,
        }
    }
}

impl<'a> Iterator for QpackFieldLineIter<'a> {
    type Item = Result<QpackFieldLine<'a>, QpackFieldLineParseError>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.failed || self.remaining.is_empty() {
            return None;
        }
        match QpackFieldLine::parse(self.remaining) {
            Ok(line) => {
                self.remaining = &self.remaining[line.as_bytes().len()..];
                Some(Ok(line))
            }
            Err(error) => {
                self.remaining = &[];
                self.failed = true;
                Some(Err(error))
            }
        }
    }
}

impl core::iter::FusedIterator for QpackFieldLineIter<'_> {}