From 02198f052d6e64c6126a55be4c4ed055b4d494bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sat, 22 Jun 2024 17:46:16 -0500 Subject: [PATCH] feat: migrated lz4 to bun --- src/core/blitz/readDVPL.ts | 25 +++++++++++++------ src/core/blitz/writeDVPL.ts | 50 ------------------------------------- 2 files changed, 17 insertions(+), 58 deletions(-) delete mode 100644 src/core/blitz/writeDVPL.ts diff --git a/src/core/blitz/readDVPL.ts b/src/core/blitz/readDVPL.ts index f27473acf..6fb5d0845 100644 --- a/src/core/blitz/readDVPL.ts +++ b/src/core/blitz/readDVPL.ts @@ -1,9 +1,8 @@ import { crc32 } from 'crc'; -import { decodeBlock } from 'lz4'; +import { decompressBlock } from 'lz4js'; /** - * Thanks Maddoxkkm! Modernized heavily. - * + * Thanks Maddoxkkm! Modified heavily to be modern. * https://github.com/Maddoxkkm/dvpl_converter/ */ export function readDVPL(buffer: Buffer) { @@ -30,6 +29,7 @@ export function readDVPL(buffer: Buffer) { if (crc32(targetBlock) !== footerData.crc32) { throw new TypeError('DVPL CRC32 mismatch'); } + if (footerData.type === 0) { if (!(footerData.oSize === footerData.cSize && footerData.type === 0)) { throw new RangeError('DVPL type and size mismatch'); @@ -37,14 +37,23 @@ export function readDVPL(buffer: Buffer) { return targetBlock; } else if (footerData.type === 1 || footerData.type === 2) { - const deDVPLBlock = Buffer.alloc(footerData.oSize); - const DecompressedBlockSize = decodeBlock(targetBlock, deDVPLBlock); + const source = new Uint8Array(targetBlock); + const destination = new Uint8Array(footerData.oSize); + const decompressedSize = decompressBlock( + source, + destination, + 0, + source.length, + 0, + ); - if (DecompressedBlockSize !== footerData.oSize) { - throw new RangeError('Decompressed DVPL size mismatch'); + if (decompressedSize !== footerData.oSize) { + throw new RangeError( + `Decompressed DVPL size mismatch (${decompressedSize} vs ${footerData.oSize})`, + ); } - return deDVPLBlock; + return Buffer.from(destination); } else { throw new SyntaxError('Unknown DVPL format'); } diff --git a/src/core/blitz/writeDVPL.ts b/src/core/blitz/writeDVPL.ts deleted file mode 100644 index 01617086f..000000000 --- a/src/core/blitz/writeDVPL.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { crc32 } from 'crc'; -import { encodeBlockHC } from 'lz4'; - -/** - * Thanks Maddoxkkm! Modernized heavily. - * - * https://github.com/Maddoxkkm/dvpl_converter/ - */ -export function writeDVPL(buffer: Buffer) { - let output = Buffer.alloc(buffer.length); - const compressedBlockSize = encodeBlockHC(buffer, output); - - if (compressedBlockSize === 0 || compressedBlockSize >= buffer.length) { - const footerBuffer = toDVPLFooter( - buffer.length, - buffer.length, - crc32(buffer), - 0, - ); - - return Buffer.concat([buffer, footerBuffer], buffer.length + 20); - } else { - output = output.subarray(0, compressedBlockSize); - const footerBuffer = toDVPLFooter( - buffer.length, - compressedBlockSize, - crc32(output), - 2, - ); - - return Buffer.concat([output, footerBuffer], compressedBlockSize + 20); - } -} - -function toDVPLFooter( - inputSize: number, - compressedSize: number, - crc32: number, - type: number, -) { - const result = Buffer.alloc(20); - - result.writeUInt32LE(inputSize, 0); - result.writeUInt32LE(compressedSize, 4); - result.writeUInt32LE(crc32, 8); - result.writeUInt32LE(type, 12); - result.write('DVPL', 16, 4, 'utf8'); - - return result; -}