| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/mimetypes/qmimemagicrule.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||||||||
| 16 | ** | - | ||||||||||||||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||||||||
| 24 | ** | - | ||||||||||||||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||||||||
| 35 | ** | - | ||||||||||||||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||||||||
| 37 | ** | - | ||||||||||||||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||||||||||||||
| 39 | - | |||||||||||||||||||||||||
| 40 | - | |||||||||||||||||||||||||
| 41 | #define QT_NO_CAST_FROM_ASCII | - | ||||||||||||||||||||||||
| 42 | - | |||||||||||||||||||||||||
| 43 | #include "qmimemagicrule_p.h" | - | ||||||||||||||||||||||||
| 44 | - | |||||||||||||||||||||||||
| 45 | #ifndef QT_NO_MIMETYPE | - | ||||||||||||||||||||||||
| 46 | - | |||||||||||||||||||||||||
| 47 | #include "qmimetypeparser_p.h" | - | ||||||||||||||||||||||||
| 48 | #include <QtCore/QList> | - | ||||||||||||||||||||||||
| 49 | #include <QtCore/QDebug> | - | ||||||||||||||||||||||||
| 50 | #include <qendian.h> | - | ||||||||||||||||||||||||
| 51 | - | |||||||||||||||||||||||||
| 52 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||||||||
| 53 | - | |||||||||||||||||||||||||
| 54 | // in the same order as Type! | - | ||||||||||||||||||||||||
| 55 | static const char magicRuleTypes_string[] = | - | ||||||||||||||||||||||||
| 56 | "invalid\0" | - | ||||||||||||||||||||||||
| 57 | "string\0" | - | ||||||||||||||||||||||||
| 58 | "host16\0" | - | ||||||||||||||||||||||||
| 59 | "host32\0" | - | ||||||||||||||||||||||||
| 60 | "big16\0" | - | ||||||||||||||||||||||||
| 61 | "big32\0" | - | ||||||||||||||||||||||||
| 62 | "little16\0" | - | ||||||||||||||||||||||||
| 63 | "little32\0" | - | ||||||||||||||||||||||||
| 64 | "byte\0" | - | ||||||||||||||||||||||||
| 65 | "\0"; | - | ||||||||||||||||||||||||
| 66 | - | |||||||||||||||||||||||||
| 67 | static const int magicRuleTypes_indices[] = { | - | ||||||||||||||||||||||||
| 68 | 0, 8, 15, 22, 29, 35, 41, 50, 59, 65, 0 | - | ||||||||||||||||||||||||
| 69 | }; | - | ||||||||||||||||||||||||
| 70 | - | |||||||||||||||||||||||||
| 71 | QMimeMagicRule::Type QMimeMagicRule::type(const QByteArray &theTypeName) | - | ||||||||||||||||||||||||
| 72 | { | - | ||||||||||||||||||||||||
| 73 | for (int i = String; i <= Byte; ++i) {
| 0-7377 | ||||||||||||||||||||||||
| 74 | if (theTypeName == magicRuleTypes_string + magicRuleTypes_indices[i])
| 3319-4058 | ||||||||||||||||||||||||
| 75 | return Type(i); executed 4058 times by 1 test: return Type(i);Executed by:
| 4058 | ||||||||||||||||||||||||
| 76 | } executed 3319 times by 1 test: end of blockExecuted by:
| 3319 | ||||||||||||||||||||||||
| 77 | return Invalid; never executed: return Invalid; | 0 | ||||||||||||||||||||||||
| 78 | } | - | ||||||||||||||||||||||||
| 79 | - | |||||||||||||||||||||||||
| 80 | QByteArray QMimeMagicRule::typeName(QMimeMagicRule::Type theType) | - | ||||||||||||||||||||||||
| 81 | { | - | ||||||||||||||||||||||||
| 82 | return magicRuleTypes_string + magicRuleTypes_indices[theType]; never executed: return magicRuleTypes_string + magicRuleTypes_indices[theType]; | 0 | ||||||||||||||||||||||||
| 83 | } | - | ||||||||||||||||||||||||
| 84 | - | |||||||||||||||||||||||||
| 85 | bool QMimeMagicRule::operator==(const QMimeMagicRule &other) const | - | ||||||||||||||||||||||||
| 86 | { | - | ||||||||||||||||||||||||
| 87 | return m_type == other.m_type && never executed: return m_type == other.m_type && m_value == other.m_value && m_startPos == other.m_startPos && m_endPos == other.m_endPos && m_mask == other.m_mask && m_pattern == other.m_pattern && m_number == other.m_number && m_numberMask == other.m_numberMask && m_matchFunction == other.m_matchFunction; | 0 | ||||||||||||||||||||||||
| 88 | m_value == other.m_value && never executed: return m_type == other.m_type && m_value == other.m_value && m_startPos == other.m_startPos && m_endPos == other.m_endPos && m_mask == other.m_mask && m_pattern == other.m_pattern && m_number == other.m_number && m_numberMask == other.m_numberMask && m_matchFunction == other.m_matchFunction; | 0 | ||||||||||||||||||||||||
| 89 | m_startPos == other.m_startPos && never executed: return m_type == other.m_type && m_value == other.m_value && m_startPos == other.m_startPos && m_endPos == other.m_endPos && m_mask == other.m_mask && m_pattern == other.m_pattern && m_number == other.m_number && m_numberMask == other.m_numberMask && m_matchFunction == other.m_matchFunction; | 0 | ||||||||||||||||||||||||
| 90 | m_endPos == other.m_endPos && never executed: return m_type == other.m_type && m_value == other.m_value && m_startPos == other.m_startPos && m_endPos == other.m_endPos && m_mask == other.m_mask && m_pattern == other.m_pattern && m_number == other.m_number && m_numberMask == other.m_numberMask && m_matchFunction == other.m_matchFunction; | 0 | ||||||||||||||||||||||||
| 91 | m_mask == other.m_mask && never executed: return m_type == other.m_type && m_value == other.m_value && m_startPos == other.m_startPos && m_endPos == other.m_endPos && m_mask == other.m_mask && m_pattern == other.m_pattern && m_number == other.m_number && m_numberMask == other.m_numberMask && m_matchFunction == other.m_matchFunction; | 0 | ||||||||||||||||||||||||
| 92 | m_pattern == other.m_pattern && never executed: return m_type == other.m_type && m_value == other.m_value && m_startPos == other.m_startPos && m_endPos == other.m_endPos && m_mask == other.m_mask && m_pattern == other.m_pattern && m_number == other.m_number && m_numberMask == other.m_numberMask && m_matchFunction == other.m_matchFunction; | 0 | ||||||||||||||||||||||||
| 93 | m_number == other.m_number && never executed: return m_type == other.m_type && m_value == other.m_value && m_startPos == other.m_startPos && m_endPos == other.m_endPos && m_mask == other.m_mask && m_pattern == other.m_pattern && m_number == other.m_number && m_numberMask == other.m_numberMask && m_matchFunction == other.m_matchFunction; | 0 | ||||||||||||||||||||||||
| 94 | m_numberMask == other.m_numberMask && never executed: return m_type == other.m_type && m_value == other.m_value && m_startPos == other.m_startPos && m_endPos == other.m_endPos && m_mask == other.m_mask && m_pattern == other.m_pattern && m_number == other.m_number && m_numberMask == other.m_numberMask && m_matchFunction == other.m_matchFunction; | 0 | ||||||||||||||||||||||||
| 95 | m_matchFunction == other.m_matchFunction; never executed: return m_type == other.m_type && m_value == other.m_value && m_startPos == other.m_startPos && m_endPos == other.m_endPos && m_mask == other.m_mask && m_pattern == other.m_pattern && m_number == other.m_number && m_numberMask == other.m_numberMask && m_matchFunction == other.m_matchFunction; | 0 | ||||||||||||||||||||||||
| 96 | } | - | ||||||||||||||||||||||||
| 97 | - | |||||||||||||||||||||||||
| 98 | // Used by both providers | - | ||||||||||||||||||||||||
| 99 | bool QMimeMagicRule::matchSubstring(const char *dataPtr, int dataSize, int rangeStart, int rangeLength, | - | ||||||||||||||||||||||||
| 100 | int valueLength, const char *valueData, const char *mask) | - | ||||||||||||||||||||||||
| 101 | { | - | ||||||||||||||||||||||||
| 102 | // Size of searched data. | - | ||||||||||||||||||||||||
| 103 | // Example: value="ABC", rangeLength=3 -> we need 3+3-1=5 bytes (ABCxx,xABCx,xxABC would match) | - | ||||||||||||||||||||||||
| 104 | const int dataNeeded = qMin(rangeLength + valueLength - 1, dataSize - rangeStart); | - | ||||||||||||||||||||||||
| 105 | - | |||||||||||||||||||||||||
| 106 | if (!mask) {
| 9901-17592 | ||||||||||||||||||||||||
| 107 | // callgrind says QByteArray::indexOf is much slower, since our strings are typically too | - | ||||||||||||||||||||||||
| 108 | // short for be worth Boyer-Moore matching (1 to 71 bytes, 11 bytes on average). | - | ||||||||||||||||||||||||
| 109 | bool found = false; | - | ||||||||||||||||||||||||
| 110 | for (int i = rangeStart; i < rangeStart + rangeLength; ++i) {
| 5928-39384 | ||||||||||||||||||||||||
| 111 | if (i + valueLength > dataSize)
| 3928-35456 | ||||||||||||||||||||||||
| 112 | break; executed 3928 times by 1 test: break;Executed by:
| 3928 | ||||||||||||||||||||||||
| 113 | - | |||||||||||||||||||||||||
| 114 | if (memcmp(valueData, dataPtr + i, valueLength) == 0) {
| 45-35411 | ||||||||||||||||||||||||
| 115 | found = true; | - | ||||||||||||||||||||||||
| 116 | break; executed 45 times by 1 test: break;Executed by:
| 45 | ||||||||||||||||||||||||
| 117 | } | - | ||||||||||||||||||||||||
| 118 | } executed 35411 times by 1 test: end of blockExecuted by:
| 35411 | ||||||||||||||||||||||||
| 119 | if (!found)
| 45-9856 | ||||||||||||||||||||||||
| 120 | return false; executed 9856 times by 1 test: return false;Executed by:
| 9856 | ||||||||||||||||||||||||
| 121 | } else { executed 45 times by 1 test: end of blockExecuted by:
| 45 | ||||||||||||||||||||||||
| 122 | bool found = false; | - | ||||||||||||||||||||||||
| 123 | const char *readDataBase = dataPtr + rangeStart; | - | ||||||||||||||||||||||||
| 124 | // Example (continued from above): | - | ||||||||||||||||||||||||
| 125 | // deviceSize is 4, so dataNeeded was max'ed to 4. | - | ||||||||||||||||||||||||
| 126 | // maxStartPos = 4 - 3 + 1 = 2, and indeed | - | ||||||||||||||||||||||||
| 127 | // we need to check for a match a positions 0 and 1 (ABCx and xABC). | - | ||||||||||||||||||||||||
| 128 | const int maxStartPos = dataNeeded - valueLength + 1; | - | ||||||||||||||||||||||||
| 129 | for (int i = 0; i < maxStartPos; ++i) {
| 17592-38620 | ||||||||||||||||||||||||
| 130 | const char *d = readDataBase + i; | - | ||||||||||||||||||||||||
| 131 | bool valid = true; | - | ||||||||||||||||||||||||
| 132 | for (int idx = 0; idx < valueLength; ++idx) {
| 46-39071 | ||||||||||||||||||||||||
| 133 | if (((*d++) & mask[idx]) != (valueData[idx] & mask[idx])) {
| 497-38574 | ||||||||||||||||||||||||
| 134 | valid = false; | - | ||||||||||||||||||||||||
| 135 | break; executed 38574 times by 1 test: break;Executed by:
| 38574 | ||||||||||||||||||||||||
| 136 | } | - | ||||||||||||||||||||||||
| 137 | } executed 497 times by 1 test: end of blockExecuted by:
| 497 | ||||||||||||||||||||||||
| 138 | if (valid)
| 46-38574 | ||||||||||||||||||||||||
| 139 | found = true; executed 46 times by 1 test: found = true;Executed by:
| 46 | ||||||||||||||||||||||||
| 140 | } executed 38620 times by 1 test: end of blockExecuted by:
| 38620 | ||||||||||||||||||||||||
| 141 | if (!found)
| 46-17546 | ||||||||||||||||||||||||
| 142 | return false; executed 17546 times by 1 test: return false;Executed by:
| 17546 | ||||||||||||||||||||||||
| 143 | } executed 46 times by 1 test: end of blockExecuted by:
| 46 | ||||||||||||||||||||||||
| 144 | //qDebug() << "Found" << value << "in" << searchedData; | - | ||||||||||||||||||||||||
| 145 | return true; executed 91 times by 1 test: return true;Executed by:
| 91 | ||||||||||||||||||||||||
| 146 | } | - | ||||||||||||||||||||||||
| 147 | - | |||||||||||||||||||||||||
| 148 | bool QMimeMagicRule::matchString(const QByteArray &data) const | - | ||||||||||||||||||||||||
| 149 | { | - | ||||||||||||||||||||||||
| 150 | const int rangeLength = m_endPos - m_startPos + 1; | - | ||||||||||||||||||||||||
| 151 | return QMimeMagicRule::matchSubstring(data.constData(), data.size(), m_startPos, rangeLength, m_pattern.size(), m_pattern.constData(), m_mask.constData()); executed 17377 times by 1 test: return QMimeMagicRule::matchSubstring(data.constData(), data.size(), m_startPos, rangeLength, m_pattern.size(), m_pattern.constData(), m_mask.constData());Executed by:
| 17377 | ||||||||||||||||||||||||
| 152 | } | - | ||||||||||||||||||||||||
| 153 | - | |||||||||||||||||||||||||
| 154 | template <typename T> | - | ||||||||||||||||||||||||
| 155 | bool QMimeMagicRule::matchNumber(const QByteArray &data) const | - | ||||||||||||||||||||||||
| 156 | { | - | ||||||||||||||||||||||||
| 157 | const T value(m_number); | - | ||||||||||||||||||||||||
| 158 | const T mask(m_numberMask); | - | ||||||||||||||||||||||||
| 159 | - | |||||||||||||||||||||||||
| 160 | //qDebug() << "matchNumber" << "0x" << QString::number(m_number, 16) << "size" << sizeof(T); | - | ||||||||||||||||||||||||
| 161 | //qDebug() << "mask" << QString::number(m_numberMask, 16); | - | ||||||||||||||||||||||||
| 162 | - | |||||||||||||||||||||||||
| 163 | const char *p = data.constData() + m_startPos; | - | ||||||||||||||||||||||||
| 164 | const char *e = data.constData() + qMin(data.size() - int(sizeof(T)), m_endPos + 1); | - | ||||||||||||||||||||||||
| 165 | for ( ; p <= e; ++p) {
| 1854-3457 | ||||||||||||||||||||||||
| 166 | if ((qFromUnaligned<T>(p) & mask) == (value & mask))
| 13-3444 | ||||||||||||||||||||||||
| 167 | return true; executed 13 times by 1 test: return true;Executed by:
| 13 | ||||||||||||||||||||||||
| 168 | } executed 3444 times by 1 test: end of blockExecuted by:
| 3444 | ||||||||||||||||||||||||
| 169 | - | |||||||||||||||||||||||||
| 170 | return false; executed 1854 times by 1 test: return false;Executed by:
| 1854 | ||||||||||||||||||||||||
| 171 | } | - | ||||||||||||||||||||||||
| 172 | - | |||||||||||||||||||||||||
| 173 | static inline QByteArray makePattern(const QByteArray &value) | - | ||||||||||||||||||||||||
| 174 | { | - | ||||||||||||||||||||||||
| 175 | QByteArray pattern(value.size(), Qt::Uninitialized); | - | ||||||||||||||||||||||||
| 176 | char *data = pattern.data(); | - | ||||||||||||||||||||||||
| 177 | - | |||||||||||||||||||||||||
| 178 | const char *p = value.constData(); | - | ||||||||||||||||||||||||
| 179 | const char *e = p + value.size(); | - | ||||||||||||||||||||||||
| 180 | for ( ; p < e; ++p) {
| 3431-33647 | ||||||||||||||||||||||||
| 181 | if (*p == '\\' && ++p < e) {
| 0-31392 | ||||||||||||||||||||||||
| 182 | if (*p == 'x') { // hex (\\xff)
| 775-1480 | ||||||||||||||||||||||||
| 183 | char c = 0; | - | ||||||||||||||||||||||||
| 184 | for (int i = 0; i < 2 && p + 1 < e; ++i) {
| 10-1550 | ||||||||||||||||||||||||
| 185 | ++p; | - | ||||||||||||||||||||||||
| 186 | if (*p >= '0' && *p <= '9')
| 0-1540 | ||||||||||||||||||||||||
| 187 | c = (c << 4) + *p - '0'; executed 1260 times by 1 test: c = (c << 4) + *p - '0';Executed by:
| 1260 | ||||||||||||||||||||||||
| 188 | else if (*p >= 'a' && *p <= 'f')
| 0-220 | ||||||||||||||||||||||||
| 189 | c = (c << 4) + *p - 'a' + 10; executed 220 times by 1 test: c = (c << 4) + *p - 'a' + 10;Executed by:
| 220 | ||||||||||||||||||||||||
| 190 | else if (*p >= 'A' && *p <= 'F')
| 0-60 | ||||||||||||||||||||||||
| 191 | c = (c << 4) + *p - 'A' + 10; executed 60 times by 1 test: c = (c << 4) + *p - 'A' + 10;Executed by:
| 60 | ||||||||||||||||||||||||
| 192 | else | - | ||||||||||||||||||||||||
| 193 | continue; never executed: continue; | 0 | ||||||||||||||||||||||||
| 194 | } | - | ||||||||||||||||||||||||
| 195 | *data++ = c; | - | ||||||||||||||||||||||||
| 196 | } else if (*p >= '0' && *p <= '7') { // oct (\\7, or \\77, or \\377) executed 775 times by 1 test: end of blockExecuted by:
| 45-1435 | ||||||||||||||||||||||||
| 197 | char c = *p - '0'; | - | ||||||||||||||||||||||||
| 198 | if (p + 1 < e && p[1] >= '0' && p[1] <= '7') {
| 0-1195 | ||||||||||||||||||||||||
| 199 | c = (c << 3) + *(++p) - '0'; | - | ||||||||||||||||||||||||
| 200 | if (p + 1 < e && p[1] >= '0' && p[1] <= '7' && p[-1] <= '3')
| 0-1055 | ||||||||||||||||||||||||
| 201 | c = (c << 3) + *(++p) - '0'; executed 1045 times by 1 test: c = (c << 3) + *(++p) - '0';Executed by:
| 1045 | ||||||||||||||||||||||||
| 202 | } executed 1060 times by 1 test: end of blockExecuted by:
| 1060 | ||||||||||||||||||||||||
| 203 | *data++ = c; | - | ||||||||||||||||||||||||
| 204 | } else if (*p == 'n') { executed 1280 times by 1 test: end of blockExecuted by:
| 75-1280 | ||||||||||||||||||||||||
| 205 | *data++ = '\n'; | - | ||||||||||||||||||||||||
| 206 | } else if (*p == 'r') { executed 75 times by 1 test: end of blockExecuted by:
| 10-115 | ||||||||||||||||||||||||
| 207 | *data++ = '\r'; | - | ||||||||||||||||||||||||
| 208 | } else if (*p == 't') { executed 10 times by 1 test: end of blockExecuted by:
| 10-100 | ||||||||||||||||||||||||
| 209 | *data++ = '\t'; | - | ||||||||||||||||||||||||
| 210 | } else { // escaped executed 15 times by 1 test: end of blockExecuted by:
| 15 | ||||||||||||||||||||||||
| 211 | *data++ = *p; | - | ||||||||||||||||||||||||
| 212 | } executed 100 times by 1 test: end of blockExecuted by:
| 100 | ||||||||||||||||||||||||
| 213 | } else { | - | ||||||||||||||||||||||||
| 214 | *data++ = *p; | - | ||||||||||||||||||||||||
| 215 | } executed 31392 times by 1 test: end of blockExecuted by:
| 31392 | ||||||||||||||||||||||||
| 216 | } | - | ||||||||||||||||||||||||
| 217 | pattern.truncate(data - pattern.data()); | - | ||||||||||||||||||||||||
| 218 | - | |||||||||||||||||||||||||
| 219 | return pattern; executed 3431 times by 1 test: return pattern;Executed by:
| 3431 | ||||||||||||||||||||||||
| 220 | } | - | ||||||||||||||||||||||||
| 221 | - | |||||||||||||||||||||||||
| 222 | // Evaluate a magic match rule like | - | ||||||||||||||||||||||||
| 223 | // <match value="must be converted with BinHex" type="string" offset="11"/> | - | ||||||||||||||||||||||||
| 224 | // <match value="0x9501" type="big16" offset="0:64"/> | - | ||||||||||||||||||||||||
| 225 | - | |||||||||||||||||||||||||
| 226 | QMimeMagicRule::QMimeMagicRule(const QString &type, | - | ||||||||||||||||||||||||
| 227 | const QByteArray &value, | - | ||||||||||||||||||||||||
| 228 | const QString &offsets, | - | ||||||||||||||||||||||||
| 229 | const QByteArray &mask, | - | ||||||||||||||||||||||||
| 230 | QString *errorString) | - | ||||||||||||||||||||||||
| 231 | : m_type(QMimeMagicRule::type(type.toLatin1())), | - | ||||||||||||||||||||||||
| 232 | m_value(value), | - | ||||||||||||||||||||||||
| 233 | m_mask(mask), | - | ||||||||||||||||||||||||
| 234 | m_matchFunction(nullptr) | - | ||||||||||||||||||||||||
| 235 | { | - | ||||||||||||||||||||||||
| 236 | if (Q_UNLIKELY(m_type == Invalid))
| 0-4058 | ||||||||||||||||||||||||
| 237 | *errorString = QLatin1String("Type ") + type + QLatin1String(" is not supported"); never executed: *errorString = QLatin1String("Type ") + type + QLatin1String(" is not supported"); | 0 | ||||||||||||||||||||||||
| 238 | - | |||||||||||||||||||||||||
| 239 | // Parse for offset as "1" or "1:10" | - | ||||||||||||||||||||||||
| 240 | const int colonIndex = offsets.indexOf(QLatin1Char(':')); | - | ||||||||||||||||||||||||
| 241 | const QStringRef startPosStr = offsets.midRef(0, colonIndex); // \ These decay to returning 'offsets' | - | ||||||||||||||||||||||||
| 242 | const QStringRef endPosStr = offsets.midRef(colonIndex + 1);// / unchanged when colonIndex == -1 | - | ||||||||||||||||||||||||
| 243 | if (Q_UNLIKELY(!QMimeTypeParserBase::parseNumber(startPosStr, &m_startPos, errorString)) ||
| 0-4058 | ||||||||||||||||||||||||
| 244 | Q_UNLIKELY(!QMimeTypeParserBase::parseNumber(endPosStr, &m_endPos, errorString))) {
| 0-4058 | ||||||||||||||||||||||||
| 245 | m_type = Invalid; | - | ||||||||||||||||||||||||
| 246 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 247 | } | - | ||||||||||||||||||||||||
| 248 | - | |||||||||||||||||||||||||
| 249 | if (Q_UNLIKELY(m_value.isEmpty())) {
| 0-4058 | ||||||||||||||||||||||||
| 250 | m_type = Invalid; | - | ||||||||||||||||||||||||
| 251 | if (errorString)
| 0 | ||||||||||||||||||||||||
| 252 | *errorString = QStringLiteral("Invalid empty magic rule value"); never executed: *errorString = ([]() -> QString { enum { Size = sizeof(u"" "Invalid empty magic rule value")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "Invalid empty magic rule value" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }());never executed: return qstring_literal_temp; | 0 | ||||||||||||||||||||||||
| 253 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 254 | } | - | ||||||||||||||||||||||||
| 255 | - | |||||||||||||||||||||||||
| 256 | if (m_type >= Host16 && m_type <= Byte) {
| 0-3431 | ||||||||||||||||||||||||
| 257 | bool ok; | - | ||||||||||||||||||||||||
| 258 | m_number = m_value.toUInt(&ok, 0); // autodetect base | - | ||||||||||||||||||||||||
| 259 | if (Q_UNLIKELY(!ok)) {
| 2-625 | ||||||||||||||||||||||||
| 260 | m_type = Invalid; | - | ||||||||||||||||||||||||
| 261 | if (errorString)
| 0-2 | ||||||||||||||||||||||||
| 262 | *errorString = QLatin1String("Invalid magic rule value \"") + QLatin1String(m_value) + QLatin1Char('"'); executed 2 times by 1 test: *errorString = QLatin1String("Invalid magic rule value \"") + QLatin1String(m_value) + QLatin1Char('"');Executed by:
| 2 | ||||||||||||||||||||||||
| 263 | return; executed 2 times by 1 test: return;Executed by:
| 2 | ||||||||||||||||||||||||
| 264 | } | - | ||||||||||||||||||||||||
| 265 | m_numberMask = !m_mask.isEmpty() ? m_mask.toUInt(&ok, 0) : 0; // autodetect base
| 50-575 | ||||||||||||||||||||||||
| 266 | } executed 625 times by 1 test: end of blockExecuted by:
| 625 | ||||||||||||||||||||||||
| 267 | - | |||||||||||||||||||||||||
| 268 | switch (m_type) { | - | ||||||||||||||||||||||||
| 269 | case String: executed 3431 times by 1 test: case String:Executed by:
| 3431 | ||||||||||||||||||||||||
| 270 | m_pattern = makePattern(m_value); | - | ||||||||||||||||||||||||
| 271 | m_pattern.squeeze(); | - | ||||||||||||||||||||||||
| 272 | if (!m_mask.isEmpty()) {
| 29-3402 | ||||||||||||||||||||||||
| 273 | if (Q_UNLIKELY(m_mask.size() < 4 || !m_mask.startsWith("0x"))) {
| 2-27 | ||||||||||||||||||||||||
| 274 | m_type = Invalid; | - | ||||||||||||||||||||||||
| 275 | if (errorString)
| 0-2 | ||||||||||||||||||||||||
| 276 | *errorString = QLatin1String("Invalid magic rule mask \"") + QLatin1String(m_mask) + QLatin1Char('"'); executed 2 times by 1 test: *errorString = QLatin1String("Invalid magic rule mask \"") + QLatin1String(m_mask) + QLatin1Char('"');Executed by:
| 2 | ||||||||||||||||||||||||
| 277 | return; executed 2 times by 1 test: return;Executed by:
| 2 | ||||||||||||||||||||||||
| 278 | } | - | ||||||||||||||||||||||||
| 279 | const QByteArray &tempMask = QByteArray::fromHex(QByteArray::fromRawData( | - | ||||||||||||||||||||||||
| 280 | m_mask.constData() + 2, m_mask.size() - 2)); | - | ||||||||||||||||||||||||
| 281 | if (Q_UNLIKELY(tempMask.size() != m_pattern.size())) {
| 2-25 | ||||||||||||||||||||||||
| 282 | m_type = Invalid; | - | ||||||||||||||||||||||||
| 283 | if (errorString)
| 0-2 | ||||||||||||||||||||||||
| 284 | *errorString = QLatin1String("Invalid magic rule mask size \"") + QLatin1String(m_mask) + QLatin1Char('"'); executed 2 times by 1 test: *errorString = QLatin1String("Invalid magic rule mask size \"") + QLatin1String(m_mask) + QLatin1Char('"');Executed by:
| 2 | ||||||||||||||||||||||||
| 285 | return; executed 2 times by 1 test: return;Executed by:
| 2 | ||||||||||||||||||||||||
| 286 | } | - | ||||||||||||||||||||||||
| 287 | m_mask = tempMask; | - | ||||||||||||||||||||||||
| 288 | } else { executed 25 times by 1 test: end of blockExecuted by:
| 25 | ||||||||||||||||||||||||
| 289 | m_mask.fill(char(-1), m_pattern.size()); | - | ||||||||||||||||||||||||
| 290 | } executed 3402 times by 1 test: end of blockExecuted by:
| 3402 | ||||||||||||||||||||||||
| 291 | m_mask.squeeze(); | - | ||||||||||||||||||||||||
| 292 | m_matchFunction = &QMimeMagicRule::matchString; | - | ||||||||||||||||||||||||
| 293 | break; executed 3427 times by 1 test: break;Executed by:
| 3427 | ||||||||||||||||||||||||
| 294 | case Byte: executed 205 times by 1 test: case Byte:Executed by:
| 205 | ||||||||||||||||||||||||
| 295 | if (m_number <= quint8(-1)) {
| 0-205 | ||||||||||||||||||||||||
| 296 | if (m_numberMask == 0)
| 0-205 | ||||||||||||||||||||||||
| 297 | m_numberMask = quint8(-1); executed 205 times by 1 test: m_numberMask = quint8(-1);Executed by:
| 205 | ||||||||||||||||||||||||
| 298 | m_matchFunction = &QMimeMagicRule::matchNumber<quint8>; | - | ||||||||||||||||||||||||
| 299 | } executed 205 times by 1 test: end of blockExecuted by:
| 205 | ||||||||||||||||||||||||
| 300 | break; executed 205 times by 1 test: break;Executed by:
| 205 | ||||||||||||||||||||||||
| 301 | case Big16: executed 65 times by 1 test: case Big16:Executed by:
| 65 | ||||||||||||||||||||||||
| 302 | case Host16: executed 20 times by 1 test: case Host16:Executed by:
| 20 | ||||||||||||||||||||||||
| 303 | case Little16: executed 85 times by 1 test: case Little16:Executed by:
| 85 | ||||||||||||||||||||||||
| 304 | if (m_number <= quint16(-1)) {
| 0-170 | ||||||||||||||||||||||||
| 305 | m_number = m_type == Little16 ? qFromLittleEndian<quint16>(m_number) : qFromBigEndian<quint16>(m_number);
| 85 | ||||||||||||||||||||||||
| 306 | if (m_numberMask == 0)
| 5-165 | ||||||||||||||||||||||||
| 307 | m_numberMask = quint16(-1); executed 165 times by 1 test: m_numberMask = quint16(-1);Executed by:
| 165 | ||||||||||||||||||||||||
| 308 | m_matchFunction = &QMimeMagicRule::matchNumber<quint16>; | - | ||||||||||||||||||||||||
| 309 | } executed 170 times by 1 test: end of blockExecuted by:
| 170 | ||||||||||||||||||||||||
| 310 | break; executed 170 times by 1 test: break;Executed by:
| 170 | ||||||||||||||||||||||||
| 311 | case Big32: executed 95 times by 1 test: case Big32:Executed by:
| 95 | ||||||||||||||||||||||||
| 312 | case Host32: executed 20 times by 1 test: case Host32:Executed by:
| 20 | ||||||||||||||||||||||||
| 313 | case Little32: executed 135 times by 1 test: case Little32:Executed by:
| 135 | ||||||||||||||||||||||||
| 314 | if (m_number <= quint32(-1)) {
| 0-250 | ||||||||||||||||||||||||
| 315 | m_number = m_type == Little32 ? qFromLittleEndian<quint32>(m_number) : qFromBigEndian<quint32>(m_number);
| 115-135 | ||||||||||||||||||||||||
| 316 | if (m_numberMask == 0)
| 45-205 | ||||||||||||||||||||||||
| 317 | m_numberMask = quint32(-1); executed 205 times by 1 test: m_numberMask = quint32(-1);Executed by:
| 205 | ||||||||||||||||||||||||
| 318 | m_matchFunction = &QMimeMagicRule::matchNumber<quint32>; | - | ||||||||||||||||||||||||
| 319 | } executed 250 times by 1 test: end of blockExecuted by:
| 250 | ||||||||||||||||||||||||
| 320 | break; executed 250 times by 1 test: break;Executed by:
| 250 | ||||||||||||||||||||||||
| 321 | default: never executed: default: | 0 | ||||||||||||||||||||||||
| 322 | break; never executed: break; | 0 | ||||||||||||||||||||||||
| 323 | } | - | ||||||||||||||||||||||||
| 324 | } | - | ||||||||||||||||||||||||
| 325 | - | |||||||||||||||||||||||||
| 326 | QByteArray QMimeMagicRule::mask() const | - | ||||||||||||||||||||||||
| 327 | { | - | ||||||||||||||||||||||||
| 328 | QByteArray result = m_mask; | - | ||||||||||||||||||||||||
| 329 | if (m_type == String) {
| 0 | ||||||||||||||||||||||||
| 330 | // restore '0x' | - | ||||||||||||||||||||||||
| 331 | result = "0x" + result.toHex(); | - | ||||||||||||||||||||||||
| 332 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 333 | return result; never executed: return result; | 0 | ||||||||||||||||||||||||
| 334 | } | - | ||||||||||||||||||||||||
| 335 | - | |||||||||||||||||||||||||
| 336 | bool QMimeMagicRule::matches(const QByteArray &data) const | - | ||||||||||||||||||||||||
| 337 | { | - | ||||||||||||||||||||||||
| 338 | const bool ok = m_matchFunction && (this->*m_matchFunction)(data);
| 59-19185 | ||||||||||||||||||||||||
| 339 | if (!ok)
| 59-19185 | ||||||||||||||||||||||||
| 340 | return false; executed 19185 times by 1 test: return false;Executed by:
| 19185 | ||||||||||||||||||||||||
| 341 | - | |||||||||||||||||||||||||
| 342 | // No submatch? Then we are done. | - | ||||||||||||||||||||||||
| 343 | if (m_subMatches.isEmpty())
| 22-37 | ||||||||||||||||||||||||
| 344 | return true; executed 37 times by 1 test: return true;Executed by:
| 37 | ||||||||||||||||||||||||
| 345 | - | |||||||||||||||||||||||||
| 346 | //qDebug() << "Checking" << m_subMatches.count() << "sub-rules"; | - | ||||||||||||||||||||||||
| 347 | // Check that one of the submatches matches too | - | ||||||||||||||||||||||||
| 348 | for ( QList<QMimeMagicRule>::const_iterator it = m_subMatches.begin(), end = m_subMatches.end() ; | - | ||||||||||||||||||||||||
| 349 | it != end ; ++it ) {
| 18-22 | ||||||||||||||||||||||||
| 350 | if ((*it).matches(data)) {
| 4-18 | ||||||||||||||||||||||||
| 351 | // One of the hierarchies matched -> mimetype recognized. | - | ||||||||||||||||||||||||
| 352 | return true; executed 4 times by 1 test: return true;Executed by:
| 4 | ||||||||||||||||||||||||
| 353 | } | - | ||||||||||||||||||||||||
| 354 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||||||||
| 355 | return false; executed 18 times by 1 test: return false;Executed by:
| 18 | ||||||||||||||||||||||||
| 356 | - | |||||||||||||||||||||||||
| 357 | - | |||||||||||||||||||||||||
| 358 | } | - | ||||||||||||||||||||||||
| 359 | - | |||||||||||||||||||||||||
| 360 | QT_END_NAMESPACE | - | ||||||||||||||||||||||||
| 361 | - | |||||||||||||||||||||||||
| 362 | #endif // QT_NO_MIMETYPE | - | ||||||||||||||||||||||||
| Source code | Switch to Preprocessed file |