Skip to content

Commit 19f1a3e

Browse files
committed
feat: add supports_stream to check if byte stream is supported by parser
1 parent f9f59b2 commit 19f1a3e

File tree

11 files changed

+101
-5
lines changed

11 files changed

+101
-5
lines changed

sdk/src/asset_handlers/bmff_io.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,15 @@ impl AssetIO for BmffIO {
16401640
fn supported_types(&self) -> &[&str] {
16411641
&SUPPORTED_TYPES
16421642
}
1643+
1644+
fn supports_stream(&self, stream: &mut dyn CAIRead) -> Result<bool> {
1645+
stream.rewind()?;
1646+
1647+
let mut header = [0u8; 12];
1648+
stream.read_exact(&mut header)?;
1649+
1650+
Ok(header[4..8] == *b"ftyp")
1651+
}
16431652
}
16441653

16451654
impl CAIWriter for BmffIO {

sdk/src/asset_handlers/c2pa_io.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ impl AssetIO for C2paIO {
128128
&SUPPORTED_TYPES
129129
}
130130

131+
fn supports_stream(&self, _stream: &mut dyn CAIRead) -> Result<bool> {
132+
// TODO: complex
133+
Ok(true)
134+
}
135+
131136
fn composed_data_ref(&self) -> Option<&dyn ComposedManifestRef> {
132137
Some(self)
133138
}

sdk/src/asset_handlers/gif_io.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,15 @@ impl AssetIO for GifIO {
379379
fn supported_types(&self) -> &[&str] {
380380
&["gif", "image/gif"]
381381
}
382+
383+
fn supports_stream(&self, stream: &mut dyn CAIRead) -> Result<bool> {
384+
stream.rewind()?;
385+
386+
let mut header = [0u8; 6];
387+
stream.read_exact(&mut header)?;
388+
389+
Ok(header == *b"GIF87a" || header == *b"GIF89a")
390+
}
382391
}
383392

384393
impl GifIO {

sdk/src/asset_handlers/jpeg_io.rs

Lines changed: 10 additions & 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, Write},
17+
io::{BufReader, Cursor, SeekFrom, Write},
1818
path::*,
1919
};
2020

@@ -568,6 +568,15 @@ impl AssetIO for JpegIO {
568568
fn supported_types(&self) -> &[&str] {
569569
&SUPPORTED_TYPES
570570
}
571+
572+
fn supports_stream(&self, stream: &mut dyn CAIRead) -> Result<bool> {
573+
stream.rewind()?;
574+
575+
let mut header = [0u8; 3];
576+
stream.read_exact(&mut header)?;
577+
578+
Ok(header == [0xFF, 0xD8, 0xFF])
579+
}
571580
}
572581

573582
impl RemoteRefEmbed for JpegIO {

sdk/src/asset_handlers/mp3_io.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ impl AssetIO for Mp3IO {
344344
fn supported_types(&self) -> &[&str] {
345345
&SUPPORTED_TYPES
346346
}
347+
fn supports_stream(&self, _stream: &mut dyn CAIRead) -> Result<bool> {
348+
// TODO: complex
349+
Ok(true)
350+
}
347351
}
348352

349353
impl CAIWriter for Mp3IO {

sdk/src/asset_handlers/pdf_io.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
// specific language governing permissions and limitations under
1212
// each license.
1313

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

1616
use crate::{
1717
asset_handlers::pdf::{C2paPdf, Pdf},
1818
asset_io::{AssetIO, CAIRead, CAIReader, CAIWriter, ComposedManifestRef, HashObjectPositions},
19-
Error,
20-
Error::{JumbfNotFound, NotImplemented, PdfReadError},
19+
Error::{self, JumbfNotFound, NotImplemented, PdfReadError},
2120
};
2221

2322
static SUPPORTED_TYPES: [&str; 2] = ["pdf", "application/pdf"];
@@ -109,6 +108,15 @@ impl AssetIO for PdfIO {
109108
&SUPPORTED_TYPES
110109
}
111110

111+
fn supports_stream(&self, stream: &mut dyn CAIRead) -> crate::Result<bool> {
112+
stream.rewind()?;
113+
114+
let mut header = [0u8; 5];
115+
stream.read_exact(&mut header)?;
116+
117+
Ok(header == *b"%PDF-")
118+
}
119+
112120
fn composed_data_ref(&self) -> Option<&dyn ComposedManifestRef> {
113121
Some(self)
114122
}

sdk/src/asset_handlers/png_io.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ fn read_string(asset_reader: &mut dyn CAIRead, max_read: u32) -> Result<String>
196196

197197
Ok(String::from_utf8_lossy(&s).to_string())
198198
}
199+
199200
pub struct PngIO {}
200201

201202
impl CAIReader for PngIO {
@@ -569,6 +570,15 @@ impl AssetIO for PngIO {
569570
fn supported_types(&self) -> &[&str] {
570571
&SUPPORTED_TYPES
571572
}
573+
574+
fn supports_stream(&self, stream: &mut dyn CAIRead) -> Result<bool> {
575+
stream.rewind()?;
576+
577+
let mut header = [0u8; 8];
578+
stream.read_exact(&mut header)?;
579+
580+
Ok(header == [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])
581+
}
572582
}
573583

574584
fn get_xmp_insertion_point(asset_reader: &mut dyn CAIRead) -> Option<(u64, u32)> {

sdk/src/asset_handlers/riff_io.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ impl AssetIO for RiffIO {
386386
fn supported_types(&self) -> &[&str] {
387387
&SUPPORTED_TYPES
388388
}
389+
390+
fn supports_stream(&self, stream: &mut dyn CAIRead) -> Result<bool> {
391+
stream.rewind()?;
392+
393+
let mut header = [0u8; 4];
394+
stream.read_exact(&mut header)?;
395+
396+
// TODO: if we want to detect the subtype we can do extra parsing on the next few bytes
397+
Ok(header == *b"RIFF")
398+
}
389399
}
390400

391401
impl CAIWriter for RiffIO {

sdk/src/asset_handlers/svg_io.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ impl AssetIO for SvgIO {
159159
fn supported_types(&self) -> &[&str] {
160160
&SUPPORTED_TYPES
161161
}
162+
163+
fn supports_stream(&self, _stream: &mut dyn CAIRead) -> Result<bool> {
164+
// TODO: complex
165+
Ok(true)
166+
}
162167
}
163168

164169
// create manifest entry

sdk/src/asset_handlers/tiff_io.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,7 @@ where
13321332

13331333
asset_reader.read_to_vec(xmp_ifd_entry.value_count).ok()
13341334
}
1335+
13351336
pub struct TiffIO {}
13361337

13371338
impl CAIReader for TiffIO {
@@ -1426,6 +1427,24 @@ impl AssetIO for TiffIO {
14261427
fn supported_types(&self) -> &[&str] {
14271428
&SUPPORTED_TYPES
14281429
}
1430+
1431+
fn supports_stream(&self, stream: &mut dyn CAIRead) -> Result<bool> {
1432+
stream.rewind()?;
1433+
1434+
let mut header = [0u8; 4];
1435+
stream.read_exact(&mut header)?;
1436+
1437+
Ok(
1438+
// Little-endian TIFF
1439+
header == [0x49, 0x49, 0x2A, 0x00]
1440+
// Big-endian TIFF
1441+
|| header == [0x4D, 0x4D, 0x00, 0x2A]
1442+
// Little-endian BigTIFF
1443+
|| header == [0x49, 0x49, 0x2B, 0x00]
1444+
// Big-endian BigTIFF
1445+
|| header == [0x4D, 0x4D, 0x00, 0x2B],
1446+
)
1447+
}
14291448
}
14301449

14311450
impl CAIWriter for TiffIO {

0 commit comments

Comments
 (0)