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.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 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
6
7#define CBOR_NO_ENCODER_API
8#include <private/qcborcommon_p.h>
9
10#include <private/qnumeric_p.h>
11#include <private/qstringconverter_p.h>
12#include <qiodevice.h>
13#include <qdebug.h>
14#include <qstack.h>
15#include <qvarlengtharray.h>
16
18
19static bool qt_cbor_decoder_can_read(void *token, size_t len);
20static void qt_cbor_decoder_advance(void *token, size_t len);
21static void *qt_cbor_decoder_read(void *token, void *userptr, size_t offset, size_t len);
22static CborError qt_cbor_decoder_transfer_string(void *token, const void **userptr, size_t offset, size_t len);
23
24#define CBOR_PARSER_READER_CONTROL 1
25#define CBOR_PARSER_CAN_READ_BYTES_FUNCTION qt_cbor_decoder_can_read
26#define CBOR_PARSER_ADVANCE_BYTES_FUNCTION qt_cbor_decoder_advance
27#define CBOR_PARSER_TRANSFER_STRING_FUNCTION qt_cbor_decoder_transfer_string
28#define CBOR_PARSER_READ_BYTES_FUNCTION qt_cbor_decoder_read
29
30QT_WARNING_PUSH
31QT_WARNING_DISABLE_MSVC(4334) // '<<': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
32QT_WARNING_DISABLE_GCC("-Wimplicit-fallthrough")
33
34#include <cborparser.c>
35#include <cborparser_dup_string.c>
36#include <cborparser_float.c>
37
38QT_WARNING_POP
39
40// confirm our constants match TinyCBOR's
41static_assert(int(QCborStreamReader::UnsignedInteger) == CborIntegerType);
42static_assert(int(QCborStreamReader::ByteString) == CborByteStringType);
43static_assert(int(QCborStreamReader::TextString) == CborTextStringType);
44static_assert(int(QCborStreamReader::Array) == CborArrayType);
45static_assert(int(QCborStreamReader::Map) == CborMapType);
46static_assert(int(QCborStreamReader::Tag) == CborTagType);
47static_assert(int(QCborStreamReader::SimpleType) == CborSimpleType);
48static_assert(int(QCborStreamReader::HalfFloat) == CborHalfFloatType);
49static_assert(int(QCborStreamReader::Float) == CborFloatType);
50static_assert(int(QCborStreamReader::Double) == CborDoubleType);
51static_assert(int(QCborStreamReader::Invalid) == CborInvalidType);
52
53/*!
54 \class QCborStreamReader
55 \inmodule QtCore
56 \ingroup cbor
57 \ingroup qtserialization
58 \reentrant
59 \since 5.12
60
61 \brief The QCborStreamReader class is a simple CBOR stream decoder, operating
62 on either a QByteArray or QIODevice.
63
64 This class can be used to decode a stream of CBOR content directly from
65 either a QByteArray or a QIODevice. CBOR is the Concise Binary Object
66 Representation, a very compact form of binary data encoding that is
67 compatible with JSON. It was created by the IETF Constrained RESTful
68 Environments (CoRE) WG, which has used it in many new RFCs. It is meant to
69 be used alongside the \l{RFC 7252}{CoAP
70 protocol}.
71
72 QCborStreamReader provides a StAX-like API, similar to that of
73 \l{QXmlStreamReader}. Using it requires a bit of knowledge of CBOR encoding.
74 For a simpler API, see \l{QCborValue} and especially the decoding function
75 QCborValue::fromCbor().
76
77 Typically, one creates a QCborStreamReader by passing the source QByteArray
78 or QIODevice as a parameter to the constructor, then pop elements off the
79 stream if there were no errors in decoding. There are three kinds of CBOR
80 types:
81
82 \table
83 \header \li Kind \li Types \li Behavior
84 \row \li Fixed-width \li Integers, Tags, Simple types, Floating point
85 \li Value is pre-parsed by QCborStreamReader, so accessor functions
86 are \c const. Must call next() to advance.
87 \row \li Strings \li Byte arrays, Text strings
88 \li Length (if known) is pre-parsed, but the string itself is not.
89 The accessor functions are not const and may allocate memory.
90 Once called, the accessor functions automatically advance to
91 the next element.
92 \row \li Containers \li Arrays, Maps
93 \li Length (if known) is pre-parsed. To access the elements, you
94 must call enterContainer(), read all elements, then call
95 leaveContainer(). That function advances to the next element.
96 \endtable
97
98 So a processor function typically looks like this:
99
100 \snippet code/src_corelib_serialization_qcborstream.cpp 24
101
102 \section1 CBOR support
103
104 The following table lists the CBOR features that QCborStreamReader supports.
105
106 \table
107 \header \li Feature \li Support
108 \row \li Unsigned numbers \li Yes (full range)
109 \row \li Negative numbers \li Yes (full range)
110 \row \li Byte strings \li Yes
111 \row \li Text strings \li Yes
112 \row \li Chunked strings \li Yes
113 \row \li Tags \li Yes (arbitrary)
114 \row \li Booleans \li Yes
115 \row \li Null \li Yes
116 \row \li Undefined \li Yes
117 \row \li Arbitrary simple values \li Yes
118 \row \li Half-precision float (16-bit) \li Yes
119 \row \li Single-precision float (32-bit) \li Yes
120 \row \li Double-precision float (64-bit) \li Yes
121 \row \li Infinities and NaN floating point \li Yes
122 \row \li Determinate-length arrays and maps \li Yes
123 \row \li Indeterminate-length arrays and maps \li Yes
124 \row \li Map key types other than strings and integers \li Yes (arbitrary)
125 \endtable
126
127 \section1 Dealing with invalid or incomplete CBOR streams
128
129 QCborStreamReader is capable of detecting corrupt input on its own. The
130 library it uses has been extensively tested against invalid input of any
131 kind and is quite able to report errors. If any is detected,
132 QCborStreamReader will set lastError() to a value besides
133 QCborError::NoError, indicating which situation was detected.
134
135 Most errors detected by QCborStreamReader during normal item parsing are not
136 recoverable. The code using QCborStreamReader may opt to handle the data
137 that was properly decoded or it can opt to discard the entire data.
138
139 The only recoverable error is QCborError::EndOfFile, which indicates that
140 more data is required in order to complete the parsing. This situation is
141 useful when data is being read from an asynchronous source, such as a pipe
142 (QProcess) or a socket (QTcpSocket, QUdpSocket, QNetworkReply, etc.). When
143 more data arrives, the surrounding code needs to call either addData(), if
144 parsing from a QByteArray, or reparse(), if it is instead reading directly
145 a the QIDOevice that now has more data available (see setDevice()).
146
147 \sa QCborStreamWriter, QCborValue, QXmlStreamReader,
148 {Parsing and displaying CBOR data}, {Serialization Converter},
149 {Saving and Loading a Game}
150 */
151
152/*!
153 \enum QCborStreamReader::Type
154
155 This enumeration contains all possible CBOR types as decoded by
156 QCborStreamReader. CBOR has 7 major types, plus a number of simple types
157 carrying no value, and floating point values.
158
159 \value UnsignedInteger (Major type 0) Ranges from 0 to 2\sup{64} - 1
160 (18,446,744,073,709,551,616)
161 \value NegativeInteger (Major type 1) Ranges from -1 to -2\sup{64}
162 (-18,446,744,073,709,551,616)
163 \value ByteArray (Major type 2) Arbitrary binary data.
164 \value ByteString An alias to ByteArray.
165 \value String (Major type 3) Unicode text, possibly containing NULs.
166 \value TextString An alias to String
167 \value Array (Major type 4) Array of heterogeneous items.
168 \value Map (Major type 5) Map/dictionary of heterogeneous items.
169 \value Tag (Major type 6) Numbers giving further semantic value
170 to generic CBOR items. See \l QCborTag for more information.
171 \value SimpleType (Major type 7) Types carrying no further value. Includes
172 booleans (true and false), null, undefined.
173 \value Float16 IEEE 754 half-precision floating point (\c qfloat16).
174 \value HalfFloat An alias to Float16.
175 \value Float IEEE 754 single-precision floating point (\tt float).
176 \value Double IEEE 754 double-precision floating point (\tt double).
177 \value Invalid Not a valid type, either due to parsing error or due to
178 reaching the end of an array or map.
179 */
180
181/*!
182 \enum QCborStreamReader::StringResultCode
183
184 This enum is returned by readString() and readByteArray() and is used to
185 indicate what the status of the parsing is.
186
187 \value EndOfString The parsing for the string is complete, with no error.
188 \value Ok The function returned data; there was no error.
189 \value Error Parsing failed with an error.
190 */
191
192/*!
193 \class QCborStreamReader::StringResult
194 \inmodule QtCore
195
196 StringResult<Container> is a template class where \a Container specifies
197 the type used to hold the string data (such as QString or QByteArray).
198
199 This class is returned by readString() and readByteArray(), with either the
200 contents of the string that was read or an indication that the parsing is
201 done or found an error.
202
203 The contents of \l data are valid only if \l status is
204 \l{StringResultCode}{Ok}. Otherwise, it should be null.
205 */
206
207/*!
208 \variable QCborStreamReader::StringResult::data
209
210 Contains the actual data from the string if \l status is \c Ok.
211 */
212
213/*!
214 \variable QCborStreamReader::StringResult::status
215
216 Contains the status of the attempt of reading the string from the stream.
217 */
218
219/*!
220 \fn QCborStreamReader::Type QCborStreamReader::type() const
221
222 Returns the type of the current element. It is one of the valid types or
223 Invalid.
224
225 \sa isValid(), isUnsignedInteger(), isNegativeInteger(), isInteger(),
226 isByteArray(), isString(), isArray(), isMap(), isTag(), isSimpleType(),
227 isBool(), isFalse(), isTrue(), isNull(), isUndefined(), isFloat16(),
228 isFloat(), isDouble()
229 */
230
231/*!
232 \fn bool QCborStreamReader::isValid() const
233
234 Returns true if the current element is valid, false otherwise. The current
235 element may be invalid if there was a decoding error or we've just parsed
236 the last element in an array or map.
237
238 \note This function is not the opposite of isNull(). Null is a normal CBOR
239 type that must be handled by the application.
240
241 \sa type(), isInvalid()
242 */
243
244/*!
245 \fn bool QCborStreamReader::isInvalid() const
246
247 Returns true if the current element is invalid, false otherwise. The current
248 element may be invalid if there was a decoding error or we've just parsed
249 the last element in an array or map.
250
251 \note This function is not to be confused with isNull(). Null is a normal
252 CBOR type that must be handled by the application.
253
254 \sa type(), isValid()
255 */
256
257/*!
258 \fn bool QCborStreamReader::isUnsignedInteger() const
259
260 Returns true if the type of the current element is an unsigned integer (that
261 is if type() returns QCborStreamReader::UnsignedInteger). If this function
262 returns true, you may call toUnsignedInteger() or toInteger() to read that value.
263
264 \sa type(), toUnsignedInteger(), toInteger(), isInteger(), isNegativeInteger()
265 */
266
267/*!
268 \fn bool QCborStreamReader::isNegativeInteger() const
269
270 Returns true if the type of the current element is a negative integer (that
271 is if type() returns QCborStreamReader::NegativeInteger). If this function
272 returns true, you may call toNegativeInteger() or toInteger() to read that value.
273
274 \sa type(), toNegativeInteger(), toInteger(), isInteger(), isUnsignedInteger()
275 */
276
277/*!
278 \fn bool QCborStreamReader::isInteger() const
279
280 Returns true if the type of the current element is either an unsigned
281 integer or a negative one (that is, if type() returns
282 QCborStreamReader::UnsignedInteger or QCborStreamReader::NegativeInteger).
283 If this function returns true, you may call toInteger() to read that
284 value.
285
286 \sa type(), toInteger(), toUnsignedInteger(), toNegativeInteger(),
287 isUnsignedInteger(), isNegativeInteger()
288 */
289
290/*!
291 \fn bool QCborStreamReader::isByteArray() const
292
293 Returns true if the type of the current element is a byte array (that is,
294 if type() returns QCborStreamReader::ByteArray). If this function returns
295 true, you may call readByteArray() to read that data.
296
297 \sa type(), readByteArray(), isString()
298 */
299
300/*!
301 \fn bool QCborStreamReader::isString() const
302
303 Returns true if the type of the current element is a text string (that is,
304 if type() returns QCborStreamReader::String). If this function returns
305 true, you may call readString() to read that data.
306
307 \sa type(), readString(), isByteArray()
308 */
309
310/*!
311 \fn bool QCborStreamReader::isArray() const
312
313 Returns true if the type of the current element is an array (that is,
314 if type() returns QCborStreamReader::Array). If this function returns
315 true, you may call enterContainer() to begin parsing that container.
316
317 When the current element is an array, you may also call isLengthKnown() to
318 find out if the array's size is explicit in the CBOR stream. If it is, that
319 size can be obtained by calling length().
320
321 The following example pre-allocates a QVariantList given the array's size
322 for more efficient decoding:
323
324 \snippet code/src_corelib_serialization_qcborstream.cpp 25
325
326 \note The code above does not validate that the length is a sensible value.
327 If the input stream reports that the length is 1 billion elements, the above
328 function will try to allocate some 16 GB or more of RAM, which can lead to a
329 crash.
330
331 \sa type(), isMap(), isLengthKnown(), length(), enterContainer(), leaveContainer()
332 */
333
334/*!
335 \fn bool QCborStreamReader::isMap() const
336
337 Returns true if the type of the current element is a map (that is, if type()
338 returns QCborStreamReader::Map). If this function returns true, you may call
339 enterContainer() to begin parsing that container.
340
341 When the current element is a map, you may also call isLengthKnown() to
342 find out if the map's size is explicit in the CBOR stream. If it is, that
343 size can be obtained by calling length().
344
345 The following example pre-allocates a QVariantMap given the map's size
346 for more efficient decoding:
347
348 \snippet code/src_corelib_serialization_qcborstream.cpp 26
349
350 The example above uses a function called \c readElementAsString to read the
351 map's keys and obtain a string. That is because CBOR maps may contain any
352 type as keys, not just strings. User code needs to either perform this
353 conversion, reject non-string keys, or instead use a different container
354 besides \l QVariantMap and \l QVariantHash. For example, if the map is
355 expected to contain integer keys, which is recommended as it reduces stream
356 size and parsing, the correct container would be \c{\l{QMap}<int, QVariant>}
357 or \c{\l{QHash}<int, QVariant>}.
358
359 \note The code above does not validate that the length is a sensible value.
360 If the input stream reports that the length is 1 billion elements, the above
361 function will try to allocate some 24 GB or more of RAM, which can lead to a
362 crash.
363
364 \sa type(), isArray(), isLengthKnown(), length(), enterContainer(), leaveContainer()
365 */
366
367/*!
368 \fn bool QCborStreamReader::isTag() const
369
370 Returns true if the type of the current element is a CBOR tag (that is,
371 if type() returns QCborStreamReader::Tag). If this function returns
372 true, you may call toTag() to read that data.
373
374 \sa type(), toTag()
375 */
376
377/*!
378 \fn bool QCborStreamReader::isFloat16() const
379
380 Returns true if the type of the current element is an IEEE 754
381 half-precision floating point (that is, if type() returns
382 QCborStreamReader::Float16). If this function returns true, you may call
383 toFloat16() to read that data.
384
385 \sa type(), toFloat16(), isFloat(), isDouble()
386 */
387
388/*!
389 \fn bool QCborStreamReader::isFloat() const
390
391 Returns true if the type of the current element is an IEEE 754
392 single-precision floating point (that is, if type() returns
393 QCborStreamReader::Float). If this function returns true, you may call
394 toFloat() to read that data.
395
396 \sa type(), toFloat(), isFloat16(), isDouble()
397 */
398
399/*!
400 \fn bool QCborStreamReader::isDouble() const
401
402 Returns true if the type of the current element is an IEEE 754
403 double-precision floating point (that is, if type() returns
404 QCborStreamReader::Double). If this function returns true, you may call
405 toDouble() to read that data.
406
407 \sa type(), toDouble(), isFloat16(), isFloat()
408 */
409
410/*!
411 \fn bool QCborStreamReader::isSimpleType() const
412
413 Returns true if the type of the current element is any CBOR simple type,
414 including a boolean value (true and false) as well as null and undefined. To
415 find out which simple type this is, call toSimpleType(). Alternatively, to
416 test for one specific simple type, call the overload that takes a
417 QCborSimpleType parameter.
418
419 CBOR simple types are types that do not carry extra value. There are 255
420 possibilities, but there are currently only four values that have defined
421 meaning. Code is not expected to cope with unknown simple types and may
422 simply discard the stream as invalid if it finds an unknown one.
423
424 \sa QCborSimpleType, type(), isSimpleType(QCborSimpleType), toSimpleType()
425 */
426
427/*!
428 \fn bool QCborStreamReader::isSimpleType(QCborSimpleType st) const
429
430 Returns true if the type of the current element is the simple type \a st,
431 false otherwise. If this function returns true, then toSimpleType() will
432 return \a st.
433
434 CBOR simple types are types that do not carry extra value. There are 255
435 possibilities, but there are currently only four values that have defined
436 meaning. Code is not expected to cope with unknown simple types and may
437 simply discard the stream as invalid if it finds an unknown one.
438
439 \sa QCborSimpleType, type(), isSimpleType(), toSimpleType()
440 */
441
442/*!
443 \fn bool QCborStreamReader::isFalse() const
444
445 Returns true if the current element is the \c false value, false if it is
446 anything else.
447
448 \sa type(), isTrue(), isBool(), toBool(), isSimpleType(), toSimpleType()
449 */
450
451/*!
452 \fn bool QCborStreamReader::isTrue() const
453
454 Returns true if the current element is the \c true value, false if it is
455 anything else.
456
457 \sa type(), isFalse(), isBool(), toBool(), isSimpleType(), toSimpleType()
458 */
459
460/*!
461 \fn bool QCborStreamReader::isBool() const
462
463 Returns true if the current element is a boolean value (\c true or \c
464 false), false if it is anything else. If this function returns true, you may
465 call toBool() to retrieve the value of the boolean. You may also call
466 toSimpleType() and compare to either QCborSimpleValue::True or
467 QCborSimpleValue::False.
468
469 \sa type(), isFalse(), isTrue(), toBool(), isSimpleType(), toSimpleType()
470 */
471
472/*!
473 \fn bool QCborStreamReader::isNull() const
474
475 Returns true if the current element is the \c null value, false if it is
476 anything else. Null values may be used to indicate the absence of some
477 optional data.
478
479 \note This function is not the opposite of isValid(). A Null value is a
480 valid CBOR value.
481
482 \sa type(), isSimpleType(), toSimpleType()
483 */
484
485/*!
486 \fn bool QCborStreamReader::isUndefined() const
487
488 Returns true if the current element is the \c undefined value, false if it
489 is anything else. Undefined values may be encoded to indicate that some
490 conversion failed or was not possible when creating the stream.
491 QCborStreamReader never performs any replacement and this function will only
492 return true if the stream contains an explicit undefined value.
493
494 \sa type(), isSimpleType(), toSimpleType()
495 */
496
497/*!
498 \fn bool QCborStreamReader::isContainer() const
499
500 Returns true if the current element is a container (that is, an array or a
501 map), false if it is anything else. If the current element is a container,
502 the isLengthKnown() function may be used to find out if the container's size
503 is explicit in the stream and, if so, length() can be used to get that size.
504
505 More importantly, for a container, the enterContainer() function is
506 available to begin iterating through the elements contained therein.
507
508 \sa type(), isArray(), isMap(), isLengthKnown(), length(), enterContainer(),
509 leaveContainer(), containerDepth()
510 */
511
513{
514public:
515 enum {
516 // 9 bytes is the maximum size for any integer, floating point or
517 // length in CBOR.
520 };
521
522 QIODevice *device;
525
529
531 bool corrupt = false;
532
534 : device(nullptr), buffer(data)
535 {
537 }
538
539 QCborStreamReaderPrivate(QIODevice *device)
540 {
541 setDevice(device);
542 }
543
547
548 void setDevice(QIODevice *dev)
549 {
550 buffer.clear();
551 device = dev;
553 }
554
556 {
557 containerStack.clear();
558 bufferStart = 0;
559 if (device) {
560 buffer.clear();
561 buffer.reserve(IdealIoBufferSize); // sets the CapacityReserved flag
562 }
563
564 preread();
565 if (CborError err = cbor_parser_init_reader(nullptr, &parser, &currentElement, this))
566 handleError(err);
567 else
568 lastError = { QCborError::NoError };
569 }
570
571 char *bufferPtr()
572 {
573 Q_ASSERT(buffer.isDetached());
574 return const_cast<char *>(buffer.constBegin()) + bufferStart;
575 }
576
577 void preread()
578 {
579 if (device && buffer.size() - bufferStart < MaxCborIndividualSize) {
580 // load more, but only if there's more to be read
581 qint64 avail = device->bytesAvailable();
582 Q_ASSERT(avail >= buffer.size());
583 if (avail == buffer.size())
584 return;
585
586 if (bufferStart)
587 device->skip(bufferStart); // skip what we've already parsed
588
589 if (buffer.size() != IdealIoBufferSize)
590 buffer.resize(IdealIoBufferSize);
591
592 bufferStart = 0;
593 qint64 read = device->peek(bufferPtr(), IdealIoBufferSize);
594 if (read < 0)
595 buffer.clear();
596 else if (read != IdealIoBufferSize)
597 buffer.truncate(read);
598 }
599 }
600
601 void handleError(CborError err) noexcept
602 {
603 Q_ASSERT(err);
604
605 // is the error fatal?
606 if (err != CborErrorUnexpectedEOF)
607 corrupt = true;
608
609 lastError = QCborError { QCborError::Code(int(err)) };
610 }
611
613 union {
614 char *ptr;
617 };
618 enum Type { ByteArray = -1, String = -3, Utf8String = -5 };
620
621 ReadStringChunk(char *ptr, qsizetype maxlen) : ptr(ptr), maxlen_or_type(maxlen) {}
624 bool isString() const { return maxlen_or_type == String; }
625 bool isUtf8String() const { return maxlen_or_type == Utf8String; }
626 bool isByteArray() const { return maxlen_or_type == ByteArray; }
627 bool isPlainPointer() const { return maxlen_or_type >= 0; }
628 };
629
637};
638
640{
641 d->handleError(CborError(error.c));
642}
643
644static inline bool qt_cbor_decoder_can_read(void *token, size_t len)
645{
647 auto self = static_cast<QCborStreamReaderPrivate *>(token);
648
649 qint64 avail = self->buffer.size() - self->bufferStart;
650 return len <= quint64(avail);
651}
652
653static void qt_cbor_decoder_advance(void *token, size_t len)
654{
656 auto self = static_cast<QCborStreamReaderPrivate *>(token);
657 Q_ASSERT(len <= size_t(self->buffer.size() - self->bufferStart));
658
659 self->bufferStart += int(len);
660 self->preread();
661}
662
663static void *qt_cbor_decoder_read(void *token, void *userptr, size_t offset, size_t len)
664{
665 Q_ASSERT(len == 1 || len == 2 || len == 4 || len == 8);
666 Q_ASSERT(offset == 0 || offset == 1);
667 auto self = static_cast<const QCborStreamReaderPrivate *>(token);
668
669 // we must have pre-read the data
670 Q_ASSERT(len + offset <= size_t(self->buffer.size() - self->bufferStart));
671 return memcpy(userptr, self->buffer.constBegin() + self->bufferStart + offset, len);
672}
673
674static CborError qt_cbor_decoder_transfer_string(void *token, const void **userptr, size_t offset, size_t len)
675{
676 auto self = static_cast<QCborStreamReaderPrivate *>(token);
677 Q_ASSERT(offset <= size_t(self->buffer.size()));
678 static_assert(sizeof(size_t) >= sizeof(QByteArray::size_type));
679 static_assert(sizeof(size_t) == sizeof(qsizetype));
680
681 // check that we will have enough data from the QIODevice before we advance
682 // (otherwise, we'd lose the length information)
683 qsizetype total;
684 if (len > size_t(std::numeric_limits<QByteArray::size_type>::max())
685 || qAddOverflow<qsizetype>(offset, len, &total))
686 return CborErrorDataTooLarge;
687
688 // our string transfer is just saving the offset to the userptr
689 *userptr = reinterpret_cast<void *>(offset);
690
691 qint64 avail = (self->device ? self->device->bytesAvailable() : self->buffer.size()) -
692 self->bufferStart;
693 return total > avail ? CborErrorUnexpectedEOF : CborNoError;
694}
695
697{
698 if (currentElement.flags & CborIteratorFlag_IteratingStringChunks)
699 return true;
700
701 CborError err = cbor_value_begin_string_iteration(&currentElement);
702 if (!err)
703 return true;
704 handleError(err);
705 return false;
706}
707
708/*!
709 \internal
710 */
711inline void QCborStreamReader::preparse()
712{
713 if (lastError() == QCborError::NoError) {
714 type_ = cbor_value_get_type(&d->currentElement);
715
716 if (type_ == CborInvalidType) {
717 // We may have reached the end.
718 if (d->device && d->containerStack.isEmpty()) {
719 d->buffer.clear();
720 if (d->bufferStart)
721 d->device->skip(d->bufferStart);
722 d->bufferStart = 0;
723 }
724 } else {
725 d->lastError = {};
726 // Undo the type mapping that TinyCBOR does (we have an explicit type
727 // for negative integer and we don't have separate types for Boolean,
728 // Null and Undefined).
729 if (type_ == CborBooleanType || type_ == CborNullType || type_ == CborUndefinedType) {
730 type_ = CborSimpleType;
731 value64 = quint8(d->buffer.at(d->bufferStart)) - CborSimpleType;
732 } else {
733 // Using internal TinyCBOR API!
734 value64 = _cbor_value_extract_int64_helper(&d->currentElement);
735
736 if (cbor_value_is_negative_integer(&d->currentElement))
737 type_ = quint8(QCborStreamReader::NegativeInteger);
738 }
739 }
740 } else {
741 type_ = Invalid;
742 }
743}
744
745/*!
746 Creates a QCborStreamReader object with no source data. After construction,
747 QCborStreamReader will report an error parsing.
748
749 You can add more data by calling addData() or by setting a different source
750 device using setDevice().
751
752 \sa addData(), isValid()
753 */
754QCborStreamReader::QCborStreamReader()
755 : d(new QCborStreamReaderPrivate({})), type_(Invalid)
756{
757}
758
759/*!
760 \overload
761
762 Creates a QCborStreamReader object with \a len bytes of data starting at \a
763 data. The pointer must remain valid until QCborStreamReader is destroyed.
764 */
765QCborStreamReader::QCborStreamReader(const char *data, qsizetype len)
766 : QCborStreamReader(QByteArray::fromRawData(data, len))
767{
768}
769
770/*!
771 \overload
772
773 Creates a QCborStreamReader object with \a len bytes of data starting at \a
774 data. The pointer must remain valid until QCborStreamReader is destroyed.
775 */
776QCborStreamReader::QCborStreamReader(const quint8 *data, qsizetype len)
777 : QCborStreamReader(QByteArray::fromRawData(reinterpret_cast<const char *>(data), len))
778{
779}
780
781/*!
782 \overload
783
784 Creates a QCborStreamReader object that will parse the CBOR stream found in
785 \a data.
786 */
787QCborStreamReader::QCborStreamReader(const QByteArray &data)
788 : d(new QCborStreamReaderPrivate(data))
789{
790 preparse();
791}
792
793/*!
794 \overload
795
796 Creates a QCborStreamReader object that will parse the CBOR stream found by
797 reading from \a device. QCborStreamReader does not take ownership of \a
798 device, so it must remain valid until this object is destroyed.
799 */
800QCborStreamReader::QCborStreamReader(QIODevice *device)
801 : d(new QCborStreamReaderPrivate(device))
802{
803 preparse();
804}
805
806/*!
807 Destroys this QCborStreamReader object and frees any associated resources.
808 */
809QCborStreamReader::~QCborStreamReader()
810{
811}
812
813/*!
814 Sets the source of data to \a device, resetting the decoder to its initial
815 state.
816 */
817void QCborStreamReader::setDevice(QIODevice *device)
818{
819 d->setDevice(device);
820 preparse();
821}
822
823/*!
824 Returns the QIODevice that was set with either setDevice() or the
825 QCborStreamReader constructor. If this object was reading from a QByteArray,
826 this function returns nullptr instead.
827 */
828QIODevice *QCborStreamReader::device() const
829{
830 return d->device;
831}
832
833/*!
834 Adds \a data to the CBOR stream and reparses the current element. This
835 function is useful if the end of the data was previously reached while
836 processing the stream, but now more data is available.
837 */
838void QCborStreamReader::addData(const QByteArray &data)
839{
840 addData(data.constBegin(), data.size());
841}
842
843/*!
844 \fn void QCborStreamReader::addData(const quint8 *data, qsizetype len)
845 \overload
846
847 Adds \a len bytes of data starting at \a data to the CBOR stream and
848 reparses the current element. This function is useful if the end of the data
849 was previously reached while processing the stream, but now more data is
850 available.
851 */
852
853/*!
854 \overload
855
856 Adds \a len bytes of data starting at \a data to the CBOR stream and
857 reparses the current element. This function is useful if the end of the data
858 was previously reached while processing the stream, but now more data is
859 available.
860 */
861void QCborStreamReader::addData(const char *data, qsizetype len)
862{
863 if (!d->device) {
864 if (len > 0)
865 d->buffer.append(data, len);
866 reparse();
867 } else {
868 qWarning("QCborStreamReader: addData() with device()");
869 }
870}
871
872/*!
873 Reparses the current element. This function must be called when more data
874 becomes available in the source QIODevice after parsing failed due to
875 reaching the end of the input data before the end of the CBOR stream.
876
877 When reading from QByteArray(), the addData() function automatically calls
878 this function. Calling it when the reading had not failed is a no-op.
879 */
880void QCborStreamReader::reparse()
881{
882 d->lastError = {};
883 d->preread();
884 if (CborError err = cbor_value_reparse(&d->currentElement))
885 d->handleError(err);
886 else
887 preparse();
888}
889
890/*!
891 Clears the decoder state and resets the input source data to an empty byte
892 array. After this function is called, QCborStreamReader will be indicating
893 an error parsing.
894
895 Call addData() to add more data to be parsed.
896
897 \sa reset(), setDevice()
898 */
899void QCborStreamReader::clear()
900{
901 setDevice(nullptr);
902}
903
904/*!
905 Resets the source back to the beginning and clears the decoder state. If the
906 source data was a QByteArray, QCborStreamReader will restart from the
907 beginning of the array.
908
909 If the source data is a QIODevice, this function will call
910 QIODevice::reset(), which will seek to byte position 0. If the CBOR stream
911 is not found at the beginning of the device (e.g., beginning of a file),
912 then this function will likely do the wrong thing. Instead, position the
913 QIODevice to the right offset and call setDevice().
914
915 \sa clear(), setDevice()
916 */
917void QCborStreamReader::reset()
918{
919 if (d->device)
920 d->device->reset();
921 d->lastError = {};
922 d->initDecoder();
923 preparse();
924}
925
926/*!
927 Returns the last error in decoding the stream, if any. If no error
928 was encountered, this returns an QCborError::NoError.
929
930 \sa isValid()
931 */
932QCborError QCborStreamReader::lastError() const
933{
934 return d->lastError;
935}
936
937/*!
938 Returns the offset in the input stream of the item currently being decoded.
939 The current offset is the number of decoded bytes so far only if the source
940 data is a QByteArray or it is a QIODevice that was positioned at its
941 beginning when decoding started.
942
943 \sa reset(), clear(), device()
944 */
945qint64 QCborStreamReader::currentOffset() const
946{
947 return (d->device ? d->device->pos() : 0) + d->bufferStart;
948}
949
950/*!
951 Returns the number of containers that this stream has entered with
952 enterContainer() but not yet left.
953
954 \sa enterContainer(), leaveContainer()
955 */
956int QCborStreamReader::containerDepth() const
957{
958 return d->containerStack.size();
959}
960
961/*!
962 Returns either QCborStreamReader::Array or QCborStreamReader::Map,
963 indicating whether the container that contains the current item was an array
964 or map, respectively. If we're currently parsing the root element, this
965 function returns QCborStreamReader::Invalid.
966
967 \sa containerDepth(), enterContainer()
968 */
969QCborStreamReader::Type QCborStreamReader::parentContainerType() const
970{
971 if (d->containerStack.isEmpty())
972 return Invalid;
973 return Type(cbor_value_get_type(&std::as_const(d->containerStack).top()));
974}
975
976/*!
977 Returns true if there are more items to be decoded in the current container
978 or false of we've reached its end. If we're parsing the root element,
979 hasNext() returning false indicates the parsing is complete; otherwise, if
980 the container depth is non-zero, then the outer code needs to call
981 leaveContainer().
982
983 \sa parentContainerType(), containerDepth(), leaveContainer()
984 */
985bool QCborStreamReader::hasNext() const noexcept
986{
987 return cbor_value_is_valid(&d->currentElement) &&
988 !cbor_value_at_end(&d->currentElement);
989}
990
991/*!
992 Advance the CBOR stream decoding one element. You should usually call this
993 function when parsing fixed-width basic elements (that is, integers, simple
994 values, tags and floating point values). But this function can be called
995 when the current item is a string, array or map too and it will skip over
996 that entire element, including all contained elements.
997
998 This function returns true if advancing was successful, false otherwise. It
999 may fail if the stream is corrupt, incomplete or if the nesting level of
1000 arrays and maps exceeds \a maxRecursion. Calling this function when
1001 hasNext() has returned false is also an error. If this function returns
1002 false, lastError() will return the error code detailing what the failure
1003 was.
1004
1005 \sa lastError(), isValid(), hasNext()
1006 */
1007bool QCborStreamReader::next(int maxRecursion)
1008{
1009 if (lastError() != QCborError::NoError)
1010 return false;
1011
1012 if (!hasNext()) {
1013 d->handleError(CborErrorAdvancePastEOF);
1014 } else if (maxRecursion < 0) {
1015 d->handleError(CborErrorNestingTooDeep);
1016 } else if (isContainer()) {
1017 // iterate over each element
1018 enterContainer();
1019 while (lastError() == QCborError::NoError && hasNext())
1020 next(maxRecursion - 1);
1021 if (lastError() == QCborError::NoError)
1022 leaveContainer();
1023 } else if (isByteArray()) {
1024 char c;
1025 StringResult<qsizetype> r;
1026 do {
1027 r = readStringChunk(&c, 1);
1028 } while (r.status == Ok);
1029 } else if (isString()) {
1030 // we need to use actual readString so we get UTF-8 validation
1031 StringResult<QString> r;
1032 do {
1033 r = readString();
1034 } while (r.status == Ok);
1035 } else {
1036 // fixed types
1037 CborError err = cbor_value_advance_fixed(&d->currentElement);
1038 if (err)
1039 d->handleError(err);
1040 }
1041
1042 preparse();
1043 return d->lastError == QCborError::NoError;
1044}
1045
1046/*!
1047 Returns true if the length of the current array, map, byte array or string
1048 is known (explicit in the CBOR stream), false otherwise. This function
1049 should only be called if the element is one of those.
1050
1051 If the length is known, it may be obtained by calling length().
1052
1053 If the length of a map or an array is not known, it is implied by the number
1054 of elements present in the stream. QCborStreamReader has no API to calculate
1055 the length in that condition.
1056
1057 Strings and byte arrays may also have indeterminate length (that is, they
1058 may be transmitted in multiple chunks). Those cannot currently be created
1059 with QCborStreamWriter, but they could be with other encoders, so
1060 QCborStreamReader supports them.
1061
1062 \sa length(), QCborStreamWriter::startArray(), QCborStreamWriter::startMap()
1063 */
1064bool QCborStreamReader::isLengthKnown() const noexcept
1065{
1066 return cbor_value_is_length_known(&d->currentElement);
1067}
1068
1069/*!
1070 Returns the length of the string or byte array, or the number of items in an
1071 array or the number, of item pairs in a map, if known. This function must
1072 not be called if the length is unknown (that is, if isLengthKnown() returned
1073 false). It is an error to do that and it will cause QCborStreamReader to
1074 stop parsing the input stream.
1075
1076 \sa isLengthKnown(), QCborStreamWriter::startArray(), QCborStreamWriter::startMap()
1077 */
1078quint64 QCborStreamReader::length() const
1079{
1080 CborError err;
1081 switch (type()) {
1082 case String:
1083 case ByteArray:
1084 case Map:
1085 case Array:
1086 if (isLengthKnown())
1087 return value64;
1088 err = CborErrorUnknownLength;
1089 break;
1090
1091 default:
1092 err = CborErrorIllegalType;
1093 break;
1094 }
1095
1096 d->handleError(err);
1097 return quint64(-1);
1098}
1099
1100/*!
1101 \fn bool QCborStreamReader::enterContainer()
1102
1103 Enters the array or map that is the current item and prepares for iterating
1104 the elements contained in the container. Returns true if entering the
1105 container succeeded, false otherwise (usually, a parsing error). Each call
1106 to enterContainer() must be paired with a call to leaveContainer().
1107
1108 This function may only be called if the current item is an array or a map
1109 (that is, if isArray(), isMap() or isContainer() is true). Calling it in any
1110 other condition is an error.
1111
1112 \sa leaveContainer(), isContainer(), isArray(), isMap()
1113 */
1114bool QCborStreamReader::_enterContainer_helper()
1115{
1116 d->containerStack.push(d->currentElement);
1117 CborError err = cbor_value_enter_container(&d->containerStack.top(), &d->currentElement);
1118 if (!err) {
1119 preparse();
1120 return true;
1121 }
1122 d->handleError(err);
1123 return false;
1124}
1125
1126/*!
1127 Leaves the array or map whose items were being processed and positions the
1128 decoder at the next item after the end of the container. Returns true if
1129 leaving the container succeeded, false otherwise (usually, a parsing error).
1130 Each call to enterContainer() must be paired with a call to
1131 leaveContainer().
1132
1133 This function may only be called if hasNext() has returned false and
1134 containerDepth() is not zero. Calling it in any other condition is an error.
1135
1136 \sa enterContainer(), parentContainerType(), containerDepth()
1137 */
1138bool QCborStreamReader::leaveContainer()
1139{
1140 if (d->containerStack.isEmpty()) {
1141 qWarning("QCborStreamReader::leaveContainer: trying to leave top-level element");
1142 return false;
1143 }
1144 if (d->corrupt)
1145 return false;
1146
1147 CborValue container = d->containerStack.pop();
1148 CborError err = cbor_value_leave_container(&container, &d->currentElement);
1149 d->currentElement = container;
1150 if (err) {
1151 d->handleError(err);
1152 return false;
1153 }
1154
1155 preparse();
1156 return true;
1157}
1158
1159/*!
1160 \fn bool QCborStreamReader::toBool() const
1161
1162 Returns the boolean value of the current element.
1163
1164 This function does not perform any type conversions, including from integer.
1165 Therefore, it may only be called if isTrue(), isFalse() or isBool() returned
1166 true; calling it in any other condition is an error.
1167
1168 \sa isBool(), isTrue(), isFalse(), toInteger()
1169 */
1170
1171/*!
1172 \fn QCborTag QCborStreamReader::toTag() const
1173
1174 Returns the tag value of the current element.
1175
1176 This function does not perform any type conversions, including from integer.
1177 Therefore, it may only be called if isTag() is true; calling it in any other
1178 condition is an error.
1179
1180 Tags are 64-bit numbers attached to generic CBOR types that give them
1181 further meaning. For a list of known tags, see the \l QCborKnownTags
1182 enumeration.
1183
1184 \sa isTag(), toInteger(), QCborKnownTags
1185 */
1186
1187/*!
1188 \fn quint64 QCborStreamReader::toUnsignedInteger() const
1189
1190 Returns the unsigned integer value of the current element.
1191
1192 This function does not perform any type conversions, including from boolean
1193 or CBOR tag. Therefore, it may only be called if isUnsignedInteger() is
1194 true; calling it in any other condition is an error.
1195
1196 This function may be used to obtain numbers beyond the range of the return
1197 type of toInteger().
1198
1199 \sa type(), toInteger(), isUnsignedInteger(), isNegativeInteger()
1200 */
1201
1202/*!
1203 \fn QCborNegativeValue QCborStreamReader::toNegativeInteger() const
1204
1205 Returns the negative integer value of the current element.
1206 QCborNegativeValue is a 64-bit unsigned integer containing the absolute
1207 value of the negative number that was stored in the CBOR stream.
1208 Additionally, QCborNegativeValue(0) represents the number -2\sup{64}.
1209
1210 This function does not perform any type conversions, including from boolean
1211 or CBOR tag. Therefore, it may only be called if isNegativeInteger() is
1212 true; calling it in any other condition is an error.
1213
1214 This function may be used to obtain numbers beyond the range of the return
1215 type of toInteger(). However, use of negative numbers smaller than -2\sup{63}
1216 is extremely discouraged.
1217
1218 \sa type(), toInteger(), isNegativeInteger(), isUnsignedInteger()
1219 */
1220
1221/*!
1222 \fn qint64 QCborStreamReader::toInteger() const
1223
1224 Returns the integer value of the current element, be it negative, positive
1225 or zero. If the value is larger than 2\sup{63} - 1 or smaller than
1226 -2\sup{63}, the returned value will overflow and will have an incorrect
1227 sign. If handling those values is required, use toUnsignedInteger() or
1228 toNegativeInteger() instead.
1229
1230 This function does not perform any type conversions, including from boolean
1231 or CBOR tag. Therefore, it may only be called if isInteger() is true;
1232 calling it in any other condition is an error.
1233
1234 \sa isInteger(), toUnsignedInteger(), toNegativeInteger()
1235 */
1236
1237/*!
1238 \fn QCborSimpleType QCborStreamReader::toSimpleType() const
1239
1240 Returns value of the current simple type.
1241
1242 This function does not perform any type conversions, including from integer.
1243 Therefore, it may only be called if isSimpleType() is true; calling it in
1244 any other condition is an error.
1245
1246 \sa isSimpleType(), isTrue(), isFalse(), isBool(), isNull(), isUndefined()
1247 */
1248
1249/*!
1250 \fn qfloat16 QCborStreamReader::toFloat16() const
1251
1252 Returns the 16-bit half-precision floating point value of the current element.
1253
1254 This function does not perform any type conversions, including from other
1255 floating point types or from integer values. Therefore, it may only be
1256 called if isFloat16() is true; calling it in any other condition is an
1257 error.
1258
1259 \sa isFloat16(), toFloat(), toDouble()
1260 */
1261
1262/*!
1263 \fn float QCborStreamReader::toFloat() const
1264
1265 Returns the 32-bit single-precision floating point value of the current
1266 element.
1267
1268 This function does not perform any type conversions, including from other
1269 floating point types or from integer values. Therefore, it may only be
1270 called if isFloat() is true; calling it in any other condition is an error.
1271
1272 \sa isFloat(), toFloat16(), toDouble()
1273 */
1274
1275/*!
1276 \fn double QCborStreamReader::toDouble() const
1277
1278 Returns the 64-bit double-precision floating point value of the current
1279 element.
1280
1281 This function does not perform any type conversions, including from other
1282 floating point types or from integer values. Therefore, it may only be
1283 called if isDouble() is true; calling it in any other condition is an error.
1284
1285 \sa isDouble(), toFloat16(), toFloat()
1286 */
1287
1288/*!
1289 \fn QCborStreamReader::StringResult<QString> QCborStreamReader::readString()
1290
1291 Decodes one string chunk from the CBOR string and returns it. This function
1292 is used for both regular and chunked string contents, so the caller must
1293 always loop around calling this function, even if isLengthKnown()
1294 is true. The typical use of this function is as follows:
1295
1296 \snippet code/src_corelib_serialization_qcborstream.cpp 27
1297
1298 The readAllString() function implements the above loop and some extra checks.
1299
1300//! [string-no-type-conversions]
1301 This function does not perform any type conversions, including from integers
1302 or from byte arrays. Therefore, it may only be called if isString() returned
1303 true; calling it in any other condition is an error.
1304//! [string-no-type-conversions]
1305
1306 \sa readAllString(), readByteArray(), isString(), readStringChunk()
1307 */
1308QCborStreamReader::StringResult<QString> QCborStreamReader::_readString_helper()
1309{
1310 QCborStreamReader::StringResult<QString> result;
1311 auto r = d->readStringChunk(&result.data);
1312 result.status = r.status;
1313 if (r.status == Error) {
1314 result.data.clear();
1315 } else {
1316 Q_ASSERT(r.data == result.data.size());
1317 if (r.status == EndOfString && lastError() == QCborError::NoError)
1318 preparse();
1319 }
1320
1321 return result;
1322}
1323
1324/*!
1325 \fn QCborStreamReader::StringResult<QByteArray> QCborStreamReader::readUtf8String()
1326 \since 6.7
1327
1328 Decodes one string chunk from the CBOR string and returns it. This function
1329 is used for both regular and chunked string contents, so the caller must
1330 always loop around calling this function, even if isLengthKnown() is true.
1331 The typical use of this function is as for readString() in the following:
1332
1333 \snippet code/src_corelib_serialization_qcborstream.cpp 27
1334
1335 The readAllUtf8String() function implements the above loop and some extra checks.
1336
1337 \include qcborstreamreader.cpp string-no-type-conversions
1338
1339 \sa readAllString(), readByteArray(), isString(), readStringChunk()
1340 */
1341QCborStreamReader::StringResult<QByteArray> QCborStreamReader::_readUtf8String_helper()
1342{
1343 using P = QCborStreamReaderPrivate::ReadStringChunk;
1344 QCborStreamReader::StringResult<QByteArray> result;
1345 auto r = d->readStringChunk(P{ &result.data, P::Utf8String });
1346 result.status = r.status;
1347 if (r.status == Error) {
1348 result.data.clear();
1349 } else {
1350 Q_ASSERT(r.data == result.data.size());
1351 if (r.status == EndOfString && lastError() == QCborError::NoError)
1352 preparse();
1353 }
1354
1355 return result;
1356}
1357
1358/*!
1359 \fn QCborStreamReader::StringResult<QByteArray> QCborStreamReader::readByteArray()
1360
1361 Decodes one byte array chunk from the CBOR string and returns it. This
1362 function is used for both regular and chunked contents, so the caller must
1363 always loop around calling this function, even if isLengthKnown()
1364 is true. The typical use of this function is as follows:
1365
1366 \snippet code/src_corelib_serialization_qcborstream.cpp 28
1367
1368 The readAllByteArray() function implements the above loop and some extra checks.
1369
1370//! [bytearray-no-type-conversions]
1371 This function does not perform any type conversions, including from integers
1372 or from strings. Therefore, it may only be called if isByteArray() is true;
1373 calling it in any other condition is an error.
1374//! [bytearray-no-type-conversions]
1375
1376 \sa readAllByteArray(), readString(), isByteArray(), readStringChunk()
1377 */
1378QCborStreamReader::StringResult<QByteArray> QCborStreamReader::_readByteArray_helper()
1379{
1380 QCborStreamReader::StringResult<QByteArray> result;
1381 auto r = d->readStringChunk(&result.data);
1382 result.status = r.status;
1383 if (r.status == Error) {
1384 result.data.clear();
1385 } else {
1386 Q_ASSERT(r.data == result.data.size());
1387 if (r.status == EndOfString && lastError() == QCborError::NoError)
1388 preparse();
1389 }
1390
1391 return result;
1392}
1393
1394/*!
1395 \fn qsizetype QCborStreamReader::currentStringChunkSize() const
1396
1397 Returns the size of the current text or byte string chunk. If the CBOR
1398 stream contains a non-chunked string (that is, if isLengthKnown() returns
1399 \c true), this function returns the size of the entire string, the same as
1400 length().
1401
1402 This function is useful to pre-allocate the buffer whose pointer can be passed
1403 to readStringChunk() later.
1404
1405 \sa readString(), readByteArray(), readStringChunk()
1406 */
1407qsizetype QCborStreamReader::_currentStringChunkSize() const
1408{
1409 if (!d->ensureStringIteration())
1410 return -1;
1411
1412 size_t len;
1413 CborError err = cbor_value_get_string_chunk_size(&d->currentElement, &len);
1414 if (err == CborErrorNoMoreStringChunks)
1415 return 0; // not a real error
1416 else if (err)
1417 d->handleError(err);
1418 else if (qsizetype(len) < 0)
1419 d->handleError(CborErrorDataTooLarge);
1420 else
1421 return qsizetype(len);
1422 return -1;
1423}
1424
1426{
1427 auto r = readStringChunk(params);
1428 while (r.status == QCborStreamReader::Ok) {
1429 // keep appending
1430 r = readStringChunk(params);
1431 }
1432
1433 bool ok = r.status == QCborStreamReader::EndOfString;
1434 Q_ASSERT(ok == !lastError);
1435 return ok;
1436}
1437
1438/*!
1439 \fn QCborStreamReader::readAllString()
1440 \since 6.7
1441
1442 Decodes the current text string and returns it. If the string is chunked,
1443 this function will iterate over all chunks and concatenate them. If an
1444 error happens, this function returns a default-constructed QString(), but
1445 that may not be distinguishable from certain empty text strings. Instead,
1446 check lastError() to determine if an error has happened.
1447
1448 \include qcborstreamreader.cpp string-no-type-conversions
1449
1450//! [note-not-restartable]
1451 \note This function cannot be resumed. That is, this function should not
1452 be used in contexts where the CBOR data may still be received, for example
1453 from a socket or pipe. It should only be used when the full data has
1454 already been received and is available in the input QByteArray or
1455 QIODevice.
1456//! [note-not-restartable]
1457
1458 \sa readString(), readStringChunk(), isString(), readAllByteArray()
1459 */
1460/*!
1461 \fn QCborStreamReader::readAndAppendToString(QString &dst)
1462 \since 6.7
1463
1464 Decodes the current text string and appends to \a dst. If the string is
1465 chunked, this function will iterate over all chunks and concatenate them.
1466 If an error happens during decoding, other chunks that could be decoded
1467 successfully may have been written to \a dst nonetheless. Returns \c true
1468 if the decoding happened without errors, \c false otherwise.
1469
1470 \include qcborstreamreader.cpp string-no-type-conversions
1471
1472 \include qcborstreamreader.cpp note-not-restartable
1473
1474 \sa readString(), readStringChunk(), isString(), readAndAppendToByteArray()
1475 */
1476bool QCborStreamReader::_readAndAppendToString_helper(QString &dst)
1477{
1478 bool ok = d->readFullString(&dst);
1479 if (ok)
1480 preparse();
1481 return ok;
1482}
1483
1484/*!
1485 \fn QCborStreamReader::readAllUtf8String()
1486 \since 6.7
1487
1488 Decodes the current text string and returns it. If the string is chunked,
1489 this function will iterate over all chunks and concatenate them. If an
1490 error happens, this function returns a default-constructed QString(), but
1491 that may not be distinguishable from certain empty text strings. Instead,
1492 check lastError() to determine if an error has happened.
1493
1494 \include qcborstreamreader.cpp string-no-type-conversions
1495
1496 \include qcborstreamreader.cpp note-not-restartable
1497
1498 \sa readString(), readStringChunk(), isString(), readAllByteArray()
1499 */
1500/*!
1501 \fn QCborStreamReader::readAndAppendToUtf8String(QByteArray &dst)
1502 \since 6.7
1503
1504 Decodes the current text string and appends to \a dst. If the string is
1505 chunked, this function will iterate over all chunks and concatenate them.
1506 If an error happens during decoding, other chunks that could be decoded
1507 successfully may have been written to \a dst nonetheless. Returns \c true
1508 if the decoding happened without errors, \c false otherwise.
1509
1510 \include qcborstreamreader.cpp string-no-type-conversions
1511
1512 \include qcborstreamreader.cpp note-not-restartable
1513
1514 \sa readString(), readStringChunk(), isString(), readAndAppendToByteArray()
1515 */
1516bool QCborStreamReader::_readAndAppendToUtf8String_helper(QByteArray &dst)
1517{
1518 using P = QCborStreamReaderPrivate::ReadStringChunk;
1519 bool ok = d->readFullString({ &dst, P::Utf8String });
1520 if (ok)
1521 preparse();
1522 return ok;
1523}
1524
1525/*!
1526 \fn QCborStreamReader::readAllByteArray()
1527 \since 6.7
1528
1529 Decodes the current byte string and returns it. If the string is chunked,
1530 this function will iterate over all chunks and concatenate them. If an
1531 error happens, this function returns a default-constructed QByteArray(),
1532 but that may not be distinguishable from certain empty byte strings.
1533 Instead, check lastError() to determine if an error has happened.
1534
1535 \include qcborstreamreader.cpp bytearray-no-type-conversions
1536
1537 \include qcborstreamreader.cpp note-not-restartable
1538
1539 \sa readByteArray(), readStringChunk(), isByteArray(), readAllString()
1540 */
1541
1542/*!
1543 \fn QCborStreamReader::readAndAppendToByteArray(QByteArray &dst)
1544 \since 6.7
1545
1546 Decodes the current byte string and appends to \a dst. If the string is
1547 chunked, this function will iterate over all chunks and concatenate them.
1548 If an error happens during decoding, other chunks that could be decoded
1549 successfully may have been written to \a dst nonetheless. Returns \c true
1550 if the decoding happened without errors, \c false otherwise.
1551
1552 \include qcborstreamreader.cpp bytearray-no-type-conversions
1553
1554 \include qcborstreamreader.cpp note-not-restartable
1555
1556 \sa readByteArray(), readStringChunk(), isByteArray(), readAndAppendToString()
1557 */
1558bool QCborStreamReader::_readAndAppendToByteArray_helper(QByteArray &dst)
1559{
1560 bool ok = d->readFullString(&dst);
1561 if (ok)
1562 preparse();
1563 return ok;
1564}
1565
1566/*!
1567 Reads the current string chunk into the buffer pointed to by \a ptr, whose
1568 size is \a maxlen. This function returns a \l StringResult object, with the
1569 number of bytes copied into \a ptr saved in the \c \l StringResult::data
1570 member. The \c \l StringResult::status member indicates whether there was
1571 an error reading the string, whether data was copied or whether this was
1572 the last chunk.
1573
1574 This function can be called for both \l String and \l ByteArray types.
1575 For the latter, this function will read the same data that readByteArray()
1576 would have returned. For strings, it returns the UTF-8 equivalent of the \l
1577 QString that would have been returned.
1578
1579 This function is usually used alongside currentStringChunkSize() in a loop.
1580 For example:
1581
1582 \snippet code/src_corelib_serialization_qcborstream.cpp 29
1583
1584 Unlike readByteArray() and readString(), this function is not limited by
1585 implementation limits of QByteArray and QString.
1586
1587 \note This function does not perform verification that the UTF-8 contents
1588 are properly formatted. That means this function does not produce the
1589 QCborError::InvalidUtf8String error, even when readString() does.
1590
1591 \sa currentStringChunkSize(), readString(), readByteArray(),
1592 isString(), isByteArray()
1593 */
1594QCborStreamReader::StringResult<qsizetype>
1595QCborStreamReader::readStringChunk(char *ptr, qsizetype maxlen)
1596{
1597 auto r = d->readStringChunk({ptr, maxlen});
1598 if (r.status == EndOfString && lastError() == QCborError::NoError)
1599 preparse();
1600 return r;
1601}
1602
1603// used by qcborvalue.cpp
1604QCborStreamReader::StringResultCode qt_cbor_append_string_chunk(QCborStreamReader &reader, QByteArray *data)
1605{
1606 return QCborStreamReaderPrivate::appendStringChunk(reader, data);
1607}
1608
1609inline QCborStreamReader::StringResultCode
1610QCborStreamReaderPrivate::appendStringChunk(QCborStreamReader &reader, QByteArray *data)
1611{
1612 auto status = reader.d->readStringChunk(data).status;
1613 if (status == QCborStreamReader::EndOfString && reader.lastError() == QCborError::NoError)
1614 reader.preparse();
1615 return status;
1616}
1617
1618Q_NEVER_INLINE QCborStreamReader::StringResult<qsizetype>
1619QCborStreamReaderPrivate::readStringChunk(ReadStringChunk params)
1620{
1621 CborError err;
1622 size_t len;
1623 const void *content = nullptr;
1624 QCborStreamReader::StringResult<qsizetype> result;
1625 result.data = 0;
1626 result.status = QCborStreamReader::Error;
1627
1628 lastError = {};
1629 if (!ensureStringIteration())
1630 return result;
1631
1632 // Note: in the current implementation, the call into TinyCBOR below only
1633 // succeeds if we *already* have all the data in memory. That's obvious for
1634 // the case of direct memory (no QIODevice), whereas for QIODevices
1635 // qt_cbor_decoder_transfer_string() enforces that
1636 // QIODevice::bytesAvailable() be bigger than the amount we're about to
1637 // read.
1638 //
1639 // This is an important security gate: if the CBOR stream is corrupt or
1640 // malicious, and has an impossibly large string size, we only go past it
1641 // if the transfer to the destination buffer will succeed (modulo QIODevice
1642 // I/O failures).
1643
1644#if 1
1645 // Using internal TinyCBOR API!
1646 err = _cbor_value_get_string_chunk(&currentElement, &content, &len, &currentElement);
1647#else
1648 // the above is effectively the same as:
1649 if (cbor_value_is_byte_string(&currentElement))
1650 err = cbor_value_get_byte_string_chunk(&currentElement, reinterpret_cast<const uint8_t **>(&content),
1651 &len, &currentElement);
1652 else
1653 err = cbor_value_get_text_string_chunk(&currentElement, reinterpret_cast<const char **>(&content),
1654 &len, &currentElement);
1655#endif
1656
1657 // Range check: using implementation-defined behavior in converting an
1658 // unsigned value out of range of the destination signed type (same as
1659 // "len > size_t(std::numeric_limits<qsizetype>::max())", but generates
1660 // better code with ICC and MSVC).
1661 if (!err && qsizetype(len) < 0)
1662 err = CborErrorDataTooLarge;
1663
1664 if (err) {
1665 if (err == CborErrorNoMoreStringChunks) {
1666 preread();
1667 err = cbor_value_finish_string_iteration(&currentElement);
1668 result.status = QCborStreamReader::EndOfString;
1669 }
1670 if (err)
1671 handleError(err);
1672 // caller musts call preparse()
1673 return result;
1674 }
1675
1676 qptrdiff offset = qptrdiff(content);
1677 bufferStart += offset;
1678 if (device) {
1679 // This first skip can't fail because we've already read this many bytes.
1680 device->skip(bufferStart);
1681 }
1682
1683 if (params.isString()) {
1684 // readString()
1685 result.data = readStringChunk_unicode(params, qsizetype(len));
1686 } else if (params.isUtf8String()) {
1687 result.data = readStringChunk_utf8(params, qsizetype(len));
1688 } else {
1689 // readByteArray() or readStringChunk()
1690 result.data = readStringChunk_byte(params, qsizetype(len));
1691 }
1692
1693 if (result.data < 0)
1694 return result; // error
1695
1696 // adjust the buffers after we're done reading the string
1697 bufferStart += len;
1698 if (device) {
1699 qsizetype remainingInBuffer = buffer.size() - bufferStart;
1700
1701 if (remainingInBuffer <= 0) {
1702 // We've read from the QIODevice more than what was in the buffer.
1703 buffer.truncate(0);
1704 } else {
1705 // There's still data buffered, but we need to move it around.
1706 char *ptr = buffer.data();
1707 memmove(ptr, ptr + bufferStart, remainingInBuffer);
1708 buffer.truncate(remainingInBuffer);
1709 }
1710
1711 bufferStart = 0;
1712 }
1713
1714 preread();
1715 result.status = QCborStreamReader::Ok;
1716 return result;
1717}
1718
1719inline qsizetype
1721{
1722 qint64 actuallyRead;
1723 qsizetype toRead = qsizetype(len);
1724 qsizetype left = 0; // bytes from the chunk not copied to the user buffer, to discard
1725 char *ptr = nullptr;
1726
1727 if (params.isPlainPointer()) {
1728 left = toRead - params.maxlen_or_type;
1729 if (left < 0)
1730 left = 0; // buffer bigger than string
1731 else
1732 toRead = params.maxlen_or_type; // buffer smaller than string
1733 ptr = params.ptr;
1734 } else if (!params.isString()) {
1735 // See note above on having ensured there is enough incoming data.
1736 auto oldSize = params.array->size();
1737 auto newSize = oldSize;
1738 if (qAddOverflow<decltype(newSize)>(oldSize, toRead, &newSize)) {
1739 handleError(CborErrorDataTooLarge);
1740 return -1;
1741 }
1742 QT_TRY {
1743 params.array->resize(newSize);
1744 } QT_CATCH (const std::bad_alloc &) {
1745 // the distinction between DataTooLarge and OOM is mostly for
1746 // compatibility with Qt 5; in Qt 6, we could consider everything
1747 // to be OOM.
1748 handleError(newSize > QByteArray::maxSize() ? CborErrorDataTooLarge: CborErrorOutOfMemory);
1749 return -1;
1750 }
1751
1752 ptr = const_cast<char *>(params.array->constBegin()) + oldSize;
1753 }
1754
1755 if (device) {
1756 actuallyRead = device->read(ptr, toRead);
1757
1758 if (actuallyRead != toRead) {
1759 actuallyRead = -1;
1760 } else if (left) {
1761 qint64 skipped = device->skip(left);
1762 if (skipped != left)
1763 actuallyRead = -1;
1764 }
1765
1766 if (actuallyRead < 0) {
1767 handleError(CborErrorIO);
1768 return -1;
1769 }
1770 } else {
1771 actuallyRead = toRead;
1772 memcpy(ptr, buffer.constBegin() + bufferStart, toRead);
1773 }
1774
1775 return actuallyRead;
1776}
1777
1778inline qsizetype
1780{
1781 Q_ASSERT(params.isString());
1782
1783 // See QUtf8::convertToUnicode() a detailed explanation of why this
1784 // conversion uses the same number of words or less.
1785 qsizetype currentSize = params.string->size();
1786 size_t newSize = size_t(utf8len) + size_t(currentSize); // can't overflow
1787 if (utf8len > QString::maxSize() || qsizetype(newSize) < 0) {
1788 handleError(CborErrorDataTooLarge);
1789 return -1;
1790 }
1791 QT_TRY {
1792 params.string->resize(qsizetype(newSize));
1793 } QT_CATCH (const std::bad_alloc &) {
1794 handleError(CborErrorOutOfMemory);
1795 return -1;
1796 }
1797
1798 QChar *begin = const_cast<QChar *>(params.string->constBegin());
1799 QChar *ptr = begin + currentSize;
1800 QStringConverter::State cs(QStringConverter::Flag::Stateless);
1801 if (device == nullptr) {
1802 // Easy case: we can decode straight from the buffer we already have
1803 ptr = QUtf8::convertToUnicode(ptr, { buffer.constBegin() + bufferStart, utf8len }, &cs);
1804 } else {
1805 // read in chunks, to avoid creating large, intermediate buffers
1806 constexpr qsizetype StringChunkSize = 16384;
1807 qsizetype chunkSize = qMin(StringChunkSize, utf8len);
1808 QVarLengthArray<char> chunk(chunkSize);
1809
1810 cs = { QStringConverter::Flag::ConvertInitialBom };
1811 while (utf8len > 0 && cs.invalidChars == 0) {
1812 qsizetype toRead = qMin(chunkSize, utf8len);
1813 qint64 actuallyRead = device->read(chunk.data(), toRead);
1814 if (actuallyRead == toRead)
1815 ptr = QUtf8::convertToUnicode(ptr, { chunk.data(), toRead }, &cs);
1816
1817 if (actuallyRead != toRead) {
1818 handleError(CborErrorIO);
1819 return -1;
1820 }
1821 utf8len -= toRead;
1822 }
1823 }
1824
1825 if (cs.invalidChars != 0 || cs.remainingChars != 0) {
1826 handleError(CborErrorInvalidUtf8TextString);
1827 return -1;
1828 }
1829
1830 qsizetype size = ptr - begin;
1831 params.string->truncate(ptr - begin);
1832 return size - currentSize; // how many bytes we added
1833}
1834
1835inline qsizetype
1837{
1838 qsizetype result = readStringChunk_byte(params, utf8len);
1839 if (result < 0)
1840 return result;
1841
1842 // validate the UTF-8 content we've just read
1843 QByteArrayView chunk = *params.array;
1844 chunk = chunk.last(result);
1845 if (QtPrivate::isValidUtf8(chunk))
1846 return result;
1847
1848 handleError(CborErrorInvalidUtf8TextString);
1849 return -1;
1850}
1851
1852QT_END_NAMESPACE
1853
1854#include "moc_qcborstreamreader.cpp"
void handleError(CborError err) noexcept
QCborStreamReaderPrivate(QIODevice *device)
QByteArray::size_type bufferStart
qsizetype readStringChunk_byte(ReadStringChunk params, qsizetype len)
bool readFullString(ReadStringChunk params)
QStack< CborValue > containerStack
qsizetype readStringChunk_unicode(ReadStringChunk params, qsizetype utf8len)
qsizetype readStringChunk_utf8(ReadStringChunk params, qsizetype utf8len)
void setDevice(QIODevice *dev)
QCborStreamReaderPrivate(const QByteArray &data)
Combined button and popup list for selecting options.
static CborError qt_cbor_decoder_transfer_string(void *token, const void **userptr, size_t offset, size_t len)
QCborStreamReader::StringResultCode qt_cbor_append_string_chunk(QCborStreamReader &reader, QByteArray *data)
static QT_BEGIN_NAMESPACE bool qt_cbor_decoder_can_read(void *token, size_t len)
void qt_cbor_stream_set_error(QCborStreamReaderPrivate *d, QCborError error)
static void qt_cbor_decoder_advance(void *token, size_t len)
static void * qt_cbor_decoder_read(void *token, void *userptr, size_t offset, size_t len)
ReadStringChunk(QByteArray *array, Type type=ByteArray)