| 1 | // Copyright (C) 2018 Intel Corporation. |
| 2 | // Copyright (C) 2019 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | // Qt-Security score:critical reason:data-parser |
| 5 | |
| 6 | #define CBOR_NO_ENCODER_API |
| 7 | #define CBOR_NO_PARSER_API |
| 8 | #include "qcborcommon_p.h" |
| 9 | |
| 10 | #include <QtCore/qdatastream.h> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | QT_IMPL_METATYPE_EXTERN(QCborTag) |
| 15 | |
| 16 | #include <cborerrorstrings.c> |
| 17 | |
| 18 | /*! |
| 19 | \headerfile <QtCborCommon> |
| 20 | \inmodule QtCore |
| 21 | \ingroup qtserialization |
| 22 | \brief The <QtCborCommon> header contains definitions common to both the |
| 23 | streaming classes (QCborStreamReader and QCborStreamWriter) and to |
| 24 | QCborValue. |
| 25 | |
| 26 | \sa QCborError |
| 27 | */ |
| 28 | |
| 29 | /*! |
| 30 | \enum QCborSimpleType |
| 31 | \relates <QtCborCommon> |
| 32 | |
| 33 | This enum contains the possible "Simple Types" for CBOR. Simple Types range |
| 34 | from 0 to 255 and are types that carry no further value. |
| 35 | |
| 36 | The following values are currently known: |
| 37 | |
| 38 | \value False A "false" boolean. |
| 39 | \value True A "true" boolean. |
| 40 | \value Null Absence of value (null). |
| 41 | \value Undefined Missing or deleted value, usually an error. |
| 42 | |
| 43 | Qt CBOR API supports encoding and decoding any Simple Type, whether one of |
| 44 | those above or any other value. |
| 45 | |
| 46 | Applications should only use further values if a corresponding specification |
| 47 | has been published, otherwise interpretation and validation by the remote |
| 48 | may fail. Values 24 to 31 are reserved and must not be used. |
| 49 | |
| 50 | The current authoritative list is maintained by IANA in the |
| 51 | \l{https://www.iana.org/assignments/cbor-simple-values/cbor-simple-values.xml}{Simple |
| 52 | Values registry}. |
| 53 | |
| 54 | \sa QCborStreamWriter::append(QCborSimpleType), QCborStreamReader::isSimpleType(), |
| 55 | QCborStreamReader::toSimpleType(), QCborValue::isSimpleType(), QCborValue::toSimpleType() |
| 56 | */ |
| 57 | |
| 58 | #if !defined(QT_NO_DATASTREAM) |
| 59 | QDataStream &operator<<(QDataStream &ds, QCborSimpleType st) |
| 60 | { |
| 61 | return ds << quint8(st); |
| 62 | } |
| 63 | |
| 64 | QDataStream &operator>>(QDataStream &ds, QCborSimpleType &st) |
| 65 | { |
| 66 | quint8 v; |
| 67 | ds >> v; |
| 68 | st = QCborSimpleType(v); |
| 69 | return ds; |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | /*! |
| 74 | \enum QCborTag |
| 75 | \relates <QtCborCommon> |
| 76 | |
| 77 | This enum contains no enumeration and is used only to provide type-safe |
| 78 | access to a CBOR tag. |
| 79 | |
| 80 | CBOR tags are 64-bit numbers that are attached to generic CBOR types to |
| 81 | provide further semantic meaning. QCborTag may be constructed from an |
| 82 | enumeration found in QCborKnownTags or directly by providing the numeric |
| 83 | representation. |
| 84 | |
| 85 | For example, the following creates a QCborValue containing a byte array |
| 86 | tagged with a tag 2. |
| 87 | |
| 88 | \snippet code/src_corelib_serialization_qcborstream.cpp 0 |
| 89 | |
| 90 | \sa QCborKnownTags, QCborStreamWriter::append(QCborTag), |
| 91 | QCborStreamReader::isTag(), QCborStreamReader::toTag(), |
| 92 | QCborValue::isTag(), QCborValue::tag() |
| 93 | */ |
| 94 | |
| 95 | /*! |
| 96 | \enum QCborKnownTags |
| 97 | \relates <QtCborCommon> |
| 98 | |
| 99 | This enum contains a list of CBOR tags, known at the time of the Qt |
| 100 | implementation. This list is not meant to be complete and contains only |
| 101 | tags that are either backed by an RFC or specifically used by the Qt |
| 102 | implementation. |
| 103 | |
| 104 | The authoritative list is maintained by IANA in the |
| 105 | \l{https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml}{CBOR tag |
| 106 | registry}. |
| 107 | |
| 108 | \value DateTimeString A date and time string, formatted according to RFC 3339, as refined |
| 109 | by RFC 4287. It is the same format as Qt::ISODate and |
| 110 | Qt::ISODateWithMs. |
| 111 | \value UnixTime_t A numerical representation of seconds elapsed since |
| 112 | 1970-01-01T00:00Z. |
| 113 | \value PositiveBignum A positive number of arbitrary length, encoded as a byte array in |
| 114 | network byte order. For example, the number 2\sup{64} is represented by |
| 115 | a byte array containing the byte value 0x01 followed by 8 zero bytes. |
| 116 | \value NegativeBignum A negative number of arbitrary length, encoded as the absolute value |
| 117 | of that number, minus one. For example, a byte array containing |
| 118 | byte value 0x02 followed by 8 zero bytes represents the number |
| 119 | -2\sup{65} - 1. |
| 120 | \value Decimal A decimal fraction, encoded as an array of two integers: the first |
| 121 | is the exponent of the power of 10, the second the integral |
| 122 | mantissa. The value 273.15 would be encoded as array \c{[-2, 27315]}. |
| 123 | \value Bigfloat Similar to Decimal, but the exponent is a power of 2 instead. |
| 124 | \value COSE_Encrypt0 An \c Encrypt0 map as specified by \l{RFC 8152} |
| 125 | (CBOR Object Signing and Encryption). |
| 126 | \value COSE_Mac0 A \c Mac0 map as specified by \l{RFC 8152} |
| 127 | (CBOR Object Signing and Encryption). |
| 128 | \value COSE_Sign1 A \c Sign1 map as specified by \l{RFC 8152} |
| 129 | (CBOR Object Signing and Encryption). |
| 130 | \value ExpectedBase64url Indicates that the byte array should be encoded using Base64url |
| 131 | if the stream is converted to JSON. |
| 132 | \value ExpectedBase64 Indicates that the byte array should be encoded using Base64 |
| 133 | if the stream is converted to JSON. |
| 134 | \value ExpectedBase16 Indicates that the byte array should be encoded using Base16 (hex) |
| 135 | if the stream is converted to JSON. |
| 136 | \value EncodedCbor Indicates that the byte array contains a CBOR stream. |
| 137 | \value Url Indicates that the string contains a URL. |
| 138 | \value Base64url Indicates that the string contains data encoded using Base64url. |
| 139 | \value Base64 Indicates that the string contains data encoded using Base64. |
| 140 | \value RegularExpression Indicates that the string contains a Perl-Compatible Regular |
| 141 | Expression pattern. |
| 142 | \value MimeMessage Indicates that the string contains a MIME message (according to |
| 143 | \l{RFC 2045}). |
| 144 | \value Uuid Indicates that the byte array contains a UUID. |
| 145 | \value COSE_Encrypt An \c Encrypt map as specified by \l{RFC 8152} |
| 146 | (CBOR Object Signing and Encryption). |
| 147 | \value COSE_Mac A \c Mac map as specified by \l{RFC 8152} |
| 148 | (CBOR Object Signing and Encryption). |
| 149 | \value COSE_Sign A \c Sign map as specified by \l{RFC 8152} |
| 150 | (CBOR Object Signing and Encryption). |
| 151 | \value Signature No change in interpretation; this tag can be used as the outermost |
| 152 | tag in a CBOR stream as the file header. |
| 153 | |
| 154 | The following tags are interpreted by QCborValue during decoding and will |
| 155 | produce objects with extended Qt types, and it will use those tags when |
| 156 | encoding the same extended types. |
| 157 | |
| 158 | \value DateTimeString \l QDateTime |
| 159 | \value UnixTime_t \l QDateTime (only in decoding) |
| 160 | \value Url \l QUrl |
| 161 | \value Uuid \l QUuid |
| 162 | |
| 163 | Additionally, if a QCborValue containing a QByteArray is tagged using one of |
| 164 | \c ExpectedBase64url, \c ExpectedBase64 or \c ExpectedBase16, QCborValue |
| 165 | will use the expected encoding when converting to JSON (see |
| 166 | QCborValue::toJsonValue). |
| 167 | |
| 168 | \sa QCborTag, QCborStreamWriter::append(QCborTag), |
| 169 | QCborStreamReader::isTag(), QCborStreamReader::toTag(), |
| 170 | QCborValue::isTag(), QCborValue::tag() |
| 171 | */ |
| 172 | |
| 173 | /*! |
| 174 | \class QCborError |
| 175 | \inmodule QtCore |
| 176 | \inheaderfile QtCborCommon |
| 177 | \reentrant |
| 178 | \since 5.12 |
| 179 | |
| 180 | \brief The QCborError class holds the error condition found while parsing or |
| 181 | validating a CBOR stream. |
| 182 | |
| 183 | \sa QCborStreamReader, QCborValue, QCborParserError, |
| 184 | {Parsing and displaying CBOR data}, {Serialization Converter}, |
| 185 | {Saving and Loading a Game} |
| 186 | */ |
| 187 | |
| 188 | /*! |
| 189 | \enum QCborError::Code |
| 190 | |
| 191 | This enum contains the possible error condition codes. |
| 192 | |
| 193 | \value NoError No error was detected. |
| 194 | \value UnknownError An unknown error occurred and no further details are available. |
| 195 | \value AdvancePastEnd QCborStreamReader::next() was called but there are no more elements in |
| 196 | the current context. |
| 197 | \value InputOutputError An I/O error with the QIODevice occurred. |
| 198 | \value GarbageAtEnd Data was found in the input stream after the last element. |
| 199 | \value EndOfFile The end of the input stream was unexpectedly reached while processing an |
| 200 | element. |
| 201 | \value UnexpectedBreak The CBOR stream contains a Break where it is not allowed (data is |
| 202 | corrupt and the error is not recoverable). |
| 203 | \value UnknownType The CBOR stream contains an unknown/unparsable Type (data is corrupt |
| 204 | and the error is not recoverable). |
| 205 | \value IllegalType The CBOR stream contains a known type in a position it is not allowed |
| 206 | to exist (data is corrupt and the error is not recoverable). |
| 207 | \value IllegalNumber The CBOR stream appears to be encoding a number larger than 64-bit |
| 208 | (data is corrupt and the error is not recoverable). |
| 209 | \value IllegalSimpleType The CBOR stream contains a Simple Type encoded incorrectly (data is |
| 210 | corrupt and the error is not recoverable). |
| 211 | \value InvalidUtf8String The CBOR stream contains a text string that does not decode properly |
| 212 | as UTF-8 (data is corrupt and the error is not recoverable). |
| 213 | \value DataTooLarge CBOR string, map or array is too big and cannot be parsed by Qt |
| 214 | (internal limitation, but the error is not recoverable). |
| 215 | \value NestingTooDeep Too many levels of arrays or maps encountered while processing the |
| 216 | input (internal limitation, but the error is not recoverable). |
| 217 | \value UnsupportedType The CBOR stream contains a known type that the implementation does not |
| 218 | support (internal limitation, but the error is not recoverable). |
| 219 | */ |
| 220 | |
| 221 | /*! |
| 222 | \variable QCborError::c |
| 223 | \internal |
| 224 | */ |
| 225 | |
| 226 | /*! |
| 227 | \fn QCborError::operator Code() const |
| 228 | |
| 229 | Returns the error code that this QCborError object stores. |
| 230 | */ |
| 231 | |
| 232 | /*! |
| 233 | Returns a text string that matches the error code in this QCborError object. |
| 234 | |
| 235 | Note: the string is not translated. Applications whose interface allow users |
| 236 | to parse CBOR streams need to provide their own, translated strings. |
| 237 | |
| 238 | \sa QCborError::Code |
| 239 | */ |
| 240 | QString QCborError::toString() const |
| 241 | { |
| 242 | switch (c) { |
| 243 | case NoError: |
| 244 | static_assert(int(NoError) == int(CborNoError)); |
| 245 | return QString(); |
| 246 | |
| 247 | case UnknownError: |
| 248 | static_assert(int(UnknownError) == int(CborUnknownError)); |
| 249 | return QStringLiteral("Unknown error" ); |
| 250 | case AdvancePastEnd: |
| 251 | static_assert(int(AdvancePastEnd) == int(CborErrorAdvancePastEOF)); |
| 252 | return QStringLiteral("Read past end of buffer (more bytes needed)" ); |
| 253 | case InputOutputError: |
| 254 | static_assert(int(InputOutputError) == int(CborErrorIO)); |
| 255 | return QStringLiteral("Input/Output error" ); |
| 256 | case GarbageAtEnd: |
| 257 | static_assert(int(GarbageAtEnd) == int(CborErrorGarbageAtEnd)); |
| 258 | return QStringLiteral("Data found after the end of the stream" ); |
| 259 | case EndOfFile: |
| 260 | static_assert(int(EndOfFile) == int(CborErrorUnexpectedEOF)); |
| 261 | return QStringLiteral("Unexpected end of input data (more bytes needed)" ); |
| 262 | case UnexpectedBreak: |
| 263 | static_assert(int(UnexpectedBreak) == int(CborErrorUnexpectedBreak)); |
| 264 | return QStringLiteral("Invalid CBOR stream: unexpected 'break' byte" ); |
| 265 | case UnknownType: |
| 266 | static_assert(int(UnknownType) == int(CborErrorUnknownType)); |
| 267 | return QStringLiteral("Invalid CBOR stream: unknown type" ); |
| 268 | case IllegalType: |
| 269 | static_assert(int(IllegalType) == int(CborErrorIllegalType)); |
| 270 | return QStringLiteral("Invalid CBOR stream: illegal type found" ); |
| 271 | case IllegalNumber: |
| 272 | static_assert(int(IllegalNumber) == int(CborErrorIllegalNumber)); |
| 273 | return QStringLiteral("Invalid CBOR stream: illegal number encoding (future extension)" ); |
| 274 | case IllegalSimpleType: |
| 275 | static_assert(int(IllegalSimpleType) == int(CborErrorIllegalSimpleType)); |
| 276 | return QStringLiteral("Invalid CBOR stream: illegal simple type" ); |
| 277 | case InvalidUtf8String: |
| 278 | static_assert(int(InvalidUtf8String) == int(CborErrorInvalidUtf8TextString)); |
| 279 | return QStringLiteral("Invalid CBOR stream: invalid UTF-8 text string" ); |
| 280 | case DataTooLarge: |
| 281 | static_assert(int(DataTooLarge) == int(CborErrorDataTooLarge)); |
| 282 | return QStringLiteral("Internal limitation: data set too large" ); |
| 283 | case NestingTooDeep: |
| 284 | static_assert(int(NestingTooDeep) == int(CborErrorNestingTooDeep)); |
| 285 | return QStringLiteral("Internal limitation: data nesting too deep" ); |
| 286 | case UnsupportedType: |
| 287 | static_assert(int(UnsupportedType) == int(CborErrorUnsupportedType)); |
| 288 | return QStringLiteral("Internal limitation: unsupported type" ); |
| 289 | } |
| 290 | |
| 291 | // get the error string from TinyCBOR |
| 292 | CborError err = CborError(int(c)); |
| 293 | return QString::fromLatin1(ba: cbor_error_string(error: err)); |
| 294 | } |
| 295 | |
| 296 | QT_END_NAMESPACE |
| 297 | |
| 298 | #ifndef QT_BOOTSTRAPPED |
| 299 | #include "moc_qcborcommon.cpp" |
| 300 | #endif |
| 301 | |