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 1b553e0d959cf88742e4635d0ff8cedb175029ae
parent 6f91da0c3add42d0d77494bc57701dbd9e6aa4e9
Author: Gavin  Peacock <gpeacock@adobe.com>
Date:   Wed,  8 Jun 2022 15:05:24 -0700

Make most jumbf_io functions crate private and move Store dependencies to Store (#37)

Move Store dependencies to store
Diffstat:
Msdk/src/jumbf_io.rs | 75+++++++++++----------------------------------------------------------------
Msdk/src/store.rs | 40++++++++++++++++++++++++++++++++++++----
2 files changed, 47 insertions(+), 68 deletions(-)

diff --git a/sdk/src/jumbf_io.rs b/sdk/src/jumbf_io.rs @@ -11,15 +11,17 @@ // specific language governing permissions and limitations under // each license. -use std::fs; -use std::io::Cursor; -use std::path::{Path, PathBuf}; - -use crate::asset_handlers::{c2pa_io::C2paIO, jpeg_io::JpegIO, png_io::PngIO}; -use crate::asset_io::{AssetIO, CAILoader, HashObjectPositions}; -use crate::error::{Error, Result}; -use crate::status_tracker::StatusTracker; -use crate::store::Store; +use crate::{ + asset_handlers::{c2pa_io::C2paIO, jpeg_io::JpegIO, png_io::PngIO}, + asset_io::{AssetIO, CAILoader, HashObjectPositions}, + error::{Error, Result}, +}; + +use std::{ + fs, + io::Cursor, + path::{Path, PathBuf}, +}; static SUPPORTED_TYPES: &[&str; 6] = &[ "c2pa", // stand-alone manifest file @@ -44,20 +46,6 @@ pub fn load_jumbf_from_memory(asset_type: &str, data: &[u8]) -> Result<Vec<u8>> Ok(cai_block) } -/// Return Store from in memory asset -pub fn load_cai_from_memory( - asset_type: &str, - data: &[u8], - validation_log: &mut impl StatusTracker, -) -> Result<Store> { - load_jumbf_from_memory(asset_type, data).and_then(|cai_block| { - // load and validate with CAI toolkit and dump if desired - Store::from_jumbf(&cai_block, validation_log) - }) -} - -// TODO [scouten]: Find a cleaner way to opt in or out of PDF IO. -#[cfg(not(target_arch = "wasm32"))] pub fn get_assetio_handler(ext: &str) -> Option<Box<dyn AssetIO>> { match ext { "c2pa" => Some(Box::new(C2paIO {})), @@ -67,28 +55,6 @@ pub fn get_assetio_handler(ext: &str) -> Option<Box<dyn AssetIO>> { } } -#[cfg(target_arch = "wasm32")] -pub fn get_assetio_handler(ext: &str) -> Option<Box<dyn AssetIO>> { - match ext { - "c2pa" => Some(Box::new(C2paIO {})), - "jpg" | "jpeg" => Some(Box::new(JpegIO {})), - "png" => Some(Box::new(PngIO {})), - _ => None, - } -} - -// TODO [scouten]: Find a cleaner way to opt in or out of PDF IO. -#[cfg(not(target_arch = "wasm32"))] -pub fn get_cailoader_handler(asset_type: &str) -> Option<Box<dyn CAILoader>> { - match asset_type { - "c2pa" | "application/c2pa" => Some(Box::new(C2paIO {})), - "jpg" | "jpeg" | "image/jpeg" => Some(Box::new(JpegIO {})), - "png" | "image/png" => Some(Box::new(PngIO {})), - _ => None, - } -} - -#[cfg(target_arch = "wasm32")] pub fn get_cailoader_handler(asset_type: &str) -> Option<Box<dyn CAILoader>> { match asset_type { "c2pa" | "application/c2pa" => Some(Box::new(C2paIO {})), @@ -184,25 +150,6 @@ pub fn load_jumbf_from_file(in_path: &Path) -> Result<Vec<u8>> { } } -/// load a CAI store from a file -/// -/// in_path - path to source file -/// validation_log - optional vec to contain addition info about the asset -pub fn load_cai_from_file( - in_path: &Path, - validation_log: &mut impl StatusTracker, -) -> Result<Store> { - // get jumbf block - load_jumbf_from_file(in_path).and_then(|buffer| { - if buffer.is_empty() { - return Err(Error::JumbfNotFound); - } - - // load and validate with CAI toolkit and dump if desired - Store::from_jumbf(&buffer, validation_log) - }) -} - pub fn object_locations(in_path: &Path) -> Result<Vec<HashObjectPositions>> { let ext = get_file_extension(in_path).ok_or(Error::UnsupportedType)?; diff --git a/sdk/src/store.rs b/sdk/src/store.rs @@ -18,7 +18,7 @@ use crate::{ error::{Error, Result}, hash_utils::{hash_by_alg, vec_compare, verify_by_alg}, jumbf::{self, boxes::*}, - jumbf_io::{get_cailoader_handler, load_cai_from_memory}, + jumbf_io::{get_cailoader_handler, load_jumbf_from_memory}, status_tracker::{log_item, OneShotStatusTracker, StatusTracker}, validation_status, xmp_inmemory_utils::extract_provenance, @@ -33,7 +33,7 @@ use crate::{ cose_validator::verify_cose, embedded_xmp, jumbf_io::{ - get_supported_file_extension, load_cai_from_file, object_locations, save_jumbf_to_file, + get_supported_file_extension, load_jumbf_from_file, object_locations, save_jumbf_to_file, }, utils::{ hash_utils::{hash256, Exclusion}, @@ -1457,6 +1457,38 @@ impl Store { Ok(()) } + /// Return Store from in memory asset + pub fn load_cai_from_memory( + asset_type: &str, + data: &[u8], + validation_log: &mut impl StatusTracker, + ) -> Result<Store> { + load_jumbf_from_memory(asset_type, data).and_then(|cai_block| { + // load and validate with CAI toolkit and dump if desired + Store::from_jumbf(&cai_block, validation_log) + }) + } + + /// load a CAI store from a file + /// + /// in_path - path to source file + /// validation_log - optional vec to contain addition info about the asset + #[cfg(feature = "file_io")] + pub fn load_cai_from_file( + in_path: &Path, + validation_log: &mut impl StatusTracker, + ) -> Result<Store> { + // get jumbf block + load_jumbf_from_file(in_path).and_then(|buffer| { + if buffer.is_empty() { + return Err(Error::JumbfNotFound); + } + + // load and validate with CAI toolkit and dump if desired + Store::from_jumbf(&buffer, validation_log) + }) + } + /// Load Store from claims in an existing asset /// asset_path: path to input asset /// verify: determines whether to verify the contents of the provenance claim. Must be set true to use validation_log @@ -1468,7 +1500,7 @@ impl Store { validation_log: &mut impl StatusTracker, ) -> Result<Store> { // load jumbf if available - load_cai_from_file(asset_path, validation_log) + Self::load_cai_from_file(asset_path, validation_log) .and_then(|mut store| { // verify the store if verify { @@ -1502,7 +1534,7 @@ impl Store { let xmp = cai_loader.read_xmp(&mut buf_reader); // load jumbf if available - load_cai_from_memory(asset_type, data, validation_log) + Self::load_cai_from_memory(asset_type, data, validation_log) .map(|store| (store, xmp)) .map_err(|e| { let err = match e {