18#ifndef C2PA_INTERNAL_HPP
19#define C2PA_INTERNAL_HPP
41 std::vector<std::string> result;
42 if (mime_types ==
nullptr) {
return result; }
44 result.reserve(count);
45 for(uintptr_t i = 0; i < count; i++) {
46 result.emplace_back(mime_types[i]);
49 c2pa_free_string_array(mime_types, count);
56 case C2paSeekMode::Start:
return std::ios_base::beg;
57 case C2paSeekMode::Current:
return std::ios_base::cur;
58 case C2paSeekMode::End:
return std::ios_base::end;
59 default:
return std::ios_base::beg;
64template<
typename Stream>
66 return s && !s->bad();
70template<
typename Stream>
75 static void seek(std::istream* s, intptr_t offset, std::ios_base::seekdir dir) {
76 s->seekg(offset, dir);
78 static int64_t
tell(std::istream* s) {
79 return static_cast<int64_t
>(s->tellg());
85 static void seek(std::ostream* s, intptr_t offset, std::ios_base::seekdir dir) {
86 s->seekp(offset, dir);
88 static int64_t
tell(std::ostream* s) {
89 return static_cast<int64_t
>(s->tellp());
95 static void seek(std::iostream* s, intptr_t offset, std::ios_base::seekdir dir) {
96 s->seekg(offset, dir);
97 s->seekp(offset, dir);
99 static int64_t
tell(std::iostream* s) {
100 return static_cast<int64_t
>(s->tellp());
105template<
typename Stream>
106intptr_t
stream_seeker(StreamContext* context, intptr_t offset, C2paSeekMode whence) {
107 auto* stream =
reinterpret_cast<Stream*
>(context);
114 if (stream->fail()) {
124 return static_cast<intptr_t
>(pos);
128template<
typename Stream>
129intptr_t
stream_reader(StreamContext* context, uint8_t* buffer, intptr_t size) {
130 if (!context || !buffer) {
139 auto* stream =
reinterpret_cast<Stream*
>(context);
143 stream->read(
reinterpret_cast<char*
>(buffer), size);
144 if (stream->fail()) {
145 if (!stream->eof()) {
152 return static_cast<intptr_t
>(stream->gcount());
156template<
typename Stream,
typename Op>
158 auto* stream =
reinterpret_cast<Stream*
>(context);
162 const intptr_t result = op(stream);
163 if (stream->fail()) {
173template<
typename Stream>
174intptr_t
stream_writer(StreamContext* context,
const uint8_t* buffer, intptr_t size) {
176 s->write(
reinterpret_cast<const char*
>(
buffer),
size);
182template<
typename Stream>
194template<
typename StreamType>
197 auto stream = std::make_unique<StreamType>(
199 std::ios_base::binary
211 auto ext =
path.extension().string();
212 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:86
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:40
intptr_t stream_writer(StreamContext *context, const uint8_t *buffer, intptr_t size)
Writer impl.
Definition c2pa_internal.hpp:174
intptr_t stream_op(StreamContext *context, Op op)
Get stream from context, used by writer and flusher.
Definition c2pa_internal.hpp:157
std::string extract_file_extension(const std::filesystem::path &path) noexcept
Extract file extension without the leading dot.
Definition c2pa_internal.hpp:210
intptr_t stream_flusher(StreamContext *context)
Flusher impl.
Definition c2pa_internal.hpp:183
constexpr std::ios_base::seekdir whence_to_seekdir(C2paSeekMode whence) noexcept
Maps C2PA seek mode to std::ios seek direction.
Definition c2pa_internal.hpp:54
bool is_stream_usable(Stream *s) noexcept
Check if stream is in valid state for I/O operations.
Definition c2pa_internal.hpp:65
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:235
intptr_t stream_reader(StreamContext *context, uint8_t *buffer, intptr_t size)
Reader impl.
Definition c2pa_internal.hpp:129
std::string c_string_to_string(T *c_result)
Convert C string result to C++ string with cleanup.
Definition c2pa_internal.hpp:219
intptr_t stream_seeker(StreamContext *context, intptr_t offset, C2paSeekMode whence)
Seeker impl.
Definition c2pa_internal.hpp:106
std::unique_ptr< StreamType > open_file_binary(const std::filesystem::path &path)
Open a binary file stream with error handling.
Definition c2pa_internal.hpp:195
int stream_error_return(StreamError e) noexcept
Set errno from StreamError and return error sentinel.
Definition c2pa.hpp:78
static void seek(std::iostream *s, intptr_t offset, std::ios_base::seekdir dir)
Definition c2pa_internal.hpp:95
static int64_t tell(std::iostream *s)
Definition c2pa_internal.hpp:99
static int64_t tell(std::istream *s)
Definition c2pa_internal.hpp:78
static void seek(std::istream *s, intptr_t offset, std::ios_base::seekdir dir)
Definition c2pa_internal.hpp:75
static void seek(std::ostream *s, intptr_t offset, std::ios_base::seekdir dir)
Definition c2pa_internal.hpp:85
static int64_t tell(std::ostream *s)
Definition c2pa_internal.hpp:88
Traits (templated): how to seek and get position for a given stream type.
Definition c2pa_internal.hpp:71