CyberChef/src/core/operations/CharEnc.js

98 lines
2.1 KiB
JavaScript
Raw Normal View History

import cptable from "../lib/codepage.js";
import Utils from "../Utils.js";
import CryptoJS from "crypto-js";
2016-11-28 11:42:58 +01:00
/**
* Character encoding operations.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*/
const CharEnc = {
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
IO_FORMAT: ["UTF8", "UTF16", "UTF16LE", "UTF16BE", "Latin1", "Windows-1251", "Hex", "Base64"],
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* Text encoding operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run: function(input, args) {
var inputFormat = args[0],
outputFormat = args[1];
2017-02-09 16:09:33 +01:00
if (inputFormat === "Windows-1251") {
input = Utils.win1251ToUnicode(input);
2016-11-28 11:42:58 +01:00
input = CryptoJS.enc.Utf8.parse(input);
} else {
input = Utils.format[inputFormat].parse(input);
2016-11-28 11:42:58 +01:00
}
2017-02-09 16:09:33 +01:00
if (outputFormat === "Windows-1251") {
2016-11-28 11:42:58 +01:00
input = CryptoJS.enc.Utf8.stringify(input);
return Utils.unicodeToWin1251(input);
2016-11-28 11:42:58 +01:00
} else {
return Utils.format[outputFormat].stringify(input);
2016-11-28 11:42:58 +01:00
}
},
2017-02-09 16:09:33 +01:00
/**
*
* @author tlwr [toby@toby.codes]
*
* @constant
* @default
*/
EBCDIC_CODEPAGES_MAPPING: {
"IBM EBCDIC International": 500,
"IBM EBCDIC US-Canada": 37,
},
/**
* To EBCDIC operation.
*
* @author tlwr [toby@toby.codes]
*
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
runToEBCDIC: function(input, args) {
let pageNum = CharEnc.EBCDIC_CODEPAGES_MAPPING[args[0]];
let output = cptable.utils.encode(pageNum, input);
return Array.from(output);
},
/**
* From EBCDIC operation.
*
* @author tlwr [toby@toby.codes]
*
* @param {byteArray} input
* @param {Object[]} args
* @returns {string}
*/
runFromEBCDIC: function(input, args) {
let pageNum = CharEnc.EBCDIC_CODEPAGES_MAPPING[args[0]];
let output = cptable.utils.decode(pageNum, input);
return output;
},
2016-11-28 11:42:58 +01:00
};
export default CharEnc;