use core::ffi::{c_char, c_int};
use core::ptr;
use core::slice;
use std::borrow::Cow;
use crate::abi::demux::WebPData;
use crate::abi::encode::{Mode, WebPPicture, progress_closure, with_picture};
use crate::abi::types::malloc_copy;
use crate::mux::anim_encode::{self as anim, WebPAnimEncoder as RsAnimEncoder};
use crate::mux::muxedit as edit;
use crate::mux::muxi::{CHUNK_INDEX, WebPMux as RsMux};
use crate::mux::muxinternal::{self as internal, chunk_get_index_from_four_cc};
use crate::mux::muxread as read;
use crate::webp::encode::WebPConfig;
use crate::webp::format_constants::MAX_CHUNK_PAYLOAD;
use crate::webp::mux::{
WEBP_MUX_ABI_VERSION, WebPAnimEncoderOptions, WebPChunkId, WebPMuxAnimParams, WebPMuxError,
WebPMuxFrameInfo as RsFrameInfo,
};
use crate::webp::mux_types::{WebPData as RsData, WebPMuxAnimBlend, WebPMuxAnimDispose};
use crate::webp::types::webp_abi_is_incompatible;
const MAX_IMAGE_AREA: u64 = crate::webp::format_constants::MAX_IMAGE_AREA;
const ERROR_STR_MAX_LENGTH: usize = 100;
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct WebPMuxFrameInfo {
pub bitstream: WebPData,
pub x_offset: c_int,
pub y_offset: c_int,
pub duration: c_int,
pub id: WebPChunkId,
pub dispose_method: WebPMuxAnimDispose,
pub blend_method: WebPMuxAnimBlend,
pub pad: [u32; 1],
}
pub type WebPMux = RsMux<'static>;
pub struct WebPAnimEncoder {
enc: RsAnimEncoder<'static>,
error: [c_char; ERROR_STR_MAX_LENGTH],
}
impl WebPAnimEncoder {
fn sync_error(&mut self) {
let message = anim::webp_anim_encoder_get_error(&self.enc).as_bytes();
copy_error_bytes(&mut self.error, message);
}
}
fn copy_error_bytes(error: &mut [c_char; ERROR_STR_MAX_LENGTH], message: &[u8]) {
let length = message.len().min(ERROR_STR_MAX_LENGTH - 1);
for (target, &byte) in error.iter_mut().zip(&message[..length]) {
*target = c_char::from_ne_bytes([byte]);
}
error[length] = 0;
}
unsafe fn put<T>(ptr: *mut T, value: T) {
unsafe { ptr.write(value) };
}
unsafe fn lend(data: &WebPData) -> RsData<'static> {
let bytes: &'static [u8] = unsafe { slice::from_raw_parts(data.bytes, data.size) };
RsData {
bytes: Cow::Borrowed(bytes),
}
}
fn data_to_c(data: &RsData<'_>) -> WebPData {
WebPData {
bytes: if crate::mux::muxi::data_is_null(data) {
ptr::null()
} else {
data.bytes.as_ptr()
},
size: data.bytes.len(),
}
}
fn data_to_caller(data: &RsData<'_>) -> Option<WebPData> {
let block = malloc_copy(&data.bytes);
(!block.is_null()).then_some(WebPData {
bytes: block,
size: data.bytes.len(),
})
}
unsafe fn read_fourcc(fourcc: *const c_char) -> [u8; 4] {
unsafe { fourcc.cast::<[u8; 4]>().read_unaligned() }
}
unsafe fn mux_mut<'m>(mux: *mut WebPMux) -> Option<&'m mut RsMux<'static>> {
unsafe { mux.as_mut() }
}
unsafe fn mux_ref<'m>(mux: *const WebPMux) -> Option<&'m RsMux<'static>> {
unsafe { mux.as_ref() }
}
unsafe fn encoder<'e>(enc: *mut WebPAnimEncoder) -> Option<&'e mut WebPAnimEncoder> {
unsafe { enc.as_mut() }
}
fn code<T>(result: &Result<T, WebPMuxError>) -> WebPMuxError {
WebPMuxError::of(result)
}
#[unsafe(export_name = "WebPGetMuxVersion")]
#[must_use]
pub extern "C" fn webp_get_mux_version() -> c_int {
internal::webp_get_mux_version()
}
#[unsafe(export_name = "WebPNewInternal")]
#[must_use]
pub extern "C" fn webp_new_internal(version: c_int) -> *mut WebPMux {
match edit::webp_new_internal(version) {
Some(mux) => Box::into_raw(Box::new(mux)),
None => ptr::null_mut(),
}
}
#[unsafe(export_name = "WebPMuxDelete")]
pub unsafe extern "C" fn webp_mux_delete(mux: *mut WebPMux) {
if mux.is_null() {
return;
}
let mux = unsafe { Box::from_raw(mux) };
edit::webp_mux_delete(*mux);
}
#[unsafe(export_name = "WebPMuxCreateInternal")]
#[must_use]
pub unsafe extern "C" fn webp_mux_create_internal(
bitstream: *const WebPData,
copy_data: c_int,
version: c_int,
) -> *mut WebPMux {
if webp_abi_is_incompatible(version, WEBP_MUX_ABI_VERSION) {
return ptr::null_mut(); }
let Some(bitstream) = (unsafe { bitstream.as_ref() }) else {
return ptr::null_mut();
};
if bitstream.bytes.is_null() {
return ptr::null_mut();
}
let data = unsafe { lend(bitstream) };
match read::webp_mux_create_internal(&data, copy_data, version) {
Some(mux) => Box::into_raw(Box::new(mux)),
None => ptr::null_mut(),
}
}
#[unsafe(export_name = "WebPMuxSetChunk")]
#[must_use]
pub unsafe extern "C" fn webp_mux_set_chunk(
mux: *mut WebPMux,
fourcc: *const c_char,
chunk_data: *const WebPData,
copy_data: c_int,
) -> WebPMuxError {
let mux = unsafe { mux_mut(mux) };
let chunk_data = unsafe { chunk_data.as_ref() };
let (Some(mux), Some(chunk_data)) = (mux, chunk_data) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if fourcc.is_null()
|| chunk_data.bytes.is_null()
|| chunk_data.size as u64 > u64::from(MAX_CHUNK_PAYLOAD)
{
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let tag = unsafe { read_fourcc(fourcc) };
let data = unsafe { lend(chunk_data) };
code(&edit::webp_mux_set_chunk(mux, &tag, &data, copy_data))
}
#[unsafe(export_name = "WebPMuxGetChunk")]
#[must_use]
pub unsafe extern "C" fn webp_mux_get_chunk(
mux: *const WebPMux,
fourcc: *const c_char,
chunk_data: *mut WebPData,
) -> WebPMuxError {
let Some(mux) = (unsafe { mux_ref(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if fourcc.is_null() || chunk_data.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let tag = unsafe { read_fourcc(fourcc) };
unsafe { get_chunk(read::webp_mux_get_chunk(mux, &tag), &tag, chunk_data) }
}
unsafe fn get_chunk(
result: Result<&RsData<'_>, WebPMuxError>,
tag: &[u8; 4],
chunk_data: *mut WebPData,
) -> WebPMuxError {
match result {
Ok(data) => {
unsafe { put(chunk_data, data_to_c(data)) };
WebPMuxError::WEBP_MUX_OK
}
Err(err) => {
if err == WebPMuxError::WEBP_MUX_NOT_FOUND
&& chunk_get_index_from_four_cc(tag) != CHUNK_INDEX::IDX_UNKNOWN
{
let empty = WebPData {
bytes: ptr::null(),
size: 0,
};
unsafe { put(chunk_data, empty) };
}
err
}
}
}
#[unsafe(export_name = "WebPMuxDeleteChunk")]
#[must_use]
pub unsafe extern "C" fn webp_mux_delete_chunk(
mux: *mut WebPMux,
fourcc: *const c_char,
) -> WebPMuxError {
let Some(mux) = (unsafe { mux_mut(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if fourcc.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let tag = unsafe { read_fourcc(fourcc) };
code(&edit::webp_mux_delete_chunk(mux, &tag))
}
#[unsafe(export_name = "WebPMuxSetImage")]
#[must_use]
pub unsafe extern "C" fn webp_mux_set_image(
mux: *mut WebPMux,
bitstream: *const WebPData,
copy_data: c_int,
) -> WebPMuxError {
let mux = unsafe { mux_mut(mux) };
let bitstream = unsafe { bitstream.as_ref() };
let (Some(mux), Some(bitstream)) = (mux, bitstream) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if bitstream.bytes.is_null() || bitstream.size as u64 > u64::from(MAX_CHUNK_PAYLOAD) {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let data = unsafe { lend(bitstream) };
code(&edit::webp_mux_set_image(mux, &data, copy_data))
}
#[unsafe(export_name = "WebPMuxPushFrame")]
#[must_use]
pub unsafe extern "C" fn webp_mux_push_frame(
mux: *mut WebPMux,
frame: *const WebPMuxFrameInfo,
copy_data: c_int,
) -> WebPMuxError {
let mux = unsafe { mux_mut(mux) };
let frame = unsafe { frame.as_ref() };
let (Some(mux), Some(frame)) = (mux, frame) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if frame.id != WebPChunkId::WEBP_CHUNK_ANMF {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
if frame.bitstream.bytes.is_null() || frame.bitstream.size as u64 > u64::from(MAX_CHUNK_PAYLOAD)
{
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let info = RsFrameInfo {
bitstream: unsafe { lend(&frame.bitstream) },
x_offset: frame.x_offset,
y_offset: frame.y_offset,
duration: frame.duration,
id: frame.id,
dispose_method: frame.dispose_method,
blend_method: frame.blend_method,
pad: frame.pad,
};
code(&edit::webp_mux_push_frame(mux, &info, copy_data))
}
#[unsafe(export_name = "WebPMuxGetFrame")]
#[must_use]
pub unsafe extern "C" fn webp_mux_get_frame(
mux: *const WebPMux,
nth: u32,
frame: *mut WebPMuxFrameInfo,
) -> WebPMuxError {
let Some(mux) = (unsafe { mux_ref(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if frame.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let info = match read::webp_mux_get_frame(mux, nth) {
Ok(info) => info,
Err(err) => return err,
};
let bitstream = data_to_caller(&info.bitstream);
let field = unsafe { &raw mut (*frame).x_offset };
unsafe { put(field, info.x_offset) };
let field = unsafe { &raw mut (*frame).y_offset };
unsafe { put(field, info.y_offset) };
let field = unsafe { &raw mut (*frame).duration };
unsafe { put(field, info.duration) };
let field = unsafe { &raw mut (*frame).id };
unsafe { put(field, info.id) };
let field = unsafe { &raw mut (*frame).dispose_method };
unsafe { put(field, info.dispose_method) };
let field = unsafe { &raw mut (*frame).blend_method };
unsafe { put(field, info.blend_method) };
let Some(bitstream) = bitstream else {
return WebPMuxError::WEBP_MUX_MEMORY_ERROR;
};
let field = unsafe { &raw mut (*frame).bitstream };
unsafe { put(field, bitstream) };
WebPMuxError::WEBP_MUX_OK
}
#[unsafe(export_name = "WebPMuxDeleteFrame")]
#[must_use]
pub unsafe extern "C" fn webp_mux_delete_frame(mux: *mut WebPMux, nth: u32) -> WebPMuxError {
let Some(mux) = (unsafe { mux_mut(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
code(&edit::webp_mux_delete_frame(mux, nth))
}
#[unsafe(export_name = "WebPMuxSetAnimationParams")]
#[must_use]
pub unsafe extern "C" fn webp_mux_set_animation_params(
mux: *mut WebPMux,
params: *const WebPMuxAnimParams,
) -> WebPMuxError {
let mux = unsafe { mux_mut(mux) };
let params = unsafe { params.as_ref() };
let (Some(mux), Some(params)) = (mux, params) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
code(&edit::webp_mux_set_animation_params(mux, params))
}
#[unsafe(export_name = "WebPMuxGetAnimationParams")]
#[must_use]
pub unsafe extern "C" fn webp_mux_get_animation_params(
mux: *const WebPMux,
params: *mut WebPMuxAnimParams,
) -> WebPMuxError {
let Some(mux) = (unsafe { mux_ref(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if params.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
match read::webp_mux_get_animation_params(mux) {
Ok(value) => {
unsafe { put(params, value) };
WebPMuxError::WEBP_MUX_OK
}
Err(err) => err,
}
}
#[unsafe(export_name = "WebPMuxSetCanvasSize")]
#[must_use]
pub unsafe extern "C" fn webp_mux_set_canvas_size(
mux: *mut WebPMux,
width: c_int,
height: c_int,
) -> WebPMuxError {
let Some(mux) = (unsafe { mux_mut(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
code(&edit::webp_mux_set_canvas_size(mux, width, height))
}
#[unsafe(export_name = "WebPMuxGetCanvasSize")]
#[must_use]
pub unsafe extern "C" fn webp_mux_get_canvas_size(
mux: *const WebPMux,
width: *mut c_int,
height: *mut c_int,
) -> WebPMuxError {
let Some(mux) = (unsafe { mux_ref(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if width.is_null() || height.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
match read::webp_mux_get_canvas_size(mux) {
Ok((w, h)) => {
unsafe { put(width, w) };
unsafe { put(height, h) };
WebPMuxError::WEBP_MUX_OK
}
Err(err) => err,
}
}
#[unsafe(export_name = "WebPMuxGetFeatures")]
#[must_use]
pub unsafe extern "C" fn webp_mux_get_features(
mux: *const WebPMux,
flags: *mut u32,
) -> WebPMuxError {
let Some(mux) = (unsafe { mux_ref(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if flags.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
match read::webp_mux_get_features(mux) {
Ok(value) => {
unsafe { put(flags, value) };
WebPMuxError::WEBP_MUX_OK
}
Err(err) => err,
}
}
#[unsafe(export_name = "WebPMuxNumChunks")]
#[must_use]
pub unsafe extern "C" fn webp_mux_num_chunks(
mux: *const WebPMux,
id: WebPChunkId,
num_elements: *mut c_int,
) -> WebPMuxError {
let Some(mux) = (unsafe { mux_ref(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if num_elements.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
match read::webp_mux_num_chunks(mux, id) {
Ok(value) => {
unsafe { put(num_elements, value) };
WebPMuxError::WEBP_MUX_OK
}
Err(err) => err,
}
}
#[unsafe(export_name = "WebPMuxAssemble")]
#[must_use]
pub unsafe extern "C" fn webp_mux_assemble(
mux: *mut WebPMux,
assembled_data: *mut WebPData,
) -> WebPMuxError {
if assembled_data.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let empty = WebPData {
bytes: ptr::null(),
size: 0,
};
unsafe { put(assembled_data, empty) };
let Some(mux) = (unsafe { mux_mut(mux) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
let data = match edit::webp_mux_assemble(mux) {
Ok(data) => data,
Err(err) => return err,
};
let Some(data) = data_to_caller(&data) else {
return WebPMuxError::WEBP_MUX_MEMORY_ERROR;
};
unsafe { put(assembled_data, data) };
WebPMuxError::WEBP_MUX_OK
}
#[unsafe(export_name = "WebPAnimEncoderOptionsInitInternal")]
#[must_use]
pub unsafe extern "C" fn webp_anim_encoder_options_init_internal(
enc_options: *mut WebPAnimEncoderOptions,
abi_version: c_int,
) -> c_int {
if enc_options.is_null() {
return 0;
}
let mut defaults = WebPAnimEncoderOptions::default();
if !anim::webp_anim_encoder_options_init_internal(&mut defaults, abi_version) {
return 0;
}
let field = unsafe { &raw mut (*enc_options).anim_params };
unsafe { put(field, defaults.anim_params) };
let field = unsafe { &raw mut (*enc_options).minimize_size };
unsafe { put(field, defaults.minimize_size) };
let field = unsafe { &raw mut (*enc_options).kmin };
unsafe { put(field, defaults.kmin) };
let field = unsafe { &raw mut (*enc_options).kmax };
unsafe { put(field, defaults.kmax) };
let field = unsafe { &raw mut (*enc_options).allow_mixed };
unsafe { put(field, defaults.allow_mixed) };
let field = unsafe { &raw mut (*enc_options).verbose };
unsafe { put(field, defaults.verbose) };
1
}
unsafe fn read_options(options: *const WebPAnimEncoderOptions) -> WebPAnimEncoderOptions {
unsafe fn get<T>(field: *const T) -> T {
unsafe { field.read() }
}
let anim_params = unsafe { &raw const (*options).anim_params };
let minimize_size = unsafe { &raw const (*options).minimize_size };
let kmin = unsafe { &raw const (*options).kmin };
let kmax = unsafe { &raw const (*options).kmax };
let allow_mixed = unsafe { &raw const (*options).allow_mixed };
let verbose = unsafe { &raw const (*options).verbose };
WebPAnimEncoderOptions {
anim_params: unsafe { get(anim_params) },
minimize_size: unsafe { get(minimize_size) },
kmin: unsafe { get(kmin) },
kmax: unsafe { get(kmax) },
allow_mixed: unsafe { get(allow_mixed) },
verbose: unsafe { get(verbose) },
padding: [0; 4],
}
}
#[unsafe(export_name = "WebPAnimEncoderNewInternal")]
#[must_use]
pub unsafe extern "C" fn webp_anim_encoder_new_internal(
width: c_int,
height: c_int,
enc_options: *const WebPAnimEncoderOptions,
abi_version: c_int,
) -> *mut WebPAnimEncoder {
if webp_abi_is_incompatible(abi_version, WEBP_MUX_ABI_VERSION)
|| width <= 0
|| height <= 0
|| u64::from(width.unsigned_abs()) * u64::from(height.unsigned_abs()) >= MAX_IMAGE_AREA
{
return ptr::null_mut();
}
let options = if enc_options.is_null() {
None
} else {
Some(unsafe { read_options(enc_options) })
};
let Some(enc) =
anim::webp_anim_encoder_new_internal(width, height, options.as_ref(), abi_version)
else {
return ptr::null_mut();
};
let mut handle = Box::new(WebPAnimEncoder {
enc,
error: [0; ERROR_STR_MAX_LENGTH],
});
handle.sync_error();
Box::into_raw(handle)
}
#[unsafe(export_name = "WebPAnimEncoderAdd")]
#[must_use]
pub unsafe extern "C" fn webp_anim_encoder_add(
enc: *mut WebPAnimEncoder,
frame: *mut WebPPicture,
timestamp: c_int,
encoder_config: *const WebPConfig,
) -> c_int {
let Some(handle) = (unsafe { encoder(enc) }) else {
return 0;
};
let config: Option<WebPConfig> = unsafe { encoder_config.as_ref() }.copied();
let ok = match unsafe { frame.as_ref() } {
None => anim::webp_anim_encoder_add(&mut handle.enc, None, timestamp, config.as_ref()),
Some(c) => {
let hook = c.progress_hook;
let modes = if c.use_argb != 0 {
(Mode::Discard, Mode::Read)
} else {
(Mode::Read, Mode::Discard)
};
let enc = &mut handle.enc;
let result = unsafe {
with_picture(frame, modes, true, |rs| {
rs.progress_hook = hook.map(|hook| progress_closure(hook, frame));
let ok = anim::webp_anim_encoder_add(enc, Some(rs), timestamp, config.as_ref());
rs.progress_hook = None;
ok
})
};
if let Some((ok, written)) = result {
ok && written
} else {
anim::mark_error(
&mut handle.enc,
"ERROR adding frame: the C ABI cannot lend the frame's planes",
);
false
}
}
};
handle.sync_error();
c_int::from(ok)
}
static UNWRITTEN: [u8; 1] = [0];
#[unsafe(export_name = "WebPAnimEncoderAssemble")]
#[must_use]
pub unsafe extern "C" fn webp_anim_encoder_assemble(
enc: *mut WebPAnimEncoder,
webp_data: *mut WebPData,
) -> c_int {
let Some(handle) = (unsafe { encoder(enc) }) else {
return 0;
};
if webp_data.is_null() {
anim::mark_error(&mut handle.enc, "ERROR assembling: NULL input");
handle.sync_error();
return 0;
}
let mut data = RsData {
bytes: Cow::Borrowed(&UNWRITTEN),
};
let mut ok = anim::webp_anim_encoder_assemble(&mut handle.enc, &mut data);
if !ptr::eq(data.bytes.as_ptr(), UNWRITTEN.as_ptr()) {
let value = if data.bytes.is_empty() {
WebPData {
bytes: ptr::null(),
size: 0,
}
} else if let Some(value) = data_to_caller(&data) {
value
} else {
ok = false;
WebPData {
bytes: ptr::null(),
size: 0,
}
};
unsafe { put(webp_data, value) };
}
handle.sync_error();
c_int::from(ok)
}
#[unsafe(export_name = "WebPAnimEncoderGetError")]
#[must_use]
pub unsafe extern "C" fn webp_anim_encoder_get_error(enc: *mut WebPAnimEncoder) -> *const c_char {
let Some(handle) = (unsafe { encoder(enc) }) else {
return ptr::null();
};
handle.sync_error();
handle.error.as_ptr()
}
#[unsafe(export_name = "WebPAnimEncoderDelete")]
pub unsafe extern "C" fn webp_anim_encoder_delete(enc: *mut WebPAnimEncoder) {
if enc.is_null() {
return;
}
let handle = unsafe { Box::from_raw(enc) };
anim::webp_anim_encoder_delete(handle.enc);
}
#[unsafe(export_name = "WebPAnimEncoderSetChunk")]
#[must_use]
pub unsafe extern "C" fn webp_anim_encoder_set_chunk(
enc: *mut WebPAnimEncoder,
fourcc: *const c_char,
chunk_data: *const WebPData,
copy_data: c_int,
) -> WebPMuxError {
let Some(handle) = (unsafe { encoder(enc) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
let Some(chunk_data) = (unsafe { chunk_data.as_ref() }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if fourcc.is_null()
|| chunk_data.bytes.is_null()
|| chunk_data.size as u64 > u64::from(MAX_CHUNK_PAYLOAD)
{
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let tag = unsafe { read_fourcc(fourcc) };
let data = unsafe { lend(chunk_data) };
code(&anim::webp_anim_encoder_set_chunk(
&mut handle.enc,
&tag,
&data,
copy_data,
))
}
#[unsafe(export_name = "WebPAnimEncoderGetChunk")]
#[must_use]
pub unsafe extern "C" fn webp_anim_encoder_get_chunk(
enc: *const WebPAnimEncoder,
fourcc: *const c_char,
chunk_data: *mut WebPData,
) -> WebPMuxError {
let Some(handle) = (unsafe { enc.as_ref() }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if fourcc.is_null() || chunk_data.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let tag = unsafe { read_fourcc(fourcc) };
unsafe {
get_chunk(
anim::webp_anim_encoder_get_chunk(&handle.enc, &tag),
&tag,
chunk_data,
)
}
}
#[unsafe(export_name = "WebPAnimEncoderDeleteChunk")]
#[must_use]
pub unsafe extern "C" fn webp_anim_encoder_delete_chunk(
enc: *mut WebPAnimEncoder,
fourcc: *const c_char,
) -> WebPMuxError {
let Some(handle) = (unsafe { encoder(enc) }) else {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
};
if fourcc.is_null() {
return WebPMuxError::WEBP_MUX_INVALID_ARGUMENT;
}
let tag = unsafe { read_fourcc(fourcc) };
code(&anim::webp_anim_encoder_delete_chunk(&mut handle.enc, &tag))
}
#[cfg(test)]
mod tests {
use super::{ERROR_STR_MAX_LENGTH, c_char, copy_error_bytes};
#[test]
fn error_bytes_preserve_high_bits_and_terminate_when_truncated() {
let message = [0x80, 0xff, b'A'];
let mut error = [0 as c_char; ERROR_STR_MAX_LENGTH];
copy_error_bytes(&mut error, &message);
assert_eq!(
error[..3]
.iter()
.map(|byte| byte.to_ne_bytes()[0])
.collect::<Vec<_>>(),
message
);
assert_eq!(error[3], 0);
let long_message = [0xff; ERROR_STR_MAX_LENGTH + 1];
copy_error_bytes(&mut error, &long_message);
assert!(
error[..ERROR_STR_MAX_LENGTH - 1]
.iter()
.all(|byte| byte.to_ne_bytes() == [0xff])
);
assert_eq!(error[ERROR_STR_MAX_LENGTH - 1], 0);
}
}