libz_rs_sys

Function compress

Source
#[export_name = "compress"]
pub unsafe extern "C-unwind" fn compress(
    dest: *mut Bytef,
    destLen: *mut c_ulong,
    source: *const Bytef,
    sourceLen: c_ulong,
) -> c_int
Expand description

Compresses source into dest, and writes the final deflated size into destLen.

sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be at least the value returned by compressBound(sourceLen). Upon exit, destLen is the actual size of the compressed data.

A call to compress is equivalent to compress2 with a level parameter of Z_DEFAULT_COMPRESSION.

§Returns

§Safety

The caller must guarantee that

§Example

use libz_rs_sys::{Z_OK, compress};

let source = b"Ferris";

let mut dest = vec![0u8; 100];
let mut dest_len = dest.len() as _;

let err = unsafe {
    compress(
        dest.as_mut_ptr(),
        &mut dest_len,
        source.as_ptr(),
        source.len() as _,
    )
};

assert_eq!(err, Z_OK);
assert_eq!(dest_len, 14);

dest.truncate(dest_len as usize);
assert_eq!(dest, [120, 156, 115, 75, 45, 42, 202, 44, 6, 0, 8, 6, 2, 108]);