Skip to content

Commit fc2828f

Browse files
committed
Added Magic operation with the ability to detect language, file type and some encoding types.
1 parent aa68904 commit fc2828f

File tree

7 files changed

+273
-1
lines changed

7 files changed

+273
-1
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
src/core/lib/**
2+
!src/core/lib/Magic.js
23
src/core/config/MetaConfig.js

src/core/FlowControl.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Recipe from "./Recipe.js";
22
import Dish from "./Dish.js";
3+
import Magic from "./lib/Magic.js";
34

45

56
/**
@@ -253,6 +254,29 @@ const FlowControl = {
253254
},
254255

255256

257+
/**
258+
* Magic operation.
259+
*
260+
* @param {Object} state - The current state of the recipe.
261+
* @param {number} state.progress - The current position in the recipe.
262+
* @param {Dish} state.dish - The Dish being operated on.
263+
* @param {Operation[]} state.opList - The list of operations in the recipe.
264+
* @returns {Object} The updated state of the recipe.
265+
*/
266+
runMagic: function(state) {
267+
const dish = state.dish;
268+
269+
const magic = new Magic(dish.get(Dish.ARRAY_BUFFER));
270+
271+
const output = JSON.stringify(magic.detectLanguage(), null, 2) + "\n\n" +
272+
JSON.stringify(magic.detectFileType(), null, 2) + "\n\n" +
273+
JSON.stringify(magic.findMatchingOps(), null, 2);
274+
275+
dish.set(output, Dish.STRING);
276+
return state;
277+
},
278+
279+
256280
/**
257281
* Returns the index of a label.
258282
*

src/core/config/Categories.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ const Categories = [
329329
{
330330
name: "Flow control",
331331
ops: [
332+
"Magic",
332333
"Fork",
333334
"Merge",
334335
"Register",

src/core/config/OperationConfig.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ import URL_ from "../operations/URL.js";
8181
* @type {Object.<string, OpConf>}
8282
*/
8383
const OperationConfig = {
84+
"Magic": {
85+
module: "Default",
86+
description: "Attempts to detect what the input data is and which operations could help to make more sense of it.",
87+
inputType: "ArrayBuffer",
88+
outputType: "string",
89+
flowControl: true,
90+
args: []
91+
},
8492
"Fork": {
8593
module: "Default",
8694
description: "Split the input data up based on the specified delimiter and run all subsequent operations on each branch separately.<br><br>For example, to decode multiple Base64 strings, enter them all on separate lines then add the 'Fork' and 'From Base64' operations to the recipe. Each string will be decoded separately.",
@@ -239,6 +247,13 @@ const OperationConfig = {
239247
type: "boolean",
240248
value: Base64.REMOVE_NON_ALPH_CHARS
241249
}
250+
],
251+
patterns: [
252+
{
253+
match: "^(?:[A-Z\\d+/]{4})+(?:[A-Z\\d+/]{2}==|[A-Z\\d+/]{3}=)?$",
254+
flags: "i",
255+
args: ["A-Za-z0-9+/=", false]
256+
}
242257
]
243258
},
244259
"To Base64": {
@@ -625,6 +640,53 @@ const OperationConfig = {
625640
type: "option",
626641
value: ByteRepr.HEX_DELIM_OPTIONS
627642
}
643+
],
644+
patterns: [
645+
{
646+
match: "^(?:[\\dA-F]{2})+$",
647+
flags: "i",
648+
args: ["None"]
649+
},
650+
{
651+
match: "^[\\dA-F]{2}(?: [\\dA-F]{2})*$",
652+
flags: "i",
653+
args: ["Space"]
654+
},
655+
{
656+
match: "^[\\dA-F]{2}(?:,[\\dA-F]{2})*$",
657+
flags: "i",
658+
args: ["Comma"]
659+
},
660+
{
661+
match: "^[\\dA-F]{2}(?:;[\\dA-F]{2})*$",
662+
flags: "i",
663+
args: ["Semi-colon"]
664+
},
665+
{
666+
match: "^[\\dA-F]{2}(?::[\\dA-F]{2})*$",
667+
flags: "i",
668+
args: ["Colon"]
669+
},
670+
{
671+
match: "^[\\dA-F]{2}(?:\\n[\\dA-F]{2})*$",
672+
flags: "i",
673+
args: ["Line feed"]
674+
},
675+
{
676+
match: "^[\\dA-F]{2}(?:\\r\\n[\\dA-F]{2})*$",
677+
flags: "i",
678+
args: ["CRLF"]
679+
},
680+
{
681+
match: "^[\\dA-F]{2}(?:0x[\\dA-F]{2})*$",
682+
flags: "i",
683+
args: ["0x"]
684+
},
685+
{
686+
match: "^[\\dA-F]{2}(?:\\\\x[\\dA-F]{2})*$",
687+
flags: "i",
688+
args: ["\\x"]
689+
}
628690
]
629691
},
630692
"To Hex": {

src/core/config/modules/Default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ OpModules.Default = {
140140
"Numberwang": Numberwang.run,
141141
"Generate TOTP": OTP.runTOTP,
142142
"Generate HOTP": OTP.runHOTP,
143+
"Magic": FlowControl.runMagic,
143144
"Fork": FlowControl.runFork,
144145
"Merge": FlowControl.runMerge,
145146
"Register": FlowControl.runRegister,

0 commit comments

Comments
 (0)