Decoder.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Utf8View.h>
  10. #include <LibTextCodec/Decoder.h>
  11. namespace TextCodec {
  12. namespace {
  13. Latin1Decoder s_latin1_decoder;
  14. UTF8Decoder s_utf8_decoder;
  15. UTF16BEDecoder s_utf16be_decoder;
  16. UTF16LEDecoder s_utf16le_decoder;
  17. Latin2Decoder s_latin2_decoder;
  18. HebrewDecoder s_hebrew_decoder;
  19. CyrillicDecoder s_cyrillic_decoder;
  20. Koi8RDecoder s_koi8r_decoder;
  21. Latin9Decoder s_latin9_decoder;
  22. TurkishDecoder s_turkish_decoder;
  23. XUserDefinedDecoder s_x_user_defined_decoder;
  24. }
  25. Decoder* decoder_for(String const& a_encoding)
  26. {
  27. auto encoding = get_standardized_encoding(a_encoding);
  28. if (encoding.has_value()) {
  29. if (encoding.value().equals_ignoring_case("windows-1252"))
  30. return &s_latin1_decoder;
  31. if (encoding.value().equals_ignoring_case("utf-8"))
  32. return &s_utf8_decoder;
  33. if (encoding.value().equals_ignoring_case("utf-16be"))
  34. return &s_utf16be_decoder;
  35. if (encoding.value().equals_ignoring_case("utf-16le"))
  36. return &s_utf16le_decoder;
  37. if (encoding.value().equals_ignoring_case("iso-8859-2"))
  38. return &s_latin2_decoder;
  39. if (encoding.value().equals_ignoring_case("windows-1255"))
  40. return &s_hebrew_decoder;
  41. if (encoding.value().equals_ignoring_case("windows-1251"))
  42. return &s_cyrillic_decoder;
  43. if (encoding.value().equals_ignoring_case("koi8-r"))
  44. return &s_koi8r_decoder;
  45. if (encoding.value().equals_ignoring_case("iso-8859-15"))
  46. return &s_latin9_decoder;
  47. if (encoding.value().equals_ignoring_case("windows-1254"))
  48. return &s_turkish_decoder;
  49. if (encoding.value().equals_ignoring_case("x-user-defined"))
  50. return &s_x_user_defined_decoder;
  51. }
  52. dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
  53. return nullptr;
  54. }
  55. // https://encoding.spec.whatwg.org/#concept-encoding-get
  56. Optional<StringView> get_standardized_encoding(StringView encoding)
  57. {
  58. encoding = encoding.trim_whitespace();
  59. if (encoding.is_one_of_ignoring_case("unicode-1-1-utf-8", "unicode11utf8", "unicode20utf8", "utf-8", "utf8", "x-unicode20utf8"))
  60. return "UTF-8";
  61. if (encoding.is_one_of_ignoring_case("866", "cp866", "csibm866", "ibm866"))
  62. return "IBM866";
  63. if (encoding.is_one_of_ignoring_case("csisolatin2", "iso-8859-2", "iso-ir-101", "iso8859-2", "iso88592", "iso_8859-2", "iso_8859-2:1987", "l2", "latin2"))
  64. return "ISO-8859-2";
  65. if (encoding.is_one_of_ignoring_case("csisolatin3", "iso-8859-3", "iso-ir-109", "iso8859-3", "iso88593", "iso_8859-3", "iso_8859-3:1988", "l3", "latin3"))
  66. return "ISO-8859-3";
  67. if (encoding.is_one_of_ignoring_case("csisolatin4", "iso-8859-4", "iso-ir-110", "iso8859-4", "iso88594", "iso_8859-4", "iso_8859-4:1989", "l4", "latin4"))
  68. return "ISO-8859-4";
  69. if (encoding.is_one_of_ignoring_case("csisolatincyrillic", "cyrillic", "iso-8859-5", "iso-ir-144", "iso8859-5", "iso88595", "iso_8859-5", "iso_8859-5:1988"))
  70. return "ISO-8859-5";
  71. if (encoding.is_one_of_ignoring_case("arabic", "asmo-708", "csiso88596e", "csiso88596i", "csisolatinarabic", "ecma-114", "iso-8859-6", "iso-8859-6-e", "iso-8859-6-i", "iso-ir-127", "iso8859-6", "iso88596", "iso_8859-6", "iso_8859-6:1987"))
  72. return "ISO-8859-6";
  73. if (encoding.is_one_of_ignoring_case("csisolatingreek", "ecma-118", "elot_928", "greek", "greek8", "iso-8859-7", "iso-ir-126", "iso8859-7", "iso88597", "iso_8859-7", "iso_8859-7:1987", "sun_eu_greek"))
  74. return "ISO-8859-7";
  75. if (encoding.is_one_of_ignoring_case("csiso88598e", "csisolatinhebrew", "hebrew", "iso-8859-8", "iso-8859-8-e", "iso-ir-138", "iso8859-8", "iso88598", "iso_8859-8", "iso_8859-8:1988", "visual"))
  76. return "ISO-8859-8";
  77. if (encoding.is_one_of_ignoring_case("csiso88598i", "iso-8859-8-i", "logical"))
  78. return "ISO-8859-8-I";
  79. if (encoding.is_one_of_ignoring_case("csisolatin6", "iso8859-10", "iso-ir-157", "iso8859-10", "iso885910", "l6", "latin6"))
  80. return "ISO-8859-10";
  81. if (encoding.is_one_of_ignoring_case("iso-8859-13", "iso8859-13", "iso885913"))
  82. return "ISO-8859-13";
  83. if (encoding.is_one_of_ignoring_case("iso-8859-14", "iso8859-14", "iso885914"))
  84. return "ISO-8859-14";
  85. if (encoding.is_one_of_ignoring_case("csisolatin9", "iso-8859-15", "iso8859-15", "iso885915", "iso_8859-15", "l9"))
  86. return "ISO-8859-15";
  87. if (encoding.is_one_of_ignoring_case("iso-8859-16"))
  88. return "ISO-8859-16";
  89. if (encoding.is_one_of_ignoring_case("cskoi8r", "koi", "koi8", "koi8-r", "koi8_r"))
  90. return "KOI8-R";
  91. if (encoding.is_one_of_ignoring_case("koi8-ru", "koi8-u"))
  92. return "KOI8-U";
  93. if (encoding.is_one_of_ignoring_case("csmacintosh", "mac", "macintosh", "x-mac-roman"))
  94. return "macintosh";
  95. if (encoding.is_one_of_ignoring_case("dos-874", "iso-8859-11", "iso8859-11", "iso885911", "tis-620", "windows-874"))
  96. return "windows-874";
  97. if (encoding.is_one_of_ignoring_case("cp1250", "windows-1250", "x-cp1250"))
  98. return "windows-1250";
  99. if (encoding.is_one_of_ignoring_case("cp1251", "windows-1251", "x-cp1251"))
  100. return "windows-1251";
  101. if (encoding.is_one_of_ignoring_case("ansi_x3.4-1968", "ascii", "cp1252", "cp819", "csisolatin1", "ibm819", "iso-8859-1", "iso-ir-100", "iso8859-1", "iso88591", "iso_8859-1", "iso_8859-1:1987", "l1", "latin1", "us-ascii", "windows-1252", "x-cp1252"))
  102. return "windows-1252";
  103. if (encoding.is_one_of_ignoring_case("cp1253", "windows-1253", "x-cp1253"))
  104. return "windows-1253";
  105. if (encoding.is_one_of_ignoring_case("cp1254", "csisolatin5", "iso-8859-9", "iso-ir-148", "iso-8859-9", "iso-88599", "iso_8859-9", "iso_8859-9:1989", "l5", "latin5", "windows-1254", "x-cp1254"))
  106. return "windows-1254";
  107. if (encoding.is_one_of_ignoring_case("cp1255", "windows-1255", "x-cp1255"))
  108. return "windows-1255";
  109. if (encoding.is_one_of_ignoring_case("cp1256", "windows-1256", "x-cp1256"))
  110. return "windows-1256";
  111. if (encoding.is_one_of_ignoring_case("cp1257", "windows-1257", "x-cp1257"))
  112. return "windows-1257";
  113. if (encoding.is_one_of_ignoring_case("cp1258", "windows-1258", "x-cp1258"))
  114. return "windows-1258";
  115. if (encoding.is_one_of_ignoring_case("x-mac-cyrillic", "x-mac-ukrainian"))
  116. return "x-mac-cyrillic";
  117. if (encoding.is_one_of_ignoring_case("koi8-r", "koi8r"))
  118. return "koi8-r";
  119. if (encoding.is_one_of_ignoring_case("chinese", "csgb2312", "csiso58gb231280", "gb2312", "gb_2312", "gb_2312-80", "gbk", "iso-ir-58", "x-gbk"))
  120. return "GBK";
  121. if (encoding.is_one_of_ignoring_case("gb18030"))
  122. return "gb18030";
  123. if (encoding.is_one_of_ignoring_case("big5", "big5-hkscs", "cn-big5", "csbig5", "x-x-big5"))
  124. return "Big5";
  125. if (encoding.is_one_of_ignoring_case("cseucpkdfmtjapanese", "euc-jp", "x-euc-jp"))
  126. return "EUC-JP";
  127. if (encoding.is_one_of_ignoring_case("csiso2022jp", "iso-2022-jp"))
  128. return "ISO-2022-JP";
  129. if (encoding.is_one_of_ignoring_case("csshiftjis", "ms932", "ms_kanji", "shift-jis", "shift_jis", "sjis", "windows-31j", "x-sjis"))
  130. return "Shift_JIS";
  131. if (encoding.is_one_of_ignoring_case("cseuckr", "csksc56011987", "euc-kr", "iso-ir-149", "korean", "ks_c_5601-1987", "ks_c_5601-1989", "ksc5601", "ksc_5601", "windows-949"))
  132. return "EUC-KR";
  133. if (encoding.is_one_of_ignoring_case("csiso2022kr", "hz-gb-2312", "iso-2022-cn", "iso-2022-cn-ext", "iso-2022-kr", "replacement"))
  134. return "replacement";
  135. if (encoding.is_one_of_ignoring_case("unicodefffe", "utf-16be"))
  136. return "UTF-16BE";
  137. if (encoding.is_one_of_ignoring_case("csunicode", "iso-10646-ucs-2", "ucs-2", "unicode", "unicodefeff", "utf-16", "utf-16le"))
  138. return "UTF-16LE";
  139. if (encoding.is_one_of_ignoring_case("x-user-defined"))
  140. return "x-user-defined";
  141. dbgln("TextCodec: Unrecognized encoding: {}", encoding);
  142. return {};
  143. }
  144. // https://encoding.spec.whatwg.org/#bom-sniff
  145. Decoder* bom_sniff_to_decoder(StringView input)
  146. {
  147. // 1. Let BOM be the result of peeking 3 bytes from ioQueue, converted to a byte sequence.
  148. // 2. For each of the rows in the table below, starting with the first one and going down,
  149. // if BOM starts with the bytes given in the first column, then return the encoding given
  150. // in the cell in the second column of that row. Otherwise, return null.
  151. // Byte Order Mark | Encoding
  152. // --------------------------
  153. // 0xEF 0xBB 0xBF | UTF-8
  154. // 0xFE 0xFF | UTF-16BE
  155. // 0xFF 0xFE | UTF-16LE
  156. auto bytes = input.bytes();
  157. if (bytes.size() < 2)
  158. return nullptr;
  159. auto first_byte = bytes[0];
  160. switch (first_byte) {
  161. case 0xEF: // UTF-8
  162. if (bytes.size() < 3)
  163. return nullptr;
  164. return bytes[1] == 0xBB && bytes[2] == 0xBF ? &s_utf8_decoder : nullptr;
  165. case 0xFE: // UTF-16BE
  166. return bytes[1] == 0xFF ? &s_utf16be_decoder : nullptr;
  167. case 0xFF: // UTF-16LE
  168. return bytes[1] == 0xFE ? &s_utf16le_decoder : nullptr;
  169. }
  170. return nullptr;
  171. }
  172. // https://encoding.spec.whatwg.org/#decode
  173. String convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder& fallback_decoder, StringView input)
  174. {
  175. Decoder* actual_decoder = &fallback_decoder;
  176. // 1. Let BOMEncoding be the result of BOM sniffing ioQueue.
  177. // 2. If BOMEncoding is non-null:
  178. if (auto* unicode_decoder = bom_sniff_to_decoder(input); unicode_decoder) {
  179. // 1. Set encoding to BOMEncoding.
  180. actual_decoder = unicode_decoder;
  181. // 2. Read three bytes from ioQueue, if BOMEncoding is UTF-8; otherwise read two bytes. (Do nothing with those bytes.)
  182. // FIXME: I imagine this will be pretty slow for large inputs, as it's regenerating the input without the first 2/3 bytes.
  183. input = input.substring_view(unicode_decoder == &s_utf8_decoder ? 3 : 2);
  184. }
  185. VERIFY(actual_decoder);
  186. // FIXME: 3. Process a queue with an instance of encoding’s decoder, ioQueue, output, and "replacement".
  187. // This isn't the exact same as the spec, especially the error mode of "replacement", which we don't have the concept of yet.
  188. // 4. Return output.
  189. return actual_decoder->to_utf8(input);
  190. }
  191. String Decoder::to_utf8(StringView input)
  192. {
  193. StringBuilder builder(input.length());
  194. process(input, [&builder](u32 c) { builder.append_code_point(c); });
  195. return builder.to_string();
  196. }
  197. void UTF8Decoder::process(StringView input, Function<void(u32)> on_code_point)
  198. {
  199. for (auto c : Utf8View(input)) {
  200. on_code_point(c);
  201. }
  202. }
  203. String UTF8Decoder::to_utf8(StringView input)
  204. {
  205. // Discard the BOM
  206. auto bomless_input = input;
  207. if (auto bytes = input.bytes(); bytes.size() >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
  208. bomless_input = input.substring_view(3);
  209. }
  210. return bomless_input;
  211. }
  212. void UTF16BEDecoder::process(StringView input, Function<void(u32)> on_code_point)
  213. {
  214. size_t utf16_length = input.length() - (input.length() % 2);
  215. for (size_t i = 0; i < utf16_length; i += 2) {
  216. u16 code_point = (input[i] << 8) | input[i + 1];
  217. on_code_point(code_point);
  218. }
  219. }
  220. String UTF16BEDecoder::to_utf8(StringView input)
  221. {
  222. // Discard the BOM
  223. auto bomless_input = input;
  224. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF)
  225. bomless_input = input.substring_view(2);
  226. StringBuilder builder(bomless_input.length() / 2);
  227. process(bomless_input, [&builder](u32 c) { builder.append_code_point(c); });
  228. return builder.to_string();
  229. }
  230. void UTF16LEDecoder::process(StringView input, Function<void(u32)> on_code_point)
  231. {
  232. size_t utf16_length = input.length() - (input.length() % 2);
  233. for (size_t i = 0; i < utf16_length; i += 2) {
  234. u16 code_point = input[i] | (input[i + 1] << 8);
  235. on_code_point(code_point);
  236. }
  237. }
  238. String UTF16LEDecoder::to_utf8(StringView input)
  239. {
  240. // Discard the BOM
  241. auto bomless_input = input;
  242. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE)
  243. bomless_input = input.substring_view(2);
  244. StringBuilder builder(bomless_input.length() / 2);
  245. process(bomless_input, [&builder](u32 c) { builder.append_code_point(c); });
  246. return builder.to_string();
  247. }
  248. void Latin1Decoder::process(StringView input, Function<void(u32)> on_code_point)
  249. {
  250. for (size_t i = 0; i < input.length(); ++i) {
  251. u8 ch = input[i];
  252. // Latin1 is the same as the first 256 Unicode code_points, so no mapping is needed, just utf-8 encoding.
  253. on_code_point(ch);
  254. }
  255. }
  256. namespace {
  257. u32 convert_latin2_to_utf8(u8 in)
  258. {
  259. switch (in) {
  260. #define MAP(X, Y) \
  261. case X: \
  262. return Y
  263. MAP(0xA1, 0x104);
  264. MAP(0xA2, 0x2D8);
  265. MAP(0xA3, 0x141);
  266. MAP(0xA5, 0x13D);
  267. MAP(0xA6, 0x15A);
  268. MAP(0xA9, 0x160);
  269. MAP(0xAA, 0x15E);
  270. MAP(0xAB, 0x164);
  271. MAP(0xAC, 0x179);
  272. MAP(0xAE, 0x17D);
  273. MAP(0xAF, 0x17B);
  274. MAP(0xB1, 0x105);
  275. MAP(0xB2, 0x2DB);
  276. MAP(0xB3, 0x142);
  277. MAP(0xB5, 0x13E);
  278. MAP(0xB6, 0x15B);
  279. MAP(0xB7, 0x2C7);
  280. MAP(0xB9, 0x161);
  281. MAP(0xBA, 0x15F);
  282. MAP(0xBB, 0x165);
  283. MAP(0xBC, 0x17A);
  284. MAP(0xBD, 0x2DD);
  285. MAP(0xBE, 0x17E);
  286. MAP(0xBF, 0x17C);
  287. MAP(0xC0, 0x154);
  288. MAP(0xC3, 0x102);
  289. MAP(0xC5, 0x139);
  290. MAP(0xC6, 0x106);
  291. MAP(0xC8, 0x10C);
  292. MAP(0xCA, 0x118);
  293. MAP(0xCC, 0x11A);
  294. MAP(0xCF, 0x10E);
  295. MAP(0xD0, 0x110);
  296. MAP(0xD1, 0x143);
  297. MAP(0xD2, 0x147);
  298. MAP(0xD5, 0x150);
  299. MAP(0xD8, 0x158);
  300. MAP(0xD9, 0x16E);
  301. MAP(0xDB, 0x170);
  302. MAP(0xDE, 0x162);
  303. MAP(0xE0, 0x155);
  304. MAP(0xE3, 0x103);
  305. MAP(0xE5, 0x13A);
  306. MAP(0xE6, 0x107);
  307. MAP(0xE8, 0x10D);
  308. MAP(0xEA, 0x119);
  309. MAP(0xEC, 0x11B);
  310. MAP(0xEF, 0x10F);
  311. MAP(0xF0, 0x111);
  312. MAP(0xF1, 0x144);
  313. MAP(0xF2, 0x148);
  314. MAP(0xF5, 0x151);
  315. MAP(0xF8, 0x159);
  316. MAP(0xF9, 0x16F);
  317. MAP(0xFB, 0x171);
  318. MAP(0xFE, 0x163);
  319. MAP(0xFF, 0x2D9);
  320. #undef MAP
  321. default:
  322. return in;
  323. }
  324. }
  325. }
  326. void Latin2Decoder::process(StringView input, Function<void(u32)> on_code_point)
  327. {
  328. for (auto c : input) {
  329. on_code_point(convert_latin2_to_utf8(c));
  330. }
  331. }
  332. void HebrewDecoder::process(StringView input, Function<void(u32)> on_code_point)
  333. {
  334. static constexpr Array<u32, 128> translation_table = {
  335. 0x20AC, 0xFFFD, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, 0x2021, 0x2C6, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  336. 0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x2DC, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  337. 0xA0, 0xA1, 0xA2, 0xA3, 0x20AA, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xD7, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
  338. 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xF7, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
  339. 0x5B0, 0x5B1, 0x5B2, 0x5B3, 0x5B4, 0x5B5, 0x5B6, 0x5B7, 0x5B8, 0x5B9, 0x5BA, 0x5BB, 0x5BC, 0x5BD, 0x5BE, 0x5BF,
  340. 0x5C0, 0x5C1, 0x5C2, 0x5C3, 0x5F0, 0x5F1, 0x5F2, 0x5F3, 0x5F4, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  341. 0x5D0, 0x5D1, 0x5D2, 0x5D3, 0x5D4, 0x5D5, 0x5D6, 0x5D7, 0x5D8, 0x5D9, 0x5DA, 0x5DB, 0x5DC, 0x5DD, 0x5DE, 0x5DF,
  342. 0x5E0, 0x5E1, 0x5E2, 0x5E3, 0x5E4, 0x5E5, 0x5E6, 0x5E7, 0x5E8, 0x5E9, 0x5EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD
  343. };
  344. for (unsigned char ch : input) {
  345. if (ch < 0x80) { // Superset of ASCII
  346. on_code_point(ch);
  347. } else {
  348. on_code_point(translation_table[ch - 0x80]);
  349. }
  350. }
  351. }
  352. void CyrillicDecoder::process(StringView input, Function<void(u32)> on_code_point)
  353. {
  354. static constexpr Array<u32, 128> translation_table = {
  355. 0x402, 0x403, 0x201A, 0x453, 0x201E, 0x2026, 0x2020, 0x2021, 0x20AC, 0x2030, 0x409, 0x2039, 0x40A, 0x40C, 0x40B, 0x40F,
  356. 0x452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0x459, 0x203A, 0x45A, 0x45C, 0x45B, 0x45F,
  357. 0xA0, 0x40E, 0x45E, 0x408, 0xA4, 0x490, 0xA6, 0xA7, 0x401, 0xA9, 0x404, 0xAB, 0xAC, 0xAD, 0xAE, 0x407,
  358. 0xB0, 0xB1, 0x406, 0x456, 0x491, 0xB5, 0xB6, 0xB7, 0x451, 0x2116, 0x454, 0xBB, 0x458, 0x405, 0x455, 0x457,
  359. 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, 0x416, 0x417, 0x418, 0x419, 0x41A, 0x41B, 0x41C, 0x41D, 0x41E, 0x41F,
  360. 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427, 0x428, 0x429, 0x42A, 0x42B, 0x42C, 0x42D, 0x42E, 0x42F,
  361. 0x430, 0x431, 0x432, 0x433, 0x434, 0x435, 0x436, 0x437, 0x438, 0x439, 0x43A, 0x43B, 0x43C, 0x43D, 0x43E, 0x43F,
  362. 0x440, 0x441, 0x442, 0x443, 0x444, 0x445, 0x446, 0x447, 0x448, 0x449, 0x44A, 0x44B, 0x44C, 0x44D, 0x44E, 0x44F
  363. };
  364. for (unsigned char ch : input) {
  365. if (ch < 0x80) { // Superset of ASCII
  366. on_code_point(ch);
  367. } else {
  368. on_code_point(translation_table[ch - 0x80]);
  369. }
  370. }
  371. }
  372. void Koi8RDecoder::process(StringView input, Function<void(u32)> on_code_point)
  373. {
  374. // clang-format off
  375. static constexpr Array<u32, 128> translation_table = {
  376. 0x2500,0x2502,0x250c,0x2510,0x2514,0x2518,0x251c,0x2524,0x252c,0x2534,0x253c,0x2580,0x2584,0x2588,0x258c,0x2590,
  377. 0x2591,0x2592,0x2593,0x2320,0x25a0,0x2219,0x221a,0x2248,0x2264,0x2265,0xA0,0x2321,0xb0,0xb2,0xb7,0xf7,
  378. 0x2550,0x2551,0x2552,0xd191,0x2553,0x2554,0x2555,0x2556,0x2557,0x2558,0x2559,0x255a,0x255b,0x255c,0x255d,0x255e,
  379. 0x255f,0x2560,0x2561,0xd081,0x2562,0x2563,0x2564,0x2565,0x2566,0x2567,0x2568,0x2569,0x256a,0x256b,0x256c,0xa9,
  380. 0x44e,0x430,0x431,0x446,0x434,0x435,0x444,0x433,0x445,0x438,0x439,0x43a,0x43b,0x43c,0x43d,0x43e,
  381. 0x43f,0x44f,0x440,0x441,0x442,0x443,0x436,0x432,0x44c,0x44b,0x437,0x448,0x44d,0x449,0x447,0x44a,
  382. 0x42e,0x410,0x441,0x426,0x414,0x415,0x424,0x413,0x425,0x418,0x419,0x41a,0x41b,0x41c,0x41d,0x41e,
  383. 0x41f,0x42f,0x420,0x421,0x422,0x423,0x416,0x412,0x42c,0x42b,0x417,0x428,0x42d,0x429,0x427,0x42a,
  384. };
  385. // clang-format on
  386. for (unsigned char ch : input) {
  387. if (ch < 0x80) { // Superset of ASCII
  388. on_code_point(ch);
  389. } else {
  390. on_code_point(translation_table[ch - 0x80]);
  391. }
  392. }
  393. }
  394. void Latin9Decoder::process(StringView input, Function<void(u32)> on_code_point)
  395. {
  396. auto convert_latin9_to_utf8 = [](u8 ch) -> u32 {
  397. // Latin9 is the same as the first 256 Unicode code points, except for 8 characters.
  398. switch (ch) {
  399. case 0xA4:
  400. return 0x20AC;
  401. case 0xA6:
  402. return 0x160;
  403. case 0xA8:
  404. return 0x161;
  405. case 0xB4:
  406. return 0x17D;
  407. case 0xB8:
  408. return 0x17E;
  409. case 0xBC:
  410. return 0x152;
  411. case 0xBD:
  412. return 0x153;
  413. case 0xBE:
  414. return 0x178;
  415. default:
  416. return ch;
  417. }
  418. };
  419. for (auto ch : input) {
  420. on_code_point(convert_latin9_to_utf8(ch));
  421. }
  422. }
  423. void TurkishDecoder::process(StringView input, Function<void(u32)> on_code_point)
  424. {
  425. auto convert_turkish_to_utf8 = [](u8 ch) -> u32 {
  426. // Turkish (aka ISO-8859-9, Windows-1254) is the same as the first 256 Unicode code points, except for 6 characters.
  427. switch (ch) {
  428. case 0xD0:
  429. return 0x11E;
  430. case 0xDD:
  431. return 0x130;
  432. case 0xDE:
  433. return 0x15E;
  434. case 0xF0:
  435. return 0x11F;
  436. case 0xFD:
  437. return 0x131;
  438. case 0xFE:
  439. return 0x15F;
  440. default:
  441. return ch;
  442. }
  443. };
  444. for (auto ch : input) {
  445. on_code_point(convert_turkish_to_utf8(ch));
  446. }
  447. }
  448. // https://encoding.spec.whatwg.org/#x-user-defined-decoder
  449. void XUserDefinedDecoder::process(StringView input, Function<void(u32)> on_code_point)
  450. {
  451. auto convert_x_user_defined_to_utf8 = [](u8 ch) -> u32 {
  452. // 2. If byte is an ASCII byte, return a code point whose value is byte.
  453. // https://infra.spec.whatwg.org/#ascii-byte
  454. // An ASCII byte is a byte in the range 0x00 (NUL) to 0x7F (DEL), inclusive.
  455. // NOTE: This doesn't check for ch >= 0x00, as that would always be true due to being unsigned.
  456. if (ch <= 0x7f)
  457. return ch;
  458. // 3. Return a code point whose value is 0xF780 + byte − 0x80.
  459. return 0xF780 + ch - 0x80;
  460. };
  461. for (auto ch : input) {
  462. on_code_point(convert_x_user_defined_to_utf8(ch));
  463. }
  464. // 1. If byte is end-of-queue, return finished.
  465. }
  466. }