Decoder.cpp 18 KB

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