Decoder.cpp 18 KB

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