18#ifndef C2PA_INTERNAL_HPP
19#define C2PA_INTERNAL_HPP
39 return message !=
nullptr && std::strstr(message,
"ManifestNotFound") !=
nullptr;
49 std::vector<std::string> result;
50 if (mime_types ==
nullptr) {
return result; }
53 result.reserve(count);
54 for(uintptr_t i = 0; i < count; i++) {
55 if (mime_types[i] !=
nullptr) {
56 result.emplace_back(mime_types[i]);
60 c2pa_free_string_array(mime_types, count);
64 c2pa_free_string_array(mime_types, count);
71 case C2paSeekMode::Start:
return std::ios_base::beg;
72 case C2paSeekMode::Current:
return std::ios_base::cur;
73 case C2paSeekMode::End:
return std::ios_base::end;
74 default:
return std::ios_base::beg;
79template<
typename Stream>
81 return s && !s->bad();
85template<
typename Stream>
90 static void seek(std::istream* s, intptr_t offset, std::ios_base::seekdir dir) {
91 s->seekg(offset, dir);
93 static int64_t
tell(std::istream* s) {
94 return static_cast<int64_t
>(s->tellg());
100 static void seek(std::ostream* s, intptr_t offset, std::ios_base::seekdir dir) {
101 s->seekp(offset, dir);
103 static int64_t
tell(std::ostream* s) {
104 return static_cast<int64_t
>(s->tellp());
110 static void seek(std::iostream* s, intptr_t offset, std::ios_base::seekdir dir) {
111 s->seekg(offset, dir);
112 s->seekp(offset, dir);
114 static int64_t
tell(std::iostream* s) {
115 return static_cast<int64_t
>(s->tellp());
122template<
typename Stream>
123intptr_t
stream_seeker(StreamContext* context, intptr_t offset, C2paSeekMode whence) {
125 auto* stream =
reinterpret_cast<Stream*
>(context);
132 if (stream->fail()) {
142 return static_cast<intptr_t
>(pos);
151template<
typename Stream>
152intptr_t
stream_reader(StreamContext* context, uint8_t* buffer, intptr_t size) {
153 if (!context || !buffer) {
163 auto* stream =
reinterpret_cast<Stream*
>(context);
167 stream->read(
reinterpret_cast<char*
>(buffer), size);
168 if (stream->fail()) {
169 if (!stream->eof()) {
176 return static_cast<intptr_t
>(stream->gcount());
185template<
typename Stream,
typename Op>
188 auto* stream =
reinterpret_cast<Stream*
>(context);
192 const intptr_t result = op(stream);
193 if (stream->fail()) {
206template<
typename Stream>
207intptr_t
stream_writer(StreamContext* context,
const uint8_t* buffer, intptr_t size) {
209 s->write(
reinterpret_cast<const char*
>(
buffer),
size);
215template<
typename Stream>
227template<
typename StreamType>
230 auto stream = std::make_unique<StreamType>(
232 std::ios_base::binary
244 auto ext =
path.extension().string();
245 return ext.empty() ?
"" :
ext.substr(1);
262 if (
first == std::string::npos) {
268 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
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:48
intptr_t stream_writer(StreamContext *context, const uint8_t *buffer, intptr_t size)
Writer impl.
Definition c2pa_internal.hpp:207
intptr_t stream_op(StreamContext *context, Op op)
Definition c2pa_internal.hpp:186
std::string extract_file_extension(const std::filesystem::path &path) noexcept
Extract file extension without the leading dot.
Definition c2pa_internal.hpp:243
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:38
intptr_t stream_flusher(StreamContext *context)
Flusher impl.
Definition c2pa_internal.hpp:216
constexpr std::ios_base::seekdir whence_to_seekdir(C2paSeekMode whence) noexcept
Maps C2PA seek mode to std::ios seek direction.
Definition c2pa_internal.hpp:69
bool is_stream_usable(Stream *s) noexcept
Check if stream is in valid state for I/O operations.
Definition c2pa_internal.hpp:80
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:307
constexpr const char * kFormatWhitespace
ASCII whitespace ignored around a format.
Definition c2pa_internal.hpp:255
intptr_t stream_reader(StreamContext *context, uint8_t *buffer, intptr_t size)
Definition c2pa_internal.hpp:152
std::string resolve_format(const std::string &format)
Resolve a format that must be stated explicitly. These paths never infer the container type from cont...
Definition c2pa_internal.hpp:277
std::string c_string_to_string(T *c_result)
Convert C string result to C++ string with cleanup.
Definition c2pa_internal.hpp:289
intptr_t stream_seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence)
Definition c2pa_internal.hpp:123
constexpr const char * kDetectFormatFromContent
Format asking the library to determine the container type from content.
Definition c2pa_internal.hpp:250
std::unique_ptr< StreamType > open_file_binary(const std::filesystem::path &path)
Open a binary file stream with error handling.
Definition c2pa_internal.hpp:228
std::string normalize_format(const std::string &format)
Trim and lowercase a caller-supplied format; empty when format is blank.
Definition c2pa_internal.hpp:260
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:110
static int64_t tell(std::iostream *s)
Definition c2pa_internal.hpp:114
static int64_t tell(std::istream *s)
Definition c2pa_internal.hpp:93
static void seek(std::istream *s, intptr_t offset, std::ios_base::seekdir dir)
Definition c2pa_internal.hpp:90
static void seek(std::ostream *s, intptr_t offset, std::ios_base::seekdir dir)
Definition c2pa_internal.hpp:100
static int64_t tell(std::ostream *s)
Definition c2pa_internal.hpp:103
Traits (templated): how to seek and get position for a given stream type.
Definition c2pa_internal.hpp:86