CyberChef/src/core/operations/UnescapeUnicodeCharacters.mjs

75 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

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 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;