mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-26 09:46:17 -04:00
80 lines
1.9 KiB
JavaScript
80 lines
1.9 KiB
JavaScript
![]() |
/**
|
||
|
* @author Oshawk [oshawk@protonmail.com]
|
||
|
* @copyright Crown Copyright 2019
|
||
|
* @license Apache-2.0
|
||
|
*/
|
||
|
|
||
|
import Operation from "../Operation.mjs";
|
||
|
import OperationError from "../errors/OperationError.mjs";
|
||
|
|
||
|
/**
|
||
|
* Drop nth bytes operation
|
||
|
*/
|
||
|
class DropNthBytes extends Operation {
|
||
|
|
||
|
/**
|
||
|
* DropNthBytes constructor
|
||
|
*/
|
||
|
constructor() {
|
||
|
super();
|
||
|
|
||
|
this.name = "Drop nth bytes";
|
||
|
this.module = "Default";
|
||
|
this.description = "Drops every nth byte starting with a given byte.";
|
||
|
this.infoURL = "";
|
||
|
this.inputType = "byteArray";
|
||
|
this.outputType = "byteArray";
|
||
|
this.args = [
|
||
|
{
|
||
|
name: "Drop every",
|
||
|
type: "number",
|
||
|
value: 4
|
||
|
},
|
||
|
{
|
||
|
name: "Starting at",
|
||
|
type: "number",
|
||
|
value: 0
|
||
|
},
|
||
|
{
|
||
|
name: "Apply to each line",
|
||
|
type: "boolean",
|
||
|
value: false
|
||
|
}
|
||
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param {byteArray} input
|
||
|
* @param {Object[]} args
|
||
|
* @returns {byteArray}
|
||
|
*/
|
||
|
run(input, args) {
|
||
|
const n = args[0];
|
||
|
const start = args[1];
|
||
|
const eachLine = args[2];
|
||
|
|
||
|
if (parseInt(n, 10) !== n || n <= 0) {
|
||
|
throw new OperationError("'Drop every' must be a positive integer.");
|
||
|
}
|
||
|
if (parseInt(start, 10) !== start || start < 0) {
|
||
|
throw new OperationError("'Starting at' must be a positive or zero integer.");
|
||
|
}
|
||
|
|
||
|
let offset = 0;
|
||
|
const output = [];
|
||
|
for (let i = 0; i < input.length; i++) {
|
||
|
if (eachLine && input[i] === 0x0a) {
|
||
|
output.push(0x0a);
|
||
|
offset = i + 1;
|
||
|
} else if (i - offset < start || (i - (start + offset)) % n !== 0) {
|
||
|
output.push(input[i]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return output;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
export default DropNthBytes;
|