From d502dd9857f3880e483200bf46bcb01b7ad63dd5 Mon Sep 17 00:00:00 2001 From: Matt C Date: Mon, 19 Sep 2022 14:20:27 +0100 Subject: [PATCH] Add LZMA Decompress operation --- src/core/config/Categories.json | 3 +- src/core/operations/LZMACompress.mjs | 3 +- src/core/operations/LZMADecompress.mjs | 51 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/core/operations/LZMADecompress.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 24963c99..7869893a 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -330,8 +330,9 @@ "Bzip2 Compress", "Tar", "Untar", - "LZString Compress", "LZString Decompress", + "LZString Compress", + "LZMA Decompress", "LZMA Compress" ] }, diff --git a/src/core/operations/LZMACompress.mjs b/src/core/operations/LZMACompress.mjs index 5c038786..0be27089 100644 --- a/src/core/operations/LZMACompress.mjs +++ b/src/core/operations/LZMACompress.mjs @@ -44,8 +44,9 @@ class LZMACompress extends Operation { * @returns {ArrayBuffer} */ run(input, args) { + const mode = Number(args[0]); return new Promise((resolve, reject) => { - compress(new Uint8Array(input), Number(args[0]), (result, error) => { + compress(new Uint8Array(input), mode, (result, error) => { if (error) { reject(new OperationError(`Failed to compress input: ${error.message}`)); } diff --git a/src/core/operations/LZMADecompress.mjs b/src/core/operations/LZMADecompress.mjs new file mode 100644 index 00000000..44908f32 --- /dev/null +++ b/src/core/operations/LZMADecompress.mjs @@ -0,0 +1,51 @@ +/** + * @author Matt C [me@mitt.dev] + * @copyright Crown Copyright 2022 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; +import {decompress} from "@blu3r4y/lzma"; + +/** + * LZMA Decompress operation + */ +class LZMADecompress extends Operation { + + /** + * LZMADecompress constructor + */ + constructor() { + super(); + + this.name = "LZMA Decompress"; + this.module = "Compression"; + this.description = "Decompresses data using the Lempel-Ziv-Markov chain Algorithm."; + this.infoURL = "https://wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Markov_chain_algorithm"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; + } + + /** + * @param {ArrayBuffer} input + * @param {Object[]} args + * @returns {ArrayBuffer} + */ + run(input, args) { + return new Promise((resolve, reject) => { + decompress(new Uint8Array(input), (result, error) => { + if (error) { + reject(new OperationError(`Failed to decompress input: ${error.message}`)); + } + // The decompression returns as an Int8Array, but we can just get the unsigned data from the buffer + resolve(new Int8Array(result).buffer); + }, (percent) => { + self.sendStatusMessage(`Decompressing input: ${(percent*100).toFixed(2)}%`); + }); + }); + } + +} + +export default LZMADecompress;