Crate libz_rs_sys
source ·Expand description
This crate is a C API for zlib-rs. The API is broadly equivalent to zlib-sys
and zlib-ng-sys
, but does not currently provide the gz*
family of functions.
From a rust perspective, this API is not very ergonomic. Use the flate2
crate for a more
ergonomic rust interface to zlib.
§Example
This example compresses (“deflates”) the string "Hello, World!"
and then decompresses
(“inflates”) it again.
let mut strm = libz_rs_sys::z_stream::default();
let version = libz_rs_sys::zlibVersion();
let stream_size = core::mem::size_of_val(&strm) as i32;
let level = 6; // the default compression level
let err = unsafe { libz_rs_sys::deflateInit_(&mut strm, level, version, stream_size) };
assert_eq!(err, libz_rs_sys::Z_OK);
let input = "Hello, World!";
strm.avail_in = input.len() as _;
strm.next_in = input.as_ptr();
let mut output = [0u8; 32];
strm.avail_out = output.len() as _;
strm.next_out = output.as_mut_ptr();
let err = unsafe { libz_rs_sys::deflate(&mut strm, libz_rs_sys::Z_FINISH) };
assert_eq!(err, libz_rs_sys::Z_STREAM_END);
let err = unsafe { libz_rs_sys::deflateEnd(&mut strm) };
assert_eq!(err, libz_rs_sys::Z_OK);
let deflated = &mut output[..strm.total_out as usize];
let mut strm = libz_rs_sys::z_stream::default();
let err = unsafe { libz_rs_sys::inflateInit_(&mut strm, version, stream_size) };
assert_eq!(err, libz_rs_sys::Z_OK);
strm.avail_in = deflated.len() as _;
strm.next_in = deflated.as_ptr();
let mut output = [0u8; 32];
strm.avail_out = output.len() as _;
strm.next_out = output.as_mut_ptr();
let err = unsafe { libz_rs_sys::inflate(&mut strm, libz_rs_sys::Z_FINISH) };
assert_eq!(err, libz_rs_sys::Z_STREAM_END);
let err = unsafe { libz_rs_sys::inflateEnd(&mut strm) };
assert_eq!(err, libz_rs_sys::Z_OK);
let inflated = &output[..strm.total_out as usize];
assert_eq!(inflated, input.as_bytes())
§Safety of *mut z_stream
Most functions require an argument of type *mut z_stream
. Unless
otherwise noted, the safety requirements on such arguments are at least that the
pointer must be either:
- A
NULL
pointer - A pointer to a correctly aligned, initialized value of type
z_stream
.
In other words, it must be safe to cast the *mut z_stream
to a Option<&mut z_stream>
. It is
always safe to provide an argument of type &mut z_stream
: rust will automatically downcast
the argument to *mut z_stream
.
Structs§
- gzip header information passed to and from zlib routines. See RFC 1952 for more details on the meanings of these fields.
Enums§
Constants§
Functions§
- Inflates
source
intodest
, and writes the final inflated size intodestLen
.