Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmimemagicrule_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QMIMEMAGICRULE_P_H
6#define QMIMEMAGICRULE_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/private/qglobal_p.h>
20
22
23#include <QtCore/qbytearray.h>
24#include <QtCore/qscopedpointer.h>
25#include <QtCore/qlist.h>
26
27QT_BEGIN_NAMESPACE
28
29class QMimeMagicRule
30{
31public:
32 enum Type { Invalid = 0, String, Host16, Host32, Big16, Big32, Little16, Little32, Byte };
33 // Maximum nesting depth of magic rules that is followed, both for <match>
34 // elements in XML and for matchlets in mime.cache. The deepest nesting in
35 // the shared-mime-info database is 5, so this leaves ample headroom while
36 // keeping recursion bounded for corrupt or hostile input.
37 static constexpr int MaxRecursionDepth = 50;
38
39 QMimeMagicRule(const QString &typeStr, const QByteArray &value, const QString &offsets,
40 const QByteArray &mask, QString *errorString);
41
42 void swap(QMimeMagicRule &other) noexcept
43 {
44 qSwap(m_type, other.m_type);
45 qSwap(m_value, other.m_value);
46 qSwap(m_startPos, other.m_startPos);
47 qSwap(m_endPos, other.m_endPos);
48 qSwap(m_mask, other.m_mask);
49 qSwap(m_pattern, other.m_pattern);
50 qSwap(m_number, other.m_number);
51 qSwap(m_numberMask, other.m_numberMask);
52 qSwap(m_matchFunction, other.m_matchFunction);
53 }
54
55 bool operator==(const QMimeMagicRule &other) const;
56
57 Type type() const { return m_type; }
58 QByteArray value() const { return m_value; }
59 int startPos() const { return m_startPos; }
60 int endPos() const { return m_endPos; }
61 QByteArray mask() const;
62
63 bool isValid() const { return m_matchFunction != nullptr; }
64
65 bool matches(const QByteArray &data, int depth = MaxRecursionDepth) const;
66
67 QList<QMimeMagicRule> m_subMatches;
68
69 static Type type(const QByteArray &type);
70 static QByteArray typeName(Type type);
71
72 static bool matchSubstring(const char *dataPtr, qsizetype dataSize, quint64 rangeStart,
73 quint64 rangeLength, quint64 valueLength, const char *valueData,
74 const char *mask);
75
76private:
77 Type m_type;
78 QByteArray m_value;
79 int m_startPos;
80 int m_endPos;
81 QByteArray m_mask;
82
83 QByteArray m_pattern;
84 quint32 m_number;
85 quint32 m_numberMask;
86
87 typedef bool (QMimeMagicRule::*MatchFunction)(const QByteArray &data) const;
88 MatchFunction m_matchFunction;
89
90private:
91 // match functions
92 bool matchString(const QByteArray &data) const;
93 template <typename T>
94 bool matchNumber(const QByteArray &data) const;
95};
96Q_DECLARE_SHARED(QMimeMagicRule)
97
98QT_END_NAMESPACE
99
100#endif // QMIMEMAGICRULE_H
Combined button and popup list for selecting options.
QT_REQUIRE_CONFIG(mimetype)
static QByteArray makePattern(const QByteArray &value)
static constexpr auto magicRuleTypes