mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-25 09:16:17 -04:00
Add operation
Added 'Take nth bytes' operation.
This commit is contained in:
parent
60f5093c6c
commit
ce6d38860d
2 changed files with 80 additions and 1 deletions
|
@ -238,7 +238,8 @@
|
|||
"Escape string",
|
||||
"Unescape string",
|
||||
"Pseudo-Random Number Generator",
|
||||
"Sleep"
|
||||
"Sleep",
|
||||
"Take nth bytes"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
78
src/core/operations/TakeNthBytes.mjs
Normal file
78
src/core/operations/TakeNthBytes.mjs
Normal file
|
@ -0,0 +1,78 @@
|
|||
/**
|
||||
* @author Oshawk [oshawk@protonmail.com]
|
||||
* @copyright Crown Copyright 2019
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* Take nth bytes operation
|
||||
*/
|
||||
class TakeNthBytes extends Operation {
|
||||
|
||||
/**
|
||||
* TakeNthBytes constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Take nth bytes";
|
||||
this.module = "Default";
|
||||
this.description = "Takes every nth byte starting with a given byte.";
|
||||
this.infoURL = "";
|
||||
this.inputType = "byteArray";
|
||||
this.outputType = "byteArray";
|
||||
this.args = [
|
||||
{
|
||||
name: "Take 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) {
|
||||
let n = args[0];
|
||||
let start = args[1];
|
||||
let eachLine = args[2];
|
||||
|
||||
if (parseInt(n) !== n || n <= 0) {
|
||||
throw new OperationError("'Take every' must be a positive integer.")
|
||||
}
|
||||
if (parseInt(start) !== start || start < 0) {
|
||||
throw new OperationError("'Starting at' must be a positive or zero integer.")
|
||||
}
|
||||
|
||||
let offset = 0;
|
||||
let output = [];
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
if (eachLine && input[i] == 0x0a) {
|
||||
offset = i + 1;
|
||||
} else if (i - offset >= start && (i - (start + offset)) % n == 0) {
|
||||
output.push(input[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default TakeNthBytes;
|
Loading…
Add table
Add a link
Reference in a new issue