c2pa-rs

A fork of https://github.com/contentauth/c2pa-rs/
git clone git://archive.git.mtrnord.blog/mtrnords-photography-manager/c2pa-rs.git
Log | Files | Refs | README

commit 279e10b3de447fff978b941b54b5a80e5f31876b
parent 1c6cd7f4061eb00ecc1d5f81114680ecb61eacde
Author: mauricefisher64 <92736594+mauricefisher64@users.noreply.github.com>
Date:   Mon, 21 Aug 2023 15:37:16 -0400

Error saving stream writes (#290)

* Wasm embedding error example

* Fix stream saves hash generation failure.

* Remove unneeded padding function

---------

Co-authored-by: Dave Kozma <dkozma@adobe.com>
Diffstat:
Msdk/src/asset_handlers/riff_io.rs | 16++--------------
Msdk/src/manifest.rs | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msdk/src/store.rs | 2+-
Msdk/src/utils/test.rs | 2++
Asdk/tests/fixtures/mars.webp | 0
5 files changed, 68 insertions(+), 15 deletions(-)

diff --git a/sdk/src/asset_handlers/riff_io.rs b/sdk/src/asset_handlers/riff_io.rs @@ -212,12 +212,7 @@ where // place at the end for maximum compatibility if is_riff_chunk && !data.is_empty() { - let mut d = data.to_vec(); - if d.len() % 2 == 1 { - // must be even - d.push(0); - } - children_contents.push(ChunkContents::Data(C2PA_CHUNK_ID, d)); + children_contents.push(ChunkContents::Data(C2PA_CHUNK_ID, data.to_vec())); } Ok(ChunkContents::Children(id, chunk_type, children_contents)) @@ -271,14 +266,7 @@ impl CAIReader for RiffIO { for c in top_level_chunks.iter(&mut chunk_reader) { if c.id() == C2PA_CHUNK_ID { - let mut output = c.read_contents(&mut chunk_reader)?; - // the data may have been padded to account for even boundary requirement - if let Some(last_byte) = output.last() { - if *last_byte == 0 { - output.pop(); - } - } - return Ok(output); + return Ok(c.read_contents(&mut chunk_reader)?); } } diff --git a/sdk/src/manifest.rs b/sdk/src/manifest.rs @@ -1729,6 +1729,43 @@ pub(crate) mod tests { println!("It worked: {manifest_store}\n"); } + #[cfg_attr(not(target_arch = "wasm32"), actix::test)] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + async fn test_embed_webp_stream_wasm() { + use crate::assertions::User; + let image = include_bytes!("../tests/fixtures/mars.webp"); + // convert buffer to cursor with Read/Write/Seek capability + + let mut manifest = Manifest::new("my_app".to_owned()); + manifest.set_title("EmbedStream"); + manifest + .add_assertion(&User::new( + "org.contentauth.mylabel", + r#"{"my_tag":"Anything I want"}"#, + )) + .unwrap(); + + let signer = temp_remote_signer(); + + // Embed a manifest using the signer. + let (out_vec, _out_manifest) = manifest + .embed_from_memory_remote_signed("image/webp", image, signer.as_ref()) + .await + .expect("embed_stream"); + + // try to load the image + let manifest_store = + crate::ManifestStore::from_bytes("image/webp", &out_vec, true).unwrap(); + + /* to be enabled later + // try to load the manifest + let mut validation_log = DetailedStatusTracker::new(); + Store::from_jumbf(&out_manifest, &mut validation_log).expect("manifest_load_error"); + */ + + println!("It worked: {manifest_store}\n"); + } + #[test] fn test_embed_stream() { use crate::assertions::User; @@ -2094,6 +2131,32 @@ pub(crate) mod tests { assert_eq!(format, "image/jpeg"); } + #[cfg(feature = "file_io")] + #[test] + fn test_embed_webp_from_json() { + use crate::utils::test::TEST_WEBP; + + let mut fixtures = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); + fixtures.push("tests/fixtures"); // the path we want to read files from + + let temp_dir = tempdir().expect("temp dir"); + let output = temp_fixture_path(&temp_dir, TEST_WEBP); + + let signer = temp_signer(); + + let mut manifest = Manifest::from_json(MANIFEST_JSON).expect("from_json"); + manifest.with_base_path(fixtures).expect("with_base"); + manifest + .embed(&output, &output, signer.as_ref()) + .expect("embed"); + + let manifest_store = crate::ManifestStore::from_file(&output).expect("from_file"); + println!("{manifest_store}"); + let active_manifest = manifest_store.get_active().unwrap(); + let (format, _) = active_manifest.thumbnail().unwrap(); + assert_eq!(format, "image/jpeg"); + } + #[test] #[cfg(feature = "file_io")] fn test_create_file_based_ingredient() { diff --git a/sdk/src/store.rs b/sdk/src/store.rs @@ -2084,7 +2084,7 @@ impl Store { &mut intermediate_stream, pc.alg(), &mut hash_ranges, - true, + false, )? }; diff --git a/sdk/src/utils/test.rs b/sdk/src/utils/test.rs @@ -39,6 +39,8 @@ use crate::{openssl::RsaSigner, signer::ConfigurableSigner}; pub const TEST_SMALL_JPEG: &str = "earth_apollo17.jpg"; +pub const TEST_WEBP: &str = "mars.webp"; + pub const TEST_VC: &str = r#"{ "@context": [ "https://www.w3.org/2018/credentials/v1", diff --git a/sdk/tests/fixtures/mars.webp b/sdk/tests/fixtures/mars.webp Binary files differ.