2024-07-03 21:18:40 -04:00
|
|
|
// Please note that the "meaningless" shifts of 0 are to force
|
|
|
|
// the value to be a 32-bit integer. Do not remove them.
|
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
let compressWasm: WebAssembly.WebAssemblyInstantiatedSource;
|
2024-07-03 21:18:40 -04:00
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
interface CompressWasmExports {
|
|
|
|
memory: WebAssembly.Memory;
|
|
|
|
agentDecompressWASM: any;
|
2024-07-03 21:18:40 -04:00
|
|
|
}
|
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
// Initalize the decompression module
|
|
|
|
export async function compressInit() {
|
|
|
|
let url = new URL('decompress.wasm', import.meta.url);
|
|
|
|
compressWasm = await WebAssembly.instantiateStreaming(fetch(url));
|
2024-07-03 21:18:40 -04:00
|
|
|
}
|
|
|
|
|
2024-07-04 05:13:35 -04:00
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
function compressWasmGetExports() {
|
|
|
|
return (compressWasm.instance.exports as any) as CompressWasmExports;
|
|
|
|
}
|
2024-07-04 05:13:35 -04:00
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
function compressWASMGetMemory() : WebAssembly.Memory {
|
|
|
|
return compressWasmGetExports().memory;
|
|
|
|
}
|
2024-07-04 05:13:35 -04:00
|
|
|
|
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
// debugging
|
|
|
|
//(window as any).DEBUGcompressGetWASM = () => {
|
|
|
|
// return compressWasm;
|
|
|
|
//}
|
2024-07-04 05:13:35 -04:00
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
// Decompress Agent compressed data. This compression algorithm sucks.
|
|
|
|
// [dest] is to be preallocated to the decompressed data size.
|
|
|
|
export function compressDecompress(src: Uint8Array, dest: Uint8Array) {
|
|
|
|
// Grow the WASM heap if needed. Funnily enough, this code is never hit in most
|
|
|
|
// ACSes, so IDK if it's even needed
|
|
|
|
let memory = compressWASMGetMemory();
|
|
|
|
if(memory.buffer.byteLength < src.length + dest.length) {
|
|
|
|
// A WebAssembly page is 64kb, so we need to grow at least that much
|
|
|
|
let npages = Math.floor((src.length + dest.length) / 65535) + 1;
|
|
|
|
console.log("Need to grow WASM heap", npages, "pages", "(current byteLength is", memory.buffer.byteLength, ", we need", src.length + dest.length, ")");
|
|
|
|
memory.grow(npages);
|
|
|
|
}
|
2024-07-04 05:13:35 -04:00
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
let copyBuffer = new Uint8Array(memory.buffer);
|
2024-07-04 05:13:35 -04:00
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
// Copy source to memory[0]. This will make things a bit simpler
|
|
|
|
copyBuffer.set(src, 0);
|
2024-07-04 05:13:35 -04:00
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
// Call the WASM compression routine
|
|
|
|
let nrBytesDecompressed = compressWasmGetExports().agentDecompressWASM(0, src.length, src.length, dest.length);
|
2024-07-04 05:13:35 -04:00
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
if(nrBytesDecompressed != dest.length)
|
|
|
|
throw new Error(`decompression failed: ${nrBytesDecompressed} != ${dest.length}`);
|
2024-07-04 05:13:35 -04:00
|
|
|
|
2024-07-09 18:53:18 -04:00
|
|
|
// Dest will be memory[src.length..dest.length]
|
|
|
|
dest.set(copyBuffer.slice(src.length, dest.length), 0);
|
2024-07-03 21:18:40 -04:00
|
|
|
}
|