Skip to main content

djvu_bzz/
lib.rs

1//! BZZ compressor and decompressor for DjVu documents.
2//!
3//! BZZ combines ZP adaptive arithmetic coding, move-to-front coding, and the
4//! Burrows-Wheeler transform. DjVu uses it for compressed metadata chunks such
5//! as DIRM, NAVM, ANTz, TXTz, and FGbz.
6
7#![cfg_attr(not(feature = "std"), no_std)]
8#![deny(unsafe_code)]
9
10#[cfg(not(feature = "std"))]
11extern crate alloc;
12
13mod decode;
14#[cfg(feature = "std")]
15mod encode;
16
17#[cfg(feature = "parallel")]
18pub use decode::bzz_decode_parallel;
19pub use decode::{bzz_decode, decode};
20#[cfg(feature = "std")]
21pub use encode::bzz_encode;
22
23/// BZZ compression decoding errors.
24#[derive(Debug, thiserror::Error, PartialEq, Eq)]
25pub enum BzzError {
26    /// Input is too short to be a valid BZZ stream (fewer than 2 bytes).
27    #[error("BZZ input is too short")]
28    TooShort,
29
30    /// The block size field in the BZZ stream is invalid or out of range.
31    #[error("BZZ stream contains an invalid block size")]
32    InvalidBlockSize,
33
34    /// The BWT sort index embedded in the stream is out of range.
35    #[error("BZZ stream contains an invalid BWT index")]
36    InvalidBwtIndex,
37
38    /// The ZP arithmetic coder encountered an error.
39    #[error("ZP coder error in BZZ stream")]
40    ZpError,
41
42    /// The BWT block did not contain an end-of-block marker.
43    #[error("BZZ block is missing the end-of-block marker")]
44    MissingMarker,
45
46    /// A single block's decoded size exceeds the safety limit (4 MB).
47    #[error("BZZ block size exceeds maximum allowed ({0} > 4 MB)")]
48    BlockSizeTooLarge(usize),
49
50    /// The total decompressed output exceeds the safety limit (256 MB).
51    #[error("BZZ total output size exceeds maximum allowed (256 MB)")]
52    OutputTooLarge,
53}
54
55/// Map ZP-coder init errors into [`BzzError`] so callers using `?` keep working.
56impl From<djvu_zp::ZpError> for BzzError {
57    fn from(_: djvu_zp::ZpError) -> Self {
58        BzzError::TooShort
59    }
60}