CyberChef/src/core/operations/Unicode.js

68 lines
1.3 KiB
JavaScript
Raw Normal View History

import Utils from "../Utils.js";
2016-11-28 11:42:58 +01:00
/**
* Unicode operations.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*/
const Unicode = {
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
PREFIXES: ["\\u", "%u", "U+"],
/**
* Unescape Unicode Characters operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runUnescape: function(input, args) {
2017-04-13 19:08:50 +02:00
let prefix = Unicode._prefixToRegex[args[0]],
2016-11-28 11:42:58 +01:00
regex = new RegExp(prefix+"([a-f\\d]{4,6})", "ig"),
output = "",
m,
i = 0;
2017-02-09 16:09:33 +01:00
2016-12-14 17:39:17 +01:00
while ((m = regex.exec(input))) {
2016-11-28 11:42:58 +01:00
// Add up to match
output += input.slice(i, m.index);
i = m.index;
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Add match
output += Utils.chr(parseInt(m[1], 16));
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
i = regex.lastIndex;
}
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Add all after final match
output += input.slice(i, input.length);
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
return output;
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* Lookup table to add prefixes to unicode delimiters so that they can be used in a regex.
*
* @private
* @constant
*/
_prefixToRegex: {
2016-11-28 11:42:58 +01:00
"\\u": "\\\\u",
"%u": "%u",
"U+": "U\\+"
},
};
export default Unicode;