diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 61d345bb2..420137f2e 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -79,6 +79,8 @@ int ZSTD_rust_targetCBlockSizeAction(int bss, int isFirstBlock, int maybeRLE, int isRLE, size_t cSize, size_t srcSize, int strategy); +size_t ZSTD_rust_CStreamInSize(void); +size_t ZSTD_rust_CStreamOutSize(void); /* Context-free compression-parameter selection and sizing leaves live in * Rust (rust/src/zstd_compress_params.rs). This file retains @@ -4384,11 +4386,11 @@ size_t ZSTD_freeCStream(ZSTD_CStream* zcs) /*====== Initialization ======*/ -size_t ZSTD_CStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX; } +size_t ZSTD_CStreamInSize(void) { return ZSTD_rust_CStreamInSize(); } size_t ZSTD_CStreamOutSize(void) { - return ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */ ; + return ZSTD_rust_CStreamOutSize(); } static ZSTD_CParamMode_e ZSTD_getCParamMode(ZSTD_CDict const* cdict, ZSTD_CCtx_params const* params, U64 pledgedSrcSize) diff --git a/rust/src/zstd_compress_api.rs b/rust/src/zstd_compress_api.rs index d5958028c..1af1b4c97 100644 --- a/rust/src/zstd_compress_api.rs +++ b/rust/src/zstd_compress_api.rs @@ -14,6 +14,8 @@ const ZSTD_MAX_INPUT_SIZE: usize = 0xFF00_FF00; const SMALL_INPUT_THRESHOLD: usize = 128 << 10; const ZSTD_MINMATCH_MIN: usize = 3; const ZSTD_BLOCKSIZE_MAX_MIN: usize = 1 << 10; +const ZSTD_BLOCKSIZE_MAX: usize = 1 << 17; +const ZSTD_BLOCK_HEADER_SIZE: usize = 3; /// ABI-compatible `ZSTD_Sequence` from `zstd.h`. #[repr(C)] @@ -41,6 +43,18 @@ pub extern "C" fn ZSTD_compressBound(src_size: usize) -> usize { src_size.wrapping_add(src_size >> 8).wrapping_add(margin) } +/// Returns the recommended input buffer size for the public streaming API. +#[no_mangle] +pub extern "C" fn ZSTD_rust_CStreamInSize() -> usize { + ZSTD_BLOCKSIZE_MAX +} + +/// Returns the recommended output buffer size for the public streaming API. +#[no_mangle] +pub extern "C" fn ZSTD_rust_CStreamOutSize() -> usize { + ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_BLOCK_HEADER_SIZE + 4 +} + /// Returns the maximum number of public sequences generated for an input. #[no_mangle] pub extern "C" fn ZSTD_sequenceBound(src_size: usize) -> usize { @@ -89,6 +103,16 @@ mod tests { assert!(ERR_isError(ZSTD_compressBound(ZSTD_MAX_INPUT_SIZE))); } + #[test] + fn cstream_sizes_match_the_public_formulas() { + assert_eq!(ZSTD_rust_CStreamInSize(), 128 * 1024); + assert_eq!( + ZSTD_rust_CStreamOutSize(), + ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_BLOCK_HEADER_SIZE + 4 + ); + assert_eq!(ZSTD_rust_CStreamOutSize(), 131_591); + } + #[test] fn sequence_bound_matches_the_public_formula() { assert_eq!(ZSTD_sequenceBound(0), 2);