Decoder.cpp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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. Latin2Decoder s_latin2_decoder;
  23. Latin9Decoder s_latin9_decoder;
  24. PDFDocEncodingDecoder s_pdf_doc_encoding_decoder;
  25. TurkishDecoder s_turkish_decoder;
  26. XUserDefinedDecoder s_x_user_defined_decoder;
  27. GB18030Decoder s_gb18030_decoder;
  28. Big5Decoder s_big5_decoder;
  29. EUCJPDecoder s_euc_jp_decoder;
  30. ISO2022JPDecoder s_iso_2022_jp_decoder;
  31. ShiftJISDecoder s_shift_jis_decoder;
  32. EUCKRDecoder s_euc_kr_decoder;
  33. ReplacementDecoder s_replacement_decoder;
  34. // clang-format off
  35. // https://encoding.spec.whatwg.org/index-ibm866.txt
  36. SingleByteDecoder s_ibm866_decoder {{
  37. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  38. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  39. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  40. 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
  41. 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
  42. 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
  43. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  44. 0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4, 0x25A0, 0x00A0,
  45. }};
  46. // https://encoding.spec.whatwg.org/index-iso-8859-3.txt
  47. SingleByteDecoder s_latin3_decoder {{
  48. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  49. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  50. 0x00A0, 0x0126, 0x02D8, 0x00A3, 0x00A4, 0xFFFD, 0x0124, 0x00A7, 0x00A8, 0x0130, 0x015E, 0x011E, 0x0134, 0x00AD, 0xFFFD, 0x017B,
  51. 0x00B0, 0x0127, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x0125, 0x00B7, 0x00B8, 0x0131, 0x015F, 0x011F, 0x0135, 0x00BD, 0xFFFD, 0x017C,
  52. 0x00C0, 0x00C1, 0x00C2, 0xFFFD, 0x00C4, 0x010A, 0x0108, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  53. 0xFFFD, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x0120, 0x00D6, 0x00D7, 0x011C, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x016C, 0x015C, 0x00DF,
  54. 0x00E0, 0x00E1, 0x00E2, 0xFFFD, 0x00E4, 0x010B, 0x0109, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  55. 0xFFFD, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x0121, 0x00F6, 0x00F7, 0x011D, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x016D, 0x015D, 0x02D9,
  56. }};
  57. // https://encoding.spec.whatwg.org/index-iso-8859-4.txt
  58. SingleByteDecoder s_latin4_decoder {{
  59. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  60. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  61. 0x00A0, 0x0104, 0x0138, 0x0156, 0x00A4, 0x0128, 0x013B, 0x00A7, 0x00A8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00AD, 0x017D, 0x00AF,
  62. 0x00B0, 0x0105, 0x02DB, 0x0157, 0x00B4, 0x0129, 0x013C, 0x02C7, 0x00B8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014A, 0x017E, 0x014B,
  63. 0x0100, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x012E, 0x010C, 0x00C9, 0x0118, 0x00CB, 0x0116, 0x00CD, 0x00CE, 0x012A,
  64. 0x0110, 0x0145, 0x014C, 0x0136, 0x00D4, 0x00D5, 0x00D6, 0x00D7, 0x00D8, 0x0172, 0x00DA, 0x00DB, 0x00DC, 0x0168, 0x016A, 0x00DF,
  65. 0x0101, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x012F, 0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x012B,
  66. 0x0111, 0x0146, 0x014D, 0x0137, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x0169, 0x016B, 0x02D9,
  67. }};
  68. // https://encoding.spec.whatwg.org/index-iso-8859-5.txt
  69. SingleByteDecoder s_latin_cyrillic_decoder {{
  70. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  71. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  72. 0x00A0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, 0x0408, 0x0409, 0x040A, 0x040B, 0x040C, 0x00AD, 0x040E, 0x040F,
  73. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  74. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  75. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  76. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  77. 0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457, 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x00A7, 0x045E, 0x045F,
  78. }};
  79. // https://encoding.spec.whatwg.org/index-iso-8859-6.txt
  80. SingleByteDecoder s_latin_arabic_decoder {{
  81. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  82. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  83. 0x00A0, 0xFFFD, 0xFFFD, 0xFFFD, 0x00A4, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x060C, 0x00AD, 0xFFFD, 0xFFFD,
  84. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x061B, 0xFFFD, 0xFFFD, 0xFFFD, 0x061F,
  85. 0xFFFD, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627, 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F,
  86. 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x0637, 0x0638, 0x0639, 0x063A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  87. 0x0640, 0x0641, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647, 0x0648, 0x0649, 0x064A, 0x064B, 0x064C, 0x064D, 0x064E, 0x064F,
  88. 0x0650, 0x0651, 0x0652, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  89. }};
  90. // https://encoding.spec.whatwg.org/index-iso-8859-7.txt
  91. SingleByteDecoder s_latin_greek_decoder {{
  92. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  93. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  94. 0x00A0, 0x2018, 0x2019, 0x00A3, 0x20AC, 0x20AF, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x037A, 0x00AB, 0x00AC, 0x00AD, 0xFFFD, 0x2015,
  95. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x0385, 0x0386, 0x00B7, 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F,
  96. 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F,
  97. 0x03A0, 0x03A1, 0xFFFD, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF,
  98. 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
  99. 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0xFFFD,
  100. }};
  101. // https://encoding.spec.whatwg.org/index-iso-8859-8.txt
  102. SingleByteDecoder s_latin_hebrew_decoder {{
  103. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  104. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  105. 0x00A0, 0xFFFD, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  106. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0xFFFD,
  107. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  108. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2017,
  109. 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
  110. 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD,
  111. }};
  112. // https://encoding.spec.whatwg.org/index-iso-8859-10.txt
  113. SingleByteDecoder s_latin6_decoder {{
  114. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  115. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  116. 0x00A0, 0x0104, 0x0112, 0x0122, 0x012A, 0x0128, 0x0136, 0x00A7, 0x013B, 0x0110, 0x0160, 0x0166, 0x017D, 0x00AD, 0x016A, 0x014A,
  117. 0x00B0, 0x0105, 0x0113, 0x0123, 0x012B, 0x0129, 0x0137, 0x00B7, 0x013C, 0x0111, 0x0161, 0x0167, 0x017E, 0x2015, 0x016B, 0x014B,
  118. 0x0100, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x012E, 0x010C, 0x00C9, 0x0118, 0x00CB, 0x0116, 0x00CD, 0x00CE, 0x00CF,
  119. 0x00D0, 0x0145, 0x014C, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x0168, 0x00D8, 0x0172, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
  120. 0x0101, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x012F, 0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x00EF,
  121. 0x00F0, 0x0146, 0x014D, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x0169, 0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x0138,
  122. }};
  123. // https://encoding.spec.whatwg.org/index-iso-8859-13.txt
  124. SingleByteDecoder s_latin7_decoder {{
  125. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  126. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  127. 0x00A0, 0x201D, 0x00A2, 0x00A3, 0x00A4, 0x201E, 0x00A6, 0x00A7, 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6,
  128. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x201C, 0x00B5, 0x00B6, 0x00B7, 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6,
  129. 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112, 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B,
  130. 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7, 0x0172, 0x0141, 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF,
  131. 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113, 0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C,
  132. 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7, 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x2019,
  133. }};
  134. // https://encoding.spec.whatwg.org/index-iso-8859-14.txt
  135. SingleByteDecoder s_latin8_decoder {{
  136. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  137. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  138. 0x00A0, 0x1E02, 0x1E03, 0x00A3, 0x010A, 0x010B, 0x1E0A, 0x00A7, 0x1E80, 0x00A9, 0x1E82, 0x1E0B, 0x1EF2, 0x00AD, 0x00AE, 0x0178,
  139. 0x1E1E, 0x1E1F, 0x0120, 0x0121, 0x1E40, 0x1E41, 0x00B6, 0x1E56, 0x1E81, 0x1E57, 0x1E83, 0x1E60, 0x1EF3, 0x1E84, 0x1E85, 0x1E61,
  140. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  141. 0x0174, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x1E6A, 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x0176, 0x00DF,
  142. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  143. 0x0175, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x1E6B, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x0177, 0x00FF,
  144. }};
  145. // https://encoding.spec.whatwg.org/index-iso-8859-16.txt
  146. SingleByteDecoder s_latin10_decoder {{
  147. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  148. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  149. 0x00A0, 0x0104, 0x0105, 0x0141, 0x20AC, 0x201E, 0x0160, 0x00A7, 0x0161, 0x00A9, 0x0218, 0x00AB, 0x0179, 0x00AD, 0x017A, 0x017B,
  150. 0x00B0, 0x00B1, 0x010C, 0x0142, 0x017D, 0x201D, 0x00B6, 0x00B7, 0x017E, 0x010D, 0x0219, 0x00BB, 0x0152, 0x0153, 0x0178, 0x017C,
  151. 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0106, 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  152. 0x0110, 0x0143, 0x00D2, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x015A, 0x0170, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0118, 0x021A, 0x00DF,
  153. 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x0107, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  154. 0x0111, 0x0144, 0x00F2, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x015B, 0x0171, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0119, 0x021B, 0x00FF,
  155. }};
  156. // https://encoding.spec.whatwg.org/index-windows-1250.txt
  157. SingleByteDecoder s_centraleurope_decoder {{
  158. 0x20AC, 0x0081, 0x201A, 0x0083, 0x201E, 0x2026, 0x2020, 0x2021, 0x0088, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179,
  159. 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0098, 0x2122, 0x0161, 0x203A, 0x015B, 0x0165, 0x017E, 0x017A,
  160. 0x00A0, 0x02C7, 0x02D8, 0x0141, 0x00A4, 0x0104, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x017B,
  161. 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, 0x017C,
  162. 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,
  163. 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,
  164. 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
  165. 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9,
  166. }};
  167. // https://encoding.spec.whatwg.org/index-windows-1251.txt
  168. SingleByteDecoder s_cyrillic_decoder {{
  169. 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021, 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F,
  170. 0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0098, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F,
  171. 0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7, 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407,
  172. 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7, 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457,
  173. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  174. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  175. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  176. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  177. }};
  178. // https://encoding.spec.whatwg.org/index-windows-1255.txt
  179. SingleByteDecoder s_hebrew_decoder {{
  180. 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x008A, 0x2039, 0x008C, 0x008D, 0x008E, 0x008F,
  181. 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x009A, 0x203A, 0x009C, 0x009D, 0x009E, 0x009F,
  182. 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AA, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  183. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  184. 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7, 0x05B8, 0x05B9, 0x05BA, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF,
  185. 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3, 0x05F4, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  186. 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
  187. 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD,
  188. }};
  189. // https://encoding.spec.whatwg.org/index-koi8-r.txt
  190. SingleByteDecoder s_koi8r_decoder {{
  191. 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524, 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
  192. 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219, 0x221A, 0x2248, 0x2264, 0x2265, 0x00A0, 0x2321, 0x00B0, 0x00B2, 0x00B7, 0x00F7,
  193. 0x2550, 0x2551, 0x2552, 0xD191, 0x2553, 0x2554, 0x2555, 0x2556, 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x255C, 0x255D, 0x255E,
  194. 0x255F, 0x2560, 0x2561, 0xD081, 0x2562, 0x2563, 0x2564, 0x2565, 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x256B, 0x256C, 0x00A9,
  195. 0x044E, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, 0x0445, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E,
  196. 0x043F, 0x044F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, 0x044C, 0x044B, 0x0437, 0x0448, 0x044D, 0x0449, 0x0447, 0x044A,
  197. 0x042E, 0x0410, 0x0441, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, 0x0425, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
  198. 0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, 0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A,
  199. }};
  200. // https://encoding.spec.whatwg.org/index-koi8-u.txt
  201. SingleByteDecoder s_koi8u_decoder {{
  202. 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524, 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
  203. 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219, 0x221A, 0x2248, 0x2264, 0x2265, 0x00A0, 0x2321, 0x00B0, 0x00B2, 0x00B7, 0x00F7,
  204. 0x2550, 0x2551, 0x2552, 0x0451, 0x0454, 0x2554, 0x0456, 0x0457, 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x0491, 0x045E, 0x255E,
  205. 0x255F, 0x2560, 0x2561, 0x0401, 0x0404, 0x2563, 0x0406, 0x0407, 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x0490, 0x040E, 0x00A9,
  206. 0x044E, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, 0x0445, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E,
  207. 0x043F, 0x044F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, 0x044C, 0x044B, 0x0437, 0x0448, 0x044D, 0x0449, 0x0447, 0x044A,
  208. 0x042E, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, 0x0425, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
  209. 0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, 0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A,
  210. }};
  211. // https://encoding.spec.whatwg.org/index-macintosh.txt
  212. SingleByteDecoder s_mac_roman_decoder {{
  213. 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8,
  214. 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC,
  215. 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8,
  216. 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8,
  217. 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153,
  218. 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02,
  219. 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4,
  220. 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7,
  221. }};
  222. // https://encoding.spec.whatwg.org/index-windows-874.txt
  223. SingleByteDecoder s_windows874_decoder {{
  224. 0x20AC, 0x0081, 0x0082, 0x0083, 0x0084, 0x2026, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  225. 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  226. 0x00A0, 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07, 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F,
  227. 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, 0x0E16, 0x0E17, 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F,
  228. 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27, 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F,
  229. 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37, 0x0E38, 0x0E39, 0x0E3A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x0E3F,
  230. 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F,
  231. 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57, 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  232. }};
  233. // https://encoding.spec.whatwg.org/index-windows-1253.txt
  234. SingleByteDecoder s_windows1253_decoder {{
  235. 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x0088, 0x2030, 0x008A, 0x2039, 0x008C, 0x008D, 0x008E, 0x008F,
  236. 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0098, 0x2122, 0x009A, 0x203A, 0x009C, 0x009D, 0x009E, 0x009F,
  237. 0x00A0, 0x0385, 0x0386, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0xFFFD, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x2015,
  238. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x00B5, 0x00B6, 0x00B7, 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F,
  239. 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F,
  240. 0x03A0, 0x03A1, 0xFFFD, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF,
  241. 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
  242. 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0xFFFD,
  243. }};
  244. // https://encoding.spec.whatwg.org/index-windows-1256.txt
  245. SingleByteDecoder s_windows1256_decoder {{
  246. 0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688,
  247. 0x06AF, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x06A9, 0x2122, 0x0691, 0x203A, 0x0153, 0x200C, 0x200D, 0x06BA,
  248. 0x00A0, 0x060C, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x06BE, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  249. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x061B, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x061F,
  250. 0x06C1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627, 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F,
  251. 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00D7, 0x0637, 0x0638, 0x0639, 0x063A, 0x0640, 0x0641, 0x0642, 0x0643,
  252. 0x00E0, 0x0644, 0x00E2, 0x0645, 0x0646, 0x0647, 0x0648, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0649, 0x064A, 0x00EE, 0x00EF,
  253. 0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, 0x064F, 0x0650, 0x00F7, 0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E, 0x200F, 0x06D2,
  254. }};
  255. // https://encoding.spec.whatwg.org/index-windows-1257.txt
  256. SingleByteDecoder s_windows1257_decoder {{
  257. 0x20AC, 0x0081, 0x201A, 0x0083, 0x201E, 0x2026, 0x2020, 0x2021, 0x0088, 0x2030, 0x008A, 0x2039, 0x008C, 0x00A8, 0x02C7, 0x00B8,
  258. 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0098, 0x2122, 0x009A, 0x203A, 0x009C, 0x00AF, 0x02DB, 0x009F,
  259. 0x00A0, 0xFFFD, 0x00A2, 0x00A3, 0x00A4, 0xFFFD, 0x00A6, 0x00A7, 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6,
  260. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6,
  261. 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112, 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B,
  262. 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7, 0x0172, 0x0141, 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF,
  263. 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113, 0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C,
  264. 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7, 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x02D9,
  265. }};
  266. // https://encoding.spec.whatwg.org/index-windows-1258.txt
  267. SingleByteDecoder s_windows1258_decoder {{
  268. 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x008A, 0x2039, 0x0152, 0x008D, 0x008E, 0x008F,
  269. 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x009A, 0x203A, 0x0153, 0x009D, 0x009E, 0x0178,
  270. 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  271. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  272. 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x00C5, 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x0300, 0x00CD, 0x00CE, 0x00CF,
  273. 0x0110, 0x00D1, 0x0309, 0x00D3, 0x00D4, 0x01A0, 0x00D6, 0x00D7, 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x01AF, 0x0303, 0x00DF,
  274. 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0301, 0x00ED, 0x00EE, 0x00EF,
  275. 0x0111, 0x00F1, 0x0323, 0x00F3, 0x00F4, 0x01A1, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x01B0, 0x20AB, 0x00FF,
  276. }};
  277. // https://encoding.spec.whatwg.org/index-x-mac-cyrillic.txt
  278. SingleByteDecoder s_mac_cyrillic_decoder {{
  279. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  280. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  281. 0x2020, 0x00B0, 0x0490, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x0406, 0x00AE, 0x00A9, 0x2122, 0x0402, 0x0452, 0x2260, 0x0403, 0x0453,
  282. 0x221E, 0x00B1, 0x2264, 0x2265, 0x0456, 0x00B5, 0x0491, 0x0408, 0x0404, 0x0454, 0x0407, 0x0457, 0x0409, 0x0459, 0x040A, 0x045A,
  283. 0x0458, 0x0405, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x040B, 0x045B, 0x040C, 0x045C, 0x0455,
  284. 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x201E, 0x040E, 0x045E, 0x040F, 0x045F, 0x2116, 0x0401, 0x0451, 0x044F,
  285. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  286. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x20AC,
  287. }};
  288. // clang-format on
  289. }
  290. Optional<Decoder&> decoder_for(StringView a_encoding)
  291. {
  292. auto encoding = get_standardized_encoding(a_encoding);
  293. if (encoding.has_value()) {
  294. if (encoding.value().equals_ignoring_ascii_case("windows-1252"sv))
  295. return s_latin1_decoder;
  296. if (encoding.value().equals_ignoring_ascii_case("utf-8"sv))
  297. return s_utf8_decoder;
  298. if (encoding.value().equals_ignoring_ascii_case("utf-16be"sv))
  299. return s_utf16be_decoder;
  300. if (encoding.value().equals_ignoring_ascii_case("utf-16le"sv))
  301. return s_utf16le_decoder;
  302. if (encoding.value().equals_ignoring_ascii_case("big5"sv))
  303. return s_big5_decoder;
  304. if (encoding.value().equals_ignoring_ascii_case("euc-jp"sv))
  305. return s_euc_jp_decoder;
  306. if (encoding.value().equals_ignoring_ascii_case("euc-kr"sv))
  307. return s_euc_kr_decoder;
  308. if (encoding.value().equals_ignoring_ascii_case("gbk"sv))
  309. return s_gb18030_decoder;
  310. if (encoding.value().equals_ignoring_ascii_case("gb18030"sv))
  311. return s_gb18030_decoder;
  312. if (encoding.value().equals_ignoring_ascii_case("ibm866"sv))
  313. return s_ibm866_decoder;
  314. if (encoding.value().equals_ignoring_ascii_case("iso-2022-jp"sv))
  315. return s_iso_2022_jp_decoder;
  316. if (encoding.value().equals_ignoring_ascii_case("iso-8859-2"sv))
  317. return s_latin2_decoder;
  318. if (encoding.value().equals_ignoring_ascii_case("iso-8859-3"sv))
  319. return s_latin3_decoder;
  320. if (encoding.value().equals_ignoring_ascii_case("iso-8859-4"sv))
  321. return s_latin4_decoder;
  322. if (encoding.value().equals_ignoring_ascii_case("iso-8859-5"sv))
  323. return s_latin_cyrillic_decoder;
  324. if (encoding.value().equals_ignoring_ascii_case("iso-8859-6"sv))
  325. return s_latin_arabic_decoder;
  326. if (encoding.value().equals_ignoring_ascii_case("iso-8859-7"sv))
  327. return s_latin_greek_decoder;
  328. if (encoding.value().equals_ignoring_ascii_case("iso-8859-8"sv))
  329. return s_latin_hebrew_decoder;
  330. if (encoding.value().equals_ignoring_ascii_case("iso-8859-8-i"sv))
  331. return s_latin_hebrew_decoder;
  332. if (encoding.value().equals_ignoring_ascii_case("iso-8859-10"sv))
  333. return s_latin6_decoder;
  334. if (encoding.value().equals_ignoring_ascii_case("iso-8859-13"sv))
  335. return s_latin7_decoder;
  336. if (encoding.value().equals_ignoring_ascii_case("iso-8859-14"sv))
  337. return s_latin8_decoder;
  338. if (encoding.value().equals_ignoring_ascii_case("iso-8859-15"sv))
  339. return s_latin9_decoder;
  340. if (encoding.value().equals_ignoring_ascii_case("iso-8859-16"sv))
  341. return s_latin10_decoder;
  342. if (encoding.value().equals_ignoring_ascii_case("koi8-r"sv))
  343. return s_koi8r_decoder;
  344. if (encoding.value().equals_ignoring_ascii_case("koi8-u"sv))
  345. return s_koi8u_decoder;
  346. if (encoding.value().equals_ignoring_ascii_case("macintosh"sv))
  347. return s_mac_roman_decoder;
  348. if (encoding.value().equals_ignoring_ascii_case("PDFDocEncoding"sv))
  349. return s_pdf_doc_encoding_decoder;
  350. if (encoding.value().equals_ignoring_ascii_case("replacement"sv))
  351. return s_replacement_decoder;
  352. if (encoding.value().equals_ignoring_ascii_case("shift_jis"sv))
  353. return s_shift_jis_decoder;
  354. if (encoding.value().equals_ignoring_ascii_case("windows-874"sv))
  355. return s_windows874_decoder;
  356. if (encoding.value().equals_ignoring_ascii_case("windows-1250"sv))
  357. return s_centraleurope_decoder;
  358. if (encoding.value().equals_ignoring_ascii_case("windows-1251"sv))
  359. return s_cyrillic_decoder;
  360. if (encoding.value().equals_ignoring_ascii_case("windows-1253"sv))
  361. return s_windows1253_decoder;
  362. if (encoding.value().equals_ignoring_ascii_case("windows-1254"sv))
  363. return s_turkish_decoder;
  364. if (encoding.value().equals_ignoring_ascii_case("windows-1255"sv))
  365. return s_hebrew_decoder;
  366. if (encoding.value().equals_ignoring_ascii_case("windows-1256"sv))
  367. return s_windows1256_decoder;
  368. if (encoding.value().equals_ignoring_ascii_case("windows-1257"sv))
  369. return s_windows1257_decoder;
  370. if (encoding.value().equals_ignoring_ascii_case("windows-1258"sv))
  371. return s_windows1258_decoder;
  372. if (encoding.value().equals_ignoring_ascii_case("x-mac-cyrillic"sv))
  373. return s_mac_cyrillic_decoder;
  374. if (encoding.value().equals_ignoring_ascii_case("x-user-defined"sv))
  375. return s_x_user_defined_decoder;
  376. }
  377. dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
  378. return {};
  379. }
  380. // https://encoding.spec.whatwg.org/#concept-encoding-get
  381. Optional<StringView> get_standardized_encoding(StringView encoding)
  382. {
  383. // 1. Remove any leading and trailing ASCII whitespace from label.
  384. // 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.
  385. encoding = encoding.trim("\t\n\f\r "sv);
  386. // 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.
  387. 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))
  388. return "UTF-8"sv;
  389. if (encoding.is_one_of_ignoring_ascii_case("866"sv, "cp866"sv, "csibm866"sv, "ibm866"sv))
  390. return "IBM866"sv;
  391. 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))
  392. return "ISO-8859-2"sv;
  393. 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))
  394. return "ISO-8859-3"sv;
  395. 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))
  396. return "ISO-8859-4"sv;
  397. 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))
  398. return "ISO-8859-5"sv;
  399. 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))
  400. return "ISO-8859-6"sv;
  401. 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))
  402. return "ISO-8859-7"sv;
  403. 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))
  404. return "ISO-8859-8"sv;
  405. if (encoding.is_one_of_ignoring_ascii_case("csiso88598i"sv, "iso-8859-8-i"sv, "logical"sv))
  406. return "ISO-8859-8-I"sv;
  407. 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))
  408. return "ISO-8859-10"sv;
  409. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-13"sv, "iso8859-13"sv, "iso885913"sv))
  410. return "ISO-8859-13"sv;
  411. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-14"sv, "iso8859-14"sv, "iso885914"sv))
  412. return "ISO-8859-14"sv;
  413. 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))
  414. return "ISO-8859-15"sv;
  415. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-16"sv))
  416. return "ISO-8859-16"sv;
  417. if (encoding.is_one_of_ignoring_ascii_case("cskoi8r"sv, "koi"sv, "koi8"sv, "koi8-r"sv, "koi8_r"sv))
  418. return "KOI8-R"sv;
  419. if (encoding.is_one_of_ignoring_ascii_case("koi8-ru"sv, "koi8-u"sv))
  420. return "KOI8-U"sv;
  421. if (encoding.is_one_of_ignoring_ascii_case("csmacintosh"sv, "mac"sv, "macintosh"sv, "x-mac-roman"sv))
  422. return "macintosh"sv;
  423. if (encoding.is_one_of_ignoring_ascii_case("pdfdocencoding"sv))
  424. return "PDFDocEncoding"sv;
  425. 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))
  426. return "windows-874"sv;
  427. if (encoding.is_one_of_ignoring_ascii_case("cp1250"sv, "windows-1250"sv, "x-cp1250"sv))
  428. return "windows-1250"sv;
  429. if (encoding.is_one_of_ignoring_ascii_case("cp1251"sv, "windows-1251"sv, "x-cp1251"sv))
  430. return "windows-1251"sv;
  431. 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))
  432. return "windows-1252"sv;
  433. if (encoding.is_one_of_ignoring_ascii_case("cp1253"sv, "windows-1253"sv, "x-cp1253"sv))
  434. return "windows-1253"sv;
  435. 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))
  436. return "windows-1254"sv;
  437. if (encoding.is_one_of_ignoring_ascii_case("cp1255"sv, "windows-1255"sv, "x-cp1255"sv))
  438. return "windows-1255"sv;
  439. if (encoding.is_one_of_ignoring_ascii_case("cp1256"sv, "windows-1256"sv, "x-cp1256"sv))
  440. return "windows-1256"sv;
  441. if (encoding.is_one_of_ignoring_ascii_case("cp1257"sv, "windows-1257"sv, "x-cp1257"sv))
  442. return "windows-1257"sv;
  443. if (encoding.is_one_of_ignoring_ascii_case("cp1258"sv, "windows-1258"sv, "x-cp1258"sv))
  444. return "windows-1258"sv;
  445. if (encoding.is_one_of_ignoring_ascii_case("x-mac-cyrillic"sv, "x-mac-ukrainian"sv))
  446. return "x-mac-cyrillic"sv;
  447. if (encoding.is_one_of_ignoring_ascii_case("koi8-r"sv, "koi8r"sv))
  448. return "koi8-r"sv;
  449. 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))
  450. return "GBK"sv;
  451. if (encoding.is_one_of_ignoring_ascii_case("gb18030"sv))
  452. return "gb18030"sv;
  453. if (encoding.is_one_of_ignoring_ascii_case("big5"sv, "big5-hkscs"sv, "cn-big5"sv, "csbig5"sv, "x-x-big5"sv))
  454. return "Big5"sv;
  455. if (encoding.is_one_of_ignoring_ascii_case("cseucpkdfmtjapanese"sv, "euc-jp"sv, "x-euc-jp"sv))
  456. return "EUC-JP"sv;
  457. if (encoding.is_one_of_ignoring_ascii_case("csiso2022jp"sv, "iso-2022-jp"sv))
  458. return "ISO-2022-JP"sv;
  459. 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))
  460. return "Shift_JIS"sv;
  461. 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))
  462. return "EUC-KR"sv;
  463. 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))
  464. return "replacement"sv;
  465. if (encoding.is_one_of_ignoring_ascii_case("unicodefffe"sv, "utf-16be"sv))
  466. return "UTF-16BE"sv;
  467. 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))
  468. return "UTF-16LE"sv;
  469. if (encoding.is_one_of_ignoring_ascii_case("x-user-defined"sv))
  470. return "x-user-defined"sv;
  471. dbgln("TextCodec: Unrecognized encoding: {}", encoding);
  472. return {};
  473. }
  474. // https://encoding.spec.whatwg.org/#bom-sniff
  475. Optional<Decoder&> bom_sniff_to_decoder(StringView input)
  476. {
  477. // 1. Let BOM be the result of peeking 3 bytes from ioQueue, converted to a byte sequence.
  478. // 2. For each of the rows in the table below, starting with the first one and going down,
  479. // if BOM starts with the bytes given in the first column, then return the encoding given
  480. // in the cell in the second column of that row. Otherwise, return null.
  481. // Byte Order Mark | Encoding
  482. // --------------------------
  483. // 0xEF 0xBB 0xBF | UTF-8
  484. // 0xFE 0xFF | UTF-16BE
  485. // 0xFF 0xFE | UTF-16LE
  486. auto bytes = input.bytes();
  487. if (bytes.size() < 2)
  488. return {};
  489. auto first_byte = bytes[0];
  490. switch (first_byte) {
  491. case 0xEF: // UTF-8
  492. if (bytes.size() < 3)
  493. return {};
  494. if (bytes[1] == 0xBB && bytes[2] == 0xBF)
  495. return s_utf8_decoder;
  496. return {};
  497. case 0xFE: // UTF-16BE
  498. if (bytes[1] == 0xFF)
  499. return s_utf16be_decoder;
  500. return {};
  501. case 0xFF: // UTF-16LE
  502. if (bytes[1] == 0xFE)
  503. return s_utf16le_decoder;
  504. return {};
  505. }
  506. return {};
  507. }
  508. // https://encoding.spec.whatwg.org/#decode
  509. ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder& fallback_decoder, StringView input)
  510. {
  511. Decoder* actual_decoder = &fallback_decoder;
  512. // 1. Let BOMEncoding be the result of BOM sniffing ioQueue.
  513. // 2. If BOMEncoding is non-null:
  514. if (auto unicode_decoder = bom_sniff_to_decoder(input); unicode_decoder.has_value()) {
  515. // 1. Set encoding to BOMEncoding.
  516. actual_decoder = &unicode_decoder.value();
  517. // 2. Read three bytes from ioQueue, if BOMEncoding is UTF-8; otherwise read two bytes. (Do nothing with those bytes.)
  518. // FIXME: I imagine this will be pretty slow for large inputs, as it's regenerating the input without the first 2/3 bytes.
  519. input = input.substring_view(&unicode_decoder.value() == &s_utf8_decoder ? 3 : 2);
  520. }
  521. VERIFY(actual_decoder);
  522. // 3. Process a queue with an instance of encoding’s decoder, ioQueue, output, and "replacement".
  523. // FIXME: This isn't the exact same as the spec, which is written in terms of I/O queues.
  524. auto output = TRY(actual_decoder->to_utf8(input));
  525. // 4. Return output.
  526. return output;
  527. }
  528. // https://encoding.spec.whatwg.org/#get-an-output-encoding
  529. StringView get_output_encoding(StringView encoding)
  530. {
  531. // 1. If encoding is replacement or UTF-16BE/LE, then return UTF-8.
  532. if (encoding.is_one_of_ignoring_ascii_case("replacement"sv, "utf-16le"sv, "utf-16be"sv))
  533. return "UTF-8"sv;
  534. // 2. Return encoding.
  535. return encoding;
  536. }
  537. bool Decoder::validate(StringView input)
  538. {
  539. auto result = this->process(input, [](auto code_point) -> ErrorOr<void> {
  540. if (code_point == replacement_code_point)
  541. return Error::from_errno(EINVAL);
  542. return {};
  543. });
  544. return !result.is_error();
  545. }
  546. ErrorOr<String> Decoder::to_utf8(StringView input)
  547. {
  548. StringBuilder builder(input.length());
  549. TRY(process(input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  550. return builder.to_string_without_validation();
  551. }
  552. ErrorOr<void> UTF8Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  553. {
  554. for (auto c : Utf8View(input)) {
  555. TRY(on_code_point(c));
  556. }
  557. return {};
  558. }
  559. bool UTF8Decoder::validate(StringView input)
  560. {
  561. return Utf8View(input).validate();
  562. }
  563. ErrorOr<String> UTF8Decoder::to_utf8(StringView input)
  564. {
  565. // Discard the BOM
  566. auto bomless_input = input;
  567. if (auto bytes = input.bytes(); bytes.size() >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
  568. bomless_input = input.substring_view(3);
  569. }
  570. return Decoder::to_utf8(bomless_input);
  571. }
  572. ErrorOr<void> UTF16BEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  573. {
  574. // rfc2781, 2.2 Decoding UTF-16
  575. size_t utf16_length = input.length() - (input.length() % 2);
  576. for (size_t i = 0; i < utf16_length; i += 2) {
  577. // 1) If W1 < 0xD800 or W1 > 0xDFFF, the character value U is the value
  578. // of W1. Terminate.
  579. u16 w1 = (static_cast<u8>(input[i]) << 8) | static_cast<u8>(input[i + 1]);
  580. if (!is_unicode_surrogate(w1)) {
  581. TRY(on_code_point(w1));
  582. continue;
  583. }
  584. // 2) Determine if W1 is between 0xD800 and 0xDBFF. If not, the sequence
  585. // is in error and no valid character can be obtained using W1.
  586. // Terminate.
  587. // 3) If there is no W2 (that is, the sequence ends with W1), or if W2
  588. // is not between 0xDC00 and 0xDFFF, the sequence is in error.
  589. // Terminate.
  590. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length) {
  591. TRY(on_code_point(replacement_code_point));
  592. continue;
  593. }
  594. u16 w2 = (static_cast<u8>(input[i + 2]) << 8) | static_cast<u8>(input[i + 3]);
  595. if (!Utf16View::is_low_surrogate(w2)) {
  596. TRY(on_code_point(replacement_code_point));
  597. continue;
  598. }
  599. // 4) Construct a 20-bit unsigned integer U', taking the 10 low-order
  600. // bits of W1 as its 10 high-order bits and the 10 low-order bits of
  601. // W2 as its 10 low-order bits.
  602. // 5) Add 0x10000 to U' to obtain the character value U. Terminate.
  603. TRY(on_code_point(Utf16View::decode_surrogate_pair(w1, w2)));
  604. i += 2;
  605. }
  606. return {};
  607. }
  608. bool UTF16BEDecoder::validate(StringView input)
  609. {
  610. size_t utf16_length = input.length() - (input.length() % 2);
  611. for (size_t i = 0; i < utf16_length; i += 2) {
  612. u16 w1 = (static_cast<u8>(input[i]) << 8) | static_cast<u8>(input[i + 1]);
  613. if (!is_unicode_surrogate(w1))
  614. continue;
  615. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length)
  616. return false;
  617. u16 w2 = (static_cast<u8>(input[i + 2]) << 8) | static_cast<u8>(input[i + 3]);
  618. if (!Utf16View::is_low_surrogate(w2))
  619. return false;
  620. i += 2;
  621. }
  622. return true;
  623. }
  624. ErrorOr<String> UTF16BEDecoder::to_utf8(StringView input)
  625. {
  626. // Discard the BOM
  627. auto bomless_input = input;
  628. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF)
  629. bomless_input = input.substring_view(2);
  630. StringBuilder builder(bomless_input.length() / 2);
  631. TRY(process(bomless_input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  632. return builder.to_string();
  633. }
  634. ErrorOr<void> UTF16LEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  635. {
  636. // rfc2781, 2.2 Decoding UTF-16
  637. size_t utf16_length = input.length() - (input.length() % 2);
  638. for (size_t i = 0; i < utf16_length; i += 2) {
  639. // 1) If W1 < 0xD800 or W1 > 0xDFFF, the character value U is the value
  640. // of W1. Terminate.
  641. u16 w1 = static_cast<u8>(input[i]) | (static_cast<u8>(input[i + 1]) << 8);
  642. if (!is_unicode_surrogate(w1)) {
  643. TRY(on_code_point(w1));
  644. continue;
  645. }
  646. // 2) Determine if W1 is between 0xD800 and 0xDBFF. If not, the sequence
  647. // is in error and no valid character can be obtained using W1.
  648. // Terminate.
  649. // 3) If there is no W2 (that is, the sequence ends with W1), or if W2
  650. // is not between 0xDC00 and 0xDFFF, the sequence is in error.
  651. // Terminate.
  652. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length) {
  653. TRY(on_code_point(replacement_code_point));
  654. continue;
  655. }
  656. u16 w2 = static_cast<u8>(input[i + 2]) | (static_cast<u8>(input[i + 3]) << 8);
  657. if (!Utf16View::is_low_surrogate(w2)) {
  658. TRY(on_code_point(replacement_code_point));
  659. continue;
  660. }
  661. // 4) Construct a 20-bit unsigned integer U', taking the 10 low-order
  662. // bits of W1 as its 10 high-order bits and the 10 low-order bits of
  663. // W2 as its 10 low-order bits.
  664. // 5) Add 0x10000 to U' to obtain the character value U. Terminate.
  665. TRY(on_code_point(Utf16View::decode_surrogate_pair(w1, w2)));
  666. i += 2;
  667. }
  668. return {};
  669. }
  670. bool UTF16LEDecoder::validate(StringView input)
  671. {
  672. size_t utf16_length = input.length() - (input.length() % 2);
  673. for (size_t i = 0; i < utf16_length; i += 2) {
  674. u16 w1 = static_cast<u8>(input[i]) | (static_cast<u8>(input[i + 1]) << 8);
  675. if (!is_unicode_surrogate(w1))
  676. continue;
  677. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length)
  678. return false;
  679. u16 w2 = static_cast<u8>(input[i + 2]) | (static_cast<u8>(input[i + 3]) << 8);
  680. if (!Utf16View::is_low_surrogate(w2))
  681. return false;
  682. i += 2;
  683. }
  684. return true;
  685. }
  686. ErrorOr<String> UTF16LEDecoder::to_utf8(StringView input)
  687. {
  688. // Discard the BOM
  689. auto bomless_input = input;
  690. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE)
  691. bomless_input = input.substring_view(2);
  692. StringBuilder builder(bomless_input.length() / 2);
  693. TRY(process(bomless_input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  694. return builder.to_string();
  695. }
  696. ErrorOr<void> Latin1Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  697. {
  698. for (u8 ch : input) {
  699. // Latin1 is the same as the first 256 Unicode code_points, so no mapping is needed, just utf-8 encoding.
  700. TRY(on_code_point(ch));
  701. }
  702. return {};
  703. }
  704. namespace {
  705. u32 convert_latin2_to_utf8(u8 in)
  706. {
  707. switch (in) {
  708. #define MAP(X, Y) \
  709. case X: \
  710. return Y
  711. MAP(0xA1, 0x104);
  712. MAP(0xA2, 0x2D8);
  713. MAP(0xA3, 0x141);
  714. MAP(0xA5, 0x13D);
  715. MAP(0xA6, 0x15A);
  716. MAP(0xA9, 0x160);
  717. MAP(0xAA, 0x15E);
  718. MAP(0xAB, 0x164);
  719. MAP(0xAC, 0x179);
  720. MAP(0xAE, 0x17D);
  721. MAP(0xAF, 0x17B);
  722. MAP(0xB1, 0x105);
  723. MAP(0xB2, 0x2DB);
  724. MAP(0xB3, 0x142);
  725. MAP(0xB5, 0x13E);
  726. MAP(0xB6, 0x15B);
  727. MAP(0xB7, 0x2C7);
  728. MAP(0xB9, 0x161);
  729. MAP(0xBA, 0x15F);
  730. MAP(0xBB, 0x165);
  731. MAP(0xBC, 0x17A);
  732. MAP(0xBD, 0x2DD);
  733. MAP(0xBE, 0x17E);
  734. MAP(0xBF, 0x17C);
  735. MAP(0xC0, 0x154);
  736. MAP(0xC3, 0x102);
  737. MAP(0xC5, 0x139);
  738. MAP(0xC6, 0x106);
  739. MAP(0xC8, 0x10C);
  740. MAP(0xCA, 0x118);
  741. MAP(0xCC, 0x11A);
  742. MAP(0xCF, 0x10E);
  743. MAP(0xD0, 0x110);
  744. MAP(0xD1, 0x143);
  745. MAP(0xD2, 0x147);
  746. MAP(0xD5, 0x150);
  747. MAP(0xD8, 0x158);
  748. MAP(0xD9, 0x16E);
  749. MAP(0xDB, 0x170);
  750. MAP(0xDE, 0x162);
  751. MAP(0xE0, 0x155);
  752. MAP(0xE3, 0x103);
  753. MAP(0xE5, 0x13A);
  754. MAP(0xE6, 0x107);
  755. MAP(0xE8, 0x10D);
  756. MAP(0xEA, 0x119);
  757. MAP(0xEC, 0x11B);
  758. MAP(0xEF, 0x10F);
  759. MAP(0xF0, 0x111);
  760. MAP(0xF1, 0x144);
  761. MAP(0xF2, 0x148);
  762. MAP(0xF5, 0x151);
  763. MAP(0xF8, 0x159);
  764. MAP(0xF9, 0x16F);
  765. MAP(0xFB, 0x171);
  766. MAP(0xFE, 0x163);
  767. MAP(0xFF, 0x2D9);
  768. #undef MAP
  769. default:
  770. return in;
  771. }
  772. }
  773. }
  774. ErrorOr<void> Latin2Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  775. {
  776. for (auto c : input) {
  777. TRY(on_code_point(convert_latin2_to_utf8(c)));
  778. }
  779. return {};
  780. }
  781. ErrorOr<void> Latin9Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  782. {
  783. auto convert_latin9_to_utf8 = [](u8 ch) -> u32 {
  784. // Latin9 is the same as the first 256 Unicode code points, except for 8 characters.
  785. switch (ch) {
  786. case 0xA4:
  787. return 0x20AC;
  788. case 0xA6:
  789. return 0x160;
  790. case 0xA8:
  791. return 0x161;
  792. case 0xB4:
  793. return 0x17D;
  794. case 0xB8:
  795. return 0x17E;
  796. case 0xBC:
  797. return 0x152;
  798. case 0xBD:
  799. return 0x153;
  800. case 0xBE:
  801. return 0x178;
  802. default:
  803. return ch;
  804. }
  805. };
  806. for (auto ch : input) {
  807. TRY(on_code_point(convert_latin9_to_utf8(ch)));
  808. }
  809. return {};
  810. }
  811. ErrorOr<void> PDFDocEncodingDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  812. {
  813. // PDF 1.7 spec, Appendix D.2 "PDFDocEncoding Character Set"
  814. // Character codes 0-8, 11-12, 14-23, 127, 159, 173 are not defined per spec.
  815. // clang-format off
  816. static constexpr Array<u32, 256> translation_table = {
  817. 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
  818. 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
  819. 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0017, 0x0017,
  820. 0x02D8, 0x02C7, 0x02C6, 0x02D9, 0x02DD, 0x02DB, 0x02DA, 0x02DC,
  821. 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
  822. 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
  823. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
  824. 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
  825. 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
  826. 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
  827. 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
  828. 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
  829. 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
  830. 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
  831. 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
  832. 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0xFFFC,
  833. 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x0192, 0x2044,
  834. 0x2039, 0x203A, 0x2212, 0x2030, 0x201E, 0x201C, 0x201D, 0x2018,
  835. 0x2019, 0x201A, 0x2122, 0xFB01, 0xFB02, 0x0141, 0x0152, 0x0160,
  836. 0x0178, 0x017D, 0x0131, 0x0142, 0x0153, 0x0161, 0x017E, 0xFFFC,
  837. 0x20AC, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
  838. 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0xFFFC, 0x00AE, 0x00AF,
  839. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
  840. 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  841. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  842. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  843. 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
  844. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
  845. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  846. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  847. 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
  848. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF,
  849. };
  850. // clang-format on
  851. for (u8 ch : input)
  852. TRY(on_code_point(translation_table[ch]));
  853. return {};
  854. }
  855. ErrorOr<void> TurkishDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  856. {
  857. auto convert_turkish_to_utf8 = [](u8 ch) -> u32 {
  858. // Turkish (aka ISO-8859-9, Windows-1254) is the same as the first 256 Unicode code points, except for 6 characters.
  859. switch (ch) {
  860. case 0xD0:
  861. return 0x11E;
  862. case 0xDD:
  863. return 0x130;
  864. case 0xDE:
  865. return 0x15E;
  866. case 0xF0:
  867. return 0x11F;
  868. case 0xFD:
  869. return 0x131;
  870. case 0xFE:
  871. return 0x15F;
  872. default:
  873. return ch;
  874. }
  875. };
  876. for (auto ch : input) {
  877. TRY(on_code_point(convert_turkish_to_utf8(ch)));
  878. }
  879. return {};
  880. }
  881. // https://encoding.spec.whatwg.org/#x-user-defined-decoder
  882. ErrorOr<void> XUserDefinedDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  883. {
  884. auto convert_x_user_defined_to_utf8 = [](u8 ch) -> u32 {
  885. // 2. If byte is an ASCII byte, return a code point whose value is byte.
  886. // https://infra.spec.whatwg.org/#ascii-byte
  887. // An ASCII byte is a byte in the range 0x00 (NUL) to 0x7F (DEL), inclusive.
  888. // NOTE: This doesn't check for ch >= 0x00, as that would always be true due to being unsigned.
  889. if (ch <= 0x7f)
  890. return ch;
  891. // 3. Return a code point whose value is 0xF780 + byte − 0x80.
  892. return 0xF780 + ch - 0x80;
  893. };
  894. for (auto ch : input) {
  895. TRY(on_code_point(convert_x_user_defined_to_utf8(ch)));
  896. }
  897. // 1. If byte is end-of-queue, return finished.
  898. return {};
  899. }
  900. // https://encoding.spec.whatwg.org/#single-byte-decoder
  901. ErrorOr<void> SingleByteDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  902. {
  903. for (u8 const byte : input) {
  904. if (byte < 0x80) {
  905. // 2. If byte is an ASCII byte, return a code point whose value is byte.
  906. TRY(on_code_point(byte));
  907. } else {
  908. // 3. Let code point be the index code point for byte − 0x80 in index single-byte.
  909. auto code_point = m_translation_table[byte - 0x80];
  910. // 4. If code point is null, return error.
  911. // NOTE: Error is communicated with 0xFFFD
  912. // 5. Return a code point whose value is code point.
  913. TRY(on_code_point(code_point));
  914. }
  915. }
  916. // 1. If byte is end-of-queue, return finished.
  917. return {};
  918. }
  919. // https://encoding.spec.whatwg.org/#index-gb18030-ranges-code-point
  920. static Optional<u32> index_gb18030_ranges_code_point(u32 pointer)
  921. {
  922. // 1. If pointer is greater than 39419 and less than 189000, or pointer is greater than 1237575, return null.
  923. if ((pointer > 39419 && pointer < 189000) || pointer > 1237575)
  924. return {};
  925. // 2. If pointer is 7457, return code point U+E7C7.
  926. if (pointer == 7457)
  927. return 0xE7C7;
  928. // FIXME: Encoding specification is not updated to GB-18030-2022 yet (https://github.com/whatwg/encoding/issues/312)
  929. // NOTE: This matches https://commits.webkit.org/266173@main
  930. switch (pointer) {
  931. case 19057:
  932. return 0xE81E; // 82 35 90 37
  933. case 19058:
  934. return 0xE826; // 82 35 90 38
  935. case 19059:
  936. return 0xE82B; // 82 35 90 39
  937. case 19060:
  938. return 0xE82C; // 82 35 91 30
  939. case 19061:
  940. return 0xE832; // 82 35 91 31
  941. case 19062:
  942. return 0xE843; // 82 35 91 32
  943. case 19063:
  944. return 0xE854; // 82 35 91 33
  945. case 19064:
  946. return 0xE864; // 82 35 91 34
  947. case 39076:
  948. return 0xE78D; // 84 31 82 36
  949. case 39077:
  950. return 0xE78F; // 84 31 82 37
  951. case 39078:
  952. return 0xE78E; // 84 31 82 38
  953. case 39079:
  954. return 0xE790; // 84 31 82 39
  955. case 39080:
  956. return 0xE791; // 84 31 83 30
  957. case 39081:
  958. return 0xE792; // 84 31 83 31
  959. case 39082:
  960. return 0xE793; // 84 31 83 32
  961. case 39083:
  962. return 0xE794; // 84 31 83 33
  963. case 39084:
  964. return 0xE795; // 84 31 83 34
  965. case 39085:
  966. return 0xE796; // 84 31 83 35
  967. default:
  968. break;
  969. }
  970. // 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.
  971. size_t last_index;
  972. binary_search(s_gb18030_ranges, pointer, &last_index, [](auto const pointer, auto const& entry) {
  973. return pointer - entry.pointer;
  974. });
  975. auto offset = s_gb18030_ranges[last_index].pointer;
  976. auto code_point_offset = s_gb18030_ranges[last_index].code_point;
  977. // 4. Return a code point whose value is code point offset + pointer − offset.
  978. return code_point_offset + pointer - offset;
  979. }
  980. // https://encoding.spec.whatwg.org/#gb18030-decoder
  981. ErrorOr<void> GB18030Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  982. {
  983. // gb18030’s decoder has an associated gb18030 first, gb18030 second, and gb18030 third (all initially 0x00).
  984. u8 first = 0x00;
  985. u8 second = 0x00;
  986. u8 third = 0x00;
  987. // gb18030’s decoder’s handler, given ioQueue and byte, runs these steps:
  988. size_t index = 0;
  989. while (true) {
  990. // 1. If byte is end-of-queue and gb18030 first, gb18030 second, and gb18030 third are 0x00, return finished.
  991. if (index >= input.length() && first == 0x00 && second == 0x00 && third == 0x00)
  992. return {};
  993. // 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.
  994. if (index >= input.length() && (first != 0x00 || second != 0x00 || third != 0x00)) {
  995. first = 0x00;
  996. second = 0x00;
  997. third = 0x00;
  998. TRY(on_code_point(replacement_code_point));
  999. continue;
  1000. }
  1001. u8 const byte = input[index++];
  1002. // 3. If gb18030 third is not 0x00, then:
  1003. if (third != 0x00) {
  1004. // 1. If byte is not in the range 0x30 to 0x39, inclusive, then:
  1005. if (byte < 0x30 || byte > 0x39) {
  1006. // 1. Restore « gb18030 second, gb18030 third, byte » to ioQueue.
  1007. index -= 3;
  1008. // 2. Set gb18030 first, gb18030 second, and gb18030 third to 0x00.
  1009. first = 0x00;
  1010. second = 0x00;
  1011. third = 0x00;
  1012. // 3. Return error.
  1013. TRY(on_code_point(replacement_code_point));
  1014. continue;
  1015. }
  1016. // 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.
  1017. auto code_point = index_gb18030_ranges_code_point(((first - 0x81) * (10 * 126 * 10)) + ((second - 0x30) * (10 * 126)) + ((third - 0x81) * 10) + byte - 0x30);
  1018. // 3. Set gb18030 first, gb18030 second, and gb18030 third to 0x00.
  1019. first = 0x00;
  1020. second = 0x00;
  1021. third = 0x00;
  1022. // 4. If code point is null, return error.
  1023. if (!code_point.has_value()) {
  1024. TRY(on_code_point(replacement_code_point));
  1025. continue;
  1026. }
  1027. // 5. Return a code point whose value is code point.
  1028. TRY(on_code_point(code_point.value()));
  1029. continue;
  1030. }
  1031. // 4. If gb18030 second is not 0x00, then:
  1032. if (second != 0x00) {
  1033. // 1. If byte is in the range 0x81 to 0xFE, inclusive, set gb18030 third to byte and return continue.
  1034. if (byte >= 0x81 && byte <= 0xFE) {
  1035. third = byte;
  1036. continue;
  1037. }
  1038. // 2. Restore « gb18030 second, byte » to ioQueue, set gb18030 first and gb18030 second to 0x00, and return error.
  1039. index -= 2;
  1040. first = 0x00;
  1041. second = 0x00;
  1042. TRY(on_code_point(replacement_code_point));
  1043. continue;
  1044. }
  1045. // 5. If gb18030 first is not 0x00, then:
  1046. if (first != 0x00) {
  1047. // 1. If byte is in the range 0x30 to 0x39, inclusive, set gb18030 second to byte and return continue.
  1048. if (byte >= 0x30 && byte <= 0x39) {
  1049. second = byte;
  1050. continue;
  1051. }
  1052. // 2. Let lead be gb18030 first, let pointer be null, and set gb18030 first to 0x00.
  1053. auto lead = first;
  1054. Optional<u32> pointer;
  1055. first = 0x00;
  1056. // 3. Let offset be 0x40 if byte is less than 0x7F, otherwise 0x41.
  1057. u8 const offset = byte < 0x7F ? 0x40 : 0x41;
  1058. // 4. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80 to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 + (byte − offset).
  1059. if ((byte >= 0x40 && byte <= 0x7E) || (byte >= 0x80 && byte <= 0xFE))
  1060. pointer = (lead - 0x81) * 190 + (byte - offset);
  1061. // 5. Let code point be null if pointer is null, otherwise the index code point for pointer in index gb18030.
  1062. auto code_point = pointer.has_value() ? index_gb18030_code_point(pointer.value()) : Optional<u32> {};
  1063. // 6. If code point is non-null, return a code point whose value is code point.
  1064. if (code_point.has_value()) {
  1065. TRY(on_code_point(code_point.value()));
  1066. continue;
  1067. }
  1068. // 7. If byte is an ASCII byte, restore byte to ioQueue.
  1069. if (byte <= 0x7F)
  1070. index--;
  1071. // 8. Return error.
  1072. TRY(on_code_point(replacement_code_point));
  1073. continue;
  1074. }
  1075. // 6. If byte is an ASCII byte, return a code point whose value is byte.
  1076. if (byte <= 0x7F) {
  1077. TRY(on_code_point(byte));
  1078. continue;
  1079. }
  1080. // 7. If byte is 0x80, return code point U+20AC.
  1081. if (byte == 0x80) {
  1082. TRY(on_code_point(0x20AC));
  1083. continue;
  1084. }
  1085. // 8. If byte is in the range 0x81 to 0xFE, inclusive, set gb18030 first to byte and return continue.
  1086. if (byte >= 0x81 && byte <= 0xFE) {
  1087. first = byte;
  1088. continue;
  1089. }
  1090. // 9. Return error.
  1091. TRY(on_code_point(replacement_code_point));
  1092. }
  1093. }
  1094. // https://encoding.spec.whatwg.org/#big5-decoder
  1095. ErrorOr<void> Big5Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  1096. {
  1097. // Big5’s decoder has an associated Big5 lead (initially 0x00).
  1098. u8 big5_lead = 0x00;
  1099. // Big5’s decoder’s handler, given ioQueue and byte, runs these steps:
  1100. size_t index = 0;
  1101. while (true) {
  1102. // 1. If byte is end-of-queue and Big5 lead is not 0x00, set Big5 lead to 0x00 and return error.
  1103. if (index >= input.length() && big5_lead != 0x00) {
  1104. big5_lead = 0x00;
  1105. TRY(on_code_point(replacement_code_point));
  1106. continue;
  1107. }
  1108. // 2. If byte is end-of-queue and Big5 lead is 0x00, return finished.
  1109. if (index >= input.length() && big5_lead == 0x00)
  1110. return {};
  1111. u8 const byte = input[index++];
  1112. // 3. If Big5 lead is not 0x00, let lead be Big5 lead, let pointer be null, set Big5 lead to 0x00, and then:
  1113. if (big5_lead != 0x00) {
  1114. auto lead = big5_lead;
  1115. Optional<u32> pointer;
  1116. big5_lead = 0x00;
  1117. // 1. Let offset be 0x40 if byte is less than 0x7F, otherwise 0x62.
  1118. u8 const offset = byte < 0x7F ? 0x40 : 0x62;
  1119. // 2. If byte is in the range 0x40 to 0x7E, inclusive, or 0xA1 to 0xFE, inclusive, set pointer to (lead − 0x81) × 157 + (byte − offset).
  1120. if ((byte >= 0x40 && byte <= 0x7E) || (byte >= 0xA1 && byte <= 0xFE))
  1121. pointer = (lead - 0x81) * 157 + (byte - offset);
  1122. // 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):
  1123. if (pointer.has_value() && pointer.value() == 1133) {
  1124. TRY(on_code_point(0x00CA));
  1125. TRY(on_code_point(0x0304));
  1126. continue;
  1127. }
  1128. if (pointer.has_value() && pointer.value() == 1135) {
  1129. TRY(on_code_point(0x00CA));
  1130. TRY(on_code_point(0x030C));
  1131. continue;
  1132. }
  1133. if (pointer.has_value() && pointer.value() == 1164) {
  1134. TRY(on_code_point(0x00EA));
  1135. TRY(on_code_point(0x0304));
  1136. continue;
  1137. }
  1138. if (pointer.has_value() && pointer.value() == 1166) {
  1139. TRY(on_code_point(0x00EA));
  1140. TRY(on_code_point(0x030C));
  1141. continue;
  1142. }
  1143. // 4. Let code point be null if pointer is null, otherwise the index code point for pointer in index Big5.
  1144. auto code_pointer = pointer.has_value() ? index_big5_code_point(pointer.value()) : Optional<u32> {};
  1145. // 5. If code point is non-null, return a code point whose value is code point.
  1146. if (code_pointer.has_value()) {
  1147. TRY(on_code_point(code_pointer.value()));
  1148. continue;
  1149. }
  1150. // 6. If byte is an ASCII byte, restore byte to ioQueue.
  1151. if (byte <= 0x7F)
  1152. index--;
  1153. // 7. Return error.
  1154. TRY(on_code_point(replacement_code_point));
  1155. continue;
  1156. }
  1157. // 4. If byte is an ASCII byte, return a code point whose value is byte.
  1158. if (byte <= 0x7F) {
  1159. TRY(on_code_point(byte));
  1160. continue;
  1161. }
  1162. // 5. If byte is in the range 0x81 to 0xFE, inclusive, set Big5 lead to byte and return continue.
  1163. if (byte >= 0x81 && byte <= 0xFE) {
  1164. big5_lead = byte;
  1165. continue;
  1166. }
  1167. // 6. Return error
  1168. TRY(on_code_point(replacement_code_point));
  1169. }
  1170. }
  1171. // https://encoding.spec.whatwg.org/#euc-jp-decoder
  1172. ErrorOr<void> EUCJPDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  1173. {
  1174. // EUC-JP’s decoder has an associated EUC-JP jis0212 (initially false) and EUC-JP lead (initially 0x00).
  1175. bool jis0212 = false;
  1176. u8 euc_jp_lead = 0x00;
  1177. // EUC-JP’s decoder’s handler, given ioQueue and byte, runs these steps:
  1178. size_t index = 0;
  1179. while (true) {
  1180. // 1. If byte is end-of-queue and EUC-JP lead is not 0x00, set EUC-JP lead to 0x00, and return error.
  1181. if (index >= input.length() && euc_jp_lead != 0x00) {
  1182. euc_jp_lead = 0x00;
  1183. TRY(on_code_point(replacement_code_point));
  1184. continue;
  1185. }
  1186. // 2. If byte is end-of-queue and EUC-JP lead is 0x00, return finished.
  1187. if (index >= input.length() && euc_jp_lead == 0x00)
  1188. return {};
  1189. u8 const byte = input[index++];
  1190. // 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.
  1191. if (euc_jp_lead == 0x8E && byte >= 0xA1 && byte <= 0xDF) {
  1192. euc_jp_lead = 0x00;
  1193. TRY(on_code_point(0xFF61 - 0xA1 + byte));
  1194. continue;
  1195. }
  1196. // 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.
  1197. if (euc_jp_lead == 0x8F && byte >= 0xA1 && byte <= 0xFE) {
  1198. jis0212 = true;
  1199. euc_jp_lead = byte;
  1200. continue;
  1201. }
  1202. // 5. If EUC-JP lead is not 0x00, let lead be EUC-JP lead, set EUC-JP lead to 0x00, and then:
  1203. if (euc_jp_lead != 0x00) {
  1204. auto lead = euc_jp_lead;
  1205. euc_jp_lead = 0x00;
  1206. // 1. Let code point be null.
  1207. Optional<u32> code_point;
  1208. // 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.
  1209. if (lead >= 0xA1 && lead <= 0xFE && byte >= 0xA1 && byte <= 0xFE) {
  1210. auto pointer = (lead - 0xA1) * 94 + byte - 0xA1;
  1211. code_point = jis0212 ? index_jis0212_code_point(pointer) : index_jis0208_code_point(pointer);
  1212. }
  1213. // 3. Set EUC-JP jis0212 to false.
  1214. jis0212 = false;
  1215. // 4. If code point is non-null, return a code point whose value is code point.
  1216. if (code_point.has_value()) {
  1217. TRY(on_code_point(code_point.value()));
  1218. continue;
  1219. }
  1220. // 5. If byte is an ASCII byte, restore byte to ioQueue.
  1221. if (byte <= 0x7F)
  1222. index--;
  1223. // 6. Return error.
  1224. TRY(on_code_point(replacement_code_point));
  1225. continue;
  1226. }
  1227. // 6. If byte is an ASCII byte, return a code point whose value is byte.
  1228. if (byte <= 0x7F) {
  1229. TRY(on_code_point(byte));
  1230. continue;
  1231. }
  1232. // 7. If byte is 0x8E, 0x8F, or in the range 0xA1 to 0xFE, inclusive, set EUC-JP lead to byte and return continue.
  1233. if (byte == 0x8E || byte == 0x8F || (byte >= 0xA1 && byte <= 0xFE)) {
  1234. euc_jp_lead = byte;
  1235. continue;
  1236. }
  1237. // 8. Return error.
  1238. TRY(on_code_point(replacement_code_point));
  1239. }
  1240. }
  1241. enum class ISO2022JPState {
  1242. ASCII,
  1243. Roman,
  1244. Katakana,
  1245. LeadByte,
  1246. TrailByte,
  1247. EscapeStart,
  1248. Escape,
  1249. };
  1250. // https://encoding.spec.whatwg.org/#iso-2022-jp-decoder
  1251. ErrorOr<void> ISO2022JPDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  1252. {
  1253. // 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).
  1254. auto decoder_state = ISO2022JPState::ASCII;
  1255. auto output_state = ISO2022JPState::ASCII;
  1256. u8 iso2022_jp_lead = 0x00;
  1257. bool iso2022_jp_output = false;
  1258. size_t index = 0;
  1259. while (true) {
  1260. Optional<u8> byte;
  1261. if (index < input.length())
  1262. byte = input[index++];
  1263. // ISO-2022-JP’s decoder’s handler, given ioQueue and byte, runs these steps, switching on ISO-2022-JP decoder state:
  1264. switch (decoder_state) {
  1265. case ISO2022JPState::ASCII:
  1266. // Based on byte:
  1267. // 0x1B: Set ISO-2022-JP decoder state to escape start and return continue.
  1268. if (byte == 0x1B) {
  1269. decoder_state = ISO2022JPState::EscapeStart;
  1270. continue;
  1271. }
  1272. // 0x00 to 0x7F, excluding 0x0E, 0x0F, and 0x1B: Set ISO-2022-JP output to false and return a code point whose value is byte.
  1273. if (byte.has_value() && byte.value() <= 0x7F && byte != 0x0E && byte != 0x0F) {
  1274. iso2022_jp_output = false;
  1275. TRY(on_code_point(byte.value()));
  1276. continue;
  1277. }
  1278. // end-of-queue: Return finished.
  1279. if (!byte.has_value())
  1280. return {};
  1281. // Otherwise: Set ISO-2022-JP output to false and return error.
  1282. iso2022_jp_output = false;
  1283. TRY(on_code_point(replacement_code_point));
  1284. break;
  1285. case ISO2022JPState::Roman:
  1286. // Based on byte:
  1287. // 0x1B: Set ISO-2022-JP decoder state to escape start and return continue.
  1288. if (byte == 0x1B) {
  1289. decoder_state = ISO2022JPState::EscapeStart;
  1290. continue;
  1291. }
  1292. // 0x5C: Set ISO-2022-JP output to false and return code point U+00A5.
  1293. if (byte == 0x5C) {
  1294. iso2022_jp_output = false;
  1295. TRY(on_code_point(0x00A5));
  1296. continue;
  1297. }
  1298. // 0x7E: Set ISO-2022-JP output to false and return code point U+203E.
  1299. if (byte == 0x7E) {
  1300. iso2022_jp_output = false;
  1301. TRY(on_code_point(0x203E));
  1302. continue;
  1303. }
  1304. // 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.
  1305. if (byte.has_value() && byte.value() <= 0x7F && byte != 0x0E && byte != 0x0F) {
  1306. iso2022_jp_output = false;
  1307. TRY(on_code_point(byte.value()));
  1308. continue;
  1309. }
  1310. // end-of-queue: Return finished.
  1311. if (!byte.has_value())
  1312. return {};
  1313. // Otherwise: Set ISO-2022-JP output to false and return error.
  1314. iso2022_jp_output = false;
  1315. TRY(on_code_point(replacement_code_point));
  1316. break;
  1317. case ISO2022JPState::Katakana:
  1318. // Based on byte:
  1319. // 0x1B: Set ISO-2022-JP decoder state to escape start and return continue.
  1320. if (byte == 0x1B) {
  1321. decoder_state = ISO2022JPState::EscapeStart;
  1322. continue;
  1323. }
  1324. // 0x21 to 0x5F: Set ISO-2022-JP output to false and return a code point whose value is 0xFF61 − 0x21 + byte.
  1325. if (byte.has_value() && byte.value() >= 0x21 && byte.value() <= 0x5F) {
  1326. iso2022_jp_output = false;
  1327. TRY(on_code_point(0xFF61 - 0x21 + byte.value()));
  1328. continue;
  1329. }
  1330. // end-of-queue: Return finished.
  1331. if (!byte.has_value())
  1332. return {};
  1333. // Otherwise: Set ISO-2022-JP output to false and return error.
  1334. iso2022_jp_output = false;
  1335. TRY(on_code_point(replacement_code_point));
  1336. break;
  1337. case ISO2022JPState::LeadByte:
  1338. // Based on byte:
  1339. // 0x1B: Set ISO-2022-JP decoder state to escape start and return continue.
  1340. if (byte == 0x1B) {
  1341. decoder_state = ISO2022JPState::EscapeStart;
  1342. continue;
  1343. }
  1344. // 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.
  1345. if (byte.has_value() && byte.value() >= 0x21 && byte.value() <= 0x7E) {
  1346. iso2022_jp_output = false;
  1347. iso2022_jp_lead = byte.value();
  1348. decoder_state = ISO2022JPState::TrailByte;
  1349. continue;
  1350. }
  1351. // end-of-queue: Return finished.
  1352. if (!byte.has_value())
  1353. return {};
  1354. // Otherwise: Set ISO-2022-JP output to false and return error.
  1355. iso2022_jp_output = false;
  1356. TRY(on_code_point(replacement_code_point));
  1357. break;
  1358. case ISO2022JPState::TrailByte:
  1359. // Based on byte:
  1360. // 0x1B: Set ISO-2022-JP decoder state to escape start and return error.
  1361. if (byte == 0x1B) {
  1362. decoder_state = ISO2022JPState::EscapeStart;
  1363. TRY(on_code_point(replacement_code_point));
  1364. continue;
  1365. }
  1366. // 0x21 to 0x7E:
  1367. if (byte.has_value() && byte.value() >= 0x21 && byte.value() <= 0x7E) {
  1368. // 1. Set the ISO-2022-JP decoder state to lead byte.
  1369. decoder_state = ISO2022JPState::LeadByte;
  1370. // 2. Let pointer be (ISO-2022-JP lead − 0x21) × 94 + byte − 0x21.
  1371. u32 pointer = (iso2022_jp_lead - 0x21) * 94 + byte.value() - 0x21;
  1372. // 3. Let code point be the index code point for pointer in index jis0208.
  1373. auto code_point = index_jis0208_code_point(pointer);
  1374. // 4. If code point is null, return error.
  1375. if (!code_point.has_value()) {
  1376. TRY(on_code_point(replacement_code_point));
  1377. continue;
  1378. }
  1379. // 5. Return a code point whose value is code point.
  1380. TRY(on_code_point(code_point.value()));
  1381. continue;
  1382. }
  1383. // end-of-queue: Set the ISO-2022-JP decoder state to lead byte and return error.
  1384. if (!byte.has_value()) {
  1385. decoder_state = ISO2022JPState::LeadByte;
  1386. TRY(on_code_point(replacement_code_point));
  1387. continue;
  1388. }
  1389. // Otherwise: Set ISO-2022-JP decoder state to lead byte and return error.
  1390. decoder_state = ISO2022JPState::LeadByte;
  1391. TRY(on_code_point(replacement_code_point));
  1392. break;
  1393. case ISO2022JPState::EscapeStart:
  1394. // 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.
  1395. if (byte == 0x24 || byte == 0x28) {
  1396. iso2022_jp_lead = byte.value();
  1397. decoder_state = ISO2022JPState::Escape;
  1398. continue;
  1399. }
  1400. // 2. If byte is not end-of-queue, then restore byte to ioQueue.
  1401. if (byte.has_value())
  1402. index--;
  1403. // 3. Set ISO-2022-JP output to false, ISO-2022-JP decoder state to ISO-2022-JP decoder output state, and return error.
  1404. iso2022_jp_output = false;
  1405. decoder_state = output_state;
  1406. TRY(on_code_point(replacement_code_point));
  1407. break;
  1408. case ISO2022JPState::Escape: {
  1409. // 1. Let lead be ISO-2022-JP lead and set ISO-2022-JP lead to 0x00.
  1410. auto lead = iso2022_jp_lead;
  1411. iso2022_jp_lead = 0x00;
  1412. // 2. Let state be null.
  1413. Optional<ISO2022JPState> state;
  1414. // 3. If lead is 0x28 and byte is 0x42, set state to ASCII.
  1415. if (lead == 0x28 && byte == 0x42)
  1416. state = ISO2022JPState::ASCII;
  1417. // 4. If lead is 0x28 and byte is 0x4A, set state to Roman.
  1418. if (lead == 0x28 && byte == 0x4A)
  1419. state = ISO2022JPState::Roman;
  1420. // 5. If lead is 0x28 and byte is 0x49, set state to katakana.
  1421. if (lead == 0x28 && byte == 0x49)
  1422. state = ISO2022JPState::Katakana;
  1423. // 6. If lead is 0x24 and byte is either 0x40 or 0x42, set state to lead byte.
  1424. if (lead == 0x24 && (byte == 0x40 || byte == 0x42))
  1425. state = ISO2022JPState::LeadByte;
  1426. // 7. If state is non-null, then:
  1427. if (state.has_value()) {
  1428. // 1. Set ISO-2022-JP decoder state and ISO-2022-JP decoder output state to state.
  1429. decoder_state = state.value();
  1430. output_state = state.value();
  1431. // 2. Let output be the value of ISO-2022-JP output.
  1432. auto output = iso2022_jp_output;
  1433. // 3. Set ISO-2022-JP output to true.
  1434. iso2022_jp_output = true;
  1435. // 4. Return continue, if output is false, and error otherwise.
  1436. if (output)
  1437. TRY(on_code_point(replacement_code_point));
  1438. continue;
  1439. }
  1440. // 8. If byte is end-of-queue, then restore lead to ioQueue; otherwise, restore « lead, byte » to ioQueue.
  1441. index -= byte.has_value() ? 2 : 1;
  1442. // 9. Set ISO-2022-JP output to false, ISO-2022-JP decoder state to ISO-2022-JP decoder output state and return error.
  1443. iso2022_jp_output = false;
  1444. decoder_state = output_state;
  1445. TRY(on_code_point(replacement_code_point));
  1446. break;
  1447. }
  1448. }
  1449. }
  1450. }
  1451. // https://encoding.spec.whatwg.org/#shift_jis-decoder
  1452. ErrorOr<void> ShiftJISDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  1453. {
  1454. // Shift_JIS’s decoder has an associated Shift_JIS lead (initially 0x00).
  1455. u8 shift_jis_lead = 0x00;
  1456. // Shift_JIS’s decoder’s handler, given ioQueue and byte, runs these steps:
  1457. size_t index = 0;
  1458. while (true) {
  1459. // 1. If byte is end-of-queue and Shift_JIS lead is not 0x00, set Shift_JIS lead to 0x00 and return error.
  1460. if (index >= input.length() && shift_jis_lead != 0x00) {
  1461. shift_jis_lead = 0x00;
  1462. TRY(on_code_point(replacement_code_point));
  1463. continue;
  1464. }
  1465. // 2. If byte is end-of-queue and Shift_JIS lead is 0x00, return finished.
  1466. if (index >= input.length() && shift_jis_lead == 0x00)
  1467. return {};
  1468. u8 const byte = input[index++];
  1469. // 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:
  1470. if (shift_jis_lead != 0x00) {
  1471. auto lead = shift_jis_lead;
  1472. Optional<u32> pointer;
  1473. shift_jis_lead = 0x00;
  1474. // 1. Let offset be 0x40 if byte is less than 0x7F, otherwise 0x41.
  1475. u8 const offset = byte < 0x7F ? 0x40 : 0x41;
  1476. // 2. Let lead offset be 0x81 if lead is less than 0xA0, otherwise 0xC1.
  1477. u8 const lead_offset = lead < 0xA0 ? 0x81 : 0xC1;
  1478. // 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.
  1479. if ((byte >= 0x40 && byte <= 0x7E) || (byte >= 0x80 && byte <= 0xFC))
  1480. pointer = (lead - lead_offset) * 188 + byte - offset;
  1481. // 4. If pointer is in the range 8836 to 10715, inclusive, return a code point whose value is 0xE000 − 8836 + pointer.
  1482. if (pointer.has_value() && pointer.value() >= 8836 && pointer.value() <= 10715) {
  1483. TRY(on_code_point(0xE000 - 8836 + pointer.value()));
  1484. continue;
  1485. }
  1486. // 5. Let code point be null if pointer is null, otherwise the index code point for pointer in index jis0208.
  1487. auto code_point = pointer.has_value() ? index_jis0208_code_point(pointer.value()) : Optional<u32> {};
  1488. // 6. If code point is non-null, return a code point whose value is code point.
  1489. if (code_point.has_value()) {
  1490. TRY(on_code_point(code_point.value()));
  1491. continue;
  1492. }
  1493. // 7. If byte is an ASCII byte, restore byte to ioQueue.
  1494. if (byte <= 0x7F)
  1495. index--;
  1496. // 8. Return error.
  1497. TRY(on_code_point(replacement_code_point));
  1498. continue;
  1499. }
  1500. // 4. If byte is an ASCII byte or 0x80, return a code point whose value is byte.
  1501. if (byte <= 0x80) {
  1502. TRY(on_code_point(byte));
  1503. continue;
  1504. }
  1505. // 5. If byte is in the range 0xA1 to 0xDF, inclusive, return a code point whose value is 0xFF61 − 0xA1 + byte.
  1506. if (byte >= 0xA1 && byte <= 0xDF) {
  1507. TRY(on_code_point(0xFF61 - 0xA1 + byte));
  1508. continue;
  1509. }
  1510. // 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.
  1511. if ((byte >= 0x81 && byte <= 0x9F) || (byte >= 0xE0 && byte <= 0xFC)) {
  1512. shift_jis_lead = byte;
  1513. continue;
  1514. }
  1515. // 7. Return error.
  1516. TRY(on_code_point(replacement_code_point));
  1517. }
  1518. }
  1519. // https://encoding.spec.whatwg.org/#euc-kr-decoder
  1520. ErrorOr<void> EUCKRDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  1521. {
  1522. // EUC-KR’s decoder has an associated EUC-KR lead (initially 0x00).
  1523. u8 euc_kr_lead = 0x00;
  1524. // EUC-KR’s decoder’s handler, given ioQueue and byte, runs these steps:
  1525. size_t index = 0;
  1526. while (true) {
  1527. // 1. If byte is end-of-queue and EUC-KR lead is not 0x00, set EUC-KR lead to 0x00 and return error.
  1528. if (index >= input.length() && euc_kr_lead != 0x00) {
  1529. euc_kr_lead = 0x00;
  1530. TRY(on_code_point(replacement_code_point));
  1531. continue;
  1532. }
  1533. // 2. If byte is end-of-queue and EUC-KR lead is 0x00, return finished.
  1534. if (index >= input.length() && euc_kr_lead == 0x00)
  1535. return {};
  1536. u8 const byte = input[index++];
  1537. // 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:
  1538. if (euc_kr_lead != 0x00) {
  1539. auto lead = euc_kr_lead;
  1540. Optional<u32> pointer;
  1541. euc_kr_lead = 0x00;
  1542. // 1. If byte is in the range 0x41 to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 + (byte − 0x41).
  1543. if (byte >= 0x41 && byte <= 0xFE)
  1544. pointer = (lead - 0x81) * 190 + (byte - 0x41);
  1545. // 2. Let code point be null if pointer is null, otherwise the index code point for pointer in index EUC-KR.
  1546. auto code_point = pointer.has_value() ? index_euc_kr_code_point(pointer.value()) : Optional<u32> {};
  1547. // 3. If code point is non-null, return a code point whose value is code point.
  1548. if (code_point.has_value()) {
  1549. TRY(on_code_point(code_point.value()));
  1550. continue;
  1551. }
  1552. // 4. If byte is an ASCII byte, restore byte to ioQueue.
  1553. if (byte <= 0x7F)
  1554. index--;
  1555. // 5. Return error.
  1556. TRY(on_code_point(replacement_code_point));
  1557. continue;
  1558. }
  1559. // 4. If byte is an ASCII byte, return a code point whose value is byte.
  1560. if (byte <= 0x7F) {
  1561. TRY(on_code_point(byte));
  1562. continue;
  1563. }
  1564. // 5. If byte is in the range 0x81 to 0xFE, inclusive, set EUC-KR lead to byte and return continue.
  1565. if (byte >= 0x81 && byte <= 0xFE) {
  1566. euc_kr_lead = byte;
  1567. continue;
  1568. }
  1569. // 6. Return error.
  1570. TRY(on_code_point(replacement_code_point));
  1571. }
  1572. }
  1573. // https://encoding.spec.whatwg.org/#replacement-decoder
  1574. ErrorOr<void> ReplacementDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  1575. {
  1576. // replacement’s decoder has an associated replacement error returned (initially false).
  1577. // replacement’s decoder’s handler, given ioQueue and byte, runs these steps:
  1578. // 1. If byte is end-of-queue, return finished.
  1579. // 2. If replacement error returned is false, set replacement error returned to true and return error.
  1580. // 3. Return finished.
  1581. if (!input.is_empty())
  1582. return on_code_point(replacement_code_point);
  1583. return {};
  1584. }
  1585. }