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 #ifndef QMIMEMAGICRULE_P_H
41 #define QMIMEMAGICRULE_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtCore/private/qglobal_p.h>
55 
56 QT_REQUIRE_CONFIG(mimetype);
57 
58 #include <QtCore/qbytearray.h>
59 #include <QtCore/qscopedpointer.h>
60 #include <QtCore/qlist.h>
61 
62 QT_BEGIN_NAMESPACE
63 
64 class QMimeMagicRule
65 {
66 public:
67     enum Type { Invalid = 0, String, Host16, Host32, Big16, Big32, Little16, Little32, Byte };
68 
69     QMimeMagicRule(const QString &typeStr, const QByteArray &value, const QString &offsets,
70                    const QByteArray &mask, QString *errorString);
71 
swap(QMimeMagicRule & other)72     void swap(QMimeMagicRule &other) noexcept
73     {
74         qSwap(m_type,          other.m_type);
75         qSwap(m_value,         other.m_value);
76         qSwap(m_startPos,      other.m_startPos);
77         qSwap(m_endPos,        other.m_endPos);
78         qSwap(m_mask,          other.m_mask);
79         qSwap(m_pattern,       other.m_pattern);
80         qSwap(m_number,        other.m_number);
81         qSwap(m_numberMask,    other.m_numberMask);
82         qSwap(m_matchFunction, other.m_matchFunction);
83     }
84 
85     bool operator==(const QMimeMagicRule &other) const;
86 
type()87     Type type() const { return m_type; }
value()88     QByteArray value() const { return m_value; }
startPos()89     int startPos() const { return m_startPos; }
endPos()90     int endPos() const { return m_endPos; }
91     QByteArray mask() const;
92 
isValid()93     bool isValid() const { return m_matchFunction != nullptr; }
94 
95     bool matches(const QByteArray &data) const;
96 
97     QList<QMimeMagicRule> m_subMatches;
98 
99     static Type type(const QByteArray &type);
100     static QByteArray typeName(Type type);
101 
102     static bool matchSubstring(const char *dataPtr, int dataSize, int rangeStart, int rangeLength, int valueLength, const char *valueData, const char *mask);
103 
104 private:
105     Type m_type;
106     QByteArray m_value;
107     int m_startPos;
108     int m_endPos;
109     QByteArray m_mask;
110 
111     QByteArray m_pattern;
112     quint32 m_number;
113     quint32 m_numberMask;
114 
115     typedef bool (QMimeMagicRule::*MatchFunction)(const QByteArray &data) const;
116     MatchFunction m_matchFunction;
117 
118 private:
119     // match functions
120     bool matchString(const QByteArray &data) const;
121     template <typename T>
122     bool matchNumber(const QByteArray &data) const;
123 };
124 Q_DECLARE_SHARED(QMimeMagicRule)
125 
126 QT_END_NAMESPACE
127 
128 #endif // QMIMEMAGICRULE_H
129