Exemplo n.º 1
0
static QString evilBytes(const QString& str,
    bool isUtf8, int format, const QByteArray &codecName)
{
    //qDebug() << "EVIL: " << str << isUtf8 << format << codecName;
    if (isUtf8)
        return protect(str);
    if (format == 20)
        return protect(str);
    if (codecName == "UTF-8")
        return protect(str);
    QTextCodec *codec = QTextCodec::codecForName(codecName);
    if (!codec)
        return protect(str);
    QString t = QString::fromLatin1(codec->fromUnicode(protect(str)).data());
    int len = (int) t.length();
    QString result;
    // FIXME: Factor is sensible only for latin scripts, probably.
    result.reserve(t.length() * 2);
    for (int k = 0; k < len; k++) {
        if (t[k].unicode() >= 0x7f)
            result += numericEntity(t[k].unicode());
        else
            result += t[k];
    }
    return result;
}
Exemplo n.º 2
0
static QString protect(const QString &str)
{
    QString result;
    result.reserve(str.length() * 12 / 10);
    for (int i = 0; i != str.size(); ++i) {
        uint c = str.at(i).unicode();
        switch (c) {
        case '\"':
            result += QLatin1String("&quot;");
            break;
        case '&':
            result += QLatin1String("&amp;");
            break;
        case '>':
            result += QLatin1String("&gt;");
            break;
        case '<':
            result += QLatin1String("&lt;");
            break;
        case '\'':
            result += QLatin1String("&apos;");
            break;
        default:
            if (c < 0x20 && c != '\r' && c != '\n' && c != '\t')
                result += numericEntity(c);
            else // this also covers surrogates
                result += QChar(c);
        }
    }
    return result;
}
Exemplo n.º 3
0
static QString
protect(const QCString& str)
{
    QString result;
    int len = (int) str.length();
    for (int k = 0; k < len; k++) {
	switch (str[k]) {
	case '\"':
	    result += QString("&quot;");
	    break;
	case '&':
	    result += QString("&amp;");
	    break;
	case '>':
	    result += QString("&gt;");
	    break;
	case '<':
	    result += QString("&lt;");
	    break;
	case '\'':
	    result += QString("&apos;");
	    break;
	default:
	    if (uchar(str[k]) < 0x20 && str[k] != '\n')
		result += numericEntity(uchar(str[k]));
	    else
		result += str[k];
	}
    }
    return result;
}
Exemplo n.º 4
0
static QString protect( const QByteArray& str )
{
    QString result;
    int len = (int) str.length();
    for ( int k = 0; k < len; k++ ) {
        switch( str[k] ) {
        case '\"':
            result += QLatin1String( "&quot;" );
            break;
        case '&':
            result += QLatin1String( "&amp;" );
            break;
        case '>':
            result += QLatin1String( "&gt;" );
            break;
        case '<':
            result += QLatin1String( "&lt;" );
            break;
        case '\'':
            result += QLatin1String( "&apos;" );
            break;
        default:
            if ( (uchar) str[k] < 0x20 )
                result += numericEntity( (uchar) str[k] );
            else
                result += QLatin1Char(str[k]);
        }
    }
    return result;
}
static QString evilBytes( const QCString& str, bool utf8 )
{
    if ( utf8 ) {
	return protect( str );
    } else {
	QString result;
	QCString t = protect( str ).latin1();
	int len = (int) t.length();
	for ( int k = 0; k < len; k++ ) {
	    if ( (uchar) t[k] >= 0x7f )
		result += numericEntity( (uchar) t[k] );
	    else
		result += QChar( t[k] );
	}
	return result;
    }
}