| 1 |
This crate is a C API for [zlib-rs](https://docs.rs/zlib-rs/latest/zlib_rs/). The API is broadly equivalent to [`zlib-sys`](https://docs.rs/libz-sys/latest/libz_sys/) and [`zlib-ng-sys`](https://docs.rs/libz-ng-sys/latest/libz_ng_sys/), but does not currently provide the `gz*` family of functions. |
| 2 |
|
| 3 |
From a rust perspective, this API is not very ergonomic. Use the [`flate2`](https://crates.io/crates/flate2) crate for a more |
| 4 |
ergonomic rust interface to zlib. |
| 5 |
|
| 6 |
## Features |
| 7 |
|
| 8 |
**`custom-prefix`** |
| 9 |
|
| 10 |
Add a custom prefix to all exported symbols. |
| 11 |
|
| 12 |
The value of the `LIBZ_RS_SYS_PREFIX` is used as a prefix for all exported symbols. For example: |
| 13 |
|
| 14 |
```ignore |
| 15 |
> LIBZ_RS_SYS_PREFIX="MY_CUSTOM_PREFIX" cargo build -p libz-rs-sys --features=custom-prefix |
| 16 |
Compiling libz-rs-sys v0.2.1 (/home/folkertdev/rust/zlib-rs/libz-rs-sys) |
| 17 |
Finished `dev` profile [optimized + debuginfo] target(s) in 0.21s |
| 18 |
> objdump -tT target/debug/liblibz_rs_sys.so | grep "uncompress" |
| 19 |
0000000000081028 l O .got 0000000000000000 _ZN7zlib_rs7inflate10uncompress17he7d985e55c58a189E$got |
| 20 |
000000000002c570 l F .text 00000000000001ef _ZN7zlib_rs7inflate10uncompress17he7d985e55c58a189E |
| 21 |
0000000000024330 g F .text 000000000000008e MY_CUSTOM_PREFIXuncompress |
| 22 |
0000000000024330 g DF .text 000000000000008e Base MY_CUSTOM_PREFIXuncompress |
| 23 |
``` |
| 24 |
|
| 25 |
**`c-allocator`, `rust-allocator`** |
| 26 |
|
| 27 |
Pick the default allocator implementation that is used if no `zalloc` and `zfree` are configured in the input `z_stream`. |
| 28 |
|
| 29 |
- `c-allocator`: use `malloc`/`free` for the implementation of `zalloc` and `zfree` |
| 30 |
- `rust-allocator`: the rust global allocator for the implementation of `zalloc` and `zfree` |
| 31 |
|
| 32 |
The `rust-allocator` is the default when this crate is used as a rust dependency, and slightly more efficient because alignment is handled by the allocator. When building a dynamic library, it may make sense to use `c-allocator` instead. |
| 33 |
|
| 34 |
**`std`** |
| 35 |
|
| 36 |
Assume that `std` is available. When this feature is turned off, this crate is compatible with `#![no_std]`. |
| 37 |
|
| 38 |
## Example |
| 39 |
|
| 40 |
This example compresses ("deflates") the string `"Hello, World!"` and then decompresses |
| 41 |
("inflates") it again. |
| 42 |
|
| 43 |
```rust |
| 44 |
let mut strm = libz_rs_sys::z_stream::default(); |
| 45 |
|
| 46 |
let version = libz_rs_sys::zlibVersion(); |
| 47 |
let stream_size = core::mem::size_of_val(&strm) as i32; |
| 48 |
|
| 49 |
let level = 6; // the default compression level |
| 50 |
let err = unsafe { libz_rs_sys::deflateInit_(&mut strm, level, version, stream_size) }; |
| 51 |
assert_eq!(err, libz_rs_sys::Z_OK); |
| 52 |
|
| 53 |
let input = "Hello, World!"; |
| 54 |
strm.avail_in = input.len() as _; |
| 55 |
strm.next_in = input.as_ptr(); |
| 56 |
|
| 57 |
let mut output = [0u8; 32]; |
| 58 |
strm.avail_out = output.len() as _; |
| 59 |
strm.next_out = output.as_mut_ptr(); |
| 60 |
|
| 61 |
let err = unsafe { libz_rs_sys::deflate(&mut strm, libz_rs_sys::Z_FINISH) }; |
| 62 |
assert_eq!(err, libz_rs_sys::Z_STREAM_END); |
| 63 |
|
| 64 |
let err = unsafe { libz_rs_sys::deflateEnd(&mut strm) }; |
| 65 |
assert_eq!(err, libz_rs_sys::Z_OK); |
| 66 |
|
| 67 |
let deflated = &mut output[..strm.total_out as usize]; |
| 68 |
|
| 69 |
let mut strm = libz_rs_sys::z_stream::default(); |
| 70 |
let err = unsafe { libz_rs_sys::inflateInit_(&mut strm, version, stream_size) }; |
| 71 |
assert_eq!(err, libz_rs_sys::Z_OK); |
| 72 |
|
| 73 |
strm.avail_in = deflated.len() as _; |
| 74 |
strm.next_in = deflated.as_ptr(); |
| 75 |
|
| 76 |
let mut output = [0u8; 32]; |
| 77 |
strm.avail_out = output.len() as _; |
| 78 |
strm.next_out = output.as_mut_ptr(); |
| 79 |
|
| 80 |
let err = unsafe { libz_rs_sys::inflate(&mut strm, libz_rs_sys::Z_FINISH) }; |
| 81 |
assert_eq!(err, libz_rs_sys::Z_STREAM_END); |
| 82 |
|
| 83 |
let err = unsafe { libz_rs_sys::inflateEnd(&mut strm) }; |
| 84 |
assert_eq!(err, libz_rs_sys::Z_OK); |
| 85 |
|
| 86 |
let inflated = &output[..strm.total_out as usize]; |
| 87 |
|
| 88 |
assert_eq!(inflated, input.as_bytes()) |
| 89 |
``` |
| 90 |
|
| 91 |
## Compression Levels |
| 92 |
|
| 93 |
The zlib library supports compression levels 0 up to and including 9. The level indicates a tradeoff between time spent on the compression versus the compression ratio, the factor by which the input is reduced in size: |
| 94 |
|
| 95 |
- level 0: no compression at all |
| 96 |
- level 1: fastest compression |
| 97 |
- level 6: default (a good tradeoff between speed and compression ratio) |
| 98 |
- level 9: best compression |
| 99 |
|
| 100 |
Beyond this intuition, the exact behavior of the compression levels is not specified. The implementation of `zlib-rs` follows the implementation of [`zlig-ng`](https://github.com/zlib-ng/zlib-ng), and deviates from the one in stock zlib. |
| 101 |
|
| 102 |
In particular, our compression level 1 is extremely fast, but also just does not compress that well. On the `silesia-small.tar` input file, we see these output sizes: |
| 103 |
|
| 104 |
| implementation | compression level | output size (mb) | |
| 105 |
| --- | --- | --- | |
| 106 |
| - | 0 | `15.74` | |
| 107 |
| stock | 1 | ` 7.05` | |
| 108 |
| rs | 1 | ` 8.52` | |
| 109 |
| rs | 2 | ` 6.90` | |
| 110 |
| rs | 4 | ` 6.55` | |
| 111 |
|
| 112 |
But, `zlib-rs` is much faster than stock zlib. In our benchmarks, it is only at level 4 that we spend roughly as much time as stock zlib on level 1: |
| 113 |
|
| 114 |
| implementation | compression level | wall time (ms) | |
| 115 |
| --- | --- | --- | |
| 116 |
| stock | 1 | 185 | |
| 117 |
| rs | 2 | 139 | |
| 118 |
| rs | 4 | 181 | |
| 119 |
|
| 120 |
In our example, the main options are: |
| 121 |
|
| 122 |
- level 1: worse compression, but much faster |
| 123 |
- level 2: equivalent compression, but significantly faster |
| 124 |
- level 4: better compression, at the same speed |
| 125 |
|
| 126 |
In summary, when you upgrade from stock zlib, we recommend that you benchmark on your data and target platform, and pick the right compression level for your use case. |