mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
add other flowcontrol ops. Update tests
This commit is contained in:
parent
046e1ebad9
commit
8ff6596657
15 changed files with 735 additions and 402 deletions
89
src/core/operations/ConditionalJump.mjs
Normal file
89
src/core/operations/ConditionalJump.mjs
Normal file
|
@ -0,0 +1,89 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Dish from "../Dish";
|
||||
import { getLabelIndex } from "../lib/FlowControl";
|
||||
|
||||
/**
|
||||
* Conditional Jump operation
|
||||
*/
|
||||
class ConditionalJump extends Operation {
|
||||
|
||||
/**
|
||||
* ConditionalJump constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Conditional Jump";
|
||||
this.flowControl = true;
|
||||
this.module = "Default";
|
||||
this.description = "Conditionally jump forwards or backwards to the specified Label based on whether the data matches the specified regular expression.";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Match (regex)",
|
||||
"type": "string",
|
||||
"value": ""
|
||||
},
|
||||
{
|
||||
"name": "Invert match",
|
||||
"type": "boolean",
|
||||
"value": false
|
||||
},
|
||||
{
|
||||
"name": "Label name",
|
||||
"type": "shortString",
|
||||
"value": ""
|
||||
},
|
||||
{
|
||||
"name": "Maximum jumps (if jumping backwards)",
|
||||
"type": "number",
|
||||
"value": 10
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Conditional Jump operation.
|
||||
*
|
||||
* @param {Object} state - The current state of the recipe.
|
||||
* @param {number} state.progress - The current position in the recipe.
|
||||
* @param {Dish} state.dish - The Dish being operated on.
|
||||
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
||||
* @param {number} state.numJumps - The number of jumps taken so far.
|
||||
* @returns {Object} The updated state of the recipe.
|
||||
*/
|
||||
async run(state) {
|
||||
const ings = state.opList[state.progress].ingValues,
|
||||
dish = state.dish,
|
||||
regexStr = ings[0],
|
||||
invert = ings[1],
|
||||
label = ings[2],
|
||||
maxJumps = ings[3],
|
||||
jmpIndex = getLabelIndex(label, state);
|
||||
|
||||
if (state.numJumps >= maxJumps || jmpIndex === -1) {
|
||||
return state;
|
||||
}
|
||||
|
||||
if (regexStr !== "") {
|
||||
const str = await dish.get(Dish.STRING);
|
||||
const strMatch = str.search(regexStr) > -1;
|
||||
if (!invert && strMatch || invert && !strMatch) {
|
||||
state.progress = jmpIndex;
|
||||
state.numJumps++;
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default ConditionalJump;
|
Loading…
Add table
Add a link
Reference in a new issue