Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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++'sGetNextBoxdoes, 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
GetNextBoxrejects a box smaller than its header andGetNextBoxWithTagstops 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_eofandtest_read_next_box_length_zero_at_nonzero_offset, which pin size 0 to the end of the stream), not 7.Problem
Cr3GetPreviewDataandCr3GetOrientation(C++, Rust) walk a CR3's boxes.GetNextBoxuses the size as the box's length. A box is invalid only when it does not end after it starts (Box::IsValid).GetNextBoxWithTaglooks for a child from an offset on, whatever its parent's size.read_next_boxfollows ISO/IEC 14496-12. A size of 0 runs to the end of the stream. A size smaller than the box's header is an error.find_next_box_with_tagdoes not return a child that runs past its parent.For example, on a small CR3 with one box's 32-bit size changed:
date_timeempty,iso0date_time"2021:02:03 04:05:06",iso400Cr3GetOrientationtrue, orientation 8Cr3GetOrientationfalse(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
Cr3GetPreviewDatareturns kFail. The port reads mdat to the end of the file and returns the full-size preview in it: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
Cr3GetOrientationreturns its orientation (8). It then takes the next box to start 7 bytes after CMT1. The port stops at CMT1 withInvalidBoxHeader. 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:
Question
Which behaviour do we want: C++'s or the Rust port's? Specifically:
This PR implements C++'s behaviour.
Fix
This PR changes
rust/src/cr3/box_reader.rsto follow C++'sGetNextBoxandGetNextBoxWithTag:length_32 == 0branch. A size of 0 then makes a box that ends where it starts, whichread_next_boxrejects, asBox::IsValiddoes.total_length < header_lengtherror.read_next_boxnow checks only that the box ends after it starts (C++'sBox::IsValid), in place ofBoxHeader::is_valid, which also wants the whole header inside the box.find_next_box_with_tagthat returnedNonefor a child ending past its parent.The examples above then return what C++ returns. Replayed before and after (x86-64):
date_timeempty,iso0Notes for review:
cr3::box_reader::tests::test_read_next_box_invalid_short_length, and intests/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_overrunandtest_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_tagstill stops looking at its parent's end. C++'sGetNextBoxWithTagwould 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.length_32 == 1) goes through the same checks: an extended size below 16 is now read as C++ reads it.