c2pa-cpp
C++ API for the C2PA SDK
Loading...
Searching...
No Matches
c2pa::Reader Class Reference

Reader class for reading a manifest. More...

#include <c2pa.hpp>

Public Member Functions

 Reader (IContextProvider &context, const std::string &format, std::istream &stream)
 Create a Reader from a context and stream.
 
 Reader (IContextProvider &context, const std::filesystem::path &source_path)
 Create a Reader from a context and file path.
 
 Reader (std::shared_ptr< IContextProvider > context, const std::string &format, std::istream &stream)
 Create a Reader from a shared context and stream.
 
 Reader (std::shared_ptr< IContextProvider > context, std::istream &stream)
 Create a Reader from a shared context and stream, guessing the format from content.
 
 Reader (std::shared_ptr< IContextProvider > context, const std::filesystem::path &source_path)
 Create a Reader from a shared context and file path.
 
 Reader (std::shared_ptr< IContextProvider > context, const std::string &format, std::istream &image_stream, const std::vector< uint8_t > &manifest_jumbf)
 Create a Reader from a shared context, image stream, and external JUMBF manifest.
 
 Reader (const std::string &format, std::istream &stream)
 Create a Reader from a stream (will use global settings if any loaded).
 
 Reader (const std::filesystem::path &source_path)
 Create a Reader from a file path (will use global settings if any loaded).
 
 Reader (const Reader &)=delete
 
Readeroperator= (const Reader &)=delete
 
 Reader (Reader &&other) noexcept
 
Readeroperator= (Reader &&other) noexcept
 
 ~Reader ()
 
bool is_embedded () const
 Check if the reader was created from an embedded manifest.
 
std::optional< std::string > remote_url () const
 Returns the remote url of the manifest if this Reader obtained the manifest remotely.
 
Readerwith_fragment (const std::string &format, std::istream &stream, std::istream &fragment)
 Process a BMFF fragment stream with this Reader instance.
 
std::string json () const
 Get the manifest as a JSON string.
 
std::string detailed_json () const
 Get the manifest as a detailed JSON string.
 
std::string crjson () const
 Get the manifest store as a pretty-printed crJSON string.
 
int64_t get_resource (const std::string &uri, const std::filesystem::path &path)
 Get a resource from the reader and write it to a file.
 
int64_t get_resource (const std::string &uri, std::ostream &stream)
 Get a resource from the reader and write it to an output stream.
 
C2paReader * get_api_internal_raw_reader () const
 Get the raw C2paReader pointer.
 

Static Public Member Functions

static std::optional< Readerfrom_asset (IContextProvider &context, const std::filesystem::path &source_path)
 Try to open a Reader from a context and file path when the asset may lack C2PA data.
 
static std::optional< Readerfrom_asset (IContextProvider &context, const std::string &format, std::istream &stream)
 Try to create a Reader from a context and stream when the asset may lack C2PA data.
 
static std::optional< Readerfrom_asset (std::shared_ptr< IContextProvider > context, const std::filesystem::path &source_path)
 Try to open a Reader from a shared context and file path when the asset may lack C2PA data.
 
static std::optional< Readerfrom_asset (std::shared_ptr< IContextProvider > context, const std::string &format, std::istream &stream)
 Try to create a Reader from a shared context and stream when the asset may lack C2PA data.
 
static std::optional< Readerfrom_asset (std::shared_ptr< IContextProvider > context, std::istream &stream)
 Try to create a Reader from a shared context and stream, guessing the format from content.
 
static std::vector< std::string > supported_mime_types ()
 Get a list of mime types that the SDK can read manifests from.
 

Detailed Description

Reader class for reading a manifest.

This class is used to read and validate a manifest from a stream or file. Resources are managed using RAII; member order ensures cpp_stream (which holds a C stream pointing at the ifstream) is destroyed before owned_stream.

Constructor & Destructor Documentation

◆ Reader() [1/10]

c2pa::Reader::Reader ( IContextProvider context,
const std::string &  format,
std::istream &  stream 
)

Create a Reader from a context and stream.

Parameters
contextContext provider; used at construction to configure settings.
formatThe mime format of the stream.
streamThe input stream to read from.
Exceptions
C2paExceptionif context.is_valid() returns false, or for other errors encountered by the C2PA library.
Deprecated:
Use Reader(std::shared_ptr<IContextProvider>, format, stream) instead. The reference overload does not extend the lifetime of the context, which can be problematic when progress callbacks fire after the context is destroyed.

◆ Reader() [2/10]

c2pa::Reader::Reader ( IContextProvider context,
const std::filesystem::path &  source_path 
)

Create a Reader from a context and file path.

Parameters
contextContext provider; used at construction only to configure settings.
source_pathThe path to the file to read.
Exceptions
C2paExceptionif context.is_valid() returns false, or for other errors encountered by the C2PA library.
Note
Prefer using the streaming APIs if possible.
Deprecated:
Use Reader(std::shared_ptr<IContextProvider>, source_path) instead. The reference overload does not extend the lifetime of the context, which can be problematic when progress callbacks fire after the context is destroyed.

◆ Reader() [3/10]

c2pa::Reader::Reader ( std::shared_ptr< IContextProvider context,
const std::string &  format,
std::istream &  stream 
)

Create a Reader from a shared context and stream.

The Reader retains a shared reference to the context, keeping it alive for the lifetime of the Reader.

Parameters
contextShared context provider.
formatThe mime type or file extension of the stream. Treated as a hint and reconciled against the container detected in the leading bytes: the same container keeps format (so "dng" is not widened to "image/tiff"), a different one wins over format, and if nothing is detected format is used as given. Pass an empty string to rely on detection alone. A format absent from supported_mime_types() is not rejected here.
streamThe input stream to read from. Must support seeking; it is rewound before inspection.
Exceptions
C2paExceptionif context is null or context->is_valid() returns false, or if no container is detected and format does not name a supported one.

◆ Reader() [4/10]

c2pa::Reader::Reader ( std::shared_ptr< IContextProvider context,
std::istream &  stream 
)

Create a Reader from a shared context and stream, guessing the format from content.

Equivalent to passing an empty format. Detection is best effort; prefer the overload taking a format when the format is known. The stream is rewound before inspection, so it must support seeking.

Parameters
contextShared context provider.
streamThe input stream to read from. Must support seeking.
Exceptions
C2paExceptionif context is null, context->is_valid() returns false, or the container type cannot be determined from the bytes.

◆ Reader() [5/10]

c2pa::Reader::Reader ( std::shared_ptr< IContextProvider context,
const std::filesystem::path &  source_path 
)

Create a Reader from a shared context and file path.

The Reader retains a shared reference to the context, keeping it alive for the lifetime of the Reader.

Parameters
contextShared context provider.
source_pathThe path to the file to read.
Exceptions
C2paExceptionif context is null or context->is_valid() returns false.
Note
The extension is used as a hint only. With no extension, the container is guessed from the leading bytes (best-effort). A wrong or unrecognized extension is accepted, since a detected container overrides it.

◆ Reader() [6/10]

c2pa::Reader::Reader ( std::shared_ptr< IContextProvider context,
const std::string &  format,
std::istream &  image_stream,
const std::vector< uint8_t > &  manifest_jumbf 
)

Create a Reader from a shared context, image stream, and external JUMBF manifest.

Performs full C2PA binding verification between image_stream and the provided JUMBF manifest bytes without requiring the manifest to be embedded in the asset. Typical use: sidecar .c2pa files, database-stored manifests, in-memory pipelines. The Reader retains a shared reference to the context for its lifetime.

Parameters
contextShared context provider (trust anchors, verification policy).
formatMIME type of the image stream (e.g., "image/jpeg").
image_streamAsset data. Must support seeking (used for exclusion-range hashing).
manifest_jumbfRaw JUMBF manifest bytes (e.g., contents of a .c2pa sidecar file).
Note
image_stream, manifest_jumbf, and format need only remain valid for the duration of the construction call. The Reader does not retain their references.
Exceptions
C2paExceptionif context is null, context->is_valid() is false, manifest_jumbf is empty, or the C2PA library reports an error.

◆ Reader() [7/10]

c2pa::Reader::Reader ( const std::string &  format,
std::istream &  stream 
)

Create a Reader from a stream (will use global settings if any loaded).

The validation_status field in the JSON contains validation results.

Parameters
formatThe mime format of the stream.
streamThe input stream to read from.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.
Deprecated:
Use Reader(IContextProvider& context, format, stream) instead.

◆ Reader() [8/10]

c2pa::Reader::Reader ( const std::filesystem::path &  source_path)

Create a Reader from a file path (will use global settings if any loaded).

Parameters
source_pathThe path to the file to read.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.
Deprecated:
Use Reader(IContextProvider& context, source_path) instead.
Note
Prefer using the streaming APIs if possible.

◆ Reader() [9/10]

c2pa::Reader::Reader ( const Reader )
delete

◆ Reader() [10/10]

c2pa::Reader::Reader ( Reader &&  other)
inlinenoexcept
924 : c2pa_reader(std::exchange(other.c2pa_reader, nullptr)),
925 owned_stream(std::move(other.owned_stream)),
926 cpp_stream(std::move(other.cpp_stream)),
927 context_ref(std::move(other.context_ref)) {
928 }

◆ ~Reader()

c2pa::Reader::~Reader ( )

Member Function Documentation

◆ crjson()

std::string c2pa::Reader::crjson ( ) const

Get the manifest store as a pretty-printed crJSON string.

crJSON is a standardized JSON format for C2PA manifest data. This call is infallible at yields valid empty JSON ("{}") if there are no Content Credentials.

Returns
The manifest store as a crJSON string.
Exceptions
C2paExceptionif the underlying C call returns null.

◆ detailed_json()

std::string c2pa::Reader::detailed_json ( ) const

Get the manifest as a detailed JSON string.

The detailed view more closely resembles the underlying JUMBF manifest store. Unlike json(), it exposes the full assertion_store map for each manifest, including assertions that the regular view filters out (such as c2pa.thumbnail.*, c2pa.actions.*, and c2pa.hash.*). Use this when building redaction URIs or otherwise needing the canonical assertion labels embedded in the manifest.

Returns
The detailed manifest as a JSON string.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.

◆ from_asset() [1/5]

static std::optional< Reader > c2pa::Reader::from_asset ( IContextProvider context,
const std::filesystem::path &  source_path 
)
static

Try to open a Reader from a context and file path when the asset may lack C2PA data.

Returns
A Reader if JUMBF (c2pa/manifest) data is present; std::nullopt if none.
Exceptions
C2paExceptionfor errors other than a missing manifest (e.g. invalid asset).
std::system_errorif the file cannot be opened.
Deprecated:
Use from_asset(std::shared_ptr<IContextProvider>, source_path) instead. The reference overload does not extend the lifetime of the context, which can cause a use-after-free crash when progress callbacks fire after the context is destroyed.

◆ from_asset() [2/5]

static std::optional< Reader > c2pa::Reader::from_asset ( IContextProvider context,
const std::string &  format,
std::istream &  stream 
)
static

Try to create a Reader from a context and stream when the asset may lack C2PA data.

Returns
A Reader if JUMBF (c2pa/manifest) data is present; std::nullopt if none.
Exceptions
C2paExceptionfor errors other than a missing manifest.
Deprecated:
Use from_asset(std::shared_ptr<IContextProvider>, format, stream) instead. The reference overload does not extend the lifetime of the context, which can cause a use-after-free crash when progress callbacks fire after the context is destroyed.

◆ from_asset() [3/5]

static std::optional< Reader > c2pa::Reader::from_asset ( std::shared_ptr< IContextProvider context,
const std::filesystem::path &  source_path 
)
static

Try to open a Reader from a shared context and file path when the asset may lack C2PA data.

The Reader retains a shared reference to the context if C2PA data is found.

Returns
A Reader if JUMBF (c2pa/manifest) data is present; std::nullopt if none.
Exceptions
C2paExceptionfor errors other than a missing manifest.

◆ from_asset() [4/5]

static std::optional< Reader > c2pa::Reader::from_asset ( std::shared_ptr< IContextProvider context,
const std::string &  format,
std::istream &  stream 
)
static

Try to create a Reader from a shared context and stream when the asset may lack C2PA data.

The Reader retains a shared reference to the context if C2PA data is found.

Parameters
contextShared context provider.
formatThe mime type or file extension of the stream, used as a hint and reconciled against the detected container. See the corresponding constructor.
streamThe input stream to read from. Must support seeking.
Returns
A Reader if JUMBF (c2pa/manifest) data is present; std::nullopt if none.
Exceptions
C2paExceptionfor errors other than a missing manifest.

◆ from_asset() [5/5]

static std::optional< Reader > c2pa::Reader::from_asset ( std::shared_ptr< IContextProvider context,
std::istream &  stream 
)
static

Try to create a Reader from a shared context and stream, guessing the format from content.

The Reader retains a shared reference to the context if C2PA data is found. Detection is best effort; prefer the overload taking a format when it is known.

Parameters
contextShared context provider.
streamThe input stream to read from. Must support seeking.
Returns
A Reader if JUMBF (c2pa/manifest) data is present; std::nullopt if none.
Exceptions
C2paExceptionfor errors other than a missing manifest. Content whose container cannot be identified raises an error rather than returning nullopt.

◆ get_api_internal_raw_reader()

C2paReader * c2pa::Reader::get_api_internal_raw_reader ( ) const
inline

Get the raw C2paReader pointer.

Returns
The raw C2paReader pointer.
Note
This is intended for internal API use and compatibility with C APIs.
1022{ return c2pa_reader; }

◆ get_resource() [1/2]

int64_t c2pa::Reader::get_resource ( const std::string &  uri,
const std::filesystem::path &  path 
)

Get a resource from the reader and write it to a file.

Parameters
uriThe URI of the resource.
pathThe file path to write the resource to.
Returns
The number of bytes written.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.
Note
Prefer using the streaming APIs if possible.

◆ get_resource() [2/2]

int64_t c2pa::Reader::get_resource ( const std::string &  uri,
std::ostream &  stream 
)

Get a resource from the reader and write it to an output stream.

Parameters
uriThe URI of the resource.
streamThe output stream to write the resource to.
Returns
The number of bytes written.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.

◆ is_embedded()

bool c2pa::Reader::is_embedded ( ) const
inline

Check if the reader was created from an embedded manifest.

Returns
true if the manifest was embedded in the asset, false if external.
Exceptions
C2paExceptionif the Reader holds no valid handle (e.g. it was moved from, or a prior with_fragment() failed and consumed it), or for other errors encountered by the C2PA library.
948 {
949 ensure_initialized();
950 return c2pa_reader_is_embedded(c2pa_reader);
951 }

◆ json()

std::string c2pa::Reader::json ( ) const

Get the manifest as a JSON string.

Returns
The manifest as a JSON string.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.

◆ operator=() [1/2]

Reader & c2pa::Reader::operator= ( const Reader )
delete

◆ operator=() [2/2]

Reader & c2pa::Reader::operator= ( Reader &&  other)
inlinenoexcept
930 {
931 if (this != &other) {
932 c2pa_free(c2pa_reader);
933 c2pa_reader = std::exchange(other.c2pa_reader, nullptr);
934 owned_stream = std::move(other.owned_stream);
935 cpp_stream = std::move(other.cpp_stream);
936 context_ref = std::move(other.context_ref);
937 }
938 return *this;
939 }

◆ remote_url()

std::optional< std::string > c2pa::Reader::remote_url ( ) const

Returns the remote url of the manifest if this Reader obtained the manifest remotely.

Returns
Optional string containing the remote URL, or std::nullopt if manifest was embedded.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.

◆ supported_mime_types()

static std::vector< std::string > c2pa::Reader::supported_mime_types ( )
static

Get a list of mime types that the SDK can read manifests from.

Returns
Vector of supported MIME type strings.

◆ with_fragment()

Reader & c2pa::Reader::with_fragment ( const std::string &  format,
std::istream &  stream,
std::istream &  fragment 
)

Process a BMFF fragment stream with this Reader instance.

Used for fragmented BMFF media (DASH/HLS streaming) where content is split into an init/main segment and separate fragment files. The Reader is created from the init segment (e.g. Reader(context, "video/mp4", init)), then goes through one fragment at a time via this method. The underlying native SDK consumes the current reader and returns a new one configured with the fragment. This method swaps the handle in place and returns *this for chaining across fragments as the reading steps go through fragments.

Parameters
formatMIME type of the media (e.g., "video/mp4").
streamThe main/init segment.
fragmentThe current fragment to process.
Returns
*this, for chaining across multiple fragments.
Note
stream and fragment need only remain valid for the duration of this call. The Reader does not retain a reference to either after returning.
Exceptions
C2paExceptionif the C2PA library reports an error. On failure the underlying reader handle is consumed and this Reader must not be used further.

The documentation for this class was generated from the following file: