#[cfg(target_os = "macos")]
#[no_mangle]
pub unsafe extern "C" fn bun_icu_maybe_decompress(
pointer: *const core::ffi::c_void,
_length: *mut i32,
) -> *const core::ffi::c_void {
pointer
}
#[cfg(not(target_os = "macos"))]
pub use compressed::bun_icu_maybe_decompress;
#[cfg(not(target_os = "macos"))]
mod compressed {
use core::ffi::c_void;
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
#[cfg(not(redjs_engine_dll))]
extern "C" {
static bun_icu_zstd_dict: u8;
static bun_icu_zstd_dict_size: u32;
}
#[cfg(redjs_engine_dll)]
extern "C" {
fn rjs_icu_zstd_dict(size: *mut u32) -> *const c_void;
}
unsafe fn dictionary() -> (*const c_void, usize) {
#[cfg(not(redjs_engine_dll))]
return (
core::ptr::addr_of!(bun_icu_zstd_dict) as *const c_void,
core::ptr::addr_of!(bun_icu_zstd_dict_size).read() as usize,
);
#[cfg(redjs_engine_dll)]
{
let mut size = 0;
let dictionary = rjs_icu_zstd_dict(&mut size);
(dictionary, size as usize)
}
}
const ZSTD_MAGIC: u32 = 0xFD2F_B528;
const ZSTD_CONTENTSIZE_UNKNOWN: u64 = u64::MAX; const ZSTD_CONTENTSIZE_ERROR: u64 = u64::MAX - 1;
struct State {
dctx: usize, ddict: usize, cache: HashMap<usize, (usize, i32)>,
}
unsafe impl Send for State {}
fn state() -> &'static Mutex<State> {
static S: OnceLock<Mutex<State>> = OnceLock::new();
S.get_or_init(|| unsafe {
let dctx = zstd_sys::ZSTD_createDCtx() as usize;
let (dict, dict_size) = dictionary();
let ddict = if dict_size > 0 {
zstd_sys::ZSTD_createDDict(dict, dict_size) as usize
} else {
0
};
Mutex::new(State { dctx, ddict, cache: HashMap::new() })
})
}
#[no_mangle]
pub unsafe extern "C" fn bun_icu_maybe_decompress(p: *const c_void, length: *mut i32) -> *const c_void {
if p.is_null() {
return p;
}
let magic = (p as *const u32).read_unaligned();
if magic != ZSTD_MAGIC {
return p; }
let mut st = state().lock().unwrap();
let toc_len = if length.is_null() { 0 } else { *length };
let bound = if toc_len > 0 { toc_len as usize } else { 1usize << 20 };
if let Some(&(buf, dlen)) = st.cache.get(&(p as usize)) {
if !length.is_null() {
*length = dlen;
}
return buf as *const c_void;
}
let clen = zstd_sys::ZSTD_findFrameCompressedSize(p, bound);
if zstd_sys::ZSTD_isError(clen) != 0 {
return p;
}
let dlen = zstd_sys::ZSTD_getFrameContentSize(p, clen);
if dlen == ZSTD_CONTENTSIZE_UNKNOWN || dlen == ZSTD_CONTENTSIZE_ERROR || dlen == 0 {
return p;
}
let mut buf = vec![0u8; dlen as usize].into_boxed_slice();
let r = if st.ddict != 0 {
zstd_sys::ZSTD_decompress_usingDDict(
st.dctx as *mut zstd_sys::ZSTD_DCtx,
buf.as_mut_ptr() as *mut c_void,
dlen as usize,
p,
clen,
st.ddict as *const zstd_sys::ZSTD_DDict,
)
} else {
zstd_sys::ZSTD_decompressDCtx(
st.dctx as *mut zstd_sys::ZSTD_DCtx,
buf.as_mut_ptr() as *mut c_void,
dlen as usize,
p,
clen,
)
};
if zstd_sys::ZSTD_isError(r) != 0 {
return p;
}
let leaked = Box::leak(buf);
st.cache.insert(p as usize, (leaked.as_ptr() as usize, dlen as i32));
if !length.is_null() {
*length = dlen as i32;
}
leaked.as_ptr() as *const c_void
}
}