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);
219 [[nodiscard]]
bool is_valid() const noexcept;
226 Settings& set(const std::
string& path, const std::
string& json_value);
248 C2paSettings* settings_ptr;
345 [[nodiscard]]
bool is_valid() const noexcept;
363 ContextBuilder& with_json_settings_file(const std::filesystem::path& settings_path);
409 ContextBuilder& with_http_resolver(
void* user_data, C2paHttpResolverCallback callback);
451 C2paContextBuilder* context_builder;
490 [[nodiscard]] C2paContext* c_context() const noexcept override;
495 [[nodiscard]]
bool is_valid() const noexcept override;
518 void cancel() noexcept;
524 C2paContext* context;
541 [[deprecated("Use
Context::from_json() or
Context::from_settings() instead")]]
542 void C2PA_CPP_API load_settings(const std::
string& data, const std::
string& format);
550 [[deprecated("Use
Reader object instead")]]
551 std::optional<std::
string>
C2PA_CPP_API read_file(const std::filesystem::path &source_path, const std::optional<std::filesystem::path> data_dir = std::nullopt);
560 std::
string C2PA_CPP_API read_ingredient_file(const std::filesystem::path &source_path, const std::filesystem::path &data_dir);
570 [[deprecated("Use
Builder.sign instead")]]
572 const std::filesystem::path &dest_path,
573 const
char *manifest,
575 const std::optional<std::filesystem::path> data_dir = std::nullopt);
599 template <
typename IStream>
601 static_assert(std::is_base_of<std::istream, IStream>::value,
602 "Stream must be derived from std::istream");
603 c_stream = c2pa_create_stream(
reinterpret_cast<StreamContext *
>(&istream), reader, seeker, writer, flusher);
604 if (c_stream ==
nullptr) {
605 throw C2paException(
"Failed to create input stream wrapper: is stream open and valid?");
625 static intptr_t reader(StreamContext *context, uint8_t *buffer, intptr_t size);
632 static intptr_t writer(StreamContext *context,
const uint8_t *buffer, intptr_t size);
639 static intptr_t seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence);
644 static intptr_t flusher(StreamContext *context);
661 template <
typename OStream>
663 static_assert(std::is_base_of<std::ostream, OStream>::value,
"Stream must be derived from std::ostream");
664 c_stream = c2pa_create_stream(
reinterpret_cast<StreamContext *
>(&ostream), reader, seeker, writer, flusher);
665 if (c_stream ==
nullptr) {
666 throw C2paException(
"Failed to create output stream wrapper: is stream open and valid?");
686 static intptr_t reader(StreamContext *context, uint8_t *buffer, intptr_t size);
693 static intptr_t writer(StreamContext *context,
const uint8_t *buffer, intptr_t size);
700 static intptr_t seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence);
705 static intptr_t flusher(StreamContext *context);
720 template <
typename IOStream>
722 static_assert(std::is_base_of<std::iostream, IOStream>::value,
"Stream must be derived from std::iostream");
723 c_stream = c2pa_create_stream(
reinterpret_cast<StreamContext *
>(&iostream), reader, seeker, writer, flusher);
724 if (c_stream ==
nullptr) {
725 throw C2paException(
"Failed to create I/O stream wrapper: is stream open and valid?");
745 static intptr_t reader(StreamContext *context, uint8_t *buffer, intptr_t size);
752 static intptr_t writer(StreamContext *context,
const uint8_t *buffer, intptr_t size);
759 static intptr_t seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence);
764 static intptr_t flusher(StreamContext *context);
774 C2paReader *c2pa_reader;
775 std::unique_ptr<std::ifstream> owned_stream;
776 std::unique_ptr<CppIStream> cpp_stream;
777 std::shared_ptr<IContextProvider> context_ref;
779 void init_from_context(
IContextProvider& context,
const std::string &format, std::istream &stream);
780 void init_from_context(
IContextProvider& context,
const std::filesystem::path &source_path);
781 Reader() : c2pa_reader(
nullptr) {}
793 [[deprecated(
"Use Reader(std::shared_ptr<IContextProvider>, format, stream) instead. "
794 "The reference overload does not extend the lifetime of the context, which can "
795 "be problematic when progress callbacks fire after the context is destroyed.")]]
807 [[deprecated(
"Use Reader(std::shared_ptr<IContextProvider>, source_path) instead. "
808 "The reference overload does not extend the lifetime of the context, which can "
809 "be problematic when progress callbacks fire after the context is destroyed.")]]
819 Reader(std::shared_ptr<IContextProvider> context,
const std::string &format, std::istream &stream);
827 Reader(std::shared_ptr<IContextProvider> context,
const std::filesystem::path &source_path);
835 [[deprecated(
"Use Reader(IContextProvider& context, format, stream) instead")]]
836 Reader(
const std::string &format, std::istream &stream);
843 [[deprecated(
"Use Reader(IContextProvider& context, source_path) instead")]]
844 Reader(
const std::filesystem::path &source_path);
854 [[deprecated(
"Use from_asset(std::shared_ptr<IContextProvider>, source_path) instead. "
855 "The reference overload does not extend the lifetime of the context, which can "
856 "be problematic when progress callbacks fire after the context is destroyed.")]]
866 [[deprecated(
"Use from_asset(std::shared_ptr<IContextProvider>, format, stream) instead. "
867 "The reference overload does not extend the lifetime of the context, which can "
868 "be problematic when progress callbacks fire after the context is destroyed.")]]
875 static std::optional<Reader>
from_asset(std::shared_ptr<IContextProvider> context,
const std::filesystem::path &source_path);
881 static std::optional<Reader>
from_asset(std::shared_ptr<IContextProvider> context,
const std::string &format, std::istream &stream);
889 : c2pa_reader(std::exchange(other.c2pa_reader,
nullptr)),
890 owned_stream(std::move(other.owned_stream)),
891 cpp_stream(std::move(other.cpp_stream)),
892 context_ref(std::move(other.context_ref)) {
896 if (
this != &other) {
897 c2pa_free(c2pa_reader);
898 c2pa_reader = std::exchange(other.c2pa_reader,
nullptr);
899 owned_stream = std::move(other.owned_stream);
900 cpp_stream = std::move(other.cpp_stream);
901 context_ref = std::move(other.context_ref);
912 return c2pa_reader_is_embedded(c2pa_reader);
932 int64_t
get_resource(
const std::string &uri,
const std::filesystem::path &path);
956 using SignerFunc = std::vector<unsigned char>(
const std::vector<unsigned char> &);
977 C2paSigner* release() noexcept {
978 return std::exchange(signer,
nullptr);
984 static const char *validate_tsa_uri(
const std::string &tsa_uri);
989 static const char *validate_tsa_uri(
const std::optional<std::string> &tsa_uri);
998 Signer(
SignerFunc *callback, C2paSigningAlg alg,
const std::string &sign_cert,
const std::string &tsa_uri);
1002 Signer(C2paSigner *c_signer) : signer(c_signer) {
1014 Signer(
const std::string &alg,
const std::string &sign_cert,
const std::string &private_key,
const std::optional<std::string> &tsa_uri = std::nullopt);
1022 Signer(
Signer&& other) noexcept : signer(std::exchange(other.signer,
nullptr)) {
1026 if (
this != &other) {
1028 signer = std::exchange(other.signer,
nullptr);
1049 C2paBuilder *builder;
1050 std::shared_ptr<IContextProvider> context_ref;
1053 void init_from_context(
IContextProvider& context,
const std::string &manifest_json);
1063 [[deprecated(
"Use Builder(std::shared_ptr<IContextProvider>) instead. "
1064 "The reference overload does not extend the lifetime of the context, which can "
1065 "be problematic when progress callbacks fire after the context is destroyed.")]]
1076 [[deprecated(
"Use Builder(std::shared_ptr<IContextProvider>, manifest_json) instead. "
1077 "The reference overload does not extend the lifetime of the context, which can "
1078 "be problematic when progress callbacks fire after the context is destroyed.")]]
1087 explicit Builder(std::shared_ptr<IContextProvider> context);
1095 Builder(std::shared_ptr<IContextProvider> context,
const std::string &manifest_json);
1101 [[deprecated(
"Use Builder(IContextProvider& context, manifest_json) instead")]]
1114 : builder(std::exchange(other.builder,
nullptr)),
1115 context_ref(std::move(other.context_ref)) {
1119 if (
this != &other) {
1121 builder = std::exchange(other.builder,
nullptr);
1122 context_ref = std::move(other.context_ref);
1137 Builder& with_definition(const std::
string &manifest_json);
1141 void set_no_embed();
1146 void set_remote_url(const std::
string &remote_url);
1155 void set_base_path(const std::
string &base_path);
1161 void add_resource(const std::
string &uri, std::istream &source);
1168 void add_resource(const std::
string &uri, const std::filesystem::path &source_path);
1175 void add_ingredient(const std::
string &ingredient_json, const std::
string &format, std::istream &source);
1182 void add_ingredient(const std::
string &ingredient_json, const std::filesystem::path &source_path);
1187 void add_action(const std::
string &action_json);
1194 void set_intent(C2paBuilderIntent intent, C2paDigitalSourceType digital_source_type = Empty);
1204 std::vector<
unsigned char> sign(const std::
string &format, std::istream &source, std::ostream &dest,
Signer &signer);
1213 std::vector<
unsigned char> sign(const std::
string &format, std::istream &source, std::iostream &dest,
Signer &signer);
1222 std::vector<
unsigned char> sign(const std::filesystem::path &source_path, const std::filesystem::path &dest_path,
Signer &signer);
1234 std::vector<
unsigned char> sign(const std::
string &format, std::istream &source, std::iostream &dest);
1245 std::vector<
unsigned char> sign(const std::filesystem::path &source_path, const std::filesystem::path &dest_path);
1251 static
Builder from_archive(std::istream &archive);
1258 static
Builder from_archive(const std::filesystem::path &archive_path);
1271 void to_archive(std::ostream &dest);
1277 void to_archive(const std::filesystem::path &dest_path);
1284 std::vector<
unsigned char> data_hashed_placeholder(uintptr_t reserved_size, const std::
string &format);
1293 std::vector<
unsigned char> sign_data_hashed_embeddable(
Signer &signer, const std::
string &data_hash, const std::
string &format, std::istream *asset =
nullptr);
1299 static std::vector<
unsigned char> format_embeddable(const std::
string &format, std::vector<
unsigned char> &data);
1308 bool needs_placeholder(const std::
string &format);
1320 std::vector<
unsigned char> placeholder(const std::
string &format);
1329 void set_data_hash_exclusions(const std::vector<std::pair<uint64_t, uint64_t>> &exclusions);
1340 void update_hash_from_stream(const std::
string &format, std::istream &stream);
1352 std::vector<
unsigned char> sign_embeddable(const std::
string &format);
1356 static std::vector<std::
string> supported_mime_types();
1359 explicit
Builder(std::istream &archive);
1365#pragma GCC diagnostic pop
#define C2PA_CPP_API
Definition c2pa.hpp:49
Builder class for creating a manifest.
Definition c2pa.hpp:1047
Builder(const std::string &manifest_json)
Create a Builder from a manifest JSON string (will use global settings if any loaded).
Builder & operator=(const Builder &)=delete
Builder(Builder &&other) noexcept
Definition c2pa.hpp:1113
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.
C2paBuilder * c2pa_builder() const noexcept
Get the underlying C2paBuilder pointer.
Builder & operator=(Builder &&other) noexcept
Definition c2pa.hpp:1118
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.
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.
ContextBuilder for creating customized Context instances.
Definition c2pa.hpp:330
ReleasedBuilder release() noexcept
Release ownership of the underlying C2paContextBuilder handle and its progress-callback heap block (i...
~ContextBuilder() noexcept
C2PA context implementing IContextProvider.
Definition c2pa.hpp:322
IOStream Class wrapper for C2paStream.
Definition c2pa.hpp:711
CppIOStream & operator=(const CppIOStream &)=delete
CppIOStream(const CppIOStream &)=delete
CppIOStream & operator=(CppIOStream &&)=delete
C2paStream * c_stream
Pointer to the underlying C2paStream.
Definition c2pa.hpp:714
CppIOStream(IOStream &iostream)
Construct an I/O stream wrapper from a std::iostream-derived object.
Definition c2pa.hpp:721
CppIOStream(CppIOStream &&)=delete
Input stream IStream wrapper for C2paStream.
Definition c2pa.hpp:590
CppIStream & operator=(const CppIStream &)=delete
CppIStream(CppIStream &&)=delete
CppIStream & operator=(CppIStream &&)=delete
C2paStream * c_stream
Pointer to the underlying C2paStream.
Definition c2pa.hpp:593
CppIStream(IStream &istream)
Construct an input stream wrapper from a std::istream-derived object.
Definition c2pa.hpp:600
CppIStream(const CppIStream &)=delete
Output stream OStream wrapper for C2paStream.
Definition c2pa.hpp:652
CppOStream(OStream &ostream)
Construct an output stream wrapper from a std::ostream-derived object.
Definition c2pa.hpp:662
CppOStream(CppOStream &&)=delete
CppOStream & operator=(CppOStream &&)=delete
C2paStream * c_stream
Pointer to the underlying C2paStream.
Definition c2pa.hpp:655
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
Reader class for reading a manifest.
Definition c2pa.hpp:772
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.
bool is_embedded() const
Check if the reader was created from an embedded manifest.
Definition c2pa.hpp:911
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 (will use global settings if any loaded).
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:888
Reader(const Reader &)=delete
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.
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:895
Reader(const std::filesystem::path &source_path)
Create a Reader from a file path (will use global settings if any loaded).
C2paReader * get_api_internal_raw_reader() const
Get the raw C2paReader pointer.
Definition c2pa.hpp:944
Reader(IContextProvider &context, const std::filesystem::path &source_path)
Create a Reader from a context and file path.
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 & 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).
Signer class for creating a Signer.
Definition c2pa.hpp:962
Signer & operator=(Signer &&other) noexcept
Definition c2pa.hpp:1025
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:1002
Signer & operator=(const Signer &)=delete
Signer(Signer &&other) noexcept
Move constructor.
Definition c2pa.hpp:1022
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.
@ Error
Operation failed (check C2paException for details)
ProgressPhase
Phase values reported to the ProgressCallbackFunc.
Definition c2pa.hpp:265
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
int stream_error_return(StreamError e) noexcept
Set errno from StreamError and return error sentinel.
Definition c2pa.hpp:79
C2paSignerInfo SignerInfo
Type alias for C2paSignerInfo from the C API.
Definition c2pa.hpp:55
StreamError
Stream/FFI error codes (maps to errno values used by the C layer).
Definition c2pa.hpp:70
std::vector< unsigned char >(const std::vector< unsigned char > &) SignerFunc
Signer callback function type.
Definition c2pa.hpp:956
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