Decoder.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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/StringBuilder.h>
  10. #include <AK/Utf16View.h>
  11. #include <AK/Utf8View.h>
  12. #include <LibTextCodec/Decoder.h>
  13. namespace TextCodec {
  14. static constexpr u32 replacement_code_point = 0xfffd;
  15. namespace {
  16. Latin1Decoder s_latin1_decoder;
  17. UTF8Decoder s_utf8_decoder;
  18. UTF16BEDecoder s_utf16be_decoder;
  19. UTF16LEDecoder s_utf16le_decoder;
  20. Latin2Decoder s_latin2_decoder;
  21. Latin9Decoder s_latin9_decoder;
  22. PDFDocEncodingDecoder s_pdf_doc_encoding_decoder;
  23. TurkishDecoder s_turkish_decoder;
  24. XUserDefinedDecoder s_x_user_defined_decoder;
  25. // clang-format off
  26. // https://encoding.spec.whatwg.org/index-ibm866.txt
  27. SingleByteDecoder s_ibm866_decoder {{
  28. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  29. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  30. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  31. 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
  32. 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
  33. 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
  34. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  35. 0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4, 0x25A0, 0x00A0,
  36. }};
  37. // https://encoding.spec.whatwg.org/index-iso-8859-3.txt
  38. SingleByteDecoder s_latin3_decoder {{
  39. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  40. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  41. 0x00A0, 0x0126, 0x02D8, 0x00A3, 0x00A4, 0xFFFD, 0x0124, 0x00A7, 0x00A8, 0x0130, 0x015E, 0x011E, 0x0134, 0x00AD, 0xFFFD, 0x017B,
  42. 0x00B0, 0x0127, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x0125, 0x00B7, 0x00B8, 0x0131, 0x015F, 0x011F, 0x0135, 0x00BD, 0xFFFD, 0x017C,
  43. 0x00C0, 0x00C1, 0x00C2, 0xFFFD, 0x00C4, 0x010A, 0x0108, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  44. 0xFFFD, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x0120, 0x00D6, 0x00D7, 0x011C, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x016C, 0x015C, 0x00DF,
  45. 0x00E0, 0x00E1, 0x00E2, 0xFFFD, 0x00E4, 0x010B, 0x0109, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  46. 0xFFFD, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x0121, 0x00F6, 0x00F7, 0x011D, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x016D, 0x015D, 0x02D9,
  47. }};
  48. // https://encoding.spec.whatwg.org/index-iso-8859-4.txt
  49. SingleByteDecoder s_latin4_decoder {{
  50. 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
  51. 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
  52. 0x00A0, 0x0104, 0x0138, 0x0156, 0x00A4, 0x0128, 0x013B, 0x00A7, 0x00A8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00AD, 0x017D, 0x00AF,
  53. 0x00B0, 0x0105, 0x02DB, 0x0157, 0x00B4, 0x0129, 0x013C, 0x02C7, 0x00B8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014A, 0x017E, 0x014B,
  54. 0x0100, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x012E, 0x010C, 0x00C9, 0x0118, 0x00CB, 0x0116, 0x00CD, 0x00CE, 0x012A,
  55. 0x0110, 0x0145, 0x014C, 0x0136, 0x00D4, 0x00D5, 0x00D6, 0x00D7, 0x00D8, 0x0172, 0x00DA, 0x00DB, 0x00DC, 0x0168, 0x016A, 0x00DF,
  56. 0x0101, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x012F, 0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x012B,
  57. 0x0111, 0x0146, 0x014D, 0x0137, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x0169, 0x016B, 0x02D9,
  58. }};
  59. // https://encoding.spec.whatwg.org/index-windows-1250.txt
  60. SingleByteDecoder s_centraleurope_decoder {{
  61. 0x20AC, 0x0081, 0x201A, 0x0083, 0x201E, 0x2026, 0x2020, 0x2021, 0x0088, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179,
  62. 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0098, 0x2122, 0x0161, 0x203A, 0x015B, 0x0165, 0x017E, 0x017A,
  63. 0x00A0, 0x02C7, 0x02D8, 0x0141, 0x00A4, 0x0104, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x017B,
  64. 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, 0x017C,
  65. 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,
  66. 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,
  67. 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
  68. 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9,
  69. }};
  70. // https://encoding.spec.whatwg.org/index-windows-1251.txt
  71. SingleByteDecoder s_cyrillic_decoder {{
  72. 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021, 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F,
  73. 0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0098, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F,
  74. 0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7, 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407,
  75. 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7, 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457,
  76. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  77. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  78. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  79. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  80. }};
  81. // https://encoding.spec.whatwg.org/index-windows-1255.txt
  82. SingleByteDecoder s_hebrew_decoder {{
  83. 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x008A, 0x2039, 0x008C, 0x008D, 0x008E, 0x008F,
  84. 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x009A, 0x203A, 0x009C, 0x009D, 0x009E, 0x009F,
  85. 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AA, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  86. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  87. 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7, 0x05B8, 0x05B9, 0x05BA, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF,
  88. 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3, 0x05F4, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  89. 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
  90. 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD,
  91. }};
  92. // https://encoding.spec.whatwg.org/index-koi8-r.txt
  93. SingleByteDecoder s_koi8r_decoder {{
  94. 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524, 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
  95. 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219, 0x221A, 0x2248, 0x2264, 0x2265, 0x00A0, 0x2321, 0x00B0, 0x00B2, 0x00B7, 0x00F7,
  96. 0x2550, 0x2551, 0x2552, 0xD191, 0x2553, 0x2554, 0x2555, 0x2556, 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x255C, 0x255D, 0x255E,
  97. 0x255F, 0x2560, 0x2561, 0xD081, 0x2562, 0x2563, 0x2564, 0x2565, 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x256B, 0x256C, 0x00A9,
  98. 0x044E, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, 0x0445, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E,
  99. 0x043F, 0x044F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, 0x044C, 0x044B, 0x0437, 0x0448, 0x044D, 0x0449, 0x0447, 0x044A,
  100. 0x042E, 0x0410, 0x0441, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, 0x0425, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
  101. 0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, 0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A,
  102. }};
  103. // https://encoding.spec.whatwg.org/index-macintosh.txt
  104. SingleByteDecoder s_mac_roman_decoder {{
  105. 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8,
  106. 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC,
  107. 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8,
  108. 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8,
  109. 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153,
  110. 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02,
  111. 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4,
  112. 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7,
  113. }};
  114. // clang-format on
  115. }
  116. Optional<Decoder&> decoder_for(StringView a_encoding)
  117. {
  118. auto encoding = get_standardized_encoding(a_encoding);
  119. if (encoding.has_value()) {
  120. if (encoding.value().equals_ignoring_ascii_case("windows-1252"sv))
  121. return s_latin1_decoder;
  122. if (encoding.value().equals_ignoring_ascii_case("utf-8"sv))
  123. return s_utf8_decoder;
  124. if (encoding.value().equals_ignoring_ascii_case("utf-16be"sv))
  125. return s_utf16be_decoder;
  126. if (encoding.value().equals_ignoring_ascii_case("utf-16le"sv))
  127. return s_utf16le_decoder;
  128. if (encoding.value().equals_ignoring_ascii_case("ibm866"sv))
  129. return s_ibm866_decoder;
  130. if (encoding.value().equals_ignoring_ascii_case("iso-8859-2"sv))
  131. return s_latin2_decoder;
  132. if (encoding.value().equals_ignoring_ascii_case("iso-8859-3"sv))
  133. return s_latin3_decoder;
  134. if (encoding.value().equals_ignoring_ascii_case("iso-8859-4"sv))
  135. return s_latin4_decoder;
  136. if (encoding.value().equals_ignoring_ascii_case("windows-1250"sv))
  137. return s_centraleurope_decoder;
  138. if (encoding.value().equals_ignoring_ascii_case("windows-1255"sv))
  139. return s_hebrew_decoder;
  140. if (encoding.value().equals_ignoring_ascii_case("windows-1251"sv))
  141. return s_cyrillic_decoder;
  142. if (encoding.value().equals_ignoring_ascii_case("koi8-r"sv))
  143. return s_koi8r_decoder;
  144. if (encoding.value().equals_ignoring_ascii_case("iso-8859-15"sv))
  145. return s_latin9_decoder;
  146. if (encoding.value().equals_ignoring_ascii_case("macintosh"sv))
  147. return s_mac_roman_decoder;
  148. if (encoding.value().equals_ignoring_ascii_case("PDFDocEncoding"sv))
  149. return s_pdf_doc_encoding_decoder;
  150. if (encoding.value().equals_ignoring_ascii_case("windows-1254"sv))
  151. return s_turkish_decoder;
  152. if (encoding.value().equals_ignoring_ascii_case("x-user-defined"sv))
  153. return s_x_user_defined_decoder;
  154. }
  155. dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
  156. return {};
  157. }
  158. // https://encoding.spec.whatwg.org/#concept-encoding-get
  159. Optional<StringView> get_standardized_encoding(StringView encoding)
  160. {
  161. encoding = encoding.trim_whitespace();
  162. 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))
  163. return "UTF-8"sv;
  164. if (encoding.is_one_of_ignoring_ascii_case("866"sv, "cp866"sv, "csibm866"sv, "ibm866"sv))
  165. return "IBM866"sv;
  166. 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))
  167. return "ISO-8859-2"sv;
  168. 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))
  169. return "ISO-8859-3"sv;
  170. 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))
  171. return "ISO-8859-4"sv;
  172. 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))
  173. return "ISO-8859-5"sv;
  174. 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))
  175. return "ISO-8859-6"sv;
  176. 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))
  177. return "ISO-8859-7"sv;
  178. 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))
  179. return "ISO-8859-8"sv;
  180. if (encoding.is_one_of_ignoring_ascii_case("csiso88598i"sv, "iso-8859-8-i"sv, "logical"sv))
  181. return "ISO-8859-8-I"sv;
  182. if (encoding.is_one_of_ignoring_ascii_case("csisolatin6"sv, "iso8859-10"sv, "iso-ir-157"sv, "iso8859-10"sv, "iso885910"sv, "l6"sv, "latin6"sv))
  183. return "ISO-8859-10"sv;
  184. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-13"sv, "iso8859-13"sv, "iso885913"sv))
  185. return "ISO-8859-13"sv;
  186. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-14"sv, "iso8859-14"sv, "iso885914"sv))
  187. return "ISO-8859-14"sv;
  188. 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))
  189. return "ISO-8859-15"sv;
  190. if (encoding.is_one_of_ignoring_ascii_case("iso-8859-16"sv))
  191. return "ISO-8859-16"sv;
  192. if (encoding.is_one_of_ignoring_ascii_case("cskoi8r"sv, "koi"sv, "koi8"sv, "koi8-r"sv, "koi8_r"sv))
  193. return "KOI8-R"sv;
  194. if (encoding.is_one_of_ignoring_ascii_case("koi8-ru"sv, "koi8-u"sv))
  195. return "KOI8-U"sv;
  196. if (encoding.is_one_of_ignoring_ascii_case("csmacintosh"sv, "mac"sv, "macintosh"sv, "x-mac-roman"sv))
  197. return "macintosh"sv;
  198. if (encoding.is_one_of_ignoring_ascii_case("pdfdocencoding"sv))
  199. return "PDFDocEncoding"sv;
  200. 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))
  201. return "windows-874"sv;
  202. if (encoding.is_one_of_ignoring_ascii_case("cp1250"sv, "windows-1250"sv, "x-cp1250"sv))
  203. return "windows-1250"sv;
  204. if (encoding.is_one_of_ignoring_ascii_case("cp1251"sv, "windows-1251"sv, "x-cp1251"sv))
  205. return "windows-1251"sv;
  206. 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))
  207. return "windows-1252"sv;
  208. if (encoding.is_one_of_ignoring_ascii_case("cp1253"sv, "windows-1253"sv, "x-cp1253"sv))
  209. return "windows-1253"sv;
  210. 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))
  211. return "windows-1254"sv;
  212. if (encoding.is_one_of_ignoring_ascii_case("cp1255"sv, "windows-1255"sv, "x-cp1255"sv))
  213. return "windows-1255"sv;
  214. if (encoding.is_one_of_ignoring_ascii_case("cp1256"sv, "windows-1256"sv, "x-cp1256"sv))
  215. return "windows-1256"sv;
  216. if (encoding.is_one_of_ignoring_ascii_case("cp1257"sv, "windows-1257"sv, "x-cp1257"sv))
  217. return "windows-1257"sv;
  218. if (encoding.is_one_of_ignoring_ascii_case("cp1258"sv, "windows-1258"sv, "x-cp1258"sv))
  219. return "windows-1258"sv;
  220. if (encoding.is_one_of_ignoring_ascii_case("x-mac-cyrillic"sv, "x-mac-ukrainian"sv))
  221. return "x-mac-cyrillic"sv;
  222. if (encoding.is_one_of_ignoring_ascii_case("koi8-r"sv, "koi8r"sv))
  223. return "koi8-r"sv;
  224. 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))
  225. return "GBK"sv;
  226. if (encoding.is_one_of_ignoring_ascii_case("gb18030"sv))
  227. return "gb18030"sv;
  228. if (encoding.is_one_of_ignoring_ascii_case("big5"sv, "big5-hkscs"sv, "cn-big5"sv, "csbig5"sv, "x-x-big5"sv))
  229. return "Big5"sv;
  230. if (encoding.is_one_of_ignoring_ascii_case("cseucpkdfmtjapanese"sv, "euc-jp"sv, "x-euc-jp"sv))
  231. return "EUC-JP"sv;
  232. if (encoding.is_one_of_ignoring_ascii_case("csiso2022jp"sv, "iso-2022-jp"sv))
  233. return "ISO-2022-JP"sv;
  234. 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))
  235. return "Shift_JIS"sv;
  236. 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))
  237. return "EUC-KR"sv;
  238. 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))
  239. return "replacement"sv;
  240. if (encoding.is_one_of_ignoring_ascii_case("unicodefffe"sv, "utf-16be"sv))
  241. return "UTF-16BE"sv;
  242. 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))
  243. return "UTF-16LE"sv;
  244. if (encoding.is_one_of_ignoring_ascii_case("x-user-defined"sv))
  245. return "x-user-defined"sv;
  246. dbgln("TextCodec: Unrecognized encoding: {}", encoding);
  247. return {};
  248. }
  249. // https://encoding.spec.whatwg.org/#bom-sniff
  250. Optional<Decoder&> bom_sniff_to_decoder(StringView input)
  251. {
  252. // 1. Let BOM be the result of peeking 3 bytes from ioQueue, converted to a byte sequence.
  253. // 2. For each of the rows in the table below, starting with the first one and going down,
  254. // if BOM starts with the bytes given in the first column, then return the encoding given
  255. // in the cell in the second column of that row. Otherwise, return null.
  256. // Byte Order Mark | Encoding
  257. // --------------------------
  258. // 0xEF 0xBB 0xBF | UTF-8
  259. // 0xFE 0xFF | UTF-16BE
  260. // 0xFF 0xFE | UTF-16LE
  261. auto bytes = input.bytes();
  262. if (bytes.size() < 2)
  263. return {};
  264. auto first_byte = bytes[0];
  265. switch (first_byte) {
  266. case 0xEF: // UTF-8
  267. if (bytes.size() < 3)
  268. return {};
  269. if (bytes[1] == 0xBB && bytes[2] == 0xBF)
  270. return s_utf8_decoder;
  271. return {};
  272. case 0xFE: // UTF-16BE
  273. if (bytes[1] == 0xFF)
  274. return s_utf16be_decoder;
  275. return {};
  276. case 0xFF: // UTF-16LE
  277. if (bytes[1] == 0xFE)
  278. return s_utf16le_decoder;
  279. return {};
  280. }
  281. return {};
  282. }
  283. // https://encoding.spec.whatwg.org/#decode
  284. ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(Decoder& fallback_decoder, StringView input)
  285. {
  286. Decoder* actual_decoder = &fallback_decoder;
  287. // 1. Let BOMEncoding be the result of BOM sniffing ioQueue.
  288. // 2. If BOMEncoding is non-null:
  289. if (auto unicode_decoder = bom_sniff_to_decoder(input); unicode_decoder.has_value()) {
  290. // 1. Set encoding to BOMEncoding.
  291. actual_decoder = &unicode_decoder.value();
  292. // 2. Read three bytes from ioQueue, if BOMEncoding is UTF-8; otherwise read two bytes. (Do nothing with those bytes.)
  293. // FIXME: I imagine this will be pretty slow for large inputs, as it's regenerating the input without the first 2/3 bytes.
  294. input = input.substring_view(&unicode_decoder.value() == &s_utf8_decoder ? 3 : 2);
  295. }
  296. VERIFY(actual_decoder);
  297. // 3. Process a queue with an instance of encoding’s decoder, ioQueue, output, and "replacement".
  298. // FIXME: This isn't the exact same as the spec, which is written in terms of I/O queues.
  299. auto output = TRY(actual_decoder->to_utf8(input));
  300. // 4. Return output.
  301. return output;
  302. }
  303. // https://encoding.spec.whatwg.org/#get-an-output-encoding
  304. StringView get_output_encoding(StringView encoding)
  305. {
  306. // 1. If encoding is replacement or UTF-16BE/LE, then return UTF-8.
  307. if (encoding.is_one_of_ignoring_ascii_case("replacement"sv, "utf-16le"sv, "utf-16be"sv))
  308. return "UTF-8"sv;
  309. // 2. Return encoding.
  310. return encoding;
  311. }
  312. bool Decoder::validate(StringView)
  313. {
  314. // By-default we assume that any input sequence is valid, character encodings that do not accept all inputs may override this
  315. return true;
  316. }
  317. ErrorOr<String> Decoder::to_utf8(StringView input)
  318. {
  319. StringBuilder builder(input.length());
  320. TRY(process(input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  321. return builder.to_string_without_validation();
  322. }
  323. ErrorOr<void> UTF8Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  324. {
  325. for (auto c : Utf8View(input)) {
  326. TRY(on_code_point(c));
  327. }
  328. return {};
  329. }
  330. bool UTF8Decoder::validate(StringView input)
  331. {
  332. return Utf8View(input).validate();
  333. }
  334. ErrorOr<String> UTF8Decoder::to_utf8(StringView input)
  335. {
  336. // Discard the BOM
  337. auto bomless_input = input;
  338. if (auto bytes = input.bytes(); bytes.size() >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
  339. bomless_input = input.substring_view(3);
  340. }
  341. return Decoder::to_utf8(bomless_input);
  342. }
  343. ErrorOr<void> UTF16BEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  344. {
  345. // rfc2781, 2.2 Decoding UTF-16
  346. size_t utf16_length = input.length() - (input.length() % 2);
  347. for (size_t i = 0; i < utf16_length; i += 2) {
  348. // 1) If W1 < 0xD800 or W1 > 0xDFFF, the character value U is the value
  349. // of W1. Terminate.
  350. u16 w1 = (static_cast<u8>(input[i]) << 8) | static_cast<u8>(input[i + 1]);
  351. if (!is_unicode_surrogate(w1)) {
  352. TRY(on_code_point(w1));
  353. continue;
  354. }
  355. // 2) Determine if W1 is between 0xD800 and 0xDBFF. If not, the sequence
  356. // is in error and no valid character can be obtained using W1.
  357. // Terminate.
  358. // 3) If there is no W2 (that is, the sequence ends with W1), or if W2
  359. // is not between 0xDC00 and 0xDFFF, the sequence is in error.
  360. // Terminate.
  361. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length) {
  362. TRY(on_code_point(replacement_code_point));
  363. continue;
  364. }
  365. u16 w2 = (static_cast<u8>(input[i + 2]) << 8) | static_cast<u8>(input[i + 3]);
  366. if (!Utf16View::is_low_surrogate(w2)) {
  367. TRY(on_code_point(replacement_code_point));
  368. continue;
  369. }
  370. // 4) Construct a 20-bit unsigned integer U', taking the 10 low-order
  371. // bits of W1 as its 10 high-order bits and the 10 low-order bits of
  372. // W2 as its 10 low-order bits.
  373. // 5) Add 0x10000 to U' to obtain the character value U. Terminate.
  374. TRY(on_code_point(Utf16View::decode_surrogate_pair(w1, w2)));
  375. i += 2;
  376. }
  377. return {};
  378. }
  379. bool UTF16BEDecoder::validate(StringView input)
  380. {
  381. size_t utf16_length = input.length() - (input.length() % 2);
  382. for (size_t i = 0; i < utf16_length; i += 2) {
  383. u16 w1 = (static_cast<u8>(input[i]) << 8) | static_cast<u8>(input[i + 1]);
  384. if (!is_unicode_surrogate(w1))
  385. continue;
  386. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length)
  387. return false;
  388. u16 w2 = (static_cast<u8>(input[i + 2]) << 8) | static_cast<u8>(input[i + 3]);
  389. if (!Utf16View::is_low_surrogate(w2))
  390. return false;
  391. i += 2;
  392. }
  393. return true;
  394. }
  395. ErrorOr<String> UTF16BEDecoder::to_utf8(StringView input)
  396. {
  397. // Discard the BOM
  398. auto bomless_input = input;
  399. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF)
  400. bomless_input = input.substring_view(2);
  401. StringBuilder builder(bomless_input.length() / 2);
  402. TRY(process(bomless_input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  403. return builder.to_string();
  404. }
  405. ErrorOr<void> UTF16LEDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  406. {
  407. // rfc2781, 2.2 Decoding UTF-16
  408. size_t utf16_length = input.length() - (input.length() % 2);
  409. for (size_t i = 0; i < utf16_length; i += 2) {
  410. // 1) If W1 < 0xD800 or W1 > 0xDFFF, the character value U is the value
  411. // of W1. Terminate.
  412. u16 w1 = static_cast<u8>(input[i]) | (static_cast<u8>(input[i + 1]) << 8);
  413. if (!is_unicode_surrogate(w1)) {
  414. TRY(on_code_point(w1));
  415. continue;
  416. }
  417. // 2) Determine if W1 is between 0xD800 and 0xDBFF. If not, the sequence
  418. // is in error and no valid character can be obtained using W1.
  419. // Terminate.
  420. // 3) If there is no W2 (that is, the sequence ends with W1), or if W2
  421. // is not between 0xDC00 and 0xDFFF, the sequence is in error.
  422. // Terminate.
  423. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length) {
  424. TRY(on_code_point(replacement_code_point));
  425. continue;
  426. }
  427. u16 w2 = static_cast<u8>(input[i + 2]) | (static_cast<u8>(input[i + 3]) << 8);
  428. if (!Utf16View::is_low_surrogate(w2)) {
  429. TRY(on_code_point(replacement_code_point));
  430. continue;
  431. }
  432. // 4) Construct a 20-bit unsigned integer U', taking the 10 low-order
  433. // bits of W1 as its 10 high-order bits and the 10 low-order bits of
  434. // W2 as its 10 low-order bits.
  435. // 5) Add 0x10000 to U' to obtain the character value U. Terminate.
  436. TRY(on_code_point(Utf16View::decode_surrogate_pair(w1, w2)));
  437. i += 2;
  438. }
  439. return {};
  440. }
  441. bool UTF16LEDecoder::validate(StringView input)
  442. {
  443. size_t utf16_length = input.length() - (input.length() % 2);
  444. for (size_t i = 0; i < utf16_length; i += 2) {
  445. u16 w1 = static_cast<u8>(input[i]) | (static_cast<u8>(input[i + 1]) << 8);
  446. if (!is_unicode_surrogate(w1))
  447. continue;
  448. if (!Utf16View::is_high_surrogate(w1) || i + 2 == utf16_length)
  449. return false;
  450. u16 w2 = static_cast<u8>(input[i + 2]) | (static_cast<u8>(input[i + 3]) << 8);
  451. if (!Utf16View::is_low_surrogate(w2))
  452. return false;
  453. i += 2;
  454. }
  455. return true;
  456. }
  457. ErrorOr<String> UTF16LEDecoder::to_utf8(StringView input)
  458. {
  459. // Discard the BOM
  460. auto bomless_input = input;
  461. if (auto bytes = input.bytes(); bytes.size() >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE)
  462. bomless_input = input.substring_view(2);
  463. StringBuilder builder(bomless_input.length() / 2);
  464. TRY(process(bomless_input, [&builder](u32 c) { return builder.try_append_code_point(c); }));
  465. return builder.to_string();
  466. }
  467. ErrorOr<void> Latin1Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  468. {
  469. for (u8 ch : input) {
  470. // Latin1 is the same as the first 256 Unicode code_points, so no mapping is needed, just utf-8 encoding.
  471. TRY(on_code_point(ch));
  472. }
  473. return {};
  474. }
  475. namespace {
  476. u32 convert_latin2_to_utf8(u8 in)
  477. {
  478. switch (in) {
  479. #define MAP(X, Y) \
  480. case X: \
  481. return Y
  482. MAP(0xA1, 0x104);
  483. MAP(0xA2, 0x2D8);
  484. MAP(0xA3, 0x141);
  485. MAP(0xA5, 0x13D);
  486. MAP(0xA6, 0x15A);
  487. MAP(0xA9, 0x160);
  488. MAP(0xAA, 0x15E);
  489. MAP(0xAB, 0x164);
  490. MAP(0xAC, 0x179);
  491. MAP(0xAE, 0x17D);
  492. MAP(0xAF, 0x17B);
  493. MAP(0xB1, 0x105);
  494. MAP(0xB2, 0x2DB);
  495. MAP(0xB3, 0x142);
  496. MAP(0xB5, 0x13E);
  497. MAP(0xB6, 0x15B);
  498. MAP(0xB7, 0x2C7);
  499. MAP(0xB9, 0x161);
  500. MAP(0xBA, 0x15F);
  501. MAP(0xBB, 0x165);
  502. MAP(0xBC, 0x17A);
  503. MAP(0xBD, 0x2DD);
  504. MAP(0xBE, 0x17E);
  505. MAP(0xBF, 0x17C);
  506. MAP(0xC0, 0x154);
  507. MAP(0xC3, 0x102);
  508. MAP(0xC5, 0x139);
  509. MAP(0xC6, 0x106);
  510. MAP(0xC8, 0x10C);
  511. MAP(0xCA, 0x118);
  512. MAP(0xCC, 0x11A);
  513. MAP(0xCF, 0x10E);
  514. MAP(0xD0, 0x110);
  515. MAP(0xD1, 0x143);
  516. MAP(0xD2, 0x147);
  517. MAP(0xD5, 0x150);
  518. MAP(0xD8, 0x158);
  519. MAP(0xD9, 0x16E);
  520. MAP(0xDB, 0x170);
  521. MAP(0xDE, 0x162);
  522. MAP(0xE0, 0x155);
  523. MAP(0xE3, 0x103);
  524. MAP(0xE5, 0x13A);
  525. MAP(0xE6, 0x107);
  526. MAP(0xE8, 0x10D);
  527. MAP(0xEA, 0x119);
  528. MAP(0xEC, 0x11B);
  529. MAP(0xEF, 0x10F);
  530. MAP(0xF0, 0x111);
  531. MAP(0xF1, 0x144);
  532. MAP(0xF2, 0x148);
  533. MAP(0xF5, 0x151);
  534. MAP(0xF8, 0x159);
  535. MAP(0xF9, 0x16F);
  536. MAP(0xFB, 0x171);
  537. MAP(0xFE, 0x163);
  538. MAP(0xFF, 0x2D9);
  539. #undef MAP
  540. default:
  541. return in;
  542. }
  543. }
  544. }
  545. ErrorOr<void> Latin2Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  546. {
  547. for (auto c : input) {
  548. TRY(on_code_point(convert_latin2_to_utf8(c)));
  549. }
  550. return {};
  551. }
  552. ErrorOr<void> Latin9Decoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  553. {
  554. auto convert_latin9_to_utf8 = [](u8 ch) -> u32 {
  555. // Latin9 is the same as the first 256 Unicode code points, except for 8 characters.
  556. switch (ch) {
  557. case 0xA4:
  558. return 0x20AC;
  559. case 0xA6:
  560. return 0x160;
  561. case 0xA8:
  562. return 0x161;
  563. case 0xB4:
  564. return 0x17D;
  565. case 0xB8:
  566. return 0x17E;
  567. case 0xBC:
  568. return 0x152;
  569. case 0xBD:
  570. return 0x153;
  571. case 0xBE:
  572. return 0x178;
  573. default:
  574. return ch;
  575. }
  576. };
  577. for (auto ch : input) {
  578. TRY(on_code_point(convert_latin9_to_utf8(ch)));
  579. }
  580. return {};
  581. }
  582. ErrorOr<void> PDFDocEncodingDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  583. {
  584. // PDF 1.7 spec, Appendix D.2 "PDFDocEncoding Character Set"
  585. // Character codes 0-8, 11-12, 14-23, 127, 159, 173 are not defined per spec.
  586. // clang-format off
  587. static constexpr Array<u32, 256> translation_table = {
  588. 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
  589. 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
  590. 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0017, 0x0017,
  591. 0x02D8, 0x02C7, 0x02C6, 0x02D9, 0x02DD, 0x02DB, 0x02DA, 0x02DC,
  592. 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
  593. 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
  594. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
  595. 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
  596. 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
  597. 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
  598. 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
  599. 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
  600. 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
  601. 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
  602. 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
  603. 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0xFFFC,
  604. 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x0192, 0x2044,
  605. 0x2039, 0x203A, 0x2212, 0x2030, 0x201E, 0x201C, 0x201D, 0x2018,
  606. 0x2019, 0x201A, 0x2122, 0xFB01, 0xFB02, 0x0141, 0x0152, 0x0160,
  607. 0x0178, 0x017D, 0x0131, 0x0142, 0x0153, 0x0161, 0x017E, 0xFFFC,
  608. 0x20AC, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
  609. 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0xFFFC, 0x00AE, 0x00AF,
  610. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
  611. 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  612. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  613. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  614. 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
  615. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
  616. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  617. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  618. 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
  619. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF,
  620. };
  621. // clang-format on
  622. for (u8 ch : input)
  623. TRY(on_code_point(translation_table[ch]));
  624. return {};
  625. }
  626. ErrorOr<void> TurkishDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  627. {
  628. auto convert_turkish_to_utf8 = [](u8 ch) -> u32 {
  629. // Turkish (aka ISO-8859-9, Windows-1254) is the same as the first 256 Unicode code points, except for 6 characters.
  630. switch (ch) {
  631. case 0xD0:
  632. return 0x11E;
  633. case 0xDD:
  634. return 0x130;
  635. case 0xDE:
  636. return 0x15E;
  637. case 0xF0:
  638. return 0x11F;
  639. case 0xFD:
  640. return 0x131;
  641. case 0xFE:
  642. return 0x15F;
  643. default:
  644. return ch;
  645. }
  646. };
  647. for (auto ch : input) {
  648. TRY(on_code_point(convert_turkish_to_utf8(ch)));
  649. }
  650. return {};
  651. }
  652. // https://encoding.spec.whatwg.org/#x-user-defined-decoder
  653. ErrorOr<void> XUserDefinedDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  654. {
  655. auto convert_x_user_defined_to_utf8 = [](u8 ch) -> u32 {
  656. // 2. If byte is an ASCII byte, return a code point whose value is byte.
  657. // https://infra.spec.whatwg.org/#ascii-byte
  658. // An ASCII byte is a byte in the range 0x00 (NUL) to 0x7F (DEL), inclusive.
  659. // NOTE: This doesn't check for ch >= 0x00, as that would always be true due to being unsigned.
  660. if (ch <= 0x7f)
  661. return ch;
  662. // 3. Return a code point whose value is 0xF780 + byte − 0x80.
  663. return 0xF780 + ch - 0x80;
  664. };
  665. for (auto ch : input) {
  666. TRY(on_code_point(convert_x_user_defined_to_utf8(ch)));
  667. }
  668. // 1. If byte is end-of-queue, return finished.
  669. return {};
  670. }
  671. // https://encoding.spec.whatwg.org/#single-byte-decoder
  672. ErrorOr<void> SingleByteDecoder::process(StringView input, Function<ErrorOr<void>(u32)> on_code_point)
  673. {
  674. for (u8 const byte : input) {
  675. if (byte < 0x80) {
  676. // 2. If byte is an ASCII byte, return a code point whose value is byte.
  677. TRY(on_code_point(byte));
  678. } else {
  679. // 3. Let code point be the index code point for byte − 0x80 in index single-byte.
  680. auto code_point = m_translation_table[byte - 0x80];
  681. // 4. If code point is null, return error.
  682. // NOTE: Error is communicated with 0xFFFD
  683. // 5. Return a code point whose value is code point.
  684. TRY(on_code_point(code_point));
  685. }
  686. }
  687. // 1. If byte is end-of-queue, return finished.
  688. return {};
  689. }
  690. }