mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 09:33:25 -08:00
* [DeckList] Apply include-what-you-use to libcockatrice_deck_list * [IWYU] Apply include-what-you-use across the tree Run one whole-tree IWYU pass over the libcockatrice_* libraries, the Cockatrice client, Oracle, Servatrice and the test suite, so each translation unit includes what it uses directly and forward-declares what it does not need. IWYU only analyses real translation units, never the moc code CMake generates from Q_OBJECT headers, so it strips includes that moc output silently relies on. Where a type is passed by value (or nested in a QList/QMap) in a signal or slot signature, the generated moc code needs the complete type; those includes are kept with an explanatory `// IWYU pragma: keep`. The same holds for Q_DECLARE_METATYPE targets such as QSqlDatabase. A handful of includes are added by hand where callers brace-initialize a const-reference parameter, which also requires the complete type. Applied as a single commit on purpose: stripping includes from the libcockatrice_* headers is precisely what breaks the client, test and server translation units, and the fixes for those live in the same translation units. Splitting by component would therefore leave intermediate commits that do not compile. All 426 modified Q_OBJECT/Q_GADGET headers were verified to be moc-self-contained by compiling each generated moc_*.cpp standalone; several built only because of moc include ordering in mocs_compilation.cpp before the keep-includes were added. * [Tooling] Emit public Qt headers and fix IWYU fallout CI found Three problems the local build could not see, all because a green local build only proves the configuration it was run under. Private Qt headers. IWYU emitted private, lowercase Qt headers, and qtypes.h does not exist at all before Qt 6.9, so the Debian 12 and Ubuntu 24.04 jobs on Qt 6.4.2 failed to compile. The generator only emitted a mapping when the capital header name mirrored the private one (QAbstractAnimation for qabstractanimation.h), so qtypes.h, which is wrapped by QtTypes instead, got no mapping and kept the private spelling. Map it explicitly to <QtTypes>, which is public since Qt 5 and provides the same typedefs either way, and rewrite the 100 affected includes. Do not generalize this to "prefer any Qt* wrapper". The table is generated from whichever Qt is installed, and the wrappers are not equally old: QtLogging only appeared in Qt 6.8 and QtMinMax in 6.5, so that heuristic trades one breakage for another. Every other lowercase header still in the tree has shipped publicly since Qt 5, so those are left alone. Platform-guarded includes. IWYU only sees the preprocessor configuration it is handed, so it dropped an include that only the macOS branch of main.cpp needs, and the macOS jobs failed on an undeclared UrlSchemeEventFilter. Restore the include. Force-compiling every file that carries a platform guard under Q_OS_MAC, Q_OS_MACOS and __APPLE__ reports nothing else, so the macOS branches are covered rather than assumed. shellcheck. Drop a leftover variable and stop assigning the include-what-you-use lookup, which was only ever an existence check. * [Tooling] Map Qt 6.5+ split headers to QtGlobal for Qt 6.4 compatibility IWYU emitted QtTypes, QtPreprocessorSupport, QtVersionChecks, QtAssert, QtMinMax, QtClassHelperMacros, QtEnvironmentVariables and QtTranslation. Qt 6.5 split those declarations out of qglobal.h into separate headers, so neither the Qt* wrapper nor the q*.h private name exists on the oldest Qt we support (6.4.2), and Debian 12 / Ubuntu 24.04 fail with "No such file or directory". qtypes.h is worse still: it only appeared in Qt 6.9. QtGlobal is the one spelling that works across the whole supported range. It has been public since Qt 5, defines these declarations itself up to Qt 6.4, and includes the split headers from Qt 6.5 on, so redirect all eight private headers to it and collapse the affected includes in the tree. Curated overrides now take precedence over the name-mirroring rule, which cannot know about Qt version floors because the mapping is generated from whatever Qt is installed. That rule was mapping qtversionchecks.h to <QtVersionChecks> even with an override present, because the override was only consulted as a fallback. A curated key that matches nothing is now a hard error rather than a silent fallback, and the redirected headers are no longer advertised as the home of a symbol. Verified every Qt include in the tree resolves against the real qt6-base-dev 6.4.2 package: 15 distinct includes, 0 missing. * [Tooling] Map QOverload to QtGlobal and drop the QMetaMethodArgument include Two more headers IWYU emitted do not exist on Qt 6.4.2: QOverload was split out of qglobal.h in Qt 6.5, so on 6.4.2 it is declared in qglobal.h directly and the standalone header is a hard error. QtGlobal covers both, so map qoverload.h to it like the other 6.5+ split headers. QMetaMethodArgument is never referenced anywhere in the tree. It is a bare forwarder for qobjectdefs.h that our generator advertised as a class symbol, which is how IWYU came to add the include at all. The symbol mappings are generated from header names, so they cannot tell a forwarder that happens to be capitalised from a real class; removing the unused include is the fix, and the generic case stays a known limitation of the generator. Extending the audit to every Q* header (an earlier pass only matched headers starting with "Qt" or lowercase q*.h, so it missed all of them) against the real qt6-base-dev, qt6-multimedia-dev, qt6-declarative-dev, qt6-svg-dev and qt6-websockets-dev 6.4.2 packages: 341 distinct Qt includes, 0 missing. * [Game] Give moc the complete CardItem type in player_actions.h player_actions.h forward declares CardItem but passes QList<CardItem *> by value in actPlay/actPlayFacedown/actHide. moc instantiates the metatype for those parameters, and on Qt 6.4 that expands to sizeof(CardItem), so Ubuntu 24.04 fails with "invalid application of 'sizeof' to incomplete type 'CardItem'". Newer Qt tolerates the incomplete type, which is why only the 6.4.2 jobs failed. Include the defining header with an IWYU pragma keep, matching how the other MOC-required includes in this area are handled. * [Game] Give moc the complete ServerInfo_Card type player_logic.h, game_scene.h, view_zone.h and view_zone_widget.h forward declare ServerInfo_Card but pass QList<const ServerInfo_Card *> through signal and slot signatures. moc instantiates the metatype for those parameters, which Qt 6.4 expands to sizeof(ServerInfo_Card), so the connect in game_scene.cpp fails on Debian 12 and Ubuntu 24.04 with "invalid application of 'sizeof' to incomplete type 'const ServerInfo_Card'". Newer Qt tolerates it, so only the 6.4.2 jobs failed. Include the generated protobuf header with an IWYU pragma keep, following the pattern already used for ServerInfo_Player and ServerInfo_User in game_event_handler.h. * [Client] Complete the types Qt 6.4 needs for connect and moc Qt 6.4 expands QMetaType::fromType on the normalised argument list of a PMF connect and on moc-generated metatypes, which requires sizeof on every signal and slot parameter. Newer Qt tolerates forward declarations there, so only the Debian 12 and Ubuntu 24.04 jobs failed, with "invalid application of 'sizeof' to incomplete type" for QMouseEvent, QPrinter and GameReplay. Replace the forward declarations with the real includes: - QMouseEvent in the nine widget headers that pass it through signals - QPrinter in abstract_tab_deck_editor.cpp, for the actPrintDeck lambda - game_replay.pb.h in tab_replays.h. GameReplay is a protobuf message, so the project's game/replay.h only forward declares it and cannot supply the complete type. Found by recompiling all 1918 project TUs against the Debian Qt 6.4.2 headers, which reproduces those two CI jobs locally in one pass. * [Client] Complete Release and QMenu for Qt 6.4 moc and connect Continuing the Qt 6.4 complete-type sweep: - window_main.cpp connects ClientUpdateChecker::finishedCheck to MainWindow::checkClientUpdatesFinished, whose Release * parameter made moc 6.4 instantiate the metatype. GameReplay turned out to be a protobuf message, so Release likewise needed release_channel.h in the three headers declaring those signals: window_main.h, dlg_update.h and client_update_checker.h. - moc 6.4 also needs sizeof(QMenu) for the QList<QMenu *> carried by TabSupervisor::setMenu and MainWindow::updateTabMenu. Verified by regenerating moc output with the Debian moc 6.4.2 for all 434 Q_OBJECT/Q_GADGET classes and compiling it, plus recompiling all 1612 source TUs, against the Qt 6.4.2 headers: no remaining failures. --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
681 lines
No EOL
20 KiB
C++
681 lines
No EOL
20 KiB
C++
#include "raw_json_scanner.h"
|
|
|
|
#include <QByteArray>
|
|
#include <QChar>
|
|
#include <QSharedPointer>
|
|
#include <QStringLiteral>
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
|
|
namespace
|
|
{
|
|
|
|
// Nesting cap matching QJsonDocument's limit, so a pathologically deep document
|
|
// fails shallowly instead of overflowing the stack through the recursive
|
|
// skipValue/skipArray/skipObject walk (Qt's parser caps at 1024 for the same
|
|
// reason and reports DeepNesting).
|
|
constexpr int kMaxNestingDepth = 1024;
|
|
|
|
/**
|
|
* @brief Throttled byte-position reporting for scanSetRanges().
|
|
*
|
|
* Threaded through the skip walk so progress can be reported without materializing
|
|
* the whole document. Reports are rate-limited so a GUI showing progress isn't
|
|
* flooded with interrupts: a callback is invoked at most ~100 times per scan
|
|
* regardless of element count. The closing stretch (the last ~1%) is reported
|
|
* more finely so a large document doesn't stall the progress bar on the final
|
|
* percent before the scan wraps up.
|
|
*/
|
|
struct ScanProgress
|
|
{
|
|
const char *begin = nullptr;
|
|
qsizetype size = 0;
|
|
RawJson::ScanProgressCallback callback;
|
|
qsizetype step = 1;
|
|
qsizetype lastReported = 0;
|
|
|
|
/**
|
|
* @brief Reports the scanner's absolute offset, unless within @p step bytes
|
|
* of the previous report and not yet at the end of the document.
|
|
*/
|
|
void report(const char *p)
|
|
{
|
|
if (!callback) {
|
|
return;
|
|
}
|
|
const qsizetype offset = p - begin;
|
|
if (offset == lastReported) {
|
|
return; // the final element often already sits exactly at the end
|
|
}
|
|
const qsizetype reportingStep = offset >= size - step ? std::max<qsizetype>(1, step / 16) : step;
|
|
if (offset - lastReported < reportingStep && offset < size) {
|
|
return;
|
|
}
|
|
lastReported = offset;
|
|
callback(offset, size);
|
|
}
|
|
};
|
|
|
|
inline bool isWhitespace(char c)
|
|
{
|
|
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
|
|
}
|
|
|
|
const char *skipWhitespace(const char *p, const char *end)
|
|
{
|
|
while (p < end && isWhitespace(*p)) {
|
|
++p;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
inline bool isHexDigit(char c)
|
|
{
|
|
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
|
}
|
|
|
|
inline quint8 hexValue(char c)
|
|
{
|
|
if (c >= '0' && c <= '9') {
|
|
return c - '0';
|
|
}
|
|
if (c >= 'a' && c <= 'f') {
|
|
return c - 'a' + 10;
|
|
}
|
|
return c - 'A' + 10;
|
|
}
|
|
|
|
/**
|
|
* @brief Skips past a JSON string without decoding it, validating escapes.
|
|
* @param p In: pointing at the opening quote. Out: pointing past the closing quote.
|
|
*/
|
|
bool skipString(const char *&p, const char *end)
|
|
{
|
|
++p; // opening quote
|
|
for (;;) {
|
|
const void *quote = memchr(p, '"', static_cast<size_t>(end - p));
|
|
if (!quote) {
|
|
return false; // unterminated string
|
|
}
|
|
// Backslash escapes can only appear before the closing quote, so bound
|
|
// the scan to the string extent instead of the rest of the document.
|
|
const void *backslash = memchr(p, '\\', static_cast<size_t>(static_cast<const char *>(quote) - p));
|
|
if (!backslash) {
|
|
p = static_cast<const char *>(quote) + 1;
|
|
return true;
|
|
}
|
|
const char *b = static_cast<const char *>(backslash);
|
|
if (end - b < 2) {
|
|
return false;
|
|
}
|
|
const char escaped = b[1];
|
|
if (escaped == 'u') {
|
|
if (end - b < 6) {
|
|
return false;
|
|
}
|
|
quint32 codepoint = 0;
|
|
for (int i = 0; i < 4; ++i) {
|
|
if (!isHexDigit(b[2 + i])) {
|
|
return false;
|
|
}
|
|
codepoint = codepoint * 16 + hexValue(b[2 + i]);
|
|
}
|
|
p = b + 6;
|
|
if (codepoint >= 0xD800 && codepoint <= 0xDBFF) {
|
|
// expect the low-surrogate escape for the second half
|
|
if (end - p < 6 || p[0] != '\\' || p[1] != 'u') {
|
|
return false; // unpaired high surrogate
|
|
}
|
|
quint32 low = 0;
|
|
for (int i = 0; i < 4; ++i) {
|
|
if (!isHexDigit(p[2 + i])) {
|
|
return false;
|
|
}
|
|
low = low * 16 + hexValue(p[2 + i]);
|
|
}
|
|
if (low < 0xDC00 || low > 0xDFFF) {
|
|
return false;
|
|
}
|
|
p += 6;
|
|
} else if (codepoint >= 0xDC00 && codepoint <= 0xDFFF) {
|
|
return false; // unpaired low surrogate
|
|
}
|
|
continue;
|
|
}
|
|
switch (escaped) {
|
|
case '"':
|
|
case '\\':
|
|
case '/':
|
|
case 'b':
|
|
case 'f':
|
|
case 'n':
|
|
case 'r':
|
|
case 't':
|
|
p = b + 2;
|
|
continue;
|
|
default:
|
|
return false; // invalid escape
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Decodes a JSON string into @p out, validating it as it goes.
|
|
* @param p In: pointing at the opening quote. Out: pointing past the closing quote.
|
|
*/
|
|
bool decodeString(const char *&p, const char *end, QString &out)
|
|
{
|
|
out.clear();
|
|
QByteArray utf8;
|
|
auto flush = [&out, &utf8]() {
|
|
if (!utf8.isEmpty()) {
|
|
out += QString::fromUtf8(utf8);
|
|
utf8.clear();
|
|
}
|
|
};
|
|
|
|
++p; // opening quote
|
|
while (p < end) {
|
|
const char c = *p;
|
|
if (c == '\\') {
|
|
flush();
|
|
++p; // escaped character
|
|
if (p >= end) {
|
|
return false;
|
|
}
|
|
const char escaped = *p;
|
|
if (escaped == 'u') {
|
|
++p; // first hex digit
|
|
if (p + 4 > end) {
|
|
return false;
|
|
}
|
|
quint32 codepoint = 0;
|
|
for (int i = 0; i < 4; ++i) {
|
|
if (!isHexDigit(p[i])) {
|
|
return false;
|
|
}
|
|
codepoint = codepoint * 16 + hexValue(p[i]);
|
|
}
|
|
p += 4;
|
|
if (codepoint >= 0xD800 && codepoint <= 0xDBFF) {
|
|
// expect a low-surrogate escape for the second half
|
|
if (p + 6 > end || p[0] != '\\' || p[1] != 'u') {
|
|
return false; // unpaired high surrogate
|
|
}
|
|
quint32 low = 0;
|
|
for (int i = 0; i < 4; ++i) {
|
|
if (!isHexDigit(p[2 + i])) {
|
|
return false;
|
|
}
|
|
low = low * 16 + hexValue(p[2 + i]);
|
|
}
|
|
if (low < 0xDC00 || low > 0xDFFF) {
|
|
return false;
|
|
}
|
|
out += QChar(codepoint);
|
|
out += QChar(low);
|
|
p += 6;
|
|
} else if (codepoint >= 0xDC00 && codepoint <= 0xDFFF) {
|
|
return false; // unpaired low surrogate
|
|
} else {
|
|
out += QChar(codepoint);
|
|
}
|
|
continue;
|
|
}
|
|
switch (escaped) {
|
|
case '"':
|
|
out += '"';
|
|
break;
|
|
case '\\':
|
|
out += '\\';
|
|
break;
|
|
case '/':
|
|
out += '/';
|
|
break;
|
|
case 'b':
|
|
out += '\b';
|
|
break;
|
|
case 'f':
|
|
out += '\f';
|
|
break;
|
|
case 'n':
|
|
out += '\n';
|
|
break;
|
|
case 'r':
|
|
out += '\r';
|
|
break;
|
|
case 't':
|
|
out += '\t';
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
++p;
|
|
continue;
|
|
}
|
|
if (c == '"') {
|
|
++p;
|
|
flush();
|
|
return true;
|
|
}
|
|
// Deliberately accept unescaped control characters (e.g. a tab inside
|
|
// a set name): QJsonDocument and skipString accept them too, so
|
|
// rejecting them here would fail the whole document on a byte that
|
|
// Qt is fine with — the very total-failure mode this scanner avoids.
|
|
utf8 += c;
|
|
++p;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @brief Reads a set-metadata field, tolerating null and non-string values.
|
|
*
|
|
* A set's metadata may carry null or non-string values in otherwise-valid
|
|
* payloads ("releaseDate": null, "type": 7). The token itself was already
|
|
* structurally validated by skipValue, so a non-string value is accepted and
|
|
* leaves @p out at its default (empty) — one bad set must not abort the
|
|
* import of every other set in the document.
|
|
*/
|
|
bool decodeStringMember(const char *&fs, const char *&fe, QString &out)
|
|
{
|
|
if (fs >= fe) {
|
|
return false;
|
|
}
|
|
if (*fs != '"') {
|
|
return true;
|
|
}
|
|
return decodeString(fs, fe, out);
|
|
}
|
|
|
|
bool matchLiteral(const char *&p, const char *end, const char *literal, int length)
|
|
{
|
|
if (end - p < length || memcmp(p, literal, static_cast<size_t>(length)) != 0) {
|
|
return false;
|
|
}
|
|
const char *after = p + length;
|
|
if (after < end && (QChar::isLetter(*after) || QChar::isDigit(*after) || *after == '_')) {
|
|
return false;
|
|
}
|
|
p = after;
|
|
return true;
|
|
}
|
|
|
|
bool skipNumber(const char *&p, const char *end)
|
|
{
|
|
// JSON number: -?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?
|
|
if (p < end && *p == '-') {
|
|
++p;
|
|
}
|
|
if (p < end && *p == '0') {
|
|
++p;
|
|
} else if (p < end && *p >= '1' && *p <= '9') {
|
|
++p;
|
|
while (p < end && QChar::isDigit(*p)) {
|
|
++p;
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
if (p < end && *p == '.') {
|
|
++p;
|
|
if (p >= end || !QChar::isDigit(*p)) {
|
|
return false;
|
|
}
|
|
while (p < end && QChar::isDigit(*p)) {
|
|
++p;
|
|
}
|
|
}
|
|
if (p < end && (*p == 'e' || *p == 'E')) {
|
|
++p;
|
|
if (p < end && (*p == '+' || *p == '-')) {
|
|
++p;
|
|
}
|
|
if (p >= end || !QChar::isDigit(*p)) {
|
|
return false;
|
|
}
|
|
while (p < end && QChar::isDigit(*p)) {
|
|
++p;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool skipValue(const char *&p, const char *end, int depth, ScanProgress &scan);
|
|
bool skipObject(const char *&p, const char *end, int depth, ScanProgress &scan);
|
|
bool skipArray(const char *&p, const char *end, int depth, ScanProgress &scan);
|
|
|
|
bool skipPrimitive(const char *&p, const char *end)
|
|
{
|
|
if (p >= end) {
|
|
return false;
|
|
}
|
|
const char c = *p;
|
|
if (c == '"') {
|
|
return skipString(p, end);
|
|
}
|
|
if (c == 't') {
|
|
return matchLiteral(p, end, "true", 4);
|
|
}
|
|
if (c == 'f') {
|
|
return matchLiteral(p, end, "false", 5);
|
|
}
|
|
if (c == 'n') {
|
|
return matchLiteral(p, end, "null", 4);
|
|
}
|
|
if (c == '-' || (c >= '0' && c <= '9')) {
|
|
return skipNumber(p, end);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool skipObject(const char *&p, const char *end, int depth, ScanProgress &scan)
|
|
{
|
|
if (depth <= 0) {
|
|
return false; // nest deeper than the cap
|
|
}
|
|
++p; // '{'
|
|
p = skipWhitespace(p, end);
|
|
if (p < end && *p == '}') {
|
|
++p;
|
|
return true;
|
|
}
|
|
for (;;) {
|
|
p = skipWhitespace(p, end);
|
|
if (p >= end || *p != '"') {
|
|
return false;
|
|
}
|
|
if (!skipString(p, end)) {
|
|
return false;
|
|
}
|
|
p = skipWhitespace(p, end);
|
|
if (p >= end || *p != ':') {
|
|
return false;
|
|
}
|
|
++p;
|
|
if (!skipValue(p, end, depth - 1, scan)) {
|
|
return false;
|
|
}
|
|
scan.report(p);
|
|
p = skipWhitespace(p, end);
|
|
if (p >= end) {
|
|
return false;
|
|
}
|
|
if (*p == ',') {
|
|
++p;
|
|
continue;
|
|
}
|
|
if (*p == '}') {
|
|
++p;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool skipArray(const char *&p, const char *end, int depth, ScanProgress &scan)
|
|
{
|
|
if (depth <= 0) {
|
|
return false; // nest deeper than the cap
|
|
}
|
|
++p; // '['
|
|
p = skipWhitespace(p, end);
|
|
if (p < end && *p == ']') {
|
|
++p;
|
|
return true;
|
|
}
|
|
for (;;) {
|
|
if (!skipValue(p, end, depth - 1, scan)) {
|
|
return false;
|
|
}
|
|
scan.report(p);
|
|
p = skipWhitespace(p, end);
|
|
if (p >= end) {
|
|
return false;
|
|
}
|
|
if (*p == ',') {
|
|
++p;
|
|
continue;
|
|
}
|
|
if (*p == ']') {
|
|
++p;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool skipValue(const char *&p, const char *end, int depth, ScanProgress &scan)
|
|
{
|
|
p = skipWhitespace(p, end);
|
|
if (p >= end) {
|
|
return false;
|
|
}
|
|
const char c = *p;
|
|
if (c == '{') {
|
|
// pass depth through: skipObject consumes the single decrement for this level
|
|
return skipObject(p, end, depth, scan);
|
|
}
|
|
if (c == '[') {
|
|
return skipArray(p, end, depth, scan);
|
|
}
|
|
// a primitive is a leaf, so it never wastes a nesting level
|
|
return skipPrimitive(p, end);
|
|
}
|
|
|
|
/**
|
|
* @brief Iterates the members of the object starting at @p p.
|
|
*
|
|
* For each member invokes @p memberCallback with the key and the byte range of
|
|
* its value. Advancing @p p is unaffected by the callback.
|
|
*/
|
|
template <typename F>
|
|
bool forEachObjectMember(const char *&p, const char *end, int depth, F &&memberCallback, ScanProgress &scan)
|
|
{
|
|
if (depth <= 0) {
|
|
return false; // nest deeper than the cap
|
|
}
|
|
++p; // '{'
|
|
p = skipWhitespace(p, end);
|
|
if (p < end && *p == '}') {
|
|
++p;
|
|
return true;
|
|
}
|
|
for (;;) {
|
|
p = skipWhitespace(p, end);
|
|
if (p >= end || *p != '"') {
|
|
return false;
|
|
}
|
|
QString key;
|
|
if (!decodeString(p, end, key)) {
|
|
return false;
|
|
}
|
|
p = skipWhitespace(p, end);
|
|
if (p >= end || *p != ':') {
|
|
return false;
|
|
}
|
|
++p;
|
|
const char *valueStart = skipWhitespace(p, end);
|
|
const char *valueEnd = valueStart;
|
|
if (!skipValue(valueEnd, end, depth - 1, scan)) {
|
|
return false;
|
|
}
|
|
if (!memberCallback(key, valueStart, valueEnd)) {
|
|
return false;
|
|
}
|
|
p = valueEnd;
|
|
p = skipWhitespace(p, end);
|
|
if (p >= end) {
|
|
return false;
|
|
}
|
|
if (*p == ',') {
|
|
++p;
|
|
continue;
|
|
}
|
|
if (*p == '}') {
|
|
++p;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Counts the direct elements of an array value; returns -1 if the array is malformed.
|
|
int countArrayElements(const char *p, const char *end, int depth, ScanProgress &scan)
|
|
{
|
|
if (depth <= 0) {
|
|
return -1; // nest deeper than the cap
|
|
}
|
|
++p; // '['
|
|
p = skipWhitespace(p, end);
|
|
int count = 0;
|
|
if (p < end && *p == ']') {
|
|
return 0;
|
|
}
|
|
for (;;) {
|
|
if (!skipValue(p, end, depth - 1, scan)) {
|
|
return -1;
|
|
}
|
|
scan.report(p);
|
|
++count;
|
|
p = skipWhitespace(p, end);
|
|
if (p >= end) {
|
|
return -1;
|
|
}
|
|
if (*p == ',') {
|
|
++p;
|
|
continue;
|
|
}
|
|
if (*p == ']') {
|
|
return count;
|
|
}
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
namespace RawJson
|
|
{
|
|
|
|
QList<SetRange> scanSetRanges(const QByteArray &json, ScanError *error, const ScanProgressCallback &progress)
|
|
{
|
|
QList<SetRange> ranges;
|
|
if (error) {
|
|
*error = ScanError{};
|
|
}
|
|
|
|
const auto fail = [&](const QString &message) -> QList<SetRange> {
|
|
if (error) {
|
|
error->message = message;
|
|
}
|
|
return {};
|
|
};
|
|
|
|
const char *begin = json.constData();
|
|
const char *end = begin + json.size();
|
|
if (begin >= end) {
|
|
return fail(QStringLiteral("empty JSON document"));
|
|
}
|
|
|
|
// Throttle reports to ~100 per scan so a GUI thread unthrottling them never
|
|
// drowns under per-card interrupts, whatever the document size.
|
|
ScanProgress scan;
|
|
scan.begin = begin;
|
|
scan.size = end - begin;
|
|
scan.step = std::max<qsizetype>(1, scan.size / 100);
|
|
scan.callback = progress;
|
|
|
|
const char *p = skipWhitespace(begin, end);
|
|
if (p >= end || *p != '{') {
|
|
return fail(QStringLiteral("top-level JSON must be an object"));
|
|
}
|
|
|
|
bool foundData = false;
|
|
bool malformedSetData = false;
|
|
|
|
const auto topLevelCallback = [&](const QString &key, const char *valueStart, const char *valueEnd) {
|
|
if (key == QStringLiteral("data")) {
|
|
foundData = true;
|
|
if (valueStart >= valueEnd || *valueStart != '{') {
|
|
malformedSetData = true;
|
|
return false;
|
|
}
|
|
const char *setP = valueStart;
|
|
const bool ok = forEachObjectMember(
|
|
setP, valueEnd, kMaxNestingDepth - 1,
|
|
[&](const QString &setCode, const char *setStart, const char *setEnd) {
|
|
if (setStart >= setEnd || *setStart != '{') {
|
|
malformedSetData = true;
|
|
return false;
|
|
}
|
|
SetRange range;
|
|
range.dataRange.start = setStart - begin;
|
|
range.dataRange.length = setEnd - setStart;
|
|
range.code = setCode;
|
|
|
|
const char *memberP = setStart;
|
|
const bool metaOk = forEachObjectMember(
|
|
memberP, setEnd, kMaxNestingDepth - 2,
|
|
[&](const QString &field, const char *fs, const char *fe) {
|
|
if (field == QStringLiteral("code")) {
|
|
return decodeStringMember(fs, fe, range.code);
|
|
}
|
|
if (field == QStringLiteral("name")) {
|
|
return decodeStringMember(fs, fe, range.name);
|
|
}
|
|
if (field == QStringLiteral("type")) {
|
|
return decodeStringMember(fs, fe, range.type);
|
|
}
|
|
if (field == QStringLiteral("releaseDate")) {
|
|
return decodeStringMember(fs, fe, range.releaseDate);
|
|
}
|
|
if (field == QStringLiteral("cards")) {
|
|
if (fs >= fe) {
|
|
return false;
|
|
}
|
|
if (*fs != '[') {
|
|
// e.g. "cards": null — treat as an empty array,
|
|
// matching Qt's tolerance.
|
|
return true;
|
|
}
|
|
range.dataRange.cardCount = countArrayElements(fs, fe, kMaxNestingDepth - 2, scan);
|
|
return range.dataRange.cardCount >= 0;
|
|
}
|
|
return true;
|
|
},
|
|
scan);
|
|
if (!metaOk) {
|
|
malformedSetData = true;
|
|
return false;
|
|
}
|
|
ranges.append(range);
|
|
return true;
|
|
},
|
|
scan);
|
|
if (!ok) {
|
|
malformedSetData = true;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
};
|
|
|
|
if (!forEachObjectMember(p, end, kMaxNestingDepth, topLevelCallback, scan)) {
|
|
return fail(malformedSetData ? QStringLiteral("malformed set data") : QStringLiteral("malformed JSON"));
|
|
}
|
|
p = skipWhitespace(p, end);
|
|
if (p != end) {
|
|
return fail(QStringLiteral("trailing content after top-level JSON object"));
|
|
}
|
|
if (!foundData) {
|
|
return fail(QStringLiteral("missing \"data\" object"));
|
|
}
|
|
if (ranges.isEmpty()) {
|
|
return fail(QStringLiteral("no sets found in \"data\""));
|
|
}
|
|
scan.report(end);
|
|
return ranges;
|
|
}
|
|
|
|
} // namespace RawJson
|