CyberChef/src/core/Dish.js

275 lines
7.5 KiB
JavaScript
Raw Normal View History

import Utils from "./Utils.js";
import BigNumber from "bignumber.js";
2016-11-28 10:42:58 +00:00
/**
* The data being operated on by each operation.
*
* @author n1474335 [n1474335@gmail.com]
* @author Matt C [matt@artemisbot.uk]
2016-11-28 10:42:58 +00:00
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @class
* @param {byteArray|string|number|ArrayBuffer|BigNumber} value - The value of the input data.
2016-11-28 10:42:58 +00:00
* @param {number} type - The data type of value, see Dish enums.
*/
2017-05-02 22:53:57 +01:00
const Dish = function(value, type) {
this.value = value || typeof value === "string" ? value : null;
2017-12-20 16:34:21 +00:00
this.type = type || Dish.BYTE_ARRAY;
2017-05-02 22:53:57 +01:00
};
2016-11-28 10:42:58 +00:00
/**
* Dish data type enum for byte arrays.
* @readonly
* @enum
*/
Dish.BYTE_ARRAY = 0;
/**
* Dish data type enum for strings.
* @readonly
* @enum
*/
Dish.STRING = 1;
/**
* Dish data type enum for numbers.
* @readonly
* @enum
*/
Dish.NUMBER = 2;
/**
* Dish data type enum for HTML.
* @readonly
* @enum
*/
Dish.HTML = 3;
/**
* Dish data type enum for ArrayBuffers.
* @readonly
* @enum
*/
Dish.ARRAY_BUFFER = 4;
/**
* Dish data type enum for BigNumbers.
* @readonly
* @enum
*/
Dish.BIG_NUMBER = 5;
2016-11-28 10:42:58 +00:00
/**
* Returns the data type enum for the given type string.
*
* @static
* @param {string} typeStr - The name of the data type.
2016-11-28 10:42:58 +00:00
* @returns {number} The data type enum value.
*/
Dish.typeEnum = function(typeStr) {
switch (typeStr.toLowerCase()) {
case "bytearray":
case "byte array":
2016-11-28 10:42:58 +00:00
return Dish.BYTE_ARRAY;
case "string":
return Dish.STRING;
case "number":
return Dish.NUMBER;
case "html":
return Dish.HTML;
case "arraybuffer":
case "array buffer":
return Dish.ARRAY_BUFFER;
case "bignumber":
case "big number":
return Dish.BIG_NUMBER;
2016-11-28 10:42:58 +00:00
default:
throw "Invalid data type string. No matching enum.";
}
};
/**
* Returns the data type string for the given type enum.
*
* @static
* @param {number} typeEnum - The enum value of the data type.
* @returns {string} The data type as a string.
2016-11-28 10:42:58 +00:00
*/
Dish.enumLookup = function(typeEnum) {
switch (typeEnum) {
2016-11-28 10:42:58 +00:00
case Dish.BYTE_ARRAY:
return "byteArray";
2016-11-28 10:42:58 +00:00
case Dish.STRING:
return "string";
case Dish.NUMBER:
return "number";
case Dish.HTML:
return "html";
case Dish.ARRAY_BUFFER:
return "ArrayBuffer";
case Dish.BIG_NUMBER:
return "BigNumber";
2016-11-28 10:42:58 +00:00
default:
throw "Invalid data type enum. No matching type.";
}
};
/**
* Sets the data value and type and then validates them.
*
* @param {byteArray|string|number|ArrayBuffer|BigNumber} value - The value of the input data.
2016-11-28 10:42:58 +00:00
* @param {number} type - The data type of value, see Dish enums.
*/
Dish.prototype.set = function(value, type) {
log.debug("Dish type: " + Dish.enumLookup(type));
2016-11-28 10:42:58 +00:00
this.value = value;
2017-12-20 16:34:21 +00:00
this.type = type;
2017-02-09 15:09:33 +00:00
2016-11-28 10:42:58 +00:00
if (!this.valid()) {
2017-04-13 18:08:50 +01:00
const sample = Utils.truncate(JSON.stringify(this.value), 13);
throw "Data is not a valid " + Dish.enumLookup(type) + ": " + sample;
2016-11-28 10:42:58 +00:00
}
};
/**
* Returns the value of the data in the type format specified.
*
* @param {number} type - The data type of value, see Dish enums.
2018-01-06 16:02:50 +00:00
* @param {boolean} [notUTF8] - Do not treat strings as UTF8.
* @returns {byteArray|string|number|ArrayBuffer|BigNumber} The value of the output data.
2016-11-28 10:42:58 +00:00
*/
2018-01-06 16:02:50 +00:00
Dish.prototype.get = function(type, notUTF8) {
2016-12-14 16:39:17 +00:00
if (this.type !== type) {
2018-01-06 16:02:50 +00:00
this.translate(type, notUTF8);
2016-11-28 10:42:58 +00:00
}
return this.value;
};
/**
* Translates the data to the given type format.
*
* @param {number} toType - The data type of value, see Dish enums.
2018-01-06 16:02:50 +00:00
* @param {boolean} [notUTF8] - Do not treat strings as UTF8.
2016-11-28 10:42:58 +00:00
*/
2018-01-06 16:02:50 +00:00
Dish.prototype.translate = function(toType, notUTF8) {
log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
2018-01-06 16:02:50 +00:00
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
// Convert data to intermediate byteArray type
2016-11-28 10:42:58 +00:00
switch (this.type) {
case Dish.STRING:
this.value = this.value ? Utils.strToByteArray(this.value) : [];
2016-11-28 10:42:58 +00:00
break;
case Dish.NUMBER:
this.value = typeof this.value == "number" ? Utils.strToByteArray(this.value.toString()) : [];
2016-11-28 10:42:58 +00:00
break;
case Dish.HTML:
this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : [];
break;
case Dish.ARRAY_BUFFER:
// Array.from() would be nicer here, but it's slightly slower
this.value = Array.prototype.slice.call(new Uint8Array(this.value));
2016-11-28 10:42:58 +00:00
break;
case Dish.BIG_NUMBER:
this.value = this.value instanceof BigNumber ? Utils.strToByteArray(this.value.toString()) : [];
break;
2016-11-28 10:42:58 +00:00
default:
break;
}
2017-02-09 15:09:33 +00:00
this.type = Dish.BYTE_ARRAY;
// Convert from byteArray to toType
switch (toType) {
2016-11-28 10:42:58 +00:00
case Dish.STRING:
case Dish.HTML:
2018-01-06 16:02:50 +00:00
this.value = this.value ? byteArrayToStr(this.value) : "";
2016-11-28 10:42:58 +00:00
this.type = Dish.STRING;
break;
case Dish.NUMBER:
2018-01-06 16:02:50 +00:00
this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0;
2016-11-28 10:42:58 +00:00
this.type = Dish.NUMBER;
break;
case Dish.ARRAY_BUFFER:
this.value = new Uint8Array(this.value).buffer;
this.type = Dish.ARRAY_BUFFER;
break;
case Dish.BIG_NUMBER:
try {
2018-01-06 16:02:50 +00:00
this.value = new BigNumber(byteArrayToStr(this.value));
} catch (err) {
this.value = new BigNumber(NaN);
}
this.type = Dish.BIG_NUMBER;
break;
2016-11-28 10:42:58 +00:00
default:
break;
}
};
/**
* Validates that the value is the type that has been specified.
* May have to disable parts of BYTE_ARRAY validation if it effects performance.
*
* @returns {boolean} Whether the data is valid or not.
*/
Dish.prototype.valid = function() {
switch (this.type) {
case Dish.BYTE_ARRAY:
if (!(this.value instanceof Array)) {
return false;
}
2017-02-09 15:09:33 +00:00
2016-11-28 10:42:58 +00:00
// Check that every value is a number between 0 - 255
2017-04-13 18:08:50 +01:00
for (let i = 0; i < this.value.length; i++) {
if (typeof this.value[i] !== "number" ||
2016-11-28 10:42:58 +00:00
this.value[i] < 0 ||
this.value[i] > 255) {
return false;
}
}
return true;
case Dish.STRING:
case Dish.HTML:
return typeof this.value === "string";
2016-11-28 10:42:58 +00:00
case Dish.NUMBER:
return typeof this.value === "number";
case Dish.ARRAY_BUFFER:
return this.value instanceof ArrayBuffer;
case Dish.BIG_NUMBER:
return this.value instanceof BigNumber;
2016-11-28 10:42:58 +00:00
default:
return false;
}
};
/**
* Determines how much space the Dish takes up.
* Numbers in JavaScript are 64-bit floating point, however for the purposes of the Dish,
* we measure how many bytes are taken up when the number is written as a string.
*
* @returns {number}
*/
Dish.prototype.size = function() {
switch (this.type) {
case Dish.BYTE_ARRAY:
case Dish.STRING:
case Dish.HTML:
return this.value.length;
case Dish.NUMBER:
case Dish.BIG_NUMBER:
return this.value.toString().length;
case Dish.ARRAY_BUFFER:
return this.value.byteLength;
default:
return -1;
}
};
export default Dish;