






#include "src/sfnt/SkOTTable_name.h"

#include "src/base/SkEndian.h"
#include "src/base/SkTSearch.h"
#include "src/base/SkUTF.h"
#include "src/core/SkStringUtils.h"

static SkUnichar next_unichar_UTF16BE(const uint8_t** srcPtr, size_t* length) {
    SkASSERT(srcPtr && *srcPtr && length);
    SkASSERT(*length > 0);

    uint16_t leading;
    if (*length < sizeof(leading)) {
        *length = 0;
        return 0xFFFD;
    }
    memcpy(&leading, *srcPtr, sizeof(leading));
    *srcPtr += sizeof(leading);
    *length -= sizeof(leading);
    SkUnichar c = SkEndian_SwapBE16(leading);

    if (SkUTF::IsTrailingSurrogateUTF16(c)) {
        return 0xFFFD;
    }
    if (SkUTF::IsLeadingSurrogateUTF16(c)) {
        uint16_t trailing;
        if (*length < sizeof(trailing)) {
            *length = 0;
            return 0xFFFD;
        }
        memcpy(&trailing, *srcPtr, sizeof(trailing));
        SkUnichar c2 = SkEndian_SwapBE16(trailing);
        if (!SkUTF::IsTrailingSurrogateUTF16(c2)) {
            return 0xFFFD;
        }
        *srcPtr += sizeof(trailing);
        *length -= sizeof(trailing);

        c = (c << 10) + c2 + (0x10000 - (0xD800 << 10) - 0xDC00);
    }
    return c;
}

static void SkString_from_UTF16BE(const uint8_t* utf16be, size_t length, SkString& utf8) {
    
    SkASSERT(utf16be != nullptr);

    utf8.reset();
    while (length) {
        utf8.appendUnichar(next_unichar_UTF16BE(&utf16be, &length));
    }
}






static const uint16_t UnicodeFromMacRoman[0x80] = {
    0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1,
    0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8,
    0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3,
    0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC,
    0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF,
    0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8,
    0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211,
    0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8,
    0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB,
    0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153,
    0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA,
    0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02,
    0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1,
    0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4,
    0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC,
    0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7,
};

static void SkStringFromMacRoman(const uint8_t* macRoman, size_t length, SkString& utf8) {
    utf8.reset();
    for (size_t i = 0; i < length; ++i) {
        utf8.appendUnichar(macRoman[i] < 0x80 ? macRoman[i]
                                              : UnicodeFromMacRoman[macRoman[i] - 0x80]);
    }
}

static const struct BCP47FromLanguageId {
    uint16_t languageID;
    const char* bcp47;
}

BCP47FromLanguageID[] = {
    



    {0, "en"}, 
    {1, "fr"}, 
    {2, "de"}, 
    {3, "it"}, 
    {4, "nl"}, 
    {5, "sv"}, 
    {6, "es"}, 
    {7, "da"}, 
    {8, "pt"}, 
    {9, "nb"}, 
    {10, "he"}, 
    {11, "ja"}, 
    {12, "ar"}, 
    {13, "fi"}, 
    {14, "el"}, 
    {15, "is"}, 
    {16, "mt"}, 
    {17, "tr"}, 
    {18, "hr"}, 
    {19, "zh-Hant"}, 
    {20, "ur"}, 
    {21, "hi"}, 
    {22, "th"}, 
    {23, "ko"}, 
    {24, "lt"}, 
    {25, "pl"}, 
    {26, "hu"}, 
    {27, "et"}, 
    {28, "lv"}, 
    {29, "se"}, 
    {30, "fo"}, 
    {31, "fa"}, 
    {32, "ru"}, 
    {33, "zh-Hans"}, 
    {34, "nl"}, 
    {35, "ga"}, 
    {36, "sq"}, 
    {37, "ro"}, 
    {38, "cs"}, 
    {39, "sk"}, 
    {40, "sl"}, 
    {41, "yi"}, 
    {42, "sr"}, 
    {43, "mk"}, 
    {44, "bg"}, 
    {45, "uk"}, 
    {46, "be"}, 
    {47, "uz"}, 
    {48, "kk"}, 
    {49, "az-Cyrl"}, 
    {50, "az-Arab"}, 
    {51, "hy"}, 
    {52, "ka"}, 
    {53, "mo"}, 
    {54, "ky"}, 
    {55, "tg"}, 
    {56, "tk"}, 
    {57, "mn-Mong"}, 
    {58, "mn-Cyrl"}, 
    {59, "ps"}, 
    {60, "ku"}, 
    {61, "ks"}, 
    {62, "sd"}, 
    {63, "bo"}, 
    {64, "ne"}, 
    {65, "sa"}, 
    {66, "mr"}, 
    {67, "bn"}, 
    {68, "as"}, 
    {69, "gu"}, 
    {70, "pa"}, 
    {71, "or"}, 
    {72, "ml"}, 
    {73, "kn"}, 
    {74, "ta"}, 
    {75, "te"}, 
    {76, "si"}, 
    {77, "my"}, 
    {78, "km"}, 
    {79, "lo"}, 
    {80, "vi"}, 
    {81, "id"}, 
    {82, "tl"}, 
    {83, "ms-Latn"}, 
    {84, "ms-Arab"}, 
    {85, "am"}, 
    {86, "ti"}, 
    {87, "om"}, 
    {88, "so"}, 
    {89, "sw"}, 
    {90, "rw"}, 
    {91, "rn"}, 
    {92, "ny"}, 
    {93, "mg"}, 
    {94, "eo"}, 
    {128, "cy"}, 
    {129, "eu"}, 
    {130, "ca"}, 
    {131, "la"}, 
    {132, "qu"}, 
    {133, "gn"}, 
    {134, "ay"}, 
    {135, "tt"}, 
    {136, "ug"}, 
    {137, "dz"}, 
    {138, "jv-Latn"}, 
    {139, "su-Latn"}, 
    {140, "gl"}, 
    {141, "af"}, 
    {142, "br"}, 
    {143, "iu"}, 
    {144, "gd"}, 
    {145, "gv"}, 
    {146, "ga"}, 
    {147, "to"}, 
    {148, "el"}, 
    {149, "kl"}, 
    {150, "az-Latn"}, 
    {151, "nn"}, 

    





    { 0x0401, "ar-SA" }, 
    { 0x0402, "bg-BG" }, 
    { 0x0403, "ca-ES" }, 
    { 0x0404, "zh-TW" }, 
    { 0x0405, "cs-CZ" }, 
    { 0x0406, "da-DK" }, 
    { 0x0407, "de-DE" }, 
    { 0x0408, "el-GR" }, 
    { 0x0409, "en-US" }, 
    { 0x040a, "es-ES_tradnl" }, 
    { 0x040b, "fi-FI" }, 
    { 0x040c, "fr-FR" }, 
    { 0x040d, "he-IL" }, 
    { 0x040d, "he" }, 
    { 0x040e, "hu-HU" }, 
    { 0x040e, "hu" }, 
    { 0x040f, "is-IS" }, 
    { 0x0410, "it-IT" }, 
    { 0x0411, "ja-JP" }, 
    { 0x0412, "ko-KR" }, 
    { 0x0413, "nl-NL" }, 
    { 0x0414, "nb-NO" }, 
    { 0x0415, "pl-PL" }, 
    { 0x0416, "pt-BR" }, 
    { 0x0417, "rm-CH" }, 
    { 0x0418, "ro-RO" }, 
    { 0x0419, "ru-RU" }, 
    { 0x041a, "hr-HR" }, 
    { 0x041b, "sk-SK" }, 
    { 0x041c, "sq-AL" }, 
    { 0x041d, "sv-SE" }, 
    { 0x041e, "th-TH" }, 
    { 0x041f, "tr-TR" }, 
    { 0x0420, "ur-PK" }, 
    { 0x0421, "id-ID" }, 
    { 0x0422, "uk-UA" }, 
    { 0x0423, "be-BY" }, 
    { 0x0424, "sl-SI" }, 
    { 0x0425, "et-EE" }, 
    { 0x0426, "lv-LV" }, 
    { 0x0427, "lt-LT" }, 
    { 0x0428, "tg-Cyrl-TJ" }, 
    { 0x0429, "fa-IR" }, 
    { 0x042a, "vi-VN" }, 
    { 0x042b, "hy-AM" }, 
    { 0x042c, "az-Latn-AZ" }, 
    { 0x042d, "eu-ES" }, 
    { 0x042e, "hsb-DE" }, 
    { 0x042f, "mk-MK" }, 
    { 0x0432, "tn-ZA" }, 
    { 0x0434, "xh-ZA" }, 
    { 0x0435, "zu-ZA" }, 
    { 0x0436, "af-ZA" }, 
    { 0x0437, "ka-GE" }, 
    { 0x0438, "fo-FO" }, 
    { 0x0439, "hi-IN" }, 
    { 0x043a, "mt-MT" }, 
    { 0x043b, "se-NO" }, 
    { 0x043e, "ms-MY" }, 
    { 0x043f, "kk-KZ" }, 
    { 0x0440, "ky-KG" }, 
    { 0x0441, "sw-KE" }, 
    { 0x0442, "tk-TM" }, 
    { 0x0443, "uz-Latn-UZ" }, 
    { 0x0443, "uz" }, 
    { 0x0444, "tt-RU" }, 
    { 0x0445, "bn-IN" }, 
    { 0x0446, "pa-IN" }, 
    { 0x0447, "gu-IN" }, 
    { 0x0448, "or-IN" }, 
    { 0x0449, "ta-IN" }, 
    { 0x044a, "te-IN" }, 
    { 0x044b, "kn-IN" }, 
    { 0x044c, "ml-IN" }, 
    { 0x044d, "as-IN" }, 
    { 0x044e, "mr-IN" }, 
    { 0x044f, "sa-IN" }, 
    { 0x0450, "mn-Cyrl" }, 
    { 0x0451, "bo-CN" }, 
    { 0x0452, "cy-GB" }, 
    { 0x0453, "km-KH" }, 
    { 0x0454, "lo-LA" }, 
    { 0x0456, "gl-ES" }, 
    { 0x0457, "kok-IN" }, 
    { 0x045a, "syr-SY" }, 
    { 0x045b, "si-LK" }, 
    { 0x045d, "iu-Cans-CA" }, 
    { 0x045e, "am-ET" }, 
    { 0x0461, "ne-NP" }, 
    { 0x0462, "fy-NL" }, 
    { 0x0463, "ps-AF" }, 
    { 0x0464, "fil-PH" }, 
    { 0x0465, "dv-MV" }, 
    { 0x0468, "ha-Latn-NG" }, 
    { 0x046a, "yo-NG" }, 
    { 0x046b, "quz-BO" }, 
    { 0x046c, "nso-ZA" }, 
    { 0x046d, "ba-RU" }, 
    { 0x046e, "lb-LU" }, 
    { 0x046f, "kl-GL" }, 
    { 0x0470, "ig-NG" }, 
    { 0x0478, "ii-CN" }, 
    { 0x047a, "arn-CL" }, 
    { 0x047c, "moh-CA" }, 
    { 0x047e, "br-FR" }, 
    { 0x0480, "ug-CN" }, 
    { 0x0481, "mi-NZ" }, 
    { 0x0482, "oc-FR" }, 
    { 0x0483, "co-FR" }, 
    { 0x0484, "gsw-FR" }, 
    { 0x0485, "sah-RU" }, 
    { 0x0486, "qut-GT" }, 
    { 0x0487, "rw-RW" }, 
    { 0x0488, "wo-SN" }, 
    { 0x048c, "prs-AF" }, 
    { 0x0491, "gd-GB" }, 
    { 0x0801, "ar-IQ" }, 
    { 0x0804, "zh-Hans" }, 
    { 0x0807, "de-CH" }, 
    { 0x0809, "en-GB" }, 
    { 0x080a, "es-MX" }, 
    { 0x080c, "fr-BE" }, 
    { 0x0810, "it-CH" }, 
    { 0x0813, "nl-BE" }, 
    { 0x0814, "nn-NO" }, 
    { 0x0816, "pt-PT" }, 
    { 0x081a, "sr-Latn-CS" }, 
    { 0x081d, "sv-FI" }, 
    { 0x082c, "az-Cyrl-AZ" }, 
    { 0x082e, "dsb-DE" }, 
    { 0x082e, "dsb" }, 
    { 0x083b, "se-SE" }, 
    { 0x083c, "ga-IE" }, 
    { 0x083e, "ms-BN" }, 
    { 0x0843, "uz-Cyrl-UZ" }, 
    { 0x0845, "bn-BD" }, 
    { 0x0850, "mn-Mong-CN" }, 
    { 0x085d, "iu-Latn-CA" }, 
    { 0x085f, "tzm-Latn-DZ" }, 
    { 0x086b, "quz-EC" }, 
    { 0x0c01, "ar-EG" }, 
    { 0x0c04, "zh-Hant" }, 
    { 0x0c07, "de-AT" }, 
    { 0x0c09, "en-AU" }, 
    { 0x0c0a, "es-ES" }, 
    { 0x0c0c, "fr-CA" }, 
    { 0x0c1a, "sr-Cyrl-CS" }, 
    { 0x0c3b, "se-FI" }, 
    { 0x0c6b, "quz-PE" }, 
    { 0x1001, "ar-LY" }, 
    { 0x1004, "zh-SG" }, 
    { 0x1007, "de-LU" }, 
    { 0x1009, "en-CA" }, 
    { 0x100a, "es-GT" }, 
    { 0x100c, "fr-CH" }, 
    { 0x101a, "hr-BA" }, 
    { 0x103b, "smj-NO" }, 
    { 0x1401, "ar-DZ" }, 
    { 0x1404, "zh-MO" }, 
    { 0x1407, "de-LI" }, 
    { 0x1409, "en-NZ" }, 
    { 0x140a, "es-CR" }, 
    { 0x140c, "fr-LU" }, 
    { 0x141a, "bs-Latn-BA" }, 
    { 0x141a, "bs" }, 
    { 0x143b, "smj-SE" }, 
    { 0x143b, "smj" }, 
    { 0x1801, "ar-MA" }, 
    { 0x1809, "en-IE" }, 
    { 0x180a, "es-PA" }, 
    { 0x180c, "fr-MC" }, 
    { 0x181a, "sr-Latn-BA" }, 
    { 0x183b, "sma-NO" }, 
    { 0x1c01, "ar-TN" }, 
    { 0x1c09, "en-ZA" }, 
    { 0x1c0a, "es-DO" }, 
    { 0x1c1a, "sr-Cyrl-BA" }, 
    { 0x1c3b, "sma-SE" }, 
    { 0x1c3b, "sma" }, 
    { 0x2001, "ar-OM" }, 
    { 0x2009, "en-JM" }, 
    { 0x200a, "es-VE" }, 
    { 0x201a, "bs-Cyrl-BA" }, 
    { 0x201a, "bs-Cyrl" }, 
    { 0x203b, "sms-FI" }, 
    { 0x203b, "sms" }, 
    { 0x2401, "ar-YE" }, 
    { 0x2409, "en-029" }, 
    { 0x240a, "es-CO" }, 
    { 0x241a, "sr-Latn-RS" }, 
    { 0x243b, "smn-FI" }, 
    { 0x2801, "ar-SY" }, 
    { 0x2809, "en-BZ" }, 
    { 0x280a, "es-PE" }, 
    { 0x281a, "sr-Cyrl-RS" }, 
    { 0x2c01, "ar-JO" }, 
    { 0x2c09, "en-TT" }, 
    { 0x2c0a, "es-AR" }, 
    { 0x2c1a, "sr-Latn-ME" }, 
    { 0x3001, "ar-LB" }, 
    { 0x3009, "en-ZW" }, 
    { 0x300a, "es-EC" }, 
    { 0x301a, "sr-Cyrl-ME" }, 
    { 0x3401, "ar-KW" }, 
    { 0x3409, "en-PH" }, 
    { 0x340a, "es-CL" }, 
    { 0x3801, "ar-AE" }, 
    { 0x380a, "es-UY" }, 
    { 0x3c01, "ar-BH" }, 
    { 0x3c0a, "es-PY" }, 
    { 0x4001, "ar-QA" }, 
    { 0x4009, "en-IN" }, 
    { 0x400a, "es-BO" }, 
    { 0x4409, "en-MY" }, 
    { 0x440a, "es-SV" }, 
    { 0x4809, "en-SG" }, 
    { 0x480a, "es-HN" }, 
    { 0x4c0a, "es-NI" }, 
    { 0x500a, "es-PR" }, 
    { 0x540a, "es-US" }, 
};

namespace {
bool BCP47FromLanguageIdLess(const BCP47FromLanguageId& a, const BCP47FromLanguageId& b) {
    return a.languageID < b.languageID;
}
}  

bool SkOTTableName::Iterator::next(SkOTTableName::Iterator::Record& record) {
    SkOTTableName nameTable;
    if (fNameTableSize < sizeof(nameTable)) {
        return false;
    }
    memcpy(&nameTable, fNameTable, sizeof(nameTable));

    const uint8_t* nameRecords = fNameTable + sizeof(nameTable);
    const size_t nameRecordsSize = fNameTableSize - sizeof(nameTable);

    const size_t stringTableOffset = SkEndian_SwapBE16(nameTable.stringOffset);
    if (fNameTableSize < stringTableOffset) {
        return false;
    }
    const uint8_t* stringTable = fNameTable + stringTableOffset;
    const size_t stringTableSize = fNameTableSize - stringTableOffset;

    
    SkOTTableName::Record nameRecord;
    const size_t nameRecordsCount = SkEndian_SwapBE16(nameTable.count);
    const size_t nameRecordsMax = std::min(nameRecordsCount, nameRecordsSize / sizeof(nameRecord));
    do {
        if (fIndex >= nameRecordsMax) {
            return false;
        }

        memcpy(&nameRecord, nameRecords + sizeof(nameRecord)*fIndex, sizeof(nameRecord));
        ++fIndex;
    } while (fType != -1 && nameRecord.nameID.fontSpecific != fType);

    record.type = nameRecord.nameID.fontSpecific;

    
    const size_t nameOffset = SkEndian_SwapBE16(nameRecord.offset);
    const size_t nameLength = SkEndian_SwapBE16(nameRecord.length);
    if (stringTableSize < nameOffset + nameLength) {
        return false; 
    }
    const uint8_t* nameString = stringTable + nameOffset;
    switch (nameRecord.platformID.value) {
        case SkOTTableName::Record::PlatformID::Windows:
            if (SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2
                   != nameRecord.encodingID.windows.value
                && SkOTTableName::Record::EncodingID::Windows::UnicodeUCS4
                   != nameRecord.encodingID.windows.value
                && SkOTTableName::Record::EncodingID::Windows::Symbol
                   != nameRecord.encodingID.windows.value)
            {
                record.name.reset();
                break; 
            }
            [[fallthrough]];
        case SkOTTableName::Record::PlatformID::Unicode:
        case SkOTTableName::Record::PlatformID::ISO:
            SkString_from_UTF16BE(nameString, nameLength, record.name);
            break;

        case SkOTTableName::Record::PlatformID::Macintosh:
            
            if (SkOTTableName::Record::EncodingID::Macintosh::Roman
                != nameRecord.encodingID.macintosh.value)
            {
                record.name.reset();
                break;  
            }
            SkStringFromMacRoman(nameString, nameLength, record.name);
            break;

        case SkOTTableName::Record::PlatformID::Custom:
            
        default:
            SkASSERT(false);
            record.name.reset();
            break;  
    }

    
    const uint16_t languageID = SkEndian_SwapBE16(nameRecord.languageID.languageTagID);

    
    if (SkOTTableName::format_1 == nameTable.format && languageID >= 0x8000) {
        const uint16_t languageTagRecordIndex = languageID - 0x8000;

        if (nameRecordsSize < sizeof(nameRecord)*nameRecordsCount) {
            return false; 
        }
        const uint8_t* format1extData = nameRecords + sizeof(nameRecord)*nameRecordsCount;
        size_t format1extSize = nameRecordsSize - sizeof(nameRecord)*nameRecordsCount;
        SkOTTableName::Format1Ext format1ext;
        if (format1extSize < sizeof(format1ext)) {
            return false; 
        }
        memcpy(&format1ext, format1extData, sizeof(format1ext));

        const uint8_t* languageTagRecords = format1extData + sizeof(format1ext);
        size_t languageTagRecordsSize = format1extSize - sizeof(format1ext);
        if (languageTagRecordIndex < SkEndian_SwapBE16(format1ext.langTagCount)) {
            SkOTTableName::Format1Ext::LangTagRecord languageTagRecord;
            if (languageTagRecordsSize < sizeof(languageTagRecord)*(languageTagRecordIndex+1)) {
                return false; 
            }
            const uint8_t* languageTagData = languageTagRecords
                                           + sizeof(languageTagRecord)*languageTagRecordIndex;
            memcpy(&languageTagRecord, languageTagData, sizeof(languageTagRecord));

            uint16_t languageOffset = SkEndian_SwapBE16(languageTagRecord.offset);
            uint16_t languageLength = SkEndian_SwapBE16(languageTagRecord.length);

            if (fNameTableSize < stringTableOffset + languageOffset + languageLength) {
                return false; 
            }
            const uint8_t* languageString = stringTable + languageOffset;
            SkString_from_UTF16BE(languageString, languageLength, record.language);
            return true;
        }
    }

    
    const BCP47FromLanguageId target = { languageID, "" };
    int languageIndex = SkTSearch<BCP47FromLanguageId, BCP47FromLanguageIdLess>(
        BCP47FromLanguageID, std::size(BCP47FromLanguageID), target, sizeof(target));
    if (languageIndex >= 0) {
        record.language = BCP47FromLanguageID[languageIndex].bcp47;
        return true;
    }

    
    record.language = "und";
    return true;
}
