Decoder.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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/StringBuilder.h>
  9. #include <AK/Utf16View.h>
  10. #include <AK/Utf8View.h>
  11. #include <LibTextCodec/Decoder.h>
  12. namespace TextCodec {
  13. static constexpr u32 replacement_code_point = 0xfffd;
  14. namespace {
  15. Latin1Decoder s_latin1_decoder;
  16. UTF8Decoder s_utf8_decoder;
  17. UTF16BEDecoder s_utf16be_decoder;
  18. UTF16LEDecoder s_utf16le_decoder;
  19. Latin2Decoder s_latin2_decoder;
  20. HebrewDecoder s_hebrew_decoder;
  21. CyrillicDecoder s_cyrillic_decoder;
  22. Koi8RDecoder s_koi8r_decoder;
  23. Latin9Decoder s_latin9_decoder;
  24. MacRomanDecoder s_mac_roman_decoder;
  25. PDFDocEncodingDecoder s_pdf_doc_encoding_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_ascii_case("windows-1252"sv))
  34. return s_latin1_decoder;
  35. if (encoding.value().equals_ignoring_ascii_case("utf-8"sv))
  36. return s_utf8_decoder;
  37. if (encoding.value().equals_ignoring_ascii_case("utf-16be"sv))
  38. return s_utf16be_decoder;
  39. if (encoding.value().equals_ignoring_ascii_case("utf-16le"sv))
  40. return s_utf16le_decoder;
  41. if (encoding.value().equals_ignoring_ascii_case("iso-8859-2"sv))
  42. return s_latin2_decoder;
  43. if (encoding.value().equals_ignoring_ascii_case("windows-1255"sv))
  44. return s_hebrew_decoder;
  45. if (encoding.value().equals_ignoring_ascii_case("windows-1251"sv))
  46. return s_cyrillic_decoder;
  47. if (encoding.value().equals_ignoring_ascii_case("koi8-r"sv))
  48. return s_koi8r_decoder;
  49. if (encoding.value().equals_ignoring_ascii_case("iso-8859-15"sv))
  50. return s_latin9_decoder;
  51. if (encoding.value().equals_ignoring_ascii_case("macintosh"sv))
  52. return s_mac_roman_decoder;
  53. if (encoding.value().equals_ignoring_ascii_case("PDFDocEncoding"sv))
  54. return s_pdf_doc_encoding_decoder;
  55. if (encoding.value().equals_ignoring_ascii_case("windows-1254"sv))
  56. return s_turkish_decoder;
  57. if (encoding.value().equals_ignoring_ascii_case("x-user-defined"sv))
  58. return s_x_user_defined_decoder;
  59. }
  60. dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
  61. return {};
  62. }
  63. // https://encoding.spec.whatwg.org/#concept-encoding-get
  64. Optional<StringView> get_standardized_encoding(StringView encoding)
  65. {
  66. encoding = encoding.trim_whitespace();
  67. if (encoding.is_one_of_ignoring_ascii_case("unicode-1-1-utf-8"sv, "unicode11utf8"sv, "unicode20utf8"sv, "utf-8"sv, "utf8"sv, "x-unicode20utf8"sv))
  68. return "UTF-8"sv;
  69. if (encoding.is_one_of_ignoring_ascii_case("866"sv, "cp866"sv, "csibm866"sv, "ibm866"sv))
  70. return "IBM866"sv;
  71. if (encoding.is_one_of_ignoring_ascii_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))
  72. return "ISO-8859-2"sv;
  73. if (encoding.is_one_of_ignoring_ascii_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))
  74. return "ISO-8859-3"sv;
  75. if (encoding.is_one_of_ignoring_ascii_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))
  76. return "ISO-8859-4"sv;
  77. if (encoding.is_one_of_ignoring_ascii_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))
  78. return "ISO-8859-5"sv;
  79. if (encoding.is_one_of_ignoring_ascii_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))
  80. return "ISO-8859-6"sv;
  81. if (encoding.is_one_of_ignoring_ascii_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))
  82. return "ISO-8859-7"sv;
  83. if (encoding.is_one_of_ignoring_ascii_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))
  84. return "ISO-8859-8"sv;
  85. if (encoding.is_one_of_ignoring_ascii_case("csiso88598i"sv, "iso-8859-8-i"sv, "logical"sv))
  86. return "ISO-8859-8-I"sv;
  87. if (encoding.is_one_of_ignoring_ascii_case("csisolatin6"sv, "iso8859-10"sv, "iso-ir-157"sv, "iso8859-10"sv, "iso885910"sv, "l6"sv, "latin6"sv))
  88. return "ISO-8859-10"sv;
  89. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-13"sv, "iso8859-13"sv, "iso885913"sv))
  90. return "ISO-8859-13"sv;
  91. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-14"sv, "iso8859-14"sv, "iso885914"sv))
  92. return "ISO-8859-14"sv;
  93. if (encoding.is_one_of_ignoring_ascii_case("csisolatin9"sv, "iso-8859-15"sv, "iso8859-15"sv, "iso885915"sv, "iso_8859-15"sv, "l9"sv))
  94. return "ISO-8859-15"sv;
  95. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-16"sv))
  96. return "ISO-8859-16"sv;
  97. if (encoding.is_one_of_ignoring_ascii_case("cskoi8r"sv, "koi"sv, "koi8"sv, "koi8-r"sv, "koi8_r"sv))
  98. return "KOI8-R"sv;
  99. if (encoding.is_one_of_ignoring_ascii_case("koi8-ru"sv, "koi8-u"sv))
  100. return "KOI8-U"sv;
  101. if (encoding.is_one_of_ignoring_ascii_case("csmacintosh"sv, "mac"sv, "macintosh"sv, "x-mac-roman"sv))
  102. return "macintosh"sv;
  103. if (encoding.is_one_of_ignoring_ascii_case("pdfdocencoding"sv))
  104. return "PDFDocEncoding"sv;
  105. if (encoding.is_one_of_ignoring_ascii_case("dos-874"sv, "iso-8859-11"sv, "iso8859-11"sv, "iso885911"sv, "tis-620"sv, "windows-874"sv))
  106. return "windows-874"sv;
  107. if (encoding.is_one_of_ignoring_ascii_case("cp1250"sv, "windows-1250"sv, "x-cp1250"sv))
  108. return "windows-1250"sv;
  109. if (encoding.is_one_of_ignoring_ascii_case("cp1251"sv, "windows-1251"sv, "x-cp1251"sv))
  110. return "windows-1251"sv;
  111. if (encoding.is_one_of_ignoring_ascii_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))
  112. return "windows-1252"sv;
  113. if (encoding.is_one_of_ignoring_ascii_case("cp1253"sv, "windows-1253"sv, "x-cp1253"sv))
  114. return "windows-1253"sv;
  115. if (encoding.is_one_of_ignoring_ascii_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))
  116. return "windows-1254"sv;
  117. if (encoding.is_one_of_ignoring_ascii_case("cp1255"sv, "windows-1255"sv, "x-cp1255"sv))
  118. return "windows-1255"sv;
  119. if (encoding.is_one_of_ignoring_ascii_case("cp1256"sv, "windows-1256"sv, "x-cp1256"sv))
  120. return "windows-1256"sv;
  121. if (encoding.is_one_of_ignoring_ascii_case("cp1257"sv, "windows-1257"sv, "x-cp1257"sv))
  122. return "windows-1257"sv;
  123. if (encoding.is_one_of_ignoring_ascii_case("cp1258"sv, "windows-1258"sv, "x-cp1258"sv))
  124. return "windows-1258"sv;
  125. if (encoding.is_one_of_ignoring_ascii_case("x-mac-cyrillic"sv, "x-mac-ukrainian"sv))
  126. return "x-mac-cyrillic"sv;
  127. if (encoding.is_one_of_ignoring_ascii_case("koi8-r"sv, "koi8r"sv))
  128. return "koi8-r"sv;
  129. if (encoding.is_one_of_ignoring_ascii_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))
  130. return "GBK"sv;
  131. if (encoding.is_one_of_ignoring_ascii_case("gb18030"sv))
  132. return "gb18030"sv;
  133. if (encoding.is_one_of_ignoring_ascii_case("big5"sv, "big5-hkscs"sv, "cn-big5"sv, "csbig5"sv, "x-x-big5"sv))
  134. return "Big5"sv;
  135. if (encoding.is_one_of_ignoring_ascii_case("cseucpkdfmtjapanese"sv, "euc-jp"sv, "x-euc-jp"sv))
  136. return "EUC-JP"sv;
  137. if (encoding.is_one_of_ignoring_ascii_case("csiso2022jp"sv, "iso-2022-jp"sv))
  138. return "ISO-2022-JP"sv;
  139. if (encoding.is_one_of_ignoring_ascii_case("csshiftjis"sv, "ms932"sv, "ms_kanji"sv, "shift-jis"sv, "shift_jis"sv, "sjis"sv, "windows-31j"sv, "x-sjis"sv))
  140. return "Shift_JIS"sv;
  141. if (encoding.is_one_of_ignoring_ascii_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))
  142. return "EUC-KR"sv;
  143. if (encoding.is_one_of_ignoring_ascii_case("csiso2022kr"sv, "hz-gb-2312"sv, "iso-2022-cn"sv, "iso-2022-cn-ext"sv, "iso-2022-kr"sv, "replacement"sv))
  144. return "replacement"sv;
  145. if (encoding.is_one_of_ignoring_ascii_case("unicodefffe"sv, "utf-16be"sv))
  146. return "UTF-16BE"sv;
  147. if (encoding.is_one_of_ignoring_ascii_case("csunicode"sv, "iso-10646-ucs-2"sv, "ucs-2"sv, "unicode"sv, "unicodefeff"sv, "utf-16"sv, "utf-16le"sv))
  148. return "UTF-16LE"sv;
  149. if (encoding.is_one_of_ignoring_ascii_case("x-user-defined"sv))
  150. return "x-user-defined"sv;
  151. dbgln("TextCodec: Unrecognized encoding: {}", encoding);
  152. return {};
  153. }
  154. // https://encoding.spec.whatwg.org/#bom-sniff
  155. Optional<Decoder&> bom_sniff_to_decoder(StringView input)
  156. {
  157. // 1. Let BOM be the result of peeking 3 bytes from ioQueue, converted to a byte sequence.
  158. // 2. For each of the rows in the table below, starting with the first one and going down,
  159. // if BOM starts with the bytes given in the first column, then return the encoding given
  160. // in the cell in the second column of that row. Otherwise, return null.
  161. // Byte Order Mark | Encoding
  162. // --------------------------
  163. // 0xEF 0xBB 0xBF | UTF-8
  164. // 0xFE 0xFF | UTF-16BE
  165. // 0xFF 0xFE | UTF-16LE
  166. auto bytes = input.bytes();
  167. if (bytes.size() < 2)
  168. return {};
  169. auto first_byte = bytes[0];
  170. switch (first_byte) {
  171. case 0xEF: // UTF-8
  172. if (bytes.size() < 3)
  173. return {};
  174. if (bytes[1] == 0xBB && bytes[2] == 0xBF)
  175. return s_utf8_decoder;
  176. return {};
  177. case 0xFE: // UTF-16BE
  178. if (bytes[1] == 0xFF)
  179. return s_utf16be_decoder;
  180. return {};
  181. case 0xFF: // UTF-16LE
  182. if (bytes[1] == 0xFE)
  183. return s_utf16le_decoder;
  184. return {};
  185. }
  186. return {};
  187. }
  188. // https://encoding.spec.whatwg.org/#decode
  189. ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder& fallback_decoder, StringView input)
  190. {
  191. Decoder* actual_decoder = &fallback_decoder;
  192. // 1. Let BOMEncoding be the result of BOM sniffing ioQueue.
  193. // 2. If BOMEncoding is non-null:
  194. if (auto unicode_decoder = bom_sniff_to_decoder(input); unicode_decoder.has_value()) {
  195. // 1. Set encoding to BOMEncoding.
  196. actual_decoder = &unicode_decoder.value();
  197. // 2. Read three bytes from ioQueue, if BOMEncoding is UTF-8; otherwise read two bytes. (Do nothing with those bytes.)
  198. // FIXME: I imagine this will be pretty slow for large inputs, as it's regenerating the input without the first 2/3 bytes.
  199. input = input.substring_view(&unicode_decoder.value() == &s_utf8_decoder ? 3 : 2);
  200. }
  201. VERIFY(actual_decoder);
  202. // 3. Process a queue with an instance of encoding’s decoder, ioQueue, output, and "replacement".
  203. // FIXME: This isn't the exact same as the spec, which is written in terms of I/O queues.
  204. auto output = TRY(actual_decoder->to_utf8(input));
  205. // 4. Return output.
  206. return output;
  207. }
  208. // https://encoding.spec.whatwg.org/#get-an-output-encoding
  209. StringView get_output_encoding(StringView encoding)
  210. {
  211. // 1. If encoding is replacement or UTF-16BE/LE, then return UTF-8.
  212. if (encoding.is_one_of_ignoring_ascii_case("replacement"sv, "utf-16le"sv, "utf-16be"sv))
  213. return "UTF-8"sv;
  214. // 2. Return encoding.
  215. return encoding;
  216. }
  217. bool Decoder::validate(StringView)
  218. {
  219. // By-default we assume that any input sequence is valid, character encodings that do not accept all inputs may override this
  220. return true;
  221. }
  222. ErrorOr<String> Decoder::to_utf8(StringView input)
  223. {
  224. StringBuilder builder(input.length());
  225. TRY(process(input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  226. return builder.to_string();
  227. }
  228. ErrorOr<void> UTF8Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  229. {
  230. for (auto c : Utf8View(input)) {
  231. TRY(on_code_point(c));
  232. }
  233. return {};
  234. }
  235. bool UTF8Decoder::validate(StringView input)
  236. {
  237. return Utf8View(input).validate();
  238. }
  239. ErrorOr<String> UTF8Decoder::to_utf8(StringView input)
  240. {
  241. // Discard the BOM
  242. auto bomless_input = input;
  243. if (auto bytes = input.bytes(); bytes.size() >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
  244. bomless_input = input.substring_view(3);
  245. }
  246. return Decoder::to_utf8(bomless_input);
  247. }
  248. ErrorOr<void> UTF16BEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  249. {
  250. // rfc2781, 2.2 Decoding UTF-16
  251. size_t utf16_length = input.length() - (input.length() % 2);
  252. for (size_t i = 0; i < utf16_length; i += 2) {
  253. // 1) If W1 < 0xD800 or W1 > 0xDFFF, the character value U is the value
  254. // of W1. Terminate.
  255. u16 w1 = (static_cast<u8>(input[i]) << 8) | static_cast<u8>(input[i + 1]);
  256. if (!is_unicode_surrogate(w1)) {
  257. TRY(on_code_point(w1));
  258. continue;
  259. }
  260. // 2) Determine if W1 is between 0xD800 and 0xDBFF. If not, the sequence
  261. // is in error and no valid character can be obtained using W1.
  262. // Terminate.
  263. // 3) If there is no W2 (that is, the sequence ends with W1), or if W2
  264. // is not between 0xDC00 and 0xDFFF, the sequence is in error.
  265. // Terminate.
  266. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length) {
  267. TRY(on_code_point(replacement_code_point));
  268. continue;
  269. }
  270. u16 w2 = (static_cast<u8>(input[i + 2]) << 8) | static_cast<u8>(input[i + 3]);
  271. if (!Utf16View::is_low_surrogate(w2)) {
  272. TRY(on_code_point(replacement_code_point));
  273. continue;
  274. }
  275. // 4) Construct a 20-bit unsigned integer U', taking the 10 low-order
  276. // bits of W1 as its 10 high-order bits and the 10 low-order bits of
  277. // W2 as its 10 low-order bits.
  278. // 5) Add 0x10000 to U' to obtain the character value U. Terminate.
  279. TRY(on_code_point(Utf16View::decode_surrogate_pair(w1, w2)));
  280. i += 2;
  281. }
  282. return {};
  283. }
  284. bool UTF16BEDecoder::validate(StringView input)
  285. {
  286. size_t utf16_length = input.length() - (input.length() % 2);
  287. for (size_t i = 0; i < utf16_length; i += 2) {
  288. u16 w1 = (static_cast<u8>(input[i]) << 8) | static_cast<u8>(input[i + 1]);
  289. if (!is_unicode_surrogate(w1))
  290. continue;
  291. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length)
  292. return false;
  293. u16 w2 = (static_cast<u8>(input[i + 2]) << 8) | static_cast<u8>(input[i + 3]);
  294. if (!Utf16View::is_low_surrogate(w2))
  295. return false;
  296. i += 2;
  297. }
  298. return true;
  299. }
  300. ErrorOr<String> UTF16BEDecoder::to_utf8(StringView input)
  301. {
  302. // Discard the BOM
  303. auto bomless_input = input;
  304. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF)
  305. bomless_input = input.substring_view(2);
  306. StringBuilder builder(bomless_input.length() / 2);
  307. TRY(process(bomless_input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  308. return builder.to_string();
  309. }
  310. ErrorOr<void> UTF16LEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  311. {
  312. // rfc2781, 2.2 Decoding UTF-16
  313. size_t utf16_length = input.length() - (input.length() % 2);
  314. for (size_t i = 0; i < utf16_length; i += 2) {
  315. // 1) If W1 < 0xD800 or W1 > 0xDFFF, the character value U is the value
  316. // of W1. Terminate.
  317. u16 w1 = static_cast<u8>(input[i]) | (static_cast<u8>(input[i + 1]) << 8);
  318. if (!is_unicode_surrogate(w1)) {
  319. TRY(on_code_point(w1));
  320. continue;
  321. }
  322. // 2) Determine if W1 is between 0xD800 and 0xDBFF. If not, the sequence
  323. // is in error and no valid character can be obtained using W1.
  324. // Terminate.
  325. // 3) If there is no W2 (that is, the sequence ends with W1), or if W2
  326. // is not between 0xDC00 and 0xDFFF, the sequence is in error.
  327. // Terminate.
  328. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length) {
  329. TRY(on_code_point(replacement_code_point));
  330. continue;
  331. }
  332. u16 w2 = static_cast<u8>(input[i + 2]) | (static_cast<u8>(input[i + 3]) << 8);
  333. if (!Utf16View::is_low_surrogate(w2)) {
  334. TRY(on_code_point(replacement_code_point));
  335. continue;
  336. }
  337. // 4) Construct a 20-bit unsigned integer U', taking the 10 low-order
  338. // bits of W1 as its 10 high-order bits and the 10 low-order bits of
  339. // W2 as its 10 low-order bits.
  340. // 5) Add 0x10000 to U' to obtain the character value U. Terminate.
  341. TRY(on_code_point(Utf16View::decode_surrogate_pair(w1, w2)));
  342. i += 2;
  343. }
  344. return {};
  345. }
  346. bool UTF16LEDecoder::validate(StringView input)
  347. {
  348. size_t utf16_length = input.length() - (input.length() % 2);
  349. for (size_t i = 0; i < utf16_length; i += 2) {
  350. u16 w1 = static_cast<u8>(input[i]) | (static_cast<u8>(input[i + 1]) << 8);
  351. if (!is_unicode_surrogate(w1))
  352. continue;
  353. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length)
  354. return false;
  355. u16 w2 = static_cast<u8>(input[i + 2]) | (static_cast<u8>(input[i + 3]) << 8);
  356. if (!Utf16View::is_low_surrogate(w2))
  357. return false;
  358. i += 2;
  359. }
  360. return true;
  361. }
  362. ErrorOr<String> UTF16LEDecoder::to_utf8(StringView input)
  363. {
  364. // Discard the BOM
  365. auto bomless_input = input;
  366. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE)
  367. bomless_input = input.substring_view(2);
  368. StringBuilder builder(bomless_input.length() / 2);
  369. TRY(process(bomless_input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  370. return builder.to_string();
  371. }
  372. ErrorOr<void> Latin1Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  373. {
  374. for (u8 ch : input) {
  375. // Latin1 is the same as the first 256 Unicode code_points, so no mapping is needed, just utf-8 encoding.
  376. TRY(on_code_point(ch));
  377. }
  378. return {};
  379. }
  380. namespace {
  381. u32 convert_latin2_to_utf8(u8 in)
  382. {
  383. switch (in) {
  384. #define MAP(X, Y) \
  385. case X: \
  386. return Y
  387. MAP(0xA1, 0x104);
  388. MAP(0xA2, 0x2D8);
  389. MAP(0xA3, 0x141);
  390. MAP(0xA5, 0x13D);
  391. MAP(0xA6, 0x15A);
  392. MAP(0xA9, 0x160);
  393. MAP(0xAA, 0x15E);
  394. MAP(0xAB, 0x164);
  395. MAP(0xAC, 0x179);
  396. MAP(0xAE, 0x17D);
  397. MAP(0xAF, 0x17B);
  398. MAP(0xB1, 0x105);
  399. MAP(0xB2, 0x2DB);
  400. MAP(0xB3, 0x142);
  401. MAP(0xB5, 0x13E);
  402. MAP(0xB6, 0x15B);
  403. MAP(0xB7, 0x2C7);
  404. MAP(0xB9, 0x161);
  405. MAP(0xBA, 0x15F);
  406. MAP(0xBB, 0x165);
  407. MAP(0xBC, 0x17A);
  408. MAP(0xBD, 0x2DD);
  409. MAP(0xBE, 0x17E);
  410. MAP(0xBF, 0x17C);
  411. MAP(0xC0, 0x154);
  412. MAP(0xC3, 0x102);
  413. MAP(0xC5, 0x139);
  414. MAP(0xC6, 0x106);
  415. MAP(0xC8, 0x10C);
  416. MAP(0xCA, 0x118);
  417. MAP(0xCC, 0x11A);
  418. MAP(0xCF, 0x10E);
  419. MAP(0xD0, 0x110);
  420. MAP(0xD1, 0x143);
  421. MAP(0xD2, 0x147);
  422. MAP(0xD5, 0x150);
  423. MAP(0xD8, 0x158);
  424. MAP(0xD9, 0x16E);
  425. MAP(0xDB, 0x170);
  426. MAP(0xDE, 0x162);
  427. MAP(0xE0, 0x155);
  428. MAP(0xE3, 0x103);
  429. MAP(0xE5, 0x13A);
  430. MAP(0xE6, 0x107);
  431. MAP(0xE8, 0x10D);
  432. MAP(0xEA, 0x119);
  433. MAP(0xEC, 0x11B);
  434. MAP(0xEF, 0x10F);
  435. MAP(0xF0, 0x111);
  436. MAP(0xF1, 0x144);
  437. MAP(0xF2, 0x148);
  438. MAP(0xF5, 0x151);
  439. MAP(0xF8, 0x159);
  440. MAP(0xF9, 0x16F);
  441. MAP(0xFB, 0x171);
  442. MAP(0xFE, 0x163);
  443. MAP(0xFF, 0x2D9);
  444. #undef MAP
  445. default:
  446. return in;
  447. }
  448. }
  449. }
  450. ErrorOr<void> Latin2Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  451. {
  452. for (auto c : input) {
  453. TRY(on_code_point(convert_latin2_to_utf8(c)));
  454. }
  455. return {};
  456. }
  457. ErrorOr<void> HebrewDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  458. {
  459. static constexpr Array<u32, 128> translation_table = {
  460. 0x20AC, 0xFFFD, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, 0x2021, 0x2C6, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  461. 0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x2DC, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  462. 0xA0, 0xA1, 0xA2, 0xA3, 0x20AA, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xD7, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
  463. 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xF7, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
  464. 0x5B0, 0x5B1, 0x5B2, 0x5B3, 0x5B4, 0x5B5, 0x5B6, 0x5B7, 0x5B8, 0x5B9, 0x5BA, 0x5BB, 0x5BC, 0x5BD, 0x5BE, 0x5BF,
  465. 0x5C0, 0x5C1, 0x5C2, 0x5C3, 0x5F0, 0x5F1, 0x5F2, 0x5F3, 0x5F4, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  466. 0x5D0, 0x5D1, 0x5D2, 0x5D3, 0x5D4, 0x5D5, 0x5D6, 0x5D7, 0x5D8, 0x5D9, 0x5DA, 0x5DB, 0x5DC, 0x5DD, 0x5DE, 0x5DF,
  467. 0x5E0, 0x5E1, 0x5E2, 0x5E3, 0x5E4, 0x5E5, 0x5E6, 0x5E7, 0x5E8, 0x5E9, 0x5EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD
  468. };
  469. for (unsigned char ch : input) {
  470. if (ch < 0x80) { // Superset of ASCII
  471. TRY(on_code_point(ch));
  472. } else {
  473. TRY(on_code_point(translation_table[ch - 0x80]));
  474. }
  475. }
  476. return {};
  477. }
  478. ErrorOr<void> CyrillicDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  479. {
  480. static constexpr Array<u32, 128> translation_table = {
  481. 0x402, 0x403, 0x201A, 0x453, 0x201E, 0x2026, 0x2020, 0x2021, 0x20AC, 0x2030, 0x409, 0x2039, 0x40A, 0x40C, 0x40B, 0x40F,
  482. 0x452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0x459, 0x203A, 0x45A, 0x45C, 0x45B, 0x45F,
  483. 0xA0, 0x40E, 0x45E, 0x408, 0xA4, 0x490, 0xA6, 0xA7, 0x401, 0xA9, 0x404, 0xAB, 0xAC, 0xAD, 0xAE, 0x407,
  484. 0xB0, 0xB1, 0x406, 0x456, 0x491, 0xB5, 0xB6, 0xB7, 0x451, 0x2116, 0x454, 0xBB, 0x458, 0x405, 0x455, 0x457,
  485. 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, 0x416, 0x417, 0x418, 0x419, 0x41A, 0x41B, 0x41C, 0x41D, 0x41E, 0x41F,
  486. 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427, 0x428, 0x429, 0x42A, 0x42B, 0x42C, 0x42D, 0x42E, 0x42F,
  487. 0x430, 0x431, 0x432, 0x433, 0x434, 0x435, 0x436, 0x437, 0x438, 0x439, 0x43A, 0x43B, 0x43C, 0x43D, 0x43E, 0x43F,
  488. 0x440, 0x441, 0x442, 0x443, 0x444, 0x445, 0x446, 0x447, 0x448, 0x449, 0x44A, 0x44B, 0x44C, 0x44D, 0x44E, 0x44F
  489. };
  490. for (unsigned char ch : input) {
  491. if (ch < 0x80) { // Superset of ASCII
  492. TRY(on_code_point(ch));
  493. } else {
  494. TRY(on_code_point(translation_table[ch - 0x80]));
  495. }
  496. }
  497. return {};
  498. }
  499. ErrorOr<void> Koi8RDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  500. {
  501. // clang-format off
  502. static constexpr Array<u32, 128> translation_table = {
  503. 0x2500,0x2502,0x250c,0x2510,0x2514,0x2518,0x251c,0x2524,0x252c,0x2534,0x253c,0x2580,0x2584,0x2588,0x258c,0x2590,
  504. 0x2591,0x2592,0x2593,0x2320,0x25a0,0x2219,0x221a,0x2248,0x2264,0x2265,0xA0,0x2321,0xb0,0xb2,0xb7,0xf7,
  505. 0x2550,0x2551,0x2552,0xd191,0x2553,0x2554,0x2555,0x2556,0x2557,0x2558,0x2559,0x255a,0x255b,0x255c,0x255d,0x255e,
  506. 0x255f,0x2560,0x2561,0xd081,0x2562,0x2563,0x2564,0x2565,0x2566,0x2567,0x2568,0x2569,0x256a,0x256b,0x256c,0xa9,
  507. 0x44e,0x430,0x431,0x446,0x434,0x435,0x444,0x433,0x445,0x438,0x439,0x43a,0x43b,0x43c,0x43d,0x43e,
  508. 0x43f,0x44f,0x440,0x441,0x442,0x443,0x436,0x432,0x44c,0x44b,0x437,0x448,0x44d,0x449,0x447,0x44a,
  509. 0x42e,0x410,0x441,0x426,0x414,0x415,0x424,0x413,0x425,0x418,0x419,0x41a,0x41b,0x41c,0x41d,0x41e,
  510. 0x41f,0x42f,0x420,0x421,0x422,0x423,0x416,0x412,0x42c,0x42b,0x417,0x428,0x42d,0x429,0x427,0x42a,
  511. };
  512. // clang-format on
  513. for (unsigned char ch : input) {
  514. if (ch < 0x80) { // Superset of ASCII
  515. TRY(on_code_point(ch));
  516. } else {
  517. TRY(on_code_point(translation_table[ch - 0x80]));
  518. }
  519. }
  520. return {};
  521. }
  522. ErrorOr<void> Latin9Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  523. {
  524. auto convert_latin9_to_utf8 = [](u8 ch) -> u32 {
  525. // Latin9 is the same as the first 256 Unicode code points, except for 8 characters.
  526. switch (ch) {
  527. case 0xA4:
  528. return 0x20AC;
  529. case 0xA6:
  530. return 0x160;
  531. case 0xA8:
  532. return 0x161;
  533. case 0xB4:
  534. return 0x17D;
  535. case 0xB8:
  536. return 0x17E;
  537. case 0xBC:
  538. return 0x152;
  539. case 0xBD:
  540. return 0x153;
  541. case 0xBE:
  542. return 0x178;
  543. default:
  544. return ch;
  545. }
  546. };
  547. for (auto ch : input) {
  548. TRY(on_code_point(convert_latin9_to_utf8(ch)));
  549. }
  550. return {};
  551. }
  552. ErrorOr<void> MacRomanDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  553. {
  554. // https://encoding.spec.whatwg.org/index-macintosh.txt
  555. // clang-format off
  556. static constexpr Array<u32, 128> translation_table = {
  557. 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8,
  558. 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC,
  559. 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8,
  560. 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8,
  561. 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153,
  562. 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02,
  563. 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4,
  564. 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7,
  565. };
  566. // clang-format on
  567. for (u8 ch : input) {
  568. if (ch < 0x80) { // Superset of ASCII
  569. TRY(on_code_point(ch));
  570. } else {
  571. TRY(on_code_point(translation_table[ch - 0x80]));
  572. }
  573. }
  574. return {};
  575. }
  576. ErrorOr<void> PDFDocEncodingDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  577. {
  578. // PDF 1.7 spec, Appendix D.2 "PDFDocEncoding Character Set"
  579. // Character codes 0-8, 11-12, 14-23, 127, 159, 173 are not defined per spec.
  580. // clang-format off
  581. static constexpr Array<u32, 256> translation_table = {
  582. 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
  583. 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
  584. 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0017, 0x0017,
  585. 0x02D8, 0x02C7, 0x02C6, 0x02D9, 0x02DD, 0x02DB, 0x02DA, 0x02DC,
  586. 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
  587. 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
  588. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
  589. 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
  590. 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
  591. 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
  592. 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
  593. 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
  594. 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
  595. 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
  596. 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
  597. 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0xFFFC,
  598. 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x0192, 0x2044,
  599. 0x2039, 0x203A, 0x2212, 0x2030, 0x201E, 0x201C, 0x201D, 0x2018,
  600. 0x2019, 0x201A, 0x2122, 0xFB01, 0xFB02, 0x0141, 0x0152, 0x0160,
  601. 0x0178, 0x017D, 0x0131, 0x0142, 0x0153, 0x0161, 0x017E, 0xFFFC,
  602. 0x20AC, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
  603. 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0xFFFC, 0x00AE, 0x00AF,
  604. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
  605. 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  606. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  607. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  608. 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
  609. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
  610. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  611. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  612. 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
  613. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF,
  614. };
  615. // clang-format on
  616. for (u8 ch : input)
  617. TRY(on_code_point(translation_table[ch]));
  618. return {};
  619. }
  620. ErrorOr<void> TurkishDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  621. {
  622. auto convert_turkish_to_utf8 = [](u8 ch) -> u32 {
  623. // Turkish (aka ISO-8859-9, Windows-1254) is the same as the first 256 Unicode code points, except for 6 characters.
  624. switch (ch) {
  625. case 0xD0:
  626. return 0x11E;
  627. case 0xDD:
  628. return 0x130;
  629. case 0xDE:
  630. return 0x15E;
  631. case 0xF0:
  632. return 0x11F;
  633. case 0xFD:
  634. return 0x131;
  635. case 0xFE:
  636. return 0x15F;
  637. default:
  638. return ch;
  639. }
  640. };
  641. for (auto ch : input) {
  642. TRY(on_code_point(convert_turkish_to_utf8(ch)));
  643. }
  644. return {};
  645. }
  646. // https://encoding.spec.whatwg.org/#x-user-defined-decoder
  647. ErrorOr<void> XUserDefinedDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  648. {
  649. auto convert_x_user_defined_to_utf8 = [](u8 ch) -> u32 {
  650. // 2. If byte is an ASCII byte, return a code point whose value is byte.
  651. // https://infra.spec.whatwg.org/#ascii-byte
  652. // An ASCII byte is a byte in the range 0x00 (NUL) to 0x7F (DEL), inclusive.
  653. // NOTE: This doesn't check for ch >= 0x00, as that would always be true due to being unsigned.
  654. if (ch <= 0x7f)
  655. return ch;
  656. // 3. Return a code point whose value is 0xF780 + byte − 0x80.
  657. return 0xF780 + ch - 0x80;
  658. };
  659. for (auto ch : input) {
  660. TRY(on_code_point(convert_x_user_defined_to_utf8(ch)));
  661. }
  662. // 1. If byte is end-of-queue, return finished.
  663. return {};
  664. }
  665. }