mirror of
https://github.com/gchq/CyberChef
synced 2025-01-04 00:38:41 +00:00
improve "Reverse" operation
* Make "Character" option actually reverse characters * Add new option "Byte" that behaves as previous "Character" option
This commit is contained in:
parent
ed8bd34915
commit
3700780d14
1 changed files with 21 additions and 1 deletions
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
|
import Utils from "../Utils.mjs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverse operation
|
* Reverse operation
|
||||||
|
@ -26,7 +27,8 @@ class Reverse extends Operation {
|
||||||
{
|
{
|
||||||
"name": "By",
|
"name": "By",
|
||||||
"type": "option",
|
"type": "option",
|
||||||
"value": ["Character", "Line"]
|
"value": ["Byte", "Character", "Line"],
|
||||||
|
"defaultIndex": 1
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -57,6 +59,24 @@ class Reverse extends Operation {
|
||||||
result.push(0x0a);
|
result.push(0x0a);
|
||||||
}
|
}
|
||||||
return result.slice(0, input.length);
|
return result.slice(0, input.length);
|
||||||
|
} else if (args[0] === "Character") {
|
||||||
|
const inputString = Utils.byteArrayToUtf8(input);
|
||||||
|
let result = "";
|
||||||
|
for (let i = inputString.length - 1; i >= 0; i--) {
|
||||||
|
const c = inputString.charCodeAt(i);
|
||||||
|
if (i > 0 && 0xdc00 <= c && c <= 0xdfff) {
|
||||||
|
const c2 = inputString.charCodeAt(i - 1);
|
||||||
|
if (0xd800 <= c2 && c2 <= 0xdbff) {
|
||||||
|
// surrogates
|
||||||
|
result += inputString.charAt(i - 1);
|
||||||
|
result += inputString.charAt(i);
|
||||||
|
i--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result += inputString.charAt(i);
|
||||||
|
}
|
||||||
|
return Utils.strToUtf8ByteArray(result);
|
||||||
} else {
|
} else {
|
||||||
return input.reverse();
|
return input.reverse();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue