c2pa-c
C++ API for c2pa-c library
Loading...
Searching...
No Matches
c2pa::detail Namespace Reference

Classes

struct  StreamSeekTraits
 Traits (templated): how to seek and get position for a given stream type. More...
 
struct  StreamSeekTraits< std::iostream >
 
struct  StreamSeekTraits< std::istream >
 
struct  StreamSeekTraits< std::ostream >
 

Functions

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.
 
constexpr std::ios_base::seekdir whence_to_seekdir (C2paSeekMode whence) noexcept
 Maps C2PA seek mode to std::ios seek direction.
 
template<typename Stream >
bool is_stream_usable (Stream *s) noexcept
 Check if stream is in valid state for I/O operations.
 
template<typename Stream >
intptr_t stream_seeker (StreamContext *context, intptr_t offset, C2paSeekMode whence)
 Seeker impl.
 
template<typename Stream >
intptr_t stream_reader (StreamContext *context, uint8_t *buffer, intptr_t size)
 Reader impl.
 
template<typename Stream , typename Op >
intptr_t stream_op (StreamContext *context, Op op)
 Get stream from context, used by writer and flusher.
 
template<typename Stream >
intptr_t stream_writer (StreamContext *context, const uint8_t *buffer, intptr_t size)
 Writer impl.
 
template<typename Stream >
intptr_t stream_flusher (StreamContext *context)
 Flusher impl.
 
template<typename StreamType >
std::unique_ptr< StreamTypeopen_file_binary (const std::filesystem::path &path)
 Open a binary file stream with error handling.
 
std::string extract_file_extension (const std::filesystem::path &path) noexcept
 Extract file extension without the leading dot.
 
template<typename T >
std::string c_string_to_string (T *c_result)
 Convert C string result to C++ string with cleanup.
 
std::vector< unsigned charto_byte_vector (const unsigned char *data, int64_t size)
 Convert C byte array result to C++ vector.
 

Function Documentation

◆ c_mime_types_to_vector()

std::vector< std::string > c2pa::detail::c_mime_types_to_vector ( const char *const mime_types,
uintptr_t  count 
)
inline

Converts a C array of C strings to a std::vector of std::string.

Parameters
mime_typesPointer to an array of C strings (const char*).
countNumber of elements in the array.
Returns
A std::vector containing the strings from the input array.

This function takes ownership of the input array and frees it using c2pa_free_string_array().

40 {
41 std::vector<std::string> result;
42 if (mime_types == nullptr) { return result; }
43
44 result.reserve(count);
45 for(uintptr_t i = 0; i < count; i++) {
46 result.emplace_back(mime_types[i]);
47 }
48
49 c2pa_free_string_array(mime_types, count);
50 return result;
51}

◆ c_string_to_string()

template<typename T >
std::string c2pa::detail::c_string_to_string ( T c_result)
inline

Convert C string result to C++ string with cleanup.

Parameters
c_resultRaw C string from C API
Returns
C++ string (throws if null)
219 {
220 if (c_result == nullptr) {
221 throw C2paException();
222 }
223 std::string str(c_result);
224 c2pa_free(c_result);
225 return str;
226}
Exception class for C2pa errors. This class is used to throw exceptions for errors encountered by the...
Definition c2pa.hpp:86

◆ extract_file_extension()

std::string c2pa::detail::extract_file_extension ( const std::filesystem::path &  path)
inlinenoexcept

Extract file extension without the leading dot.

Parameters
pathFilesystem path
Returns
Extension string (e.g., "jpg" not ".jpg")
210 {
211 auto ext = path.extension().string();
212 return ext.empty() ? "" : ext.substr(1);
213}

◆ is_stream_usable()

template<typename Stream >
bool c2pa::detail::is_stream_usable ( Stream s)
inlinenoexcept

Check if stream is in valid state for I/O operations.

65 {
66 return s && !s->bad();
67}

◆ open_file_binary()

template<typename StreamType >
std::unique_ptr< StreamType > c2pa::detail::open_file_binary ( const std::filesystem::path &  path)
inline

Open a binary file stream with error handling.

Template Parameters
StreamTypestd::ifstream or std::ofstream
Parameters
pathPath to the file
Returns
Unique pointer to opened stream
196{
197 auto stream = std::make_unique<StreamType>(
198 path,
199 std::ios_base::binary
200 );
201 if (!stream->is_open()) {
202 throw C2paException("Failed to open file: " + path.string());
203 }
204 return stream;
205}

◆ stream_flusher()

template<typename Stream >
intptr_t c2pa::detail::stream_flusher ( StreamContext context)

Flusher impl.

183 {
184 return stream_op<Stream>(context, [](Stream* s) {
185 s->flush();
186 return 0;
187 });
188}

◆ stream_op()

template<typename Stream , typename Op >
intptr_t c2pa::detail::stream_op ( StreamContext *  context,
Op  op 
)

Get stream from context, used by writer and flusher.

157 {
158 auto* stream = reinterpret_cast<Stream*>(context);
159 if (!is_stream_usable(stream)) {
160 return stream_error_return(StreamError::IoError);
161 }
162 const intptr_t result = op(stream);
163 if (stream->fail()) {
164 return stream_error_return(StreamError::InvalidArgument);
165 }
166 if (stream->bad()) {
167 return stream_error_return(StreamError::IoError);
168 }
169 return result;
170}
bool is_stream_usable(Stream *s) noexcept
Check if stream is in valid state for I/O operations.
Definition c2pa_internal.hpp:65
int stream_error_return(StreamError e) noexcept
Set errno from StreamError and return error sentinel.
Definition c2pa.hpp:78

◆ stream_reader()

template<typename Stream >
intptr_t c2pa::detail::stream_reader ( StreamContext context,
uint8_t buffer,
intptr_t  size 
)

Reader impl.

129 {
130 if (!context || !buffer) {
131 return stream_error_return(StreamError::InvalidArgument);
132 }
133 if (size < 0) {
134 return stream_error_return(StreamError::InvalidArgument);
135 }
136 if (size == 0) {
137 return 0;
138 }
139 auto* stream = reinterpret_cast<Stream*>(context);
140 if (!is_stream_usable(stream)) {
141 return stream_error_return(StreamError::IoError);
142 }
143 stream->read(reinterpret_cast<char*>(buffer), size);
144 if (stream->fail()) {
145 if (!stream->eof()) {
146 return stream_error_return(StreamError::InvalidArgument);
147 }
148 }
149 if (stream->bad()) {
150 return stream_error_return(StreamError::IoError);
151 }
152 return static_cast<intptr_t>(stream->gcount());
153}

◆ stream_seeker()

template<typename Stream >
intptr_t c2pa::detail::stream_seeker ( StreamContext context,
intptr_t  offset,
C2paSeekMode  whence 
)

Seeker impl.

106 {
107 auto* stream = reinterpret_cast<Stream*>(context);
108 if (!is_stream_usable(stream)) {
109 return stream_error_return(StreamError::IoError);
110 }
111 const std::ios_base::seekdir dir = whence_to_seekdir(whence);
112 stream->clear();
113 StreamSeekTraits<Stream>::seek(stream, offset, dir);
114 if (stream->fail()) {
115 return stream_error_return(StreamError::InvalidArgument);
116 }
117 if (stream->bad()) {
118 return stream_error_return(StreamError::IoError);
119 }
120 const int64_t pos = StreamSeekTraits<Stream>::tell(stream);
121 if (pos < 0) {
122 return stream_error_return(StreamError::IoError);
123 }
124 return static_cast<intptr_t>(pos);
125}

◆ stream_writer()

template<typename Stream >
intptr_t c2pa::detail::stream_writer ( StreamContext context,
const uint8_t buffer,
intptr_t  size 
)

Writer impl.

174 {
175 return stream_op<Stream>(context, [buffer, size](Stream* s) {
176 s->write(reinterpret_cast<const char*>(buffer), size);
177 return size;
178 });
179}

◆ to_byte_vector()

std::vector< unsigned char > c2pa::detail::to_byte_vector ( const unsigned char data,
int64_t  size 
)
inline

Convert C byte array result to C++ vector.

Parameters
dataRaw byte array from C API
sizeSize of the byte array (result from C API call)
Returns
Vector containing the bytes (throws if null or negative size)

This helper extracts the pattern of checking C API results, copying to a vector, and freeing the C-allocated memory. The C API contract is: if result < 0 or data == nullptr, the operation failed.

235 {
236 if (size < 0 || data == nullptr) {
237 c2pa_free(data); // May be null or allocated, c2pa_free handles both
238 throw C2paException();
239 }
240
241 auto result = std::vector<unsigned char>(data, data + size);
242 c2pa_free(data);
243 return result;
244}

◆ whence_to_seekdir()

constexpr std::ios_base::seekdir c2pa::detail::whence_to_seekdir ( C2paSeekMode  whence)
constexprnoexcept

Maps C2PA seek mode to std::ios seek direction.

54 {
55 switch (whence) {
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;
60 }
61}