Skip to content

Commit 04cb96e

Browse files
committed
fix: rewind stream when reading c2pa_io and add source to incorrect format error
1 parent 6b464b8 commit 04cb96e

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

sdk/src/asset_handlers/c2pa_io.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub struct C2paIO {}
3333

3434
impl CAIReader for C2paIO {
3535
fn read_cai(&self, asset_reader: &mut dyn CAIRead) -> Result<Vec<u8>> {
36+
asset_reader.rewind()?;
37+
3638
let mut cai_data = Vec::new();
3739
// read the whole file
3840
asset_reader.read_to_end(&mut cai_data)?;

sdk/src/asset_handlers/jpeg_io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use std::{
1515
collections::HashMap,
1616
fs::File,
17-
io::{BufReader, Cursor, SeekFrom, Write},
17+
io::{BufReader, Cursor, Write},
1818
path::*,
1919
};
2020

sdk/src/asset_handlers/pdf_io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// specific language governing permissions and limitations under
1212
// each license.
1313

14-
use std::{fs::File, io::SeekFrom, path::Path};
14+
use std::{fs::File, path::Path};
1515

1616
use crate::{
1717
asset_handlers::pdf::{C2paPdf, Pdf},

sdk/src/error.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,12 @@ pub enum Error {
228228
#[error("thumbnail format {0} is unsupported")]
229229
UnsupportedThumbnailFormat(String),
230230

231-
#[error("the specified stream is not of format {0}")]
232-
IncorrectFormat(String),
231+
#[error("the specified stream is not of format {format}")]
232+
IncorrectFormat {
233+
format: String,
234+
#[source]
235+
source: Option<Box<dyn std::error::Error + Send + Sync>>,
236+
},
233237

234238
#[error("`trust.signer_info` is missing from settings")]
235239
MissingSignerSettings,

sdk/src/jumbf_io.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,24 @@ pub(crate) fn is_bmff_format(asset_type: &str) -> bool {
104104
/// [Error::IncorrectFormat].
105105
pub fn check_stream_supported(asset_type: &str, stream: &mut dyn CAIRead) -> Result<()> {
106106
match get_assetio_handler(asset_type) {
107-
Some(asset_handler) => {
108-
if !asset_handler.supports_stream(stream)? {
109-
Err(Error::IncorrectFormat(asset_type.to_owned()))
110-
} else {
111-
Ok(())
112-
}
113-
}
114-
None => Err(Error::IncorrectFormat(asset_type.to_owned())),
107+
Some(asset_handler) => match asset_handler.supports_stream(stream) {
108+
// Failed to check if asset supports the stream.
109+
Err(err) => Err(Error::IncorrectFormat {
110+
format: asset_type.to_owned(),
111+
source: Some(Box::new(err)),
112+
}),
113+
// Did not fail, although the asset doesn't support the stream.
114+
Ok(false) => Err(Error::IncorrectFormat {
115+
format: asset_type.to_owned(),
116+
source: None,
117+
}),
118+
// Stream supported.
119+
Ok(true) => Ok(()),
120+
},
121+
None => Err(Error::IncorrectFormat {
122+
format: asset_type.to_owned(),
123+
source: None,
124+
}),
115125
}
116126
}
117127

@@ -530,8 +540,12 @@ pub mod tests {
530540
.unwrap();
531541
removed.set_position(0);
532542
let result = load_jumbf_from_stream(asset_type, &mut removed);
533-
if (asset_type != "wav")
534-
&& (asset_type != "avi" && asset_type != "mp3" && asset_type != "webp")
543+
// For c2pa this is expected to return `Error::IncorrectFormat`.
544+
if asset_type != "wav"
545+
&& asset_type != "avi"
546+
&& asset_type != "mp3"
547+
&& asset_type != "webp"
548+
&& asset_type != "c2pa"
535549
{
536550
assert!(matches!(&result.err().unwrap(), Error::JumbfNotFound));
537551
}

sdk/src/store.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5797,11 +5797,14 @@ pub mod tests {
57975797
&mut report,
57985798
&Settings::default(),
57995799
);
5800-
assert!(matches!(result, Err(Error::IncorrectFormat(_))));
5800+
assert!(matches!(result, Err(Error::IncorrectFormat { .. })));
58015801
println!("Error report: {report:?}");
58025802
assert!(!report.logged_items().is_empty());
58035803

5804-
assert!(report.has_error(Error::IncorrectFormat(format.to_owned())));
5804+
assert!(report.has_error(Error::IncorrectFormat {
5805+
format: format.to_owned(),
5806+
source: None
5807+
}));
58055808
}
58065809

58075810
#[test]

0 commit comments

Comments
 (0)