QPlugin: use one global metadata magic string
And use QByteArrayView where applicable. Change-Id: I2de1b4dfacd443148279fffd16a39ec22908f75f Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
788a7bfdb1
commit
16e7366b5c
@ -48,7 +48,6 @@
|
||||
#include "private/qobject_p.h"
|
||||
#include "qcbormap.h"
|
||||
#include "qcborvalue.h"
|
||||
#include "qendian.h"
|
||||
#include "qjsonarray.h"
|
||||
#include "qjsondocument.h"
|
||||
#include "qjsonobject.h"
|
||||
@ -65,24 +64,23 @@
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
static inline int metaDataSignatureLength()
|
||||
QJsonDocument qJsonFromRawLibraryMetaData(const char *raw, qsizetype size, QString *errMsg)
|
||||
{
|
||||
return sizeof("QTMETADATA ") - 1;
|
||||
}
|
||||
Q_ASSERT(size >= qsizetype(sizeof(QPluginMetaData::MagicString)));
|
||||
raw += sizeof(QPluginMetaData::MagicString);
|
||||
size -= sizeof(QPluginMetaData::MagicString);
|
||||
|
||||
static QJsonDocument jsonFromCborMetaData(const char *raw, qsizetype size, QString *errMsg)
|
||||
{
|
||||
// extract the keys not stored in CBOR
|
||||
int qt_metadataVersion = quint8(raw[0]);
|
||||
int qt_version = qFromBigEndian<quint16>(raw + 1);
|
||||
int qt_archRequirements = quint8(raw[3]);
|
||||
if (Q_UNLIKELY(raw[-1] != '!' || qt_metadataVersion != 0)) {
|
||||
QPluginMetaData::Header header;
|
||||
Q_ASSERT(size >= qsizetype(sizeof(header)));
|
||||
memcpy(&header, raw, sizeof(header));
|
||||
if (Q_UNLIKELY(header.version > QPluginMetaData::CurrentMetaDataVersion)) {
|
||||
*errMsg = QStringLiteral("Invalid metadata version");
|
||||
return QJsonDocument();
|
||||
}
|
||||
|
||||
raw += 4;
|
||||
size -= 4;
|
||||
raw += sizeof(header);
|
||||
size -= sizeof(header);
|
||||
QByteArray ba = QByteArray::fromRawData(raw, int(size));
|
||||
QCborParserError err;
|
||||
QCborValue metadata = QCborValue::fromCbor(ba, &err);
|
||||
@ -98,9 +96,10 @@ static QJsonDocument jsonFromCborMetaData(const char *raw, qsizetype size, QStri
|
||||
}
|
||||
|
||||
QJsonObject o;
|
||||
o.insert(QLatin1String("version"), qt_version << 8);
|
||||
o.insert(QLatin1String("debug"), bool(qt_archRequirements & 1));
|
||||
o.insert(QLatin1String("archreq"), qt_archRequirements);
|
||||
o.insert(QLatin1String("version"),
|
||||
QT_VERSION_CHECK(header.qt_major_version, header.qt_minor_version, 0));
|
||||
o.insert(QLatin1String("debug"), bool(header.plugin_arch_requirements & 1));
|
||||
o.insert(QLatin1String("archreq"), header.plugin_arch_requirements);
|
||||
|
||||
// convert the top-level map integer keys
|
||||
for (auto it : metadata.toMap()) {
|
||||
@ -128,14 +127,6 @@ static QJsonDocument jsonFromCborMetaData(const char *raw, qsizetype size, QStri
|
||||
return QJsonDocument(o);
|
||||
}
|
||||
|
||||
QJsonDocument qJsonFromRawLibraryMetaData(const char *raw, qsizetype sectionSize, QString *errMsg)
|
||||
{
|
||||
raw += metaDataSignatureLength();
|
||||
sectionSize -= metaDataSignatureLength();
|
||||
|
||||
return jsonFromCborMetaData(raw, sectionSize, errMsg);
|
||||
}
|
||||
|
||||
class QFactoryLoaderPrivate : public QObjectPrivate
|
||||
{
|
||||
Q_DECLARE_PUBLIC(QFactoryLoader)
|
||||
|
@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Copyright (C) 2018 Intel Corporation.
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Copyright (C) 2021 Intel Corporation.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the QtCore module of the Qt Toolkit.
|
||||
@ -182,9 +182,7 @@ QT_BEGIN_NAMESPACE
|
||||
\sa loadHints
|
||||
*/
|
||||
|
||||
|
||||
static qsizetype qt_find_pattern(const char *s, qsizetype s_len,
|
||||
const char *pattern, ulong p_len)
|
||||
static qsizetype qt_find_pattern(const char *s, qsizetype s_len)
|
||||
{
|
||||
/*
|
||||
We used to search from the end of the file so we'd skip the code and find
|
||||
@ -195,8 +193,8 @@ static qsizetype qt_find_pattern(const char *s, qsizetype s_len,
|
||||
More importantly, the pattern string may exist in the debug information due
|
||||
to it being used in the plugin in the first place.
|
||||
*/
|
||||
|
||||
static const QByteArrayMatcher matcher(QByteArray(pattern, p_len));
|
||||
QByteArrayView pattern = QPluginMetaData::MagicString;
|
||||
static const QByteArrayMatcher matcher(pattern.toByteArray());
|
||||
return matcher.indexIn(s, s_len);
|
||||
}
|
||||
|
||||
@ -278,10 +276,7 @@ static bool findPatternUnloaded(const QString &library, QLibraryPrivate *lib)
|
||||
}
|
||||
#endif // defined(Q_OF_ELF) && defined(Q_CC_GNU)
|
||||
|
||||
char pattern[] = "qTMETADATA ";
|
||||
pattern[0] = 'Q'; // Ensure the pattern "QTMETADATA" is not found in this library should QPluginLoader ever encounter it.
|
||||
const ulong plen = ulong(qstrlen(pattern));
|
||||
if (qsizetype rel = qt_find_pattern(filedata + r.pos, r.length, pattern, plen);
|
||||
if (qsizetype rel = qt_find_pattern(filedata + r.pos, r.length);
|
||||
rel >= 0) {
|
||||
const char *data = filedata + r.pos + rel;
|
||||
QString errMsg;
|
||||
|
Loading…
x
Reference in New Issue
Block a user