Skip to content

Commit

Permalink
Avoid using C types on wasm32-unknown-unknown target
Browse files Browse the repository at this point in the history
Temporary fix until we change the callback API
  • Loading branch information
oyvindln committed Jul 13, 2018
1 parent e80cc4e commit 828fbef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
27 changes: 23 additions & 4 deletions miniz_oxide/src/deflate/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ use super::super::*;
use shared::{HUFFMAN_LENGTH_ORDER, MZ_ADLER32_INIT, update_adler32};
use deflate::buffer::{HashBuffers, LZ_CODE_BUF_SIZE, OUT_BUF_SIZE, LocalBuf};


// Avoid using libc types on wasm, since they don't exist.
// This is a temporary fix until the callback API has improved.
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[allow(non_camel_case_types)]
type callback_c_void = u8;
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[allow(non_camel_case_types)]
type callback_c_int = i32;
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
use libc::c_void as callback_c_void;
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
use libc::c_int as callback_c_int;


const MAX_PROBES_MASK: i32 = 0xFFF;

const MAX_SUPPORTED_HUFF_CODESIZE: usize = 32;
Expand Down Expand Up @@ -156,7 +171,8 @@ struct SymFreq {
}

/// Compression callback function type.
pub type PutBufFuncPtrNotNull = unsafe extern "C" fn(*const c_void, c_int, *mut c_void) -> bool;
pub type PutBufFuncPtrNotNull = unsafe extern "C" fn(
*const callback_c_void, callback_c_int, *mut callback_c_void) -> bool;
/// `Option` alias for compression callback function type.
pub type PutBufFuncPtr = Option<PutBufFuncPtrNotNull>;

Expand Down Expand Up @@ -221,7 +237,7 @@ impl From<MZFlush> for TDEFLFlush {
}

impl TDEFLFlush {
pub fn new(flush: c_int) -> Result<Self, MZError> {
pub fn new(flush: i32) -> Result<Self, MZError> {
match flush {
0 => Ok(TDEFLFlush::None),
2 => Ok(TDEFLFlush::Sync),
Expand Down Expand Up @@ -365,7 +381,7 @@ impl CompressorOxide {
#[derive(Copy, Clone)]
pub struct CallbackFunc {
pub put_buf_func: PutBufFuncPtrNotNull,
pub put_buf_user: *mut c_void,
pub put_buf_user: *mut callback_c_void,
}

impl CallbackFunc {
Expand All @@ -379,7 +395,7 @@ impl CallbackFunc {
// this whole function should maybe be unsafe as well.
let call_success = unsafe {
(self.put_buf_func)(
&params.local_buf.b[0] as *const u8 as *const c_void,
&params.local_buf.b[0] as *const u8 as *const callback_c_void,
saved_output.pos as i32,
self.put_buf_user,
)
Expand Down Expand Up @@ -2032,6 +2048,9 @@ pub fn compress(
/// # Returns
/// Returns a tuple containing the current status of the compressor, the current position
/// in the input buffer.
///
/// The caller is responsible for ensuring the CallbackFunc struct will not cause undefined
/// behaviour.
pub fn compress_to_output(
d: &mut CompressorOxide,
in_buf: &[u8],
Expand Down
4 changes: 1 addition & 3 deletions miniz_oxide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ mod shared;
pub use shared::update_adler32 as mz_adler32_oxide;
pub use shared::MZ_ADLER32_INIT;

use libc::{c_int, c_void};

/// A list of flush types.
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand All @@ -49,7 +47,7 @@ impl MZFlush {
/// Create a Flush instance from an integer value.
///
/// Returns `MZError::Param` on invalid values.
pub fn new(flush: c_int) -> Result<Self, MZError> {
pub fn new(flush: i32) -> Result<Self, MZError> {
match flush {
0 => Ok(MZFlush::None),
1 | 2 => Ok(MZFlush::Sync),
Expand Down

0 comments on commit 828fbef

Please sign in to comment.