Skip to content

fix: xmp jpeg write #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

fix: xmp jpeg write #1156

wants to merge 8 commits into from

Conversation

cdmurph32
Copy link
Collaborator

Changes in this pull request

Adding the manifest url to large XMP segments could cause the XMP data
to exceed the limit of 65503 bytes.

There were 3 causes of this issue:

  1. Extra whitespace from formatting. The quick_xml writer shiftwidth
    could be much larger than that of the original. Potentially adding
    several KB of whitespace.
  2. Manifest url added multiple times. The manifest url was added to
    multiple keys within the XMP data.
  3. Lack of padding awareness. According to the spec, XMP segments should
    have 2KB to 4KB of padding. The padding should be adjusted to keep
    the XMP data length the same with the addition of the remote manifest
    url. This is important for data hashes as it keeps the offsets of the
    JPEG metadata the same.

Checklist

  • This PR represents a single feature, fix, or change.
  • All applicable changes have been documented.
  • Any TO DO items (or similar) have been entered as GitHub issues and the link to that issue has been included in a comment.

Adding the manifest url to large XMP segments could cause the XMP data
to exceed the limit of 65503 bytes.

There were 3 causes of this issue:
1. Extra whitespace from formatting. The quick_xml writer shiftwidth
   could be much larger than that of the original. Potentially adding
   several KB of whitespace.
2. Manifest url added multiple times. The manifest url was added to
   multiple keys within the XMP data.
3. Lack of padding awareness. According to the spec, XMP segments should
   have 2KB to 4KB of padding. The padding should be adjusted to keep
   the XMP data length the same with the addition of the remote manifest
   url. This is important for data hashes as it keeps the offsets of the
   JPEG metadata the same.
Copy link

codecov bot commented Jun 16, 2025

Codecov Report

Attention: Patch coverage is 97.91667% with 1 line in your changes missing coverage. Please review.

Project coverage is 79.31%. Comparing base (47df42e) to head (3fa75eb).

Files with missing lines Patch % Lines
sdk/src/utils/xmp_inmemory_utils.rs 97.87% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1156      +/-   ##
==========================================
+ Coverage   79.29%   79.31%   +0.02%     
==========================================
  Files         147      147              
  Lines       40701    40743      +42     
==========================================
+ Hits        32274    32316      +42     
  Misses       8427     8427              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cdmurph32 cdmurph32 changed the title Fix xmp jpeg write Fix: xmp jpeg write Jun 16, 2025
@cdmurph32 cdmurph32 changed the title Fix: xmp jpeg write fix: xmp jpeg write Jun 16, 2025
@@ -620,7 +620,7 @@ impl RemoteRefEmbed for JpegIO {
.unwrap_or((None, MIN_XMP));

// add provenance and JPEG XMP prefix
let xmp = format!("{XMP_SIGNATURE}\0 {}", add_provenance(xmp, &manifest_uri)?);
let xmp = format!("{XMP_SIGNATURE}\0{}", add_provenance(xmp, &manifest_uri)?);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra space caused there to be a leading space in the XMP data

// Minimal padding should be 2 KB to 4 KB. This is used if no XMP packet is found.
let mut target_length = 4096;
// Remove trailing xpacket if present for easier manipulation
let xpacket_end_single = "<?xpacket end='w'?>";
Copy link
Collaborator Author

@cdmurph32 cdmurph32 Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single quotes are what the spec has, but we specifically test for double quotes. The test images from cameras also use double quotes.
<?xpacket end="w"?>

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be more flexible. You may also encounter end='r'.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we could be a good citizen and respect the 'r' which means read only.

@@ -130,13 +149,14 @@ fn add_xmp_key(xmp: &str, key: &str, value: &str) -> Result<String> {
if !added {
// didn't exist, so add it
elem.push_attribute((key, value));
added = true;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of added = true is why the remote manifests url was being added multiple times.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this down!

)]
#[cfg_attr(target_os = "wasi", wstd::test)]
async fn test_broken_xmp_read_write_stream() {
let source_bytes = include_bytes!("../../tests/fixtures/broken.jpg");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename the file?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not our file so I don't think we can include it in a unit test.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's too bad. It would be cool to have a picture from summit in the repo. :-).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can ask Jen if we can use it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to keep our repo images small, this one is over 6mb.

let xpacket_end_double = "<?xpacket end=\"w\"?>";
let xmp_body = if let Some(pos) = xmp.rfind(xpacket_end_single) {
target_length = orig_length - xpacket_end_single.len();
xmp[..pos].trim_end()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I commented above, I would just search for "<?xpacket" since the rest of the tag may vary.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely need to be flexible reading here. I've also found cases where the end just wasn't there and other tools think it is fine.

Copy link
Collaborator Author

@cdmurph32 cdmurph32 Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the end is completely missing then I can't just look for <?xpacket since that could be the beginning. The test add_xmp does this.

xmp[..pos].trim_end()
} else if let Some(pos) = xmp.rfind(xpacket_end_double) {
target_length = orig_length - xpacket_end_double.len();
xmp[..pos].trim_end()
Copy link
Collaborator

@mauricefisher64 mauricefisher64 Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change I suggested about removes the else if case.

@@ -177,6 +198,8 @@ fn add_xmp_key(xmp: &str, key: &str, value: &str) -> Result<String> {
}
}
}
let padding_length = target_length.saturating_sub(writer.get_ref().get_ref().len());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this take into account the existing padding to keep the length the same if possible? This seems like it would not take into account the existing padding. Is there somewhere you account to the existing padding?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Line 107

target_length = orig_length - xpacket_end_length;

@mauricefisher64 mauricefisher64 requested a review from gpeacock June 17, 2025 14:02
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.

3 participants