From d4789df7b7feb4a6b9c38d0c19bebd52ea6f9bed Mon Sep 17 00:00:00 2001 From: Tania Mathern Date: Thu, 15 May 2025 11:19:55 -0700 Subject: [PATCH] fix: Change macros --- include/c2pa.h | 10 ---------- src/c_api.rs | 40 ++++++++++++++++++---------------------- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/include/c2pa.h b/include/c2pa.h index 0955e168..10721e63 100644 --- a/include/c2pa.h +++ b/include/c2pa.h @@ -353,16 +353,6 @@ struct C2paReader *c2pa_reader_from_stream(const char *format, * Reads from NULL-terminated C strings. * The returned value MUST be released by calling c2pa_reader_free * and it is no longer valid after that call. - * - * # Example - * ```c - * auto result = c2pa_reader_from_manifest_data_and_stream(manifest_data, manifest_data_size, "image/jpeg", stream); - * if (result == NULL) { - * let error = c2pa_error(); - * printf("Error: %s\n", error); - * c2pa_string_free(error); - * } - * ``` */ C2PA_API extern struct C2paReader *c2pa_reader_from_manifest_data_and_stream(const char *format, diff --git a/src/c_api.rs b/src/c_api.rs index 9da19db6..e1c38dfb 100644 --- a/src/c_api.rs +++ b/src/c_api.rs @@ -75,6 +75,12 @@ pub struct C2paSigner { // Null check macro for C pointers. #[macro_export] macro_rules! null_check { + ($ptr : expr) => { + if $ptr.is_null() { + Error::set_last(Error::NullParameter(stringify!($ptr).to_string())); + return std::ptr::null_mut(); + } + }; (($ptr:expr), $transform:expr, $default:expr) => { if $ptr.is_null() { Error::set_last(Error::NullParameter(stringify!($ptr).to_string())); @@ -96,6 +102,18 @@ macro_rules! null_check_int { }; } +/// If the expression is null, set the last error and return std::ptr::null_mut(). +#[macro_export] +macro_rules! from_cstr_or_return_null { + ($ptr : expr) => { + null_check!( + ($ptr), + |ptr| { std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned() }, + std::ptr::null_mut() + ) + }; +} + // Internal routine to convert a *const c_char to a rust String or return a NULL error. #[macro_export] macro_rules! from_cstr_null_check { @@ -170,18 +188,6 @@ macro_rules! return_boxed { }; } -/// If the expression is null, set the last error and return std::ptr::null_mut(). -#[macro_export] -macro_rules! from_cstr_or_return_null { - ($ptr : expr) => { - null_check!( - ($ptr), - |ptr| { std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned() }, - std::ptr::null_mut() - ) - }; -} - /// Defines a callback to read from a stream. /// /// # Parameters @@ -478,16 +484,6 @@ pub unsafe extern "C" fn c2pa_reader_from_stream( /// Reads from NULL-terminated C strings. /// The returned value MUST be released by calling c2pa_reader_free /// and it is no longer valid after that call. -/// -/// # Example -/// ```c -/// auto result = c2pa_reader_from_manifest_data_and_stream(manifest_data, manifest_data_size, "image/jpeg", stream); -/// if (result == NULL) { -/// let error = c2pa_error(); -/// printf("Error: %s\n", error); -/// c2pa_string_free(error); -/// } -/// ``` #[no_mangle] pub unsafe extern "C" fn c2pa_reader_from_manifest_data_and_stream( format: *const c_char,