Skip to content
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

fix: 622: Allow for multiple data exclusions, minimyzing where appropriate #623

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e4a0db1
Allow for multiple data exclusions, minimyzing where appropriate
brogdonm Oct 8, 2024
75317c3
Add the unit test in, showing the logic working
brogdonm Oct 9, 2024
6d21d2c
Fix formatting.
brogdonm Oct 9, 2024
01a54a0
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Oct 15, 2024
3a0283d
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Oct 16, 2024
0c88c8a
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Oct 17, 2024
2f191c8
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Oct 18, 2024
1f8d0f1
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Oct 28, 2024
36ce6f0
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Oct 29, 2024
c198564
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 1, 2024
bf820a5
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 6, 2024
dc29e66
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 7, 2024
523f861
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 11, 2024
ef2e55e
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 14, 2024
0a62581
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 15, 2024
8a84c83
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 15, 2024
88e2d6c
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 18, 2024
f6ea43f
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 21, 2024
40dea6e
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Nov 25, 2024
8d48963
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Dec 4, 2024
4bfd6f2
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Dec 10, 2024
c6da4df
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Dec 11, 2024
9263e4a
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Dec 12, 2024
f6f446f
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Dec 12, 2024
cc5c5ac
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Dec 17, 2024
56541b4
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Dec 17, 2024
170a9b0
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Dec 20, 2024
9f98886
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Jan 6, 2025
eb0a98f
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Jan 13, 2025
e58133c
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Jan 16, 2025
92decbd
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Jan 21, 2025
43a3d10
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Jan 23, 2025
1d76ce5
Merge branch 'main' into fix/C2PA-542/622/multipleDataHashExclusions
brogdonm Feb 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 91 additions & 28 deletions sdk/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,50 +1566,54 @@ impl Store {
// sort blocks by offset
block_locations.sort_by(|a, b| a.offset.cmp(&b.offset));

// generate default data hash that excludes jumbf block
// find the first jumbf block (ours are always in order)
// find the first block after the jumbf blocks
let mut block_start: usize = 0;
let mut block_end: usize = 0;
let mut found_jumbf = false;
// Setup to assume fragmented CAI blocks
let mut exclusions = Vec::<(usize, usize)>::new();
for item in block_locations {
// find start of jumbf
if !found_jumbf && item.htype == HashBlockObjectType::Cai {
block_start = item.offset;
found_jumbf = true;
}

// find start of block after jumbf blocks
if found_jumbf && item.htype == HashBlockObjectType::Cai {
block_end = item.offset + item.length;
if item.htype == HashBlockObjectType::Cai {
// Make sure we have a valid range
if item.offset <= (item.offset + item.length) {
// If we are calculating hashes, avoid adding an
// exclusion if the CAI block is beyond the end of the
// stream. Some asset handlers will inject a
// placeholder for the CAI block at the end of the
// stream before the stream itself has the block.
if !calc_hashes || (item.offset + item.length) as u64 <= stream_len {
let mut exclusion = (item.offset, item.offset + item.length);
// Setup to de-fragment sections that are contiguous but may have
// been listed as separate
if let Some(last_exclusion) = exclusions.last() {
// If the last exclusion ends where this one starts,
// merge them
if last_exclusion.1 == exclusion.0 {
exclusion.0 = last_exclusion.0;
exclusions.pop();
}
}
exclusions.push(exclusion);
}
}
}
}

if found_jumbf {
if !exclusions.is_empty() {
// add exclusion hash for bytes before and after jumbf
let mut dh = DataHash::new("jumbf manifest", alg);

if calc_hashes {
if block_end > block_start && (block_end as u64) <= stream_len {
dh.add_exclusion(HashRange::new(block_start, block_end - block_start));
for exclusion in &exclusions {
if exclusion.1 > exclusion.0 {
dh.add_exclusion(HashRange::new(exclusion.0, exclusion.1 - exclusion.0));
}
}

// this check is only valid on the final sized asset
//
// a case may occur where there is no existing manifest in the stream and the
// asset handler creates a placeholder beyond the length of the stream
if block_end as u64 > stream_len + (block_end - block_start) as u64 {
if calc_hashes {
if exclusions.iter().any(|x| x.1 as u64 > stream_len) {
return Err(Error::BadParam(
"data hash exclusions out of range".to_string(),
));
}

dh.gen_hash_from_stream(stream)?;
} else {
if block_end > block_start {
dh.add_exclusion(HashRange::new(block_start, block_end - block_start));
}

match alg {
"sha256" => dh.set_hash([0u8; 32].to_vec()),
"sha384" => dh.set_hash([0u8; 48].to_vec()),
Expand Down Expand Up @@ -5869,6 +5873,65 @@ pub mod tests {
assert!(errors.is_empty());
}

#[test]
fn test_generate_data_hashes_for_stream_multiple_exclusions() {
// Setup the test data
let mut data = vec![0u8; 100];
// And wrap in a cursor to treat it like a stream for the API
let mut stream = Cursor::new(&mut data);
let alg = "sha256";

// Return a total of three blocked locations, all of which report they are
// CAI blocks, which should be excluded from the data hash
let mut block_locations = vec![
HashObjectPositions {
offset: 0,
length: 42,
htype: HashBlockObjectType::Cai,
},
// This one and the next one should be merged into a single exclusion
HashObjectPositions {
offset: 80,
length: 10,
htype: HashBlockObjectType::Cai,
},
HashObjectPositions {
offset: 90,
length: 10,
htype: HashBlockObjectType::Cai,
},
];
let calc_hashes = true;

// Generate the data hash
let data_hash_result = Store::generate_data_hashes_for_stream(
&mut stream,
alg,
&mut block_locations,
calc_hashes,
);
// Which should have executed without issue
assert!(data_hash_result.is_ok());
// Grab the actual data hash object
let data_hash = data_hash_result.unwrap();

// Which should have a single entry
assert_eq!(1, data_hash.len());
let data_hash_0 = &data_hash[0];
// And it should have exclusions specified
assert!(data_hash_0.exclusions.is_some());
// Grab the exclusions
let exclusions = data_hash_0.exclusions.as_ref().unwrap();
// Should be a totale of 2 exclusions to catch the de-fragmented data
assert_eq!(2, exclusions.len());
// And the first exclusion should match what we specified
assert_eq!(0, exclusions[0].start());
assert_eq!(42, exclusions[0].length());
// And the second exclusion should be the last two blocks
assert_eq!(80, exclusions[1].start());
assert_eq!(20, exclusions[1].length());
}

#[test]
#[cfg(feature = "file_io")]
fn test_datahash_embeddable_manifest() {
Expand Down