Merge pull request #1915 from Oshawk/take-nth-bytes
Add 'Take nth bytes' operation
This commit is contained in:
commit
fed10f3ee6
@ -327,6 +327,7 @@
|
|||||||
"Pseudo-Random Number Generator",
|
"Pseudo-Random Number Generator",
|
||||||
"Sleep",
|
"Sleep",
|
||||||
"File Tree",
|
"File Tree",
|
||||||
|
"Take nth bytes",
|
||||||
"Drop nth bytes"
|
"Drop nth bytes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
79
src/core/operations/TakeNthBytes.mjs
Normal file
79
src/core/operations/TakeNthBytes.mjs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/**
|
||||||
|
* @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) {
|
||||||
|
const n = args[0];
|
||||||
|
const start = args[1];
|
||||||
|
const eachLine = args[2];
|
||||||
|
|
||||||
|
if (parseInt(n, 10) !== n || n <= 0) {
|
||||||
|
throw new OperationError("'Take 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 TakeNthBytes;
|
@ -150,6 +150,7 @@ import "./tests/StripUDPHeader.mjs";
|
|||||||
import "./tests/Subsection.mjs";
|
import "./tests/Subsection.mjs";
|
||||||
import "./tests/SwapCase.mjs";
|
import "./tests/SwapCase.mjs";
|
||||||
import "./tests/SymmetricDifference.mjs";
|
import "./tests/SymmetricDifference.mjs";
|
||||||
|
import "./tests/TakeNthBytes.mjs";
|
||||||
import "./tests/TextEncodingBruteForce.mjs";
|
import "./tests/TextEncodingBruteForce.mjs";
|
||||||
import "./tests/ToFromInsensitiveRegex.mjs";
|
import "./tests/ToFromInsensitiveRegex.mjs";
|
||||||
import "./tests/TranslateDateTimeFormat.mjs";
|
import "./tests/TranslateDateTimeFormat.mjs";
|
||||||
|
123
tests/operations/tests/TakeNthBytes.mjs
Normal file
123
tests/operations/tests/TakeNthBytes.mjs
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
/**
|
||||||
|
* @author Oshawk [oshawk@protonmail.com]
|
||||||
|
* @copyright Crown Copyright 2019
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Take nth bytes tests
|
||||||
|
*/
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Nothing",
|
||||||
|
input: "",
|
||||||
|
expectedOutput: "",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 0, false],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Nothing (apply to each line)",
|
||||||
|
input: "",
|
||||||
|
expectedOutput: "",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 0, true],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Basic single line",
|
||||||
|
input: "0123456789",
|
||||||
|
expectedOutput: "048",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 0, false],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Basic single line (apply to each line)",
|
||||||
|
input: "0123456789",
|
||||||
|
expectedOutput: "048",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 0, true],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Complex single line",
|
||||||
|
input: "0123456789",
|
||||||
|
expectedOutput: "59",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 5, false],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Complex single line (apply to each line)",
|
||||||
|
input: "0123456789",
|
||||||
|
expectedOutput: "59",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 5, true],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Basic multi line",
|
||||||
|
input: "01234\n56789",
|
||||||
|
expectedOutput: "047",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 0, false],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Basic multi line (apply to each line)",
|
||||||
|
input: "01234\n56789",
|
||||||
|
expectedOutput: "04\n59",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 0, true],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Complex multi line",
|
||||||
|
input: "01234\n56789",
|
||||||
|
expectedOutput: "\n8",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 5, false],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Take nth bytes: Complex multi line (apply to each line)",
|
||||||
|
input: "012345\n6789ab",
|
||||||
|
expectedOutput: "5\nb",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Take nth bytes",
|
||||||
|
args: [4, 5, true],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]);
|
Loading…
x
Reference in New Issue
Block a user