Skip to content

use zlib-rs as a dynamic c library #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading