2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2020-05-19 16:05:53 +00:00
|
|
|
** Copyright (C) 2020 The Qt Company Ltd.
|
2016-01-15 12:36:27 +00:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the utils of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
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
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
|
2019-02-22 16:49:45 +00:00
|
|
|
const QString quadQuote = QStringLiteral("\"\""); // Closes one string, opens a new one.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
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);
|
2019-02-22 16:49:45 +00:00
|
|
|
bool wasHex = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
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);
|
2019-02-22 16:49:45 +00:00
|
|
|
wasHex = true;
|
2011-04-27 10:05:43 +00:00
|
|
|
} 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"
|
|
|
|
bool isHexChar = ((c >= '0' && c <= '9') ||
|
2019-02-22 16:49:45 +00:00
|
|
|
(c >= 'a' && c <= 'f') ||
|
|
|
|
(c >= 'A' && c <= 'F'));
|
|
|
|
if (wasHex && isHexChar)
|
|
|
|
result += quadQuote;
|
2011-04-27 10:05:43 +00:00
|
|
|
result += c;
|
2019-02-22 16:49:45 +00:00
|
|
|
wasHex = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-22 16:49:45 +00:00
|
|
|
/*
|
|
|
|
Digest public suffix data into efficiently-searchable form.
|
|
|
|
|
|
|
|
Takes the public suffix list (see usage message), a list of DNS domains
|
|
|
|
whose child domains should not be presumed to trust one another, and
|
2020-05-19 16:16:16 +00:00
|
|
|
converts it to a form that lets qtbase/src/network/kernel/qtldurl.cpp's query
|
2019-02-22 16:49:45 +00:00
|
|
|
functions find entries efficiently.
|
|
|
|
|
|
|
|
Each line of the suffix file (aside from comments and blanks) gives a suffix
|
|
|
|
(starting with a dot) with an optional prefix of '*' (to include every
|
|
|
|
immediate child) or of '!' (to exclude the suffix, e.g. from a '*' line for
|
|
|
|
a tail of it). A line with neither of these prefixes is an exact match.
|
|
|
|
|
|
|
|
Each line is hashed and the hash is reduced modulo the number of lines
|
|
|
|
(tldCount); lines are grouped by reduced hash and separated by '\0' bytes
|
|
|
|
within each group. Conceptually, the groups are then emitted to a single
|
|
|
|
huge string, along with a table (tldIndices[tldCount]) of indices into that
|
|
|
|
string of the starts of the the various groups.
|
|
|
|
|
|
|
|
However, that huge string would exceed the 64k limit at least one compiler
|
|
|
|
imposes on a single string literal, so we actually split up the huge string
|
|
|
|
into an array of chunks, each less than 64k in size. Each group is written
|
|
|
|
to a single chunk (so we start a new chunk if the next group would take the
|
|
|
|
present chunk over the limit). There are tldChunkCount chunks; their lengths
|
|
|
|
are saved in tldChunks[tldChunkCount]; the chunks themselves in
|
|
|
|
tldData[tldChunkCount]. See qtldurl.cpp's containsTLDEntry() for how to
|
|
|
|
search for a string in the resulting data.
|
|
|
|
*/
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-02-22 16:49:45 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2011-04-27 10:05:43 +00:00
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
if (argc < 3) {
|
2019-02-20 11:17:19 +00:00
|
|
|
printf("\nUsage: ./%s inputFile outputFile\n\n", argv[0]);
|
2011-04-27 10:05:43 +00:00
|
|
|
printf("'inputFile' should be a list of effective TLDs, one per line,\n");
|
2019-02-20 11:17:19 +00:00
|
|
|
printf("as obtained from http://publicsuffix.org/. To create indices and data\n");
|
2011-04-27 10:05:43 +00:00
|
|
|
printf("file, do the following:\n\n");
|
2021-01-21 12:36:14 +00:00
|
|
|
printf(" wget https://publicsuffix.org/list/public_suffix_list.dat -O suffixes.dat\n");
|
|
|
|
printf(" ./%s suffixes.dat public_suffix_list.cpp\n\n", argv[0]);
|
|
|
|
printf("Then replace the code in qtbase/src/network/kernel/qurltlds_p.h\n"
|
|
|
|
"with public_suffix_list.cpp's contents\n\n");
|
2019-02-20 11:17:19 +00:00
|
|
|
return 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
QFile file(argv[1]);
|
2019-02-22 16:49:45 +00:00
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2019-10-23 14:16:56 +00:00
|
|
|
fprintf(stderr, "Failed to open input file (%s); see %s -usage", argv[1], argv[0]);
|
2019-02-22 16:49:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-02-22 16:49:45 +00:00
|
|
|
QFile outFile(argv[2]);
|
|
|
|
if (!outFile.open(QIODevice::WriteOnly)) {
|
2019-10-23 14:16:56 +00:00
|
|
|
file.close();
|
|
|
|
fprintf(stderr, "Failed to open output file (%s); see %s -usage", argv[2], argv[0]);
|
2019-02-22 16:49:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-02-22 16:49:45 +00:00
|
|
|
// Write tldData[] and tldIndices[] in one scan of the (input) file, but
|
|
|
|
// buffer tldData[] so we don'te interleave them in the outFile.
|
2011-04-27 10:05:43 +00:00
|
|
|
QByteArray outDataBufferBA;
|
|
|
|
QBuffer outDataBuffer(&outDataBufferBA);
|
|
|
|
outDataBuffer.open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
int lineCount = 0;
|
|
|
|
while (!file.atEnd()) {
|
2021-01-21 12:36:14 +00:00
|
|
|
QString st = QString::fromUtf8(file.readLine()).trimmed();
|
|
|
|
if (st.isEmpty() || st.startsWith(u"//"))
|
|
|
|
continue;
|
2011-04-27 10:05:43 +00:00
|
|
|
lineCount++;
|
|
|
|
}
|
2019-02-22 16:49:45 +00:00
|
|
|
outFile.write("static const quint16 tldCount = ");
|
|
|
|
outFile.write(QByteArray::number(lineCount));
|
|
|
|
outFile.write(";\n");
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
file.reset();
|
2020-07-07 10:04:21 +00:00
|
|
|
QStringList strings(lineCount);
|
2011-04-27 10:05:43 +00:00
|
|
|
while (!file.atEnd()) {
|
2019-02-22 16:49:45 +00:00
|
|
|
QString st = QString::fromUtf8(file.readLine()).trimmed();
|
2021-01-21 12:36:14 +00:00
|
|
|
if (st.isEmpty() || st.startsWith(u"//"))
|
|
|
|
continue;
|
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;
|
2019-02-22 16:49:45 +00:00
|
|
|
QString &entry = strings[num];
|
|
|
|
st = utf8encode(st.toUtf8());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-02-22 16:49:45 +00:00
|
|
|
// For domain 1.com, we could get something like a.com\01.com, which
|
|
|
|
// would be misinterpreted as octal 01, so we need to separate such
|
|
|
|
// strings with quotes:
|
|
|
|
if (!entry.isEmpty() && st.at(0).isDigit())
|
|
|
|
entry.append(quadQuote);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-02-22 16:49:45 +00:00
|
|
|
entry.append(st);
|
|
|
|
entry.append("\\0");
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2020-05-19 16:05:53 +00:00
|
|
|
|
|
|
|
outFile.write("// After the tldCount \"real\" entries in tldIndices, include a final entry\n");
|
|
|
|
outFile.write("// that records the sum of the lengths of all the chunks, i.e. the index\n");
|
|
|
|
outFile.write("// just past the end of tldChunks.\n");
|
2020-05-19 16:18:20 +00:00
|
|
|
outFile.write("static constexpr quint32 tldIndices[tldCount + 1] = {\n");
|
|
|
|
outDataBuffer.write("static const char * const tldData[tldChunkCount] = {");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-02-08 16:48:05 +00:00
|
|
|
int totalUtf8Size = 0;
|
2019-02-26 10:33:13 +00:00
|
|
|
int chunkSize = 0; // strlen of the current chunk (sizeof is bigger by 1)
|
2013-02-08 16:48:05 +00:00
|
|
|
QStringList chunks;
|
2011-04-27 10:05:43 +00:00
|
|
|
for (int a = 0; a < lineCount; a++) {
|
2019-02-22 16:49:45 +00:00
|
|
|
outFile.write(QByteArray::number(totalUtf8Size));
|
|
|
|
outFile.write(",\n");
|
|
|
|
const QString &entry = strings.at(a);
|
|
|
|
if (!entry.isEmpty()) {
|
|
|
|
const int zeroCount = entry.count(QLatin1String("\\0"));
|
|
|
|
const int utf8CharsCount = entry.count(QLatin1String("\\x"));
|
|
|
|
const int quoteCount = entry.count('"');
|
|
|
|
const int stringUtf8Size = entry.count() - (zeroCount + quoteCount + utf8CharsCount * 3);
|
|
|
|
chunkSize += stringUtf8Size;
|
|
|
|
// MSVC 2015 chokes if sizeof(a single string) > 0xffff
|
|
|
|
if (chunkSize >= 0xffff) {
|
|
|
|
static int chunkCount = 0;
|
|
|
|
qWarning() << "chunk" << ++chunkCount << "has length" << chunkSize - stringUtf8Size;
|
2019-02-26 11:26:54 +00:00
|
|
|
outDataBuffer.write(",\n");
|
2019-02-22 16:49:45 +00:00
|
|
|
chunks.append(QString::number(totalUtf8Size));
|
|
|
|
chunkSize = 0;
|
|
|
|
}
|
|
|
|
totalUtf8Size += stringUtf8Size;
|
|
|
|
|
2019-02-26 11:26:54 +00:00
|
|
|
outDataBuffer.write("\n\"");
|
2019-02-22 16:49:45 +00:00
|
|
|
outDataBuffer.write(entry.toUtf8());
|
2019-02-26 11:26:54 +00:00
|
|
|
outDataBuffer.write("\"");
|
2013-02-08 16:48:05 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2019-02-22 16:49:45 +00:00
|
|
|
chunks.append(QString::number(totalUtf8Size));
|
2020-05-19 16:05:53 +00:00
|
|
|
|
|
|
|
// Write one extra entry, at tldIndices[tldCount], that contains the total size.
|
2019-02-22 16:49:45 +00:00
|
|
|
outFile.write(QByteArray::number(totalUtf8Size));
|
2019-02-26 11:26:54 +00:00
|
|
|
outFile.write("\n};\n");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-02-26 11:26:54 +00:00
|
|
|
outDataBuffer.write("\n};\n");
|
2011-04-27 10:05:43 +00:00
|
|
|
outDataBuffer.close();
|
2013-02-08 16:48:05 +00:00
|
|
|
|
2020-05-19 16:05:53 +00:00
|
|
|
// First we have to define tldChunkCount.
|
2013-02-08 16:48:05 +00:00
|
|
|
outFile.write("\nstatic const quint16 tldChunkCount = ");
|
|
|
|
outFile.write(QByteArray::number(chunks.count()));
|
2020-05-19 16:05:53 +00:00
|
|
|
outFile.write(";\n");
|
|
|
|
|
|
|
|
// Write tldData[tldChunkCount] = {...}.
|
|
|
|
outFile.write(outDataBufferBA);
|
|
|
|
|
2020-05-19 16:18:20 +00:00
|
|
|
outFile.write("static constexpr quint32 tldChunks[tldChunkCount] = {");
|
2013-02-08 16:48:05 +00:00
|
|
|
outFile.write(chunks.join(", ").toLatin1());
|
|
|
|
outFile.write("};\n");
|
2011-04-27 10:05:43 +00:00
|
|
|
outFile.close();
|
2020-02-12 13:09:29 +00:00
|
|
|
printf("Data generated to %s - now revise qtbase/src/network/kernel/qurltlds_p.h to use this data.\n", argv[2]);
|
2019-02-22 16:49:45 +00:00
|
|
|
return 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|