Decoder.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibTextCodec/Decoder.h>
  29. namespace TextCodec {
  30. namespace {
  31. Latin1Decoder& latin1_decoder()
  32. {
  33. static Latin1Decoder* decoder = nullptr;
  34. if (!decoder)
  35. decoder = new Latin1Decoder;
  36. return *decoder;
  37. }
  38. UTF8Decoder& utf8_decoder()
  39. {
  40. static UTF8Decoder* decoder = nullptr;
  41. if (!decoder)
  42. decoder = new UTF8Decoder;
  43. return *decoder;
  44. }
  45. UTF16BEDecoder& utf16be_decoder()
  46. {
  47. static UTF16BEDecoder* decoder = nullptr;
  48. if (!decoder)
  49. decoder = new UTF16BEDecoder;
  50. return *decoder;
  51. }
  52. Latin2Decoder& latin2_decoder()
  53. {
  54. static Latin2Decoder* decoder = nullptr;
  55. if (!decoder)
  56. decoder = new Latin2Decoder;
  57. return *decoder;
  58. }
  59. HebrewDecoder& hebrew_decoder()
  60. {
  61. static HebrewDecoder* decoder = nullptr;
  62. if (!decoder)
  63. decoder = new HebrewDecoder;
  64. return *decoder;
  65. }
  66. }
  67. Decoder* decoder_for(const String& a_encoding)
  68. {
  69. auto encoding = get_standardized_encoding(a_encoding);
  70. if (encoding.equals_ignoring_case("windows-1252"))
  71. return &latin1_decoder();
  72. if (encoding.equals_ignoring_case("utf-8"))
  73. return &utf8_decoder();
  74. if (encoding.equals_ignoring_case("utf-16be"))
  75. return &utf16be_decoder();
  76. if (encoding.equals_ignoring_case("iso-8859-2"))
  77. return &latin2_decoder();
  78. if (encoding.equals_ignoring_case("windows-1255"))
  79. return &hebrew_decoder();
  80. dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
  81. return nullptr;
  82. }
  83. // https://encoding.spec.whatwg.org/#concept-encoding-get
  84. String get_standardized_encoding(const String& encoding)
  85. {
  86. String trimmed_lowercase_encoding = encoding.trim_whitespace().to_lowercase();
  87. if (trimmed_lowercase_encoding.is_one_of("unicode-1-1-utf-8", "unicode11utf8", "unicode20utf8", "utf-8", "utf8", "x-unicode20utf8"))
  88. return "UTF-8";
  89. if (trimmed_lowercase_encoding.is_one_of("866", "cp866", "csibm866", "ibm866"))
  90. return "IBM866";
  91. 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"))
  92. return "ISO-8859-2";
  93. 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"))
  94. return "ISO-8859-3";
  95. 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"))
  96. return "ISO-8859-4";
  97. 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"))
  98. return "ISO-8859-5";
  99. 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"))
  100. return "ISO-8859-6";
  101. 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"))
  102. return "ISO-8859-7";
  103. 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"))
  104. return "ISO-8859-8";
  105. if (trimmed_lowercase_encoding.is_one_of("csiso88598i", "iso-8859-8-i", "logical"))
  106. return "ISO-8859-8-I";
  107. if (trimmed_lowercase_encoding.is_one_of("csisolatin6", "iso8859-10", "iso-ir-157", "iso8859-10", "iso885910", "l6", "latin6"))
  108. return "ISO-8859-10";
  109. if (trimmed_lowercase_encoding.is_one_of("iso-8859-13", "iso8859-13", "iso885913"))
  110. return "ISO-8859-13";
  111. if (trimmed_lowercase_encoding.is_one_of("iso-8859-14", "iso8859-14", "iso885914"))
  112. return "ISO-8859-14";
  113. if (trimmed_lowercase_encoding.is_one_of("csisolatin9", "iso-8859-15", "iso8859-15", "iso885915", "iso_8859-15", "l9"))
  114. return "ISO-8859-15";
  115. if (trimmed_lowercase_encoding == "iso-8859-16")
  116. return "ISO-8859-16";
  117. if (trimmed_lowercase_encoding.is_one_of("cskoi8r", "koi", "koi8", "koi8-r", "koi8_r"))
  118. return "KOI8-R";
  119. if (trimmed_lowercase_encoding.is_one_of("koi8-ru", "koi8-u"))
  120. return "KOI8-U";
  121. if (trimmed_lowercase_encoding.is_one_of("csmacintosh", "mac", "macintosh", "x-mac-roman"))
  122. return "macintosh";
  123. if (trimmed_lowercase_encoding.is_one_of("dos-874", "iso-8859-11", "iso8859-11", "iso885911", "tis-620", "windows-874"))
  124. return "windows-874";
  125. if (trimmed_lowercase_encoding.is_one_of("cp1250", "windows-1250", "x-cp1250"))
  126. return "windows-1250";
  127. if (trimmed_lowercase_encoding.is_one_of("cp1251", "windows-1251", "x-cp1251"))
  128. return "windows-1251";
  129. 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"))
  130. return "windows-1252";
  131. if (trimmed_lowercase_encoding.is_one_of("cp1253", "windows-1253", "x-cp1253"))
  132. return "windows-1253";
  133. 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"))
  134. return "windows-1254";
  135. if (trimmed_lowercase_encoding.is_one_of("cp1255", "windows-1255", "x-cp1255"))
  136. return "windows-1255";
  137. if (trimmed_lowercase_encoding.is_one_of("cp1256", "windows-1256", "x-cp1256"))
  138. return "windows-1256";
  139. if (trimmed_lowercase_encoding.is_one_of("cp1257", "windows-1257", "x-cp1257"))
  140. return "windows-1257";
  141. if (trimmed_lowercase_encoding.is_one_of("cp1258", "windows-1258", "x-cp1258"))
  142. return "windows-1258";
  143. if (trimmed_lowercase_encoding.is_one_of("x-mac-cyrillic", "x-mac-ukrainian"))
  144. return "x-mac-cyrillic";
  145. if (trimmed_lowercase_encoding.is_one_of("chinese", "csgb2312", "csiso58gb231280", "gb2312", "gb_2312", "gb_2312-80", "gbk", "iso-ir-58", "x-gbk"))
  146. return "GBK";
  147. if (trimmed_lowercase_encoding == "gb18030")
  148. return "gb18030";
  149. if (trimmed_lowercase_encoding.is_one_of("big5", "big5-hkscs", "cn-big5", "csbig5", "x-x-big5"))
  150. return "Big5";
  151. if (trimmed_lowercase_encoding.is_one_of("cseucpkdfmtjapanese", "euc-jp", "x-euc-jp"))
  152. return "EUC-JP";
  153. if (trimmed_lowercase_encoding.is_one_of("csiso2022jp", "iso-2022-jp"))
  154. return "ISO-2022-JP";
  155. if (trimmed_lowercase_encoding.is_one_of("csshiftjis", "ms932", "ms_kanji", "shift-jis", "shift_jis", "sjis", "windows-31j", "x-sjis"))
  156. return "Shift_JIS";
  157. 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"))
  158. return "EUC-KR";
  159. if (trimmed_lowercase_encoding.is_one_of("csiso2022kr", "hz-gb-2312", "iso-2022-cn", "iso-2022-cn-ext", "iso-2022-kr", "replacement"))
  160. return "replacement";
  161. if (trimmed_lowercase_encoding.is_one_of("unicodefffe", "utf-16be"))
  162. return "UTF-16BE";
  163. if (trimmed_lowercase_encoding.is_one_of("csunicode", "iso-10646-ucs-2", "ucs-2", "unicode", "unicodefeff", "utf-16", "utf-16le"))
  164. return "UTF-16LE";
  165. if (trimmed_lowercase_encoding == "x-user-defined")
  166. return "x-user-defined";
  167. dbgln("TextCodec: Unrecognized encoding: {}", encoding);
  168. return {};
  169. }
  170. bool is_standardized_encoding(const String& encoding)
  171. {
  172. return encoding.equals_ignoring_case(get_standardized_encoding(encoding));
  173. }
  174. String UTF8Decoder::to_utf8(const StringView& input)
  175. {
  176. return input;
  177. }
  178. String UTF16BEDecoder::to_utf8(const StringView& input)
  179. {
  180. StringBuilder builder(input.length() / 2);
  181. size_t utf16_length = input.length() - (input.length() % 2);
  182. for (size_t i = 0; i < utf16_length; i += 2) {
  183. u16 code_point = (input[i] << 8) | input[i + 1];
  184. builder.append_code_point(code_point);
  185. }
  186. return builder.to_string();
  187. }
  188. String Latin1Decoder::to_utf8(const StringView& input)
  189. {
  190. StringBuilder builder(input.length());
  191. for (size_t i = 0; i < input.length(); ++i) {
  192. u8 ch = input[i];
  193. // Latin1 is the same as the first 256 Unicode code_points, so no mapping is needed, just utf-8 encoding.
  194. builder.append_code_point(ch);
  195. }
  196. return builder.to_string();
  197. }
  198. namespace {
  199. u32 convert_latin2_to_utf8(u8 in)
  200. {
  201. switch (in) {
  202. #define MAP(X, Y) \
  203. case X: \
  204. return Y
  205. MAP(0xA1, 0x104);
  206. MAP(0xA2, 0x2D8);
  207. MAP(0xA3, 0x141);
  208. MAP(0xA5, 0x13D);
  209. MAP(0xA6, 0x15A);
  210. MAP(0xA9, 0x160);
  211. MAP(0xAA, 0x15E);
  212. MAP(0xAB, 0x164);
  213. MAP(0xAC, 0x179);
  214. MAP(0xAE, 0x17D);
  215. MAP(0xAF, 0x17B);
  216. MAP(0xB1, 0x105);
  217. MAP(0xB2, 0x2DB);
  218. MAP(0xB3, 0x142);
  219. MAP(0xB5, 0x13E);
  220. MAP(0xB6, 0x15B);
  221. MAP(0xB7, 0x2C7);
  222. MAP(0xB9, 0x161);
  223. MAP(0xBA, 0x15F);
  224. MAP(0xBB, 0x165);
  225. MAP(0xBC, 0x17A);
  226. MAP(0xBD, 0x2DD);
  227. MAP(0xBE, 0x17E);
  228. MAP(0xBF, 0x17C);
  229. MAP(0xC0, 0x154);
  230. MAP(0xC3, 0x102);
  231. MAP(0xC5, 0x139);
  232. MAP(0xC6, 0x106);
  233. MAP(0xC8, 0x10C);
  234. MAP(0xCA, 0x118);
  235. MAP(0xCC, 0x11A);
  236. MAP(0xCF, 0x10E);
  237. MAP(0xD0, 0x110);
  238. MAP(0xD1, 0x143);
  239. MAP(0xD2, 0x147);
  240. MAP(0xD5, 0x150);
  241. MAP(0xD8, 0x158);
  242. MAP(0xD9, 0x16E);
  243. MAP(0xDB, 0x170);
  244. MAP(0xDE, 0x162);
  245. MAP(0xE0, 0x155);
  246. MAP(0xE3, 0x103);
  247. MAP(0xE5, 0x13A);
  248. MAP(0xE6, 0x107);
  249. MAP(0xE8, 0x10D);
  250. MAP(0xEA, 0x119);
  251. MAP(0xEC, 0x11B);
  252. MAP(0xEF, 0x10F);
  253. MAP(0xF0, 0x111);
  254. MAP(0xF1, 0x144);
  255. MAP(0xF2, 0x148);
  256. MAP(0xF5, 0x151);
  257. MAP(0xF8, 0x159);
  258. MAP(0xF9, 0x16F);
  259. MAP(0xFB, 0x171);
  260. MAP(0xFE, 0x163);
  261. MAP(0xFF, 0x2D9);
  262. #undef MAP
  263. default:
  264. return in;
  265. }
  266. }
  267. }
  268. String Latin2Decoder::to_utf8(const StringView& input)
  269. {
  270. StringBuilder builder(input.length());
  271. for (auto c : input) {
  272. builder.append_code_point(convert_latin2_to_utf8(c));
  273. }
  274. return builder.to_string();
  275. }
  276. String HebrewDecoder::to_utf8(const StringView& input)
  277. {
  278. static constexpr Array<u32, 128> translation_table = {
  279. 0x20AC, 0xFFFD, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, 0x2021, 0x2C6, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  280. 0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x2DC, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  281. 0xA0, 0xA1, 0xA2, 0xA3, 0x20AA, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xD7, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
  282. 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xF7, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
  283. 0x5B0, 0x5B1, 0x5B2, 0x5B3, 0x5B4, 0x5B5, 0x5B6, 0x5B7, 0x5B8, 0x5B9, 0x5BA, 0x5BB, 0x5BC, 0x5BD, 0x5BE, 0x5BF,
  284. 0x5C0, 0x5C1, 0x5C2, 0x5C3, 0x5F0, 0x5F1, 0x5F2, 0x5F3, 0x5F4, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  285. 0x5D0, 0x5D1, 0x5D2, 0x5D3, 0x5D4, 0x5D5, 0x5D6, 0x5D7, 0x5D8, 0x5D9, 0x5DA, 0x5DB, 0x5DC, 0x5DD, 0x5DE, 0x5DF,
  286. 0x5E0, 0x5E1, 0x5E2, 0x5E3, 0x5E4, 0x5E5, 0x5E6, 0x5E7, 0x5E8, 0x5E9, 0x5EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD
  287. };
  288. StringBuilder builder(input.length());
  289. for (unsigned char ch : input) {
  290. if (ch < 0x80) { // Superset of ASCII
  291. builder.append(ch);
  292. } else {
  293. builder.append_code_point(translation_table[ch - 0x80]);
  294. }
  295. }
  296. return builder.to_string();
  297. }
  298. }