Skip to content

Read CR3 box sizes as the C++ library does - #11

Open
kunpai wants to merge 2 commits into
google:masterfrom
kunpai:cr3-box-sizes-as-written
Open

kunpai wants to merge 2 commits into
google:masterfrom
kunpai:cr3-box-sizes-as-written

Conversation

@kunpai

@kunpai kunpai commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Read CR3 box sizes as the C++ library does

NOTE: #32 makes the same change to read_next_box: it now takes a size of 0 as written, so the header check rejects it as C++'s GetNextBox does, and it updates the port's size-0 tests. On master with #32, all the examples below agree and the port's tests pass (44 + 179), so this PR can be closed once #32 is merged.

NOTE: Since the C++ export in fca6535, C++'s GetNextBox rejects a box smaller than its header and GetNextBoxWithTag stops at a child that runs past its parent, as the port does, so questions 2 and 3 below are answered. bb9c28d puts the port's two checks back. This PR now changes only a box of size 0 (question 1), which C++ still reads as a box that ends where it starts: on master, a CR3 whose CRAW box has size 0 gives kFail and no preview in C++, and kOk with a 6000×4000 preview in the port. With this PR the examples below all agree on master, and 2 of the port's tests fail (test_read_next_box_length_zero_extends_to_eof and test_read_next_box_length_zero_at_nonzero_offset, which pin size 0 to the end of the stream), not 7.

Problem

Cr3GetPreviewData and Cr3GetOrientation (C++, Rust) walk a CR3's boxes.

For example, on a small CR3 with one box's 32-bit size changed:

# C++ Rust
1 CMT2's size is 0 the walk stops at CMT2: date_time empty, iso 0 CMT2 runs to the end: date_time "2021:02:03 04:05:06", iso 400
1 CRAW's size is 0 kFail, no preview kOk, preview 6000×4000
2 CMT1's size is 7 Cr3GetOrientation true, orientation 8 Cr3GetOrientation false
2 THMB's size is 5 thumbnail 160×120, 35 bytes at offset 306 no thumbnail
3 co64's size runs past its stbl kOk, preview 6000×4000 kFail, no preview

(For CMT2 size 0, CMT1 size 7 and THMB size 5, both sides return kFail from Cr3GetPreviewData: the walk cannot go on after the odd box. What differs is what each side wrote before it stopped.)

C++ is on the left and the Rust port on the right in the pictures below. Each panel shows the image a viewer would decode from the file at the preview offset and length that side returned.

1. A box of size 0

ISO BMFF allows size 0 for a file's last box: "this box is the last one in the file, and its contents extend to the end of the file". When a CR3's mdat box says size 0, C++ reads it as a box that ends where it starts, an invalid box, and Cr3GetPreviewData returns kFail. The port reads mdat to the end of the file and returns the full-size preview in it:

P3-1-mdat-size-0

2. A box smaller than its header

When CMT1's size is 7, C++ still parses CMT1 from its usual place, 8 bytes in, and Cr3GetOrientation returns its orientation (8). It then takes the next box to start 7 bytes after CMT1. The port stops at CMT1 with InvalidBoxHeader. A THMB of size 5 is the same: C++ reads the thumbnail's size and offset from their usual places in it, and the port reads nothing.

3. A child box that runs past its parent

When co64's size runs past the end of the stbl box it is in, C++ still finds co64, reads the preview's offset from it, and returns the full-size preview. The port's search does not return a box that ends past its parent, so it finds no co64 and returns kFail:

P3-3-co64-past-stbl

Question

Which behaviour do we want: C++'s or the Rust port's? Specifically:

  1. should a box of size 0 be an invalid box that ends the walk (like the C++ library), or run to the end of the stream (like the Rust port)?
  2. should a box smaller than its header still be read (like the C++ library), or be an error (like the Rust port)?
  3. should a child box that runs past its parent be found (like the C++ library), or not (like the Rust port)?

This PR implements C++'s behaviour.

Fix

This PR changes rust/src/cr3/box_reader.rs to follow C++'s GetNextBox and GetNextBoxWithTag:

  • Size 0 (1): drops the length_32 == 0 branch. A size of 0 then makes a box that ends where it starts, which read_next_box rejects, as Box::IsValid does.
  • Smaller than the header (2): drops the total_length < header_length error. read_next_box now checks only that the box ends after it starts (C++'s Box::IsValid), in place of BoxHeader::is_valid, which also wants the whole header inside the box.
  • Past the parent (3): drops the check in find_next_box_with_tag that returned None for a child ending past its parent.

The examples above then return what C++ returns. Replayed before and after (x86-64):

# C++ Rust main Rust patched
1 CMT2's size is 0 date_time empty, iso 0 "2021:02:03 04:05:06", 400 empty, 0
1 CRAW's size is 0 kFail kOk, 6000×4000 kFail
1 mdat's size is 0 (picture 1) kFail kOk, 6000×4000 kFail
2 CMT1's size is 7 orientation true, 8 false true, 8
2 THMB's size is 5 thumbnail 160×120 at 306 none 160×120 at 306
3 co64 past its stbl kOk, 6000×4000 kFail kOk, 6000×4000
3 co64 past its stbl (picture 3) kOk, 6000×4000 kFail kOk, 6000×4000

Notes for review:

  • The port's tests pin the ISO BMFF rules, and 7 of them fail with this patch. They are cr3::box_reader::tests::test_read_next_box_invalid_short_length, and in tests/unit/cr3_box_reader.rs: test_read_next_box_length_zero_extends_to_eof, test_read_next_box_length_zero_at_nonzero_offset, test_read_next_box_invalid_compact_sizes, test_read_next_box_invalid_extended_sizes, test_find_next_box_with_tag_enclosing_boundary_overrun and test_find_next_box_with_tag_detects_non_advancing_progress (its child box has size 4, which C++ accepts). They are left as they are, to show what changes.
  • find_next_box_with_tag still stops looking at its parent's end. C++'s GetNextBoxWithTag would go on past it, into the boxes after the parent. Only a child that starts inside its parent and runs past it showed up in fuzzing, and this PR covers that.
  • A 64-bit size (length_32 == 1) goes through the same checks: an extended size below 16 is now read as C++ reads it.

C++'s GetNextBox takes a box's size as written: a size of 0 is an
invalid box, a size below the header is used, and a child may run past
its parent. The port applied ISO BMFF's rules to all three.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant