Decoder.cpp 63 KB

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