mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-09 07:45:00 -04:00
issue#419
This commit is contained in:
parent
ae1b12c120
commit
46d40e961d
5 changed files with 171 additions and 1 deletions
BIN
doc/EscapeSmartCharacters.xlsx
Normal file
BIN
doc/EscapeSmartCharacters.xlsx
Normal file
Binary file not shown.
|
@ -63,7 +63,8 @@
|
||||||
"JSON to CSV",
|
"JSON to CSV",
|
||||||
"Avro to JSON",
|
"Avro to JSON",
|
||||||
"CBOR Encode",
|
"CBOR Encode",
|
||||||
"CBOR Decode"
|
"CBOR Decode",
|
||||||
|
"Escape Smart Characters"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
146
src/core/operations/EscapeSmartCharacters.mjs
Normal file
146
src/core/operations/EscapeSmartCharacters.mjs
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
/**
|
||||||
|
* @author john19696 [john19696@protonmail.com]
|
||||||
|
* @copyright Crown Copyright 2021
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Operation from "../Operation.mjs";
|
||||||
|
// import OperationError from "../errors/OperationError.mjs";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escape Smart Characters operation
|
||||||
|
*/
|
||||||
|
class EscapeSmartCharacters extends Operation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EscapeSmartCharacters constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = "Escape Smart Characters";
|
||||||
|
this.module = "Default";
|
||||||
|
this.description = "An operation to convert smart characters (quotes, dashes, apostrophes, \
|
||||||
|
arrows, copyright signs, ellipses etc.) back to plain ASCII.\
|
||||||
|
<br>";
|
||||||
|
this.infoURL = "http://unicode.scarfboy.com/?s=quotation+mark";
|
||||||
|
this.inputType = "string";
|
||||||
|
this.outputType = "string";
|
||||||
|
this.args = [
|
||||||
|
/* Arguments. See the project wiki for full details.*/
|
||||||
|
{
|
||||||
|
name: "Second arg",
|
||||||
|
type: "option",
|
||||||
|
value: ["Escape", "Remove", "Replace with '.'"]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} input
|
||||||
|
* @param {Object[]} args
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
run(input, args) {
|
||||||
|
const [outArg] = args;
|
||||||
|
|
||||||
|
/* JSON map of characters to escape
|
||||||
|
generated by EscapeSmartCharacters.xls in doc
|
||||||
|
*/
|
||||||
|
const ESCAPE_MAP = {
|
||||||
|
// Pasted from git:/doc/SmartEscape.xlsx
|
||||||
|
"": "'",
|
||||||
|
"": "'",
|
||||||
|
"": "\"",
|
||||||
|
"": "\"",
|
||||||
|
"©": "(C)",
|
||||||
|
"®": "(R)",
|
||||||
|
"™": "(TM)",
|
||||||
|
"→": "-->",
|
||||||
|
"←": "<--",
|
||||||
|
"↔": "<->",
|
||||||
|
"‐": "-",
|
||||||
|
"‑": "-",
|
||||||
|
"‒": "-",
|
||||||
|
"–": "-",
|
||||||
|
"—": "--",
|
||||||
|
"―": "--",
|
||||||
|
"‖": "||",
|
||||||
|
"‗": "==",
|
||||||
|
"‘": "'",
|
||||||
|
"’": "'",
|
||||||
|
"‚": "'",
|
||||||
|
"‛": "'",
|
||||||
|
"“": "\"",
|
||||||
|
"”": "\"",
|
||||||
|
"„": "\"",
|
||||||
|
"‟": "\"",
|
||||||
|
"•": ".",
|
||||||
|
"‣": ">",
|
||||||
|
"․": ".",
|
||||||
|
"‥": "..",
|
||||||
|
"…": "...",
|
||||||
|
"‧": ".",
|
||||||
|
"‰": "%0",
|
||||||
|
"‱": "%00",
|
||||||
|
"′": "'",
|
||||||
|
"″": "''",
|
||||||
|
"‴": "'''",
|
||||||
|
"‵": "''",
|
||||||
|
"‶": "''",
|
||||||
|
"‷": "'''",
|
||||||
|
"‸": "^",
|
||||||
|
"‹": "<",
|
||||||
|
"›": ">",
|
||||||
|
"‼": "!!",
|
||||||
|
"‽": "?!",
|
||||||
|
"⁃": "-",
|
||||||
|
"⁄": "/",
|
||||||
|
"⁅": "[-",
|
||||||
|
"⁆": "-]",
|
||||||
|
"⁇": "??",
|
||||||
|
"⁈": "?!",
|
||||||
|
"⁉": "!?",
|
||||||
|
"⁌": ".",
|
||||||
|
"⁍": ".",
|
||||||
|
"⁎": "*",
|
||||||
|
"⁏": ";",
|
||||||
|
"⁒": "%",
|
||||||
|
"⁓": "~",
|
||||||
|
"⁕": "*",
|
||||||
|
"⁗": "''''",
|
||||||
|
"": "*",
|
||||||
|
"": "+",
|
||||||
|
};
|
||||||
|
|
||||||
|
let output, result = "";
|
||||||
|
for (const char of input) {
|
||||||
|
if (Object.keys(ESCAPE_MAP).includes(char)) {
|
||||||
|
output = ESCAPE_MAP[char];
|
||||||
|
} else {
|
||||||
|
// Do Something Sensible with the rest of the arrows
|
||||||
|
const charCode = char.charCodeAt(0);
|
||||||
|
if (charCode >= 8592 && charCode <= 8703) {
|
||||||
|
output = "->";
|
||||||
|
} else {
|
||||||
|
output = char;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch (outArg) {
|
||||||
|
case "Remove":
|
||||||
|
break;
|
||||||
|
case "Replace with '.'":
|
||||||
|
result += ".";
|
||||||
|
break;
|
||||||
|
default: { // Escape & no change are the same
|
||||||
|
result += output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default EscapeSmartCharacters;
|
|
@ -107,6 +107,7 @@ import "./tests/CBORDecode.mjs";
|
||||||
import "./tests/JA3Fingerprint.mjs";
|
import "./tests/JA3Fingerprint.mjs";
|
||||||
import "./tests/JA3SFingerprint.mjs";
|
import "./tests/JA3SFingerprint.mjs";
|
||||||
import "./tests/HASSH.mjs";
|
import "./tests/HASSH.mjs";
|
||||||
|
import "./tests/EscapeSmartCharacters.mjs";
|
||||||
|
|
||||||
|
|
||||||
// Cannot test operations that use the File type yet
|
// Cannot test operations that use the File type yet
|
||||||
|
|
22
tests/operations/tests/EscapeSmartCharacters.mjs
Normal file
22
tests/operations/tests/EscapeSmartCharacters.mjs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
/**
|
||||||
|
* @author john19696 [john19696@protonmail.com]
|
||||||
|
* @copyright Crown Copyright 2021
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "Escape Smart Characters",
|
||||||
|
input: "“”—‘’ →©…",
|
||||||
|
expectedOutput: "\"\"--'' -->(C)...",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Escape Smart Characters",
|
||||||
|
args: ["Escape"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
g
|
Loading…
Add table
Add a link
Reference in a new issue