Decoder.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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. * Copyright (c) 2024, Simon Wanner <simon@skyrising.xyz>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/BinarySearch.h>
  10. #include <AK/StringBuilder.h>
  11. #include <AK/Utf16View.h>
  12. #include <AK/Utf8View.h>
  13. #include <LibTextCodec/Decoder.h>
  14. #include <LibTextCodec/LookupTables.h>
  15. namespace TextCodec {
  16. static constexpr u32 replacement_code_point = 0xfffd;
  17. namespace {
  18. Latin1Decoder s_latin1_decoder;
  19. UTF8Decoder s_utf8_decoder;
  20. UTF16BEDecoder s_utf16be_decoder;
  21. UTF16LEDecoder s_utf16le_decoder;
  22. PDFDocEncodingDecoder s_pdf_doc_encoding_decoder;
  23. XUserDefinedDecoder s_x_user_defined_decoder;
  24. GB18030Decoder s_gb18030_decoder;
  25. Big5Decoder s_big5_decoder;
  26. EUCJPDecoder s_euc_jp_decoder;
  27. ISO2022JPDecoder s_iso_2022_jp_decoder;
  28. ShiftJISDecoder s_shift_jis_decoder;
  29. EUCKRDecoder s_euc_kr_decoder;
  30. ReplacementDecoder s_replacement_decoder;
  31. // s_{encoding}_index is generated from https://encoding.spec.whatwg.org/indexes.json
  32. // Found separately in https://encoding.spec.whatwg.org/index-{encoding}.txt
  33. SingleByteDecoder s_ibm866_decoder { s_ibm866_index };
  34. SingleByteDecoder s_latin2_decoder { s_iso_8859_2_index };
  35. SingleByteDecoder s_latin3_decoder { s_iso_8859_3_index };
  36. SingleByteDecoder s_latin4_decoder { s_iso_8859_4_index };
  37. SingleByteDecoder s_latin_cyrillic_decoder { s_iso_8859_5_index };
  38. SingleByteDecoder s_latin_arabic_decoder { s_iso_8859_6_index };
  39. SingleByteDecoder s_latin_greek_decoder { s_iso_8859_7_index };
  40. SingleByteDecoder s_latin_hebrew_decoder { s_iso_8859_8_index };
  41. SingleByteDecoder s_latin6_decoder { s_iso_8859_10_index };
  42. SingleByteDecoder s_latin7_decoder { s_iso_8859_13_index };
  43. SingleByteDecoder s_latin8_decoder { s_iso_8859_14_index };
  44. SingleByteDecoder s_latin9_decoder { s_iso_8859_15_index };
  45. SingleByteDecoder s_latin10_decoder { s_iso_8859_16_index };
  46. SingleByteDecoder s_centraleurope_decoder { s_windows_1250_index };
  47. SingleByteDecoder s_cyrillic_decoder { s_windows_1251_index };
  48. SingleByteDecoder s_hebrew_decoder { s_windows_1255_index };
  49. SingleByteDecoder s_koi8r_decoder { s_koi8_r_index };
  50. SingleByteDecoder s_koi8u_decoder { s_koi8_u_index };
  51. SingleByteDecoder s_mac_roman_decoder { s_macintosh_index };
  52. SingleByteDecoder s_windows874_decoder { s_windows_874_index };
  53. SingleByteDecoder s_windows1252_decoder { s_windows_1252_index };
  54. SingleByteDecoder s_windows1253_decoder { s_windows_1253_index };
  55. SingleByteDecoder s_turkish_decoder { s_windows_1254_index };
  56. SingleByteDecoder s_windows1256_decoder { s_windows_1256_index };
  57. SingleByteDecoder s_windows1257_decoder { s_windows_1257_index };
  58. SingleByteDecoder s_windows1258_decoder { s_windows_1258_index };
  59. SingleByteDecoder s_mac_cyrillic_decoder { s_x_mac_cyrillic_index };
  60. }
  61. Optional<Decoder&> decoder_for(StringView label)
  62. {
  63. auto encoding = get_standardized_encoding(label);
  64. return encoding.has_value() ? decoder_for_exact_name(encoding.value()) : Optional<Decoder&> {};
  65. }
  66. Optional<Decoder&> decoder_for_exact_name(StringView encoding)
  67. {
  68. if (encoding.equals_ignoring_ascii_case("iso-8859-1"sv))
  69. return s_latin1_decoder;
  70. if (encoding.equals_ignoring_ascii_case("windows-1252"sv))
  71. return s_windows1252_decoder;
  72. if (encoding.equals_ignoring_ascii_case("utf-8"sv))
  73. return s_utf8_decoder;
  74. if (encoding.equals_ignoring_ascii_case("utf-16be"sv))
  75. return s_utf16be_decoder;
  76. if (encoding.equals_ignoring_ascii_case("utf-16le"sv))
  77. return s_utf16le_decoder;
  78. if (encoding.equals_ignoring_ascii_case("big5"sv))
  79. return s_big5_decoder;
  80. if (encoding.equals_ignoring_ascii_case("euc-jp"sv))
  81. return s_euc_jp_decoder;
  82. if (encoding.equals_ignoring_ascii_case("euc-kr"sv))
  83. return s_euc_kr_decoder;
  84. if (encoding.equals_ignoring_ascii_case("gbk"sv))
  85. return s_gb18030_decoder;
  86. if (encoding.equals_ignoring_ascii_case("gb18030"sv))
  87. return s_gb18030_decoder;
  88. if (encoding.equals_ignoring_ascii_case("ibm866"sv))
  89. return s_ibm866_decoder;
  90. if (encoding.equals_ignoring_ascii_case("iso-2022-jp"sv))
  91. return s_iso_2022_jp_decoder;
  92. if (encoding.equals_ignoring_ascii_case("iso-8859-2"sv))
  93. return s_latin2_decoder;
  94. if (encoding.equals_ignoring_ascii_case("iso-8859-3"sv))
  95. return s_latin3_decoder;
  96. if (encoding.equals_ignoring_ascii_case("iso-8859-4"sv))
  97. return s_latin4_decoder;
  98. if (encoding.equals_ignoring_ascii_case("iso-8859-5"sv))
  99. return s_latin_cyrillic_decoder;
  100. if (encoding.equals_ignoring_ascii_case("iso-8859-6"sv))
  101. return s_latin_arabic_decoder;
  102. if (encoding.equals_ignoring_ascii_case("iso-8859-7"sv))
  103. return s_latin_greek_decoder;
  104. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-8"sv, "iso-8859-8-i"sv))
  105. return s_latin_hebrew_decoder;
  106. if (encoding.equals_ignoring_ascii_case("iso-8859-10"sv))
  107. return s_latin6_decoder;
  108. if (encoding.equals_ignoring_ascii_case("iso-8859-13"sv))
  109. return s_latin7_decoder;
  110. if (encoding.equals_ignoring_ascii_case("iso-8859-14"sv))
  111. return s_latin8_decoder;
  112. if (encoding.equals_ignoring_ascii_case("iso-8859-15"sv))
  113. return s_latin9_decoder;
  114. if (encoding.equals_ignoring_ascii_case("iso-8859-16"sv))
  115. return s_latin10_decoder;
  116. if (encoding.equals_ignoring_ascii_case("koi8-r"sv))
  117. return s_koi8r_decoder;
  118. if (encoding.equals_ignoring_ascii_case("koi8-u"sv))
  119. return s_koi8u_decoder;
  120. if (encoding.equals_ignoring_ascii_case("macintosh"sv))
  121. return s_mac_roman_decoder;
  122. if (encoding.equals_ignoring_ascii_case("PDFDocEncoding"sv))
  123. return s_pdf_doc_encoding_decoder;
  124. if (encoding.equals_ignoring_ascii_case("replacement"sv))
  125. return s_replacement_decoder;
  126. if (encoding.equals_ignoring_ascii_case("shift_jis"sv))
  127. return s_shift_jis_decoder;
  128. if (encoding.equals_ignoring_ascii_case("windows-874"sv))
  129. return s_windows874_decoder;
  130. if (encoding.equals_ignoring_ascii_case("windows-1250"sv))
  131. return s_centraleurope_decoder;
  132. if (encoding.equals_ignoring_ascii_case("windows-1251"sv))
  133. return s_cyrillic_decoder;
  134. if (encoding.equals_ignoring_ascii_case("windows-1253"sv))
  135. return s_windows1253_decoder;
  136. if (encoding.equals_ignoring_ascii_case("windows-1254"sv))
  137. return s_turkish_decoder;
  138. if (encoding.equals_ignoring_ascii_case("windows-1255"sv))
  139. return s_hebrew_decoder;
  140. if (encoding.equals_ignoring_ascii_case("windows-1256"sv))
  141. return s_windows1256_decoder;
  142. if (encoding.equals_ignoring_ascii_case("windows-1257"sv))
  143. return s_windows1257_decoder;
  144. if (encoding.equals_ignoring_ascii_case("windows-1258"sv))
  145. return s_windows1258_decoder;
  146. if (encoding.equals_ignoring_ascii_case("x-mac-cyrillic"sv))
  147. return s_mac_cyrillic_decoder;
  148. if (encoding.equals_ignoring_ascii_case("x-user-defined"sv))
  149. return s_x_user_defined_decoder;
  150. dbgln("TextCodec: No decoder implemented for encoding '{}'", encoding);
  151. return {};
  152. }
  153. // https://encoding.spec.whatwg.org/#concept-encoding-get
  154. Optional<StringView> get_standardized_encoding(StringView encoding)
  155. {
  156. // 1. Remove any leading and trailing ASCII whitespace from label.
  157. // https://infra.spec.whatwg.org/#ascii-whitespace: ASCII whitespace is U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020 SPACE.
  158. encoding = encoding.trim("\t\n\f\r "sv);
  159. // 2. If label is an ASCII case-insensitive match for any of the labels listed in the table below, then return the corresponding encoding; otherwise return failure.
  160. 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))
  161. return "UTF-8"sv;
  162. if (encoding.is_one_of_ignoring_ascii_case("866"sv, "cp866"sv, "csibm866"sv, "ibm866"sv))
  163. return "IBM866"sv;
  164. 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))
  165. return "ISO-8859-2"sv;
  166. 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))
  167. return "ISO-8859-3"sv;
  168. 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:1988"sv, "l4"sv, "latin4"sv))
  169. return "ISO-8859-4"sv;
  170. 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))
  171. return "ISO-8859-5"sv;
  172. 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))
  173. return "ISO-8859-6"sv;
  174. 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))
  175. return "ISO-8859-7"sv;
  176. 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))
  177. return "ISO-8859-8"sv;
  178. if (encoding.is_one_of_ignoring_ascii_case("csiso88598i"sv, "iso-8859-8-i"sv, "logical"sv))
  179. return "ISO-8859-8-I"sv;
  180. if (encoding.is_one_of_ignoring_ascii_case("csisolatin6"sv, "iso-8859-10"sv, "iso-ir-157"sv, "iso8859-10"sv, "iso885910"sv, "l6"sv, "latin6"sv))
  181. return "ISO-8859-10"sv;
  182. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-13"sv, "iso8859-13"sv, "iso885913"sv))
  183. return "ISO-8859-13"sv;
  184. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-14"sv, "iso8859-14"sv, "iso885914"sv))
  185. return "ISO-8859-14"sv;
  186. 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))
  187. return "ISO-8859-15"sv;
  188. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-16"sv))
  189. return "ISO-8859-16"sv;
  190. if (encoding.is_one_of_ignoring_ascii_case("cskoi8r"sv, "koi"sv, "koi8"sv, "koi8-r"sv, "koi8_r"sv))
  191. return "KOI8-R"sv;
  192. if (encoding.is_one_of_ignoring_ascii_case("koi8-ru"sv, "koi8-u"sv))
  193. return "KOI8-U"sv;
  194. if (encoding.is_one_of_ignoring_ascii_case("csmacintosh"sv, "mac"sv, "macintosh"sv, "x-mac-roman"sv))
  195. return "macintosh"sv;
  196. if (encoding.is_one_of_ignoring_ascii_case("pdfdocencoding"sv))
  197. return "PDFDocEncoding"sv;
  198. 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))
  199. return "windows-874"sv;
  200. if (encoding.is_one_of_ignoring_ascii_case("cp1250"sv, "windows-1250"sv, "x-cp1250"sv))
  201. return "windows-1250"sv;
  202. if (encoding.is_one_of_ignoring_ascii_case("cp1251"sv, "windows-1251"sv, "x-cp1251"sv))
  203. return "windows-1251"sv;
  204. 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))
  205. return "windows-1252"sv;
  206. if (encoding.is_one_of_ignoring_ascii_case("cp1253"sv, "windows-1253"sv, "x-cp1253"sv))
  207. return "windows-1253"sv;
  208. if (encoding.is_one_of_ignoring_ascii_case("cp1254"sv, "csisolatin5"sv, "iso-8859-9"sv, "iso-ir-148"sv, "iso8859-9"sv, "iso88599"sv, "iso_8859-9"sv, "iso_8859-9:1989"sv, "l5"sv, "latin5"sv, "windows-1254"sv, "x-cp1254"sv))
  209. return "windows-1254"sv;
  210. if (encoding.is_one_of_ignoring_ascii_case("cp1255"sv, "windows-1255"sv, "x-cp1255"sv))
  211. return "windows-1255"sv;
  212. if (encoding.is_one_of_ignoring_ascii_case("cp1256"sv, "windows-1256"sv, "x-cp1256"sv))
  213. return "windows-1256"sv;
  214. if (encoding.is_one_of_ignoring_ascii_case("cp1257"sv, "windows-1257"sv, "x-cp1257"sv))
  215. return "windows-1257"sv;
  216. if (encoding.is_one_of_ignoring_ascii_case("cp1258"sv, "windows-1258"sv, "x-cp1258"sv))
  217. return "windows-1258"sv;
  218. if (encoding.is_one_of_ignoring_ascii_case("x-mac-cyrillic"sv, "x-mac-ukrainian"sv))
  219. return "x-mac-cyrillic"sv;
  220. if (encoding.is_one_of_ignoring_ascii_case("koi8-r"sv, "koi8r"sv))
  221. return "koi8-r"sv;
  222. 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))
  223. return "GBK"sv;
  224. if (encoding.is_one_of_ignoring_ascii_case("gb18030"sv))
  225. return "gb18030"sv;
  226. if (encoding.is_one_of_ignoring_ascii_case("big5"sv, "big5-hkscs"sv, "cn-big5"sv, "csbig5"sv, "x-x-big5"sv))
  227. return "Big5"sv;
  228. if (encoding.is_one_of_ignoring_ascii_case("cseucpkdfmtjapanese"sv, "euc-jp"sv, "x-euc-jp"sv))
  229. return "EUC-JP"sv;
  230. if (encoding.is_one_of_ignoring_ascii_case("csiso2022jp"sv, "iso-2022-jp"sv))
  231. return "ISO-2022-JP"sv;
  232. 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))
  233. return "Shift_JIS"sv;
  234. 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))
  235. return "EUC-KR"sv;
  236. 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))
  237. return "replacement"sv;
  238. if (encoding.is_one_of_ignoring_ascii_case("unicodefffe"sv, "utf-16be"sv))
  239. return "UTF-16BE"sv;
  240. 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))
  241. return "UTF-16LE"sv;
  242. if (encoding.is_one_of_ignoring_ascii_case("x-user-defined"sv))
  243. return "x-user-defined"sv;
  244. dbgln("TextCodec: Unrecognized encoding: {}", encoding);
  245. return {};
  246. }
  247. // https://encoding.spec.whatwg.org/#bom-sniff
  248. Optional<Decoder&> bom_sniff_to_decoder(StringView input)
  249. {
  250. // 1. Let BOM be the result of peeking 3 bytes from ioQueue, converted to a byte sequence.
  251. // 2. For each of the rows in the table below, starting with the first one and going down,
  252. // if BOM starts with the bytes given in the first column, then return the encoding given
  253. // in the cell in the second column of that row. Otherwise, return null.
  254. // Byte Order Mark | Encoding
  255. // --------------------------
  256. // 0xEF 0xBB 0xBF | UTF-8
  257. // 0xFE 0xFF | UTF-16BE
  258. // 0xFF 0xFE | UTF-16LE
  259. auto bytes = input.bytes();
  260. if (bytes.size() < 2)
  261. return {};
  262. auto first_byte = bytes[0];
  263. switch (first_byte) {
  264. case 0xEF: // UTF-8
  265. if (bytes.size() < 3)
  266. return {};
  267. if (bytes[1] == 0xBB && bytes[2] == 0xBF)
  268. return s_utf8_decoder;
  269. return {};
  270. case 0xFE: // UTF-16BE
  271. if (bytes[1] == 0xFF)
  272. return s_utf16be_decoder;
  273. return {};
  274. case 0xFF: // UTF-16LE
  275. if (bytes[1] == 0xFE)
  276. return s_utf16le_decoder;
  277. return {};
  278. }
  279. return {};
  280. }
  281. // https://encoding.spec.whatwg.org/#decode
  282. ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder& fallback_decoder, StringView input)
  283. {
  284. Decoder* actual_decoder = &fallback_decoder;
  285. // 1. Let BOMEncoding be the result of BOM sniffing ioQueue.
  286. // 2. If BOMEncoding is non-null:
  287. if (auto unicode_decoder = bom_sniff_to_decoder(input); unicode_decoder.has_value()) {
  288. // 1. Set encoding to BOMEncoding.
  289. actual_decoder = &unicode_decoder.value();
  290. // 2. Read three bytes from ioQueue, if BOMEncoding is UTF-8; otherwise read two bytes. (Do nothing with those bytes.)
  291. // FIXME: I imagine this will be pretty slow for large inputs, as it's regenerating the input without the first 2/3 bytes.
  292. input = input.substring_view(&unicode_decoder.value() == &s_utf8_decoder ? 3 : 2);
  293. }
  294. VERIFY(actual_decoder);
  295. // 3. Process a queue with an instance of encoding’s decoder, ioQueue, output, and "replacement".
  296. // FIXME: This isn't the exact same as the spec, which is written in terms of I/O queues.
  297. auto output = TRY(actual_decoder->to_utf8(input));
  298. // 4. Return output.
  299. return output;
  300. }
  301. // https://encoding.spec.whatwg.org/#get-an-output-encoding
  302. StringView get_output_encoding(StringView encoding)
  303. {
  304. // 1. If encoding is replacement or UTF-16BE/LE, then return UTF-8.
  305. if (encoding.is_one_of_ignoring_ascii_case("replacement"sv, "utf-16le"sv, "utf-16be"sv))
  306. return "UTF-8"sv;
  307. // 2. Return encoding.
  308. return encoding;
  309. }
  310. bool Decoder::validate(StringView input)
  311. {
  312. auto result = this->process(input, [](auto code_point) -> ErrorOr<void> {
  313. if (code_point == replacement_code_point)
  314. return Error::from_errno(EINVAL);
  315. return {};
  316. });
  317. return !result.is_error();
  318. }
  319. ErrorOr<String> Decoder::to_utf8(StringView input)
  320. {
  321. StringBuilder builder(input.length());
  322. TRY(process(input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  323. return builder.to_string_without_validation();
  324. }
  325. ErrorOr<void> UTF8Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  326. {
  327. for (auto c : Utf8View(input)) {
  328. TRY(on_code_point(c));
  329. }
  330. return {};
  331. }
  332. bool UTF8Decoder::validate(StringView input)
  333. {
  334. return Utf8View(input).validate();
  335. }
  336. ErrorOr<String> UTF8Decoder::to_utf8(StringView input)
  337. {
  338. // Discard the BOM
  339. auto bomless_input = input;
  340. if (auto bytes = input.bytes(); bytes.size() >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
  341. bomless_input = input.substring_view(3);
  342. }
  343. return Decoder::to_utf8(bomless_input);
  344. }
  345. static Utf16View as_utf16(StringView view, AK::Endianness endianness)
  346. {
  347. return Utf16View {
  348. { reinterpret_cast<u16 const*>(view.bytes().data()), view.length() / 2 },
  349. endianness
  350. };
  351. }
  352. ErrorOr<void> UTF16BEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  353. {
  354. for (auto code_point : as_utf16(input, AK::Endianness::Big))
  355. TRY(on_code_point(code_point));
  356. return {};
  357. }
  358. bool UTF16BEDecoder::validate(StringView input)
  359. {
  360. return as_utf16(input, AK::Endianness::Big).validate();
  361. }
  362. ErrorOr<String> UTF16BEDecoder::to_utf8(StringView input)
  363. {
  364. // Discard the BOM
  365. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF)
  366. input = input.substring_view(2);
  367. return String::from_utf16(as_utf16(input, AK::Endianness::Big));
  368. }
  369. ErrorOr<void> UTF16LEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  370. {
  371. for (auto code_point : as_utf16(input, AK::Endianness::Little))
  372. TRY(on_code_point(code_point));
  373. return {};
  374. }
  375. bool UTF16LEDecoder::validate(StringView input)
  376. {
  377. return as_utf16(input, AK::Endianness::Little).validate();
  378. }
  379. ErrorOr<String> UTF16LEDecoder::to_utf8(StringView input)
  380. {
  381. // Discard the BOM
  382. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE)
  383. input = input.substring_view(2);
  384. return String::from_utf16(as_utf16(input, AK::Endianness::Little));
  385. }
  386. ErrorOr<void> Latin1Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  387. {
  388. for (u8 ch : input) {
  389. // Latin1 is the same as the first 256 Unicode code_points, so no mapping is needed, just utf-8 encoding.
  390. TRY(on_code_point(ch));
  391. }
  392. return {};
  393. }
  394. ErrorOr<void> PDFDocEncodingDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  395. {
  396. // PDF 1.7 spec, Appendix D.2 "PDFDocEncoding Character Set"
  397. // Character codes 0-8, 11-12, 14-23, 127, 159, 173 are not defined per spec.
  398. // clang-format off
  399. static constexpr Array<u32, 256> translation_table = {
  400. 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
  401. 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
  402. 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0017, 0x0017,
  403. 0x02D8, 0x02C7, 0x02C6, 0x02D9, 0x02DD, 0x02DB, 0x02DA, 0x02DC,
  404. 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
  405. 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
  406. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
  407. 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
  408. 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
  409. 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
  410. 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
  411. 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
  412. 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
  413. 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
  414. 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
  415. 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0xFFFC,
  416. 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x0192, 0x2044,
  417. 0x2039, 0x203A, 0x2212, 0x2030, 0x201E, 0x201C, 0x201D, 0x2018,
  418. 0x2019, 0x201A, 0x2122, 0xFB01, 0xFB02, 0x0141, 0x0152, 0x0160,
  419. 0x0178, 0x017D, 0x0131, 0x0142, 0x0153, 0x0161, 0x017E, 0xFFFC,
  420. 0x20AC, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
  421. 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0xFFFC, 0x00AE, 0x00AF,
  422. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
  423. 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  424. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  425. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  426. 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
  427. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
  428. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  429. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  430. 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
  431. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF,
  432. };
  433. // clang-format on
  434. for (u8 ch : input)
  435. TRY(on_code_point(translation_table[ch]));
  436. return {};
  437. }
  438. // https://encoding.spec.whatwg.org/#x-user-defined-decoder
  439. ErrorOr<void> XUserDefinedDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  440. {
  441. auto convert_x_user_defined_to_utf8 = [](u8 ch) -> u32 {
  442. // 2. If byte is an ASCII byte, return a code point whose value is byte.
  443. // https://infra.spec.whatwg.org/#ascii-byte
  444. // An ASCII byte is a byte in the range 0x00 (NUL) to 0x7F (DEL), inclusive.
  445. // NOTE: This doesn't check for ch >= 0x00, as that would always be true due to being unsigned.
  446. if (ch <= 0x7f)
  447. return ch;
  448. // 3. Return a code point whose value is 0xF780 + byte − 0x80.
  449. return 0xF780 + ch - 0x80;
  450. };
  451. for (auto ch : input) {
  452. TRY(on_code_point(convert_x_user_defined_to_utf8(ch)));
  453. }
  454. // 1. If byte is end-of-queue, return finished.
  455. return {};
  456. }
  457. // https://encoding.spec.whatwg.org/#single-byte-decoder
  458. template<Integral ArrayType>
  459. ErrorOr<void> SingleByteDecoder<ArrayType>::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  460. {
  461. for (u8 const byte : input) {
  462. if (byte < 0x80) {
  463. // 2. If byte is an ASCII byte, return a code point whose value is byte.
  464. TRY(on_code_point(byte));
  465. } else {
  466. // 3. Let code point be the index code point for byte − 0x80 in index single-byte.
  467. auto code_point = m_translation_table[byte - 0x80];
  468. // 4. If code point is null, return error.
  469. // NOTE: Error is communicated with 0xFFFD
  470. // 5. Return a code point whose value is code point.
  471. TRY(on_code_point(code_point));
  472. }
  473. }
  474. // 1. If byte is end-of-queue, return finished.
  475. return {};
  476. }
  477. // https://encoding.spec.whatwg.org/#index-gb18030-ranges-code-point
  478. static Optional<u32> index_gb18030_ranges_code_point(u32 pointer)
  479. {
  480. // 1. If pointer is greater than 39419 and less than 189000, or pointer is greater than 1237575, return null.
  481. if ((pointer > 39419 && pointer < 189000) || pointer > 1237575)
  482. return {};
  483. // 2. If pointer is 7457, return code point U+E7C7.
  484. if (pointer == 7457)
  485. return 0xE7C7;
  486. // FIXME: Encoding specification is not updated to GB-18030-2022 yet (https://github.com/whatwg/encoding/issues/312)
  487. // NOTE: This matches https://commits.webkit.org/266173@main
  488. switch (pointer) {
  489. case 19057:
  490. return 0xE81E; // 82 35 90 37
  491. case 19058:
  492. return 0xE826; // 82 35 90 38
  493. case 19059:
  494. return 0xE82B; // 82 35 90 39
  495. case 19060:
  496. return 0xE82C; // 82 35 91 30
  497. case 19061:
  498. return 0xE832; // 82 35 91 31
  499. case 19062:
  500. return 0xE843; // 82 35 91 32
  501. case 19063:
  502. return 0xE854; // 82 35 91 33
  503. case 19064:
  504. return 0xE864; // 82 35 91 34
  505. case 39076:
  506. return 0xE78D; // 84 31 82 36
  507. case 39077:
  508. return 0xE78F; // 84 31 82 37
  509. case 39078:
  510. return 0xE78E; // 84 31 82 38
  511. case 39079:
  512. return 0xE790; // 84 31 82 39
  513. case 39080:
  514. return 0xE791; // 84 31 83 30
  515. case 39081:
  516. return 0xE792; // 84 31 83 31
  517. case 39082:
  518. return 0xE793; // 84 31 83 32
  519. case 39083:
  520. return 0xE794; // 84 31 83 33
  521. case 39084:
  522. return 0xE795; // 84 31 83 34
  523. case 39085:
  524. return 0xE796; // 84 31 83 35
  525. default:
  526. break;
  527. }
  528. // 3. Let offset be the last pointer in index gb18030 ranges that is less than or equal to pointer and let code point offset be its corresponding code point.
  529. size_t last_index;
  530. binary_search(s_gb18030_ranges, pointer, &last_index, [](auto const pointer, auto const& entry) {
  531. return pointer - entry.pointer;
  532. });
  533. auto offset = s_gb18030_ranges[last_index].pointer;
  534. auto code_point_offset = s_gb18030_ranges[last_index].code_point;
  535. // 4. Return a code point whose value is code point offset + pointer − offset.
  536. return code_point_offset + pointer - offset;
  537. }
  538. // https://encoding.spec.whatwg.org/#gb18030-decoder
  539. ErrorOr<void> GB18030Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  540. {
  541. // gb18030’s decoder has an associated gb18030 first, gb18030 second, and gb18030 third (all initially 0x00).
  542. u8 first = 0x00;
  543. u8 second = 0x00;
  544. u8 third = 0x00;
  545. // gb18030’s decoder’s handler, given ioQueue and byte, runs these steps:
  546. size_t index = 0;
  547. while (true) {
  548. // 1. If byte is end-of-queue and gb18030 first, gb18030 second, and gb18030 third are 0x00, return finished.
  549. if (index >= input.length() && first == 0x00 && second == 0x00 && third == 0x00)
  550. return {};
  551. // 2. If byte is end-of-queue, and gb18030 first, gb18030 second, or gb18030 third is not 0x00, set gb18030 first, gb18030 second, and gb18030 third to 0x00, and return error.
  552. if (index >= input.length() && (first != 0x00 || second != 0x00 || third != 0x00)) {
  553. first = 0x00;
  554. second = 0x00;
  555. third = 0x00;
  556. TRY(on_code_point(replacement_code_point));
  557. continue;
  558. }
  559. u8 const byte = input[index++];
  560. // 3. If gb18030 third is not 0x00, then:
  561. if (third != 0x00) {
  562. // 1. If byte is not in the range 0x30 to 0x39, inclusive, then:
  563. if (byte < 0x30 || byte > 0x39) {
  564. // 1. Restore « gb18030 second, gb18030 third, byte » to ioQueue.
  565. index -= 3;
  566. // 2. Set gb18030 first, gb18030 second, and gb18030 third to 0x00.
  567. first = 0x00;
  568. second = 0x00;
  569. third = 0x00;
  570. // 3. Return error.
  571. TRY(on_code_point(replacement_code_point));
  572. continue;
  573. }
  574. // 2. Let code point be the index gb18030 ranges code point for ((gb18030 first − 0x81) × (10 × 126 × 10)) + ((gb18030 second − 0x30) × (10 × 126)) + ((gb18030 third − 0x81) × 10) + byte − 0x30.
  575. auto code_point = index_gb18030_ranges_code_point(((first - 0x81) * (10 * 126 * 10)) + ((second - 0x30) * (10 * 126)) + ((third - 0x81) * 10) + byte - 0x30);
  576. // 3. Set gb18030 first, gb18030 second, and gb18030 third to 0x00.
  577. first = 0x00;
  578. second = 0x00;
  579. third = 0x00;
  580. // 4. If code point is null, return error.
  581. if (!code_point.has_value()) {
  582. TRY(on_code_point(replacement_code_point));
  583. continue;
  584. }
  585. // 5. Return a code point whose value is code point.
  586. TRY(on_code_point(code_point.value()));
  587. continue;
  588. }
  589. // 4. If gb18030 second is not 0x00, then:
  590. if (second != 0x00) {
  591. // 1. If byte is in the range 0x81 to 0xFE, inclusive, set gb18030 third to byte and return continue.
  592. if (byte >= 0x81 && byte <= 0xFE) {
  593. third = byte;
  594. continue;
  595. }
  596. // 2. Restore « gb18030 second, byte » to ioQueue, set gb18030 first and gb18030 second to 0x00, and return error.
  597. index -= 2;
  598. first = 0x00;
  599. second = 0x00;
  600. TRY(on_code_point(replacement_code_point));
  601. continue;
  602. }
  603. // 5. If gb18030 first is not 0x00, then:
  604. if (first != 0x00) {
  605. // 1. If byte is in the range 0x30 to 0x39, inclusive, set gb18030 second to byte and return continue.
  606. if (byte >= 0x30 && byte <= 0x39) {
  607. second = byte;
  608. continue;
  609. }
  610. // 2. Let lead be gb18030 first, let pointer be null, and set gb18030 first to 0x00.
  611. auto lead = first;
  612. Optional<u32> pointer;
  613. first = 0x00;
  614. // 3. Let offset be 0x40 if byte is less than 0x7F, otherwise 0x41.
  615. u8 const offset = byte < 0x7F ? 0x40 : 0x41;
  616. // 4. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80 to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 + (byte − offset).
  617. if ((byte >= 0x40 && byte <= 0x7E) || (byte >= 0x80 && byte <= 0xFE))
  618. pointer = (lead - 0x81) * 190 + (byte - offset);
  619. // 5. Let code point be null if pointer is null, otherwise the index code point for pointer in index gb18030.
  620. auto code_point = pointer.has_value() ? index_gb18030_code_point(pointer.value()) : Optional<u32> {};
  621. // 6. If code point is non-null, return a code point whose value is code point.
  622. if (code_point.has_value()) {
  623. TRY(on_code_point(code_point.value()));
  624. continue;
  625. }
  626. // 7. If byte is an ASCII byte, restore byte to ioQueue.
  627. if (byte <= 0x7F)
  628. index--;
  629. // 8. Return error.
  630. TRY(on_code_point(replacement_code_point));
  631. continue;
  632. }
  633. // 6. If byte is an ASCII byte, return a code point whose value is byte.
  634. if (byte <= 0x7F) {
  635. TRY(on_code_point(byte));
  636. continue;
  637. }
  638. // 7. If byte is 0x80, return code point U+20AC.
  639. if (byte == 0x80) {
  640. TRY(on_code_point(0x20AC));
  641. continue;
  642. }
  643. // 8. If byte is in the range 0x81 to 0xFE, inclusive, set gb18030 first to byte and return continue.
  644. if (byte >= 0x81 && byte <= 0xFE) {
  645. first = byte;
  646. continue;
  647. }
  648. // 9. Return error.
  649. TRY(on_code_point(replacement_code_point));
  650. }
  651. }
  652. // https://encoding.spec.whatwg.org/#big5-decoder
  653. ErrorOr<void> Big5Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  654. {
  655. // Big5’s decoder has an associated Big5 lead (initially 0x00).
  656. u8 big5_lead = 0x00;
  657. // Big5’s decoder’s handler, given ioQueue and byte, runs these steps:
  658. size_t index = 0;
  659. while (true) {
  660. // 1. If byte is end-of-queue and Big5 lead is not 0x00, set Big5 lead to 0x00 and return error.
  661. if (index >= input.length() && big5_lead != 0x00) {
  662. big5_lead = 0x00;
  663. TRY(on_code_point(replacement_code_point));
  664. continue;
  665. }
  666. // 2. If byte is end-of-queue and Big5 lead is 0x00, return finished.
  667. if (index >= input.length() && big5_lead == 0x00)
  668. return {};
  669. u8 const byte = input[index++];
  670. // 3. If Big5 lead is not 0x00, let lead be Big5 lead, let pointer be null, set Big5 lead to 0x00, and then:
  671. if (big5_lead != 0x00) {
  672. auto lead = big5_lead;
  673. Optional<u32> pointer;
  674. big5_lead = 0x00;
  675. // 1. Let offset be 0x40 if byte is less than 0x7F, otherwise 0x62.
  676. u8 const offset = byte < 0x7F ? 0x40 : 0x62;
  677. // 2. If byte is in the range 0x40 to 0x7E, inclusive, or 0xA1 to 0xFE, inclusive, set pointer to (lead − 0x81) × 157 + (byte − offset).
  678. if ((byte >= 0x40 && byte <= 0x7E) || (byte >= 0xA1 && byte <= 0xFE))
  679. pointer = (lead - 0x81) * 157 + (byte - offset);
  680. // 3. If there is a row in the table below whose first column is pointer, return the two code points listed in its second column (the third column is irrelevant):
  681. if (pointer.has_value() && pointer.value() == 1133) {
  682. TRY(on_code_point(0x00CA));
  683. TRY(on_code_point(0x0304));
  684. continue;
  685. }
  686. if (pointer.has_value() && pointer.value() == 1135) {
  687. TRY(on_code_point(0x00CA));
  688. TRY(on_code_point(0x030C));
  689. continue;
  690. }
  691. if (pointer.has_value() && pointer.value() == 1164) {
  692. TRY(on_code_point(0x00EA));
  693. TRY(on_code_point(0x0304));
  694. continue;
  695. }
  696. if (pointer.has_value() && pointer.value() == 1166) {
  697. TRY(on_code_point(0x00EA));
  698. TRY(on_code_point(0x030C));
  699. continue;
  700. }
  701. // 4. Let code point be null if pointer is null, otherwise the index code point for pointer in index Big5.
  702. auto code_pointer = pointer.has_value() ? index_big5_code_point(pointer.value()) : Optional<u32> {};
  703. // 5. If code point is non-null, return a code point whose value is code point.
  704. if (code_pointer.has_value()) {
  705. TRY(on_code_point(code_pointer.value()));
  706. continue;
  707. }
  708. // 6. If byte is an ASCII byte, restore byte to ioQueue.
  709. if (byte <= 0x7F)
  710. index--;
  711. // 7. Return error.
  712. TRY(on_code_point(replacement_code_point));
  713. continue;
  714. }
  715. // 4. If byte is an ASCII byte, return a code point whose value is byte.
  716. if (byte <= 0x7F) {
  717. TRY(on_code_point(byte));
  718. continue;
  719. }
  720. // 5. If byte is in the range 0x81 to 0xFE, inclusive, set Big5 lead to byte and return continue.
  721. if (byte >= 0x81 && byte <= 0xFE) {
  722. big5_lead = byte;
  723. continue;
  724. }
  725. // 6. Return error
  726. TRY(on_code_point(replacement_code_point));
  727. }
  728. }
  729. // https://encoding.spec.whatwg.org/#euc-jp-decoder
  730. ErrorOr<void> EUCJPDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  731. {
  732. // EUC-JP’s decoder has an associated EUC-JP jis0212 (initially false) and EUC-JP lead (initially 0x00).
  733. bool jis0212 = false;
  734. u8 euc_jp_lead = 0x00;
  735. // EUC-JP’s decoder’s handler, given ioQueue and byte, runs these steps:
  736. size_t index = 0;
  737. while (true) {
  738. // 1. If byte is end-of-queue and EUC-JP lead is not 0x00, set EUC-JP lead to 0x00, and return error.
  739. if (index >= input.length() && euc_jp_lead != 0x00) {
  740. euc_jp_lead = 0x00;
  741. TRY(on_code_point(replacement_code_point));
  742. continue;
  743. }
  744. // 2. If byte is end-of-queue and EUC-JP lead is 0x00, return finished.
  745. if (index >= input.length() && euc_jp_lead == 0x00)
  746. return {};
  747. u8 const byte = input[index++];
  748. // 3. If EUC-JP lead is 0x8E and byte is in the range 0xA1 to 0xDF, inclusive, set EUC-JP lead to 0x00 and return a code point whose value is 0xFF61 − 0xA1 + byte.
  749. if (euc_jp_lead == 0x8E && byte >= 0xA1 && byte <= 0xDF) {
  750. euc_jp_lead = 0x00;
  751. TRY(on_code_point(0xFF61 - 0xA1 + byte));
  752. continue;
  753. }
  754. // 4. If EUC-JP lead is 0x8F and byte is in the range 0xA1 to 0xFE, inclusive, set EUC-JP jis0212 to true, set EUC-JP lead to byte, and return continue.
  755. if (euc_jp_lead == 0x8F && byte >= 0xA1 && byte <= 0xFE) {
  756. jis0212 = true;
  757. euc_jp_lead = byte;
  758. continue;
  759. }
  760. // 5. If EUC-JP lead is not 0x00, let lead be EUC-JP lead, set EUC-JP lead to 0x00, and then:
  761. if (euc_jp_lead != 0x00) {
  762. auto lead = euc_jp_lead;
  763. euc_jp_lead = 0x00;
  764. // 1. Let code point be null.
  765. Optional<u32> code_point;
  766. // 2. If lead and byte are both in the range 0xA1 to 0xFE, inclusive, then set code point to the index code point for (lead − 0xA1) × 94 + byte − 0xA1 in index jis0208 if EUC-JP jis0212 is false and in index jis0212 otherwise.
  767. if (lead >= 0xA1 && lead <= 0xFE && byte >= 0xA1 && byte <= 0xFE) {
  768. auto pointer = (lead - 0xA1) * 94 + byte - 0xA1;
  769. code_point = jis0212 ? index_jis0212_code_point(pointer) : index_jis0208_code_point(pointer);
  770. }
  771. // 3. Set EUC-JP jis0212 to false.
  772. jis0212 = false;
  773. // 4. If code point is non-null, return a code point whose value is code point.
  774. if (code_point.has_value()) {
  775. TRY(on_code_point(code_point.value()));
  776. continue;
  777. }
  778. // 5. If byte is an ASCII byte, restore byte to ioQueue.
  779. if (byte <= 0x7F)
  780. index--;
  781. // 6. Return error.
  782. TRY(on_code_point(replacement_code_point));
  783. continue;
  784. }
  785. // 6. If byte is an ASCII byte, return a code point whose value is byte.
  786. if (byte <= 0x7F) {
  787. TRY(on_code_point(byte));
  788. continue;
  789. }
  790. // 7. If byte is 0x8E, 0x8F, or in the range 0xA1 to 0xFE, inclusive, set EUC-JP lead to byte and return continue.
  791. if (byte == 0x8E || byte == 0x8F || (byte >= 0xA1 && byte <= 0xFE)) {
  792. euc_jp_lead = byte;
  793. continue;
  794. }
  795. // 8. Return error.
  796. TRY(on_code_point(replacement_code_point));
  797. }
  798. }
  799. enum class ISO2022JPState {
  800. ASCII,
  801. Roman,
  802. Katakana,
  803. LeadByte,
  804. TrailByte,
  805. EscapeStart,
  806. Escape,
  807. };
  808. // https://encoding.spec.whatwg.org/#iso-2022-jp-decoder
  809. ErrorOr<void> ISO2022JPDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  810. {
  811. // ISO-2022-JP’s decoder has an associated ISO-2022-JP decoder state (initially ASCII), ISO-2022-JP decoder output state (initially ASCII), ISO-2022-JP lead (initially 0x00), and ISO-2022-JP output (initially false).
  812. auto decoder_state = ISO2022JPState::ASCII;
  813. auto output_state = ISO2022JPState::ASCII;
  814. u8 iso2022_jp_lead = 0x00;
  815. bool iso2022_jp_output = false;
  816. size_t index = 0;
  817. while (true) {
  818. Optional<u8> byte;
  819. if (index < input.length())
  820. byte = input[index++];
  821. // ISO-2022-JP’s decoder’s handler, given ioQueue and byte, runs these steps, switching on ISO-2022-JP decoder state:
  822. switch (decoder_state) {
  823. case ISO2022JPState::ASCII:
  824. // Based on byte:
  825. // 0x1B: Set ISO-2022-JP decoder state to escape start and return continue.
  826. if (byte == 0x1B) {
  827. decoder_state = ISO2022JPState::EscapeStart;
  828. continue;
  829. }
  830. // 0x00 to 0x7F, excluding 0x0E, 0x0F, and 0x1B: Set ISO-2022-JP output to false and return a code point whose value is byte.
  831. if (byte.has_value() && byte.value() <= 0x7F && byte != 0x0E && byte != 0x0F) {
  832. iso2022_jp_output = false;
  833. TRY(on_code_point(byte.value()));
  834. continue;
  835. }
  836. // end-of-queue: Return finished.
  837. if (!byte.has_value())
  838. return {};
  839. // Otherwise: Set ISO-2022-JP output to false and return error.
  840. iso2022_jp_output = false;
  841. TRY(on_code_point(replacement_code_point));
  842. break;
  843. case ISO2022JPState::Roman:
  844. // Based on byte:
  845. // 0x1B: Set ISO-2022-JP decoder state to escape start and return continue.
  846. if (byte == 0x1B) {
  847. decoder_state = ISO2022JPState::EscapeStart;
  848. continue;
  849. }
  850. // 0x5C: Set ISO-2022-JP output to false and return code point U+00A5.
  851. if (byte == 0x5C) {
  852. iso2022_jp_output = false;
  853. TRY(on_code_point(0x00A5));
  854. continue;
  855. }
  856. // 0x7E: Set ISO-2022-JP output to false and return code point U+203E.
  857. if (byte == 0x7E) {
  858. iso2022_jp_output = false;
  859. TRY(on_code_point(0x203E));
  860. continue;
  861. }
  862. // 0x00 to 0x7F, excluding 0x0E, 0x0F, 0x1B, 0x5C, and 0x7E: Set ISO-2022-JP output to false and return a code point whose value is byte.
  863. if (byte.has_value() && byte.value() <= 0x7F && byte != 0x0E && byte != 0x0F) {
  864. iso2022_jp_output = false;
  865. TRY(on_code_point(byte.value()));
  866. continue;
  867. }
  868. // end-of-queue: Return finished.
  869. if (!byte.has_value())
  870. return {};
  871. // Otherwise: Set ISO-2022-JP output to false and return error.
  872. iso2022_jp_output = false;
  873. TRY(on_code_point(replacement_code_point));
  874. break;
  875. case ISO2022JPState::Katakana:
  876. // Based on byte:
  877. // 0x1B: Set ISO-2022-JP decoder state to escape start and return continue.
  878. if (byte == 0x1B) {
  879. decoder_state = ISO2022JPState::EscapeStart;
  880. continue;
  881. }
  882. // 0x21 to 0x5F: Set ISO-2022-JP output to false and return a code point whose value is 0xFF61 − 0x21 + byte.
  883. if (byte.has_value() && byte.value() >= 0x21 && byte.value() <= 0x5F) {
  884. iso2022_jp_output = false;
  885. TRY(on_code_point(0xFF61 - 0x21 + byte.value()));
  886. continue;
  887. }
  888. // end-of-queue: Return finished.
  889. if (!byte.has_value())
  890. return {};
  891. // Otherwise: Set ISO-2022-JP output to false and return error.
  892. iso2022_jp_output = false;
  893. TRY(on_code_point(replacement_code_point));
  894. break;
  895. case ISO2022JPState::LeadByte:
  896. // Based on byte:
  897. // 0x1B: Set ISO-2022-JP decoder state to escape start and return continue.
  898. if (byte == 0x1B) {
  899. decoder_state = ISO2022JPState::EscapeStart;
  900. continue;
  901. }
  902. // 0x21 to 0x7E: Set ISO-2022-JP output to false, ISO-2022-JP lead to byte, ISO-2022-JP decoder state to trail byte, and return continue.
  903. if (byte.has_value() && byte.value() >= 0x21 && byte.value() <= 0x7E) {
  904. iso2022_jp_output = false;
  905. iso2022_jp_lead = byte.value();
  906. decoder_state = ISO2022JPState::TrailByte;
  907. continue;
  908. }
  909. // end-of-queue: Return finished.
  910. if (!byte.has_value())
  911. return {};
  912. // Otherwise: Set ISO-2022-JP output to false and return error.
  913. iso2022_jp_output = false;
  914. TRY(on_code_point(replacement_code_point));
  915. break;
  916. case ISO2022JPState::TrailByte:
  917. // Based on byte:
  918. // 0x1B: Set ISO-2022-JP decoder state to escape start and return error.
  919. if (byte == 0x1B) {
  920. decoder_state = ISO2022JPState::EscapeStart;
  921. TRY(on_code_point(replacement_code_point));
  922. continue;
  923. }
  924. // 0x21 to 0x7E:
  925. if (byte.has_value() && byte.value() >= 0x21 && byte.value() <= 0x7E) {
  926. // 1. Set the ISO-2022-JP decoder state to lead byte.
  927. decoder_state = ISO2022JPState::LeadByte;
  928. // 2. Let pointer be (ISO-2022-JP lead − 0x21) × 94 + byte − 0x21.
  929. u32 pointer = (iso2022_jp_lead - 0x21) * 94 + byte.value() - 0x21;
  930. // 3. Let code point be the index code point for pointer in index jis0208.
  931. auto code_point = index_jis0208_code_point(pointer);
  932. // 4. If code point is null, return error.
  933. if (!code_point.has_value()) {
  934. TRY(on_code_point(replacement_code_point));
  935. continue;
  936. }
  937. // 5. Return a code point whose value is code point.
  938. TRY(on_code_point(code_point.value()));
  939. continue;
  940. }
  941. // end-of-queue: Set the ISO-2022-JP decoder state to lead byte and return error.
  942. if (!byte.has_value()) {
  943. decoder_state = ISO2022JPState::LeadByte;
  944. TRY(on_code_point(replacement_code_point));
  945. continue;
  946. }
  947. // Otherwise: Set ISO-2022-JP decoder state to lead byte and return error.
  948. decoder_state = ISO2022JPState::LeadByte;
  949. TRY(on_code_point(replacement_code_point));
  950. break;
  951. case ISO2022JPState::EscapeStart:
  952. // 1. If byte is either 0x24 or 0x28, set ISO-2022-JP lead to byte, ISO-2022-JP decoder state to escape, and return continue.
  953. if (byte == 0x24 || byte == 0x28) {
  954. iso2022_jp_lead = byte.value();
  955. decoder_state = ISO2022JPState::Escape;
  956. continue;
  957. }
  958. // 2. If byte is not end-of-queue, then restore byte to ioQueue.
  959. if (byte.has_value())
  960. index--;
  961. // 3. Set ISO-2022-JP output to false, ISO-2022-JP decoder state to ISO-2022-JP decoder output state, and return error.
  962. iso2022_jp_output = false;
  963. decoder_state = output_state;
  964. TRY(on_code_point(replacement_code_point));
  965. break;
  966. case ISO2022JPState::Escape: {
  967. // 1. Let lead be ISO-2022-JP lead and set ISO-2022-JP lead to 0x00.
  968. auto lead = iso2022_jp_lead;
  969. iso2022_jp_lead = 0x00;
  970. // 2. Let state be null.
  971. Optional<ISO2022JPState> state;
  972. // 3. If lead is 0x28 and byte is 0x42, set state to ASCII.
  973. if (lead == 0x28 && byte == 0x42)
  974. state = ISO2022JPState::ASCII;
  975. // 4. If lead is 0x28 and byte is 0x4A, set state to Roman.
  976. if (lead == 0x28 && byte == 0x4A)
  977. state = ISO2022JPState::Roman;
  978. // 5. If lead is 0x28 and byte is 0x49, set state to katakana.
  979. if (lead == 0x28 && byte == 0x49)
  980. state = ISO2022JPState::Katakana;
  981. // 6. If lead is 0x24 and byte is either 0x40 or 0x42, set state to lead byte.
  982. if (lead == 0x24 && (byte == 0x40 || byte == 0x42))
  983. state = ISO2022JPState::LeadByte;
  984. // 7. If state is non-null, then:
  985. if (state.has_value()) {
  986. // 1. Set ISO-2022-JP decoder state and ISO-2022-JP decoder output state to state.
  987. decoder_state = state.value();
  988. output_state = state.value();
  989. // 2. Let output be the value of ISO-2022-JP output.
  990. auto output = iso2022_jp_output;
  991. // 3. Set ISO-2022-JP output to true.
  992. iso2022_jp_output = true;
  993. // 4. Return continue, if output is false, and error otherwise.
  994. if (output)
  995. TRY(on_code_point(replacement_code_point));
  996. continue;
  997. }
  998. // 8. If byte is end-of-queue, then restore lead to ioQueue; otherwise, restore « lead, byte » to ioQueue.
  999. index -= byte.has_value() ? 2 : 1;
  1000. // 9. Set ISO-2022-JP output to false, ISO-2022-JP decoder state to ISO-2022-JP decoder output state and return error.
  1001. iso2022_jp_output = false;
  1002. decoder_state = output_state;
  1003. TRY(on_code_point(replacement_code_point));
  1004. break;
  1005. }
  1006. }
  1007. }
  1008. }
  1009. // https://encoding.spec.whatwg.org/#shift_jis-decoder
  1010. ErrorOr<void> ShiftJISDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  1011. {
  1012. // Shift_JIS’s decoder has an associated Shift_JIS lead (initially 0x00).
  1013. u8 shift_jis_lead = 0x00;
  1014. // Shift_JIS’s decoder’s handler, given ioQueue and byte, runs these steps:
  1015. size_t index = 0;
  1016. while (true) {
  1017. // 1. If byte is end-of-queue and Shift_JIS lead is not 0x00, set Shift_JIS lead to 0x00 and return error.
  1018. if (index >= input.length() && shift_jis_lead != 0x00) {
  1019. shift_jis_lead = 0x00;
  1020. TRY(on_code_point(replacement_code_point));
  1021. continue;
  1022. }
  1023. // 2. If byte is end-of-queue and Shift_JIS lead is 0x00, return finished.
  1024. if (index >= input.length() && shift_jis_lead == 0x00)
  1025. return {};
  1026. u8 const byte = input[index++];
  1027. // 3. If Shift_JIS lead is not 0x00, let lead be Shift_JIS lead, let pointer be null, set Shift_JIS lead to 0x00, and then:
  1028. if (shift_jis_lead != 0x00) {
  1029. auto lead = shift_jis_lead;
  1030. Optional<u32> pointer;
  1031. shift_jis_lead = 0x00;
  1032. // 1. Let offset be 0x40 if byte is less than 0x7F, otherwise 0x41.
  1033. u8 const offset = byte < 0x7F ? 0x40 : 0x41;
  1034. // 2. Let lead offset be 0x81 if lead is less than 0xA0, otherwise 0xC1.
  1035. u8 const lead_offset = lead < 0xA0 ? 0x81 : 0xC1;
  1036. // 3. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80 to 0xFC, inclusive, set pointer to (lead − lead offset) × 188 + byte − offset.
  1037. if ((byte >= 0x40 && byte <= 0x7E) || (byte >= 0x80 && byte <= 0xFC))
  1038. pointer = (lead - lead_offset) * 188 + byte - offset;
  1039. // 4. If pointer is in the range 8836 to 10715, inclusive, return a code point whose value is 0xE000 − 8836 + pointer.
  1040. if (pointer.has_value() && pointer.value() >= 8836 && pointer.value() <= 10715) {
  1041. TRY(on_code_point(0xE000 - 8836 + pointer.value()));
  1042. continue;
  1043. }
  1044. // 5. Let code point be null if pointer is null, otherwise the index code point for pointer in index jis0208.
  1045. auto code_point = pointer.has_value() ? index_jis0208_code_point(pointer.value()) : Optional<u32> {};
  1046. // 6. If code point is non-null, return a code point whose value is code point.
  1047. if (code_point.has_value()) {
  1048. TRY(on_code_point(code_point.value()));
  1049. continue;
  1050. }
  1051. // 7. If byte is an ASCII byte, restore byte to ioQueue.
  1052. if (byte <= 0x7F)
  1053. index--;
  1054. // 8. Return error.
  1055. TRY(on_code_point(replacement_code_point));
  1056. continue;
  1057. }
  1058. // 4. If byte is an ASCII byte or 0x80, return a code point whose value is byte.
  1059. if (byte <= 0x80) {
  1060. TRY(on_code_point(byte));
  1061. continue;
  1062. }
  1063. // 5. If byte is in the range 0xA1 to 0xDF, inclusive, return a code point whose value is 0xFF61 − 0xA1 + byte.
  1064. if (byte >= 0xA1 && byte <= 0xDF) {
  1065. TRY(on_code_point(0xFF61 - 0xA1 + byte));
  1066. continue;
  1067. }
  1068. // 6. If byte is in the range 0x81 to 0x9F, inclusive, or 0xE0 to 0xFC, inclusive, set Shift_JIS lead to byte and return continue.
  1069. if ((byte >= 0x81 && byte <= 0x9F) || (byte >= 0xE0 && byte <= 0xFC)) {
  1070. shift_jis_lead = byte;
  1071. continue;
  1072. }
  1073. // 7. Return error.
  1074. TRY(on_code_point(replacement_code_point));
  1075. }
  1076. }
  1077. // https://encoding.spec.whatwg.org/#euc-kr-decoder
  1078. ErrorOr<void> EUCKRDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  1079. {
  1080. // EUC-KR’s decoder has an associated EUC-KR lead (initially 0x00).
  1081. u8 euc_kr_lead = 0x00;
  1082. // EUC-KR’s decoder’s handler, given ioQueue and byte, runs these steps:
  1083. size_t index = 0;
  1084. while (true) {
  1085. // 1. If byte is end-of-queue and EUC-KR lead is not 0x00, set EUC-KR lead to 0x00 and return error.
  1086. if (index >= input.length() && euc_kr_lead != 0x00) {
  1087. euc_kr_lead = 0x00;
  1088. TRY(on_code_point(replacement_code_point));
  1089. continue;
  1090. }
  1091. // 2. If byte is end-of-queue and EUC-KR lead is 0x00, return finished.
  1092. if (index >= input.length() && euc_kr_lead == 0x00)
  1093. return {};
  1094. u8 const byte = input[index++];
  1095. // 3. If EUC-KR lead is not 0x00, let lead be EUC-KR lead, let pointer be null, set EUC-KR lead to 0x00, and then:
  1096. if (euc_kr_lead != 0x00) {
  1097. auto lead = euc_kr_lead;
  1098. Optional<u32> pointer;
  1099. euc_kr_lead = 0x00;
  1100. // 1. If byte is in the range 0x41 to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 + (byte − 0x41).
  1101. if (byte >= 0x41 && byte <= 0xFE)
  1102. pointer = (lead - 0x81) * 190 + (byte - 0x41);
  1103. // 2. Let code point be null if pointer is null, otherwise the index code point for pointer in index EUC-KR.
  1104. auto code_point = pointer.has_value() ? index_euc_kr_code_point(pointer.value()) : Optional<u32> {};
  1105. // 3. If code point is non-null, return a code point whose value is code point.
  1106. if (code_point.has_value()) {
  1107. TRY(on_code_point(code_point.value()));
  1108. continue;
  1109. }
  1110. // 4. If byte is an ASCII byte, restore byte to ioQueue.
  1111. if (byte <= 0x7F)
  1112. index--;
  1113. // 5. Return error.
  1114. TRY(on_code_point(replacement_code_point));
  1115. continue;
  1116. }
  1117. // 4. If byte is an ASCII byte, return a code point whose value is byte.
  1118. if (byte <= 0x7F) {
  1119. TRY(on_code_point(byte));
  1120. continue;
  1121. }
  1122. // 5. If byte is in the range 0x81 to 0xFE, inclusive, set EUC-KR lead to byte and return continue.
  1123. if (byte >= 0x81 && byte <= 0xFE) {
  1124. euc_kr_lead = byte;
  1125. continue;
  1126. }
  1127. // 6. Return error.
  1128. TRY(on_code_point(replacement_code_point));
  1129. }
  1130. }
  1131. // https://encoding.spec.whatwg.org/#replacement-decoder
  1132. ErrorOr<void> ReplacementDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  1133. {
  1134. // replacement’s decoder has an associated replacement error returned (initially false).
  1135. // replacement’s decoder’s handler, given ioQueue and byte, runs these steps:
  1136. // 1. If byte is end-of-queue, return finished.
  1137. // 2. If replacement error returned is false, set replacement error returned to true and return error.
  1138. // 3. Return finished.
  1139. if (!input.is_empty())
  1140. return on_code_point(replacement_code_point);
  1141. return {};
  1142. }
  1143. }