caj2pdf-core 0.8.1

Platform-neutral CAJ-family conversion core
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// SPDX-License-Identifier: MIT

//! Bounded metadata traversal for the three independently measured HN/C8
//! container profiles. Image payloads and text are never loaded here.
//! [`convert_document_pdf`] and [`convert_source_pages_pdf`] convert whole
//! documents; type-0, JPEG and type-3 images are checked and emitted by the
//! one shared composition pipeline. Composition validates the observed text
//! frame while retaining only raw image-coordinate words. Empirical geometry remains diagnostic.
//! [`Hnc8Reader::application_info`] reads the trailing C8 application-info
//! package's DOI, URL and note count.

mod appinfo;
mod compose;
mod inflate;
mod jpeg;
mod native;
mod native_glyphs;
mod native_page;
pub use native_glyphs::{MAX_NATIVE_FONT_GLYPHS, NativeFontGlyphAlias};
mod outline;
mod placement;
mod structure;
mod text;
mod type3_image;

pub use appinfo::{
    ApplicationInfo, ApplicationInfoDefect, ApplicationInfoReport, ApplicationInfoStatus,
    MAX_APPLICATION_INFO_BYTES, MAX_APPLICATION_INFO_FIELD_BYTES,
};
pub use compose::{
    C8FontSource, C8FontSources, ComposeOptions, ComposePage, ComposeReport, ComposeVisitor,
    ComposedImage, convert_document_pdf, convert_source_pages_pdf, uses_native_text,
};
pub use jpeg::{JpegColor, JpegInfo, read_type2_jpeg_info};
pub(crate) use native::{
    NativeRecord, NativeRecordVisitor, decode_native_character, decode_native_character_for_mode,
    decode_native_image_coordinate,
};
#[cfg(test)]
pub(crate) use native_page::labelled_font;
pub(crate) use native_page::write_c8_native_page;
pub use native_page::{
    C8_DEFAULT_DECORATION_ALIAS, C8_MAX_FONTS, C8PageFonts, NativeDecorationGlyph,
    NativeSymbolGlyph, SymbolFontIdentity, is_mode_zero_symbol,
};
pub use outline::{MAX_RECORDED_OUTLINE_DEFECTS, OutlineDefect, OutlineRepair, OutlineReport};
pub(crate) use placement::{
    C8GlyphClass, EMPIRICAL_COORDINATE_POINTS_PER_UNIT, EmpiricalPageGeometry,
    empirical_c8_horizontal_decoration, empirical_c8_segment, empirical_image_transform,
    empirical_page_from_pixels,
};
pub use structure::{ApplicationInfoTail, TextFraming, TextStructure};
pub use text::RawTextCoordinate;
pub(crate) use text::TEXT_DECODER_RESERVATION_BYTES;

use crate::jbig1::Type0Span;
use crate::{
    Cancellation, Context, Error, ErrorKind, Hnc8Stage, Limits, RangedSource, Result, read_exact_at,
};

const PAGE_ROW_BYTES: u64 = 20;
const IMAGE_RECORD_BYTES: u64 = 12;
const OUTLINE_RECORD_BYTES: u64 = 308;

/// The three measured container layouts; this is not a complete HN standard.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Variant {
    C8,
    HnA,
    HnB,
}

impl Variant {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::C8 => "C8",
            Self::HnA => "HN-A",
            Self::HnB => "HN-B",
        }
    }
}

/// Where a conversion error occurred: the container variant, the one-based
/// page and image numbers, and the source offset, each when known.
#[derive(Clone, Copy)]
struct At {
    variant: Option<Variant>,
    page: Option<u32>,
    image: Option<u32>,
    offset: Option<u64>,
}

impl At {
    const NONE: Self = Self {
        variant: None,
        page: None,
        image: None,
        offset: None,
    };

    fn with_offset(self, offset: u64) -> Self {
        Self {
            offset: Some(offset),
            ..self
        }
    }

    /// Locate `error` at the conversion `stage`. An unlocated error takes
    /// this location; an HN/C8 or JBIG2 error keeps its own offset, numbers
    /// and stage and gains the ones it lacks.
    fn locate(self, stage: Hnc8Stage, error: Error) -> Error {
        let context = match error.context {
            Context::None => Context::Hnc8 {
                variant: self.variant,
                page: self.page,
                image: self.image,
                segment: None,
                stage: Some(stage),
            },
            Context::Jbig2 { segment } => Context::Hnc8 {
                variant: self.variant,
                page: self.page,
                image: self.image,
                segment,
                stage: Some(stage),
            },
            Context::Hnc8 {
                variant,
                page,
                image,
                segment,
                stage: own,
            } => Context::Hnc8 {
                variant: variant.or(self.variant),
                page: page.or(self.page),
                image: image.or(self.image),
                segment,
                stage: own.or(Some(stage)),
            },
            _ => return error,
        };
        Error {
            offset: error.offset.or(self.offset),
            context,
            ..error
        }
    }

    /// An unlocated error at `stage`.
    fn error(self, stage: Hnc8Stage, error: Error) -> Error {
        self.locate(stage, error)
    }

    /// A mapper that locates an error at `stage`.
    fn locator(self, stage: Hnc8Stage) -> impl FnOnce(Error) -> Error {
        move |error| self.locate(stage, error)
    }
}

/// A checked absolute interval in the source. The end is exclusive.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Span {
    pub offset: u64,
    pub length: u64,
}

impl Span {
    pub fn checked_end(self) -> Option<u64> {
        self.offset.checked_add(self.length)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Header {
    pub variant: Variant,
    /// Raw native mode at C8 offset 12 or HN-B offset 148; unknown for HN-A.
    /// Independently controlled modes 0 and 2 interpret some character codes
    /// differently. Preserving this word does not admit its rendering profile.
    pub native_mode: Option<u32>,
    /// Raw C8 origin at 28/30 or HN-B origin at 164/166; unknown for HN-A.
    /// The verified native-record profile subtracts these from record coordinates.
    /// This does not establish image placement or font baseline semantics.
    pub native_origin: Option<[u16; 2]>,
    /// Declared page extents in source units; native HN-B uses these too.
    pub page_size: Option<[u16; 2]>,
    pub page_count: u32,
    pub page_index: Span,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PageRecord {
    pub page_number: u32,
    pub row_offset: u64,
    pub text: Span,
    pub image_count: u32,
    /// Uninterpreted bytes at page-row offsets +10 through +19.
    /// For the compact HN-B profile, only +10/+11 exist; the remaining eight
    /// bytes are padding, not source data. Its admitted third word is zero.
    pub unknown: [u8; 10],
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ImageRecord {
    pub page_number: u32,
    pub image_number: u32,
    pub descriptor_offset: u64,
    /// Signed descriptor field at +0, checked to be in the observed 0..=3 set.
    pub record_type: u32,
    pub payload: Span,
}

impl ImageRecord {
    /// Pass exactly the outer type-0 descriptor and absolute payload span to
    /// the bounded row decoder. Other observed image types remain metadata.
    pub fn type0_span(self) -> Option<Type0Span> {
        (self.record_type == 0).then_some(Type0Span {
            record_type: self.record_type,
            offset: self.payload.offset,
            length: self.payload.length,
        })
    }
}

#[derive(Clone, Copy)]
struct Location {
    variant: Option<Variant>,
    offset: u64,
    page: Option<u32>,
    image: Option<u32>,
}

impl Location {
    fn at(self, offset: u64) -> Self {
        Self { offset, ..self }
    }

    fn context(self) -> Context {
        Context::Hnc8 {
            variant: self.variant,
            page: self.page,
            image: self.image,
            segment: None,
            stage: None,
        }
    }

    /// Locate an unlocated error here.
    fn locate(self, error: Error) -> Error {
        error.or_at(self.offset, self.context())
    }

    fn malformed(self, reason: &'static str) -> Error {
        self.locate(Error::invalid(reason))
    }

    fn unsupported(self, reason: &'static str) -> Error {
        self.locate(Error::unsupported(self.offset, reason))
    }

    fn truncated(self, field: &'static str, expected: u64, available: u64) -> Error {
        self.locate(Error::truncated(self.offset, expected, available).because(field))
    }

    fn limit(self, resource: &'static str, limit: u64, attempted: u64) -> Error {
        self.locate(Error::limit(resource, limit, attempted))
    }

    fn cancelled(self) -> Error {
        self.locate(Error::cancelled())
    }
}

/// Every caller passes an offset and a length below 2^42: fixed header
/// offsets, a page index after at most `2^31` 308-byte outline records,
/// page rows within at most `2^31` 20-byte rows, and record fields and
/// descriptor cursors built from nonnegative i32 values. Their sum therefore
/// fits u64.
fn checked_span(
    source_size: u64,
    offset: u64,
    length: u64,
    loc: Location,
    field: &'static str,
) -> Result<Span> {
    debug_assert!(offset < 1 << 42 && length < 1 << 42);
    let end = offset + length;
    if offset > source_size || end > source_size {
        return Err(loc.truncated(field, length, source_size.saturating_sub(offset)));
    }
    Ok(Span { offset, length })
}

fn signed32(bytes: &[u8]) -> i32 {
    i32::from_le_bytes(bytes.try_into().expect("fixed field width"))
}

fn signed16(bytes: &[u8]) -> i16 {
    i16::from_le_bytes(bytes.try_into().expect("fixed field width"))
}

/// `reason` names the field and that it is negative.
fn nonnegative32(value: i32, loc: Location, reason: &'static str) -> Result<u64> {
    u64::try_from(value).map_err(|_| loc.malformed(reason))
}

fn read_fixed<S: RangedSource, C: Cancellation>(
    source: &mut S,
    limits: &Limits,
    cancellation: &C,
    offset: u64,
    bytes: &mut [u8],
    loc: Location,
    field: &'static str,
) -> Result<()> {
    checked_span(source.size(), offset, bytes.len() as u64, loc, field)?;
    let mut done = 0;
    while done < bytes.len() {
        let count = (bytes.len() - done).min(limits.io_chunk_bytes);
        let current = offset + done as u64;
        read_exact_at(
            source,
            current,
            &mut bytes[done..done + count],
            limits,
            cancellation,
        )
        .map_err(|error| match error.kind {
            ErrorKind::Truncated { .. } => loc.at(current).locate(error.because(field)),
            _ => loc.at(current).locate(error),
        })?;
        done += count;
    }
    Ok(())
}

fn overlaps_protected(span: Span, protected_end: u64) -> bool {
    span.length > 0 && span.offset < protected_end
}

#[derive(Clone, Copy)]
struct CurrentPage {
    page: PageRecord,
    next_image: u32,
    next_descriptor: u64,
}

/// A one-page-at-a-time cursor. A failed read leaves the cursor where it was;
/// the caller abandons it. The source remains borrowable between records.
pub struct Hnc8Reader<'a, S: RangedSource, C: Cancellation> {
    source: &'a mut S,
    limits: &'a Limits,
    cancellation: &'a C,
    header: Header,
    page_row_bytes: u64,
    next_page: u32,
    current: Option<CurrentPage>,
}

impl<'a, S: RangedSource, C: Cancellation> Hnc8Reader<'a, S, C> {
    /// Open a cursor at the first page. `limits` are validated here; each
    /// image payload is bounded by `Limits::max_allocation_bytes`.
    pub fn open(source: &'a mut S, limits: &'a Limits, cancellation: &'a C) -> Result<Self> {
        Self::open_starting_at(source, limits, cancellation, 1)
    }

    /// Open a fresh diagnostic cursor at one page-index row.
    ///
    /// This deliberately skips earlier pages so that malformed pages can be
    /// inspected independently. Full-document conversion must use `open`;
    /// the CLI's per-page structure report uses this cursor.
    pub fn probe_at_page(
        source: &'a mut S,
        limits: &'a Limits,
        cancellation: &'a C,
        start_page: u32,
    ) -> Result<Self> {
        Self::open_starting_at(source, limits, cancellation, start_page)
    }

    fn open_starting_at(
        source: &'a mut S,
        limits: &'a Limits,
        cancellation: &'a C,
        start_page: u32,
    ) -> Result<Self> {
        let base = Location {
            variant: None,
            offset: 0,
            page: None,
            image: None,
        };
        limits.validate().map_err(|source| base.locate(source))?;
        if cancellation.is_cancelled() {
            return Err(base.cancelled());
        }
        if source.size() > limits.max_input_bytes {
            return Err(base.limit("source bytes", limits.max_input_bytes, source.size()));
        }
        let mut magic = [0; 4];
        read_fixed(
            source,
            limits,
            cancellation,
            0,
            &mut magic,
            base,
            "signature",
        )?;
        let (variant, count_offset, index_start): (Variant, u64, u64) = match magic {
            [0xc8, 0, 0, 0] => (Variant::C8, 0x08, 0x50),
            [b'H', b'N', 0, 0] => {
                let mut marker = [0; 4];
                read_fixed(
                    source,
                    limits,
                    cancellation,
                    4,
                    &mut marker,
                    base.at(4),
                    "HN marker",
                )?;
                match marker {
                    [0x90, 0x01, 0, 0] => (Variant::HnA, 0x90, 0x15c),
                    [0xc8, 0, 0, 0] => (Variant::HnB, 0x90, 0xd8),
                    _ => {
                        return Err(base.at(4).unsupported("HN marker"));
                    }
                }
            }
            _ => {
                return Err(base.unsupported("signature"));
            }
        };
        let loc = Location {
            variant: Some(variant),
            ..base
        };
        let mut count = [0; 4];
        read_fixed(
            source,
            limits,
            cancellation,
            count_offset,
            &mut count,
            loc.at(count_offset),
            "page count",
        )?;
        let signed_count = signed32(&count);
        if signed_count <= 0 {
            return Err(loc
                .at(count_offset)
                .malformed("page count: must be positive"));
        }
        let page_count = signed_count as u32;
        if page_count > limits.max_pages {
            return Err(loc.at(count_offset).limit(
                "pages",
                u64::from(limits.max_pages),
                u64::from(page_count),
            ));
        }
        let native_mode = if variant != Variant::HnA {
            let offset = count_offset + 4;
            let mut mode = [0; 4];
            read_fixed(
                source,
                limits,
                cancellation,
                offset,
                &mut mode,
                loc.at(offset),
                "native mode",
            )?;
            Some(u32::from_le_bytes(mode))
        } else {
            None
        };
        let native_origin = if variant != Variant::HnA {
            let offset = count_offset + 20;
            let mut origin = [0; 4];
            read_fixed(
                source,
                limits,
                cancellation,
                offset,
                &mut origin,
                loc.at(offset),
                "native coordinate origin",
            )?;
            Some([
                u16::from_le_bytes([origin[0], origin[1]]),
                u16::from_le_bytes([origin[2], origin[3]]),
            ])
        } else {
            None
        };
        let page_size = {
            let offset = count_offset + 24;
            let mut size = [0; 4];
            read_fixed(
                source,
                limits,
                cancellation,
                offset,
                &mut size,
                loc.at(offset),
                "page dimensions",
            )?;
            Some([
                u16::from_le_bytes([size[0], size[1]]),
                u16::from_le_bytes([size[2], size[3]]),
            ])
        };
        let index_start = if variant == Variant::HnA {
            let mut outline = [0; 4];
            read_fixed(
                source,
                limits,
                cancellation,
                0x158,
                &mut outline,
                loc.at(0x158),
                "outline count",
            )?;
            let outline_count = nonnegative32(
                signed32(&outline),
                loc.at(0x158),
                "outline count: negative signed value",
            )?;
            // The count is below 2^31, so the index starts below 2^42.
            let outline_bytes = outline_count * OUTLINE_RECORD_BYTES;
            index_start + outline_bytes
        } else {
            index_start
        };
        let page_row_bytes = if variant == Variant::HnB {
            let mut marker = [0; 4];
            read_fixed(
                source,
                limits,
                cancellation,
                0x88,
                &mut marker,
                loc.at(0x88),
                "HN-B page-index layout",
            )?;
            match u32::from_le_bytes(marker) {
                0 => 12,
                0xc8 => PAGE_ROW_BYTES,
                _ => {
                    return Err(loc.at(0x88).unsupported("HN-B page-index layout"));
                }
            }
        } else {
            PAGE_ROW_BYTES
        };
        let index_length = u64::from(page_count) * page_row_bytes;
        let page_index = checked_span(
            source.size(),
            index_start,
            index_length,
            loc.at(index_start),
            "page index",
        )?;
        if start_page == 0 || start_page > page_count {
            return Err(loc
                .at(index_start)
                .malformed("page number: outside declared page index"));
        }
        Ok(Self {
            source,
            limits,
            cancellation,
            header: Header {
                variant,
                native_mode,
                native_origin,
                page_size,
                page_count,
                page_index,
            },
            page_row_bytes,
            next_page: start_page,
            current: None,
        })
    }

    pub fn header(&self) -> Header {
        self.header
    }

    pub fn source_mut(&mut self) -> &mut S {
        self.source
    }

    pub fn next_page(&mut self) -> Result<Option<PageRecord>> {
        let loc = Location {
            variant: Some(self.header.variant),
            offset: self.header.page_index.offset,
            page: Some(self.next_page),
            image: None,
        };
        if let Some(current) = self
            .current
            .filter(|page| page.next_image <= page.page.image_count)
        {
            return Err(Location {
                page: Some(current.page.page_number),
                image: Some(current.next_image),
                offset: current.next_descriptor,
                ..loc
            }
            .malformed("page has unread image records"));
        }
        if self.next_page > self.header.page_count {
            return Ok(None);
        }
        if self.cancellation.is_cancelled() {
            return Err(loc.cancelled());
        }
        let page_number = self.next_page;
        let row_offset =
            self.header.page_index.offset + u64::from(page_number - 1) * self.page_row_bytes;
        let loc = loc.at(row_offset);
        let mut row = [0; PAGE_ROW_BYTES as usize];
        read_fixed(
            self.source,
            self.limits,
            self.cancellation,
            row_offset,
            &mut row[..self.page_row_bytes as usize],
            loc,
            "page row",
        )?;
        let text_offset = nonnegative32(
            signed32(&row[..4]),
            loc,
            "text offset: negative signed value",
        )?;
        let text_length = nonnegative32(
            signed32(&row[4..8]),
            loc.at(row_offset + 4),
            "text length: negative signed value",
        )?;
        let text = checked_span(
            self.source.size(),
            text_offset,
            text_length,
            loc.at(row_offset),
            "text span",
        )?;
        if self.page_row_bytes == 12 {
            if text.offset < self.header.page_index.checked_end().expect("checked index") {
                return Err(loc.malformed("text span: overlaps protected container index"));
            }
            let value = u32::from_le_bytes(row[8..12].try_into().expect("four bytes"));
            if value != 0 {
                return Err(loc
                    .at(row_offset + 8)
                    .unsupported("compact HN-B third word"));
            }
        }
        let signed_images = signed16(&row[8..10]);
        if signed_images < 0 {
            return Err(loc
                .at(row_offset + 8)
                .malformed("image count: negative signed value"));
        }
        let image_count = signed_images as u32;
        let mut unknown = [0; 10];
        unknown.copy_from_slice(&row[10..]);
        let page = PageRecord {
            page_number,
            row_offset,
            text,
            image_count,
            unknown,
        };
        self.current = Some(CurrentPage {
            page,
            next_image: 1,
            next_descriptor: text.checked_end().expect("checked text span"),
        });
        self.next_page += 1;
        Ok(Some(page))
    }

    pub fn next_image(&mut self) -> Result<Option<ImageRecord>> {
        let loc = Location {
            variant: Some(self.header.variant),
            offset: self.header.page_index.offset,
            page: self.current.map(|current| current.page.page_number),
            image: self.current.map(|current| current.next_image),
        };
        let current = self
            .current
            .ok_or_else(|| loc.malformed("no current page"))?;
        if current.next_image > current.page.image_count {
            return Ok(None);
        }
        if self.cancellation.is_cancelled() {
            return Err(loc.cancelled());
        }
        let descriptor_offset = current.next_descriptor;
        let loc = loc.at(descriptor_offset);
        let descriptor = checked_span(
            self.source.size(),
            descriptor_offset,
            IMAGE_RECORD_BYTES,
            loc,
            "image descriptor",
        )?;
        if overlaps_protected(
            descriptor,
            self.header
                .page_index
                .checked_end()
                .expect("checked page index"),
        ) {
            return Err(loc.malformed("image descriptor: overlaps header or page index"));
        }
        let mut bytes = [0; IMAGE_RECORD_BYTES as usize];
        read_fixed(
            self.source,
            self.limits,
            self.cancellation,
            descriptor_offset,
            &mut bytes,
            loc,
            "image descriptor",
        )?;
        let signed_type = signed32(&bytes[..4]);
        if signed_type < 0 {
            return Err(loc.malformed("image type: negative signed value"));
        }
        if signed_type > 3 {
            return Err(loc.unsupported("image type"));
        }
        let payload_offset = nonnegative32(
            signed32(&bytes[4..8]),
            loc.at(descriptor_offset + 4),
            "image offset: negative signed value",
        )?;
        let payload_length = nonnegative32(
            signed32(&bytes[8..12]),
            loc.at(descriptor_offset + 8),
            "image length: negative signed value",
        )?;
        if payload_length == 0 {
            return Err(loc
                .at(descriptor_offset + 8)
                .malformed("image length: zero-length payload"));
        }
        let payload = checked_span(
            self.source.size(),
            payload_offset,
            payload_length,
            loc.at(descriptor_offset + 4),
            "image payload",
        )?;
        if payload_length > self.limits.max_allocation_bytes {
            return Err(loc.at(descriptor_offset + 8).limit(
                "image span bytes",
                self.limits.max_allocation_bytes,
                payload_length,
            ));
        }
        if payload.offset < descriptor.checked_end().expect("checked descriptor span") {
            return Err(loc
                .at(descriptor_offset + 4)
                .malformed("image offset: payload overlaps descriptor or chain regresses"));
        }
        // The payload starts at or after the descriptor's end, and the
        // descriptor was checked to start after the protected header and
        // page index.
        debug_assert!(!overlaps_protected(
            payload,
            self.header
                .page_index
                .checked_end()
                .expect("checked page index"),
        ));
        let record_type = signed_type as u32;
        let record = ImageRecord {
            page_number: current.page.page_number,
            image_number: current.next_image,
            descriptor_offset,
            record_type,
            payload,
        };
        self.current = Some(CurrentPage {
            next_image: current.next_image + 1,
            next_descriptor: payload.checked_end().expect("checked image span"),
            ..current
        });
        Ok(Some(record))
    }
}