c2pa-cpp
C++ API for the C2PA SDK
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

bool error_indicates_manifest_not_found (const char *message) noexcept
 True if the C2PA error message indicates no JUMBF / manifest in the asset (ManifestNotFound).
 
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().

45 {
46 std::vector<std::string> result;
47 if (mime_types == nullptr) { return result; }
48
49 result.reserve(count);
50 for(uintptr_t i = 0; i < count; i++) {
51 result.emplace_back(mime_types[i]);
52 }
53
54 c2pa_free_string_array(mime_types, count);
55 return result;
56}

◆ 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)
224 {
225 if (c_result == nullptr) {
226 throw C2paException();
227 }
228 std::string str(c_result);
229 c2pa_free(c_result);
230 return str;
231}
Exception class for C2pa errors. This class is used to throw exceptions for errors encountered by the...
Definition c2pa.hpp:87

◆ error_indicates_manifest_not_found()

bool c2pa::detail::error_indicates_manifest_not_found ( const char message)
inlinenoexcept

True if the C2PA error message indicates no JUMBF / manifest in the asset (ManifestNotFound).

35 {
36 return message != nullptr && std::strstr(message, "ManifestNotFound") != nullptr;
37}

◆ 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")
215 {
216 auto ext = path.extension().string();
217 return ext.empty() ? "" : ext.substr(1);
218}

◆ 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.

70 {
71 return s && !s->bad();
72}

◆ 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
201{
202 auto stream = std::make_unique<StreamType>(
203 path,
204 std::ios_base::binary
205 );
206 if (!stream->is_open()) {
207 throw C2paException("Failed to open file: " + path.string());
208 }
209 return stream;
210}

◆ stream_flusher()

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

Flusher impl.

188 {
189 return stream_op<Stream>(context, [](Stream* s) {
190 s->flush();
191 return 0;
192 });
193}

◆ 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.

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

◆ stream_reader()

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

Reader impl.

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

◆ stream_seeker()

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

Seeker impl.

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

◆ stream_writer()

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

Writer impl.

179 {
180 return stream_op<Stream>(context, [buffer, size](Stream* s) {
181 s->write(reinterpret_cast<const char*>(buffer), size);
182 return size;
183 });
184}

◆ 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.

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

◆ 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.

59 {
60 switch (whence) {
61 case C2paSeekMode::Start: return std::ios_base::beg;
62 case C2paSeekMode::Current: return std::ios_base::cur;
63 case C2paSeekMode::End: return std::ios_base::end;
64 default: return std::ios_base::beg;
65 }
66}