Decoder.cpp 26 KB

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