ls-qpack-rs 0.3.1

QPACK Field Compression for HTTP/3 (RFC 9204)
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright 2022 Biagio Festa

//! Module for encoding operations.
//!
//! The main struct of this module is [`Encoder`].
//!
//! # Examples
//!
//! ## Only Static Table
//! ```
//! use ls_qpack_rs::encoder::Encoder;
//! use ls_qpack_rs::StreamId;
//!
//! let (enc_hdr, enc_stream) = Encoder::new()
//!     .encode_all(
//!         StreamId::new(0),
//!         [(":status", "404"), (":method", "connect")],
//!     )
//!     .unwrap()
//!     .into();
//!
//! // Using only static table. We don't expect stream data.
//! assert_eq!(enc_stream.len(), 0);
//! println!("Encoded data: {:?}", enc_hdr);
//! ```
use crate::header::TryIntoHeader;
use crate::StreamId;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Display;
use std::marker::PhantomPinned;
use std::pin::Pin;

/// The kind of encoder error that occurred.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum EncoderErrorKind {
    /// The header could not be converted (e.g., invalid UTF-8 or too long).
    InvalidHeader,
    /// The C encoder returned an error during initialization.
    InitFailed,
    /// The C encoder returned an error during encoding.
    EncodeFailed,
    /// The C encoder failed to finalize the header block.
    EndHeaderFailed,
    /// The C encoder returned an error when processing decoder stream data.
    FeedFailed,
}

/// Error during encoding operations.
pub struct EncoderError {
    kind: EncoderErrorKind,
}

impl EncoderError {
    /// Returns the kind of encoder error.
    pub fn kind(&self) -> EncoderErrorKind {
        self.kind
    }

    fn new(kind: EncoderErrorKind) -> Self {
        Self { kind }
    }
}

/// A QPACK encoder.
pub struct Encoder {
    inner: Pin<Box<InnerEncoder>>,
    seqnos: HashMap<StreamId, u32>,
}

impl Encoder {
    /// Creates a new encoder.
    ///
    /// If not configured, this encoder will only make use of a static table.
    ///
    /// Once peer's settings has been received, you might want to allocate
    /// the dynamic table by means of [`Self::configure`].
    #[inline]
    pub fn new() -> Self {
        Self {
            inner: InnerEncoder::new(),
            seqnos: HashMap::new(),
        }
    }

    /// Sets dynamic table size and it applies peer's settings.
    ///
    /// # Returns
    /// SDTC instruction (Set Dynamic Table Capacity) data. This should be
    /// transmitted to the peer via encoder stream.
    ///
    /// # Notes
    ///   * `dyn_table_size` can be `0` to avoid dynamic table.
    ///   * `dyn_table_size` cannot be larger than `max_table_size`.
    #[inline]
    pub fn configure(
        &mut self,
        max_table_size: u32,
        dyn_table_size: u32,
        max_blocked_streams: u32,
    ) -> Result<SDTCInstruction, EncoderError> {
        self.inner
            .as_mut()
            .init(max_table_size, dyn_table_size, max_blocked_streams)
            .map(SDTCInstruction)
    }

    /// Encodes an entire header block (a list of headers).
    ///
    /// # Returns
    /// The encoded data (see [`BuffersEncoded`]).
    ///
    /// # Examples
    /// ```
    /// use ls_qpack_rs::encoder::Encoder;
    /// use ls_qpack_rs::StreamId;
    ///
    /// let mut encoder = Encoder::new();
    /// let (enc_hdr, enc_stream) = encoder
    ///     .encode_all(
    ///         StreamId::new(0),
    ///         [(":status", "404"), (":method", "connect")],
    ///     )
    ///     .unwrap()
    ///     .into();
    /// ```
    pub fn encode_all<I, H>(
        &mut self,
        stream_id: StreamId,
        headers: I,
    ) -> Result<BuffersEncoded, EncoderError>
    where
        I: IntoIterator<Item = H>,
        H: TryIntoHeader,
    {
        let mut encoding = self.encoding(stream_id);

        for header in headers {
            encoding.append(header)?;
        }

        encoding.encode()
    }

    /// Encodes a list of headers in a sequential fashion way.
    ///
    /// This method is similar to [`Self::encode_all`]. However, instead of
    /// providing the entire list of header, it is possible to append a header step by step.
    ///
    /// See [`EncodingBlock`].
    ///
    /// # Examples
    /// ```
    /// use ls_qpack_rs::encoder::Encoder;
    /// use ls_qpack_rs::StreamId;
    ///
    /// let mut encoder = Encoder::new();
    /// let mut encoding_block = encoder.encoding(StreamId::new(0));
    ///
    /// encoding_block.append((":status", "404"));
    /// encoding_block.append((":method", "connect"));
    ///
    /// let (enc_hdr, enc_stream) = encoding_block.encode().unwrap().into();
    /// ```
    #[inline]
    pub fn encoding(&mut self, stream_id: StreamId) -> EncodingBlock<'_> {
        let seqno = {
            let seqno_ref = self.seqnos.entry(stream_id).or_default();
            std::mem::replace(seqno_ref, seqno_ref.wrapping_add(1))
        };

        EncodingBlock::new(self, stream_id, seqno)
    }

    /// Feeds data from decoder's buffer stream.
    pub fn feed<D>(&mut self, data: D) -> Result<(), EncoderError>
    where
        D: AsRef<[u8]>,
    {
        self.inner.as_mut().feed_decoder_data(data.as_ref())
    }

    /// Return estimated compression ratio until this point.
    ///
    /// Compression ratio is defined as size of the output divided by the size of the
    /// input, where output includes both header blocks and instructions sent
    /// on the encoder stream.
    #[inline]
    pub fn ratio(&self) -> f32 {
        self.inner.as_ref().ratio()
    }

    #[inline]
    fn inner_mut(&mut self) -> Pin<&mut InnerEncoder> {
        self.inner.as_mut()
    }
}

impl Default for Encoder {
    fn default() -> Self {
        Self::new()
    }
}

/// SDTC instruction
///
/// *Set Dynamic Table Capacity* data.
/// It is a buffer of data to be fed to the peer's decoder.
#[derive(Debug)]
pub struct SDTCInstruction(Box<[u8]>);

impl SDTCInstruction {
    /// Returns the buffer data.
    #[inline]
    pub fn data(&self) -> &[u8] {
        &self.0
    }

    /// Takes the ownership returning the inner buffer data.
    #[inline]
    pub fn take(self) -> Box<[u8]> {
        self.0
    }
}

impl AsRef<[u8]> for SDTCInstruction {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.data()
    }
}

impl From<SDTCInstruction> for Box<[u8]> {
    fn from(sdtc_instruction: SDTCInstruction) -> Self {
        sdtc_instruction.0
    }
}

/// An encoding operation for a headers block.
///
/// This is the result of [`Encoder::encoding`] method.
pub struct EncodingBlock<'a>(&'a mut Encoder);

impl<'a> EncodingBlock<'a> {
    fn new(encoder: &'a mut Encoder, stream_id: StreamId, seqno: u32) -> Self {
        encoder
            .inner_mut()
            .start_header_block(stream_id, seqno)
            .map(|()| Self(encoder))
            .unwrap() // unwrap is safe here because no other start-block can happen
    }

    /// Appends a header to encode.
    pub fn append<H>(&mut self, header: H) -> Result<&mut Self, EncoderError>
    where
        H: TryIntoHeader,
    {
        self.0.inner_mut().encode(header).map(|()| self)
    }

    /// Encodes the header block.
    pub fn encode(self) -> Result<BuffersEncoded, EncoderError> {
        self.0
            .inner_mut()
            .end_header_block()
            .map(|(header, stream)| BuffersEncoded {
                header: header.into_boxed_slice(),
                stream: stream.into_boxed_slice(),
            })
    }
}

/// The result of the encoding operation.
///
/// This is the result of [`Encoder::encode_all`] or [`EncodingBlock::encode`].
#[derive(Debug)]
pub struct BuffersEncoded {
    header: Box<[u8]>,
    stream: Box<[u8]>,
}

impl BuffersEncoded {
    /// The data buffer of encoded headers.
    pub fn header(&self) -> &[u8] {
        &self.header
    }

    /// The buffer of the stream data for the decoder.
    pub fn stream(&self) -> &[u8] {
        &self.stream
    }

    pub fn take(self) -> (Box<[u8]>, Box<[u8]>) {
        self.into()
    }
}

impl From<BuffersEncoded> for (Box<[u8]>, Box<[u8]>) {
    fn from(buffers_encoded: BuffersEncoded) -> Self {
        (buffers_encoded.header, buffers_encoded.stream)
    }
}

struct InnerEncoder {
    encoder: ls_qpack_rs_sys::lsqpack_enc,
    enc_buffer: Vec<u8>,
    hdr_buffer: Vec<u8>,
    _marker: PhantomPinned,
}

impl InnerEncoder {
    fn new() -> Pin<Box<Self>> {
        let mut this = Box::new(Self {
            encoder: ls_qpack_rs_sys::lsqpack_enc::default(),
            enc_buffer: Vec::new(),
            hdr_buffer: Vec::new(),
            _marker: PhantomPinned,
        });

        // SAFETY: `this.encoder` is a valid, default-initialized `lsqpack_enc` struct.
        // The null second argument indicates no logging context. `this` is a live Box
        // allocation so `&mut this.encoder` is a valid pointer.
        unsafe {
            ls_qpack_rs_sys::lsqpack_enc_preinit(&mut this.encoder, std::ptr::null_mut());
        }

        Box::into_pin(this)
    }

    fn init(
        self: Pin<&mut Self>,
        max_table_size: u32,
        dyn_table_size: u32,
        max_blocked_streams: u32,
    ) -> Result<Box<[u8]>, EncoderError> {
        // SAFETY: We only access plain data fields (encoder, buffer, sdtc_buffer_size)
        // through the unpinned reference — none of which are address-sensitive.
        // The PhantomPinned marker is never moved.
        let this = unsafe { self.get_unchecked_mut() };

        let mut buffer = vec![0; ls_qpack_rs_sys::LSQPACK_LONGEST_SDTC as usize];
        let mut sdtc_buffer_size = buffer.len();

        // SAFETY: `this.encoder` was pre-initialized by `lsqpack_enc_preinit` in `new()`.
        // The buffer is a freshly allocated Vec with `LSQPACK_LONGEST_SDTC` bytes of space.
        // `sdtc_buffer_size` is set to the buffer's capacity. All pointer arguments are valid.
        let result = unsafe {
            ls_qpack_rs_sys::lsqpack_enc_init(
                &mut this.encoder,
                std::ptr::null_mut(),
                max_table_size,
                dyn_table_size,
                max_blocked_streams,
                ls_qpack_rs_sys::lsqpack_enc_opts_LSQPACK_ENC_OPT_STAGE_2,
                buffer.as_mut_ptr(),
                &mut sdtc_buffer_size,
            )
        };

        if result == 0 {
            buffer.truncate(sdtc_buffer_size);
            Ok(buffer.into_boxed_slice())
        } else {
            Err(EncoderError::new(EncoderErrorKind::InitFailed))
        }
    }

    /// Returns error if another block is started before completing the previous.
    fn start_header_block(
        self: Pin<&mut Self>,
        stream_id: StreamId,
        seqno: u32,
    ) -> Result<(), EncoderError> {
        // SAFETY: We only access plain data fields — none are address-sensitive.
        let this = unsafe { self.get_unchecked_mut() };

        // SAFETY: `this.encoder` is a fully initialized encoder (preinit was called in `new()`).
        // `stream_id` and `seqno` are plain integer values.
        let result = unsafe {
            ls_qpack_rs_sys::lsqpack_enc_start_header(&mut this.encoder, stream_id.value(), seqno)
        };

        if result == 0 {
            this.enc_buffer.clear();
            this.hdr_buffer.clear();

            Ok(())
        } else {
            Err(EncoderError::new(EncoderErrorKind::EncodeFailed))
        }
    }

    fn encode<H>(self: Pin<&mut Self>, header: H) -> Result<(), EncoderError>
    where
        H: TryIntoHeader,
    {
        const BUFFER_SIZE: usize = 4096;

        let mut header = header
            .try_into_header()
            .map_err(|_| EncoderError::new(EncoderErrorKind::InvalidHeader))?;

        // SAFETY: We only access plain data fields (encoder, enc_buffer, hdr_buffer)
        // — none are address-sensitive.
        let this = unsafe { self.get_unchecked_mut() };

        let enc_buffer_offset = this.enc_buffer.len();
        this.enc_buffer.resize(enc_buffer_offset + BUFFER_SIZE, 0);

        let hdr_buffer_offset = this.hdr_buffer.len();
        this.hdr_buffer.resize(hdr_buffer_offset + BUFFER_SIZE, 0);

        let mut enc_buffer_size = this.enc_buffer.len() - enc_buffer_offset;
        let mut hdr_buffer_size = this.hdr_buffer.len() - hdr_buffer_offset;

        // SAFETY: `this.encoder` is initialized and a header block was started via
        // `start_header_block`. The enc_buffer and hdr_buffer pointers are offset into
        // valid Vec allocations with at least BUFFER_SIZE bytes of available space.
        // `header.build_lsxpack_header()` returns a valid lsxpack_header reference.
        let result = unsafe {
            ls_qpack_rs_sys::lsqpack_enc_encode(
                &mut this.encoder,
                this.enc_buffer.as_mut_ptr().add(enc_buffer_offset),
                &mut enc_buffer_size,
                this.hdr_buffer.as_mut_ptr().add(hdr_buffer_offset),
                &mut hdr_buffer_size,
                header.build_lsxpack_header().as_ref(),
                0,
            )
        };

        if result == ls_qpack_rs_sys::lsqpack_enc_status_LQES_OK {
            this.enc_buffer
                .truncate(enc_buffer_offset + enc_buffer_size);
            this.hdr_buffer
                .truncate(hdr_buffer_offset + hdr_buffer_size);

            Ok(())
        } else {
            this.enc_buffer.truncate(enc_buffer_offset);
            this.hdr_buffer.truncate(hdr_buffer_offset);

            Err(EncoderError::new(EncoderErrorKind::EncodeFailed))
        }
    }

    /// Finalize the encoded header block.
    ///
    /// It computes the header prefix and return the encoded buffers.
    /// It returns a buffer pair:
    ///   * Buffer of encoded header
    ///   * Buffer of encoded bytes to write on the encoder stream.
    fn end_header_block(self: Pin<&mut Self>) -> Result<(Vec<u8>, Vec<u8>), EncoderError> {
        // SAFETY: We only access plain data fields — none are address-sensitive.
        let this = unsafe { self.get_unchecked_mut() };

        // SAFETY: `this.encoder` is initialized and a header block is in progress.
        // Reading the prefix size is a pure query with no side effects.
        let max_prefix_len =
            unsafe { ls_qpack_rs_sys::lsqpack_enc_header_block_prefix_size(&this.encoder) };

        let mut hdr_block = vec![0; max_prefix_len + this.hdr_buffer.len()];

        // SAFETY: `hdr_block` has at least `max_prefix_len` bytes available.
        // `this.encoder` has an active header block that was started and populated.
        let hdr_prefix_len = unsafe {
            ls_qpack_rs_sys::lsqpack_enc_end_header(
                &mut this.encoder,
                hdr_block.as_mut_ptr(),
                max_prefix_len,
                std::ptr::null_mut(),
            )
        };

        if hdr_prefix_len > 0 {
            hdr_block.truncate(hdr_prefix_len as usize);
            hdr_block.extend_from_slice(&this.hdr_buffer);

            Ok((hdr_block, std::mem::take(&mut this.enc_buffer)))
        } else {
            Err(EncoderError::new(EncoderErrorKind::EndHeaderFailed))
        }
    }

    fn feed_decoder_data(self: Pin<&mut Self>, data: &[u8]) -> Result<(), EncoderError> {
        // SAFETY: We only access plain data fields — none are address-sensitive.
        let this = unsafe { self.get_unchecked_mut() };

        // SAFETY: `this.encoder` is initialized. `data.as_ptr()` and `data.len()` describe
        // a valid byte slice. The C function reads exactly `data.len()` bytes.
        let result = unsafe {
            ls_qpack_rs_sys::lsqpack_enc_decoder_in(&mut this.encoder, data.as_ptr(), data.len())
        };

        if result == 0 {
            Ok(())
        } else {
            Err(EncoderError::new(EncoderErrorKind::FeedFailed))
        }
    }

    fn ratio(self: Pin<&Self>) -> f32 {
        // SAFETY: `self.encoder` is initialized. This is a read-only query.
        unsafe { ls_qpack_rs_sys::lsqpack_enc_ratio(&self.encoder) }
    }
}

impl Drop for InnerEncoder {
    fn drop(&mut self) {
        // SAFETY: `self.encoder` was initialized by `lsqpack_enc_preinit` (and possibly
        // `lsqpack_enc_init`). `lsqpack_enc_cleanup` frees all resources owned by the
        // C encoder. This is called exactly once during drop.
        unsafe { ls_qpack_rs_sys::lsqpack_enc_cleanup(&mut self.encoder) }
    }
}

// SAFETY: The C `lsqpack_enc` struct is a self-contained state machine. It uses no
// thread-local storage, no global mutable state, and no thread-affine resources. All
// internal raw pointers reference memory owned by the struct itself (allocated during
// init, freed during cleanup). It is safe to move an InnerEncoder to another thread.
unsafe impl Send for InnerEncoder {}

// SAFETY: All access to InnerEncoder goes through Pin<&mut Self> methods, meaning Rust's
// borrow checker guarantees exclusive access. The public Encoder API only exposes &mut self
// methods, so no concurrent &self access is possible. Shared references to the outer
// Encoder cannot invoke any mutation on the C state.
unsafe impl Sync for InnerEncoder {}

const _: () = {
    fn _assert_send<T: Send>() {}
    fn _assert_sync<T: Sync>() {}
    fn _assert_all() {
        _assert_send::<Encoder>();
        _assert_sync::<Encoder>();
    }
};

impl Debug for EncoderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EncoderError")
            .field("kind", &self.kind)
            .finish()
    }
}

impl Display for EncoderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.kind {
            EncoderErrorKind::InvalidHeader => write!(f, "invalid header"),
            EncoderErrorKind::InitFailed => write!(f, "encoder initialization failed"),
            EncoderErrorKind::EncodeFailed => write!(f, "encoding operation failed"),
            EncoderErrorKind::EndHeaderFailed => write!(f, "failed to finalize header block"),
            EncoderErrorKind::FeedFailed => write!(f, "failed to process decoder stream data"),
        }
    }
}

impl std::error::Error for EncoderError {}

#[cfg(test)]
mod tests {
    use super::Encoder;
    use super::StreamId;

    #[test]
    fn test_encoder_determinism_static() {
        let mut encoder = Encoder::new();

        let results = (0..1024)
            .map(|_| {
                encoder
                    .encode_all(StreamId::new(0), utilities::HEADERS_LIST_1)
                    .unwrap()
            })
            .collect::<Vec<_>>();

        assert!(results.iter().all(|b| b.header() == results[0].header()));
        assert!(results.iter().all(|b| b.stream().is_empty()));
    }

    mod utilities {
        pub(super) const HEADERS_LIST_1: [(&str, &str); 2] =
            [(":status", "404"), (":method", "connect")];
    }
}