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

Feat(Zstandard Decode) #1905

Closed
wants to merge 6 commits into from
Closed
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
43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Variables
UPSTREAM_REPO := upstream
UPSTREAM_URL := https://github.com/gchq/CyberChef.git
FORK_REPO := origin
DEFAULT_BRANCH := main

# Default target if no target is specified
.DEFAULT_GOAL := help

# Help command
help:
@echo "Usage:"
@echo " make pr PR_ID=<PR_ID> - Fetch and push a PR from the upstream repo"
@echo " make setup-upstream - Set up upstream repo (idempotent)"
@echo " make clean PR_ID=<PR_ID> - Clean up the local PR branch"

# Setup upstream repo (idempotent)
setup-upstream:
@echo "Checking if upstream repo exists..."
@if ! git remote get-url $(UPSTREAM_REPO) >/dev/null 2>&1; then \
echo "Upstream repo not found. Adding upstream..."; \
git remote add $(UPSTREAM_REPO) $(UPSTREAM_URL); \
else \
echo "Upstream repo already exists."; \
fi

# Fetch the PR from upstream, create a branch, and push it to your fork
pr: setup-upstream
@if [ -z "$(PR_ID)" ]; then \
echo "Error: PR_ID is not set. Usage: make pr PR_ID=<PR_ID>"; \
exit 1; \
fi
git fetch $(UPSTREAM_REPO) pull/$(PR_ID)/head:pr-$(PR_ID)
git checkout pr-$(PR_ID)
git push $(FORK_REPO) pr-$(PR_ID)

# Clean up the local PR branch
clean:
@if [ -z "$(PR_ID)" ]; then \
echo "Error: PR_ID is not set. Usage: make clean PR_ID=<PR_ID>"; \
exit 1; \
fi
git branch -d pr-$(PR_ID)
6 changes: 6 additions & 0 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 @@ -128,6 +128,7 @@
"fernet": "^0.4.0",
"file-saver": "^2.0.5",
"flat": "^6.0.1",
"fzstd": "^0.1.1",
"geodesy": "1.1.3",
"highlight.js": "^11.9.0",
"ieee754": "^1.2.1",
Expand Down
3 changes: 2 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@
"LZMA Compress",
"LZ4 Decompress",
"LZ4 Compress",
"LZNT1 Decompress"
"LZNT1 Decompress",
"ZStandard Decompress"
]
},
{
Expand Down
56 changes: 56 additions & 0 deletions src/core/operations/ZStandardDecode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @author Scarjit [[email protected]]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/

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

/**
* ZStandard Decode operation
*/
class ZStandardDecode extends Operation {

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

this.name = "ZStandard Decode";
this.module = "Compression";
this.description = "Zstandard is a lossless data compression algorithm designed for fast compression and decompression. It was developed by Facebook.";
this.infoURL = "https://wikipedia.org/wiki/Zstd"; // Usually a Wikipedia link. Remember to remove localisation (i.e. https://wikipedia.org/etc rather than https://en.wikipedia.org/etc)
this.inputType = "ArrayBuffer";
this.outputType = "ArrayBuffer";
this.args = [
/* Example arguments. See the project wiki for full details.
{
name: "First arg",
type: "string",
value: "Don't Panic"
},
{
name: "Second arg",
type: "number",
value: 42
}
*/
];
}

/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {ArrayBuffer}
*/
run(input, args) {
// const [firstArg, secondArg] = args;

throw new OperationError("Test");
}

}

export default ZStandardDecode;
91 changes: 91 additions & 0 deletions src/core/operations/ZStandardDecompress.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @author Scarjit [[email protected]]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import {isWorkerEnvironment} from "../Utils.mjs";
import * as fzstd from "fzstd";

/**
* ZStandard Decompress operation
*/
class ZStandardDecompress extends Operation {

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

this.name = "ZStandard Decompress";
this.module = "Compress";
this.description = "ZStandard is a compression algorithm focused on fast decompression.";
this.infoURL = "https://wikipedia.org/wiki/Zstd"; // Usually a Wikipedia link. Remember to remove localisation (i.e. https://wikipedia.org/etc rather than https://en.wikipedia.org/etc)
this.inputType = "ArrayBuffer";
this.outputType = "ArrayBuffer";
this.args = [
{
"name": "Chunk Size (bytes)",
"type": "number",
"value": 65536
}
];
}

/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {ArrayBuffer}
*/
run(input, args) {
const chunkSize = args[0];
if (input.byteLength <= 0) {
throw new OperationError("Please provide an input.");
}
// Validate input starts with ZStandard magic number
const ZSTD_MAGIC_NUMBER = [0x28, 0xb5, 0x2f, 0xfd];
const magicNumber = new Uint8Array(input, 0, 4);
if (!ZSTD_MAGIC_NUMBER.every((val, index) => val === magicNumber[index])) {
throw new OperationError("Invalid ZStandard input: does not start with magic number.");
}


if (isWorkerEnvironment()) self.sendStatusMessage("Loading ZStandard...");
return new Promise((resolve, reject) => {
const compressed = new Uint8Array(input);
try {
const outChunks = []; // Array of Uint8Array chunks
const stream = new fzstd.Decompress((chunk, isLast) => {
// Add to the list of output chunks
outChunks.push(chunk);
if (isLast) {
// Combine all chunks into a single Uint8Array
const totalLength = outChunks.reduce((sum, chunk) => sum + chunk.length, 0);
const result = new Uint8Array(totalLength);
let offset = 0;
for (const chunk of outChunks) {
result.set(chunk, offset);
offset += chunk.length;
}
resolve(result.buffer);
}
});
const chunks = Math.ceil(compressed.length / chunkSize);
for (let i = 0; i < compressed.length; i += chunkSize) {
if (isWorkerEnvironment()) self.sendStatusMessage(`Decompressing chunk ${i / chunkSize + 1} of ${chunks}...`);
const chunk = compressed.subarray(i, i + chunkSize);
stream.push(chunk);
}
stream.push(new Uint8Array(0), true); // Signal end of stream
} catch (error) {
reject(new OperationError("Decompression failed: " + error.message));
}
});
}

}

export default ZStandardDecompress;
15 changes: 15 additions & 0 deletions tests/operations/tests/Compress.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,19 @@ TestRegister.addTests([
}
],
},
{
name: "ZStandard Decompress",
input: "KLUv/QRYuQAAVGhlIGNhdCBzYXQgb24gdGhlIG1hdC4tJ481",
expectedOutput: "The cat sat on the mat.",
recipeConfig: [
{
"op": "From Base64",
"args": []
},
{
"op": "ZStandard Decompress",
"args": [65536]
}
],
}
]);
Loading