mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 23:06:16 -04:00
ESM: Ported HTML, Unicode, Quoted Printable and Endian operations
This commit is contained in:
parent
f26d175cad
commit
5362508a99
9 changed files with 1536 additions and 0 deletions
75
src/core/operations/UnescapeUnicodeCharacters.mjs
Normal file
75
src/core/operations/UnescapeUnicodeCharacters.mjs
Normal file
|
@ -0,0 +1,75 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Utils from "../Utils";
|
||||
|
||||
/**
|
||||
* Unescape Unicode Characters operation
|
||||
*/
|
||||
class UnescapeUnicodeCharacters extends Operation {
|
||||
|
||||
/**
|
||||
* UnescapeUnicodeCharacters constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Unescape Unicode Characters";
|
||||
this.module = "Default";
|
||||
this.description = "Converts unicode-escaped character notation back into raw characters.<br><br>Supports the prefixes:<ul><li><code>\\u</code></li><li><code>%u</code></li><li><code>U+</code></li></ul>e.g. <code>\\u03c3\\u03bf\\u03c5</code> becomes <code>σου</code>";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Prefix",
|
||||
"type": "option",
|
||||
"value": ["\\u", "%u", "U+"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const prefix = prefixToRegex[args[0]],
|
||||
regex = new RegExp(prefix+"([a-f\\d]{4})", "ig");
|
||||
let output = "",
|
||||
m,
|
||||
i = 0;
|
||||
|
||||
while ((m = regex.exec(input))) {
|
||||
// Add up to match
|
||||
output += input.slice(i, m.index);
|
||||
i = m.index;
|
||||
|
||||
// Add match
|
||||
output += Utils.chr(parseInt(m[1], 16));
|
||||
|
||||
i = regex.lastIndex;
|
||||
}
|
||||
|
||||
// Add all after final match
|
||||
output += input.slice(i, input.length);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup table to add prefixes to unicode delimiters so that they can be used in a regex.
|
||||
*/
|
||||
const prefixToRegex = {
|
||||
"\\u": "\\\\u",
|
||||
"%u": "%u",
|
||||
"U+": "U\\+"
|
||||
};
|
||||
|
||||
export default UnescapeUnicodeCharacters;
|
Loading…
Add table
Add a link
Reference in a new issue