24#pragma GCC diagnostic push
25#pragma GCC diagnostic ignored "-Wunused-function"
31#pragma warning(disable : 4505)
80 errno =
static_cast<int>(e);
109 const char*
what() const noexcept override;
112 std::
string message_;
168 [[nodiscard]] virtual C2paContext*
c_context() const noexcept = 0;
176 [[nodiscard]] virtual
bool is_valid() const noexcept = 0;
204 Settings(
const std::string& data,
const std::string& format);
226 Settings&
set(const std::
string& path, const std::
string& json_value);
248 C2paSettings* settings_ptr;
451 C2paContextBuilder* context_builder;
490 [[nodiscard]] C2paContext*
c_context() const noexcept override;
495 [[nodiscard]]
bool is_valid() const noexcept override;
524 C2paContext* context;
574 template <
typename IStream>
576 static_assert(std::is_base_of<std::istream, IStream>::value,
577 "Stream must be derived from std::istream");
579 c_stream = c2pa_create_stream(
reinterpret_cast<StreamContext *
>(
static_cast<std::istream *
>(&istream)), reader, seeker, writer, flusher);
581 throw C2paException(
"Failed to create input stream wrapper: is stream open and valid?");
601 static intptr_t reader(StreamContext *context, uint8_t *buffer, intptr_t size);
608 static intptr_t writer(StreamContext *context,
const uint8_t *buffer, intptr_t size);
615 static intptr_t seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence);
620 static intptr_t flusher(StreamContext *context);
634 template <
typename OStream>
636 static_assert(std::is_base_of<std::ostream, OStream>::value,
"Stream must be derived from std::ostream");
638 c_stream = c2pa_create_stream(
reinterpret_cast<StreamContext *
>(
static_cast<std::ostream *
>(&ostream)), reader, seeker, writer, flusher);
640 throw C2paException(
"Failed to create output stream wrapper: is stream open and valid?");
660 static intptr_t reader(StreamContext *context, uint8_t *buffer, intptr_t size);
667 static intptr_t writer(StreamContext *context,
const uint8_t *buffer, intptr_t size);
674 static intptr_t seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence);
679 static intptr_t flusher(StreamContext *context);
691 template <
typename IOStream>
693 static_assert(std::is_base_of<std::iostream, IOStream>::value,
"Stream must be derived from std::iostream");
695 c_stream = c2pa_create_stream(
reinterpret_cast<StreamContext *
>(
static_cast<std::iostream *
>(&iostream)), reader, seeker, writer, flusher);
697 throw C2paException(
"Failed to create I/O stream wrapper: is stream open and valid?");
717 static intptr_t reader(StreamContext *context, uint8_t *buffer, intptr_t size);
724 static intptr_t writer(StreamContext *context,
const uint8_t *buffer, intptr_t size);
731 static intptr_t seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence);
736 static intptr_t flusher(StreamContext *context);
746 C2paReader *c2pa_reader =
nullptr;
747 std::unique_ptr<std::ifstream> owned_stream;
748 std::unique_ptr<CppIStream> cpp_stream;
749 std::shared_ptr<IContextProvider> context_ref;
751 void init_from_context(
IContextProvider& context,
const std::string &format, std::istream &stream);
752 void init_from_context(
IContextProvider& context,
const std::filesystem::path &source_path);
754 const std::string& format,
755 std::istream& image_stream,
756 const std::vector<uint8_t>& manifest_jumbf);
759 void ensure_initialized()
const {
760 if (c2pa_reader ==
nullptr) {
765 Reader() : c2pa_reader(
nullptr) {}
777 [[deprecated(
"Use Reader(std::shared_ptr<IContextProvider>, format, stream) instead. "
778 "The reference overload does not extend the lifetime of the context, which can "
779 "be problematic when progress callbacks fire after the context is destroyed.")]]
791 [[deprecated(
"Use Reader(std::shared_ptr<IContextProvider>, source_path) instead. "
792 "The reference overload does not extend the lifetime of the context, which can "
793 "be problematic when progress callbacks fire after the context is destroyed.")]]
810 Reader(std::shared_ptr<IContextProvider> context,
const std::string &format, std::istream &stream);
820 Reader(std::shared_ptr<IContextProvider> context, std::istream &stream);
831 Reader(std::shared_ptr<IContextProvider> context,
const std::filesystem::path &source_path);
847 Reader(std::shared_ptr<IContextProvider> context,
848 const std::string& format,
849 std::istream& image_stream,
850 const std::vector<uint8_t>& manifest_jumbf);
859 [[deprecated(
"Use Reader(std::shared_ptr<IContextProvider>, format, stream) instead. "
860 "Will be removed when c2pa-c moves to c2pa-rs 0.92.0.")]]
861 Reader(
const std::string &format, std::istream &stream);
869 [[deprecated(
"Use Reader(std::shared_ptr<IContextProvider>, source_path) instead. "
870 "Will be removed when c2pa-c moves to c2pa-rs 0.92.0.")]]
871 Reader(
const std::filesystem::path &source_path);
881 [[deprecated(
"Use from_asset(std::shared_ptr<IContextProvider>, source_path) instead. "
882 "The reference overload does not extend the lifetime of the context, which can "
883 "be problematic when progress callbacks fire after the context is destroyed.")]]
893 [[deprecated(
"Use from_asset(std::shared_ptr<IContextProvider>, format, stream) instead. "
894 "The reference overload does not extend the lifetime of the context, which can "
895 "be problematic when progress callbacks fire after the context is destroyed.")]]
902 static std::optional<Reader>
from_asset(std::shared_ptr<IContextProvider> context,
const std::filesystem::path &source_path);
912 static std::optional<Reader>
from_asset(std::shared_ptr<IContextProvider> context,
const std::string &format, std::istream &stream);
922 static std::optional<Reader>
from_asset(std::shared_ptr<IContextProvider> context, std::istream &stream);
930 : c2pa_reader(std::exchange(other.c2pa_reader,
nullptr)),
931 owned_stream(std::move(other.owned_stream)),
932 cpp_stream(std::move(other.cpp_stream)),
933 context_ref(std::move(other.context_ref)) {
937 if (
this != &other) {
938 c2pa_free(c2pa_reader);
939 c2pa_reader = std::exchange(other.c2pa_reader,
nullptr);
940 owned_stream = std::move(other.owned_stream);
941 cpp_stream = std::move(other.cpp_stream);
942 context_ref = std::move(other.context_ref);
955 ensure_initialized();
956 return c2pa_reader_is_embedded(c2pa_reader);
983 Reader&
with_fragment(
const std::string& format, std::istream& stream, std::istream& fragment);
1016 int64_t
get_resource(
const std::string &uri,
const std::filesystem::path &path);
1040 using SignerFunc = std::vector<unsigned char>(
const std::vector<unsigned char> &);
1061 C2paSigner* release() noexcept {
1062 return std::exchange(signer,
nullptr);
1068 static const char *validate_tsa_uri(
const std::string &tsa_uri);
1073 static const char *validate_tsa_uri(
const std::optional<std::string> &tsa_uri);
1082 Signer(
SignerFunc *callback, C2paSigningAlg alg,
const std::string &sign_cert,
const std::string &tsa_uri);
1086 Signer(C2paSigner *c_signer) : signer(c_signer) {
1098 Signer(
const std::string &alg,
const std::string &sign_cert,
const std::string &private_key,
const std::optional<std::string> &tsa_uri = std::nullopt);
1106 Signer(
Signer&& other) noexcept : signer(std::exchange(other.signer,
nullptr)) {
1110 if (
this != &other) {
1112 signer = std::exchange(other.signer,
nullptr);
1133 C2paBuilder *builder;
1134 std::shared_ptr<IContextProvider> context_ref;
1137 void init_from_context(
IContextProvider& context,
const std::string &manifest_json);
1147 [[deprecated(
"Use Builder(std::shared_ptr<IContextProvider>) instead. "
1148 "The reference overload does not extend the lifetime of the context, which can "
1149 "be problematic when progress callbacks fire after the context is destroyed.")]]
1160 [[deprecated(
"Use Builder(std::shared_ptr<IContextProvider>, manifest_json) instead. "
1161 "The reference overload does not extend the lifetime of the context, which can "
1162 "be problematic when progress callbacks fire after the context is destroyed.")]]
1171 explicit Builder(std::shared_ptr<IContextProvider> context);
1179 Builder(std::shared_ptr<IContextProvider> context,
const std::string &manifest_json);
1186 [[deprecated(
"Use Builder(std::shared_ptr<IContextProvider>, manifest_json) instead. "
1187 "Will be removed when c2pa-c moves to c2pa-rs 0.92.0.")]]
1200 : builder(std::exchange(other.builder,
nullptr)),
1201 context_ref(std::move(other.context_ref)) {
1205 if (
this != &other) {
1207 builder = std::exchange(other.builder,
nullptr);
1208 context_ref = std::move(other.context_ref);
1254 void add_resource(const std::
string &uri, const std::filesystem::path &source_path);
1261 void add_ingredient(const std::
string &ingredient_json, const std::
string &format, std::istream &source);
1268 void add_ingredient(const std::
string &ingredient_json, const std::filesystem::path &source_path);
1280 void set_intent(C2paBuilderIntent intent, C2paDigitalSourceType digital_source_type = Empty);
1290 std::vector<
unsigned char>
sign(const std::
string &format, std::istream &source, std::ostream &dest,
Signer &signer);
1299 std::vector<
unsigned char>
sign(const std::
string &format, std::istream &source, std::iostream &dest,
Signer &signer);
1308 std::vector<
unsigned char>
sign(const std::filesystem::path &source_path, const std::filesystem::path &dest_path,
Signer &signer);
1320 std::vector<
unsigned char>
sign(const std::
string &format, std::istream &source, std::iostream &dest);
1331 std::vector<
unsigned char>
sign(const std::filesystem::path &source_path, const std::filesystem::path &dest_path);
1385 [[deprecated(
"Use sign_embeddable(format), which returns manifest bytes already formatted "
1386 "for embedding. Will be removed when c2pa-c moves to c2pa-rs 0.92.0.")]]
1387 static std::vector<
unsigned char>
format_embeddable(const std::
string &format, std::vector<
unsigned char> &data);
1447 explicit
Builder(std::istream &archive);
1453#pragma GCC diagnostic pop
#define C2PA_CPP_API
Definition c2pa.hpp:49
std::vector< unsigned char > sign(const std::string &format, std::istream &source, std::ostream &dest, Signer &signer)
Sign an input stream and write the signed data to an output stream.
Builder(const std::string &manifest_json)
Create a Builder from a manifest JSON string using a new default Context.
static Builder from_archive(std::istream &archive)
Create a Builder from an archived Builder stream.
void add_ingredient(const std::string &ingredient_json, const std::string &format, std::istream &source)
Add an ingredient to the builder from a stream.
Builder & operator=(const Builder &)=delete
std::vector< unsigned char > sign_embeddable(const std::string &format)
Sign and return the final manifest bytes, ready for embedding.
void set_data_hash_exclusions(const std::vector< std::pair< uint64_t, uint64_t > > &exclusions)
Register the byte ranges where the placeholder was embedded (DataHash workflow).
std::vector< unsigned char > placeholder(const std::string &format)
Create a composed placeholder manifest to embed in the asset.
Builder(Builder &&other) noexcept
Definition c2pa.hpp:1199
bool needs_placeholder(const std::string &format)
Check if the given format requires a placeholder embedding step.
void add_resource(const std::string &uri, std::istream &source)
Add a resource to the builder from a stream.
void to_archive(std::ostream &dest)
Write the builder to an archive stream.
void add_action(const std::string &action_json)
Add an action to the manifest.
Builder(C2paBuilder *builder)
Create a Builder from a raw C FFI builder.
Builder(const Builder &)=delete
Builder(std::shared_ptr< IContextProvider > context, const std::string &manifest_json)
Create a Builder from a shared context and manifest JSON string.
void set_no_embed()
Set the no-embed flag to prevent embedding the manifest in the asset.
void update_hash_from_stream(const std::string &format, std::istream &stream)
Compute and store the asset hash by reading a stream.
C2paBuilder * c2pa_builder() const noexcept
Get the underlying C2paBuilder pointer.
void set_intent(C2paBuilderIntent intent, C2paDigitalSourceType digital_source_type=Empty)
Set the intent for this Builder, controlling what kind of manifest to create.
void write_ingredient_archive(const std::string &ingredient_id, std::ostream &dest)
Write a single-ingredient archive for the named ingredient.
Builder & operator=(Builder &&other) noexcept
Definition c2pa.hpp:1204
static std::vector< std::string > supported_mime_types()
Get a list of mime types that the Builder supports.
Builder(std::shared_ptr< IContextProvider > context)
Create a Builder from a shared context with an empty manifest.
Builder(IContextProvider &context)
Create a Builder from a context with an empty manifest.
static std::vector< unsigned char > format_embeddable(const std::string &format, std::vector< unsigned char > &data)
Convert unformatted manifest data to an embeddable format.
void set_remote_url(const std::string &remote_url)
Set the remote URL.
void set_base_path(const std::string &base_path)
Set the base path for loading resources from files.
Builder & with_archive(std::istream &archive)
Load an archive into this builder.
void add_ingredient_from_archive(std::istream &archive)
Add an ingredient to this builder from a per-ingredient archive stream.
Builder & with_definition(const std::string &manifest_json)
Set or update the manifest definition.
Builder(IContextProvider &context, const std::string &manifest_json)
Create a Builder from a context and manifest JSON string.
Exception class for C2pa errors. This class is used to throw exceptions for errors encountered by the...
Definition c2pa.hpp:87
C2paException & operator=(C2paException &&)=default
~C2paException() override=default
C2paException(const C2paException &)=default
const char * what() const noexcept override
Get the exception message.
C2paException(std::string message)
Construct an exception with a custom error message.
C2paException & operator=(const C2paException &)=default
C2paException(C2paException &&)=default
C2paException()
Default constructor.
C2paStream(C2paStream &&)=delete
C2paStream(const C2paStream &)=delete
C2paStream & operator=(C2paStream &&)=delete
C2paStream & operator=(const C2paStream &)=delete
::C2paStream * c_stream
Pointer to the underlying C2paStream.
Definition c2pa.hpp:552
ContextBuilder for creating customized Context instances.
Definition c2pa.hpp:330
ContextBuilder & with_signer(Signer &&signer)
Set a Signer on the context being built.
ContextBuilder & with_json_settings_file(const std::filesystem::path &settings_path)
Configure settings from a JSON settings file.
ReleasedBuilder release() noexcept
Release ownership of the underlying C2paContextBuilder handle and its progress-callback heap block (i...
ContextBuilder & with_http_resolver(void *user_data, C2paHttpResolverCallback callback)
Set a custom HTTP resolver callback on the context being built.
Context create_context()
Create a Context from the current builder configuration.
ContextBuilder & with_json(const std::string &json)
Configure settings with JSON string.
bool is_valid() const noexcept
Check if the builder is in a valid state.
ContextBuilder & with_settings(const Settings &settings)
Configure with Settings object.
ContextBuilder & with_progress_callback(ProgressCallbackFunc callback)
Attach a progress callback to the context being built.
~ContextBuilder() noexcept
C2PA context implementing IContextProvider.
Definition c2pa.hpp:322
void cancel() noexcept
Request cancellation of any in-progress operation on this context.
bool is_valid() const noexcept override
Check if this Context has a valid context (validity check for context-like types).
C2paContext * c_context() const noexcept override
Get the underlying C2PA context pointer.
Context()
Create a Context with default settings.
CppIOStream & operator=(const CppIOStream &)=delete
CppIOStream(const CppIOStream &)=delete
CppIOStream & operator=(CppIOStream &&)=delete
CppIOStream(IOStream &iostream)
Construct an I/O stream wrapper from a std::iostream-derived object.
Definition c2pa.hpp:692
CppIOStream(CppIOStream &&)=delete
CppIStream & operator=(const CppIStream &)=delete
friend class Reader
Definition c2pa.hpp:622
CppIStream(CppIStream &&)=delete
CppIStream & operator=(CppIStream &&)=delete
CppIStream(IStream &istream)
Construct an input stream wrapper from a std::istream-derived object.
Definition c2pa.hpp:575
CppIStream(const CppIStream &)=delete
CppOStream(OStream &ostream)
Construct an output stream wrapper from a std::ostream-derived object.
Definition c2pa.hpp:635
CppOStream(CppOStream &&)=delete
CppOStream & operator=(CppOStream &&)=delete
CppOStream & operator=(const CppOStream &)=delete
CppOStream(const CppOStream &)=delete
Interface for types that can provide C2PA context functionality.
Definition c2pa.hpp:161
virtual ~IContextProvider() noexcept=default
virtual bool is_valid() const noexcept=0
Check if this provider has a valid context.
virtual C2paContext * c_context() const noexcept=0
Get the underlying C2PA context pointer for FFI operations.
IContextProvider()=default
Reader(std::shared_ptr< IContextProvider > context, const std::filesystem::path &source_path)
Create a Reader from a shared context and file path.
static std::optional< Reader > from_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.
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.
bool is_embedded() const
Check if the reader was created from an embedded manifest.
Definition c2pa.hpp:954
std::string json() const
Get the manifest as a JSON string.
static std::optional< Reader > from_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.
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.
Reader(const std::string &format, std::istream &stream)
Create a Reader from a stream using a new default Context.
int64_t get_resource(const std::string &uri, std::ostream &stream)
Get a resource from the reader and write it to an output stream.
Reader(std::shared_ptr< IContextProvider > context, const std::string &format, std::istream &stream)
Create a Reader from a shared context and stream.
static std::optional< Reader > from_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.
Reader(Reader &&other) noexcept
Definition c2pa.hpp:929
Reader(const Reader &)=delete
std::string crjson() const
Get the manifest store as a pretty-printed crJSON string.
Reader & operator=(const Reader &)=delete
std::optional< std::string > remote_url() const
Returns the remote url of the manifest if this Reader obtained the manifest remotely.
Reader & with_fragment(const std::string &format, std::istream &stream, std::istream &fragment)
Process a BMFF fragment stream with this Reader instance.
static std::optional< Reader > from_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.
Reader(IContextProvider &context, const std::string &format, std::istream &stream)
Create a Reader from a context and stream.
Reader & operator=(Reader &&other) noexcept
Definition c2pa.hpp:936
static std::optional< Reader > from_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.
std::string detailed_json() const
Get the manifest as a detailed JSON string.
Reader(const std::filesystem::path &source_path)
Create a Reader from a file path using a new default Context.
C2paReader * get_api_internal_raw_reader() const
Get the raw C2paReader pointer.
Definition c2pa.hpp:1028
Reader(IContextProvider &context, const std::filesystem::path &source_path)
Create a Reader from a context and file path.
Reader(std::shared_ptr< IContextProvider > context, std::istream &stream)
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.
(C2PA SDK) Settings configuration object for creating contexts.
Definition c2pa.hpp:195
Settings & set(const std::string &path, const std::string &json_value)
Set a single configuration value by path.
Settings & update(const std::string &data)
Merge configuration from a JSON string (latest configuration wins).
Definition c2pa.hpp:233
C2paSettings * c_settings() const noexcept
Get the raw C FFI settings pointer.
Settings(const std::string &data, const std::string &format)
Create settings from a configuration string.
Settings()
Create default settings.
Settings(Settings &&) noexcept
Settings & update(const std::string &data, const std::string &format)
Merge configuration from a std::string (latest configuration wins).
bool is_valid() const noexcept
Check if this Settings object is valid (holds a C settings handle).
Signer class for creating a Signer.
Definition c2pa.hpp:1046
Signer & operator=(Signer &&other) noexcept
Definition c2pa.hpp:1109
uintptr_t reserve_size()
Get the size to reserve for a signature for this Signer.
C2paSigner * c2pa_signer() const noexcept
Get the underlying C2paSigner pointer.
Signer(SignerFunc *callback, C2paSigningAlg alg, const std::string &sign_cert, const std::string &tsa_uri)
Create a Signer from a callback function.
Signer(C2paSigner *c_signer)
Create a signer from a Signer pointer and take ownership of that pointer.
Definition c2pa.hpp:1086
Signer & operator=(const Signer &)=delete
Signer(Signer &&other) noexcept
Move constructor.
Definition c2pa.hpp:1106
Signer(const Signer &)=delete
Signer(const std::string &alg, const std::string &sign_cert, const std::string &private_key, const std::optional< std::string > &tsa_uri=std::nullopt)
Create a Signer from signing credentials.
OperationResult
Result codes for C API operations (matches C API return convention).
Definition c2pa.hpp:64
@ Success
Operation succeeded.
Definition c2pa.hpp:65
@ Error
Operation failed (check C2paException for details).
Definition c2pa.hpp:66
ProgressPhase
Phase values reported to the ProgressCallbackFunc.
Definition c2pa.hpp:265
@ VerifyingIngredient
Definition c2pa.hpp:269
@ VerifyingManifest
Definition c2pa.hpp:267
@ AddingIngredient
Definition c2pa.hpp:271
@ Reading
Definition c2pa.hpp:266
@ VerifyingSignature
Definition c2pa.hpp:268
@ VerifyingAssetHash
Definition c2pa.hpp:270
@ Embedding
Definition c2pa.hpp:275
@ Signing
Definition c2pa.hpp:274
@ Writing
Definition c2pa.hpp:277
@ FetchingRemoteManifest
Definition c2pa.hpp:276
@ FetchingOCSP
Definition c2pa.hpp:278
@ Thumbnail
Definition c2pa.hpp:272
@ Hashing
Definition c2pa.hpp:273
@ FetchingTimestamp
Definition c2pa.hpp:279
C2paSignerInfo SignerInfo
Type alias for C2paSignerInfo from the C API.
Definition c2pa.hpp:55
int stream_error_return(StreamError e) noexcept
Set errno from StreamError and return error sentinel.
Definition c2pa.hpp:79
StreamError
Stream/FFI error codes (maps to errno values used by the C layer).
Definition c2pa.hpp:70
@ InvalidArgument
Definition c2pa.hpp:71
@ NoBufferSpace
Definition c2pa.hpp:73
@ IoError
Definition c2pa.hpp:72
std::string version()
Get the version of the C2PA library.
std::vector< unsigned char >(const std::vector< unsigned char > &) SignerFunc
Signer callback function type.
Definition c2pa.hpp:1040
std::function< bool(ProgressPhase phase, uint32_t step, uint32_t total)> ProgressCallbackFunc
Type alias for the progress callback passed to ContextBuilder::with_progress_callback().
Definition c2pa.hpp:299
Result of ContextBuilder::release(): the raw native builder handle paired with the heap-owned progres...
Definition c2pa.hpp:426
C2paContextBuilder * builder
Definition c2pa.hpp:427
std::unique_ptr< ProgressCallbackFunc > callback_owner
Definition c2pa.hpp:428