overlongUTF8

This tag converts all characters below or equal to 0x7ff into overflow UTF-8 sequences

Created by: hackvertor
Installed 1 times

Viewed: 619

Category: Charsets

Created on: Monday, December 9, 2024 at 12:36:41 PM

Updated on: Wednesday, January 21, 2026 at 6:58:01 PM

This is a built in tag
Tag arguments
[
   {
      "type": "number",
      "help": "Length of overlong sequence",
      "defaultValue": "6"
   },
   {
      "type": "boolean",
      "help": "Use raw characters or not",
      "defaultValue": "false"
   }
]
Code
class overlongUTF8 {
  encode(input, length, rawchars) {
    function overLongUTF8(chr, n, rawchars) {
      //thanks sdc for making a smaller version!
      if (n < 2) return escape(String.fromCharCode(chr));
      var chars = [
        String.fromCharCode(
          (0x100 - (1 << (8 - n))) |
            ((1 << (7 - n)) - 1 && chr >> (6 * (n - 1))),
        ),
      ];
      chr %= n < 7 ? 1 << (6 * (n - 1)) : Math.pow(2, 6 * (n - 1));
      for (var i = 1; i < n; i++) {
        chars.push(
          String.fromCharCode(0x80 | (63 & (chr >> (6 * (n - i - 1))))),
        );
        chr %= n < 7 ? 1 << (6 * (n - i - 1)) : Math.pow(2, 6 * (n - i - 1));
      }
      return rawchars ? chars.join("") : escape(chars.join(""));
    }
    var output = [];
    input = input.split("");
    for (var i = 0; i < input.length; i++) {
      output.push(overLongUTF8(input[i].charCodeAt(0), length, rawchars));
    }
    return output.join("");
  }
}