jbig2-decoder 0.1.2

Memory-safe JBIG2 decoder under development
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
//! Arithmetic implementation.

#[cfg(verus_keep_ghost)]
use super::*;
#[cfg(not(verus_keep_ghost))]
use super::{
    assemble_exported_symbols, check_dimensions, decode_checked_symbol_id, decode_export_runs,
    decode_text_region_body, generic_template_from_u8, new_arithmetic_codec,
    normalized_bounded_arithmetic, refinement_template_from_u8, required_integer, symbol_by_id,
    validate_bounded_arithmetic_marker, ArithmeticDictionaryResult, Bitmap, CombinationOperator,
    DecodeError, DictionaryClassState, DictionaryContexts, EntropyInteger, GenericArithmeticCall,
    GenericContexts, IntegerField, Limits, MalformedError, ReferenceCorner, RefinementGeometry,
    RefinementRequest, ResourceTracker, SymbolCodebook, SymbolCodec, SymbolDictionaryHeader,
    SymbolIdRequest, SymbolTextError, TemplateOffset, TextRegionConfig,
};

/// Decodes one T.88 6.5.8.2.2 single-instance refinement/aggregate symbol
/// bitmap, shared by the arithmetic and Huffman symbol-dictionary decoders.
///
/// Unlike the text-region per-instance refinement procedure (handled by the
/// existing [`decode_refined_bitmap`] helper), this procedure never decodes a
/// width or height delta: the symbol's dimensions were already fixed by the
/// enclosing height-class loop, so only the symbol ID and reference offset
/// are decoded before invoking the shared generic-refinement callback.
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
#[cfg_attr(verus_keep_ghost, verus_spec(out =>
    requires
        symbol_text_spec::codec_valid(old(codec)),
        symbol_text_spec::codebook_valid(imported),
        symbol_text_spec::bitmaps_valid(new_symbols@),
    ensures match out {
        Ok(bitmap) =>
            symbol_text_spec::codec_valid(final(codec))
                && crate::spec::bitmap::storage_valid(&bitmap)
                && crate::views::bitmap_width(&bitmap) == width
                && crate::views::bitmap_height(&bitmap) == height
                && symbol_text_spec::work_units(final(resources))
                    == symbol_text_spec::work_units(old(resources)),
        Err(_) => true,
    },
))]
pub(in crate::symbol_text) fn decode_dictionary_single_instance_refinement(
    codec: &mut SymbolCodec<'_>,
    imported: SymbolCodebook<'_>,
    new_symbols: &[Bitmap],
    code_length: u8,
    width: u32,
    height: u32,
    refinement_template: u8,
    refinement_adaptive: [(i8, i8); 2],
    huffman: bool,
    limits: &Limits,
    resources: &mut ResourceTracker,
) -> Result<Bitmap, SymbolTextError> {
    let available = imported
        .len()
        .checked_add(new_symbols.len())
        .and_then(|count| u32::try_from(count).ok())
        .ok_or(MalformedError::IntegerOverflow)?;
    let id = decode_checked_symbol_id(
        codec,
        SymbolIdRequest {
            available_symbols: available,
            code_length,
            huffman,
        },
    )?;
    let reference_x = required_integer(codec, IntegerField::RefinementX)?;
    let reference_y = required_integer(codec, IntegerField::RefinementY)?;
    let declared_size = if huffman {
        let size = required_integer(codec, IntegerField::RefinementSize)?;
        Some(
            u32::try_from(size)
                .map_err(|_| SymbolTextError::InvalidEntropyValue("refinement size"))?,
        )
    } else {
        None
    };
    let reference = symbol_by_id(imported, new_symbols, id)?;
    check_dimensions(width, height, limits)?;
    resources.charge_bitmap(width, height, limits)?;
    let geometry = RefinementGeometry {
        width,
        height,
        reference_dx: i32::try_from(reference_x).map_err(|_| MalformedError::IntegerOverflow)?,
        reference_dy: i32::try_from(reference_y).map_err(|_| MalformedError::IntegerOverflow)?,
    };
    let request = RefinementRequest {
        reference,
        geometry,
        template: refinement_template,
        adaptive_pixels: refinement_adaptive,
        declared_data_size: declared_size,
    };
    let bitmap = codec.decode_refinement(request, limits)?;
    if bitmap.width() != width || bitmap.height() != height {
        return Err(SymbolTextError::InvalidDimensions);
    }
    Ok(bitmap)
}

#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
#[cfg_attr(verus_keep_ghost, verus_spec(out =>
    requires
        symbol_text_spec::codec_valid(old(codec)),
        symbol_text_spec::codebook_valid(imported),
        symbol_text_spec::bitmaps_valid(new_symbols@),
        symbol_text_spec::work_units(old(resources)) <= limits.max_work_units,
        header.adaptive_count <= 4,
    ensures match out {
        Ok(bitmap) =>
            symbol_text_spec::codec_valid(final(codec))
                && crate::spec::bitmap::storage_valid(&bitmap)
                && crate::views::bitmap_width(&bitmap) == width
                && crate::views::bitmap_height(&bitmap) == height
                && symbol_text_spec::work_units(final(resources))
                    <= limits.max_work_units
                && symbol_text_spec::work_units(final(resources))
                    >= symbol_text_spec::work_units(old(resources)),
        Err(_) => true,
    },
))]
pub(in crate::symbol_text) fn decode_one_new_symbol(
    header: &SymbolDictionaryHeader,
    codec: &mut SymbolCodec<'_>,
    imported: SymbolCodebook<'_>,
    new_symbols: &[Bitmap],
    generic_contexts: Option<&mut GenericContexts>,
    generic_work: &mut u64,
    aggregation_instances: Option<u32>,
    width: u32,
    height: u32,
    code_length: u8,
    refinement_adaptive: [(i8, i8); 2],
    limits: &Limits,
    resources: &mut ResourceTracker,
) -> Result<Bitmap, SymbolTextError> {
    let config = header.config;
    if config.refinement_aggregation {
        let instances = aggregation_instances.ok_or(SymbolTextError::InvalidState(
            "missing aggregation-instance count",
        ))?;
        if instances == 0 {
            return Err(SymbolTextError::InvalidCount(
                "zero-instance aggregate symbol",
            ));
        }
        if instances > 1 {
            let capacity = imported
                .len()
                .checked_add(new_symbols.len())
                .ok_or(MalformedError::IntegerOverflow)?;
            let requested_bytes = capacity
                .checked_mul(core::mem::size_of::<&Bitmap>())
                .and_then(|bytes| u64::try_from(bytes).ok())
                .ok_or(MalformedError::IntegerOverflow)?;
            let mut symbol_refs: Vec<&Bitmap> = Vec::new();
            symbol_refs
                .try_reserve(capacity)
                .map_err(|_| DecodeError::Allocation { requested_bytes })?;
            let mut imported_index = 0_usize;
            #[cfg_attr(verus_keep_ghost, verus_spec(
                invariant
                    imported_index <= symbol_text_spec::codebook_len(imported),
                    symbol_refs@.len() == imported_index,
                    symbol_text_spec::codebook_valid(imported),
                    symbol_text_spec::bitmap_refs_valid(symbol_refs@),
                decreases symbol_text_spec::codebook_len(imported) - imported_index,
            ))]
            while imported_index < imported.len() {
                symbol_refs.push(imported.get(imported_index).ok_or(
                    SymbolTextError::InvalidState("missing imported aggregate symbol"),
                )?);
                imported_index += 1;
            }
            let mut new_index = 0_usize;
            #[cfg_attr(verus_keep_ghost, verus_spec(
                invariant
                    new_index <= new_symbols@.len(),
                    symbol_refs@.len()
                        == symbol_text_spec::codebook_len(imported) + new_index,
                    symbol_text_spec::bitmaps_valid(new_symbols@),
                    symbol_text_spec::bitmap_refs_valid(symbol_refs@),
                decreases new_symbols@.len() - new_index,
            ))]
            while new_index < new_symbols.len() {
                symbol_refs.push(&new_symbols[new_index]);
                new_index += 1;
            }
            let nested_config = TextRegionConfig {
                huffman: config.huffman,
                refinement: true,
                strip_size: 1,
                reference_corner: ReferenceCorner::TopLeft,
                transposed: false,
                operator: CombinationOperator::Or,
                default_pixel: false,
                s_offset: 0,
                refinement_template: config.refinement_template,
                huffman_choices: None,
            };
            let work_before = resources.work_units;
            let bitmap = decode_text_region_body(
                codec,
                SymbolCodebook::Borrowed(&symbol_refs),
                code_length,
                width,
                height,
                instances,
                &nested_config,
                refinement_adaptive,
                limits,
                resources,
            )?;
            if resources.work_units < work_before {
                return Err(SymbolTextError::InvalidState(
                    "resource work counter decreased",
                ));
            }
            Ok(bitmap)
        } else {
            decode_dictionary_single_instance_refinement(
                codec,
                imported,
                new_symbols,
                code_length,
                width,
                height,
                config.refinement_template,
                refinement_adaptive,
                config.huffman,
                limits,
                resources,
            )
        }
    } else {
        if aggregation_instances.is_some() {
            return Err(SymbolTextError::InvalidState(
                "aggregation count on direct-coded symbol",
            ));
        }
        resources.charge_bitmap(width, height, limits)?;
        let contexts = generic_contexts.ok_or(SymbolTextError::InvalidState(
            "missing generic contexts for a direct-coded dictionary",
        ))?;
        let mut full_adaptive = [TemplateOffset::new(0, 0); 12];
        full_adaptive[..usize::from(header.adaptive_count)]
            .copy_from_slice(&header.adaptive[..usize::from(header.adaptive_count)]);
        let call = GenericArithmeticCall {
            width,
            height,
            adaptive: full_adaptive,
            typical_prediction: false,
            skip: None,
        };
        let bitmap = match codec {
            SymbolCodec::Arithmetic(codec) => {
                codec.decode_generic(contexts, call, height, limits, generic_work)
            }
            SymbolCodec::Huffman(_) => Err(SymbolTextError::InvalidState(
                "direct dictionary bitmap requires arithmetic state",
            )),
            #[cfg(test)]
            SymbolCodec::Scripted(_) => Err(SymbolTextError::InvalidState(
                "direct dictionary bitmap requires arithmetic state",
            )),
        }?;
        if bitmap.width() != width || bitmap.height() != height {
            return Err(SymbolTextError::InvalidDimensions);
        }
        Ok(bitmap)
    }
}

#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
#[cfg_attr(verus_keep_ghost, verus_spec(out =>
    requires
        symbol_text_spec::codebook_valid(imported),
        header.adaptive_count <= 4,
        total_symbols as nat
            == symbol_text_spec::codebook_len(imported) + header.new_symbols as nat,
    ensures match out {
        Ok((exported, _, _, consumed)) =>
            symbol_text_spec::bitmaps_valid(exported@)
                && consumed == body@.len(),
        Err(_) => true,
    },
))]
pub(in crate::symbol_text) fn decode_symbol_dictionary_arithmetic(
    header: &SymbolDictionaryHeader,
    imported: SymbolCodebook<'_>,
    body: &[u8],
    code_length: u8,
    total_symbols: u32,
    refinement_adaptive: [(i8, i8); 2],
    used_contexts: Option<&DictionaryContexts>,
    limits: &Limits,
) -> ArithmeticDictionaryResult {
    let config = header.config;
    let normalized = normalized_bounded_arithmetic(body, limits)?;
    let refinement_template = config
        .refinement_aggregation
        .then(|| refinement_template_from_u8(config.refinement_template));
    let mut codec = SymbolCodec::Arithmetic(new_arithmetic_codec(
        normalized.bytes(),
        code_length,
        refinement_template,
        limits,
    )?);
    let mut resources = ResourceTracker {
        work_units: 0,
        decoded_bitmap_bytes: 0,
    };

    let mut generic_contexts = if config.refinement_aggregation {
        None
    } else {
        let mut contexts =
            GenericContexts::new(generic_template_from_u8(config.template), false, limits)?;
        if let Some(used) = used_contexts.and_then(|used| used.generic.as_ref()) {
            contexts.restore(used, limits)?;
        }
        Some(contexts)
    };
    let mut generic_work = 0_u64;

    if config.refinement_aggregation {
        if let Some(used) = used_contexts.and_then(|used| used.refinement.as_ref()) {
            let SymbolCodec::Arithmetic(arithmetic) = &mut codec else {
                return Err(SymbolTextError::InvalidState(
                    "arithmetic dictionary has non-arithmetic state",
                ));
            };
            let contexts = arithmetic
                .refinement
                .as_mut()
                .ok_or(SymbolTextError::InvalidState(
                    "missing arithmetic refinement contexts",
                ))?;
            contexts.restore(used, limits)?;
        }
    }

    let new_symbol_capacity =
        usize::try_from(header.new_symbols).map_err(|_| MalformedError::IntegerOverflow)?;
    let mut new_symbols: Vec<Bitmap> = Vec::new();
    new_symbols
        .try_reserve(new_symbol_capacity)
        .map_err(|_| DecodeError::Allocation {
            requested_bytes: u64::from(header.new_symbols),
        })?;

    let mut class_state = DictionaryClassState::new(header.new_symbols, limits)?;
    #[cfg_attr(verus_keep_ghost, verus_spec(
        invariant
            new_symbols@.len() <= header.new_symbols as nat,
            new_symbol_capacity == header.new_symbols as nat,
            class_state.decoded_symbols as nat == new_symbols@.len(),
            class_state.declared_new_symbols == header.new_symbols,
            !class_state.class_open,
            header.adaptive_count <= 4,
            symbol_text_spec::dictionary_state_valid(&class_state),
            symbol_text_spec::codebook_valid(imported),
            symbol_text_spec::bitmaps_valid(new_symbols@),
            symbol_text_spec::codec_valid(&codec),
            symbol_text_spec::work_units(&resources) <= limits.max_work_units,
        decreases limits.max_work_units - symbol_text_spec::work_units(&resources),
    ))]
    while u32::try_from(new_symbols.len()).map_err(|_| MalformedError::IntegerOverflow)?
        < header.new_symbols
    {
        #[cfg_attr(not(verus_keep_ghost), allow(unused_variables))]
        let class_start_work = resources.work_units;
        let height_delta = required_integer(&mut codec, IntegerField::HeightDelta)?;
        class_state.begin_height_class(height_delta, limits, &mut resources)?;
        #[cfg_attr(verus_keep_ghost, verus_spec(
            invariant
                new_symbols@.len() <= header.new_symbols as nat,
                new_symbol_capacity == header.new_symbols as nat,
                class_state.decoded_symbols as nat == new_symbols@.len(),
                class_state.declared_new_symbols == header.new_symbols,
                class_state.class_open,
                header.adaptive_count <= 4,
                symbol_text_spec::dictionary_state_valid(&class_state),
                symbol_text_spec::codebook_valid(imported),
                symbol_text_spec::bitmaps_valid(new_symbols@),
                symbol_text_spec::codec_valid(&codec),
                symbol_text_spec::work_units(&resources)
                    <= limits.max_work_units,
                resources.work_units > class_start_work,
            decreases limits.max_work_units
                - symbol_text_spec::work_units(&resources),
        ))]
        while let EntropyInteger::Value(width_delta) =
            codec.decode_integer(IntegerField::WidthDelta)?
        {
            let (width, height) = class_state.add_symbol(width_delta, limits, &mut resources)?;
            let aggregation_instances = if config.refinement_aggregation {
                let instances = required_integer(&mut codec, IntegerField::AggregationInstances)?;
                Some(
                    u32::try_from(instances).map_err(|_| {
                        SymbolTextError::InvalidEntropyValue("aggregation instances")
                    })?,
                )
            } else {
                None
            };
            let bitmap = decode_one_new_symbol(
                header,
                &mut codec,
                imported,
                &new_symbols,
                generic_contexts.as_mut(),
                &mut generic_work,
                aggregation_instances,
                width,
                height,
                code_length,
                refinement_adaptive,
                limits,
                &mut resources,
            )?;
            if new_symbols.len() >= new_symbol_capacity {
                return Err(SymbolTextError::InvalidCount("too many new symbols"));
            }
            new_symbols.push(bitmap);
        }
        class_state.finish_height_class()?;
    }
    class_state.finish()?;

    let exported = decode_export_runs(
        &mut codec,
        total_symbols,
        header.exported_symbols,
        limits,
        &mut resources,
    )?;

    let SymbolCodec::Arithmetic(arithmetic) = &codec else {
        return Err(SymbolTextError::InvalidState(
            "arithmetic dictionary has non-arithmetic state",
        ));
    };
    validate_bounded_arithmetic_marker(&arithmetic.decoder, normalized.len())?;

    let retained_generic = match generic_contexts.as_ref() {
        Some(contexts) => Some(contexts.try_snapshot(limits)?),
        None => None,
    };
    let retained_refinement = match arithmetic.refinement.as_ref() {
        Some(contexts) => Some(contexts.try_snapshot(limits)?),
        None => None,
    };

    let exported_symbols = assemble_exported_symbols(imported, &new_symbols, &exported, limits)?;
    Ok((
        exported_symbols,
        retained_generic,
        retained_refinement,
        body.len(),
    ))
}