Skip to content

Keep reading the JPEG header after a segment length below 2, as GetJpegDimensions does - #36

Open
kunpai wants to merge 1 commit into
google:masterfrom
kunpai:jpeg-dimensions-short-segment-length
Open

kunpai wants to merge 1 commit into
google:masterfrom
kunpai:jpeg-dimensions-short-segment-length

Conversation

@kunpai

@kunpai kunpai commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Keep reading the JPEG header after a segment length below 2, as GetJpegDimensions does

Problem

GetJpegDimensions (C++, Rust) walks a JPEG's segments from an offset until it finds the frame header (SOF0) with the image's width and height. C++ adds each segment's length to the offset and goes on, whatever the length; a length of 0 still moves it 2 bytes, past the marker it just read. The port returns an error on a length below 2, and fill_image_size then leaves the image at 0×0.

The size decides what the image is: FillPreviewImageData (C++, Rust) stores it as the thumbnail when neither side is above 512, and as the preview otherwise. A 0×0 image is always a thumbnail.

For example, two RAFs and two RW2s from the campaigns. In each, the Exif in the preview JPEG has a 53-byte thumbnail at JpegOffset 196, and both sides look for its frame header from file offset 196 (see the note below). The port stops at byte 198, on a length of 0, in all four. C++ adds the 0 and keeps walking:

  • RAF A (preview JPEG at 1,024): C++ reads the RAF header's zeros as segments of length 0, two bytes per step, hops through the CFA header at 512, reads the bytes 00 BB at 837 as the length 187 and lands on the preview JPEG at 1,024, whose frame header says 1920×1280. That is above 512, so C++ stores the image as the preview: the preview reports 1920×1280 and the thumbnail is left empty.
  • RAF B: the same walk, but the bytes 00 E7 at 1,003 (length 231) land C++ on the frame header of the real thumbnail, which starts at 1,232: 160×120.
  • RW2 A (preview JPEG at 504): C++ walks on (lengths 74 and 228) and lands on the preview JPEG at 504, whose frame header says 1920×1440, so it again stores the image as the preview. Rw2GetPreviewData then replaces the preview with the RW2's own, so only the empty thumbnail shows.
  • RW2 B: C++ lands on the frame header of a copy of the thumbnail's JPEG, at 331, and reads 160×120.
# C++ Rust
1 RAF A, preview 1920×1280, 379 bytes at 1,024 0×0, 379 bytes at 1,024
2 RAF B, thumbnail 160×120, 53 bytes at 356 0×0, 53 bytes at 356
3 RW2 A, thumbnail none (length 0) 0×0, 53 bytes at 712
4 RW2 B, thumbnail 160×120, 53 bytes at 712 0×0, 53 bytes at 712

Note: both sides read the thumbnail's frame header at its JpegOffset as a file offset (C++, Rust), while JpegOffset counts from the Exif's TIFF header, here at 1,036 in the RAFs and 516 in the RW2s. That is the same on both sides, so this PR leaves it alone.

Question

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

  1. RAF A, preview width: should GetPreviewImageData's preview.width give 1920 (like the C++ library), or give 0 (like the Rust port)?
  2. RAF B, thumbnail width: should thumbnail.width give 160 (like the C++ library), or give 0 (like the Rust port)?
  3. RW2 A, thumbnail length: should thumbnail.length give 0 (like the C++ library), or give 53 (like the Rust port)?
  4. RW2 B, thumbnail width: should thumbnail.width give 160 (like the C++ library), or give 0 (like the Rust port)?

This PR implements C++'s behaviour.

Fix

One change covers all four:

                     let length = self.read_u16_be(offset)?;
-                    if length < 2 {
-                        return Err(TiffError::CorruptedEntry { offset });
-                    }
                     offset = offset

The examples above then match C++. Replayed before and after:

# C++ Rust main Rust patched
1 RAF A, preview 1920×1280 0×0 1920×1280
2 RAF B, thumbnail 160×120 0×0 160×120
3 RW2 A, thumbnail none (length 0) 0×0, 53 bytes at 712 none (length 0)
4 RW2 B, thumbnail 160×120 0×0 160×120

Notes for review:

  • The campaigns kept 791 RAFs and 1,386 RW2s that differ this way. With this PR, C++ and the port agree on all of them.
  • NEF goes through the same function: NefGetPreviewData (C++, Rust) reads the preview's size with it and returns kUnsupported when it fails, so this PR also changes a NEF whose preview JPEG has such a segment.
  • One test changes: test_get_jpeg_dimensions_corrupted_cases expected the error for a length of 1. It now gets the read error past the end of its 14 bytes, where C++ stops too. The port's tests pass (44 + 174).

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