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