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 bytesAvailable() const;
92 qint64 currentOffset() const;
93
94 bool isValid() const { return !isInvalid(); }
95
96 int containerDepth() const;
97 QCborStreamReader::Type parentContainerType() const;
98 Q_DECL_PURE_FUNCTION bool hasNext() const noexcept;
99 bool next(int maxRecursion = 10000);
100
101 Type type() const { return QCborStreamReader::Type(type_); }
102 bool isUnsignedInteger() const { return type() == UnsignedInteger; }
103 bool isNegativeInteger() const { return type() == NegativeInteger; }
104 bool isInteger() const { return quint8(type()) <= quint8(NegativeInteger); }
105 bool isByteArray() const { return type() == ByteArray; }
106 bool isString() const { return type() == String; }
107 bool isArray() const { return type() == Array; }
108 bool isMap() const { return type() == Map; }
109 bool isTag() const { return type() == Tag; }
110 bool isSimpleType() const { return type() == SimpleType; }
111 bool isFloat16() const { return type() == Float16; }
112 bool isFloat() const { return type() == Float; }
113 bool isDouble() const { return type() == Double; }
114 bool isNumber() const { return isInteger() || isFloat16() || isFloat() || isDouble(); }
115 bool isInvalid() const { return type() == Invalid; }
116
117 bool isSimpleType(QCborSimpleType st) const { return isSimpleType() && toSimpleType() == st; }
118 bool isFalse() const { return isSimpleType(QCborSimpleType::False); }
119 bool isTrue() const { return isSimpleType(QCborSimpleType::True); }
120 bool isBool() const { return isFalse() || isTrue(); }
121 bool isNull() const { return isSimpleType(QCborSimpleType::Null); }
122 bool isUndefined() const { return isSimpleType(QCborSimpleType::Undefined); }
123
124 Q_DECL_PURE_FUNCTION bool isLengthKnown() const noexcept;
125 quint64 length() const;
126
127 bool isContainer() const { return isMap() || isArray(); }
128 bool enterContainer() { Q_ASSERT(isContainer()); return _enterContainer_helper(); }
129 bool leaveContainer();
130
131 bool readAndAppendToString(QString &dst)
132 { Q_ASSERT(isString()); return _readAndAppendToString_helper(dst); }
133 bool readAndAppendToUtf8String(QByteArray &dst)
134 { Q_ASSERT(isString()); return _readAndAppendToUtf8String_helper(dst); }
135 bool readAndAppendToByteArray(QByteArray &dst)
136 { Q_ASSERT(isByteArray()); return _readAndAppendToByteArray_helper(dst); }
137 StringResult<QString> readString() { Q_ASSERT(isString()); return _readString_helper(); }
138 StringResult<QByteArray> readUtf8String() { Q_ASSERT(isString()); return _readUtf8String_helper(); }
139 StringResult<QByteArray> readByteArray(){ Q_ASSERT(isByteArray()); return _readByteArray_helper(); }
140 qsizetype currentStringChunkSize() const{ Q_ASSERT(isString() || isByteArray()); return _currentStringChunkSize(); }
141 StringResult<qsizetype> readStringChunk(char *ptr, qsizetype maxlen);
142
143 bool toBool() const { Q_ASSERT(isBool()); return value64 - int(QCborSimpleType::False); }
144 QCborTag toTag() const { Q_ASSERT(isTag()); return QCborTag(value64); }
145 quint64 toUnsignedInteger() const { Q_ASSERT(isUnsignedInteger()); return value64; }
146 QCborNegativeInteger toNegativeInteger() const { Q_ASSERT(isNegativeInteger()); return QCborNegativeInteger(value64 + 1); }
147 QCborSimpleType toSimpleType() const{ Q_ASSERT(isSimpleType()); return QCborSimpleType(value64); }
148 qfloat16 toFloat16() const { Q_ASSERT(isFloat16()); return _toFloatingPoint<qfloat16>(); }
149 float toFloat() const { Q_ASSERT(isFloat()); return _toFloatingPoint<float>(); }
150 double toDouble() const { Q_ASSERT(isDouble()); return _toFloatingPoint<double>(); }
151
152 qint64 toInteger() const
153 {
154 Q_ASSERT(isInteger());
155 qint64 v = qint64(value64);
156 if (isNegativeInteger())
157 return -v - 1;
158 return v;
159 }
160 double toNumber() const
161 {
162 Q_ASSERT(isNumber());
163 if (isDouble())
164 return _toFloatingPoint<double>();
165 if (isFloat())
166 return _toFloatingPoint<float>();
167 if (isFloat16())
168 return _toFloatingPoint<qfloat16>();
169
170 // integer
171 double val = value64;
172 if (isNegativeInteger())
173 return -val - 1;
174 return val;
175 }
176 QString readAllString()
177 {
178 QString dst;
179 if (!readAndAppendToString(dst))
180 dst = QString{};
181 return dst;
182 }
183 QByteArray readAllUtf8String()
184 {
185 QByteArray dst;
186 if (!readAndAppendToUtf8String(dst))
187 dst = QByteArray{};
188 return dst;
189 }
190 QByteArray readAllByteArray()
191 {
192 QByteArray dst;
193 if (!readAndAppendToByteArray(dst))
194 dst = QByteArray{};
195 return dst;
196 }
197
198private:
199 void preparse();
200 bool _enterContainer_helper();
201 StringResult<QString> _readString_helper();
202 StringResult<QByteArray> _readUtf8String_helper();
203 StringResult<QByteArray> _readByteArray_helper();
204 qsizetype _currentStringChunkSize() const;
205 bool _readAndAppendToString_helper(QString &);
206 bool _readAndAppendToUtf8String_helper(QByteArray &);
207 bool _readAndAppendToByteArray_helper(QByteArray &);
208
209 template <typename FP> FP _toFloatingPoint() const noexcept
210 {
211 using UIntFP = typename QIntegerForSizeof<FP>::Unsigned;
212 UIntFP u = UIntFP(value64);
213#ifdef __cpp_lib_bit_cast
214 return std::bit_cast<FP>(u);
215#else
216 FP f;
217 memcpy(static_cast<void *>(&f), &u, sizeof(f));
218 return f;
219#endif
220 }
221
222 friend QCborStreamReaderPrivate;
223 friend class QCborContainerPrivate;
224 quint64 value64;
225 std::unique_ptr<QCborStreamReaderPrivate> d;
226 quint8 type_;
227 quint8 reserved[3] = {};
228};
229
230QT_END_NAMESPACE
231
232#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
Combined button and popup list for selecting options.
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)