Skip to main content

Structure

Struct Structure 

Source
pub struct Structure { /* private fields */ }
Expand description

A Structure holds geometry, and whatever opaque state its container attached to it (see attachment). It is Send + Sync and can be shared across threads for parallel reads.

Implementations§

Source§

impl Structure

Source

pub fn embedded_assets(&self) -> impl Iterator<Item = &Item>

Embedded files (ItemKind::EmbeddedAsset items) in declaration order: items the format declares to be files of a media type, such as HEIF jpeg items and mime items. HEIF uri items, coded thumbnails and other opaque items are ItemKind::Other and not listed here, though scan_embedded finds those that turn out to be files. EXIF-internal thumbnails and MPF sub-assets are separate APIs.

Source

pub fn scan_embedded( &self, src: &mut dyn ReadSeek, registry: &HandlerRegistry, limits: ScanLimits, ) -> Result<Vec<EmbeddedAsset>>

Recursively scan embedded files using registry, in depth-first order. Ordinary parsing remains shallow.

Every embedded asset is reported, with its parse error retained in EmbeddedAsset::structure if it has one. In addition, each opaque item (ItemKind::Other of Encoding::Unknown content, stored as is) is reported if registry recognises its first DETECT_LEN bytes; one it does not recognise is skipped silently and counts against no limit. Apple’s HEIF uri item holding a processed JPEG is found this way. A URI is an identifier only: it is never fetched. This does not carve file headers out of the middle of payloads.

Items stored with a transfer encoding other than Identity (a compressed HEIF mime item) are not decoded here: such an embedded asset is reported with Error::UnsupportedFeature as its structure, and its reader gives the stored bytes; such an opaque item is not considered at all.

Resource-budget and extent-mapping errors fail the entire scan. External HEIF references are never followed. No pixel decoding is performed.

Source§

impl Structure

Source

pub fn parse( registry: &HandlerRegistry, src: &mut dyn ReadSeek, format: Option<&str>, ) -> Result<Structure>

Parse a complete stream with blocking I/O.

Content detection takes precedence. If no handler recognizes the content, Some(format) selects a handler by extension or MIME type; with None, parsing fails. Format selection does not validate the content; the selected handler’s parser does that.

The stream’s length is determined by seeking to its end. Offsets are relative to the beginning of the stream, regardless of its initial position. The stream may be repositioned on success or error.

Source

pub fn parse_task<'a>( registry: &'a HandlerRegistry, total: u64, format: Option<&'a str>, ) -> Task<'a, Structure>

Parse a stream of total bytes as a caller-driven task.

Format selection follows parse. Selection and parsing errors are returned when the task is driven, not when it is created. The task borrows registry and format; to build a registry-independent task for a known handler, use Task::new with ContainerHandler::parse_async.

Source

pub async fn parse_async( registry: &HandlerRegistry, io: &mut Io, format: Option<&str>, ) -> Result<Structure>

Parse through io inside a larger sans-I/O computation.

Format selection follows parse. This does no I/O itself: a Task driver supplies requested bytes. The selected handler takes an owned Io::fork, leaving io available after parsing. The fork has a separate byte window, so detection bytes may be requested again by the parser.

Source§

impl Structure

Source

pub fn write_plan<'a>( &self, src: &mut dyn ReadSeek, updates: &'a Updates, ) -> Result<WritePlan<'a>>

The plan for writing this asset with updates; see WritePlan. src must be the stream this structure was parsed from. The container’s handler builds it, and reports Error::UnsupportedFormat if it describes its writes some other way.

An XMP or C2PA value that updates sets must appear in the plan’s output; if the handler cannot place it the plan is Error::UnsupportedItem.

Source§

impl Structure

Source

pub fn write_plan_task<'f, 'a: 'f>( &'f self, updates: &'a Updates, ) -> Task<'f, WritePlan<'a>>

A task that builds the plan for writing this asset with updates, for a caller that does its own I/O; see Task. The task reads the source (a stream of total_size bytes), which it asks for as it needs it. The result is the plan write_plan returns.

Source§

impl Structure

Source

pub fn trailing(&self) -> Option<ByteRange>

The bytes beyond the end of the container’s own structure, up to the end of the file: data after the last chunk or box, and, in a multi-image file, everything after the primary image. None when there are none, and for containers that do not report it.

A box hash lists any boxes among them (the later images of a Multi-Picture Format JPEG) as they are, and unnamed bytes at the end as one final box (C2PA 2.4, 18.7.2); see complete_boxes. A hash of one part of a multi-asset file covers that part only and never includes them.

Source

pub fn subassets(&self) -> &[SubAsset]

The sub-assets of the asset, in file order: contiguous, non-overlapping, and covering every byte. A file that embeds nothing else has one, the whole file. A Multi-Picture Format JPEG has one per image, plus one for any data after the last image.

This is geometry only. Whether to hash the sub-assets separately (a C2PA multi-asset hash) or as one asset is the caller’s decision.

Source

pub fn container_id(&self) -> &str

The container’s id, for example "jpeg".

Source

pub fn media_type(&self) -> &str

Media type, for example image/jpeg.

Source

pub fn box_name( &self, segment: SegmentId, element: &Segment, ) -> Option<Cow<'static, str>>

The container-defined box name of element, or None if the element is not a named box. segment identifies the owning segment in this asset; element may be a finer element reported by elements. Container knowledge, including parsed naming context, lives in the handler.

Source

pub fn total_size(&self) -> u64

Asset size in bytes.

Source

pub fn segments(&self) -> &[Segment]

All segments in file order.

Source

pub fn segment(&self, id: SegmentId) -> Result<&Segment>

Look up a segment.

Source

pub fn elements<F>(&self, f: F) -> Result<()>
where F: FnMut(SegmentId, &Segment) -> Result<()>,

Every element of the file in order: each segment, with segments that subdivide replaced by their finer elements. The elements tile the file exactly as the segments do.

f receives the owning segment’s id and the element. For most segments the element is the segment itself. The finer elements were found when the asset was parsed, so nothing is read. The same call works for every container; callers never test the format.

Source

pub fn boxes(&self) -> Result<Vec<BoxInfo>>

The asset’s boxes in file order, covering every byte once. Reads nothing.

Elements are named by box_name. An element that continues the preceding box is combined with it, such as a GIF application’s data sub-blocks. Unnamed bytes are retained with BoxInfo::name set to None, including trailing data.

Unlike segments (writer-sized units) and elements (the finest tiling), boxes describe the container’s grouping of those elements.

Source

pub fn boxes_with(&self, item: &Item, name: &str) -> Result<Vec<BoxInfo>>

boxes, grouping the segments carrying item under name.

The item must belong to this structure. Each contiguous run of its segments becomes one box, including framing and any other bytes in those segments. Noncontiguous runs remain separate boxes with the same name. All other boxes keep their container-defined names. Reads nothing.

Source

pub fn regions(&self) -> impl Iterator<Item = Region> + '_

Every header/payload/trailer run of the file, in order, covering all bytes once.

Source

pub fn items(&self) -> impl Iterator<Item = &Item> + '_

Every item, in declaration order.

Source

pub fn items_of(&self, kind: ItemKind) -> impl Iterator<Item = &Item> + '_

All items of a kind.

Source

pub fn items_labelled<'a>( &'a self, kind: ItemKind, label: &'a str, ) -> impl Iterator<Item = &'a Item> + 'a

All items of a kind carrying label (see Item::label).

Source

pub fn items_in(&self, id: SegmentId) -> impl Iterator<Item = &Item> + '_

The items with an extent in segment id: what the segment holds.

Source

pub fn walk<R, F>(&self, src: &mut R, f: F) -> Result<()>
where R: Read + Seek, F: FnMut(&Node<'_>) -> Result<()>,

Visit the container’s element hierarchy in file order, parents first.

Flat containers yield their segments. Nested ones (ISO BMFF) also yield the elements inside segments, found lazily by reading only their headers. Callers never test the format; the handler decides what the hierarchy is.

Source

pub fn item(&self, kind: ItemKind) -> Result<Option<&Item>>

The item of a kind: Ok(None) if absent, Err(Ambiguous) if several.

Source

pub fn require_item(&self, kind: ItemKind) -> Result<&Item>

Like item but absence is an error.

Source

pub fn supports_named_boxes(&self) -> bool

Whether the container’s own structure can be represented entirely by named boxes. Unnamed trailing bytes do not affect the result.

Returns false for an empty structure, unnamed boxes within it, or elements that cannot be assembled into boxes. Derived from the generic boxes view, including subdivisions and continuations; this does not select a hashing or signing scheme.

Source

pub fn c2pa_data_hash_exclusions(&self, item: &Item) -> Result<Vec<ByteRange>>

The ranges a C2PA data hash (c2pa.hash.data) excludes when item is the embedded manifest store.

The container’s C2PA embedding rules decide whether framing, checksums, padding or only stored payload bytes are excluded; see ContainerHandler::c2pa_data_hash_exclusions. Results are ordered, nonempty, disjoint, nonadjacent and within the asset. Works on parsed and planned structures without reading the source.

The item must be a C2PA item of this structure, usually its c2pa_item. This neither interprets the manifest nor computes a hash. Containers whose C2PA embedding uses another binding (ISO BMFF) return Error::UnsupportedFeature.

Source

pub fn read_range<R: Read + Seek>( &self, src: &mut R, range: ByteRange, ) -> Result<Vec<u8>>

Read a range into memory. Fails above MAX_SEGMENT_SIZE; use stream_range for large ranges.

Source

pub async fn read_item_async(&self, io: &mut Io, item: &Item) -> Result<Vec<u8>>

read_item, reading through io.

Source

pub async fn read_content_async( &self, io: &mut Io, item: &Item, ) -> Result<Vec<u8>>

read_content, reading through io.

Source

pub fn xmp(&self, src: &mut dyn ReadSeek) -> Result<Option<Vec<u8>>>

The asset’s XMP packet, or None if it has none. Containers that store XMP in several pieces return it reassembled.

Source

pub fn c2pa_item(&self) -> Result<Option<&Item>>

The C2PA manifest store item, or None if the asset has none. Several stores are Error::Ambiguous.

When the store is kept as an original plus an update (ISO BMFF), this is the original; c2pa_store reads the two combined. A PDF that also holds the stores of earlier revisions (labelled superseded) gives the latest, the active one. The item is what a caller excludes from a hash or patches in place. Needs no I/O: the container identified the store while parsing.

Source

pub fn c2pa_store<R: Read + Seek>(&self, src: &mut R) -> Result<Option<Vec<u8>>>

The C2PA manifest store as a reader sees it, or None if the asset has none: the c2pa_item with any transfer encoding undone, an update appended to its original (ISO BMFF), and the stores of earlier revisions read before it (PDF). The store is not interpreted.

Source

pub async fn c2pa_store_async(&self, io: &mut Io) -> Result<Option<Vec<u8>>>

c2pa_store, reading through io, for a Task where the caller does the reads.

Source

pub fn patch_c2pa_store<W: Read + Write + Seek>( &self, out: &mut W, store: &[u8], ) -> Result<()>

Fill the C2PA store’s placeholder in place: the counterpart of c2pa_store for patch_exact.

store is the whole store, as a reader sees it and as Updates::set_c2pa takes it. Where the file holds that store in parts (an ISO BMFF original and update, the stores of a PDF’s revisions), only the active part is patched, with what store adds to the parts before it. Those must be unchanged, byte for byte: a patch cannot rewrite earlier manifests, so a store that does not extend them is Error::UnsupportedFeature. The patched length must match exactly, as for patch_exact. out must contain the asset starting at offset 0.

Source

pub fn prepare_c2pa_patch<R: Read + Seek>( &self, src: &mut R, store: &[u8], ) -> Result<C2paPatch<'_>>

Check a patch_c2pa_store against the asset in src, without writing, and return it ready to apply to a copy of src.

For a caller that writes its output to another stream: if this fails, the store does not fit the placeholder, so the asset must be written instead.

Source

pub fn c2pa_boxes(&self) -> Result<Vec<BoxInfo>>

The boxes a C2PA box hash (c2pa.hash.boxes, C2PA 18.7) lists: complete_boxes with the C2PA manifest store as one box named C2PA and bytes after the structure as one named c2pa.after. Works without a store (to plan one) and on planned structures; reads nothing. Fails as complete_boxes does, and as c2pa_item does for several stores.

Source

pub fn complete_boxes( &self, group: Option<(&Item, &str)>, trailing_name: &str, ) -> Result<Vec<BoxInfo>>

Every byte of the asset as named boxes, in file order, each byte once: the generic boxes view with no unnamed box left.

With group, the segments of that item are one box with that name, as boxes_with gives them. Unnamed bytes past the end of the container’s own structure (data appended to a file) are one last box named trailing_name. Boxes past the structure that are named (later images of a Multi-Picture Format JPEG) are listed as they are.

An element of the structure without a box name, or a named box after the trailing bytes, is Error::UnsupportedFeature: the asset cannot be described entirely by named boxes. Reads nothing.

Source

pub fn read_item<R: Read + Seek + ?Sized>( &self, src: &mut R, item: &Item, ) -> Result<Vec<u8>>

Read an item’s extents, concatenated: the bytes stored in the file.

Source

pub fn read_content<R: Read + Seek + ?Sized>( &self, src: &mut R, item: &Item, ) -> Result<Vec<u8>>

Read an item’s content: read_item, then the item’s transfer encoding undone. For most items the two are the same.

Source

pub fn stream_range<R, F>( &self, src: &mut R, range: ByteRange, buf: &mut [u8], f: F, ) -> Result<()>
where R: Read + Seek, F: FnMut(&[u8]) -> Result<()>,

Stream a range through a caller-owned buffer; no allocation.

Source

pub fn slice<'a>(&self, data: &'a [u8], range: ByteRange) -> Result<&'a [u8]>

Borrow range from data, which must hold the whole asset starting at offset 0. No copy is made. data is only length-checked: it is the caller’s job to pass the bytes this structure was parsed from.

Source

pub fn slice_item<'a>( &self, data: &'a [u8], item: &Item, ) -> Result<Cow<'a, [u8]>>

An item’s bytes from data. Borrowed, with no copy, when the item has a single extent; copied (up to MAX_SEGMENT_SIZE) when it has several.

Source

pub fn read<R: Read + Seek>( &self, src: &mut R, sink: &mut dyn Sink, ) -> Result<()>

Deliver every byte of the asset to sink, in file order, tagged with its segment and role: the read-side counterpart of write. Runs the sink would skip (Sink::wants) are not read. Chunks are at most DEFAULT_CHUNK_SIZE bytes and borrow one reused buffer.

Source

pub fn read_slice(&self, data: &[u8], sink: &mut dyn Sink) -> Result<()>

Like read over bytes already in memory (a memory map, say). Nothing is copied: each region reaches the sink as one chunk borrowed from data. A sink that wants parallelism can split the chunks itself.

Source

pub fn patch<W: Read + Write + Seek>( &self, out: &mut W, item: &Item, data: &[u8], ) -> Result<()>

Overwrite an item’s bytes in an already-written stream.

data must fit the item’s total capacity; the remainder is zero-filled so the structure is unchanged. Container checksums that cover the patched payload (PNG CRC) are recomputed. out must contain the asset starting at offset 0.

Source

pub fn write( &self, src: &mut dyn ReadSeek, out: &mut dyn Write, updates: &Updates, sink: &mut dyn Sink, ) -> Result<Structure>

Write this asset to out with updates applied, delivering every output byte to sink, and return the structure of what was written. src must be the stream this structure was parsed from. out is only written to, from its current position. The container’s handler does the work; callers never name the format.

Source

pub fn plan( &self, src: &mut dyn ReadSeek, updates: &Updates, ) -> Result<Structure>

The structure write would produce for updates, without writing: where each item would land, and the total size. src must be the stream this structure was parsed from.

The result is the one write returns, so a caller can learn where a manifest will go (and size a hash exclusion for it) before producing any output. A container that cannot plan reports Error::UnsupportedFormat.

Source

pub fn patch_exact<W: Read + Write + Seek>( &self, out: &mut W, item: &Item, data: &[u8], ) -> Result<()>

Fill a placeholder: overwrite an item’s bytes, in place, with data of exactly the same length.

This is how a signed manifest replaces the placeholder it was sized from. Bytes outside the payload (box sizes, offsets, everything a hash or an offset table was computed over) are not touched, so none of them can move. Unlike patch, which zero-fills a shorter data, a length mismatch is an error: it means the placeholder was not sized correctly.

Writing the store again with set_c2pa is not a substitute: that is free to change the size.

data is the content, not what is stored: an item with a transfer encoding (SVG’s base64) has data encoded first, and it is the encoded length that must match.

Source§

impl Structure

Source

pub fn embedded_thumbnail<R: Read + Seek>( &self, src: &mut R, ) -> Result<Option<Thumbnail>>

The asset’s embedded thumbnail, if it has one.

An item the container records as a thumbnail (one with a Thumbnail relation, from a HEIF thmb reference) is preferred, whatever its kind. Its media type is the one the container reports for it (see Item::media_type): image/jpeg for a jpeg item, the content type of a mime item. For a coded item (hvc1, av01, …) it is None, and data is the coded item, which needs the codec configuration stored elsewhere in the file to decode. Failing that, with the exif feature, the JPEG thumbnail in the EXIF data (IFD1) is returned.

Source§

impl Structure

EXIF access for any container that reports ItemKind::Exif items (JPEG APP1, PNG eXIf, HEIF Exif). The item starts at the TIFF header.

Source

pub fn exif_info<R: Read + Seek>(&self, src: &mut R) -> Result<Option<ExifInfo>>

Available on crate feature exif only.

Basic EXIF fields from the first EXIF item, or None if there is none or it is not valid TIFF.

Trait Implementations§

Source§

impl Debug for Structure

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.