libz_rs_sys

Function deflateInit2_

Source
#[export_name = "deflateInit2_"]
pub unsafe extern "C-unwind" fn deflateInit2_(
    strm: z_streamp,
    level: c_int,
    method: c_int,
    windowBits: c_int,
    memLevel: c_int,
    strategy: c_int,
    version: *const c_char,
    stream_size: c_int,
) -> c_int
Expand description

Initializes the state for compression

The stream’s zalloc, zfree and opaque fields must be initialized before by the caller. If zalloc and zfree are set to NULL, deflateInit_ updates them to use default allocation functions. The total_in, total_out, adler, and msg fields are initialized.

The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:

  • level 0 gives no compression at all (the input data is simply copied a block at a time)
  • level 1 gives best speed
  • level 9 gives best compression
  • Z_DEFAULT_COMPRESSION requests a default compromise between speed and compression (currently equivalent to level 6).

A call to inflateInit_ is equivalent to inflateInit2_ where

§Returns

  • Z_OK if success
  • Z_MEM_ERROR if there was not enough memory
  • Z_VERSION_ERROR if the zlib library version is incompatible with the version assumed by the caller
  • Z_STREAM_ERROR if a parameter is invalid, such as a null pointer to the structure

§Safety

The caller must guarantee that

  • Either
    • strm is NULL
    • strm satisfies the requirements of &mut *strm
  • Either
  • If strm is not NULL, the following fields contain valid values
    • zalloc
    • zfree
    • opaque

§Example

use core::mem::MaybeUninit;
use libz_rs_sys::{z_stream, deflateInit2_, zlibVersion, Z_OK};

// the zalloc and zfree fields are initialized as zero/NULL.
// `deflateInit_` will set a default allocation  and deallocation function.
let mut strm = MaybeUninit::zeroed();

let err = unsafe {
    deflateInit2_(
        strm.as_mut_ptr(),
        6,
        8,
        15,
        8,
        0,
        zlibVersion(),
        core::mem::size_of::<z_stream>() as _,
    )
};
assert_eq!(err, Z_OK);

// the stream is now fully initialized. Prefer `assume_init_mut` over
// `assume_init` so the stream does not get moved.
let strm = unsafe { strm.assume_init_mut() };