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 | |
4 | #ifndef QMIMEMAGICRULE_P_H |
5 | #define QMIMEMAGICRULE_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtCore/private/qglobal_p.h> |
19 | |
20 | QT_REQUIRE_CONFIG(mimetype); |
21 | |
22 | #include <QtCore/qbytearray.h> |
23 | #include <QtCore/qscopedpointer.h> |
24 | #include <QtCore/qlist.h> |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | class QMimeMagicRule |
29 | { |
30 | public: |
31 | enum Type { Invalid = 0, String, Host16, Host32, Big16, Big32, Little16, Little32, Byte }; |
32 | |
33 | QMimeMagicRule(const QString &typeStr, const QByteArray &value, const QString &offsets, |
34 | const QByteArray &mask, QString *errorString); |
35 | |
36 | void swap(QMimeMagicRule &other) noexcept |
37 | { |
38 | qSwap(value1&: m_type, value2&: other.m_type); |
39 | qSwap(value1&: m_value, value2&: other.m_value); |
40 | qSwap(value1&: m_startPos, value2&: other.m_startPos); |
41 | qSwap(value1&: m_endPos, value2&: other.m_endPos); |
42 | qSwap(value1&: m_mask, value2&: other.m_mask); |
43 | qSwap(value1&: m_pattern, value2&: other.m_pattern); |
44 | qSwap(value1&: m_number, value2&: other.m_number); |
45 | qSwap(value1&: m_numberMask, value2&: other.m_numberMask); |
46 | qSwap(value1&: m_matchFunction, value2&: other.m_matchFunction); |
47 | } |
48 | |
49 | bool operator==(const QMimeMagicRule &other) const; |
50 | |
51 | Type type() const { return m_type; } |
52 | QByteArray value() const { return m_value; } |
53 | int startPos() const { return m_startPos; } |
54 | int endPos() const { return m_endPos; } |
55 | QByteArray mask() const; |
56 | |
57 | bool isValid() const { return m_matchFunction != nullptr; } |
58 | |
59 | bool matches(const QByteArray &data) const; |
60 | |
61 | QList<QMimeMagicRule> m_subMatches; |
62 | |
63 | static Type type(const QByteArray &type); |
64 | static QByteArray typeName(Type type); |
65 | |
66 | static bool matchSubstring(const char *dataPtr, qsizetype dataSize, int rangeStart, |
67 | int rangeLength, qsizetype valueLength, const char *valueData, |
68 | const char *mask); |
69 | |
70 | private: |
71 | Type m_type; |
72 | QByteArray m_value; |
73 | int m_startPos; |
74 | int m_endPos; |
75 | QByteArray m_mask; |
76 | |
77 | QByteArray m_pattern; |
78 | quint32 m_number; |
79 | quint32 m_numberMask; |
80 | |
81 | typedef bool (QMimeMagicRule::*MatchFunction)(const QByteArray &data) const; |
82 | MatchFunction m_matchFunction; |
83 | |
84 | private: |
85 | // match functions |
86 | bool matchString(const QByteArray &data) const; |
87 | template <typename T> |
88 | bool matchNumber(const QByteArray &data) const; |
89 | }; |
90 | Q_DECLARE_SHARED(QMimeMagicRule) |
91 | |
92 | QT_END_NAMESPACE |
93 | |
94 | #endif // QMIMEMAGICRULE_H |
95 | |