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);
566 template <
typename IStream>
568 static_assert(std::is_base_of<std::istream, IStream>::value,
569 "Stream must be derived from std::istream");
571 c_stream = c2pa_create_stream(
reinterpret_cast<StreamContext *
>(
static_cast<std::istream *
>(&istream)), reader, seeker, writer, flusher);
572 if (c_stream ==
nullptr) {
573 throw C2paException(
"Failed to create input stream wrapper: is stream open and valid?");
593 static intptr_t reader(StreamContext *context, uint8_t *buffer, intptr_t size);
600 static intptr_t writer(StreamContext *context,
const uint8_t *buffer, intptr_t size);
607 static intptr_t seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence);
612 static intptr_t flusher(StreamContext *context);
629 template <
typename OStream>
631 static_assert(std::is_base_of<std::ostream, OStream>::value,
"Stream must be derived from std::ostream");
633 c_stream = c2pa_create_stream(
reinterpret_cast<StreamContext *
>(
static_cast<std::ostream *
>(&ostream)), reader, seeker, writer, flusher);
634 if (c_stream ==
nullptr) {
635 throw C2paException(
"Failed to create output stream wrapper: is stream open and valid?");
655 static intptr_t reader(StreamContext *context, uint8_t *buffer, intptr_t size);
662 static intptr_t writer(StreamContext *context,
const uint8_t *buffer, intptr_t size);
669 static intptr_t seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence);
674 static intptr_t flusher(StreamContext *context);
689 template <
typename IOStream>
691 static_assert(std::is_base_of<std::iostream, IOStream>::value,
"Stream must be derived from std::iostream");
693 c_stream = c2pa_create_stream(
reinterpret_cast<StreamContext *
>(
static_cast<std::iostream *
>(&iostream)), reader, seeker, writer, flusher);
694 if (c_stream ==
nullptr) {
695 throw C2paException(
"Failed to create I/O stream wrapper: is stream open and valid?");
715 static intptr_t reader(StreamContext *context, uint8_t *buffer, intptr_t size);
722 static intptr_t writer(StreamContext *context,
const uint8_t *buffer, intptr_t size);
729 static intptr_t seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence);
734 static intptr_t flusher(StreamContext *context);
744 C2paReader *c2pa_reader;
745 std::unique_ptr<std::ifstream> owned_stream;
746 std::unique_ptr<CppIStream> cpp_stream;
747 std::shared_ptr<IContextProvider> context_ref;
749 void init_from_context(
IContextProvider& context,
const std::string &format, std::istream &stream);
750 void init_from_context(
IContextProvider& context,
const std::filesystem::path &source_path);
751 Reader() : c2pa_reader(
nullptr) {}
763 [[deprecated(
"Use Reader(std::shared_ptr<IContextProvider>, format, stream) instead. "
764 "The reference overload does not extend the lifetime of the context, which can "
765 "be problematic when progress callbacks fire after the context is destroyed.")]]
777 [[deprecated(
"Use Reader(std::shared_ptr<IContextProvider>, source_path) 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.")]]
789 Reader(std::shared_ptr<IContextProvider> context,
const std::string &format, std::istream &stream);
797 Reader(std::shared_ptr<IContextProvider> context,
const std::filesystem::path &source_path);
805 [[deprecated(
"Use Reader(IContextProvider& context, format, stream) instead")]]
806 Reader(
const std::string &format, std::istream &stream);
813 [[deprecated(
"Use Reader(IContextProvider& context, source_path) instead")]]
814 Reader(
const std::filesystem::path &source_path);
824 [[deprecated(
"Use from_asset(std::shared_ptr<IContextProvider>, source_path) instead. "
825 "The reference overload does not extend the lifetime of the context, which can "
826 "be problematic when progress callbacks fire after the context is destroyed.")]]
836 [[deprecated(
"Use from_asset(std::shared_ptr<IContextProvider>, format, stream) instead. "
837 "The reference overload does not extend the lifetime of the context, which can "
838 "be problematic when progress callbacks fire after the context is destroyed.")]]
845 static std::optional<Reader>
from_asset(std::shared_ptr<IContextProvider> context,
const std::filesystem::path &source_path);
851 static std::optional<Reader>
from_asset(std::shared_ptr<IContextProvider> context,
const std::string &format, std::istream &stream);
859 : c2pa_reader(std::exchange(other.c2pa_reader,
nullptr)),
860 owned_stream(std::move(other.owned_stream)),
861 cpp_stream(std::move(other.cpp_stream)),
862 context_ref(std::move(other.context_ref)) {
866 if (
this != &other) {
867 c2pa_free(c2pa_reader);
868 c2pa_reader = std::exchange(other.c2pa_reader,
nullptr);
869 owned_stream = std::move(other.owned_stream);
870 cpp_stream = std::move(other.cpp_stream);
871 context_ref = std::move(other.context_ref);
882 return c2pa_reader_is_embedded(c2pa_reader);
922 int64_t
get_resource(
const std::string &uri,
const std::filesystem::path &path);
946 using SignerFunc = std::vector<unsigned char>(
const std::vector<unsigned char> &);
967 C2paSigner* release() noexcept {
968 return std::exchange(signer,
nullptr);
974 static const char *validate_tsa_uri(
const std::string &tsa_uri);
979 static const char *validate_tsa_uri(
const std::optional<std::string> &tsa_uri);
988 Signer(
SignerFunc *callback, C2paSigningAlg alg,
const std::string &sign_cert,
const std::string &tsa_uri);
992 Signer(C2paSigner *c_signer) : signer(c_signer) {
1004 Signer(
const std::string &alg,
const std::string &sign_cert,
const std::string &private_key,
const std::optional<std::string> &tsa_uri = std::nullopt);
1012 Signer(
Signer&& other) noexcept : signer(std::exchange(other.signer,
nullptr)) {
1016 if (
this != &other) {
1018 signer = std::exchange(other.signer,
nullptr);
1039 C2paBuilder *builder;
1040 std::shared_ptr<IContextProvider> context_ref;
1043 void init_from_context(
IContextProvider& context,
const std::string &manifest_json);
1053 [[deprecated(
"Use Builder(std::shared_ptr<IContextProvider>) instead. "
1054 "The reference overload does not extend the lifetime of the context, which can "
1055 "be problematic when progress callbacks fire after the context is destroyed.")]]
1066 [[deprecated(
"Use Builder(std::shared_ptr<IContextProvider>, manifest_json) instead. "
1067 "The reference overload does not extend the lifetime of the context, which can "
1068 "be problematic when progress callbacks fire after the context is destroyed.")]]
1077 explicit Builder(std::shared_ptr<IContextProvider> context);
1085 Builder(std::shared_ptr<IContextProvider> context,
const std::string &manifest_json);
1091 [[deprecated(
"Use Builder(IContextProvider& context, manifest_json) instead")]]
1104 : builder(std::exchange(other.builder,
nullptr)),
1105 context_ref(std::move(other.context_ref)) {
1109 if (
this != &other) {
1111 builder = std::exchange(other.builder,
nullptr);
1112 context_ref = std::move(other.context_ref);
1127 Builder& with_definition(const std::
string &manifest_json);
1131 void set_no_embed();
1136 void set_remote_url(const std::
string &remote_url);
1145 void set_base_path(const std::
string &base_path);
1151 void add_resource(const std::
string &uri, std::istream &source);
1158 void add_resource(const std::
string &uri, const std::filesystem::path &source_path);
1165 void add_ingredient(const std::
string &ingredient_json, const std::
string &format, std::istream &source);
1172 void add_ingredient(const std::
string &ingredient_json, const std::filesystem::path &source_path);
1177 void add_action(const std::
string &action_json);
1184 void set_intent(C2paBuilderIntent intent, C2paDigitalSourceType digital_source_type = Empty);
1194 std::vector<
unsigned char> sign(const std::
string &format, std::istream &source, std::ostream &dest,
Signer &signer);
1203 std::vector<
unsigned char> sign(const std::
string &format, std::istream &source, std::iostream &dest,
Signer &signer);
1212 std::vector<
unsigned char> sign(const std::filesystem::path &source_path, const std::filesystem::path &dest_path,
Signer &signer);
1224 std::vector<
unsigned char> sign(const std::
string &format, std::istream &source, std::iostream &dest);
1235 std::vector<
unsigned char> sign(const std::filesystem::path &source_path, const std::filesystem::path &dest_path);
1241 static
Builder from_archive(std::istream &archive);
1248 static
Builder from_archive(const std::filesystem::path &archive_path);
1261 void to_archive(std::ostream &dest);
1267 void to_archive(const std::filesystem::path &dest_path);
1276 void write_ingredient_archive(const std::
string &ingredient_id, std::ostream &dest);
1281 void add_ingredient_from_archive(std::istream &archive);
1288 std::vector<
unsigned char> data_hashed_placeholder(uintptr_t reserved_size, const std::
string &format);
1297 std::vector<
unsigned char> sign_data_hashed_embeddable(
Signer &signer, const std::
string &data_hash, const std::
string &format, std::istream *asset =
nullptr);
1303 static std::vector<
unsigned char> format_embeddable(const std::
string &format, std::vector<
unsigned char> &data);
1312 bool needs_placeholder(const std::
string &format);
1324 std::vector<
unsigned char> placeholder(const std::
string &format);
1333 void set_data_hash_exclusions(const std::vector<std::pair<uint64_t, uint64_t>> &exclusions);
1344 void update_hash_from_stream(const std::
string &format, std::istream &stream);
1356 std::vector<
unsigned char> sign_embeddable(const std::
string &format);
1360 static std::vector<std::
string> supported_mime_types();
1363 explicit
Builder(std::istream &archive);
1369#pragma GCC diagnostic pop
#define C2PA_CPP_API
Definition c2pa.hpp:49
Builder class for creating a manifest.
Definition c2pa.hpp:1037
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:1103
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:1108
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:680
CppIOStream & operator=(const CppIOStream &)=delete
CppIOStream(const CppIOStream &)=delete
CppIOStream & operator=(CppIOStream &&)=delete
C2paStream * c_stream
Pointer to the underlying C2paStream.
Definition c2pa.hpp:683
CppIOStream(IOStream &iostream)
Construct an I/O stream wrapper from a std::iostream-derived object.
Definition c2pa.hpp:690
CppIOStream(CppIOStream &&)=delete
Input stream IStream wrapper for C2paStream.
Definition c2pa.hpp:557
CppIStream & operator=(const CppIStream &)=delete
CppIStream(CppIStream &&)=delete
CppIStream & operator=(CppIStream &&)=delete
C2paStream * c_stream
Pointer to the underlying C2paStream.
Definition c2pa.hpp:560
CppIStream(IStream &istream)
Construct an input stream wrapper from a std::istream-derived object.
Definition c2pa.hpp:567
CppIStream(const CppIStream &)=delete
Output stream OStream wrapper for C2paStream.
Definition c2pa.hpp:620
CppOStream(OStream &ostream)
Construct an output stream wrapper from a std::ostream-derived object.
Definition c2pa.hpp:630
CppOStream(CppOStream &&)=delete
CppOStream & operator=(CppOStream &&)=delete
C2paStream * c_stream
Pointer to the underlying C2paStream.
Definition c2pa.hpp:623
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:742
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:881
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:858
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.
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:865
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 (will use global settings if any loaded).
C2paReader * get_api_internal_raw_reader() const
Get the raw C2paReader pointer.
Definition c2pa.hpp:934
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:952
Signer & operator=(Signer &&other) noexcept
Definition c2pa.hpp:1015
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:992
Signer & operator=(const Signer &)=delete
Signer(Signer &&other) noexcept
Move constructor.
Definition c2pa.hpp:1012
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:946
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