2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-27 12:03:07 -04:00
|
|
|
|
|
|
|
use super::mode::Flush;
|
|
|
|
use super::mode::Mode;
|
|
|
|
use std::ffi::c_int;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
|
|
|
pub struct StreamWrapper {
|
2023-11-11 09:20:12 -05:00
|
|
|
pub strm: zlib::z_stream,
|
2023-03-27 12:03:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for StreamWrapper {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2023-11-11 09:20:12 -05:00
|
|
|
strm: zlib::z_stream {
|
2023-03-27 12:03:07 -04:00
|
|
|
next_in: std::ptr::null_mut(),
|
|
|
|
avail_in: 0,
|
|
|
|
total_in: 0,
|
|
|
|
next_out: std::ptr::null_mut(),
|
|
|
|
avail_out: 0,
|
|
|
|
total_out: 0,
|
|
|
|
msg: std::ptr::null_mut(),
|
|
|
|
state: std::ptr::null_mut(),
|
|
|
|
zalloc: super::alloc::zalloc,
|
|
|
|
zfree: super::alloc::zfree,
|
2023-11-11 09:20:12 -05:00
|
|
|
opaque: 0 as zlib::voidpf,
|
2023-03-27 12:03:07 -04:00
|
|
|
data_type: 0,
|
|
|
|
adler: 0,
|
|
|
|
reserved: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for StreamWrapper {
|
2023-11-11 09:20:12 -05:00
|
|
|
type Target = zlib::z_stream;
|
2023-03-27 12:03:07 -04:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.strm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for StreamWrapper {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.strm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StreamWrapper {
|
|
|
|
pub fn reset(&mut self, mode: Mode) -> c_int {
|
2023-11-11 09:20:12 -05:00
|
|
|
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
|
2023-03-27 12:03:07 -04:00
|
|
|
unsafe {
|
|
|
|
match mode {
|
|
|
|
Mode::Deflate | Mode::Gzip | Mode::DeflateRaw => {
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::deflateReset(&mut self.strm)
|
2023-03-27 12:03:07 -04:00
|
|
|
}
|
|
|
|
Mode::Inflate | Mode::Gunzip | Mode::InflateRaw | Mode::Unzip => {
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::inflateReset(&mut self.strm)
|
2023-03-27 12:03:07 -04:00
|
|
|
}
|
|
|
|
Mode::None => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn end(&mut self, mode: Mode) {
|
2023-11-11 09:20:12 -05:00
|
|
|
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
|
2023-03-27 12:03:07 -04:00
|
|
|
unsafe {
|
|
|
|
match mode {
|
|
|
|
Mode::Deflate | Mode::Gzip | Mode::DeflateRaw => {
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::deflateEnd(&mut self.strm);
|
2023-03-27 12:03:07 -04:00
|
|
|
}
|
|
|
|
Mode::Inflate | Mode::Gunzip | Mode::InflateRaw | Mode::Unzip => {
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::inflateEnd(&mut self.strm);
|
2023-03-27 12:03:07 -04:00
|
|
|
}
|
|
|
|
Mode::None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deflate_init(
|
|
|
|
&mut self,
|
|
|
|
level: c_int,
|
|
|
|
window_bits: c_int,
|
|
|
|
mem_level: c_int,
|
|
|
|
strategy: c_int,
|
|
|
|
) -> c_int {
|
2023-11-11 09:20:12 -05:00
|
|
|
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
|
2023-03-27 12:03:07 -04:00
|
|
|
unsafe {
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::deflateInit2_(
|
2023-03-27 12:03:07 -04:00
|
|
|
&mut self.strm,
|
|
|
|
level,
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::Z_DEFLATED,
|
2023-03-27 12:03:07 -04:00
|
|
|
window_bits,
|
|
|
|
mem_level,
|
|
|
|
strategy,
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::zlibVersion(),
|
|
|
|
std::mem::size_of::<zlib::z_stream>() as i32,
|
2023-03-27 12:03:07 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inflate_init(&mut self, window_bits: c_int) -> c_int {
|
2023-11-11 09:20:12 -05:00
|
|
|
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
|
2023-03-27 12:03:07 -04:00
|
|
|
unsafe {
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::inflateInit2_(
|
2023-03-27 12:03:07 -04:00
|
|
|
&mut self.strm,
|
|
|
|
window_bits,
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::zlibVersion(),
|
|
|
|
std::mem::size_of::<zlib::z_stream>() as i32,
|
2023-03-27 12:03:07 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deflate(&mut self, flush: Flush) -> c_int {
|
2023-11-11 09:20:12 -05:00
|
|
|
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
|
|
|
|
unsafe { zlib::deflate(&mut self.strm, flush as _) }
|
2023-03-27 12:03:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inflate(&mut self, flush: Flush) -> c_int {
|
2023-11-11 09:20:12 -05:00
|
|
|
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
|
|
|
|
unsafe { zlib::inflate(&mut self.strm, flush as _) }
|
2023-03-27 12:03:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inflate_set_dictionary(&mut self, dictionary: &[u8]) -> c_int {
|
2023-11-11 09:20:12 -05:00
|
|
|
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
|
2023-03-27 12:03:07 -04:00
|
|
|
unsafe {
|
2023-11-11 09:20:12 -05:00
|
|
|
zlib::inflateSetDictionary(
|
2023-03-27 12:03:07 -04:00
|
|
|
&mut self.strm,
|
|
|
|
dictionary.as_ptr() as *const _,
|
|
|
|
dictionary.len() as _,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|