Move the public ZBUFF compatibility layer into the Rust static archive while keeping stream contexts opaque and forwarding to the current ZSTD streaming APIs. The deprecated C translation units are now declaration-only shims, so the compatibility symbols work in compression-only and decompression-only feature builds without duplicating private context layouts. Test Plan: - cargo test --manifest-path rust/Cargo.toml - cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings - cargo +nightly fmt --manifest-path rust/Cargo.toml -- --check - make -B -C lib libzstd.a ZSTD_LIB_DEPRECATED=1 V=1 - C ZBUFF compress/decompress smoke test against the rebuilt static archive Depends-on: Rust compression and decompression streaming ABI ports
364 lines
9.8 KiB
Rust
364 lines
9.8 KiB
Rust
#![allow(non_camel_case_types)]
|
|
#![allow(non_snake_case)]
|
|
#![allow(clippy::missing_safety_doc)]
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
//! Deprecated buffered streaming compatibility wrappers.
|
|
//!
|
|
//! `ZBUFF_*` is an old spelling of the public `ZSTD_*Stream` API. The
|
|
//! wrappers deliberately keep the stream contexts opaque and forward to the
|
|
//! current implementation, so this module has no dependency on private
|
|
//! compressor or decompressor context layouts.
|
|
|
|
use std::ffi::{c_char, c_int, c_void};
|
|
|
|
#[repr(C)]
|
|
pub struct ZBUFF_CCtx {
|
|
_private: [u8; 0],
|
|
}
|
|
|
|
pub type ZBUFF_DCtx = ZBUFF_CCtx;
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy)]
|
|
pub struct ZBUFF_customMem {
|
|
customAlloc: Option<unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void>,
|
|
customFree: Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>,
|
|
opaque: *mut c_void,
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy)]
|
|
struct ZBUFF_compressionParameters {
|
|
windowLog: u32,
|
|
chainLog: u32,
|
|
hashLog: u32,
|
|
searchLog: u32,
|
|
minMatch: u32,
|
|
targetLength: u32,
|
|
strategy: c_int,
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy)]
|
|
struct ZBUFF_frameParameters {
|
|
contentSizeFlag: c_int,
|
|
checksumFlag: c_int,
|
|
noDictIDFlag: c_int,
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy)]
|
|
pub struct ZBUFF_parameters {
|
|
cParams: ZBUFF_compressionParameters,
|
|
fParams: ZBUFF_frameParameters,
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy)]
|
|
struct ZBUFF_inBuffer {
|
|
src: *const c_void,
|
|
size: usize,
|
|
pos: usize,
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy)]
|
|
struct ZBUFF_outBuffer {
|
|
dst: *mut c_void,
|
|
size: usize,
|
|
pos: usize,
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
extern "C" {
|
|
fn ZSTD_createCStream() -> *mut ZBUFF_CCtx;
|
|
fn ZSTD_createCStream_advanced(customMem: ZBUFF_customMem) -> *mut ZBUFF_CCtx;
|
|
fn ZSTD_freeCStream(zcs: *mut ZBUFF_CCtx) -> usize;
|
|
fn ZSTD_CCtx_reset(zcs: *mut ZBUFF_CCtx, reset: c_int) -> usize;
|
|
fn ZSTD_CCtx_setPledgedSrcSize(zcs: *mut ZBUFF_CCtx, pledgedSrcSize: u64) -> usize;
|
|
fn ZSTD_checkCParams(params: ZBUFF_compressionParameters) -> usize;
|
|
fn ZSTD_CCtx_setCParams(zcs: *mut ZBUFF_CCtx, params: ZBUFF_compressionParameters) -> usize;
|
|
fn ZSTD_CCtx_setFParams(zcs: *mut ZBUFF_CCtx, params: ZBUFF_frameParameters) -> usize;
|
|
fn ZSTD_CCtx_setParameter(zcs: *mut ZBUFF_CCtx, param: c_int, value: c_int) -> usize;
|
|
fn ZSTD_CCtx_loadDictionary(
|
|
zcs: *mut ZBUFF_CCtx,
|
|
dict: *const c_void,
|
|
dictSize: usize,
|
|
) -> usize;
|
|
fn ZSTD_initCStream(zcs: *mut ZBUFF_CCtx, compressionLevel: c_int) -> usize;
|
|
fn ZSTD_compressStream(
|
|
zcs: *mut ZBUFF_CCtx,
|
|
output: *mut ZBUFF_outBuffer,
|
|
input: *mut ZBUFF_inBuffer,
|
|
) -> usize;
|
|
fn ZSTD_flushStream(zcs: *mut ZBUFF_CCtx, output: *mut ZBUFF_outBuffer) -> usize;
|
|
fn ZSTD_endStream(zcs: *mut ZBUFF_CCtx, output: *mut ZBUFF_outBuffer) -> usize;
|
|
fn ZSTD_CStreamInSize() -> usize;
|
|
fn ZSTD_CStreamOutSize() -> usize;
|
|
}
|
|
|
|
#[cfg(feature = "decompression")]
|
|
extern "C" {
|
|
fn ZSTD_createDStream() -> *mut ZBUFF_DCtx;
|
|
fn ZSTD_createDStream_advanced(customMem: ZBUFF_customMem) -> *mut ZBUFF_DCtx;
|
|
fn ZSTD_freeDStream(zds: *mut ZBUFF_DCtx) -> usize;
|
|
fn ZSTD_initDStream(zds: *mut ZBUFF_DCtx) -> usize;
|
|
fn ZSTD_initDStream_usingDict(
|
|
zds: *mut ZBUFF_DCtx,
|
|
dict: *const c_void,
|
|
dictSize: usize,
|
|
) -> usize;
|
|
fn ZSTD_decompressStream(
|
|
zds: *mut ZBUFF_DCtx,
|
|
output: *mut ZBUFF_outBuffer,
|
|
input: *mut ZBUFF_inBuffer,
|
|
) -> usize;
|
|
fn ZSTD_DStreamInSize() -> usize;
|
|
fn ZSTD_DStreamOutSize() -> usize;
|
|
}
|
|
|
|
extern "C" {
|
|
fn ZSTD_isError(code: usize) -> u32;
|
|
fn ZSTD_getErrorName(code: usize) -> *const c_char;
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_createCCtx() -> *mut ZBUFF_CCtx {
|
|
unsafe { ZSTD_createCStream() }
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_createCCtx_advanced(customMem: ZBUFF_customMem) -> *mut ZBUFF_CCtx {
|
|
unsafe { ZSTD_createCStream_advanced(customMem) }
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_freeCCtx(zbc: *mut ZBUFF_CCtx) -> usize {
|
|
unsafe { ZSTD_freeCStream(zbc) }
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_compressInit_advanced(
|
|
zbc: *mut ZBUFF_CCtx,
|
|
dict: *const c_void,
|
|
dictSize: usize,
|
|
params: ZBUFF_parameters,
|
|
pledgedSrcSize: u64,
|
|
) -> usize {
|
|
let pledgedSrcSize = if pledgedSrcSize == 0 {
|
|
u64::MAX
|
|
} else {
|
|
pledgedSrcSize
|
|
};
|
|
let result = unsafe { ZSTD_CCtx_reset(zbc, 1) };
|
|
if unsafe { ZSTD_isError(result) } != 0 {
|
|
return result;
|
|
}
|
|
let result = unsafe { ZSTD_CCtx_setPledgedSrcSize(zbc, pledgedSrcSize) };
|
|
if unsafe { ZSTD_isError(result) } != 0 {
|
|
return result;
|
|
}
|
|
let result = unsafe { ZSTD_checkCParams(params.cParams) };
|
|
if unsafe { ZSTD_isError(result) } != 0 {
|
|
return result;
|
|
}
|
|
let result = unsafe { ZSTD_CCtx_setFParams(zbc, params.fParams) };
|
|
if unsafe { ZSTD_isError(result) } != 0 {
|
|
return result;
|
|
}
|
|
let result = unsafe { ZSTD_CCtx_setCParams(zbc, params.cParams) };
|
|
if unsafe { ZSTD_isError(result) } != 0 {
|
|
return result;
|
|
}
|
|
unsafe { ZSTD_CCtx_loadDictionary(zbc, dict, dictSize) }
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_compressInitDictionary(
|
|
zbc: *mut ZBUFF_CCtx,
|
|
dict: *const c_void,
|
|
dictSize: usize,
|
|
compressionLevel: c_int,
|
|
) -> usize {
|
|
let result = unsafe { ZSTD_CCtx_reset(zbc, 1) };
|
|
if unsafe { ZSTD_isError(result) } != 0 {
|
|
return result;
|
|
}
|
|
let result = unsafe { ZSTD_CCtx_setParameter(zbc, 100, compressionLevel) };
|
|
if unsafe { ZSTD_isError(result) } != 0 {
|
|
return result;
|
|
}
|
|
unsafe { ZSTD_CCtx_loadDictionary(zbc, dict, dictSize) }
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_compressInit(
|
|
zbc: *mut ZBUFF_CCtx,
|
|
compressionLevel: c_int,
|
|
) -> usize {
|
|
unsafe { ZSTD_initCStream(zbc, compressionLevel) }
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_compressContinue(
|
|
zbc: *mut ZBUFF_CCtx,
|
|
dst: *mut c_void,
|
|
dstCapacityPtr: *mut usize,
|
|
src: *const c_void,
|
|
srcSizePtr: *mut usize,
|
|
) -> usize {
|
|
let mut output = ZBUFF_outBuffer {
|
|
dst,
|
|
pos: 0,
|
|
size: unsafe { *dstCapacityPtr },
|
|
};
|
|
let mut input = ZBUFF_inBuffer {
|
|
src,
|
|
pos: 0,
|
|
size: unsafe { *srcSizePtr },
|
|
};
|
|
let result = unsafe { ZSTD_compressStream(zbc, &mut output, &mut input) };
|
|
unsafe {
|
|
*dstCapacityPtr = output.pos;
|
|
*srcSizePtr = input.pos;
|
|
}
|
|
result
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_compressFlush(
|
|
zbc: *mut ZBUFF_CCtx,
|
|
dst: *mut c_void,
|
|
dstCapacityPtr: *mut usize,
|
|
) -> usize {
|
|
let mut output = ZBUFF_outBuffer {
|
|
dst,
|
|
pos: 0,
|
|
size: unsafe { *dstCapacityPtr },
|
|
};
|
|
let result = unsafe { ZSTD_flushStream(zbc, &mut output) };
|
|
unsafe { *dstCapacityPtr = output.pos };
|
|
result
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_compressEnd(
|
|
zbc: *mut ZBUFF_CCtx,
|
|
dst: *mut c_void,
|
|
dstCapacityPtr: *mut usize,
|
|
) -> usize {
|
|
let mut output = ZBUFF_outBuffer {
|
|
dst,
|
|
pos: 0,
|
|
size: unsafe { *dstCapacityPtr },
|
|
};
|
|
let result = unsafe { ZSTD_endStream(zbc, &mut output) };
|
|
unsafe { *dstCapacityPtr = output.pos };
|
|
result
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub extern "C" fn ZBUFF_recommendedCInSize() -> usize {
|
|
unsafe { ZSTD_CStreamInSize() }
|
|
}
|
|
|
|
#[cfg(feature = "compression")]
|
|
#[no_mangle]
|
|
pub extern "C" fn ZBUFF_recommendedCOutSize() -> usize {
|
|
unsafe { ZSTD_CStreamOutSize() }
|
|
}
|
|
|
|
#[cfg(feature = "decompression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_createDCtx() -> *mut ZBUFF_DCtx {
|
|
unsafe { ZSTD_createDStream() }
|
|
}
|
|
|
|
#[cfg(feature = "decompression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_createDCtx_advanced(customMem: ZBUFF_customMem) -> *mut ZBUFF_DCtx {
|
|
unsafe { ZSTD_createDStream_advanced(customMem) }
|
|
}
|
|
|
|
#[cfg(feature = "decompression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_freeDCtx(zbd: *mut ZBUFF_DCtx) -> usize {
|
|
unsafe { ZSTD_freeDStream(zbd) }
|
|
}
|
|
|
|
#[cfg(feature = "decompression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_decompressInitDictionary(
|
|
zbd: *mut ZBUFF_DCtx,
|
|
dict: *const c_void,
|
|
dictSize: usize,
|
|
) -> usize {
|
|
unsafe { ZSTD_initDStream_usingDict(zbd, dict, dictSize) }
|
|
}
|
|
|
|
#[cfg(feature = "decompression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_decompressInit(zbd: *mut ZBUFF_DCtx) -> usize {
|
|
unsafe { ZSTD_initDStream(zbd) }
|
|
}
|
|
|
|
#[cfg(feature = "decompression")]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_decompressContinue(
|
|
zbd: *mut ZBUFF_DCtx,
|
|
dst: *mut c_void,
|
|
dstCapacityPtr: *mut usize,
|
|
src: *const c_void,
|
|
srcSizePtr: *mut usize,
|
|
) -> usize {
|
|
let mut output = ZBUFF_outBuffer {
|
|
dst,
|
|
pos: 0,
|
|
size: unsafe { *dstCapacityPtr },
|
|
};
|
|
let mut input = ZBUFF_inBuffer {
|
|
src,
|
|
pos: 0,
|
|
size: unsafe { *srcSizePtr },
|
|
};
|
|
let result = unsafe { ZSTD_decompressStream(zbd, &mut output, &mut input) };
|
|
unsafe {
|
|
*dstCapacityPtr = output.pos;
|
|
*srcSizePtr = input.pos;
|
|
}
|
|
result
|
|
}
|
|
|
|
#[cfg(feature = "decompression")]
|
|
#[no_mangle]
|
|
pub extern "C" fn ZBUFF_recommendedDInSize() -> usize {
|
|
unsafe { ZSTD_DStreamInSize() }
|
|
}
|
|
|
|
#[cfg(feature = "decompression")]
|
|
#[no_mangle]
|
|
pub extern "C" fn ZBUFF_recommendedDOutSize() -> usize {
|
|
unsafe { ZSTD_DStreamOutSize() }
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn ZBUFF_isError(code: usize) -> u32 {
|
|
unsafe { ZSTD_isError(code) }
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn ZBUFF_getErrorName(code: usize) -> *const c_char {
|
|
unsafe { ZSTD_getErrorName(code) }
|
|
}
|