1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-30 02:59:11 -05:00
denoland-deno/ext/node/ops/zlib/stream.rs

136 lines
3.2 KiB
Rust
Raw Normal View History

// 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 {
pub strm: zlib::z_stream,
2023-03-27 12:03:07 -04:00
}
impl Default for StreamWrapper {
fn default() -> Self {
Self {
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,
opaque: 0 as zlib::voidpf,
2023-03-27 12:03:07 -04:00
data_type: 0,
adler: 0,
reserved: 0,
},
}
}
}
impl Deref for StreamWrapper {
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 {
// 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 => {
zlib::deflateReset(&mut self.strm)
2023-03-27 12:03:07 -04:00
}
Mode::Inflate | Mode::Gunzip | Mode::InflateRaw | Mode::Unzip => {
zlib::inflateReset(&mut self.strm)
2023-03-27 12:03:07 -04:00
}
Mode::None => unreachable!(),
}
}
}
pub fn end(&mut self, mode: Mode) {
// 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 => {
zlib::deflateEnd(&mut self.strm);
2023-03-27 12:03:07 -04:00
}
Mode::Inflate | Mode::Gunzip | Mode::InflateRaw | Mode::Unzip => {
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 {
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
2023-03-27 12:03:07 -04:00
unsafe {
zlib::deflateInit2_(
2023-03-27 12:03:07 -04:00
&mut self.strm,
level,
zlib::Z_DEFLATED,
2023-03-27 12:03:07 -04:00
window_bits,
mem_level,
strategy,
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 {
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
2023-03-27 12:03:07 -04:00
unsafe {
zlib::inflateInit2_(
2023-03-27 12:03:07 -04:00
&mut self.strm,
window_bits,
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 {
// 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 {
// 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 {
// SAFETY: `self.strm` is an initialized `zlib::z_stream`.
2023-03-27 12:03:07 -04:00
unsafe {
zlib::inflateSetDictionary(
2023-03-27 12:03:07 -04:00
&mut self.strm,
dictionary.as_ptr() as *const _,
dictionary.len() as _,
)
}
}
}