Skip to main content

jbig2_decoder/
lib.rs

1#![forbid(unsafe_code)]
2#![cfg_attr(verus_keep_ghost, feature(proc_macro_hygiene))]
3//! A dependency-free, safe (zero-unsafe), Verus-verified Rust decoder
4//! for JBIG2 black-and-white pages.
5//!
6//! **Production status:** ready for integration into consumer products (including
7//! untrusted standalone and embedded PDF JBIG2 streams) under explicit [`Limits`].
8//! Implemented features are not an Annex F conformance certificate, encoding is
9//! out of scope, and standard-library/allocator/callback trust boundaries remain
10//! explicit. See the repository's verification limitations and threat model for
11//! full details.
12//!
13//! # Decode a standalone file
14//!
15//! [`Decoder`] provides the ordinary Rust API; neither Verus nor the repository's
16//! proof files are needed to build or use it. Reader entry points apply the
17//! decoder's configured input limit while reading.
18//!
19//! ```no_run
20//! use std::fs::File;
21//! use jbig2_decoder::{Decoder, Limits};
22//!
23//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
24//! let decoder = Decoder::new(Limits::default());
25//! let mut input = File::open("document.jb2")?;
26//! let document = decoder.decode_reader(&mut input)?;
27//!
28//! for page in document.pages() {
29//!     let bitmap = page.bitmap();
30//!     println!("page {}: {} x {}", page.number(), bitmap.width(), bitmap.height());
31//! }
32//! # Ok(())
33//! # }
34//! ```
35//!
36//! # Choose the right entry point
37//!
38//! - [`Decoder`] inspects or decodes standalone documents from bytes or a reader.
39//! - [`DecoderContext`] reuses scratch storage across calls; it can release that
40//!   storage explicitly when reuse is no longer needed.
41//! - [`GlobalSegments`] supports embedded JBIG2 streams with reusable globals,
42//!   as used by PDF containers. This crate does not parse PDF documents.
43//! - [`PageSink`] receives completed pages without requiring the caller to
44//!   collect every decoded page in one [`DecodedDocument`]. Internal reference
45//!   retention still follows the stream and configured limits.
46//!
47//! # Pixels, limits and errors
48//!
49//! [`Bitmap`] stores packed, row-major, one-bit pixels, with the leftmost pixel
50//! in the most significant bit. Its stride is measured in bytes, not pixels.
51//! Use [`Bitmap::pixel`] for bounds-checked access or [`Bitmap::bytes`] together
52//! with the dimensions and stride for bulk processing.
53//!
54//! [`Limits`] is the place to set application-specific input, image, memory and
55//! work policies. These policies are not an operating-system process-memory
56//! limit and do not replace the crate's remaining release/safety review.
57//! [`DecodeError`] distinguishes malformed or unsupported input, configured
58//! limits, allocation failures and I/O errors; callers should handle those
59//! explicitly rather than treating a rejected document as an empty image.
60//!
61//! # Implementation and verification
62//!
63//! The same Rust implementation is built by ordinary Cargo and by the pinned
64//! verification toolchain. Detailed models and proof helpers live separately
65//! in the repository; small `verus_keep_ghost` annotations attach them to the
66//! implementation and are inactive in ordinary builds.
67
68#[cfg(verus_keep_ghost)]
69extern crate vstd;
70
71#[cfg(verus_keep_ghost)]
72#[rustfmt::skip]
73#[path = "../specs/mod.rs"]
74mod spec;
75
76#[cfg(verus_keep_ghost)]
77#[macro_use]
78#[rustfmt::skip]
79#[path = "../proofs/production_impl/readers.rs"]
80mod reader_proofs;
81
82#[cfg(verus_keep_ghost)]
83#[macro_use]
84#[rustfmt::skip]
85#[path = "../proofs/production_impl/file_header.rs"]
86mod file_header_proofs;
87
88#[cfg(verus_keep_ghost)]
89#[macro_use]
90#[rustfmt::skip]
91#[path = "../proofs/production_impl/bitmap.rs"]
92mod bitmap_proofs;
93
94#[cfg(verus_keep_ghost)]
95#[macro_use]
96#[rustfmt::skip]
97#[path = "../proofs/production_impl/document.rs"]
98mod document_proofs;
99
100#[cfg(verus_keep_ghost)]
101#[macro_use]
102#[rustfmt::skip]
103#[path = "../proofs/production_impl/decode.rs"]
104mod decode_proofs;
105
106#[allow(dead_code)]
107mod arithmetic;
108mod bitmap;
109mod colour;
110mod decode;
111mod document;
112mod error;
113mod file_header;
114#[allow(dead_code)]
115mod generic_region;
116#[allow(dead_code)]
117mod halftone;
118#[allow(dead_code)]
119mod huffman;
120mod limits;
121#[allow(dead_code)]
122mod mmr;
123mod reader;
124#[allow(dead_code)]
125mod segment_data;
126#[allow(dead_code)]
127mod segment_header;
128#[allow(dead_code)]
129mod symbol_text;
130
131pub use bitmap::Bitmap;
132pub use colour::{ColourPage, DecodedColourDocument, RgbBitmap};
133pub use decode::{DecodedDocument, Page, PageSink};
134pub use document::embedded::{
135    EmbeddedPolicy, EmbeddedRecovery, EmbeddedRecoveryKind, EmbeddedResult,
136};
137pub use document::standalone::{DecodePolicy, DecodeResult, Recovery, RecoveryKind};
138pub use document::{Decoder, DecoderContext, DocumentInfo, GlobalSegments, PageInfo};
139pub use error::{
140    DecodeError, GenericRegionErrorKind, HalftoneErrorKind, HuffmanErrorKind, IoErrorKind,
141    LimitExceeded, LimitKind, MalformedError, MmrErrorKind, UnsupportedFeature,
142};
143pub use file_header::{inspect_file_header, FileHeader, Organization};
144pub use limits::Limits;
145
146#[cfg(any(fuzzing, jbig2_validation))]
147#[path = "../validation/runtime_access.rs"]
148pub mod runtime_access;
149
150#[cfg(verus_keep_ghost)]
151#[rustfmt::skip]
152#[path = "../proofs/shared/views.rs"]
153mod views;
154
155#[cfg(verus_keep_ghost)]
156#[rustfmt::skip]
157#[path = "../proofs/shared/invariants.rs"]
158mod invariants;
159
160#[cfg(verus_keep_ghost)]
161#[rustfmt::skip]
162#[path = "../proofs/spec_properties/readers.rs"]
163mod reader_properties;
164
165#[cfg(verus_keep_ghost)]
166extern crate jbig2_capacity_interface;