forked from trifectatechfoundation/bzip2-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add WASM support (target
wasm32-unknown-unknown
)
inspired by zstd-rs (gyscos/zstd-rs#139)
- Loading branch information
Showing
9 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifdef PKG_CONFIG | ||
|
||
/* Just use installed headers */ | ||
#include <bzlib.h> | ||
|
||
#else // #ifdef PKG_CONFIG | ||
|
||
#include "bzip2-1.0.8/bzlib.h" | ||
|
||
#endif // #ifdef PKG_CONFIG | ||
|
||
|
||
/* This file is used to generate bindings for both headers. | ||
* Check update_bindings.sh to see how to use it. | ||
* Or use the `bindgen` feature, which will create the bindings automatically. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#[cfg(feature = "std")] | ||
use std::os::raw::{c_char, c_int, c_uint}; | ||
|
||
#[cfg(not(feature = "std"))] | ||
use libc::{c_char, c_int, c_uint}; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn test_decompress() -> bool { | ||
let uncompressed_bytes = include_bytes!("../bzip2-1.0.8/sample1.ref"); | ||
let compressed_bytes = include_bytes!("../bzip2-1.0.8/sample1.bz2"); | ||
let mut raw: Box<bzip2_sys::bz_stream> = unsafe { Box::new(std::mem::zeroed()) }; | ||
let mut buf: [u8; 100352] = [0; 98 * 1024]; | ||
unsafe { | ||
assert_eq!(bzip2_sys::BZ2_bzDecompressInit(&mut *raw, 0, 0 as c_int), 0); | ||
raw.next_in = compressed_bytes.as_ptr() as *mut c_char; | ||
raw.avail_in = compressed_bytes.len().min(c_uint::MAX as usize) as c_uint; | ||
|
||
raw.next_out = buf.as_mut_ptr() as *mut c_char; | ||
raw.avail_out = buf.len() as c_uint; | ||
assert_eq!( | ||
bzip2_sys::BZ2_bzDecompress(&mut *raw), | ||
bzip2_sys::BZ_STREAM_END | ||
); | ||
bzip2_sys::BZ2_bzDecompressEnd(&mut *raw); | ||
}; | ||
let total_out = ((raw.total_out_lo32 as u64) | ((raw.total_out_hi32 as u64) << 32)) as usize; | ||
assert_eq!(total_out, uncompressed_bytes.len()); | ||
|
||
let slice: &[u8] = buf[0..total_out].as_ref(); | ||
assert_eq!(uncompressed_bytes, slice); | ||
|
||
return true; | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stddef.h> | ||
|
||
#ifndef _STDLIB_H | ||
#define _STDLIB_H 1 | ||
|
||
void *rust_zstd_wasm_shim_malloc(size_t size); | ||
void *rust_zstd_wasm_shim_calloc(size_t nmemb, size_t size); | ||
void rust_zstd_wasm_shim_free(void *ptr); | ||
|
||
inline void *malloc(size_t size) { | ||
return rust_zstd_wasm_shim_malloc(size); | ||
} | ||
|
||
inline void *calloc(size_t nmemb, size_t size) { | ||
return rust_zstd_wasm_shim_calloc(nmemb, size); | ||
} | ||
|
||
inline void free(void *ptr) { | ||
rust_zstd_wasm_shim_free(ptr); | ||
} | ||
|
||
#endif // _STDLIB_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stdlib.h> | ||
|
||
#ifndef _STRING_H | ||
#define _STRING_H 1 | ||
|
||
void *rust_zstd_wasm_shim_memcpy(void *restrict dest, const void *restrict src, size_t n); | ||
void *rust_zstd_wasm_shim_memmove(void *dest, const void *src, size_t n); | ||
void *rust_zstd_wasm_shim_memset(void *dest, int c, size_t n); | ||
|
||
inline void *memcpy(void *restrict dest, const void *restrict src, size_t n) { | ||
return rust_zstd_wasm_shim_memcpy(dest, src, n); | ||
} | ||
|
||
inline void *memmove(void *dest, const void *src, size_t n) { | ||
return rust_zstd_wasm_shim_memmove(dest, src, n); | ||
} | ||
|
||
inline void *memset(void *dest, int c, size_t n) { | ||
return rust_zstd_wasm_shim_memset(dest, c, n); | ||
} | ||
|
||
#endif // _STRING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::alloc::{alloc, dealloc, Layout}; | ||
use std::os::raw::{c_int, c_void}; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn rust_zstd_wasm_shim_malloc(size: usize) -> *mut c_void { | ||
unsafe { | ||
let layout = Layout::from_size_align_unchecked(size, 1); | ||
alloc(layout).cast() | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn rust_zstd_wasm_shim_calloc(nmemb: usize, size: usize) -> *mut c_void { | ||
unsafe { | ||
let layout = Layout::from_size_align_unchecked(size * nmemb, 1); | ||
alloc(layout).cast() | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn rust_zstd_wasm_shim_free(ptr: *mut c_void) { | ||
// layout is not actually used | ||
let layout = Layout::from_size_align_unchecked(1, 1); | ||
dealloc(ptr.cast(), layout); | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn rust_zstd_wasm_shim_memcpy( | ||
dest: *mut c_void, | ||
src: *const c_void, | ||
n: usize, | ||
) -> *mut c_void { | ||
std::ptr::copy_nonoverlapping(src as *const u8, dest as *mut u8, n); | ||
dest | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn rust_zstd_wasm_shim_memmove( | ||
dest: *mut c_void, | ||
src: *const c_void, | ||
n: usize, | ||
) -> *mut c_void { | ||
std::ptr::copy(src as *const u8, dest as *mut u8, n); | ||
dest | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn rust_zstd_wasm_shim_memset( | ||
dest: *mut c_void, | ||
c: c_int, | ||
n: usize, | ||
) -> *mut c_void { | ||
std::ptr::write_bytes(dest as *mut u8, c as u8, n); | ||
dest | ||
} |