Debian Codegraph @

.cargo/vendor/flate2/src/ffi/c.rs

raw
1 //! Implementation for C backends.
2 use std::fmt;
3 use std::marker;
4 use std::mem::MaybeUninit;
5 use std::os::raw::{c_int, c_uint};
6 use std::ptr;
7
8 use super::*;
9 use crate::mem;
10
11 #[derive(Clone, Default)]
12 pub struct ErrorMessage(Option<&'static str>);
13
14 impl ErrorMessage {
15 pub fn get(&self) -> Option<&str> {
16 self.0
17 }
18 }
19
20 pub struct StreamWrapper {
21 // SAFETY: The field `inner` must always be accessed as a raw pointer,
22 // since it points to a cyclic structure, and it must never be copied
23 // by Rust.
24 pub inner: *mut mz_stream,
25 }
26
27 impl fmt::Debug for StreamWrapper {
28 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
29 write!(f, "StreamWrapper")
30 }
31 }
32
33 impl Default for StreamWrapper {
34 fn default() -> StreamWrapper {
35 // SAFETY: The field `state` will be initialized across the FFI to
36 // point to the opaque type `mz_internal_state`, which will contain a copy
37 // of `inner`. This cyclic structure breaks the uniqueness invariant of
38 // &mut mz_stream, so we must use a raw pointer instead of Box<mz_stream>.
39 StreamWrapper {
40 inner: Box::into_raw(Box::new(mz_stream {
41 next_in: ptr::null_mut(),
42 avail_in: 0,
43 total_in: 0,
44 next_out: ptr::null_mut(),
45 avail_out: 0,
46 total_out: 0,
47 msg: ptr::null_mut(),
48 adler: 0,
49 data_type: 0,
50 reserved: 0,
51 opaque: ptr::null_mut(),
52 state: ptr::null_mut(),
53
54 #[cfg(any(
55 // zlib-ng
56 feature = "zlib-ng",
57 // libz-sys
58 all(not(feature = "cloudflare_zlib"), not(feature = "zlib-ng"), not(feature = "zlib-rs"))
59 ))]
60 zalloc: allocator::zalloc,
61 #[cfg(any(
62 // zlib-ng
63 feature = "zlib-ng",
64 // libz-sys
65 all(not(feature = "cloudflare_zlib"), not(feature = "zlib-ng"), not(feature = "zlib-rs"))
66 ))]
67 zfree: allocator::zfree,
68
69 #[cfg(
70 // cloudflare-zlib
71 all(feature = "cloudflare_zlib", not(feature = "zlib-rs"), not(feature = "zlib-ng")),
72 )]
73 zalloc: Some(allocator::zalloc),
74 #[cfg(
75 // cloudflare-zlib
76 all(feature = "cloudflare_zlib", not(feature = "zlib-rs"), not(feature = "zlib-ng")),
77 )]
78 zfree: Some(allocator::zfree),
79
80 // for zlib-rs, it is most efficient to have it provide the allocator.
81 // The libz-rs-sys dependency is configured to use the rust system allocator
82 #[cfg(all(feature = "zlib-rs", not(feature = "zlib-ng")))]
83 zalloc: None,
84 #[cfg(all(feature = "zlib-rs", not(feature = "zlib-ng")))]
85 zfree: None,
86 })),
87 }
88 }
89 }
90
91 impl Drop for StreamWrapper {
92 fn drop(&mut self) {
93 // SAFETY: At this point, every other allocation for struct has been freed by
94 // `inflateEnd` or `deflateEnd`, and no copies of `inner` are retained by `C`,
95 // so it is safe to drop the struct as long as the user respects the invariant that
96 // `inner` must never be copied by Rust.
97 drop(unsafe { Box::from_raw(self.inner) });
98 }
99 }
100
101 #[cfg(any(
102 // zlib-ng
103 feature = "zlib-ng",
104 // cloudflare-zlib
105 all(feature = "cloudflare_zlib", not(feature = "zlib-rs"), not(feature = "zlib-ng")),
106 // libz-sys
107 all(not(feature = "cloudflare_zlib"), not(feature = "zlib-ng"), not(feature = "zlib-rs")),
108 ))]
109 mod allocator {
110 use super::*;
111
112 use std::alloc::{self, Layout};
113 use std::convert::TryFrom;
114 use std::os::raw::c_void;
115
116 const ALIGN: usize = std::mem::align_of::<usize>();
117
118 fn align_up(size: usize, align: usize) -> usize {
119 (size + align - 1) & !(align - 1)
120 }
121
122 pub extern "C" fn zalloc(_ptr: *mut c_void, items: uInt, item_size: uInt) -> *mut c_void {
123 // We need to multiply `items` and `item_size` to get the actual desired
124 // allocation size. Since `zfree` doesn't receive a size argument we
125 // also need to allocate space for a `usize` as a header so we can store
126 // how large the allocation is to deallocate later.
127 let size = match items
128 .checked_mul(item_size)
129 .and_then(|i| usize::try_from(i).ok())
130 .map(|size| align_up(size, ALIGN))
131 .and_then(|i| i.checked_add(std::mem::size_of::<usize>()))
132 {
133 Some(i) => i,
134 None => return ptr::null_mut(),
135 };
136
137 // Make sure the `size` isn't too big to fail `Layout`'s restrictions
138 let layout = match Layout::from_size_align(size, ALIGN) {
139 Ok(layout) => layout,
140 Err(_) => return ptr::null_mut(),
141 };
142
143 unsafe {
144 // Allocate the data, and if successful store the size we allocated
145 // at the beginning and then return an offset pointer.
146 let ptr = alloc::alloc(layout) as *mut usize;
147 if ptr.is_null() {
148 return ptr as *mut c_void;
149 }
150 *ptr = size;
151 ptr.add(1) as *mut c_void
152 }
153 }
154
155 pub extern "C" fn zfree(_ptr: *mut c_void, address: *mut c_void) {
156 unsafe {
157 // Move our address being freed back one pointer, read the size we
158 // stored in `zalloc`, and then free it using the standard Rust
159 // allocator.
160 let ptr = (address as *mut usize).offset(-1);
161 let size = *ptr;
162 let layout = Layout::from_size_align_unchecked(size, ALIGN);
163 alloc::dealloc(ptr as *mut u8, layout)
164 }
165 }
166 }
167
168 unsafe impl<D: Direction> Send for Stream<D> {}
169 unsafe impl<D: Direction> Sync for Stream<D> {}
170
171 /// Trait used to call the right destroy/end function on the inner
172 /// stream object on drop.
173 pub trait Direction {
174 unsafe fn destroy(stream: *mut mz_stream) -> c_int;
175 }
176
177 #[derive(Debug)]
178 pub enum DirCompress {}
179 #[derive(Debug)]
180 pub enum DirDecompress {}
181
182 #[derive(Debug)]
183 pub struct Stream<D: Direction> {
184 pub stream_wrapper: StreamWrapper,
185 pub total_in: u64,
186 pub total_out: u64,
187 pub _marker: marker::PhantomData<D>,
188 }
189
190 impl<D: Direction> Stream<D> {
191 pub fn msg(&self) -> ErrorMessage {
192 // SAFETY: The field `inner` must always be accessed as a raw pointer,
193 // since it points to a cyclic structure. No copies of `inner` can be
194 // retained for longer than the lifetime of `self`.
195 let msg = unsafe { (*self.stream_wrapper.inner).msg };
196 ErrorMessage(if msg.is_null() {
197 None
198 } else {
199 let s = unsafe { std::ffi::CStr::from_ptr(msg) };
200 std::str::from_utf8(s.to_bytes()).ok()
201 })
202 }
203 }
204
205 impl<D: Direction> Drop for Stream<D> {
206 fn drop(&mut self) {
207 unsafe {
208 let _ = D::destroy(self.stream_wrapper.inner);
209 }
210 }
211 }
212
213 impl Direction for DirCompress {
214 unsafe fn destroy(stream: *mut mz_stream) -> c_int {
215 mz_deflateEnd(stream)
216 }
217 }
218 impl Direction for DirDecompress {
219 unsafe fn destroy(stream: *mut mz_stream) -> c_int {
220 mz_inflateEnd(stream)
221 }
222 }
223
224 #[derive(Debug)]
225 pub struct Inflate {
226 pub inner: Stream<DirDecompress>,
227 }
228
229 impl Inflate {
230 unsafe fn decompress_inner(
231 &mut self,
232 input: &[u8],
233 output_ptr: *mut u8,
234 output_len: usize,
235 flush: FlushDecompress,
236 ) -> Result<Status, DecompressError> {
237 let raw = self.inner.stream_wrapper.inner;
238 // SAFETY: The field `inner` must always be accessed as a raw pointer,
239 // since it points to a cyclic structure. No copies of `inner` can be
240 // retained for longer than the lifetime of `self`.
241 unsafe {
242 (*raw).msg = ptr::null_mut();
243 (*raw).next_in = input.as_ptr() as *mut u8;
244 (*raw).avail_in = input.len().min(c_uint::MAX as usize) as c_uint;
245 (*raw).next_out = output_ptr;
246 (*raw).avail_out = output_len.min(c_uint::MAX as usize) as c_uint;
247
248 let rc = mz_inflate(raw, flush as c_int);
249
250 // Unfortunately the total counters provided by zlib might be only
251 // 32 bits wide and overflow while processing large amounts of data.
252 self.inner.total_in += ((*raw).next_in as usize - input.as_ptr() as usize) as u64;
253 self.inner.total_out += ((*raw).next_out as usize - output_ptr as usize) as u64;
254
255 // reset these pointers so we don't accidentally read them later
256 (*raw).next_in = ptr::null_mut();
257 (*raw).avail_in = 0;
258 (*raw).next_out = ptr::null_mut();
259 (*raw).avail_out = 0;
260
261 match rc {
262 MZ_DATA_ERROR | MZ_STREAM_ERROR | MZ_MEM_ERROR => {
263 mem::decompress_failed(self.inner.msg())
264 }
265 MZ_OK => Ok(Status::Ok),
266 MZ_BUF_ERROR => Ok(Status::BufError),
267 MZ_STREAM_END => Ok(Status::StreamEnd),
268 #[allow(clippy::unnecessary_cast)]
269 MZ_NEED_DICT => mem::decompress_need_dict((*raw).adler as u32),
270 c => panic!("unknown return code: {}", c),
271 }
272 }
273 }
274 }
275 impl InflateBackend for Inflate {
276 fn make(zlib_header: bool, window_bits: u8) -> Self {
277 unsafe {
278 let state = StreamWrapper::default();
279 let ret = mz_inflateInit2(
280 state.inner,
281 if zlib_header {
282 window_bits as c_int
283 } else {
284 -(window_bits as c_int)
285 },
286 );
287 assert_eq!(ret, 0);
288 Inflate {
289 inner: Stream {
290 stream_wrapper: state,
291 total_in: 0,
292 total_out: 0,
293 _marker: marker::PhantomData,
294 },
295 }
296 }
297 }
298
299 fn decompress(
300 &mut self,
301 input: &[u8],
302 output: &mut [u8],
303 flush: FlushDecompress,
304 ) -> Result<Status, DecompressError> {
305 unsafe { self.decompress_inner(input, output.as_mut_ptr(), output.len(), flush) }
306 }
307 fn decompress_uninit(
308 &mut self,
309 input: &[u8],
310 output: &mut [MaybeUninit<u8>],
311 flush: FlushDecompress,
312 ) -> Result<Status, DecompressError> {
313 unsafe { self.decompress_inner(input, output.as_mut_ptr() as *mut _, output.len(), flush) }
314 }
315
316 fn reset(&mut self, zlib_header: bool) {
317 let bits = if zlib_header {
318 MZ_DEFAULT_WINDOW_BITS
319 } else {
320 -MZ_DEFAULT_WINDOW_BITS
321 };
322 unsafe {
323 inflateReset2(self.inner.stream_wrapper.inner, bits);
324 }
325 self.inner.total_out = 0;
326 self.inner.total_in = 0;
327 }
328 }
329
330 impl Backend for Inflate {
331 #[inline]
332 fn total_in(&self) -> u64 {
333 self.inner.total_in
334 }
335
336 #[inline]
337 fn total_out(&self) -> u64 {
338 self.inner.total_out
339 }
340 }
341
342 #[derive(Debug)]
343 pub struct Deflate {
344 pub inner: Stream<DirCompress>,
345 }
346
347 impl Deflate {
348 unsafe fn compress_inner(
349 &mut self,
350 input: &[u8],
351 output_ptr: *mut u8,
352 output_len: usize,
353 flush: FlushCompress,
354 ) -> Result<Status, CompressError> {
355 let raw = self.inner.stream_wrapper.inner;
356 // SAFETY: The field `inner` must always be accessed as a raw pointer,
357 // since it points to a cyclic structure. No copies of `inner` can be
358 // retained for longer than the lifetime of `self`.
359 unsafe {
360 (*raw).msg = ptr::null_mut();
361 (*raw).next_in = input.as_ptr() as *mut _;
362 (*raw).avail_in = input.len().min(c_uint::MAX as usize) as c_uint;
363 (*raw).next_out = output_ptr;
364 (*raw).avail_out = output_len.min(c_uint::MAX as usize) as c_uint;
365
366 let rc = mz_deflate(raw, flush as c_int);
367
368 // Unfortunately the total counters provided by zlib might be only
369 // 32 bits wide and overflow while processing large amounts of data.
370
371 self.inner.total_in += ((*raw).next_in as usize - input.as_ptr() as usize) as u64;
372 self.inner.total_out += ((*raw).next_out as usize - output_ptr as usize) as u64;
373 // reset these pointers so we don't accidentally read them later
374 (*raw).next_in = ptr::null_mut();
375 (*raw).avail_in = 0;
376 (*raw).next_out = ptr::null_mut();
377 (*raw).avail_out = 0;
378
379 match rc {
380 MZ_OK => Ok(Status::Ok),
381 MZ_BUF_ERROR => Ok(Status::BufError),
382 MZ_STREAM_END => Ok(Status::StreamEnd),
383 MZ_STREAM_ERROR => mem::compress_failed(self.inner.msg()),
384 c => panic!("unknown return code: {}", c),
385 }
386 }
387 }
388 }
389
390 impl DeflateBackend for Deflate {
391 fn make(level: Compression, zlib_header: bool, window_bits: u8) -> Self {
392 unsafe {
393 let state = StreamWrapper::default();
394 let ret = mz_deflateInit2(
395 state.inner,
396 level.0 as c_int,
397 MZ_DEFLATED,
398 if zlib_header {
399 window_bits as c_int
400 } else {
401 -(window_bits as c_int)
402 },
403 8,
404 MZ_DEFAULT_STRATEGY,
405 );
406 assert_eq!(ret, 0);
407 Deflate {
408 inner: Stream {
409 stream_wrapper: state,
410 total_in: 0,
411 total_out: 0,
412 _marker: marker::PhantomData,
413 },
414 }
415 }
416 }
417 fn compress(
418 &mut self,
419 input: &[u8],
420 output: &mut [u8],
421 flush: FlushCompress,
422 ) -> Result<Status, CompressError> {
423 unsafe { self.compress_inner(input, output.as_mut_ptr(), output.len(), flush) }
424 }
425 fn compress_uninit(
426 &mut self,
427 input: &[u8],
428 output: &mut [MaybeUninit<u8>],
429 flush: FlushCompress,
430 ) -> Result<Status, CompressError> {
431 unsafe { self.compress_inner(input, output.as_mut_ptr() as *mut _, output.len(), flush) }
432 }
433 fn reset(&mut self) {
434 self.inner.total_in = 0;
435 self.inner.total_out = 0;
436 let rc = unsafe { mz_deflateReset(self.inner.stream_wrapper.inner) };
437 assert_eq!(rc, MZ_OK);
438 }
439 }
440
441 impl Backend for Deflate {
442 #[inline]
443 fn total_in(&self) -> u64 {
444 self.inner.total_in
445 }
446
447 #[inline]
448 fn total_out(&self) -> u64 {
449 self.inner.total_out
450 }
451 }
452
453 pub use self::c_backend::*;
454
455 /// For backwards compatibility, we provide symbols as `mz_` to mimic the miniz API
456 #[allow(bad_style)]
457 #[allow(unused_imports)]
458 mod c_backend {
459 use std::mem;
460 use std::os::raw::{c_char, c_int};
461
462 #[cfg(feature = "zlib-ng")]
463 use libz_ng_sys as libz;
464
465 #[cfg(all(feature = "zlib-rs", not(feature = "zlib-ng")))]
466 use libz_rs_sys as libz;
467
468 #[cfg(
469 // cloudflare-zlib
470 all(feature = "cloudflare_zlib", not(feature = "zlib-rs"), not(feature = "zlib-ng")),
471 )]
472 use cloudflare_zlib_sys as libz;
473
474 #[cfg(
475 // libz-sys
476 all(not(feature = "cloudflare_zlib"), not(feature = "zlib-ng"), not(feature = "zlib-rs")),
477 )]
478 use libz_sys as libz;
479
480 pub use libz::deflate as mz_deflate;
481 pub use libz::deflateEnd as mz_deflateEnd;
482 pub use libz::deflateReset as mz_deflateReset;
483 pub use libz::inflate as mz_inflate;
484 pub use libz::inflateEnd as mz_inflateEnd;
485 pub use libz::z_stream as mz_stream;
486 pub use libz::*;
487
488 pub use libz::Z_BLOCK as MZ_BLOCK;
489 pub use libz::Z_BUF_ERROR as MZ_BUF_ERROR;
490 pub use libz::Z_DATA_ERROR as MZ_DATA_ERROR;
491 pub use libz::Z_DEFAULT_STRATEGY as MZ_DEFAULT_STRATEGY;
492 pub use libz::Z_DEFLATED as MZ_DEFLATED;
493 pub use libz::Z_FINISH as MZ_FINISH;
494 pub use libz::Z_FULL_FLUSH as MZ_FULL_FLUSH;
495 pub use libz::Z_MEM_ERROR as MZ_MEM_ERROR;
496 pub use libz::Z_NEED_DICT as MZ_NEED_DICT;
497 pub use libz::Z_NO_FLUSH as MZ_NO_FLUSH;
498 pub use libz::Z_OK as MZ_OK;
499 pub use libz::Z_PARTIAL_FLUSH as MZ_PARTIAL_FLUSH;
500 pub use libz::Z_STREAM_END as MZ_STREAM_END;
501 pub use libz::Z_STREAM_ERROR as MZ_STREAM_ERROR;
502 pub use libz::Z_SYNC_FLUSH as MZ_SYNC_FLUSH;
503
504 pub const MZ_DEFAULT_WINDOW_BITS: c_int = 15;
505
506 pub unsafe extern "C" fn mz_deflateInit2(
507 stream: *mut mz_stream,
508 level: c_int,
509 method: c_int,
510 window_bits: c_int,
511 mem_level: c_int,
512 strategy: c_int,
513 ) -> c_int {
514 libz::deflateInit2_(
515 stream,
516 level,
517 method,
518 window_bits,
519 mem_level,
520 strategy,
521 zlibVersion(),
522 mem::size_of::<mz_stream>() as c_int,
523 )
524 }
525 pub unsafe extern "C" fn mz_inflateInit2(stream: *mut mz_stream, window_bits: c_int) -> c_int {
526 libz::inflateInit2_(
527 stream,
528 window_bits,
529 zlibVersion(),
530 mem::size_of::<mz_stream>() as c_int,
531 )
532 }
533 }