ppmd-core 0.1.0

Pure Rust crate for PPMd compression
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! PPMd-style entropy coder for byte streams.
//!
//! This crate implements a Prediction by Partial Matching (PPM) compressor/decompressor
//! using a range encoder/decoder underneath.  PPM builds adaptive probability models
//! based on the last N bytes of context, where N is the "order."  Higher orders
//! give better compression at the cost of more memory and CPU; lower orders
//! run faster but yield larger output.
//!
//! # Key Parameters
//!
//! - `DEFAULT_ORDER: u8 = 5`  
//!   The default context length (order-5).  A good middle ground between speed and compression.
//! - `MAX_FREQ: u8 = 124`  
//!   Maximum per-symbol frequency in any context.  Caps frequencies to avoid overflow.
//! - `TOP: u32 = 1 << 24` and `BOT: u32 = 1 << 15`  
//!   Thresholds used by the underlying range coder to renormalize its internal registers.
//!   You generally don't need to touch these unless you're tuning the coder itself.

#![forbid(clippy::let_underscore_drop)]
#![forbid(unsafe_code)]
#![warn(clippy::unwrap_used)]
#![warn(missing_docs)]

use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufWriter, Read, Write};
use std::path::Path;
use thiserror::Error as ThisError;

pub const TOP: u32 = 1 << 24;
pub const BOT: u32 = 1 << 15;
pub const MAX_FREQ: u8 = 124;
pub const DEFAULT_ORDER: u8 = 5;

/// A specialized `Result` using [`PpmError`] for errors.
pub type PpmResult<T> = Result<T, PpmError>;

/// The set of errors that can occur during PPM encoding or decoding.
#[derive(ThisError, Debug)]
pub enum PpmError {
    /// Errors from any underlying I/O operations (file, stream, etc.).
    #[error("IO error: {0}")]
    IoError(#[from] io::Error),

    /// The input data was unexpectedly corrupt (e.g., decoder sees an impossible symbol).
    #[error("Corrupt input data")]
    CorruptData,

    /// The decoder was put into an invalid state (should not happen in normal use).
    #[error("Invalid decoder state")]
    InvalidState,

    /// Errors in the PPM model itself (e.g., invalid parameters).
    #[error("Model error: {0}")]
    ModelError(&'static str),
}

/// Streaming range‐encoder for arithmetic coding.
///
/// The encoder maintains three key internal values:
/// - `low`: the low end of the current coding interval  
/// - `range`: the size of the current coding interval  
/// - `buffer`: a byte buffer (flushed in 4 KB chunks) holding the high‐order output bytes
///
/// For each symbol you wish to encode, call [`encode`](#method.encode) with:
/// 1. `cum_freq`: cumulative frequency of all symbols less than the one you’re encoding  
/// 2. `freq`: the frequency of the symbol itself  
/// 3. `tot_freq`: the total of all symbol frequencies in the current context  
///
/// After encoding every symbol, call [`finish`](#method.finish) to flush any remaining bytes
/// and retrieve the underlying writer.
///
/// # Example
///
/// ```no_run
/// use std::fs::File;
/// use ppmd_core::{RangeEncoder, PpmResult};
///
/// fn encode_stream() -> PpmResult<()> {
///     let file = File::create("out.ppm")?;
///     let mut encoder = RangeEncoder::new(file);
///
///     // Suppose `model` yields (cum, freq, tot) triples for each byte:
///     for (cum, freq, tot) in model.symbols() {
///         encoder.encode(cum, freq, tot)?;
///     }
///
///     // Finalize and get back the file writer
///     let _file = encoder.finish()?;
///     Ok(())
/// }
/// ```
pub struct RangeEncoder<W: Write> {
    low: u32,
    range: u32,
    buffer: Vec<u8>,
    writer: W,
}

impl<W: Write> RangeEncoder<W> {
    /// Create a new range encoder wrapping `writer`.
    ///
    /// Initializes:
    /// - `low = 0`  
    /// - `range = u32::MAX` (the full 32-bit interval)  
    /// - an internal 4 KB output buffer  
    ///
    /// The encoder will emit one output byte at a time into `buffer`, flushing
    /// to `writer` whenever the buffer is full.
    pub fn new(writer: W) -> Self {
        Self {
            low: 0,
            range: u32::MAX,
            buffer: Vec::with_capacity(4096),
            writer,
        }
    }

    fn encode(&mut self, cum_freq: u32, freq: u32, tot_freq: u32) -> PpmResult<()> {
        assert!(tot_freq > 0, "total frequency must be positive");
        assert!(freq > 0, "symbol frequency must be positive");
        assert!(cum_freq < tot_freq, "cumulative freq out of range");
        assert!(cum_freq + freq <= tot_freq, "freq interval exceeds total");

        self.range /= tot_freq;
        self.low = self.low.wrapping_add(cum_freq * self.range);
        self.range = self.range.wrapping_mul(freq);

        while (self.low ^ (self.low.wrapping_add(self.range))) < TOP || self.range < BOT {
            if self.range < BOT {
                self.range = (-(self.low as i32) as u32) & (BOT - 1);
            }
            self.buffer.push((self.low >> 24) as u8);
            self.low <<= 8;
            self.range <<= 8;
            if self.buffer.len() >= 4096 {
                self.writer.write_all(&self.buffer)?;
                self.buffer.clear();
            }
        }
        Ok(())
    }

    fn finish(mut self) -> PpmResult<W> {
        assert!(self.range > 0, "range became zero in finish");
        for _ in 0..4 {
            self.buffer.push((self.low >> 24) as u8);
            self.low <<= 8;
        }
        assert!(!self.buffer.is_empty(), "nothing to flush in finish");
        self.writer.write_all(&self.buffer)?;
        Ok(self.writer)
    }
}

/// Streaming range‐decoder for arithmetic coding.
///
/// The decoder maintains three internal registers:
/// - `low`: the low end of the current coding interval  
/// - `code`: the buffered input bits read from the stream  
/// - `range`: the size of the current coding interval  
///
/// On each symbol decode, you first call [`get_freq`] to map the current code
/// to a frequency within the total frequency range, then call [`decode`]
/// to narrow the interval and consume bits as needed.
///
/// # Example
///
/// ```no_run
/// use std::fs::File;
/// use ppmd_core::{RangeDecoder, PpmModel, PpmResult};
///
/// fn decode_stream() -> PpmResult<()> {
///     let file = File::open("data.ppm")?;
///     let mut decoder = RangeDecoder::new(file)?;
///     let mut model = PpmModel::new(5)?;
///     let mut history = Vec::new();
///     let mut out_byte = [0u8; 1];
///
///     // Repeatedly call `decode_symbol` until end of stream
///     while model.decode_symbol(&mut decoder, &mut history, &mut out_byte).is_ok() {
///         print!("{}", out_byte[0] as char);
///     }
///     Ok(())
/// }
/// ```
pub struct RangeDecoder<R: Read> {
    low: u32,
    code: u32,
    range: u32,
    reader: R,
    buffer: [u8; 1],
}

impl<R: Read> RangeDecoder<R> {
    /// Initialize a new `RangeDecoder` by reading the first 4 bytes
    /// from `reader` into the internal `code` register.
    ///
    /// The range decoder uses these first 32 bits as its starting buffer.
    /// Subsequent calls to [`get_freq`] and [`decode`] will consume more
    /// bytes from `reader` as needed to renormalize the interval.
    ///
    /// # Errors
    ///
    /// Returns `PpmError::IoError` if reading the initial 4-byte code prefix fails.
    pub fn new(mut reader: R) -> PpmResult<Self> {
        let mut code = 0;
        for _ in 0..4 {
            let mut byte = [0];
            reader.read_exact(&mut byte)?;
            code = (code << 8) | u32::from(byte[0]);
        }
        Ok(Self {
            low: 0,
            code,
            range: u32::MAX,
            reader,
            buffer: [0],
        })
    }

    fn get_freq(&mut self, tot_freq: u32) -> PpmResult<u32> {
        assert!(tot_freq > 0, "total frequency must be positive");
        self.range /= tot_freq;
        let tmp = (self.code.wrapping_sub(self.low)) / self.range;
        if tmp >= tot_freq {
            return Err(PpmError::CorruptData);
        }
        Ok(tmp)
    }

    fn decode(&mut self, cum_freq: u32, freq: u32, tot_freq: u32) -> PpmResult<()> {
        assert!(freq > 0, "frequency must be positive");
        assert!(cum_freq < tot_freq, "cumulative freq out of range");
        assert!(cum_freq + freq <= tot_freq, "freq interval exceeds total");

        if cum_freq.wrapping_add(freq) > tot_freq {
            return Err(PpmError::CorruptData);
        }
        self.low = self.low.wrapping_add(cum_freq * self.range);
        self.range = self.range.wrapping_mul(freq);
        while (self.low ^ (self.low.wrapping_add(self.range))) < TOP || self.range < BOT {
            if self.range < BOT {
                self.range = (-(self.low as i32) as u32) & (BOT - 1);
            }
            self.reader.read_exact(&mut self.buffer)?;
            self.code = (self.code << 8) | u32::from(self.buffer[0]);
            self.low <<= 8;
            self.range <<= 8;
        }
        Ok(())
    }
}

#[derive(Debug, Clone)]
struct State {
    symbol: u8,
    freq: u8,
}

#[derive(Clone, Debug)]
struct PpmContext {
    stats: Vec<State>,
    total_freq: u32,
}

impl PpmContext {
    fn new() -> Self {
        PpmContext {
            stats: Vec::new(),
            total_freq: 0,
        }
    }

    /// PPMII "information inheritance":
    /// copy each parent frequency as max(1, parent.freq/2)
    fn inherit_from(&mut self, parent: &PpmContext) {
        // parents stats should be non‐empty if called in contexts
        assert!(parent.stats.len() > 0, "parent context has no stats");

        self.stats.clear();
        for st in &parent.stats {
            let f = (st.freq / 2).max(1);
            assert!(f >= 1, "inherited frequency dropped below 1");
            self.stats.push(State {
                symbol: st.symbol,
                freq: f,
            });
        }
        self.total_freq = self.stats.iter().map(|s| s.freq as u32).sum();
        assert!(
            self.total_freq > 0,
            "total_freq must be positive after inherit"
        );
    }

    /// PPMD escape probabilities:
    ///   symbol fᵢ = 2·cᵢ − 1
    ///   escape = q  (number of distinct symbols)
    ///   tot    = 2·C
    fn get_cumulative(&self) -> (Vec<u8>, Vec<u32>, u32, u32) {
        let c: u32 = self.stats.iter().map(|s| s.freq as u32).sum();
        let q = self.stats.len() as u32;
        let tot = 2 * c;
        assert!(tot > 0, "total (2*C) must be positive");

        let mut syms = Vec::with_capacity(self.stats.len());
        let mut freqs = Vec::with_capacity(self.stats.len());
        for st in &self.stats {
            let f = (st.freq as u32) * 2 - 1;
            assert!(f > 0, "computed symbol frequency must be positive");
            syms.push(st.symbol);
            freqs.push(f);
        }
        (syms, freqs, q, tot)
    }

    /// Lazy exclusion: bump only the first (highest-order) context
    /// that actually contained the symbol.
    fn update_exclusion(&mut self, symbol: u8) {
        let before = self.total_freq;
        if let Some(st) = self.stats.iter_mut().find(|s| s.symbol == symbol) {
            let new_freq = st.freq.saturating_add(1).min(MAX_FREQ);
            assert!(new_freq >= st.freq, "freq must not decrease on bump");

            st.freq = new_freq;
            self.total_freq = self.stats.iter().map(|s| s.freq as u32).sum();
            assert!(
                self.total_freq >= before,
                "total_freq must not shrink after update"
            );
        }
    }
}

/// The central PPM model.  Maintains up to `max_order` contexts
/// and dynamically updates symbol frequencies as you encode or decode.
///
/// Higher `max_order` (e.g. `Some(8)`) means the model looks at up to 8 previous
/// bytes for each prediction:  
/// - **Pros**: Better predictions and higher compression ratio  
/// - **Cons**: More memory and CPU overhead  
///
/// Lower `max_order` (e.g. `None` → default order 5) is faster and lighter,
/// but compresses less effectively.
///
/// # Examples
///
/// ```no_run
/// use ppmd_core::{encode_file, decode_file, PpmResult};
///
/// fn main() -> PpmResult<()> {
///     // Use default order = 5
///     encode_file("input.bin", "out.ppm", None)?;
///
///     // Use a custom order = 8 for potentially better compression
///     encode_file("input.bin", "out8.ppm", Some(8))?;
///
///     // Decode (always uses order 5)
///     decode_file("out.ppm", "decoded.bin")?;
///     Ok(())
/// }
/// ```
pub struct PpmModel {
    max_order: u8,
    contexts: HashMap<Vec<u8>, PpmContext>,
}

impl PpmModel {
    /// Create a new PPM model with contexts up to `max_order` (1..=16).
    ///
    /// # Panics
    ///
    /// Panics if `max_order == 0` or `max_order > 16`.
    pub fn new(max_order: u8) -> PpmResult<Self> {
        assert!(
            max_order > 0 && max_order <= 16,
            "max_order out of valid range"
        );
        let mut m = PpmModel {
            max_order,
            contexts: HashMap::new(),
        };
        // build the order−1 root context with uniform order‑0 stats
        let mut root = PpmContext::new();
        for sym in 0u8..=255 {
            root.stats.push(State {
                symbol: sym,
                freq: 1,
            });
        }
        root.total_freq = 256;
        assert_eq!(root.stats.len(), 256, "root must contain all 256 symbols");
        m.contexts.insert(Vec::new(), root);
        Ok(m)
    }

    /// Encode the entire contents of `input` into `output`, updating the model
    /// adaptively as you go.
    pub fn encode<R: Read, W: Write>(&mut self, mut input: R, output: W) -> PpmResult<W> {
        let mut encoder = RangeEncoder::new(output);
        let mut history = Vec::new();
        let mut buf = [0u8; 1];

        while input.read(&mut buf)? > 0 {
            let sym = buf[0];
            self.encode_symbol(&mut encoder, &history, sym)?;
            self.update_model(&mut history, sym)?;
        }
        encoder.finish()
    }

    fn encode_symbol<W: Write>(
        &self,
        encoder: &mut RangeEncoder<W>,
        history: &[u8],
        symbol: u8,
    ) -> PpmResult<()> {
        assert!(history.len() <= self.max_order as usize, "history too long");

        // back‑off from highest order down to order−1:
        for order in (1..=self.max_order.min(history.len() as u8)).rev() {
            let key = history[history.len() - order as usize..].to_vec();
            if let Some(ctx) = self.contexts.get(&key) {
                let (syms, freqs, esc, tot) = ctx.get_cumulative();
                let mut cum = 0;
                // if symbol found in this context, emit it
                for (i, &s) in syms.iter().enumerate() {
                    if s == symbol {
                        return encoder.encode(cum, freqs[i], tot);
                    }
                    cum += freqs[i];
                }
                // otherwise emit escape
                encoder.encode(cum, esc, tot)?;
            }
        }
        // final fallback at order−1 root (uniform)
        let root = &self.contexts[&Vec::new()];
        let tot0 = (root.stats.len() as u32) + 1;
        if let Some(pos) = root.stats.iter().position(|s| s.symbol == symbol) {
            encoder.encode(pos as u32, 1, tot0)
        } else {
            // one final escape (should never really happen)
            encoder.encode(root.stats.len() as u32, 1, tot0)
        }
    }

    /// After emitting symbol, update ALL contexts up to max_order
    fn update_model(&mut self, history: &mut Vec<u8>, symbol: u8) -> PpmResult<()> {
        let before = self.contexts.len();
        // lazy‐exclusion update on the longest suffix that contained the symbol
        let mut bumped = false;
        for i in 0..history.len() {
            let key = history[i..].to_vec();
            if let Some(ctx) = self.contexts.get_mut(&key) {
                if !bumped {
                    ctx.update_exclusion(symbol);
                    bumped = ctx.stats.iter().any(|s| s.symbol == symbol);
                }
            }
        }
        // if it never existed, add to the root order‑0
        if !bumped {
            let root = self.contexts.get_mut(&Vec::new()).unwrap();
            root.stats.push(State { symbol, freq: 1 });
            root.total_freq += 1;
        }

        // Slide the history window
        history.push(symbol);
        if history.len() > self.max_order as usize {
            history.remove(0);
        }
        assert!(self.contexts.len() >= before, "contexts should not shrink");

        // Create or inherit every missing context suffix up to max_order
        let current_len = history.len();
        let max_ctx = self.max_order.min(current_len as u8) as usize;
        for order in 1..=max_ctx {
            let key = history[current_len - order..].to_vec();
            if !self.contexts.contains_key(&key) {
                // build from the (order−1) parent:
                let parent_key = if key.len() > 1 {
                    key[1..].to_vec()
                } else {
                    Vec::new()
                };
                let mut ctx = PpmContext::new();
                if let Some(parent) = self.contexts.get(&parent_key) {
                    ctx.inherit_from(parent);
                }
                self.contexts.insert(key, ctx);
            }
        }

        assert!(
            history.len() <= self.max_order as usize,
            "history exceeded max_order"
        );
        Ok(())
    }

    /// Decode one symbol at a time from `decoder`, writing to `out`,
    /// and update the model adaptively.
    ///
    /// This is used by `decode_file` under the hood.
    pub fn decode_symbol<R: Read>(
        &mut self,
        decoder: &mut RangeDecoder<R>,
        history: &mut Vec<u8>,
        out: &mut [u8],
    ) -> PpmResult<()> {
        assert!(out.len() == 1, "output buffer must be exactly one byte");
        // back‑off decode:
        for order in (1..=self.max_order.min(history.len() as u8)).rev() {
            let key = history[history.len() - order as usize..].to_vec();
            if let Some(ctx) = self.contexts.get(&key) {
                let (syms, freqs, esc, tot) = ctx.get_cumulative();
                let threshold = tot.saturating_sub(esc);
                let r = decoder.get_freq(tot)?;
                if r < threshold {
                    // actual symbol
                    let mut cum = 0;
                    for (i, &f) in freqs.iter().enumerate() {
                        if r < cum + f {
                            let sym = syms[i];
                            decoder.decode(cum, f, tot)?;
                            out[0] = sym;
                            self.update_model(history, sym)?;
                            return Ok(());
                        }
                        cum += f;
                    }
                    unreachable!();
                } else {
                    // escape
                    decoder.decode(threshold, esc, tot)?;
                }
            }
        }

        // root fallback:
        let root = &self.contexts[&Vec::new()];
        let tot0 = (root.stats.len() as u32) + 1;
        let r0 = decoder.get_freq(tot0)?;
        if r0 < root.stats.len() as u32 {
            let sym = root.stats[r0 as usize].symbol;
            decoder.decode(r0, 1, tot0)?;
            out[0] = sym;
            self.update_model(history, sym)?;
            Ok(())
        } else {
            // end of stream
            decoder.decode(root.stats.len() as u32, 1, tot0)?;
            Err(PpmError::CorruptData)
        }
    }
}

/// Compress the file at `input_path` into `output_path` using PPM.
///  
/// - `max_order = None` ⇒ uses the crate’s `DEFAULT_ORDER` (5).  
/// - `max_order = Some(n)` ⇒ uses order n (up to 16).  
///
/// By default, we first write an 8-byte little-endian prefix giving the
/// original file length, then the PPM‐encoded payload.
///
/// # Errors
///
/// Returns an error if any I/O or encoding step fails.
pub fn encode_file<P: AsRef<Path>, Q: AsRef<Path>>(
    input_path: P,
    output_path: Q,
    max_order: Option<usize>,
) -> PpmResult<()> {
    let input_path = input_path.as_ref();
    let output_path = output_path.as_ref();

    // Step 1: determine original input length
    let input_file = File::open(input_path)?;
    let input_len = input_file.metadata()?.len();

    // Step 2: open output and write length prefix
    let mut output = File::create(output_path)?;
    output.write_all(&input_len.to_le_bytes())?;

    // Step 3: re-open input for reading (we already used it for metadata)
    let mut input = File::open(input_path)?;

    // Step 4: encode using PPM
    let order = max_order.unwrap_or(DEFAULT_ORDER as usize);
    let mut model = PpmModel::new(order.try_into().unwrap())?;
    model.encode(&mut input, &mut output)?;

    Ok(())
}

/// Decompress `input_path` (which must have been produced by `encode_file`)
/// back into `output_path`, using the default `DEFAULT_ORDER = 5`.
///
/// Reads the 8-byte length prefix, then decodes exactly that many bytes
/// via the range decoder + PPM model.
///
/// # Errors
///
/// Returns an error if any I/O or decoding step fails, or if the input
/// is corrupt.
pub fn decode_file<P: AsRef<Path>, Q: AsRef<Path>>(input_path: P, output_path: Q) -> PpmResult<()> {
    let input_path = input_path.as_ref();
    let output_path = output_path.as_ref();

    let mut input = File::open(input_path)?;
    let mut len_buf = [0u8; 8];
    input.read_exact(&mut len_buf)?;
    let expected = u64::from_le_bytes(len_buf);

    let mut decoder = RangeDecoder::new(input)?;
    let mut model = PpmModel::new(DEFAULT_ORDER)?;
    let mut history = Vec::new();
    let mut writer = BufWriter::new(File::create(output_path)?);

    let mut buf = [0u8; 1];
    let mut actual = 0;
    while actual < expected {
        model.decode_symbol(&mut decoder, &mut history, &mut buf)?;
        writer.write_all(&buf)?;
        actual += 1;
    }

    Ok(())
}