Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'LZString Decompress' and 'LZString Compress' operations #1266

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"lodash": "^4.17.21",
"loglevel": "^1.7.1",
"loglevel-message-prefix": "^3.0.0",
"lz-string": "^1.4.4",
"markdown-it": "^12.2.0",
"moment": "^2.29.1",
"moment-timezone": "^0.5.33",
Expand Down
4 changes: 3 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@
"Bzip2 Decompress",
"Bzip2 Compress",
"Tar",
"Untar"
"Untar",
"LZString Compress",
"LZString Decompress"
]
},
{
Expand Down
21 changes: 21 additions & 0 deletions src/core/lib/LZString.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* lz-string exports.
*
* @author crespyl [peter@crespyl.net]
* @copyright Peter Jacobs 2021
* @license Apache-2.0
*/

import LZString from "lz-string";

export const COMPRESSION_OUTPUT_FORMATS = ["default", "UTF16", "Base64"];
export const COMPRESSION_FUNCTIONS = {
"default": LZString.compress,
"UTF16": LZString.compressToUTF16,
"Base64": LZString.compressToBase64,
};
export const DECOMPRESSION_FUNCTIONS = {
"default": LZString.decompress,
"UTF16": LZString.decompressFromUTF16,
"Base64": LZString.decompressFromBase64,
};
55 changes: 55 additions & 0 deletions src/core/operations/LZStringCompress.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @author crespyl [peter@crespyl.net]
* @copyright Peter Jacobs 2021
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

import {COMPRESSION_OUTPUT_FORMATS, COMPRESSION_FUNCTIONS} from "../lib/LZString.mjs";

/**
* LZString Compress operation
*/
class LZStringCompress extends Operation {

/**
* LZStringCompress constructor
*/
constructor() {
super();

this.name = "LZString Compress";
this.module = "Compression";
this.description = "Compress the input with lz-string.";
this.infoURL = "https://pieroxy.net/blog/pages/lz-string/index.html";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Compression Format",
type: "option",
defaultIndex: 0,
value: COMPRESSION_OUTPUT_FORMATS
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const compress = COMPRESSION_FUNCTIONS[args[0]];
if (compress) {
return compress(input);
} else {
throw new OperationError("Unable to find compression function");
}
}

}

export default LZStringCompress;
56 changes: 56 additions & 0 deletions src/core/operations/LZStringDecompress.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @author crespyl [peter@crespyl.net]
* @copyright Peter Jacobs 2021
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

import {COMPRESSION_OUTPUT_FORMATS, DECOMPRESSION_FUNCTIONS} from "../lib/LZString.mjs";

/**
* LZString Decompress operation
*/
class LZStringDecompress extends Operation {

/**
* LZStringDecompress constructor
*/
constructor() {
super();

this.name = "LZString Decompress";
this.module = "Compression";
this.description = "Decompresses data that was compressed with lz-string.";
this.infoURL = "https://pieroxy.net/blog/pages/lz-string/index.html";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Compression Format",
type: "option",
defaultIndex: 0,
value: COMPRESSION_OUTPUT_FORMATS
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const decompress = DECOMPRESSION_FUNCTIONS[args[0]];
if (decompress) {
return decompress(input);
} else {
throw new OperationError("Unable to find decompression function");
}
}


}

export default LZStringDecompress;
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import "./tests/CBORDecode.mjs";
import "./tests/JA3Fingerprint.mjs";
import "./tests/JA3SFingerprint.mjs";
import "./tests/HASSH.mjs";
import "./tests/LZString.mjs";


// Cannot test operations that use the File type yet
Expand Down
33 changes: 33 additions & 0 deletions tests/operations/tests/LZString.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* LZString tests.
*
* @author crespyl [peter@crespyl.net]
* @copyright Peter Jacobs 2021
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";

TestRegister.addTests([
{
name: "LZString Compress To Base64",
input: "hello world",
expectedOutput: "BYUwNmD2AEDukCcwBMg=",
recipeConfig: [
{
"op": "LZString Compress",
"args": ["Base64"]
}
],
},
{
name: "LZString Decompress From Base64",
input: "BYUwNmD2AEDukCcwBMg=",
expectedOutput: "hello world",
recipeConfig: [
{
"op": "LZString Decompress",
"args": ["Base64"]
}
],
}
]);