Skip to main content

Crate jbig2_decoder

Crate jbig2_decoder 

Source
Expand description

A dependency-free, safe (zero-unsafe), Verus-verified Rust decoder for JBIG2 black-and-white pages.

Production status: ready for integration into consumer products (including untrusted standalone and embedded PDF JBIG2 streams) under explicit Limits. Implemented features are not an Annex F conformance certificate, encoding is out of scope, and standard-library/allocator/callback trust boundaries remain explicit. See the repository’s verification limitations and threat model for full details.

§Decode a standalone file

Decoder provides the ordinary Rust API; neither Verus nor the repository’s proof files are needed to build or use it. Reader entry points apply the decoder’s configured input limit while reading.

use std::fs::File;
use jbig2_decoder::{Decoder, Limits};

let decoder = Decoder::new(Limits::default());
let mut input = File::open("document.jb2")?;
let document = decoder.decode_reader(&mut input)?;

for page in document.pages() {
    let bitmap = page.bitmap();
    println!("page {}: {} x {}", page.number(), bitmap.width(), bitmap.height());
}

§Choose the right entry point

  • Decoder inspects or decodes standalone documents from bytes or a reader.
  • DecoderContext reuses scratch storage across calls; it can release that storage explicitly when reuse is no longer needed.
  • GlobalSegments supports embedded JBIG2 streams with reusable globals, as used by PDF containers. This crate does not parse PDF documents.
  • PageSink receives completed pages without requiring the caller to collect every decoded page in one DecodedDocument. Internal reference retention still follows the stream and configured limits.

§Pixels, limits and errors

Bitmap stores packed, row-major, one-bit pixels, with the leftmost pixel in the most significant bit. Its stride is measured in bytes, not pixels. Use Bitmap::pixel for bounds-checked access or Bitmap::bytes together with the dimensions and stride for bulk processing.

Limits is the place to set application-specific input, image, memory and work policies. These policies are not an operating-system process-memory limit and do not replace the crate’s remaining release/safety review. DecodeError distinguishes malformed or unsupported input, configured limits, allocation failures and I/O errors; callers should handle those explicitly rather than treating a rejected document as an empty image.

§Implementation and verification

The same Rust implementation is built by ordinary Cargo and by the pinned verification toolchain. Detailed models and proof helpers live separately in the repository; small verus_keep_ghost annotations attach them to the implementation and are inactive in ordinary builds.

Structs§

Bitmap
An owned row-major, one-bit-per-pixel bitmap.
ColourPage
One completely decoded, opaque RGB page.
DecodeResult
Completely decoded output with every compatibility exception reported.
DecodedColourDocument
Completely decoded pages in source page-information order.
DecodedDocument
Fully collected decoded document.
Decoder
High-level decoder configuration.
DecoderContext
Reusable decoding state for repeated documents with one Decoder.
DocumentInfo
Validated document-level metadata.
EmbeddedRecovery
One allowlisted metadata or complete-marker override.
EmbeddedResult
Successful embedded output and explicit recovery diagnostics.
FileHeader
Parsed standalone JBIG2 file-header metadata.
GlobalSegments
Immutable validated metadata for reusable embedded global segments.
LimitExceeded
Details of a resource-limit failure.
Limits
Explicit resource limits for untrusted JBIG2 input.
Page
One fully decoded page.
PageInfo
Public page metadata established by page and stripe boundary segments.
Recovery
A recovery location in the original input, or page closure at its boundary.
RgbBitmap
Packed, row-major, opaque RGB8 pixels, in red/green/blue channel order.

Enums§

DecodeError
Error returned by parsing and decoding operations.
DecodePolicy
Validation policy for standalone files. Existing entry points remain strict.
EmbeddedPolicy
Caller-selected policy for embedded JBIG2 streams. Existing APIs stay strict.
EmbeddedRecoveryKind
A metadata inconsistency explicitly recovered by the embedded policy.
GenericRegionErrorKind
Detailed malformed generic or refinement region classification.
HalftoneErrorKind
Detailed malformed pattern-dictionary or halftone classification.
HuffmanErrorKind
Detailed malformed Huffman table or code classification.
IoErrorKind
Stable deterministic I/O failure classification.
LimitKind
The resource whose configured limit was exceeded.
MalformedError
A deterministic classification of malformed input.
MmrErrorKind
Detailed malformed MMR stream classification.
Organization
Segment and payload organization for a standalone JBIG2 file.
RecoveryKind
An explicit standalone compatibility exception, not partial-output decoding.
UnsupportedFeature
A feature that is valid JBIG2 syntax but outside the supported profile.

Traits§

PageSink
Streaming destination for completed pages.

Functions§

inspect_file_header
Parses a standalone JBIG2 file header without decoding its segments.