Skip to content

Commit c02c4a7

Browse files
committed
Merge branch 'add-lz-string' of https://github.com/crespyl/CyberChef
2 parents f97ce18 + 671ae65 commit c02c4a7

File tree

8 files changed

+175
-1
lines changed

8 files changed

+175
-1
lines changed

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
"lodash": "^4.17.21",
134134
"loglevel": "^1.8.0",
135135
"loglevel-message-prefix": "^3.0.0",
136+
"lz-string": "^1.4.4",
136137
"markdown-it": "^13.0.1",
137138
"moment": "^2.29.3",
138139
"moment-timezone": "^0.5.34",

src/core/config/Categories.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@
325325
"Bzip2 Decompress",
326326
"Bzip2 Compress",
327327
"Tar",
328-
"Untar"
328+
"Untar",
329+
"LZString Compress",
330+
"LZString Decompress"
329331
]
330332
},
331333
{

src/core/lib/LZString.mjs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* lz-string exports.
3+
*
4+
* @author crespyl [[email protected]]
5+
* @copyright Peter Jacobs 2021
6+
* @license Apache-2.0
7+
*/
8+
9+
import LZString from "lz-string";
10+
11+
export const COMPRESSION_OUTPUT_FORMATS = ["default", "UTF16", "Base64"];
12+
export const COMPRESSION_FUNCTIONS = {
13+
"default": LZString.compress,
14+
"UTF16": LZString.compressToUTF16,
15+
"Base64": LZString.compressToBase64,
16+
};
17+
export const DECOMPRESSION_FUNCTIONS = {
18+
"default": LZString.decompress,
19+
"UTF16": LZString.decompressFromUTF16,
20+
"Base64": LZString.decompressFromBase64,
21+
};
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @author crespyl [[email protected]]
3+
* @copyright Peter Jacobs 2021
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
import OperationError from "../errors/OperationError.mjs";
9+
10+
import {COMPRESSION_OUTPUT_FORMATS, COMPRESSION_FUNCTIONS} from "../lib/LZString.mjs";
11+
12+
/**
13+
* LZString Compress operation
14+
*/
15+
class LZStringCompress extends Operation {
16+
17+
/**
18+
* LZStringCompress constructor
19+
*/
20+
constructor() {
21+
super();
22+
23+
this.name = "LZString Compress";
24+
this.module = "Compression";
25+
this.description = "Compress the input with lz-string.";
26+
this.infoURL = "https://pieroxy.net/blog/pages/lz-string/index.html";
27+
this.inputType = "string";
28+
this.outputType = "string";
29+
this.args = [
30+
{
31+
name: "Compression Format",
32+
type: "option",
33+
defaultIndex: 0,
34+
value: COMPRESSION_OUTPUT_FORMATS
35+
}
36+
];
37+
}
38+
39+
/**
40+
* @param {string} input
41+
* @param {Object[]} args
42+
* @returns {string}
43+
*/
44+
run(input, args) {
45+
const compress = COMPRESSION_FUNCTIONS[args[0]];
46+
if (compress) {
47+
return compress(input);
48+
} else {
49+
throw new OperationError("Unable to find compression function");
50+
}
51+
}
52+
53+
}
54+
55+
export default LZStringCompress;
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @author crespyl [[email protected]]
3+
* @copyright Peter Jacobs 2021
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
import OperationError from "../errors/OperationError.mjs";
9+
10+
import {COMPRESSION_OUTPUT_FORMATS, DECOMPRESSION_FUNCTIONS} from "../lib/LZString.mjs";
11+
12+
/**
13+
* LZString Decompress operation
14+
*/
15+
class LZStringDecompress extends Operation {
16+
17+
/**
18+
* LZStringDecompress constructor
19+
*/
20+
constructor() {
21+
super();
22+
23+
this.name = "LZString Decompress";
24+
this.module = "Compression";
25+
this.description = "Decompresses data that was compressed with lz-string.";
26+
this.infoURL = "https://pieroxy.net/blog/pages/lz-string/index.html";
27+
this.inputType = "string";
28+
this.outputType = "string";
29+
this.args = [
30+
{
31+
name: "Compression Format",
32+
type: "option",
33+
defaultIndex: 0,
34+
value: COMPRESSION_OUTPUT_FORMATS
35+
}
36+
];
37+
}
38+
39+
/**
40+
* @param {string} input
41+
* @param {Object[]} args
42+
* @returns {string}
43+
*/
44+
run(input, args) {
45+
const decompress = DECOMPRESSION_FUNCTIONS[args[0]];
46+
if (decompress) {
47+
return decompress(input);
48+
} else {
49+
throw new OperationError("Unable to find decompression function");
50+
}
51+
}
52+
53+
54+
}
55+
56+
export default LZStringDecompress;

tests/operations/index.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import "./tests/ELFInfo.mjs";
118118
import "./tests/Subsection.mjs";
119119
import "./tests/CaesarBoxCipher.mjs";
120120
import "./tests/LS47.mjs";
121+
import "./tests/LZString.mjs";
121122

122123

123124
// Cannot test operations that use the File type yet

tests/operations/tests/LZString.mjs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* LZString tests.
3+
*
4+
* @author crespyl [[email protected]]
5+
* @copyright Peter Jacobs 2021
6+
* @license Apache-2.0
7+
*/
8+
import TestRegister from "../../lib/TestRegister.mjs";
9+
10+
TestRegister.addTests([
11+
{
12+
name: "LZString Compress To Base64",
13+
input: "hello world",
14+
expectedOutput: "BYUwNmD2AEDukCcwBMg=",
15+
recipeConfig: [
16+
{
17+
"op": "LZString Compress",
18+
"args": ["Base64"]
19+
}
20+
],
21+
},
22+
{
23+
name: "LZString Decompress From Base64",
24+
input: "BYUwNmD2AEDukCcwBMg=",
25+
expectedOutput: "hello world",
26+
recipeConfig: [
27+
{
28+
"op": "LZString Decompress",
29+
"args": ["Base64"]
30+
}
31+
],
32+
}
33+
]);

0 commit comments

Comments
 (0)