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
qcborstreamreader.h
Go to the documentation of this file.
1// Copyright (C) 2018 Intel Corporation.
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:critical reason:data-parser
4
5#ifndef QCBORSTREAMREADER_H
6#define QCBORSTREAMREADER_H
7
8#include <QtCore/qbytearray.h>
9#include <QtCore/qcborcommon.h>
10#include <QtCore/qfloat16.h>
11#include <QtCore/qscopedpointer.h>
12#include <QtCore/qstring.h>
13#include <QtCore/qstringview.h>
14
15#ifdef __cpp_lib_bit_cast
16#include <bit>
17#endif
18#include <memory>
19
21
22/* X11 headers use these values too, but as defines */
23#if defined(False) && defined(True)
24# undef True
25# undef False
26#endif
27
28QT_BEGIN_NAMESPACE
29
30class QIODevice;
31
33class Q_CORE_EXPORT QCborStreamReader
34{
35 Q_GADGET
36public:
37 enum Type : quint8 {
38 UnsignedInteger = 0x00,
39 NegativeInteger = 0x20,
40 ByteString = 0x40,
41 ByteArray = ByteString,
42 TextString = 0x60,
43 String = TextString,
44 Array = 0x80,
45 Map = 0xa0,
46 Tag = 0xc0,
47 SimpleType = 0xe0,
48 HalfFloat = 0xf9,
49 Float16 = HalfFloat,
50 Float = 0xfa,
51 Double = 0xfb,
52
53 Invalid = 0xff
54 };
55 Q_ENUM(Type)
56
57 enum StringResultCode {
58 EndOfString = 0,
59 Ok = 1,
60 Error = -1
61 };
62 template <typename Container> struct StringResult {
63 Container data;
64 StringResultCode status = Error;
65 };
66 Q_ENUM(StringResultCode)
67
68 QCborStreamReader();
69 QCborStreamReader(const char *data, qsizetype len);
70 QCborStreamReader(const quint8 *data, qsizetype len);
71 explicit QCborStreamReader(const QByteArray &data);
72 explicit QCborStreamReader(QIODevice *device);
73 ~QCborStreamReader();
74 Q_DISABLE_COPY(QCborStreamReader)
75
76 void setDevice(QIODevice *device);
77 QIODevice *device() const;
78 void addData(const QByteArray &data);
79 void addData(const char *data, qsizetype len);
80 void addData(const quint8 *data, qsizetype len)
81 { addData(reinterpret_cast<const char *>(data), len); }
82 void reparse();
83 void clear();
84 void reset();
85
86#if QT_CORE_REMOVED_SINCE(6, 7)
87 QCborError lastError();
88#endif
89 QCborError lastError() const;
90
91 qint64 currentOffset() const;
92
93 bool isValid() const { return !isInvalid(); }
94
95 int containerDepth() const;
96 QCborStreamReader::Type parentContainerType() const;
97 Q_DECL_PURE_FUNCTION bool hasNext() const noexcept;
98 bool next(int maxRecursion = 10000);
99
100 Type type() const { return QCborStreamReader::Type(type_); }
101 bool isUnsignedInteger() const { return type() == UnsignedInteger; }
102 bool isNegativeInteger() const { return type() == NegativeInteger; }
103 bool isInteger() const { return quint8(type()) <= quint8(NegativeInteger); }
104 bool isByteArray() const { return type() == ByteArray; }
105 bool isString() const { return type() == String; }
106 bool isArray() const { return type() == Array; }
107 bool isMap() const { return type() == Map; }
108 bool isTag() const { return type() == Tag; }
109 bool isSimpleType() const { return type() == SimpleType; }
110 bool isFloat16() const { return type() == Float16; }
111 bool isFloat() const { return type() == Float; }
112 bool isDouble() const { return type() == Double; }
113 bool isInvalid() const { return type() == Invalid; }
114
115 bool isSimpleType(QCborSimpleType st) const { return isSimpleType() && toSimpleType() == st; }
116 bool isFalse() const { return isSimpleType(QCborSimpleType::False); }
117 bool isTrue() const { return isSimpleType(QCborSimpleType::True); }
118 bool isBool() const { return isFalse() || isTrue(); }
119 bool isNull() const { return isSimpleType(QCborSimpleType::Null); }
120 bool isUndefined() const { return isSimpleType(QCborSimpleType::Undefined); }
121
122 Q_DECL_PURE_FUNCTION bool isLengthKnown() const noexcept;
123 quint64 length() const;
124
125 bool isContainer() const { return isMap() || isArray(); }
126 bool enterContainer() { Q_ASSERT(isContainer()); return _enterContainer_helper(); }
127 bool leaveContainer();
128
129 bool readAndAppendToString(QString &dst)
130 { Q_ASSERT(isString()); return _readAndAppendToString_helper(dst); }
131 bool readAndAppendToUtf8String(QByteArray &dst)
132 { Q_ASSERT(isString()); return _readAndAppendToUtf8String_helper(dst); }
133 bool readAndAppendToByteArray(QByteArray &dst)
134 { Q_ASSERT(isByteArray()); return _readAndAppendToByteArray_helper(dst); }
135 StringResult<QString> readString() { Q_ASSERT(isString()); return _readString_helper(); }
136 StringResult<QByteArray> readUtf8String() { Q_ASSERT(isString()); return _readUtf8String_helper(); }
137 StringResult<QByteArray> readByteArray(){ Q_ASSERT(isByteArray()); return _readByteArray_helper(); }
138 qsizetype currentStringChunkSize() const{ Q_ASSERT(isString() || isByteArray()); return _currentStringChunkSize(); }
139 StringResult<qsizetype> readStringChunk(char *ptr, qsizetype maxlen);
140
141 bool toBool() const { Q_ASSERT(isBool()); return value64 - int(QCborSimpleType::False); }
142 QCborTag toTag() const { Q_ASSERT(isTag()); return QCborTag(value64); }
143 quint64 toUnsignedInteger() const { Q_ASSERT(isUnsignedInteger()); return value64; }
144 QCborNegativeInteger toNegativeInteger() const { Q_ASSERT(isNegativeInteger()); return QCborNegativeInteger(value64 + 1); }
145 QCborSimpleType toSimpleType() const{ Q_ASSERT(isSimpleType()); return QCborSimpleType(value64); }
146 qfloat16 toFloat16() const { Q_ASSERT(isFloat16()); return _toFloatingPoint<qfloat16>(); }
147 float toFloat() const { Q_ASSERT(isFloat()); return _toFloatingPoint<float>(); }
148 double toDouble() const { Q_ASSERT(isDouble()); return _toFloatingPoint<double>(); }
149
150 qint64 toInteger() const
151 {
152 Q_ASSERT(isInteger());
153 qint64 v = qint64(value64);
154 if (isNegativeInteger())
155 return -v - 1;
156 return v;
157 }
158 QString readAllString()
159 {
160 QString dst;
161 if (!readAndAppendToString(dst))
162 dst = QString{};
163 return dst;
164 }
165 QByteArray readAllUtf8String()
166 {
167 QByteArray dst;
168 if (!readAndAppendToUtf8String(dst))
169 dst = QByteArray{};
170 return dst;
171 }
172 QByteArray readAllByteArray()
173 {
174 QByteArray dst;
175 if (!readAndAppendToByteArray(dst))
176 dst = QByteArray{};
177 return dst;
178 }
179
180private:
181 void preparse();
182 bool _enterContainer_helper();
183 StringResult<QString> _readString_helper();
184 StringResult<QByteArray> _readUtf8String_helper();
185 StringResult<QByteArray> _readByteArray_helper();
186 qsizetype _currentStringChunkSize() const;
187 bool _readAndAppendToString_helper(QString &);
188 bool _readAndAppendToUtf8String_helper(QByteArray &);
189 bool _readAndAppendToByteArray_helper(QByteArray &);
190
191 template <typename FP> FP _toFloatingPoint() const noexcept
192 {
193 using UIntFP = typename QIntegerForSizeof<FP>::Unsigned;
194 UIntFP u = UIntFP(value64);
195#ifdef __cpp_lib_bit_cast
196 return std::bit_cast<FP>(u);
197#else
198 FP f;
199 memcpy(static_cast<void *>(&f), &u, sizeof(f));
200 return f;
201#endif
202 }
203
204 friend QCborStreamReaderPrivate;
205 friend class QCborContainerPrivate;
206 quint64 value64;
207 std::unique_ptr<QCborStreamReaderPrivate> d;
208 quint8 type_;
209 quint8 reserved[3] = {};
210};
211
212QT_END_NAMESPACE
213
214#endif // QCBORSTREAMREADER_H
\inmodule QtCore\reentrant
void setExtraSearchPath(const QString &path)
int indexOf(const QString &needle) const
QMultiMap< int, QString > keyMap() const
MetaDataList metaData() const
QList< QCborArray > metaDataKeys() const
QObject * instance(int index) const
QT_REQUIRE_CONFIG(cborstreamreader)
Q_TRACE_POINT(qtcore, QFactoryLoader_update, const QString &fileName)
static bool isIidMatch(QByteArrayView raw, QLatin1StringView iid)
static IterationResult iterateInPluginMetaData(QByteArrayView raw, F &&f)
PluginInterface * qLoadPlugin(const QFactoryLoader *loader, const QString &key, Args &&...args)