From 502c24183938a29e1a9a361fd0701ea8d89303f9 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Sun, 11 Jun 2023 13:14:50 +0200 Subject: [PATCH] Update lib.rs and tests for zlib-ng 2.1 --- src/lib.rs | 155 +++++++++++++++++++++++++++++++++++------------ systest/build.rs | 10 ++- 2 files changed, 126 insertions(+), 39 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bca1b8e3..ae00583f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![allow(non_camel_case_types)] +#![allow(non_snake_case)] use std::os::raw::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_void}; @@ -56,9 +57,12 @@ pub type z_off_t = libc::off_t; ))] pub type z_off_t = c_long; -#[cfg(zng)] +#[cfg(all(zng, windows, not(target_env = "gnu")))] pub type z_off_t = i64; +#[cfg(all(zng, not(all(windows, not(target_env = "gnu")))))] +pub type z_off_t = libc::off_t; + #[repr(C)] #[derive(Copy, Clone)] pub struct gz_header { @@ -103,12 +107,16 @@ pub type z_streamp = *mut z_stream; // can't parse that. #[cfg(not(zng))] macro_rules! zng_prefix { - ($name:expr) => { stringify!($name) } + ($name:expr) => { + stringify!($name) + }; } #[cfg(zng)] macro_rules! zng_prefix { - ($name:expr) => { concat!("zng_", stringify!($name)) } + ($name:expr) => { + concat!("zng_", stringify!($name)) + }; } extern "C" { @@ -124,24 +132,6 @@ extern "C" { pub fn deflateCopy(dest: z_streamp, source: z_streamp) -> c_int; #[link_name = zng_prefix!(deflateEnd)] pub fn deflateEnd(strm: z_streamp) -> c_int; - #[link_name = zng_prefix!(deflateInit_)] - pub fn deflateInit_( - strm: z_streamp, - level: c_int, - version: *const c_char, - stream_size: c_int, - ) -> c_int; - #[link_name = zng_prefix!(deflateInit2_)] - pub fn deflateInit2_( - strm: z_streamp, - level: c_int, - method: c_int, - windowBits: c_int, - memLevel: c_int, - strategy: c_int, - version: *const c_char, - stream_size: c_int, - ) -> c_int; #[link_name = zng_prefix!(deflateParams)] pub fn deflateParams(strm: z_streamp, level: c_int, strategy: c_int) -> c_int; #[link_name = zng_prefix!(deflatePrime)] @@ -176,29 +166,12 @@ extern "C" { ) -> c_int; #[link_name = zng_prefix!(inflateBackEnd)] pub fn inflateBackEnd(strm: z_streamp) -> c_int; - #[link_name = zng_prefix!(inflateBackInit_)] - pub fn inflateBackInit_( - strm: z_streamp, - windowBits: c_int, - window: *mut c_uchar, - version: *const c_char, - stream_size: c_int, - ) -> c_int; #[link_name = zng_prefix!(inflateCopy)] pub fn inflateCopy(dest: z_streamp, source: z_streamp) -> c_int; #[link_name = zng_prefix!(inflateEnd)] pub fn inflateEnd(strm: z_streamp) -> c_int; #[link_name = zng_prefix!(inflateGetHeader)] pub fn inflateGetHeader(strm: z_streamp, head: gz_headerp) -> c_int; - #[link_name = zng_prefix!(inflateInit_)] - pub fn inflateInit_(strm: z_streamp, version: *const c_char, stream_size: c_int) -> c_int; - #[link_name = zng_prefix!(inflateInit2_)] - pub fn inflateInit2_( - strm: z_streamp, - windowBits: c_int, - version: *const c_char, - stream_size: c_int, - ) -> c_int; #[link_name = zng_prefix!(inflateMark)] pub fn inflateMark(strm: z_streamp) -> c_long; #[link_name = zng_prefix!(inflatePrime)] @@ -245,6 +218,112 @@ extern "C" { pub fn zlibVersion() -> *const c_char; } +#[cfg(not(zng))] +extern "C" { + pub fn deflateInit_( + strm: z_streamp, + level: c_int, + version: *const c_char, + stream_size: c_int, + ) -> c_int; + pub fn deflateInit2_( + strm: z_streamp, + level: c_int, + method: c_int, + windowBits: c_int, + memLevel: c_int, + strategy: c_int, + version: *const c_char, + stream_size: c_int, + ) -> c_int; + pub fn inflateBackInit_( + strm: z_streamp, + windowBits: c_int, + window: *mut c_uchar, + version: *const c_char, + stream_size: c_int, + ) -> c_int; + pub fn inflateInit_(strm: z_streamp, version: *const c_char, stream_size: c_int) -> c_int; + pub fn inflateInit2_( + strm: z_streamp, + windowBits: c_int, + version: *const c_char, + stream_size: c_int, + ) -> c_int; +} + +#[cfg(zng)] +extern "C" { + pub fn zng_deflateInit(strm: z_streamp, level: c_int) -> c_int; + pub fn zng_deflateInit2( + strm: z_streamp, + level: c_int, + method: c_int, + windowBits: c_int, + memLevel: c_int, + strategy: c_int, + ) -> c_int; + pub fn zng_inflateBackInit(strm: z_streamp, windowBits: c_int, window: *mut c_uchar) -> c_int; + pub fn zng_inflateInit(strm: z_streamp) -> c_int; + pub fn zng_inflateInit2(strm: z_streamp, windowBits: c_int) -> c_int; +} + +// These methods are required to keep BC with original zlib API since zlib-ng 2.1 that changed API +#[cfg(zng)] +#[inline(always)] +pub unsafe fn inflateInit2_( + strm: z_streamp, + windowBits: c_int, + _version: *const c_char, + _stream_size: c_int, +) -> c_int { + zng_inflateInit2(strm, windowBits) +} + +#[cfg(zng)] +#[inline(always)] +pub unsafe fn inflateInit_(strm: z_streamp, _version: *const c_char, _stream_size: c_int) -> c_int { + zng_inflateInit(strm) +} + +#[cfg(zng)] +#[inline(always)] +pub unsafe fn inflateBackInit_( + strm: z_streamp, + windowBits: c_int, + window: *mut c_uchar, + _version: *const c_char, + _stream_size: c_int, +) -> c_int { + zng_inflateBackInit(strm, windowBits, window) +} + +#[cfg(zng)] +#[inline(always)] +pub unsafe fn deflateInit2_( + strm: z_streamp, + level: c_int, + method: c_int, + windowBits: c_int, + memLevel: c_int, + strategy: c_int, + _version: *const c_char, + _stream_size: c_int, +) -> c_int { + zng_deflateInit2(strm, level, method, windowBits, memLevel, strategy) +} + +#[cfg(zng)] +#[inline] +pub unsafe fn deflateInit_( + strm: z_streamp, + level: c_int, + _version: *const c_char, + _stream_size: c_int, +) -> c_int { + zng_deflateInit(strm, level) +} + #[cfg(any(zng, feature = "libc"))] extern "C" { #[link_name = zng_prefix!(adler32_combine)] diff --git a/systest/build.rs b/systest/build.rs index e213e8a3..09b23a65 100644 --- a/systest/build.rs +++ b/systest/build.rs @@ -3,6 +3,7 @@ use std::env; fn main() { let zng = env::var("CARGO_PKG_NAME").unwrap() == "systest-zng"; let mut cfg = ctest2::TestGenerator::new(); + cfg.define("WITH_GZFILEOP", Some("ON")); let (header, dep_include) = if zng { ("zlib-ng.h", "DEP_Z_NG_INCLUDE") } else { @@ -20,8 +21,13 @@ fn main() { if rust == "zlibVersion" { return "zlibng_version".to_string(); } - format!("zng_{}", rust) + if rust.starts_with("zng_") { + rust.to_string() + } else { + format!("zng_{}", rust) + } }); + cfg.cfg("zng", None); } cfg.type_name(move |n, _, _| { if zng { @@ -35,6 +41,8 @@ fn main() { return "size_t".to_string(); } else if n == "z_checksum" { return "uint32_t".to_string(); + } else if n == "z_off_t" { + return "z_off64_t".to_string(); } } else { if n == "z_size" {