CyberChef/src/core/operations/Subsection.mjs

162 lines
5.7 KiB
JavaScript
Raw Normal View History

2019-01-08 12:12:02 +01:00
/**
* @author j433866 [j433866@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import Recipe from "../Recipe.mjs";
import Dish from "../Dish.mjs";
2019-01-08 12:12:02 +01:00
/**
* Subsection operation
*/
class Subsection extends Operation {
/**
* Subsection constructor
*/
constructor() {
super();
this.name = "Subsection";
this.flowControl = true;
2019-07-09 14:42:42 +02:00
this.module = "Default";
2022-06-10 16:25:12 +02:00
this.description = "Select a part of the input data using a regular expression (regex), and run all subsequent operations on each match separately.<br><br>You can use up to one capture group, where the recipe will only be run on the data in the capture group. If there's more than one capture group, only the first one will be operated on.<br><br>Use the Merge operation to reset the effects of subsection.";
2019-01-08 12:12:02 +01:00
this.infoURL = "";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Section (regex)",
"type": "string",
"value": ""
},
{
"name": "Case sensitive matching",
"type": "boolean",
"value": true
},
{
"name": "Global matching",
"type": "boolean",
"value": true
},
2019-01-08 12:12:02 +01:00
{
"name": "Ignore errors",
"type": "boolean",
"value": false
}
];
}
/**
* @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
2019-01-08 12:12:02 +01:00
* @param {Operation[]} state.opList - The list of operations in the recipe
* @returns {Object} - The updated state of the recipe
*/
async run(state) {
const opList = state.opList,
inputType = opList[state.progress].inputType,
outputType = opList[state.progress].outputType,
input = await state.dish.get(inputType),
ings = opList[state.progress].ingValues,
[section, caseSensitive, global, ignoreErrors] = ings,
2019-01-08 12:12:02 +01:00
subOpList = [];
if (input && section !== "") {
2022-06-17 10:17:28 +02:00
// Set to 1 as if we are here, then there is one, the current one.
let numOp = 1;
2019-01-08 12:12:02 +01:00
// Create subOpList for each tranche to operate on
// all remaining operations unless we encounter a Merge
2019-01-08 12:12:02 +01:00
for (let i = state.progress + 1; i < opList.length; i++) {
if (opList[i].name === "Merge" && !opList[i].disabled) {
2022-06-17 10:17:28 +02:00
numOp--;
if (numOp === 0 || opList[i].ingValues[0])
break;
else
// Not this subsection's Merge.
subOpList.push(opList[i]);
2019-01-08 12:12:02 +01:00
} else {
2022-06-17 10:17:28 +02:00
if (opList[i].name === "Fork" || opList[i].name === "Subsection")
numOp++;
2019-01-08 12:12:02 +01:00
subOpList.push(opList[i]);
}
}
let flags = "",
2019-01-08 12:12:02 +01:00
inOffset = 0,
output = "",
m,
progress = 0;
if (!caseSensitive) flags += "i";
if (global) flags += "g";
2019-07-09 14:42:42 +02:00
const regex = new RegExp(section, flags),
2019-01-08 12:12:02 +01:00
recipe = new Recipe();
recipe.addOperations(subOpList);
state.forkOffset += state.progress + 1;
// Take a deep(ish) copy of the ingredient values
const ingValues = subOpList.map(op => JSON.parse(JSON.stringify(op.ingValues)));
let matched = false;
// Run recipe over each match
while ((m = regex.exec(input))) {
matched = true;
// Add up to match
let matchStr = m[0];
if (m.length === 1) { // No capture groups
2019-01-08 12:12:02 +01:00
output += input.slice(inOffset, m.index);
inOffset = m.index + m[0].length;
2019-01-08 12:12:02 +01:00
} else if (m.length >= 2) {
matchStr = m[1];
2019-01-08 12:12:02 +01:00
// Need to add some of the matched string that isn't in the capture group
output += input.slice(inOffset, m.index + m[0].indexOf(m[1]));
// Set i to be after the end of the first capture group
inOffset = m.index + m[0].indexOf(m[1]) + m[1].length;
2019-01-08 12:12:02 +01:00
}
2019-01-08 12:12:02 +01:00
// Baseline ing values for each tranche so that registers are reset
2019-03-07 17:33:38 +01:00
recipe.opList.forEach((op, i) => {
2019-01-08 12:12:02 +01:00
op.ingValues = JSON.parse(JSON.stringify(ingValues[i]));
});
const dish = new Dish();
dish.set(matchStr, inputType);
try {
progress = await recipe.execute(dish, 0, state);
} catch (err) {
if (!ignoreErrors) {
throw err;
}
progress = err.progress + 1;
}
output += await dish.get(outputType);
if (!regex.global) break;
2019-01-08 12:12:02 +01:00
}
2019-01-08 12:12:02 +01:00
// If no matches were found, advance progress to after a Merge op
// Otherwise, the operations below Subsection will be run on all the input data
if (!matched) {
state.progress += subOpList.length + 1;
}
output += input.slice(inOffset);
state.progress += progress;
state.dish.set(output, outputType);
}
return state;
}
}
export default Subsection;