compu/decoder/
zlib.rs

1//! `zlib` wrapper
2
3extern crate alloc;
4
5use libz_sys as sys;
6
7use alloc::boxed::Box;
8use core::ffi::c_int;
9use core::{mem, ptr};
10
11use super::zlib_common::ZlibMode;
12use super::{Decode, Decoder, Interface};
13use crate::mem::{compu_alloc, compu_free_with_state};
14
15const DEFAULT_INFLATE: i32 = 0;
16
17extern "C" {
18    pub fn zError(code: c_int) -> *const i8;
19}
20
21///`zlib` interface
22static ZLIB: Interface = Interface {
23    drop_fn,
24    reset_fn,
25    decode_fn,
26    describe_error_fn,
27};
28
29#[repr(transparent)]
30struct State {
31    inner: sys::z_stream,
32}
33
34impl State {
35    #[inline(always)]
36    fn as_mut(&mut self) -> &mut sys::z_stream {
37        &mut self.inner
38    }
39
40    #[inline(always)]
41    fn reset(&mut self) -> bool {
42        unsafe { sys::inflateReset(&mut self.inner) == sys::Z_OK }
43    }
44}
45
46impl Drop for State {
47    #[inline(always)]
48    fn drop(&mut self) {
49        unsafe {
50            sys::inflateEnd(&mut self.inner);
51        }
52    }
53}
54
55impl Interface {
56    ///Creates decoder with `zlib` interface
57    ///
58    ///Returns `None` if unable to initialize it (likely due to lack of memory)
59    pub fn zlib(mode: ZlibMode) -> Option<Decoder> {
60        let mut instance = Box::new(State {
61            inner: sys::z_stream {
62                next_in: ptr::null_mut(),
63                avail_in: 0,
64                total_in: 0,
65                next_out: ptr::null_mut(),
66                avail_out: 0,
67                total_out: 0,
68                msg: ptr::null_mut(),
69                state: ptr::null_mut(),
70                zalloc: compu_alloc,
71                zfree: compu_free_with_state,
72                opaque: ptr::null_mut(),
73                data_type: 0,
74                adler: 0,
75                reserved: 0,
76            },
77        });
78        let result = unsafe {
79            sys::inflateInit2_(
80                &mut instance.inner,
81                mode.max_bits(),
82                sys::zlibVersion(),
83                mem::size_of::<sys::z_stream>() as _,
84            )
85        };
86
87        if result == 0 {
88            let instance = ptr::NonNull::from(Box::leak(instance)).cast();
89            Some(ZLIB.inner_decoder(instance))
90        } else {
91            None
92        }
93    }
94}
95
96#[inline]
97unsafe fn decode_fn(state: ptr::NonNull<u8>, input: *const u8, input_remain: usize, output: *mut u8, output_remain: usize) -> Decode {
98    internal_zlib_impl_decode!(state, input, input_remain, output, output_remain)
99}
100
101#[inline]
102fn reset_fn(state: ptr::NonNull<u8>) -> Option<ptr::NonNull<u8>> {
103    let result = unsafe {
104        (*(state.as_ptr() as *mut State)).reset()
105    };
106    if result {
107        Some(state)
108    } else {
109        None
110    }
111}
112
113#[inline]
114fn drop_fn(data: ptr::NonNull<u8>) {
115    unsafe {
116        drop(Box::from_raw(data.as_ptr() as *mut State));
117    }
118}
119
120#[inline]
121fn describe_error_fn(code: i32) -> Option<&'static str> {
122    let result = unsafe {
123        zError(code)
124    };
125    crate::utils::convert_c_str(result)
126}