Decoder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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;
  34. if (!decoder)
  35. decoder = new Latin1Decoder;
  36. return *decoder;
  37. }
  38. UTF8Decoder& utf8_decoder()
  39. {
  40. static UTF8Decoder* decoder;
  41. if (!decoder)
  42. decoder = new UTF8Decoder;
  43. return *decoder;
  44. }
  45. Latin2Decoder& latin2_decoder()
  46. {
  47. static Latin2Decoder* decoder = nullptr;
  48. if (!decoder)
  49. decoder = new Latin2Decoder;
  50. return *decoder;
  51. }
  52. }
  53. Decoder* decoder_for(const String& a_encoding)
  54. {
  55. auto encoding = get_standardized_encoding(a_encoding);
  56. if (encoding.equals_ignoring_case("windows-1252"))
  57. return &latin1_decoder();
  58. if (encoding.equals_ignoring_case("utf-8"))
  59. return &utf8_decoder();
  60. if (encoding.equals_ignoring_case("iso-8859-2"))
  61. return &latin2_decoder();
  62. dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
  63. return nullptr;
  64. }
  65. // https://encoding.spec.whatwg.org/#concept-encoding-get
  66. String get_standardized_encoding(const String& encoding)
  67. {
  68. String trimmed_lowercase_encoding = encoding.trim_whitespace().to_lowercase();
  69. if (trimmed_lowercase_encoding.is_one_of("unicode-1-1-utf-8", "unicode11utf8", "unicode20utf8", "utf-8", "utf8", "x-unicode20utf8"))
  70. return "UTF-8";
  71. if (trimmed_lowercase_encoding.is_one_of("866", "cp866", "csibm866", "ibm666"))
  72. return "IBM666";
  73. 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"))
  74. return "ISO-8859-2";
  75. 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"))
  76. return "ISO-8859-3";
  77. 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"))
  78. return "ISO-8859-4";
  79. 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"))
  80. return "ISO-8859-5";
  81. 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"))
  82. return "ISO-8859-6";
  83. 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"))
  84. return "ISO-8859-7";
  85. 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"))
  86. return "ISO-8859-8";
  87. if (trimmed_lowercase_encoding.is_one_of("csiso88598i", "iso-8859-8-i", "logical"))
  88. return "ISO-8859-8-I";
  89. if (trimmed_lowercase_encoding.is_one_of("csisolatin6", "iso8859-10", "iso-ir-157", "iso8859-10", "iso885910", "l6", "latin6"))
  90. return "ISO-8859-10";
  91. if (trimmed_lowercase_encoding.is_one_of("iso-8859-13", "iso8859-13", "iso885913"))
  92. return "ISO-8859-13";
  93. if (trimmed_lowercase_encoding.is_one_of("iso-8859-14", "iso8859-14", "iso885914"))
  94. return "ISO-8859-14";
  95. if (trimmed_lowercase_encoding.is_one_of("csisolatin9", "iso-8859-15", "iso8859-15", "iso885915", "iso_8859-15", "l9"))
  96. return "ISO-8859-15";
  97. if (trimmed_lowercase_encoding == "iso-8859-16")
  98. return "ISO-8859-16";
  99. if (trimmed_lowercase_encoding.is_one_of("cskoi8r", "koi", "koi8", "koi8-r", "koi8_r"))
  100. return "KOI8-R";
  101. if (trimmed_lowercase_encoding.is_one_of("koi8-ru", "koi8-u"))
  102. return "KOI8-U";
  103. if (trimmed_lowercase_encoding.is_one_of("csmacintosh", "mac", "macintosh", "x-mac-roman"))
  104. return "macintosh";
  105. if (trimmed_lowercase_encoding.is_one_of("dos-874", "iso-8859-11", "iso8859-11", "iso885911", "tis-620", "windows-874"))
  106. return "windows-874";
  107. if (trimmed_lowercase_encoding.is_one_of("cp1250", "windows-1250", "x-cp1250"))
  108. return "windows-1250";
  109. if (trimmed_lowercase_encoding.is_one_of("cp1251", "windows-1251", "x-cp1251"))
  110. return "windows-1251";
  111. 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"))
  112. return "windows-1252";
  113. if (trimmed_lowercase_encoding.is_one_of("cp1253", "windows-1253", "x-cp1253"))
  114. return "windows-1253";
  115. 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"))
  116. return "windows-1254";
  117. if (trimmed_lowercase_encoding.is_one_of("cp1255", "windows-1255", "x-cp1255"))
  118. return "windows-1255";
  119. if (trimmed_lowercase_encoding.is_one_of("cp1256", "windows-1256", "x-cp1256"))
  120. return "windows-1256";
  121. if (trimmed_lowercase_encoding.is_one_of("cp1257", "windows-1257", "x-cp1257"))
  122. return "windows-1257";
  123. if (trimmed_lowercase_encoding.is_one_of("cp1258", "windows-1258", "x-cp1258"))
  124. return "windows-1258";
  125. if (trimmed_lowercase_encoding.is_one_of("x-mac-cyrillic", "x-mac-ukrainian"))
  126. return "x-mac-cyrillic";
  127. if (trimmed_lowercase_encoding.is_one_of("chinese", "csgb2312", "csiso58gb231280", "gb2312", "gb_2312", "gb_2312-80", "gbk", "iso-ir-58", "x-gbk"))
  128. return "GBK";
  129. if (trimmed_lowercase_encoding == "gb18030")
  130. return "gb18030";
  131. if (trimmed_lowercase_encoding.is_one_of("big5", "big5-hkscs", "cn-big5", "csbig5", "x-x-big5"))
  132. return "Big5";
  133. if (trimmed_lowercase_encoding.is_one_of("cseucpkdfmtjapanese", "euc-jp", "x-euc-jp"))
  134. return "EUC-JP";
  135. if (trimmed_lowercase_encoding.is_one_of("csiso2022jp", "iso-2022-jp"))
  136. return "ISO-2022-JP";
  137. if (trimmed_lowercase_encoding.is_one_of("csshiftjis", "ms932", "ms_kanji", "shift-jis", "shift_jis", "sjis", "windows-31j", "x-sjis"))
  138. return "Shift_JIS";
  139. 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"))
  140. return "EUC-KR";
  141. if (trimmed_lowercase_encoding.is_one_of("csiso2022kr", "hz-gb-2312", "iso-2022-cn", "iso-2022-cn-ext", "iso-2022-kr", "replacement"))
  142. return "replacement";
  143. if (trimmed_lowercase_encoding.is_one_of("unicodefffe", "utf-16be"))
  144. return "UTF-16BE";
  145. if (trimmed_lowercase_encoding.is_one_of("csunicode", "iso-10646-ucs-2", "ucs-2", "unicode", "unicodefeff", "utf-16", "utf-16le"))
  146. return "UTF-16LE";
  147. if (trimmed_lowercase_encoding == "x-user-defined")
  148. return "x-user-defined";
  149. dbgln("TextCodec: Unrecognized encoding: {}", encoding);
  150. return {};
  151. }
  152. bool is_standardized_encoding(const String& encoding)
  153. {
  154. auto lowercase_encoding = encoding.to_lowercase();
  155. return lowercase_encoding.is_one_of("utf-8", "ibm666", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-8-I", "iso-8859-10", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8-r", "koi8-u", "macintosh", "windows-874", "windows-1250", "windows-1251", "windows-1252", "windows-1253", "windows-1254", "windows-1255", "windows-1256", "windows-1257", "windows-1258", "x-mac-cyrillic", "gbk", "gb18030", "big5", "euc-jp", "iso-2022-JP", "shift_jis", "euc-kr", "replacement", "utf-16be", "utf-16le", "x-user-defined");
  156. }
  157. String UTF8Decoder::to_utf8(const StringView& input)
  158. {
  159. return input;
  160. }
  161. String Latin1Decoder::to_utf8(const StringView& input)
  162. {
  163. StringBuilder builder(input.length());
  164. for (size_t i = 0; i < input.length(); ++i) {
  165. u8 ch = input[i];
  166. // Latin1 is the same as the first 256 Unicode code_points, so no mapping is needed, just utf-8 encoding.
  167. builder.append_code_point(ch);
  168. }
  169. return builder.to_string();
  170. }
  171. namespace {
  172. u32 convert_latin2_to_utf8(u8 in)
  173. {
  174. switch (in) {
  175. #define MAP(X, Y) \
  176. case X: \
  177. return Y
  178. MAP(0xA1, 0x104);
  179. MAP(0xA2, 0x2D8);
  180. MAP(0xA3, 0x141);
  181. MAP(0xA5, 0x13D);
  182. MAP(0xA6, 0x15A);
  183. MAP(0xA9, 0x160);
  184. MAP(0xAA, 0x15E);
  185. MAP(0xAB, 0x164);
  186. MAP(0xAC, 0x179);
  187. MAP(0xAE, 0x17D);
  188. MAP(0xAF, 0x17B);
  189. MAP(0xB1, 0x105);
  190. MAP(0xB2, 0x2DB);
  191. MAP(0xB3, 0x142);
  192. MAP(0xB5, 0x13E);
  193. MAP(0xB6, 0x15B);
  194. MAP(0xB7, 0x2C7);
  195. MAP(0xB9, 0x161);
  196. MAP(0xBA, 0x15F);
  197. MAP(0xBB, 0x165);
  198. MAP(0xBC, 0x17A);
  199. MAP(0xBD, 0x2DD);
  200. MAP(0xBE, 0x17E);
  201. MAP(0xBF, 0x17C);
  202. MAP(0xC0, 0x154);
  203. MAP(0xC3, 0x102);
  204. MAP(0xC5, 0x139);
  205. MAP(0xC6, 0x106);
  206. MAP(0xC8, 0x10C);
  207. MAP(0xCA, 0x118);
  208. MAP(0xCC, 0x11A);
  209. MAP(0xCF, 0x10E);
  210. MAP(0xD0, 0x110);
  211. MAP(0xD1, 0x143);
  212. MAP(0xD2, 0x147);
  213. MAP(0xD5, 0x150);
  214. MAP(0xD8, 0x158);
  215. MAP(0xD9, 0x16E);
  216. MAP(0xDB, 0x170);
  217. MAP(0xDE, 0x162);
  218. MAP(0xE0, 0x155);
  219. MAP(0xE3, 0x103);
  220. MAP(0xE5, 0x13A);
  221. MAP(0xE6, 0x107);
  222. MAP(0xE8, 0x10D);
  223. MAP(0xEA, 0x119);
  224. MAP(0xEC, 0x11B);
  225. MAP(0xEF, 0x10F);
  226. MAP(0xF0, 0x111);
  227. MAP(0xF1, 0x144);
  228. MAP(0xF2, 0x148);
  229. MAP(0xF5, 0x151);
  230. MAP(0xF8, 0x159);
  231. MAP(0xF9, 0x16F);
  232. MAP(0xFB, 0x171);
  233. MAP(0xFE, 0x163);
  234. MAP(0xFF, 0x2D9);
  235. #undef MAP
  236. default:
  237. return in;
  238. }
  239. }
  240. }
  241. String Latin2Decoder::to_utf8(const StringView& input)
  242. {
  243. StringBuilder builder(input.length());
  244. for (auto c : input) {
  245. builder.append_code_point(convert_latin2_to_utf8(c));
  246. }
  247. return builder.to_string();
  248. }
  249. }