diff options
Diffstat (limited to 'src/corelib/mimetypes/qmimemagicrule.cpp')
-rw-r--r-- | src/corelib/mimetypes/qmimemagicrule.cpp | 113 |
1 files changed, 39 insertions, 74 deletions
diff --git a/src/corelib/mimetypes/qmimemagicrule.cpp b/src/corelib/mimetypes/qmimemagicrule.cpp index 2c8c2e7199..3c33d53aac 100644 --- a/src/corelib/mimetypes/qmimemagicrule.cpp +++ b/src/corelib/mimetypes/qmimemagicrule.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** 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 -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** 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-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #define QT_NO_CAST_FROM_ASCII @@ -47,29 +11,31 @@ #include <QtCore/QDebug> #include <qendian.h> +#include <private/qoffsetstringarray_p.h> +#include <private/qtools_p.h> + QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; +using namespace QtMiscUtils; + // in the same order as Type! -static const char magicRuleTypes_string[] = - "invalid\0" - "string\0" - "host16\0" - "host32\0" - "big16\0" - "big32\0" - "little16\0" - "little32\0" - "byte\0" - "\0"; - -static const int magicRuleTypes_indices[] = { - 0, 8, 15, 22, 29, 35, 41, 50, 59, 64, 0 -}; +static constexpr auto magicRuleTypes = qOffsetStringArray( + "invalid", + "string", + "host16", + "host32", + "big16", + "big32", + "little16", + "little32", + "byte" +); QMimeMagicRule::Type QMimeMagicRule::type(const QByteArray &theTypeName) { for (int i = String; i <= Byte; ++i) { - if (theTypeName == magicRuleTypes_string + magicRuleTypes_indices[i]) + if (theTypeName == magicRuleTypes.at(i)) return Type(i); } return Invalid; @@ -77,7 +43,7 @@ QMimeMagicRule::Type QMimeMagicRule::type(const QByteArray &theTypeName) QByteArray QMimeMagicRule::typeName(QMimeMagicRule::Type theType) { - return magicRuleTypes_string + magicRuleTypes_indices[theType]; + return magicRuleTypes.at(theType); } bool QMimeMagicRule::operator==(const QMimeMagicRule &other) const @@ -94,12 +60,12 @@ bool QMimeMagicRule::operator==(const QMimeMagicRule &other) const } // Used by both providers -bool QMimeMagicRule::matchSubstring(const char *dataPtr, int dataSize, int rangeStart, int rangeLength, - int valueLength, const char *valueData, const char *mask) +bool QMimeMagicRule::matchSubstring(const char *dataPtr, qsizetype dataSize, int rangeStart, int rangeLength, + qsizetype valueLength, const char *valueData, const char *mask) { // Size of searched data. // Example: value="ABC", rangeLength=3 -> we need 3+3-1=5 bytes (ABCxx,xABCx,xxABC would match) - const int dataNeeded = qMin(rangeLength + valueLength - 1, dataSize - rangeStart); + const qsizetype dataNeeded = qMin(rangeLength + valueLength - 1, dataSize - rangeStart); if (!mask) { // callgrind says QByteArray::indexOf is much slower, since our strings are typically too @@ -123,7 +89,7 @@ bool QMimeMagicRule::matchSubstring(const char *dataPtr, int dataSize, int range // deviceSize is 4, so dataNeeded was max'ed to 4. // maxStartPos = 4 - 3 + 1 = 2, and indeed // we need to check for a match a positions 0 and 1 (ABCx and xABC). - const int maxStartPos = dataNeeded - valueLength + 1; + const qsizetype maxStartPos = dataNeeded - valueLength + 1; for (int i = 0; i < maxStartPos; ++i) { const char *d = readDataBase + i; bool valid = true; @@ -181,21 +147,17 @@ static inline QByteArray makePattern(const QByteArray &value) char c = 0; for (int i = 0; i < 2 && p + 1 < e; ++i) { ++p; - if (*p >= '0' && *p <= '9') - c = (c << 4) + *p - '0'; - else if (*p >= 'a' && *p <= 'f') - c = (c << 4) + *p - 'a' + 10; - else if (*p >= 'A' && *p <= 'F') - c = (c << 4) + *p - 'A' + 10; + if (const int h = fromHex(*p); h != -1) + c = (c << 4) + h; else continue; } *data++ = c; - } else if (*p >= '0' && *p <= '7') { // oct (\\7, or \\77, or \\377) + } else if (isOctalDigit(*p)) { // oct (\\7, or \\77, or \\377) char c = *p - '0'; - if (p + 1 < e && p[1] >= '0' && p[1] <= '7') { + if (p + 1 < e && isOctalDigit(p[1])) { c = (c << 3) + *(++p) - '0'; - if (p + 1 < e && p[1] >= '0' && p[1] <= '7' && p[-1] <= '3') + if (p + 1 < e && isOctalDigit(p[1]) && p[-1] <= '3') c = (c << 3) + *(++p) - '0'; } *data++ = c; @@ -231,11 +193,14 @@ QMimeMagicRule::QMimeMagicRule(const QString &type, m_mask(mask), m_matchFunction(nullptr) { - if (Q_UNLIKELY(m_type == Invalid)) - *errorString = QLatin1String("Type ") + type + QLatin1String(" is not supported"); + if (Q_UNLIKELY(m_type == Invalid)) { + if (errorString) + *errorString = "Type "_L1 + type + " is not supported"_L1; + return; + } // Parse for offset as "1" or "1:10" - const int colonIndex = offsets.indexOf(QLatin1Char(':')); + const qsizetype colonIndex = offsets.indexOf(u':'); const QStringView startPosStr = QStringView{offsets}.mid(0, colonIndex); // \ These decay to returning 'offsets' const QStringView endPosStr = QStringView{offsets}.mid(colonIndex + 1);// / unchanged when colonIndex == -1 if (Q_UNLIKELY(!QMimeTypeParserBase::parseNumber(startPosStr, &m_startPos, errorString)) || @@ -257,7 +222,7 @@ QMimeMagicRule::QMimeMagicRule(const QString &type, if (Q_UNLIKELY(!ok)) { m_type = Invalid; if (errorString) - *errorString = QLatin1String("Invalid magic rule value \"") + QLatin1String(m_value) + QLatin1Char('"'); + *errorString = "Invalid magic rule value \""_L1 + QLatin1StringView(m_value) + u'"'; return; } m_numberMask = !m_mask.isEmpty() ? m_mask.toUInt(&ok, 0) : 0; // autodetect base @@ -271,7 +236,7 @@ QMimeMagicRule::QMimeMagicRule(const QString &type, if (Q_UNLIKELY(m_mask.size() < 4 || !m_mask.startsWith("0x"))) { m_type = Invalid; if (errorString) - *errorString = QLatin1String("Invalid magic rule mask \"") + QLatin1String(m_mask) + QLatin1Char('"'); + *errorString = "Invalid magic rule mask \""_L1 + QLatin1StringView(m_mask) + u'"'; return; } const QByteArray &tempMask = QByteArray::fromHex(QByteArray::fromRawData( @@ -279,7 +244,7 @@ QMimeMagicRule::QMimeMagicRule(const QString &type, if (Q_UNLIKELY(tempMask.size() != m_pattern.size())) { m_type = Invalid; if (errorString) - *errorString = QLatin1String("Invalid magic rule mask size \"") + QLatin1String(m_mask) + QLatin1Char('"'); + *errorString = "Invalid magic rule mask size \""_L1 + QLatin1StringView(m_mask) + u'"'; return; } m_mask = tempMask; |