18#ifndef C2PA_INTERNAL_HPP
19#define C2PA_INTERNAL_HPP
36 return message !=
nullptr && std::strstr(message,
"ManifestNotFound") !=
nullptr;
46 std::vector<std::string> result;
47 if (mime_types ==
nullptr) {
return result; }
50 result.reserve(count);
51 for(uintptr_t i = 0; i < count; i++) {
52 if (mime_types[i] !=
nullptr) {
53 result.emplace_back(mime_types[i]);
57 c2pa_free_string_array(mime_types, count);
61 c2pa_free_string_array(mime_types, count);
68 case C2paSeekMode::Start:
return std::ios_base::beg;
69 case C2paSeekMode::Current:
return std::ios_base::cur;
70 case C2paSeekMode::End:
return std::ios_base::end;
71 default:
return std::ios_base::beg;
76template<
typename Stream>
78 return s && !s->bad();
82template<
typename Stream>
87 static void seek(std::istream* s, intptr_t offset, std::ios_base::seekdir dir) {
88 s->seekg(offset, dir);
90 static int64_t
tell(std::istream* s) {
91 return static_cast<int64_t
>(s->tellg());
97 static void seek(std::ostream* s, intptr_t offset, std::ios_base::seekdir dir) {
98 s->seekp(offset, dir);
100 static int64_t
tell(std::ostream* s) {
101 return static_cast<int64_t
>(s->tellp());
107 static void seek(std::iostream* s, intptr_t offset, std::ios_base::seekdir dir) {
108 s->seekg(offset, dir);
109 s->seekp(offset, dir);
111 static int64_t
tell(std::iostream* s) {
112 return static_cast<int64_t
>(s->tellp());
119template<
typename Stream>
120intptr_t
stream_seeker(StreamContext* context, intptr_t offset, C2paSeekMode whence) {
122 auto* stream =
reinterpret_cast<Stream*
>(context);
129 if (stream->fail()) {
139 return static_cast<intptr_t
>(pos);
148template<
typename Stream>
149intptr_t
stream_reader(StreamContext* context, uint8_t* buffer, intptr_t size) {
150 if (!context || !buffer) {
160 auto* stream =
reinterpret_cast<Stream*
>(context);
164 stream->read(
reinterpret_cast<char*
>(buffer), size);
165 if (stream->fail()) {
166 if (!stream->eof()) {
173 return static_cast<intptr_t
>(stream->gcount());
182template<
typename Stream,
typename Op>
185 auto* stream =
reinterpret_cast<Stream*
>(context);
189 const intptr_t result = op(stream);
190 if (stream->fail()) {
203template<
typename Stream>
204intptr_t
stream_writer(StreamContext* context,
const uint8_t* buffer, intptr_t size) {
206 s->write(
reinterpret_cast<const char*
>(
buffer),
size);
212template<
typename Stream>
224template<
typename StreamType>
227 auto stream = std::make_unique<StreamType>(
229 std::ios_base::binary
241 auto ext =
path.extension().string();
242 return ext.empty() ?
"" :
ext.substr(1);
C++ wrapper for the C2PA C library.
Exception class for C2pa errors. This class is used to throw exceptions for errors encountered by the...
Definition c2pa.hpp:87
std::vector< std::string > c_mime_types_to_vector(const char *const *mime_types, uintptr_t count)
Converts a C array of C strings to a std::vector of std::string.
Definition c2pa_internal.hpp:45
intptr_t stream_writer(StreamContext *context, const uint8_t *buffer, intptr_t size)
Writer impl.
Definition c2pa_internal.hpp:204
intptr_t stream_op(StreamContext *context, Op op)
Definition c2pa_internal.hpp:183
std::string extract_file_extension(const std::filesystem::path &path) noexcept
Extract file extension without the leading dot.
Definition c2pa_internal.hpp:240
bool error_indicates_manifest_not_found(const char *message) noexcept
True if the C2PA error message indicates no JUMBF / manifest in the asset (ManifestNotFound).
Definition c2pa_internal.hpp:35
intptr_t stream_flusher(StreamContext *context)
Flusher impl.
Definition c2pa_internal.hpp:213
constexpr std::ios_base::seekdir whence_to_seekdir(C2paSeekMode whence) noexcept
Maps C2PA seek mode to std::ios seek direction.
Definition c2pa_internal.hpp:66
bool is_stream_usable(Stream *s) noexcept
Check if stream is in valid state for I/O operations.
Definition c2pa_internal.hpp:77
std::vector< unsigned char > to_byte_vector(const unsigned char *data, int64_t size)
Convert C byte array result to C++ vector.
Definition c2pa_internal.hpp:267
intptr_t stream_reader(StreamContext *context, uint8_t *buffer, intptr_t size)
Definition c2pa_internal.hpp:149
std::string c_string_to_string(T *c_result)
Convert C string result to C++ string with cleanup.
Definition c2pa_internal.hpp:249
intptr_t stream_seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence)
Definition c2pa_internal.hpp:120
std::unique_ptr< StreamType > open_file_binary(const std::filesystem::path &path)
Open a binary file stream with error handling.
Definition c2pa_internal.hpp:225
int stream_error_return(StreamError e) noexcept
Set errno from StreamError and return error sentinel.
Definition c2pa.hpp:79
static void seek(std::iostream *s, intptr_t offset, std::ios_base::seekdir dir)
Definition c2pa_internal.hpp:107
static int64_t tell(std::iostream *s)
Definition c2pa_internal.hpp:111
static int64_t tell(std::istream *s)
Definition c2pa_internal.hpp:90
static void seek(std::istream *s, intptr_t offset, std::ios_base::seekdir dir)
Definition c2pa_internal.hpp:87
static void seek(std::ostream *s, intptr_t offset, std::ios_base::seekdir dir)
Definition c2pa_internal.hpp:97
static int64_t tell(std::ostream *s)
Definition c2pa_internal.hpp:100
Traits (templated): how to seek and get position for a given stream type.
Definition c2pa_internal.hpp:83