CyberChef/src/core/operations/EscapeSmartCharacters.mjs
2021-12-17 16:12:16 +00:00

150 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @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;
}
}
if (output == char) {
result += output;
} else {
switch (outArg) {
case "Remove":
break;
case "Replace with '.'":
result += ".";
break;
default: { // Escape & no change are the same
result += output;
}
}
}
}
return result;
}
}
export default EscapeSmartCharacters;