#include "Charset.h" #include namespace web { namespace { // Windows-1252 from 0x80 to 0x9F, which Latin-1 leaves to control codes constexpr uint16_t WINDOWS_1252[33] = { 0x20AC, 0x81, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, 0x2021, 0x2C6, 0x2030, 0x160, 0x2039, 0x152, 0x8D, 0x17D, 0x8F, 0x90, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x2DC, 0x2122, 0x161, 0x203A, 0x153, 0x9D, 0x17E, 0x178, 0 }; // Base letters of U+00C0 to U+00FF constexpr char LATIN_BASE[] = "AAAAAAACEEEEIIIIDNOOOOOxOUUUUYTsaaaaaaaceeeeiiiidnooooo/ouuuuyty"; struct Fallback { uint16_t codepoint; char text[5]; }; // Ends with a zero entry: see the ELF loader's handling of pointers past the end of a table constexpr Fallback FALLBACKS[] = { { 0xA0, " " }, { 0xA9, "(c)" }, { 0xAB, "<<" }, { 0xAE, "(R)" }, { 0xB7, "*" }, { 0xBB, ">>" }, { 0x2010, "-" }, { 0x2011, "-" }, { 0x2012, "-" }, { 0x2013, "-" }, { 0x2014, "--" }, { 0x2015, "--" }, { 0x2018, "'" }, { 0x2019, "'" }, { 0x201A, "," }, { 0x201C, "\"" }, { 0x201D, "\"" }, { 0x201E, "\"" }, { 0x2022, "*" }, { 0x2026, "..." }, { 0x2032, "'" }, { 0x2033, "\"" }, { 0x2039, "<" }, { 0x203A, ">" }, { 0x20AC, "EUR" }, { 0x2122, "(TM)" }, { 0x2190, "<-" }, { 0x2192, "->" }, { 0x2212, "-" }, { 0, "" } }; } void appendUtf8(std::string& out, uint32_t cp) { if (cp < 0x80) { out += static_cast(cp); } else if (cp < 0x800) { out += static_cast(0xC0 | (cp >> 6)); out += static_cast(0x80 | (cp & 0x3F)); } else if (cp < 0x10000) { out += static_cast(0xE0 | (cp >> 12)); out += static_cast(0x80 | ((cp >> 6) & 0x3F)); out += static_cast(0x80 | (cp & 0x3F)); } else { out += static_cast(0xF0 | (cp >> 18)); out += static_cast(0x80 | ((cp >> 12) & 0x3F)); out += static_cast(0x80 | ((cp >> 6) & 0x3F)); out += static_cast(0x80 | (cp & 0x3F)); } } uint32_t nextUtf8(const char* text, size_t length, size_t& position) { const auto first = static_cast(text[position++]); if (first < 0x80) return first; int extra = first >= 0xF0 ? 3 : first >= 0xE0 ? 2 : 1; uint32_t cp = first & (0x3F >> extra); while (extra-- > 0 && position < length && (static_cast(text[position]) & 0xC0) == 0x80) { cp = (cp << 6) | (static_cast(text[position++]) & 0x3F); } return cp; } const char* asciiFallback(uint32_t cp) { for (size_t i = 0; FALLBACKS[i].codepoint != 0; i++) { if (FALLBACKS[i].codepoint == cp) return FALLBACKS[i].text; } if (cp >= 0x2000 && cp <= 0x200A) return " "; if (cp >= 0xC0 && cp <= 0xFF) { static char letter[2] = {}; letter[0] = LATIN_BASE[cp - 0xC0]; return letter; } return "?"; } void TextDecoder::select(const std::string& charset) { std::string name; for (const char c : charset) name += static_cast(std::tolower(static_cast(c))); latin = name.find("8859-1") != std::string::npos || name.find("1252") != std::string::npos || name.find("latin1") != std::string::npos; detected = true; } void TextDecoder::begin(const std::string& charset) { detected = false; latin = false; pendingLength = 0; if (!charset.empty()) select(charset); } void TextDecoder::feed(const char* data, size_t length, std::string& out) { if (!detected && length > 0) { // or std::string head(data, length < 2048 ? length : 2048); for (auto& c : head) c = static_cast(std::tolower(static_cast(c))); size_t start = head.find("charset="); std::string name; if (start != std::string::npos) { start += 8; while (start < head.size() && (head[start] == '"' || head[start] == '\'')) start++; while (start < head.size() && (std::isalnum(static_cast(head[start])) || head[start] == '-' || head[start] == '_')) { name += head[start++]; } } select(name); } auto single = [&out](unsigned char byte) { appendUtf8(out, byte >= 0x80 && byte < 0xA0 ? WINDOWS_1252[byte - 0x80] : byte); }; if (latin) { for (size_t i = 0; i < length; i++) single(static_cast(data[i])); return; } size_t i = 0; while (i < length || pendingLength > 0) { if (pendingLength == 0) { const auto byte = static_cast(data[i++]); if (byte < 0x80) { out += static_cast(byte); } else if (byte >= 0xC2 && byte <= 0xF4) { pending[pendingLength++] = byte; } else { // Not UTF-8: pages often claim it and contain Windows-1252 single(byte); } continue; } const size_t needed = pending[0] >= 0xF0 ? 4 : pending[0] >= 0xE0 ? 3 : 2; if (pendingLength < needed) { if (i >= length) return; // the rest comes with the next chunk if ((static_cast(data[i]) & 0xC0) != 0x80) { for (size_t k = 0; k < pendingLength; k++) single(pending[k]); pendingLength = 0; continue; } pending[pendingLength++] = static_cast(data[i++]); } if (pendingLength == needed) { out.append(reinterpret_cast(pending), needed); pendingLength = 0; } } } }