Esempio n. 1
0
static QString decode_text(const unsigned char *buf, uint length)
{
    // Decode using the correct text codec
    if (buf[0] >= 0x20)
    {
        return decode_iso6937(buf, length);
    }
    else if ((buf[0] >= 0x01) && (buf[0] <= 0x0B))
    {
        return iso8859_codecs[4 + buf[0]]->toUnicode((char*)(buf + 1), length - 1);
    }
    else if (buf[0] == 0x10)
    {
        // If the first byte of the text field has a value "0x10"
        // then the following two bytes carry a 16-bit value (uimsbf) N
        // to indicate that the remaining data of the text field is
        // coded using the character code table specified by
        // ISO Standard 8859, parts 1 to 9

        uint code = buf[1] << 8 | buf[2];
        if (code <= 15)
            return iso8859_codecs[code]->toUnicode((char*)(buf + 3), length - 3);
        else
            return QString::fromLocal8Bit((char*)(buf + 3), length - 3);
    }
    else
    {
        // Unknown/invalid encoding - assume local8Bit
        return QString::fromLocal8Bit((char*)(buf + 1), length - 1);
    }
}
Esempio n. 2
0
static QString decode_text(const unsigned char *buf, uint length)
{
    // Only some of the QTextCodec calls are reentrant.
    // If you use this please verify that you are using a reentrant call.
    static const QTextCodec *iso8859_codecs[16] =
    {
        QTextCodec::codecForName("Latin1"),
        QTextCodec::codecForName("ISO8859-1"),  // Western
        QTextCodec::codecForName("ISO8859-2"),  // Central European
        QTextCodec::codecForName("ISO8859-3"),  // Central European
        QTextCodec::codecForName("ISO8859-4"),  // Baltic
        QTextCodec::codecForName("ISO8859-5"),  // Cyrillic
        QTextCodec::codecForName("ISO8859-6"),  // Arabic
        QTextCodec::codecForName("ISO8859-7"),  // Greek
        QTextCodec::codecForName("ISO8859-8"),  // Hebrew, visually ordered
        QTextCodec::codecForName("ISO8859-9"),  // Turkish
        QTextCodec::codecForName("ISO8859-10"),
        QTextCodec::codecForName("ISO8859-11"),
        QTextCodec::codecForName("ISO8859-12"),
        QTextCodec::codecForName("ISO8859-13"),
        QTextCodec::codecForName("ISO8859-14"),
        QTextCodec::codecForName("ISO8859-15"), // Western
    };

    // Decode using the correct text codec
    if (buf[0] >= 0x20)
    {
        return decode_iso6937(buf, length);
    }
    else if ((buf[0] >= 0x01) && (buf[0] <= 0x0B))
    {
        return iso8859_codecs[4 + buf[0]]->toUnicode((char*)(buf + 1), length - 1);
    }
    else if (buf[0] == 0x10)
    {
        // If the first byte of the text field has a value "0x10"
        // then the following two bytes carry a 16-bit value (uimsbf) N
        // to indicate that the remaining data of the text field is
        // coded using the character code table specified by
        // ISO Standard 8859, parts 1 to 9

        uint code = buf[1] << 8 | buf[2];
        if (code <= 15)
            return iso8859_codecs[code]->toUnicode((char*)(buf + 3), length - 3);
        else
            return QString::fromLocal8Bit((char*)(buf + 3), length - 3);
    }
    else if (buf[0] == 0x15) // Already Unicode
    {
        return QString::fromUtf8((char*)(buf + 1), length - 1);
    }
    else
    {
        // Unknown/invalid encoding - assume local8Bit
        return QString::fromLocal8Bit((char*)(buf + 1), length - 1);
    }
}