compress-lib 0.8.0

DEFLATE, zlib, and gzip for Rust services: streaming and sans-IO with bounded memory, hard limits against decompression bombs, WebSocket permessage-deflate (RFC 7692) support, no unsafe code, and zero third-party dependencies.
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Per-message compression for WebSocket permessage-deflate (RFC 7692).
//!
//! A message is compressed with a sync flush and sent without the trailing
//! `00 00 FF FF`; the receiver appends those four bytes and inflates. With
//! context takeover the window carries over between messages; without it
//! every message stands alone (the safer default: secrets in one message
//! cannot leak through the compressed size of the next, RFC 7692 section 8).
//!
//! This module does the codec part only. Negotiating
//! `Sec-WebSocket-Extensions`, the RSV1 bit, and the per-message opt-out
//! belong to the WebSocket layer.
//!
//! ```
//! use compress_lib::message::{Compressor, Decompressor, MessageConfig};
//!
//! let config = MessageConfig::new().with_window_bits(12);
//! let mut tx = Compressor::new(config)?;
//! let mut rx = Decompressor::new(config)?;
//! let mut wire = Vec::new();
//! tx.compress_message(b"{\"hello\":\"world\"}", &mut wire)?;
//! let mut text = Vec::new();
//! rx.decompress_message(&wire, &mut text, 1 << 20)?;
//! assert_eq!(text, b"{\"hello\":\"world\"}");
//! # Ok::<(), compress_lib::Error>(())
//! ```

use alloc::vec::Vec;

use crate::{
    Error, ErrorKind, Level,
    deflate::{Deflater, Flush, Inflater, MemoryConfig},
};

/// The four bytes a sync flush ends with, removed by the sender and put
/// back by the receiver.
const TAIL: [u8; 4] = [0x00, 0x00, 0xFF, 0xFF];

/// Settings for one direction of a permessage-deflate connection.
///
/// | Setting | Default | RFC 7692 parameter |
/// |---|---|---|
/// | window bits | 15 | `server_max_window_bits` / `client_max_window_bits` |
/// | context takeover | off | `server_no_context_takeover` / `client_no_context_takeover` absent |
/// | level | 1 | (local choice) |
///
/// Idle connections: without takeover nothing carries over between
/// messages, so an idle connection can drop its engines and build new ones
/// when traffic returns (construction allocates the sizes below once, about
/// a microsecond for a decompressor). With takeover the window is
/// connection state and must be kept; [`Compressor::wipe`] and
/// [`Decompressor::wipe`] zero it when the connection ends.
///
/// Memory per direction: without takeover a [`Decompressor`] holds only its
/// decode tables (about 11 KiB, no window); with takeover it adds the window
/// (`2^window_bits` bytes). A [`Compressor`] holds a deflater sized by the
/// window bits (about 270 KiB at 15, under 100 KiB at 9).
///
/// # Examples
///
/// ```
/// use compress_lib::{Level, message::MessageConfig};
///
/// let config = MessageConfig::new().with_window_bits(10).with_context_takeover(true).with_level(Level::DEFAULT);
/// assert_eq!(config.window_bits(), 10);
/// assert!(config.context_takeover());
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct MessageConfig {
    window_bits: u8,
    context_takeover: bool,
    level: Level,
    memory: MemoryConfig,
}

impl MessageConfig {
    /// Window bits 15, no context takeover, level 1.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::MessageConfig;
    ///
    /// assert_eq!(MessageConfig::new(), MessageConfig::default());
    /// assert!(!MessageConfig::new().context_takeover());
    /// ```
    #[must_use]
    pub const fn new() -> Self {
        Self {
            window_bits: 15,
            context_takeover: false,
            level: Level::FAST,
            memory: MemoryConfig::new(),
        }
    }

    /// Sets the window bits, 8 to 15 (checked when an engine is built).
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(compress_lib::message::MessageConfig::new().with_window_bits(9).window_bits(), 9);
    /// ```
    #[must_use]
    pub const fn with_window_bits(mut self, bits: u8) -> Self {
        self.window_bits = bits;
        self
    }

    /// Keeps the window from one message to the next.
    ///
    /// # Examples
    ///
    /// ```
    /// assert!(compress_lib::message::MessageConfig::new().with_context_takeover(true).context_takeover());
    /// ```
    #[must_use]
    pub const fn with_context_takeover(mut self, on: bool) -> Self {
        self.context_takeover = on;
        self
    }

    /// Sets the compression level (the compressor only).
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::{Level, message::MessageConfig};
    ///
    /// assert_eq!(MessageConfig::new().with_level(Level::BEST).level(), Level::BEST);
    /// ```
    #[must_use]
    pub const fn with_level(mut self, level: Level) -> Self {
        self.level = level;
        self
    }

    /// Sizes the compressor's hash table and symbol buffer (see
    /// [`MemoryConfig`]); the decompressor is not affected.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::{deflate::MemoryConfig, message::MessageConfig};
    ///
    /// let config = MessageConfig::new().with_memory(MemoryConfig::new().with_hash_bits(10));
    /// assert_eq!(config.memory().hash_bits(), 10);
    /// ```
    #[must_use]
    pub const fn with_memory(mut self, memory: MemoryConfig) -> Self {
        self.memory = memory;
        self
    }

    /// The compressor's memory settings.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(compress_lib::message::MessageConfig::new().memory().hash_bits(), 0);
    /// ```
    #[must_use]
    pub const fn memory(&self) -> MemoryConfig {
        self.memory
    }

    /// The window bits.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(compress_lib::message::MessageConfig::new().window_bits(), 15);
    /// ```
    #[must_use]
    pub const fn window_bits(&self) -> u8 {
        self.window_bits
    }

    /// Whether the window carries over between messages.
    ///
    /// # Examples
    ///
    /// ```
    /// assert!(!compress_lib::message::MessageConfig::new().context_takeover());
    /// ```
    #[must_use]
    pub const fn context_takeover(&self) -> bool {
        self.context_takeover
    }

    /// The compression level.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::{Level, message::MessageConfig};
    ///
    /// assert_eq!(MessageConfig::new().level(), Level::FAST);
    /// ```
    #[must_use]
    pub const fn level(&self) -> Level {
        self.level
    }
}

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

/// Compresses messages for permessage-deflate.
///
/// # Examples
///
/// ```
/// use compress_lib::message::{Compressor, MessageConfig};
///
/// let mut tx = Compressor::new(MessageConfig::new())?;
/// let mut wire = Vec::new();
/// tx.compress_message(b"Hello", &mut wire)?;
/// assert_eq!(wire, [0xF2, 0x48, 0xCD, 0xC9, 0xC9, 0x07, 0x00]); // RFC 7692 7.2.3.1
/// # Ok::<(), compress_lib::Error>(())
/// ```
#[derive(Debug)]
pub struct Compressor {
    deflater: Deflater,
    takeover: bool,
}

impl Compressor {
    /// A compressor for one direction of a connection.
    ///
    /// # Errors
    ///
    /// [`ErrorKind::InvalidWindowBits`] outside 8 to 15.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Compressor, MessageConfig};
    ///
    /// assert!(Compressor::new(MessageConfig::new().with_window_bits(8)).is_ok());
    /// assert!(Compressor::new(MessageConfig::new().with_window_bits(16)).is_err());
    /// ```
    pub fn new(config: MessageConfig) -> Result<Self, Error> {
        let deflater = Deflater::new(config.level)
            .with_window_bits(config.window_bits)?
            .with_memory(config.memory);
        Ok(Self {
            deflater,
            takeover: config.context_takeover,
        })
    }

    /// Compresses one message, appending the payload (without the trailing
    /// `00 00 FF FF`) to `out`. Returns the payload length. An empty
    /// message compresses to the single byte `00`.
    ///
    /// The payload can be longer than the message for incompressible data;
    /// the WebSocket layer may then send the message uncompressed instead
    /// (with takeover the compressor's window has advanced either way, so
    /// call [`reset`](Compressor::reset) and tell the peer, or keep sending
    /// compressed).
    ///
    /// # Errors
    ///
    /// [`ErrorKind::AllocationFailed`] when `out` cannot grow.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Compressor, MessageConfig};
    ///
    /// let mut tx = Compressor::new(MessageConfig::new())?;
    /// let mut wire = Vec::new();
    /// assert_eq!(tx.compress_message(b"", &mut wire)?, 1);
    /// assert_eq!(wire, [0x00]);
    /// # Ok::<(), compress_lib::Error>(())
    /// ```
    pub fn compress_message(&mut self, message: &[u8], out: &mut Vec<u8>) -> Result<usize, Error> {
        let start = out.len();
        let result = self.deflater.compress_vec(message, out, Flush::Sync);
        if let Err(e) = result {
            out.truncate(start);
            self.deflater.reset();
            return Err(e);
        }
        // A sync flush always ends with the empty stored block's length
        // bytes; take them off.
        if out.len() >= start + TAIL.len() && out[out.len() - TAIL.len()..] == TAIL {
            out.truncate(out.len() - TAIL.len());
        }
        if !self.takeover {
            self.deflater.reset();
        }
        Ok(out.len() - start)
    }

    /// Zeroes the compressor's window, hash chains, and buffers (they hold
    /// plaintext of earlier messages), keeping the allocations; see
    /// [`Deflater::wipe`] for what "zeroes" can promise without `unsafe`.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Compressor, MessageConfig};
    ///
    /// let mut tx = Compressor::new(MessageConfig::new().with_context_takeover(true))?;
    /// tx.compress_message(b"token=secret", &mut Vec::new())?;
    /// tx.wipe();
    /// # Ok::<(), compress_lib::Error>(())
    /// ```
    pub fn wipe(&mut self) {
        self.deflater.wipe();
    }

    /// Forgets the window (for a connection that restarts its context).
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Compressor, MessageConfig};
    ///
    /// let mut tx = Compressor::new(MessageConfig::new().with_context_takeover(true))?;
    /// tx.reset();
    /// # Ok::<(), compress_lib::Error>(())
    /// ```
    pub fn reset(&mut self) {
        self.deflater.reset();
    }

    /// Heap bytes held.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Compressor, MessageConfig};
    ///
    /// let small = Compressor::new(MessageConfig::new().with_window_bits(9))?;
    /// let big = Compressor::new(MessageConfig::new())?;
    /// assert!(small.memory_in_use() < big.memory_in_use());
    /// # Ok::<(), compress_lib::Error>(())
    /// ```
    #[must_use]
    pub fn memory_in_use(&self) -> usize {
        self.deflater.memory_in_use()
    }
}

/// Decompresses permessage-deflate messages.
///
/// The output budget is checked per output chunk while inflating, never
/// after, so a compressed bomb stops at the budget; the error is
/// [`ErrorKind::OutputLimit`] (WebSocket close code 1009). Undecodable data
/// is any other kind (close code 1007).
///
/// # Examples
///
/// ```
/// use compress_lib::{ErrorKind, message::{Decompressor, MessageConfig}};
///
/// let mut rx = Decompressor::new(MessageConfig::new())?;
/// let mut text = Vec::new();
/// rx.decompress_message(&[0xF2, 0x48, 0xCD, 0xC9, 0xC9, 0x07, 0x00], &mut text, 1024)?;
/// assert_eq!(text, b"Hello");
/// let err = rx.decompress_message(&[0xF2, 0x48, 0xCD, 0xC9, 0xC9, 0x07, 0x00], &mut Vec::new(), 4).unwrap_err();
/// assert_eq!(err.kind(), ErrorKind::OutputLimit);
/// # Ok::<(), compress_lib::Error>(())
/// ```
#[derive(Debug)]
pub struct Decompressor {
    inflater: Inflater,
    takeover: bool,
    /// Where the current message starts in the caller's vector, while a
    /// fragmented message is open.
    open: Option<OpenMessage>,
}

#[derive(Clone, Copy, Debug)]
struct OpenMessage {
    start: usize,
    produced: usize,
    max: usize,
}

impl Decompressor {
    /// A decompressor for one direction of a connection.
    ///
    /// # Errors
    ///
    /// [`ErrorKind::InvalidWindowBits`] outside 8 to 15.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Decompressor, MessageConfig};
    ///
    /// assert!(Decompressor::new(MessageConfig::new().with_window_bits(8)).is_ok());
    /// ```
    pub fn new(config: MessageConfig) -> Result<Self, Error> {
        let inflater = if config.context_takeover {
            Inflater::new().with_window_bits(config.window_bits)?
        } else {
            // Each message is decoded into one vector, which is its own
            // history: no window is kept.
            Inflater::without_history(crate::Limits::unlimited())
                .with_window_bits(config.window_bits)?
        };
        Ok(Self {
            inflater,
            takeover: config.context_takeover,
            open: None,
        })
    }

    /// Decompresses one whole message payload, appending to `out`, with at
    /// most `max` bytes of output. Returns the bytes appended.
    ///
    /// # Errors
    ///
    /// `OutputLimit` past `max`; `TrailingData` for nonzero bytes after a
    /// final block; `UnexpectedEnd` when the message, with the
    /// `00 00 FF FF` tail appended, does not end on a block boundary (a
    /// block cut short, or a sender that left the tail in place); any
    /// decoding error. After an error the decompressor is reset
    /// (the connection should be failed anyway).
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Decompressor, MessageConfig};
    ///
    /// let mut rx = Decompressor::new(MessageConfig::new())?;
    /// let mut out = Vec::new();
    /// assert_eq!(rx.decompress_message(&[0x00], &mut out, 16)?, 0); // empty message
    /// # Ok::<(), compress_lib::Error>(())
    /// ```
    pub fn decompress_message(
        &mut self,
        payload: &[u8],
        out: &mut Vec<u8>,
        max: usize,
    ) -> Result<usize, Error> {
        self.decompress_fragment(payload, true, out, max)
    }

    /// Decompresses one frame of a message, appending to `out`; `last` marks
    /// the final frame. Every fragment of a message must append to the same
    /// vector, untouched in between: the vector is the message's history.
    /// `max` caps the whole message. Returns the bytes appended by this
    /// fragment.
    ///
    /// # Errors
    ///
    /// As [`decompress_message`](Decompressor::decompress_message), plus
    /// `InvalidState` when `out` was changed between fragments.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Decompressor, MessageConfig};
    ///
    /// let payload = [0xF2, 0x48, 0xCD, 0xC9, 0xC9, 0x07, 0x00];
    /// let mut rx = Decompressor::new(MessageConfig::new())?;
    /// let mut out = Vec::new();
    /// rx.decompress_fragment(&payload[..3], false, &mut out, 1024)?;
    /// rx.decompress_fragment(&payload[3..], true, &mut out, 1024)?;
    /// assert_eq!(out, b"Hello");
    /// # Ok::<(), compress_lib::Error>(())
    /// ```
    pub fn decompress_fragment(
        &mut self,
        fragment: &[u8],
        last: bool,
        out: &mut Vec<u8>,
        max: usize,
    ) -> Result<usize, Error> {
        let result = self.fragment(fragment, last, out, max);
        if result.is_err() {
            self.open = None;
            self.inflater.reset();
        }
        result
    }

    fn fragment(
        &mut self,
        fragment: &[u8],
        last: bool,
        out: &mut Vec<u8>,
        max: usize,
    ) -> Result<usize, Error> {
        let open = match self.open {
            Some(m) => {
                if out.len() != m.start + m.produced {
                    return Err(Error::config(ErrorKind::InvalidState));
                }
                m
            }
            None => OpenMessage {
                start: out.len(),
                produced: 0,
                max,
            },
        };
        let before = out.len();
        let base = self.inflater.total_out() - open.produced as u64;
        self.inflater
            .set_max_output(base.saturating_add(open.max as u64));

        let p = self
            .inflater
            .decompress_into(fragment, out, open.start, false)?;
        // After a final block only zero padding may follow (RFC 7692's own
        // example in section 7.2.3.4 ends with one such byte); anything else
        // is refused.
        if self.inflater.is_done() && fragment[p.consumed..].iter().any(|&b| b != 0) {
            return Err(Error::new(
                ErrorKind::TrailingData,
                self.inflater.total_in(),
            ));
        }
        if last && !self.inflater.is_done() {
            let t = self
                .inflater
                .decompress_into(&TAIL, out, open.start, false)?;
            // The tail is the empty stored block a sync flush ends with, so
            // after it the message must sit on a block boundary (RFC 7692
            // section 7.2.2). A block cut short (a stored block missing
            // bytes, a Huffman block without its end code) would otherwise
            // pass, and with context takeover corrupt the next message.
            if t.consumed != TAIL.len() || !self.inflater.at_block_boundary() {
                return Err(Error::new(
                    ErrorKind::UnexpectedEnd,
                    self.inflater.total_in(),
                ));
            }
        }
        let produced = open.produced + (out.len() - before);
        if !last {
            self.open = Some(OpenMessage { produced, ..open });
            return Ok(out.len() - before);
        }
        self.open = None;
        if self.takeover {
            self.inflater.remember(&out[open.start..]);
            if self.inflater.is_done() {
                self.inflater.next_stream()?;
            }
        } else {
            self.inflater.reset();
        }
        Ok(out.len() - before)
    }

    /// Zeroes the decompressor's window and decode tables and forgets any
    /// open message, keeping the allocations; see [`Inflater::wipe`]. The
    /// message vectors are the caller's to wipe.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Decompressor, MessageConfig};
    ///
    /// let mut rx = Decompressor::new(MessageConfig::new().with_context_takeover(true))?;
    /// rx.wipe();
    /// # Ok::<(), compress_lib::Error>(())
    /// ```
    pub fn wipe(&mut self) {
        self.open = None;
        self.inflater.wipe();
    }

    /// Forgets the window and any open message.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Decompressor, MessageConfig};
    ///
    /// let mut rx = Decompressor::new(MessageConfig::new())?;
    /// rx.reset();
    /// # Ok::<(), compress_lib::Error>(())
    /// ```
    pub fn reset(&mut self) {
        self.open = None;
        self.inflater.reset();
    }

    /// Heap bytes held: the decode tables, plus the window with takeover.
    ///
    /// # Examples
    ///
    /// ```
    /// use compress_lib::message::{Decompressor, MessageConfig};
    ///
    /// let lean = Decompressor::new(MessageConfig::new())?;
    /// let takeover = Decompressor::new(MessageConfig::new().with_context_takeover(true))?;
    /// assert!(lean.memory_in_use() + 32 * 1024 <= takeover.memory_in_use());
    /// # Ok::<(), compress_lib::Error>(())
    /// ```
    #[must_use]
    pub fn memory_in_use(&self) -> usize {
        self.inflater.memory_in_use()
    }
}