rustbinary 0.1.10

A bounded nextjson binary codec with adaptive frames, zero-allocation paths, schema evolution, and authenticated pipelines
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
//! In-tree lossless compressor: LZ77 sliding-window matching with a packed
//! bitstream (replaces the `zstd` dependency).
//!
//! The `compression` feature is a `std`-only layer, but the codec itself is
//! pure `no_std` + `alloc` so it can be reused anywhere. The design is a
//! deliberately simple, auditable LZ77:
//!
//! - **Encoder**: a hash-chain matcher over a 32 KiB sliding window. For each
//!   position it finds the longest match (minimum 3 bytes, maximum 258) and
//!   emits either a literal byte or a `(length, distance)` pair. The `level`
//!   controls how many hash-chain candidates are tried per position: higher
//!   levels search further for a better (longer) match at the cost of speed.
//! - **Bitstream**: one flag bit per token (`0` literal, `1` match), then
//!   either 8 literal bits or an 8-bit length and a 15-bit distance. Lengths
//!   are stored minus 3 (0..=255 maps to 3..=258); distances are stored minus
//!   1 (0..=32767 maps to 1..=32768).
//! - **Decoder**: single pass, bounds-checked, zero-unsafe. A malformed frame
//!   is rejected with [`LzError`] instead of reading out of bounds.
//!
//! The frame body is `u32 LE raw length` followed by the packed token stream;
//! the envelope (`src/compression.rs`) adds magic/version/flags and the raw /
//! stored length fields it already carries. Raw payloads smaller than a
//! literal-copy are emitted verbatim by the caller, so this module only ever
//! sees data it is allowed to compress.

use alloc::vec;
use alloc::vec::Vec;

/// Sliding-window size: the maximum distance back a match can reference.
pub const WINDOW_SIZE: usize = 32768;
/// Minimum match length worth emitting (below this, a literal is cheaper).
const MIN_MATCH: usize = 3;
/// Maximum match length representable in the 8-bit length field.
const MAX_MATCH: usize = 258;
/// Number of hash-chain buckets (a power of two for cheap masking).
const HASH_BUCKETS: usize = 65536;

/// Compression / decompression failure.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LzError {
    /// The compressed stream was truncated.
    UnexpectedEnd,
    /// The compressed stream references a match before the window start.
    InvalidDistance,
    /// The decompressed length differs from the declared frame length.
    LengthMismatch,
    /// Extra bytes or nonzero final padding follow the declared output.
    TrailingData,
    /// The decoded buffer could not be reserved.
    AllocationFailed,
}

impl core::fmt::Display for LzError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::UnexpectedEnd => f.write_str("LZ77 stream ended unexpectedly"),
            Self::InvalidDistance => f.write_str("LZ77 match distance out of range"),
            Self::LengthMismatch => f.write_str("LZ77 decompressed length mismatch"),
            Self::TrailingData => f.write_str("LZ77 trailing data or nonzero padding"),
            Self::AllocationFailed => f.write_str("LZ77 output allocation failed"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for LzError {}

/// Computes the 16-bit hash of a 3-byte prefix.
#[inline(always)]
fn hash3(bytes: &[u8]) -> usize {
    let a = bytes[0] as usize;
    let b = bytes[1] as usize;
    let c = bytes[2] as usize;
    let mut hash = 0x811c_9dc5usize;
    hash ^= a;
    hash = hash.wrapping_mul(0x0100_0193);
    hash ^= b;
    hash = hash.wrapping_mul(0x0100_0193);
    hash ^= c;
    hash = hash.wrapping_mul(0x0100_0193);
    (hash >> 16) & (HASH_BUCKETS - 1)
}

/// Compresses `input` with LZ77 matching. Returns an empty `Vec` if the
/// input is too small to match or exceeds the u32 frame length. The caller
/// compares the packed frame with the raw input before choosing its format.
///
/// `level` is a 1..=22 hint controlling search effort; values outside the
/// range are clamped. Higher levels yield marginally better ratios on
/// repetitive data at proportionally higher CPU cost.
pub fn compress(input: &[u8], level: i32) -> Vec<u8> {
    if input.len() < MIN_MATCH || u32::try_from(input.len()).is_err() {
        return Vec::new();
    }
    // Clamp level; chain budget grows with level (1 -> 8, 22 -> 92).
    let level = level.clamp(1, 22);
    let max_chain = (4 + (level as usize) * 4).min(96);

    // Hash-chain bookkeeping. `prev[i]` holds the previous position with the
    // same 3-byte hash (or `u32::MAX` when none), stored as absolute positions
    // in a window-sized ring. Reject expired candidates before reading their
    // ring slots: those slots may already describe newer positions.
    let mut head = vec![u32::MAX; HASH_BUCKETS];
    let mut prev = vec![u32::MAX; input.len().min(WINDOW_SIZE)];

    let mut output = BitWriter::new();
    // Frame body: u32 LE uncompressed length.
    output.write_u32(input.len() as u32);

    let mut pos = 0usize;
    while pos < input.len() {
        // Try to find the longest match starting at `pos`.
        let mut best_len = 0usize;
        let mut best_dist = 0usize;
        let max_len = core::cmp::min(MAX_MATCH, input.len() - pos);
        if max_len >= MIN_MATCH {
            let h = hash3(&input[pos..]);
            let mut candidate = head[h];
            let mut chain = 0usize;
            let window_start = pos.saturating_sub(WINDOW_SIZE);
            while candidate != u32::MAX && chain < max_chain {
                let c = candidate as usize;
                if c < window_start {
                    break;
                }
                // Only consider candidates within the 15-bit distance field.
                let dist = pos - c;
                if dist > WINDOW_SIZE {
                    candidate = prev[c % WINDOW_SIZE];
                    chain += 1;
                    continue;
                }
                if c + max_len <= input.len() {
                    let mut len = 0usize;
                    let remaining = core::cmp::min(max_len, input.len() - c);
                    while len < remaining && input[c + len] == input[pos + len] {
                        len += 1;
                    }
                    if len > best_len {
                        best_len = len;
                        best_dist = dist;
                        if len == max_len {
                            break;
                        }
                    }
                }
                candidate = prev[c % WINDOW_SIZE];
                chain += 1;
            }
        }

        if best_len >= MIN_MATCH {
            // Emit a match token.
            output.write_bit(true);
            output.write_bits((best_len - MIN_MATCH) as u32, 8);
            output.write_bits((best_dist - 1) as u32, 15);
            // Insert every position in the match into the hash chain so
            // subsequent matches can reference into it.
            let end = core::cmp::min(pos + best_len, input.len());
            for i in pos..end {
                if i + MIN_MATCH <= input.len() {
                    let h = hash3(&input[i..]);
                    prev[i % WINDOW_SIZE] = head[h];
                    head[h] = i as u32;
                }
            }
            pos = end;
        } else {
            // Emit a literal token and insert this single position.
            output.write_bit(false);
            output.write_bits(input[pos] as u32, 8);
            if pos + MIN_MATCH <= input.len() {
                let h = hash3(&input[pos..]);
                prev[pos % WINDOW_SIZE] = head[h];
                head[h] = pos as u32;
            }
            pos += 1;
        }
    }
    output.finish()
}

/// Decompresses a frame produced by [`compress`] into at most `expected`
/// bytes. Returns the decompressed payload.
pub fn decompress(input: &[u8], expected: usize) -> Result<Vec<u8>, LzError> {
    let mut reader = BitReader::new(input);
    let declared = reader.read_u32().ok_or(LzError::UnexpectedEnd)? as usize;
    // `expected` is the caller's upper bound on the decoded size (used to cap
    // allocation against an attacker-controlled header). Declared must not
    // exceed it.
    if declared > expected {
        return Err(LzError::LengthMismatch);
    }
    let mut output = Vec::new();
    while output.len() < declared {
        let is_match = reader.read_bit().ok_or(LzError::UnexpectedEnd)?;
        if is_match {
            let length = reader.read_bits(8).ok_or(LzError::UnexpectedEnd)? as usize + MIN_MATCH;
            let distance = reader.read_bits(15).ok_or(LzError::UnexpectedEnd)? as usize + 1;
            if distance > output.len() {
                return Err(LzError::InvalidDistance);
            }
            if length > declared - output.len() {
                return Err(LzError::LengthMismatch);
            }
            reserve_output(&mut output, length, declared)?;
            let start = output.len();
            for i in 0..length {
                let src = output[start + i - distance];
                output.push(src);
            }
        } else {
            let byte = reader.read_bits(8).ok_or(LzError::UnexpectedEnd)? as u8;
            reserve_output(&mut output, 1, declared)?;
            output.push(byte);
        }
    }
    if !reader.finished() {
        return Err(LzError::TrailingData);
    }
    Ok(output)
}

fn reserve_output(output: &mut Vec<u8>, additional: usize, limit: usize) -> Result<(), LzError> {
    let needed = output
        .len()
        .checked_add(additional)
        .ok_or(LzError::LengthMismatch)?;
    if needed > limit {
        return Err(LzError::LengthMismatch);
    }
    if needed > output.capacity() {
        let target = output
            .capacity()
            .saturating_mul(2)
            .max(64)
            .max(needed)
            .min(limit);
        output
            .try_reserve_exact(target - output.len())
            .map_err(|_| LzError::AllocationFailed)?;
    }
    Ok(())
}

/// Little-endian bit writer (LSB-first within each byte).
struct BitWriter {
    buffer: Vec<u8>,
    bit_buffer: u64,
    bit_count: u32,
}

impl BitWriter {
    fn new() -> Self {
        Self {
            buffer: Vec::new(),
            bit_buffer: 0,
            bit_count: 0,
        }
    }

    fn write_bit(&mut self, value: bool) {
        // Make sure at least 1 bit is free; `flush` empties full bytes and
        // leaves fewer than 8 bits in the buffer.
        self.ensure_capacity(1);
        self.bit_buffer |= (value as u64) << self.bit_count;
        self.bit_count += 1;
        if self.bit_count == 64 {
            self.flush();
        }
    }

    fn write_bits(&mut self, value: u32, count: u32) {
        debug_assert!(count <= 32);
        self.ensure_capacity(count);
        self.bit_buffer |= (value as u64) << self.bit_count;
        self.bit_count += count;
        if self.bit_count >= 64 {
            self.flush();
        }
    }

    /// Guarantees that at least `needed` more bits fit in the u64 buffer by
    /// flushing whole bytes first. After a flush, `bit_count < 8`, so
    /// `bit_count + needed <= 39` for any `needed <= 32` and never overflows.
    fn ensure_capacity(&mut self, needed: u32) {
        if self.bit_count + needed > 64 {
            self.flush();
        }
    }

    fn write_u32(&mut self, value: u32) {
        // Whole bytes: bypass the bit buffer.
        if self.bit_count == 0 {
            self.buffer.extend_from_slice(&value.to_le_bytes());
            return;
        }
        for shift in (0..32).step_by(8) {
            self.write_bits((value >> shift) & 0xff, 8);
        }
    }

    fn flush(&mut self) {
        while self.bit_count >= 8 {
            self.buffer.push((self.bit_buffer & 0xff) as u8);
            self.bit_buffer >>= 8;
            self.bit_count -= 8;
        }
    }

    fn finish(mut self) -> Vec<u8> {
        self.flush();
        if self.bit_count > 0 {
            self.buffer.push((self.bit_buffer & 0xff) as u8);
        }
        self.buffer
    }
}

/// Little-endian bit reader (LSB-first within each byte).
struct BitReader<'a> {
    bytes: &'a [u8],
    byte_pos: usize,
    bit_pos: u32,
}

impl<'a> BitReader<'a> {
    fn finished(&self) -> bool {
        if self.bit_pos == 0 {
            self.byte_pos == self.bytes.len()
        } else {
            self.byte_pos + 1 == self.bytes.len() && self.bytes[self.byte_pos] >> self.bit_pos == 0
        }
    }

    fn new(bytes: &'a [u8]) -> Self {
        Self {
            bytes,
            byte_pos: 0,
            bit_pos: 0,
        }
    }

    fn read_bit(&mut self) -> Option<bool> {
        let byte = *self.bytes.get(self.byte_pos)?;
        let bit = (byte >> self.bit_pos) & 1 != 0;
        self.bit_pos += 1;
        if self.bit_pos == 8 {
            self.bit_pos = 0;
            self.byte_pos += 1;
        }
        Some(bit)
    }

    fn read_bits(&mut self, count: u32) -> Option<u32> {
        let mut value = 0u32;
        for i in 0..count {
            let bit = self.read_bit()?;
            value |= (bit as u32) << i;
        }
        Some(value)
    }

    fn read_u32(&mut self) -> Option<u32> {
        let a = self.read_bits(8)?;
        let b = self.read_bits(8)?;
        let c = self.read_bits(8)?;
        let d = self.read_bits(8)?;
        Some(a | (b << 8) | (c << 16) | (d << 24))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::format;
    use alloc::vec;

    fn roundtrip(data: &[u8], level: i32) -> Vec<u8> {
        let packed = compress(data, level);
        if packed.is_empty() {
            return data.to_vec();
        }
        decompress(&packed, data.len()).expect("roundtrip")
    }

    #[test]
    fn literals_roundtrip() {
        let data = b"hello world, this is a mostly incompressible sentence!";
        assert_eq!(roundtrip(data, 3), data);
    }

    #[test]
    fn repetitive_data_roundtrip() {
        let data = b"abcabcabcabcabcabcabcabcabcabcabcabc";
        assert_eq!(roundtrip(data, 3), data);
        // Must actually compress.
        let packed = compress(data, 3);
        assert!(!packed.is_empty());
        assert!(packed.len() < data.len());
    }

    #[test]
    fn long_repetition_roundtrip() {
        let data = vec![0xAB_u8; 100_000];
        assert_eq!(roundtrip(&data, 9), data);
        let packed = compress(&data, 9);
        assert!(packed.len() < data.len());
    }

    #[test]
    fn window_boundary_match() {
        // A match exactly at the window boundary (distance ~32768).
        let mut data = vec![0_u8; WINDOW_SIZE];
        data.extend_from_slice(b"marker");
        data.extend_from_slice(&[0_u8; WINDOW_SIZE]);
        data.extend_from_slice(b"marker");
        assert_eq!(roundtrip(&data, 3), data);
    }

    #[test]
    fn mixed_data_roundtrip() {
        let mut data = Vec::new();
        for i in 0..1000 {
            data.extend_from_slice(format!("record-{i:04}-").as_bytes());
            data.extend_from_slice(&[(i & 0xff) as u8; 8]);
            data.push(0x00);
        }
        assert_eq!(roundtrip(&data, 6), data);
    }

    #[test]
    fn empty_and_tiny_inputs() {
        assert_eq!(roundtrip(&[], 3), []);
        assert_eq!(roundtrip(b"a", 3), b"a");
        assert_eq!(roundtrip(b"ab", 3), b"ab");
        // Below MIN_MATCH the encoder refuses to compress (raw fallback).
        assert!(compress(b"ab", 3).is_empty());
    }

    #[test]
    fn malformed_streams_are_rejected() {
        // Truncated header.
        assert_eq!(decompress(&[0x01], 0), Err(LzError::UnexpectedEnd));
        // Declared length exceeds the caller's upper bound.
        let mut frame = Vec::new();
        frame.extend_from_slice(&10u32.to_le_bytes());
        frame.push(0); // literal flag
        frame.push(0x41); // 'A'
        assert_eq!(decompress(&frame, 1), Err(LzError::LengthMismatch));
        // Match before the window start: declared length 3, first token is a
        // match with distance 1 when nothing has been emitted yet. The flag
        // bit is the LSB, so byte 0x01 means "match".
        let mut frame = Vec::new();
        frame.extend_from_slice(&3u32.to_le_bytes());
        frame.push(0x01); // match flag (bit 0 = 1)
        frame.push(0x00); // length - 3 = 0  => length 3
        frame.push(0x00); // distance - 1 low bits
        frame.push(0x00); // distance - 1 high bits (15-bit field spans 2 bytes)
        assert_eq!(decompress(&frame, 3), Err(LzError::InvalidDistance));
    }

    #[test]
    fn level_semantics_are_stable() {
        let data = vec![0x5A_u8; 50_000];
        let low = compress(&data, 1);
        let high = compress(&data, 22);
        assert!(!low.is_empty() && !high.is_empty());
        // Both must round-trip identically.
        assert_eq!(decompress(&low, data.len()).unwrap(), data);
        assert_eq!(decompress(&high, data.len()).unwrap(), data);
    }

    #[test]
    fn matches_cannot_grow_past_the_declared_output() {
        let mut writer = BitWriter::new();
        writer.write_u32(2);
        writer.write_bit(false);
        writer.write_bits(b'A' as u32, 8);
        writer.write_bit(true);
        writer.write_bits(255, 8);
        writer.write_bits(0, 15);
        assert_eq!(
            decompress(&writer.finish(), 2),
            Err(LzError::LengthMismatch)
        );
    }

    #[test]
    fn trailing_bytes_and_nonzero_padding_are_rejected() {
        // One literal A: nine token bits, seven zero padding bits.
        let valid = [1, 0, 0, 0, 0x82, 0];
        assert_eq!(decompress(&valid, 1).unwrap(), b"A");
        let mut extra = valid.to_vec();
        extra.push(0);
        assert_eq!(decompress(&extra, 1), Err(LzError::TrailingData));
        for bit in 1..8 {
            let mut bad = valid;
            bad[5] |= 1 << bit;
            assert_eq!(decompress(&bad, 1), Err(LzError::TrailingData));
        }
        for len in 0..valid.len() {
            assert!(decompress(&valid[..len], 1).is_err());
        }
    }

    #[test]
    fn ring_history_roundtrips_across_repeated_window_wraps() {
        let mut state = 0x1234_5678u32;
        let mut data = Vec::new();
        for index in 0..WINDOW_SIZE * 5 + 259 {
            state ^= state << 13;
            state ^= state >> 17;
            state ^= state << 5;
            data.push(if index > WINDOW_SIZE && index % 97 < 80 {
                data[index - WINDOW_SIZE]
            } else {
                state as u8
            });
        }
        for level in [1, 9, 22] {
            assert_eq!(roundtrip(&data, level), data);
        }
    }
}