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