Skip to content

Commit

Permalink
use zlib-rs as a dynamic c library
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Jul 31, 2024
1 parent d6308b1 commit 692b0fd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,37 @@ jobs:
for target in $(cargo fuzz list ${{ matrix.features }}) ; do
RUST_BACKTRACE=1 cargo fuzz run ${{ matrix.features }} $target -- -max_total_time=10
done
link-c-dynamic-library:
name: dynamic library
strategy:
matrix:
include:
- target: x86_64-unknown-linux-gnu
features:
- ''
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
with:
persist-credentials: false
- name: Install rust toolchain
uses: dtolnay/rust-toolchain@be73d7920c329f220ce78e0234b8f96b7ae60248
with:
toolchain: stable
targets: ${{matrix.target}}
- name: Rust cache
uses: Swatinem/rust-cache@3cf7f8cc28d1b4e7d01e3783be10a97d55d483c8
with:
shared-key: "stable-${{matrix.target}}"
- name: get zpipe.c
run: wget https://www.zlib.net/zpipe.c
- name: cargo build
run: cargo build --target ${{matrix.target}} -p libz-rs-sys --release
- name: cc
run: cc -o zpipe zpipe.c target/${{matrix.target}}/release/deps/liblibz_rs_sys.so
- name: execute
run: cat Cargo.toml | ./zpipe | ./zpipe -d > out.txt
- name: compare
run: cmp -s Cargo.toml out.txt
26 changes: 7 additions & 19 deletions libz-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,36 +650,24 @@ pub unsafe extern "C" fn deflateTune(
}
}

const LIBZ_RS_SYS_VERSION: &str = env!("CARGO_PKG_VERSION");
// the first part of this version specifies the zlib that we're compatible with (in terms of
// supported functions). In practice in most cases only the major version is checked, unless
// specific functions that were added later are used.
const LIBZ_RS_SYS_VERSION: &str = concat!("1.3.0-zlib-rs-", env!("CARGO_PKG_VERSION"), "\0");

unsafe fn is_version_compatible(version: *const c_char, stream_size: i32) -> bool {
if version.is_null() {
return false;
}

let cstr = core::ffi::CStr::from_ptr(version);

if LIBZ_RS_SYS_VERSION.as_bytes()[0] != cstr.to_bytes()[0] {
let expected_major_version = core::ptr::read(version);
if expected_major_version as u8 != LIBZ_RS_SYS_VERSION.as_bytes()[0] {
return false;
}

core::mem::size_of::<z_stream>() as i32 == stream_size
}

pub const extern "C" fn zlibVersion() -> *const c_char {
const BUF: [u8; 16] = {
let mut buf = [0; 16];

let mut i = 0;
while i < LIBZ_RS_SYS_VERSION.len() {
buf[i] = LIBZ_RS_SYS_VERSION.as_bytes()[i];
i += 1;
}

assert!(matches!(buf.last(), Some(0)));

buf
};

BUF.as_ptr() as *const c_char
LIBZ_RS_SYS_VERSION.as_ptr() as *const c_char
}

0 comments on commit 692b0fd

Please sign in to comment.