Skip to content

Commit 692b0fd

Browse files
committed
use zlib-rs as a dynamic c library
1 parent d6308b1 commit 692b0fd

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

.github/workflows/checks.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,37 @@ jobs:
192192
for target in $(cargo fuzz list ${{ matrix.features }}) ; do
193193
RUST_BACKTRACE=1 cargo fuzz run ${{ matrix.features }} $target -- -max_total_time=10
194194
done
195+
196+
link-c-dynamic-library:
197+
name: dynamic library
198+
strategy:
199+
matrix:
200+
include:
201+
- target: x86_64-unknown-linux-gnu
202+
features:
203+
- ''
204+
runs-on: ubuntu-latest
205+
steps:
206+
- name: Checkout sources
207+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
208+
with:
209+
persist-credentials: false
210+
- name: Install rust toolchain
211+
uses: dtolnay/rust-toolchain@be73d7920c329f220ce78e0234b8f96b7ae60248
212+
with:
213+
toolchain: stable
214+
targets: ${{matrix.target}}
215+
- name: Rust cache
216+
uses: Swatinem/rust-cache@3cf7f8cc28d1b4e7d01e3783be10a97d55d483c8
217+
with:
218+
shared-key: "stable-${{matrix.target}}"
219+
- name: get zpipe.c
220+
run: wget https://www.zlib.net/zpipe.c
221+
- name: cargo build
222+
run: cargo build --target ${{matrix.target}} -p libz-rs-sys --release
223+
- name: cc
224+
run: cc -o zpipe zpipe.c target/${{matrix.target}}/release/deps/liblibz_rs_sys.so
225+
- name: execute
226+
run: cat Cargo.toml | ./zpipe | ./zpipe -d > out.txt
227+
- name: compare
228+
run: cmp -s Cargo.toml out.txt

libz-rs-sys/src/lib.rs

+7-19
Original file line numberDiff line numberDiff line change
@@ -650,36 +650,24 @@ pub unsafe extern "C" fn deflateTune(
650650
}
651651
}
652652

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

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

660-
let cstr = core::ffi::CStr::from_ptr(version);
661-
662-
if LIBZ_RS_SYS_VERSION.as_bytes()[0] != cstr.to_bytes()[0] {
663+
let expected_major_version = core::ptr::read(version);
664+
if expected_major_version as u8 != LIBZ_RS_SYS_VERSION.as_bytes()[0] {
663665
return false;
664666
}
665667

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

669671
pub const extern "C" fn zlibVersion() -> *const c_char {
670-
const BUF: [u8; 16] = {
671-
let mut buf = [0; 16];
672-
673-
let mut i = 0;
674-
while i < LIBZ_RS_SYS_VERSION.len() {
675-
buf[i] = LIBZ_RS_SYS_VERSION.as_bytes()[i];
676-
i += 1;
677-
}
678-
679-
assert!(matches!(buf.last(), Some(0)));
680-
681-
buf
682-
};
683-
684-
BUF.as_ptr() as *const c_char
672+
LIBZ_RS_SYS_VERSION.as_ptr() as *const c_char
685673
}

0 commit comments

Comments
 (0)