mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-21 15:26:16 -04:00
Dish translation now obeys UTF8 rules
This commit is contained in:
parent
4588cd151c
commit
28c83fa921
2 changed files with 11 additions and 8 deletions
|
@ -85,8 +85,8 @@ Chef.prototype.bake = async function(input, recipeConfig, options, progress, ste
|
||||||
|
|
||||||
return {
|
return {
|
||||||
result: this.dish.type === Dish.HTML ?
|
result: this.dish.type === Dish.HTML ?
|
||||||
this.dish.get(Dish.HTML) :
|
this.dish.get(Dish.HTML, !options.treatAsUtf8) :
|
||||||
this.dish.get(returnType),
|
this.dish.get(returnType, !options.treatAsUtf8),
|
||||||
type: Dish.enumLookup(this.dish.type),
|
type: Dish.enumLookup(this.dish.type),
|
||||||
progress: progress,
|
progress: progress,
|
||||||
duration: new Date().getTime() - startTime,
|
duration: new Date().getTime() - startTime,
|
||||||
|
|
|
@ -136,11 +136,12 @@ Dish.prototype.set = function(value, type) {
|
||||||
* Returns the value of the data in the type format specified.
|
* Returns the value of the data in the type format specified.
|
||||||
*
|
*
|
||||||
* @param {number} type - The data type of value, see Dish enums.
|
* @param {number} type - The data type of value, see Dish enums.
|
||||||
|
* @param {boolean} [notUTF8] - Do not treat strings as UTF8.
|
||||||
* @returns {byteArray|string|number|ArrayBuffer|BigNumber} The value of the output data.
|
* @returns {byteArray|string|number|ArrayBuffer|BigNumber} The value of the output data.
|
||||||
*/
|
*/
|
||||||
Dish.prototype.get = function(type) {
|
Dish.prototype.get = function(type, notUTF8) {
|
||||||
if (this.type !== type) {
|
if (this.type !== type) {
|
||||||
this.translate(type);
|
this.translate(type, notUTF8);
|
||||||
}
|
}
|
||||||
return this.value;
|
return this.value;
|
||||||
};
|
};
|
||||||
|
@ -150,9 +151,11 @@ Dish.prototype.get = function(type) {
|
||||||
* Translates the data to the given type format.
|
* Translates the data to the given type format.
|
||||||
*
|
*
|
||||||
* @param {number} toType - The data type of value, see Dish enums.
|
* @param {number} toType - The data type of value, see Dish enums.
|
||||||
|
* @param {boolean} [notUTF8] - Do not treat strings as UTF8.
|
||||||
*/
|
*/
|
||||||
Dish.prototype.translate = function(toType) {
|
Dish.prototype.translate = function(toType, notUTF8) {
|
||||||
log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
|
log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
|
||||||
|
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
|
||||||
|
|
||||||
// Convert data to intermediate byteArray type
|
// Convert data to intermediate byteArray type
|
||||||
switch (this.type) {
|
switch (this.type) {
|
||||||
|
@ -182,11 +185,11 @@ Dish.prototype.translate = function(toType) {
|
||||||
switch (toType) {
|
switch (toType) {
|
||||||
case Dish.STRING:
|
case Dish.STRING:
|
||||||
case Dish.HTML:
|
case Dish.HTML:
|
||||||
this.value = this.value ? Utils.byteArrayToUtf8(this.value) : "";
|
this.value = this.value ? byteArrayToStr(this.value) : "";
|
||||||
this.type = Dish.STRING;
|
this.type = Dish.STRING;
|
||||||
break;
|
break;
|
||||||
case Dish.NUMBER:
|
case Dish.NUMBER:
|
||||||
this.value = this.value ? parseFloat(Utils.byteArrayToUtf8(this.value)) : 0;
|
this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0;
|
||||||
this.type = Dish.NUMBER;
|
this.type = Dish.NUMBER;
|
||||||
break;
|
break;
|
||||||
case Dish.ARRAY_BUFFER:
|
case Dish.ARRAY_BUFFER:
|
||||||
|
@ -195,7 +198,7 @@ Dish.prototype.translate = function(toType) {
|
||||||
break;
|
break;
|
||||||
case Dish.BIG_NUMBER:
|
case Dish.BIG_NUMBER:
|
||||||
try {
|
try {
|
||||||
this.value = new BigNumber(Utils.byteArrayToUtf8(this.value));
|
this.value = new BigNumber(byteArrayToStr(this.value));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.value = new BigNumber(NaN);
|
this.value = new BigNumber(NaN);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue