msagent.js/core: Clean up C++/WASM decompression code
mostly just removing C-style casts and replacing the win32 LO* macros with constexpr functions, because they suck (also, add a gitignore scoped to msagent.js to ignore the obj/ of building the WASM decompression code)
This commit is contained in:
parent
95708da8cc
commit
d9faea4c1c
3 changed files with 32 additions and 24 deletions
1
msagent.js/.gitignore
vendored
Normal file
1
msagent.js/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
core/obj
|
|
@ -2,17 +2,25 @@
|
||||||
// Integer types from WASI-libc
|
// Integer types from WASI-libc
|
||||||
using usize = unsigned int;
|
using usize = unsigned int;
|
||||||
using u32 = unsigned int;
|
using u32 = unsigned int;
|
||||||
|
using u16 = unsigned short;
|
||||||
using u8 = unsigned char;
|
using u8 = unsigned char;
|
||||||
|
|
||||||
#define LOWORD(x) (x & 0xffff)
|
template<class T>
|
||||||
#define LOBYTE(x) (x & 0xff)
|
constexpr auto LowOrderShortOf(T item) {
|
||||||
|
return static_cast<u16>(item & 0xffff);
|
||||||
|
}
|
||||||
|
|
||||||
#define PUBLIC __attribute__((visibility("default"))) extern "C"
|
template<class T>
|
||||||
|
constexpr auto LowOrderByteOf(T item) {
|
||||||
|
return static_cast<u8>(item & 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
PUBLIC usize agentDecompressWASM(const void* pSrcData, usize pSrcSize, void* pTrgData, usize pTrgSize) {
|
#define PUBLIC extern "C" __attribute__((visibility("default")))
|
||||||
const u8* lSrcPtr = (const u8*)pSrcData;
|
|
||||||
|
PUBLIC usize agentDecompressWASM(const u8* pSrcData, usize pSrcSize, u8* pTrgData, usize pTrgSize) {
|
||||||
|
const u8* lSrcPtr = pSrcData;
|
||||||
const u8* lSrcEnd = lSrcPtr + pSrcSize;
|
const u8* lSrcEnd = lSrcPtr + pSrcSize;
|
||||||
u8* lTrgPtr = (u8*)pTrgData;
|
u8* lTrgPtr = pTrgData;
|
||||||
u8* lTrgEnd = lTrgPtr + pTrgSize;
|
u8* lTrgEnd = lTrgPtr + pTrgSize;
|
||||||
u32 lSrcQuad;
|
u32 lSrcQuad;
|
||||||
u8 lTrgByte;
|
u8 lTrgByte;
|
||||||
|
@ -38,15 +46,15 @@ PUBLIC usize agentDecompressWASM(const void* pSrcData, usize pSrcSize, void* pTr
|
||||||
lSrcPtr += 5;
|
lSrcPtr += 5;
|
||||||
|
|
||||||
while((lSrcPtr < lSrcEnd) && (lTrgPtr < lTrgEnd)) {
|
while((lSrcPtr < lSrcEnd) && (lTrgPtr < lTrgEnd)) {
|
||||||
lSrcQuad = *(const u32*)(lSrcPtr - sizeof(u32));
|
lSrcQuad = *reinterpret_cast<const u32*>(lSrcPtr - sizeof(u32));
|
||||||
|
|
||||||
if(lSrcQuad & (1 << LOWORD(lBitCount))) {
|
if(lSrcQuad & (1 << LowOrderShortOf(lBitCount))) {
|
||||||
lSrcOffset = 1;
|
lSrcOffset = 1;
|
||||||
|
|
||||||
if(lSrcQuad & (1 << LOWORD(lBitCount + 1))) {
|
if(lSrcQuad & (1 << LowOrderShortOf(lBitCount + 1))) {
|
||||||
if(lSrcQuad & (1 << LOWORD(lBitCount + 2))) {
|
if(lSrcQuad & (1 << LowOrderShortOf(lBitCount + 2))) {
|
||||||
if(lSrcQuad & (1 << LOWORD(lBitCount + 3))) {
|
if(lSrcQuad & (1 << LowOrderShortOf(lBitCount + 3))) {
|
||||||
lSrcQuad >>= LOWORD(lBitCount + 4);
|
lSrcQuad >>= LowOrderShortOf(lBitCount + 4);
|
||||||
lSrcQuad &= 0x000FFFFF;
|
lSrcQuad &= 0x000FFFFF;
|
||||||
if(lSrcQuad == 0x000FFFFF) {
|
if(lSrcQuad == 0x000FFFFF) {
|
||||||
break;
|
break;
|
||||||
|
@ -56,19 +64,19 @@ PUBLIC usize agentDecompressWASM(const void* pSrcData, usize pSrcSize, void* pTr
|
||||||
|
|
||||||
lSrcOffset = 2;
|
lSrcOffset = 2;
|
||||||
} else {
|
} else {
|
||||||
lSrcQuad >>= LOWORD(lBitCount + 4);
|
lSrcQuad >>= LowOrderShortOf(lBitCount + 4);
|
||||||
lSrcQuad &= 0x00000FFF;
|
lSrcQuad &= 0x00000FFF;
|
||||||
lSrcQuad += 577;
|
lSrcQuad += 577;
|
||||||
lBitCount += 16;
|
lBitCount += 16;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lSrcQuad >>= LOWORD(lBitCount + 3);
|
lSrcQuad >>= LowOrderShortOf(lBitCount + 3);
|
||||||
lSrcQuad &= 0x000001FF;
|
lSrcQuad &= 0x000001FF;
|
||||||
lSrcQuad += 65;
|
lSrcQuad += 65;
|
||||||
lBitCount += 12;
|
lBitCount += 12;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lSrcQuad >>= LOWORD(lBitCount + 2);
|
lSrcQuad >>= LowOrderShortOf(lBitCount + 2);
|
||||||
lSrcQuad &= 0x0000003F;
|
lSrcQuad &= 0x0000003F;
|
||||||
lSrcQuad += 1;
|
lSrcQuad += 1;
|
||||||
lBitCount += 8;
|
lBitCount += 8;
|
||||||
|
@ -76,16 +84,16 @@ PUBLIC usize agentDecompressWASM(const void* pSrcData, usize pSrcSize, void* pTr
|
||||||
|
|
||||||
lSrcPtr += (lBitCount / 8);
|
lSrcPtr += (lBitCount / 8);
|
||||||
lBitCount &= 7;
|
lBitCount &= 7;
|
||||||
lRunLgth = *(const u32*)(lSrcPtr - sizeof(u32));
|
lRunLgth = *reinterpret_cast<const u32*>(lSrcPtr - sizeof(u32));
|
||||||
lRunCount = 0;
|
lRunCount = 0;
|
||||||
while(lRunLgth & (1 << LOWORD(lBitCount + lRunCount))) {
|
while(lRunLgth & (1 << LowOrderShortOf(lBitCount + lRunCount))) {
|
||||||
lRunCount++;
|
lRunCount++;
|
||||||
if(lRunCount > 11) {
|
if(lRunCount > 11) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lRunLgth >>= LOWORD(lBitCount + lRunCount + 1);
|
lRunLgth >>= LowOrderShortOf(lBitCount + lRunCount + 1);
|
||||||
lRunLgth &= (1 << lRunCount) - 1;
|
lRunLgth &= (1 << lRunCount) - 1;
|
||||||
lRunLgth += 1 << lRunCount;
|
lRunLgth += 1 << lRunCount;
|
||||||
lRunLgth += lSrcOffset;
|
lRunLgth += lSrcOffset;
|
||||||
|
@ -103,10 +111,10 @@ PUBLIC usize agentDecompressWASM(const void* pSrcData, usize pSrcSize, void* pTr
|
||||||
lRunLgth--;
|
lRunLgth--;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lSrcQuad >>= LOWORD(lBitCount + 1);
|
lSrcQuad >>= LowOrderShortOf(lBitCount + 1);
|
||||||
lBitCount += 9;
|
lBitCount += 9;
|
||||||
|
|
||||||
lTrgByte = LOBYTE(lSrcQuad);
|
lTrgByte = LowOrderByteOf(lSrcQuad);
|
||||||
*(lTrgPtr++) = lTrgByte;
|
*(lTrgPtr++) = lTrgByte;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,5 +122,5 @@ PUBLIC usize agentDecompressWASM(const void* pSrcData, usize pSrcSize, void* pTr
|
||||||
lBitCount &= 7;
|
lBitCount &= 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (usize)(lTrgPtr - (u8*)pTrgData);
|
return static_cast<usize>(lTrgPtr - pTrgData);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,7 @@ function compressWASMGetMemory(): WebAssembly.Memory {
|
||||||
// Decompress Agent compressed data. This compression algorithm sucks.
|
// Decompress Agent compressed data. This compression algorithm sucks.
|
||||||
// [dest] is to be preallocated to the decompressed data size.
|
// [dest] is to be preallocated to the decompressed data size.
|
||||||
export function compressDecompress(src: Uint8Array, dest: Uint8Array) {
|
export function compressDecompress(src: Uint8Array, dest: Uint8Array) {
|
||||||
// Grow the WASM heap if needed. Funnily enough, this code is never hit in most
|
// Grow the WASM heap if needed.
|
||||||
// ACSes, so IDK if it's even needed
|
|
||||||
compressWasm.growHeapTo(src.length + dest.length);
|
compressWasm.growHeapTo(src.length + dest.length);
|
||||||
|
|
||||||
let memory = compressWASMGetMemory();
|
let memory = compressWASMGetMemory();
|
||||||
|
@ -41,7 +40,7 @@ export function compressDecompress(src: Uint8Array, dest: Uint8Array) {
|
||||||
// Call the WASM compression routine
|
// Call the WASM compression routine
|
||||||
let nrBytesDecompressed = compressWasmGetExports().agentDecompressWASM(0, src.length, src.length, dest.length);
|
let nrBytesDecompressed = compressWasmGetExports().agentDecompressWASM(0, src.length, src.length, dest.length);
|
||||||
|
|
||||||
if (nrBytesDecompressed != dest.length) throw new Error(`decompression failed: ${nrBytesDecompressed} != ${dest.length}`);
|
if (nrBytesDecompressed != dest.length) throw new Error(`Decompression failed: Output ${nrBytesDecompressed} != expected ${dest.length}`);
|
||||||
|
|
||||||
// The uncompressed data is located at memory[src.length..dest.length].
|
// The uncompressed data is located at memory[src.length..dest.length].
|
||||||
// Copy it into the destination buffer.
|
// Copy it into the destination buffer.
|
||||||
|
|
Loading…
Reference in a new issue