Skip to content

Commit

Permalink
feat: support LZMA decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuk1ko committed Jun 13, 2024
1 parent 40bee9c commit 58aaf56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ crate-type = ["cdylib"]

[dependencies]
lz4_flex = "0.11.3"
lzma-rs = "0.3.0"
paste = "1.0.15"
texture2ddecoder = "0.0.5"
wasm-bindgen = "0.2.92"
35 changes: 31 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod tools;

use lz4_flex::decompress;
use lz4_flex::block::decompress_size_prepended;
use std::io::Cursor;

use texture2ddecoder::*;
use tools::*;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -55,9 +55,36 @@ pub fn export_decode_astc(
// LZ4
#[wasm_bindgen(js_name = decompressLz4)]
pub fn export_decompress_lz4(data: &[u8], size: usize) -> Result<Vec<u8>, JsError> {
decompress(data, size).map_err(to_js_err)
lz4_flex::decompress(data, size).map_err(to_js_err)
}
#[wasm_bindgen(js_name = decompressLz4SizePrepended)]
pub fn export_decompress_lz4_size_prepended(data: &[u8]) -> Result<Vec<u8>, JsError> {
decompress_size_prepended(data).map_err(to_js_err)
lz4_flex::decompress_size_prepended(data).map_err(to_js_err)
}

// LZMA
#[wasm_bindgen(js_name = decompressLzmaWithSize)]
pub fn export_decompress_lzma_with_size(data: &[u8], size: usize) -> Result<Vec<u8>, JsError> {
let mut data_with_size = Vec::with_capacity(data.len() + 8);
data_with_size.extend_from_slice(&data[..5]);
data_with_size.extend_from_slice(&(size as u64).to_le_bytes());
data_with_size.extend_from_slice(&data[5..]);

let mut out = Vec::new();
let result = lzma_rs::lzma_decompress(&mut Cursor::new(data_with_size), &mut out);

match result {
Ok(()) => Ok(out),
Err(e) => Err(to_js_err(e)),
}
}
#[wasm_bindgen(js_name = decompressLzma)]
pub fn export_decompress_lzma(data: &[u8]) -> Result<Vec<u8>, JsError> {
let mut out = Vec::new();
let result = lzma_rs::lzma_decompress(&mut Cursor::new(data), &mut out);

match result {
Ok(()) => Ok(out),
Err(e) => Err(to_js_err(e)),
}
}

0 comments on commit 58aaf56

Please sign in to comment.