Skip to content

Commit 2d99c36

Browse files
committed
Merge branch 'fix-reverse-character' of https://github.com/mikecat/CyberChef
2 parents 59d8be5 + 3700780 commit 2d99c36

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/core/operations/Reverse.mjs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import Operation from "../Operation.mjs";
8+
import Utils from "../Utils.mjs";
89

910
/**
1011
* Reverse operation
@@ -26,7 +27,8 @@ class Reverse extends Operation {
2627
{
2728
"name": "By",
2829
"type": "option",
29-
"value": ["Character", "Line"]
30+
"value": ["Byte", "Character", "Line"],
31+
"defaultIndex": 1
3032
}
3133
];
3234
}
@@ -57,6 +59,24 @@ class Reverse extends Operation {
5759
result.push(0x0a);
5860
}
5961
return result.slice(0, input.length);
62+
} else if (args[0] === "Character") {
63+
const inputString = Utils.byteArrayToUtf8(input);
64+
let result = "";
65+
for (let i = inputString.length - 1; i >= 0; i--) {
66+
const c = inputString.charCodeAt(i);
67+
if (i > 0 && 0xdc00 <= c && c <= 0xdfff) {
68+
const c2 = inputString.charCodeAt(i - 1);
69+
if (0xd800 <= c2 && c2 <= 0xdbff) {
70+
// surrogates
71+
result += inputString.charAt(i - 1);
72+
result += inputString.charAt(i);
73+
i--;
74+
continue;
75+
}
76+
}
77+
result += inputString.charAt(i);
78+
}
79+
return Utils.strToUtf8ByteArray(result);
6080
} else {
6181
return input.reverse();
6282
}

0 commit comments

Comments
 (0)