Browse Source

Merge branch 'control' of https://github.com/bwhitn/CyberChef into bwhitn-control

n1474335 7 years ago
parent
commit
b48e940f2d

+ 36 - 17
src/core/FlowControl.js

@@ -170,18 +170,14 @@ const FlowControl = {
      */
      */
     runJump: function(state) {
     runJump: function(state) {
         let ings     = state.opList[state.progress].getIngValues(),
         let ings     = state.opList[state.progress].getIngValues(),
-            jumpNum  = ings[0],
+            jmpIndex = FlowControl._getLabelIndex(ings[0], state),
             maxJumps = ings[1];
             maxJumps = ings[1];
 
 
-        if (jumpNum < 0) {
-            jumpNum--;
-        }
-
-        if (state.numJumps >= maxJumps) {
+        if (state.numJumps >= maxJumps || jmpIndex === -1) {
             return state;
             return state;
         }
         }
 
 
-        state.progress += jumpNum;
+        state.progress = jmpIndex;
         state.numJumps++;
         state.numJumps++;
         return state;
         return state;
     },
     },
@@ -201,25 +197,48 @@ const FlowControl = {
         let ings     = state.opList[state.progress].getIngValues(),
         let ings     = state.opList[state.progress].getIngValues(),
             dish     = state.dish,
             dish     = state.dish,
             regexStr = ings[0],
             regexStr = ings[0],
-            jumpNum  = ings[1],
-            maxJumps = ings[2];
-
-        if (jumpNum < 0) {
-            jumpNum--;
-        }
+            invert   = ings[1],
+            jmpIndex = FlowControl._getLabelIndex(ings[2], state),
+            maxJumps = ings[3];
 
 
-        if (state.numJumps >= maxJumps) {
+        if (state.numJumps >= maxJumps || jmpIndex === -1) {
             return state;
             return state;
         }
         }
 
 
-        if (regexStr !== "" && dish.get(Dish.STRING).search(regexStr) > -1) {
-            state.progress += jumpNum;
-            state.numJumps++;
+        if (regexStr !== "") {
+            let strMatch = dish.get(Dish.STRING).search(regexStr) > -1;
+            if (!invert && strMatch || invert && !strMatch) {
+                state.progress = jmpIndex;
+                state.numJumps++;
+            }
         }
         }
 
 
         return state;
         return state;
     },
     },
 
 
+    /**
+     * Returns the index of a label.
+     *
+     * @param {Object} state
+     * @param {string} name
+     * @returns {number}
+     */
+
+    _getLabelIndex: function(name, state) {
+        let index = -1;
+        for (let o = 0; o < state.opList.length; o++) {
+            let operation = state.opList[o];
+            if (operation.getConfig().op === "Label"){
+                let ings = operation.getIngValues();
+                if (name === ings[0]) {
+                    index = o;
+                    break;
+                }
+            }
+        }
+        return index;
+    },
+
 
 
     /**
     /**
      * Return operation.
      * Return operation.

+ 1 - 0
src/core/config/Categories.js

@@ -320,6 +320,7 @@ const Categories = [
             "Fork",
             "Fork",
             "Merge",
             "Merge",
             "Register",
             "Register",
+            "Label",
             "Jump",
             "Jump",
             "Conditional Jump",
             "Conditional Jump",
             "Return",
             "Return",

+ 25 - 6
src/core/config/OperationConfig.js

@@ -143,9 +143,9 @@ const OperationConfig = {
         flowControl: true,
         flowControl: true,
         args: [
         args: [
             {
             {
-                name: "Number of operations to jump over",
-                type: "number",
-                value: 0
+                name: "The Label to Jump to",
+                type: "string",
+                value: ""
             },
             },
             {
             {
                 name: "Maximum jumps (if jumping backwards)",
                 name: "Maximum jumps (if jumping backwards)",
@@ -167,9 +167,14 @@ const OperationConfig = {
                 value: ""
                 value: ""
             },
             },
             {
             {
-                name: "Number of operations to jump over if match found",
-                type: "number",
-                value: 0
+                name: "Negative match (logical NOT)",
+                type: "boolean",
+                value: false
+            },
+            {
+                name: "The Label to Jump to",
+                type: "string",
+                value: ""
             },
             },
             {
             {
                 name: "Maximum jumps (if jumping backwards)",
                 name: "Maximum jumps (if jumping backwards)",
@@ -178,6 +183,20 @@ const OperationConfig = {
             }
             }
         ]
         ]
     },
     },
+    "Label": {
+        module: "Default",
+        description: "Provides a location for for conditional and fixed jumps to jump.",
+        inputType: "string",
+        outputType: "string",
+        flowControl: true,
+        args: [
+            {
+                name: "Jump Label",
+                type: "string",
+                value: ""
+            }
+        ]
+    },
     "Return": {
     "Return": {
         module: "Default",
         module: "Default",
         description: "End execution of operations at this point in the recipe.",
         description: "End execution of operations at this point in the recipe.",

+ 1 - 0
src/core/config/modules/Default.js

@@ -151,6 +151,7 @@ OpModules.Default = {
     "Fork":                 FlowControl.runFork,
     "Fork":                 FlowControl.runFork,
     "Merge":                FlowControl.runMerge,
     "Merge":                FlowControl.runMerge,
     "Register":             FlowControl.runRegister,
     "Register":             FlowControl.runRegister,
+    "Label":                FlowControl.runComment,
     "Jump":                 FlowControl.runJump,
     "Jump":                 FlowControl.runJump,
     "Conditional Jump":     FlowControl.runCondJump,
     "Conditional Jump":     FlowControl.runCondJump,
     "Return":               FlowControl.runReturn,
     "Return":               FlowControl.runReturn,

+ 24 - 8
test/tests/operations/FlowControl.js

@@ -60,14 +60,15 @@ TestRegister.addTests([
         expectedOutput: "U29tZSBkYXRhIHdpdGggYSAxIGluIGl0\n53 6f 6d 65 20 64 61 74 61 20 77 69 74 68 20 61 20 32 20 69 6e 20 69 74\n",
         expectedOutput: "U29tZSBkYXRhIHdpdGggYSAxIGluIGl0\n53 6f 6d 65 20 64 61 74 61 20 77 69 74 68 20 61 20 32 20 69 6e 20 69 74\n",
         recipeConfig: [
         recipeConfig: [
             {"op": "Fork", "args": ["\\n", "\\n", false]},
             {"op": "Fork", "args": ["\\n", "\\n", false]},
-            {"op": "Conditional Jump", "args": ["1", "2", "10"]},
+            {"op": "Conditional Jump", "args": ["1", false, "skipReturn", "10"]},
             {"op": "To Hex", "args": ["Space"]},
             {"op": "To Hex", "args": ["Space"]},
             {"op": "Return", "args": []},
             {"op": "Return", "args": []},
+            {"op": "Label", "args": ["skipReturn"]},
             {"op": "To Base64", "args": ["A-Za-z0-9+/="]}
             {"op": "To Base64", "args": ["A-Za-z0-9+/="]}
         ]
         ]
     },
     },
     {
     {
-        name: "Jump: skips 0",
+        name: "Jump: Empty Label",
         input: [
         input: [
             "should be changed",
             "should be changed",
         ].join("\n"),
         ].join("\n"),
@@ -77,7 +78,7 @@ TestRegister.addTests([
         recipeConfig: [
         recipeConfig: [
             {
             {
                 op: "Jump",
                 op: "Jump",
-                args: [0, 10],
+                args: ["", 10],
             },
             },
             {
             {
                 op: "Find / Replace",
                 op: "Find / Replace",
@@ -105,7 +106,7 @@ TestRegister.addTests([
         recipeConfig: [
         recipeConfig: [
             {
             {
                 op: "Jump",
                 op: "Jump",
-                args: [1, 10],
+                args: ["skipReplace", 10],
             },
             },
             {
             {
                 op: "Find / Replace",
                 op: "Find / Replace",
@@ -120,6 +121,10 @@ TestRegister.addTests([
                     true,
                     true,
                 ],
                 ],
             },
             },
+            {
+                op: "Label",
+                args: ["skipReplace"]
+            },
         ],
         ],
     },
     },
     {
     {
@@ -137,7 +142,7 @@ TestRegister.addTests([
         recipeConfig: [
         recipeConfig: [
             {
             {
                 op: "Conditional Jump",
                 op: "Conditional Jump",
-                args: ["match", 0, 0],
+                args: ["match", false, "", 0],
             },
             },
             {
             {
                 op: "Find / Replace",
                 op: "Find / Replace",
@@ -212,7 +217,7 @@ TestRegister.addTests([
         recipeConfig: [
         recipeConfig: [
             {
             {
                 op: "Conditional Jump",
                 op: "Conditional Jump",
-                args: ["match", 1, 10],
+                args: ["match", false, "skip match", 10],
             },
             },
             {
             {
                 op: "Find / Replace",
                 op: "Find / Replace",
@@ -227,6 +232,9 @@ TestRegister.addTests([
                     true,
                     true,
                 ],
                 ],
             },
             },
+            {
+                op: "Label", args: ["skip match"],
+            },
             {
             {
                 op: "Find / Replace",
                 op: "Find / Replace",
                 args: [
                 args: [
@@ -251,9 +259,13 @@ TestRegister.addTests([
             "replaced",
             "replaced",
         ].join("\n"),
         ].join("\n"),
         recipeConfig: [
         recipeConfig: [
+            {
+                op: "Label",
+                args: ["back to the beginning"],
+            },
             {
             {
                 op: "Jump",
                 op: "Jump",
-                args: [1],
+                args: ["skip replace"],
             },
             },
             {
             {
                 op: "Find / Replace",
                 op: "Find / Replace",
@@ -268,9 +280,13 @@ TestRegister.addTests([
                     true,
                     true,
                 ],
                 ],
             },
             },
+            {
+                op: "Label",
+                args: ["skip replace"],
+            },
             {
             {
                 op: "Conditional Jump",
                 op: "Conditional Jump",
-                args: ["match", -2, 10],
+                args: ["match", false, "back to the beginning", 10],
             },
             },
         ],
         ],
     },
     },