2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2015-01-28 08:44:43 +00:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the utils of the Qt Toolkit.
|
|
|
|
**
|
2014-08-21 13:51:22 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see http://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at http://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-08-21 13:51:22 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2015-01-28 08:44:43 +00:00
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2011-04-27 10:05:43 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
|
|
|
|
static QString utf8encode(const QByteArray &array) // turns e.g. tranøy.no to tran\xc3\xb8y.no
|
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
result.reserve(array.length() + array.length() / 3);
|
|
|
|
for (int i = 0; i < array.length(); ++i) {
|
|
|
|
char c = array.at(i);
|
|
|
|
// if char is non-ascii, escape it
|
|
|
|
if (c < 0x20 || uchar(c) >= 0x7f) {
|
|
|
|
result += "\\x" + QString::number(uchar(c), 16);
|
|
|
|
} else {
|
|
|
|
// if previous char was escaped, we need to make sure the next char is not
|
|
|
|
// interpreted as part of the hex value, e.g. "äc.com" -> "\xabc.com"; this
|
|
|
|
// should be "\xab""c.com"
|
|
|
|
QRegExp hexEscape("\\\\x[a-fA-F0-9][a-fA-F0-9]$");
|
|
|
|
bool isHexChar = ((c >= '0' && c <= '9') ||
|
|
|
|
(c >= 'a' && c <= 'f') ||
|
|
|
|
(c >= 'A' && c <= 'F'));
|
|
|
|
if (result.contains(hexEscape) && isHexChar)
|
|
|
|
result += "\"\"";
|
|
|
|
result += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
if (argc < 3) {
|
|
|
|
printf("\nusage: %s inputFile outputFile\n\n", argv[0]);
|
|
|
|
printf("'inputFile' should be a list of effective TLDs, one per line,\n");
|
|
|
|
printf("as obtained from http://publicsuffix.org . To create indices and data file\n");
|
|
|
|
printf("file, do the following:\n\n");
|
2015-02-12 12:29:07 +00:00
|
|
|
printf(" wget https://publicsuffix.org/list/effective_tld_names.dat -O effective_tld_names.dat\n");
|
2011-04-27 10:05:43 +00:00
|
|
|
printf(" grep '^[^\\/\\/]' effective_tld_names.dat > effective_tld_names.dat.trimmed\n");
|
|
|
|
printf(" %s effective_tld_names.dat.trimmed effective_tld_names.dat.qt\n\n", argv[0]);
|
2011-05-24 09:04:27 +00:00
|
|
|
printf("Now copy the data from effective_tld_names.dat.qt to the file src/corelib/io/qurltlds_p.h in your Qt repo\n\n");
|
2011-04-27 10:05:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
QFile file(argv[1]);
|
|
|
|
QFile outFile(argv[2]);
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
outFile.open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
QByteArray outIndicesBufferBA;
|
|
|
|
QBuffer outIndicesBuffer(&outIndicesBufferBA);
|
|
|
|
outIndicesBuffer.open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
QByteArray outDataBufferBA;
|
|
|
|
QBuffer outDataBuffer(&outDataBufferBA);
|
|
|
|
outDataBuffer.open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
int lineCount = 0;
|
|
|
|
while (!file.atEnd()) {
|
|
|
|
file.readLine();
|
|
|
|
lineCount++;
|
|
|
|
}
|
|
|
|
file.reset();
|
|
|
|
QVector<QString> strings(lineCount);
|
|
|
|
while (!file.atEnd()) {
|
|
|
|
QString s = QString::fromUtf8(file.readLine());
|
|
|
|
QString st = s.trimmed();
|
Stop relying on qHash always giving the same results
The implementation of the various qHash overloads offered by
Qt can change at any time for any reason
(speed, quality, security, ...).
Therefore, relying on the fact that qHash will always give
an identical result across Qt versions (... across different
processes, etc.), given identical input, is wrong.
Note that this also implies that one cannot rely on QHash
having a stable ordering (even without the random qHash seed).
For such use cases, one must use f.i. a private hash function
that will never change outside his own control.
This patch adds a private hash function for QStrings,
which is identical to the Qt(4) qHash(QString) implementation.
A couple of spots in Qt where the results of a qHash call were
actually saved on disk are ported to use the new function,
and a bit of documentation is added to QHash docs.
Change-Id: Ia3731ea26ac68649b535b95e9f36fbec3df693c8
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
2012-03-22 09:32:03 +00:00
|
|
|
int num = qt_hash(st) % lineCount;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QString utf8String = utf8encode(st.toUtf8());
|
|
|
|
|
|
|
|
// for domain 1.com, we could get something like
|
|
|
|
// a.com\01.com, which would be interpreted as octal 01,
|
|
|
|
// so we need to separate those strings with quotes
|
|
|
|
QRegExp regexpOctalEscape(QLatin1String("^[0-9]"));
|
|
|
|
if (!strings.at(num).isEmpty() && st.contains(regexpOctalEscape))
|
|
|
|
strings[num].append("\"\"");
|
|
|
|
|
|
|
|
strings[num].append(utf8String);
|
|
|
|
strings[num].append("\\0");
|
|
|
|
}
|
|
|
|
|
|
|
|
outIndicesBuffer.write("static const quint16 tldCount = ");
|
|
|
|
outIndicesBuffer.write(QByteArray::number(lineCount));
|
|
|
|
outIndicesBuffer.write(";\n");
|
2013-02-08 16:48:05 +00:00
|
|
|
outIndicesBuffer.write("static const quint32 tldIndices[");
|
2011-04-27 10:05:43 +00:00
|
|
|
// outIndicesBuffer.write(QByteArray::number(lineCount+1)); // not needed
|
|
|
|
outIndicesBuffer.write("] = {\n");
|
|
|
|
|
2013-02-08 16:48:05 +00:00
|
|
|
int totalUtf8Size = 0;
|
|
|
|
int chunkSize = 0;
|
|
|
|
int stringUtf8Size = 0;
|
|
|
|
QStringList chunks;
|
2011-04-27 10:05:43 +00:00
|
|
|
for (int a = 0; a < lineCount; a++) {
|
|
|
|
bool lineIsEmpty = strings.at(a).isEmpty();
|
|
|
|
if (!lineIsEmpty) {
|
|
|
|
strings[a].prepend("\"");
|
|
|
|
strings[a].append("\"");
|
|
|
|
}
|
|
|
|
int zeroCount = strings.at(a).count(QLatin1String("\\0"));
|
|
|
|
int utf8CharsCount = strings.at(a).count(QLatin1String("\\x"));
|
|
|
|
int quoteCount = strings.at(a).count('"');
|
2013-02-08 16:48:05 +00:00
|
|
|
stringUtf8Size = strings.at(a).count() - (zeroCount + quoteCount + utf8CharsCount * 3);
|
|
|
|
chunkSize += stringUtf8Size;
|
|
|
|
if (chunkSize > 65535) {
|
|
|
|
static int chunkCount = 0;
|
|
|
|
qWarning() << "chunk" << ++chunkCount << "has length" << chunkSize - stringUtf8Size;
|
|
|
|
outDataBuffer.write(",\n\n");
|
|
|
|
chunks.append(QByteArray::number(totalUtf8Size));
|
|
|
|
chunkSize = 0;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
outDataBuffer.write(strings.at(a).toUtf8());
|
|
|
|
if (!lineIsEmpty)
|
|
|
|
outDataBuffer.write("\n");
|
2013-02-08 16:48:05 +00:00
|
|
|
outIndicesBuffer.write(QByteArray::number(totalUtf8Size));
|
2011-04-27 10:05:43 +00:00
|
|
|
outIndicesBuffer.write(",\n");
|
2013-02-08 16:48:05 +00:00
|
|
|
totalUtf8Size += stringUtf8Size;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2013-02-08 16:48:05 +00:00
|
|
|
chunks.append(QByteArray::number(totalUtf8Size));
|
|
|
|
outIndicesBuffer.write(QByteArray::number(totalUtf8Size));
|
2011-04-27 10:05:43 +00:00
|
|
|
outIndicesBuffer.write("};\n");
|
|
|
|
outIndicesBuffer.close();
|
|
|
|
outFile.write(outIndicesBufferBA);
|
|
|
|
|
|
|
|
outDataBuffer.close();
|
2013-02-08 16:48:05 +00:00
|
|
|
outFile.write("\nstatic const char *tldData[");
|
2011-04-27 10:05:43 +00:00
|
|
|
// outFile.write(QByteArray::number(charSize)); // not needed
|
|
|
|
outFile.write("] = {\n");
|
|
|
|
outFile.write(outDataBufferBA);
|
|
|
|
outFile.write("};\n");
|
2013-02-08 16:48:05 +00:00
|
|
|
|
|
|
|
// write chunk information
|
|
|
|
outFile.write("\nstatic const quint16 tldChunkCount = ");
|
|
|
|
outFile.write(QByteArray::number(chunks.count()));
|
|
|
|
outFile.write(";\nstatic const quint32 tldChunks[] = {");
|
|
|
|
outFile.write(chunks.join(", ").toLatin1());
|
|
|
|
outFile.write("};\n");
|
2011-04-27 10:05:43 +00:00
|
|
|
outFile.close();
|
2011-05-24 09:04:27 +00:00
|
|
|
printf("data generated to %s . Now copy the data from this file to src/corelib/io/qurltlds_p.h in your Qt repo\n", argv[2]);
|
2011-04-27 10:05:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|