| 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 | |
| 4 | #ifndef QCBORVALUE_P_H |
| 5 | #define QCBORVALUE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. |
| 12 | // This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include "qcborvalue.h" |
| 19 | |
| 20 | #if QT_CONFIG(cborstreamreader) |
| 21 | # include "qcborstreamreader.h" |
| 22 | #endif |
| 23 | |
| 24 | #include <private/qglobal_p.h> |
| 25 | #include <private/qstringconverter_p.h> |
| 26 | |
| 27 | #include <math.h> |
| 28 | |
| 29 | QT_BEGIN_NAMESPACE |
| 30 | |
| 31 | namespace QtCbor { |
| 32 | enum class Comparison { |
| 33 | ForEquality, |
| 34 | ForOrdering, |
| 35 | }; |
| 36 | |
| 37 | struct Undefined {}; |
| 38 | struct Element |
| 39 | { |
| 40 | enum ValueFlag : quint32 { |
| 41 | IsContainer = 0x0001, |
| 42 | HasByteData = 0x0002, |
| 43 | StringIsUtf16 = 0x0004, |
| 44 | StringIsAscii = 0x0008 |
| 45 | }; |
| 46 | Q_DECLARE_FLAGS(ValueFlags, ValueFlag) |
| 47 | |
| 48 | union { |
| 49 | qint64 value; |
| 50 | QCborContainerPrivate *container; |
| 51 | }; |
| 52 | QCborValue::Type type; |
| 53 | ValueFlags flags = {}; |
| 54 | |
| 55 | Element(qint64 v = 0, QCborValue::Type t = QCborValue::Undefined, ValueFlags f = {}) |
| 56 | : value(v), type(t), flags(f) |
| 57 | {} |
| 58 | |
| 59 | Element(QCborContainerPrivate *d, QCborValue::Type t, ValueFlags f = {}) |
| 60 | : container(d), type(t), flags(f | IsContainer) |
| 61 | {} |
| 62 | |
| 63 | double fpvalue() const |
| 64 | { |
| 65 | double d; |
| 66 | memcpy(dest: &d, src: &value, n: sizeof(d)); |
| 67 | return d; |
| 68 | } |
| 69 | }; |
| 70 | Q_DECLARE_OPERATORS_FOR_FLAGS(Element::ValueFlags) |
| 71 | static_assert(sizeof(Element) == 16); |
| 72 | |
| 73 | struct ByteData |
| 74 | { |
| 75 | QByteArray::size_type len; |
| 76 | |
| 77 | const char *byte() const { return reinterpret_cast<const char *>(this + 1); } |
| 78 | char *byte() { return reinterpret_cast<char *>(this + 1); } |
| 79 | const QChar *utf16() const { return reinterpret_cast<const QChar *>(this + 1); } |
| 80 | QChar *utf16() { return reinterpret_cast<QChar *>(this + 1); } |
| 81 | |
| 82 | QByteArray toByteArray() const { return QByteArray(byte(), len); } |
| 83 | QString toString() const { return QString(utf16(), len / 2); } |
| 84 | QString toUtf8String() const { return QString::fromUtf8(utf8: byte(), size: len); } |
| 85 | |
| 86 | QByteArray asByteArrayView() const { return QByteArray::fromRawData(data: byte(), size: len); } |
| 87 | QLatin1StringView asLatin1() const { return {byte(), len}; } |
| 88 | QUtf8StringView asUtf8StringView() const { return QUtf8StringView(byte(), len); } |
| 89 | QStringView asStringView() const{ return QStringView(utf16(), len / 2); } |
| 90 | QString asQStringRaw() const { return QString::fromRawData(utf16(), size: len / 2); } |
| 91 | }; |
| 92 | static_assert(std::is_trivial<ByteData>::value); |
| 93 | static_assert(std::is_standard_layout<ByteData>::value); |
| 94 | } // namespace QtCbor |
| 95 | |
| 96 | Q_DECLARE_TYPEINFO(QtCbor::Element, Q_PRIMITIVE_TYPE); |
| 97 | |
| 98 | class QCborContainerPrivate : public QSharedData |
| 99 | { |
| 100 | friend class QExplicitlySharedDataPointer<QCborContainerPrivate>; |
| 101 | ~QCborContainerPrivate(); |
| 102 | |
| 103 | public: |
| 104 | enum ContainerDisposition { CopyContainer, MoveContainer }; |
| 105 | |
| 106 | QByteArray::size_type usedData = 0; |
| 107 | QByteArray data; |
| 108 | QList<QtCbor::Element> elements; |
| 109 | |
| 110 | void deref() { if (!ref.deref()) delete this; } |
| 111 | void compact(); |
| 112 | static QCborContainerPrivate *clone(QCborContainerPrivate *d, qsizetype reserved = -1); |
| 113 | static QCborContainerPrivate *detach(QCborContainerPrivate *d, qsizetype reserved); |
| 114 | static QCborContainerPrivate *grow(QCborContainerPrivate *d, qsizetype index); |
| 115 | |
| 116 | static qptrdiff addByteDataImpl(QByteArray &target, QByteArray::size_type &targetUsed, |
| 117 | const char *block, qsizetype len) |
| 118 | { |
| 119 | // This function does not do overflow checking, since the len parameter |
| 120 | // is expected to be trusted. There's another version of this function |
| 121 | // in decodeStringFromCbor(), which checks. |
| 122 | |
| 123 | qptrdiff offset = target.size(); |
| 124 | |
| 125 | // align offset |
| 126 | offset += alignof(QtCbor::ByteData) - 1; |
| 127 | offset &= ~(alignof(QtCbor::ByteData) - 1); |
| 128 | |
| 129 | qptrdiff increment = qptrdiff(sizeof(QtCbor::ByteData)) + len; |
| 130 | |
| 131 | targetUsed += increment; |
| 132 | target.resize(size: offset + increment); |
| 133 | |
| 134 | char *ptr = target.begin() + offset; |
| 135 | auto b = new (ptr) QtCbor::ByteData; |
| 136 | b->len = len; |
| 137 | if (block) |
| 138 | memcpy(dest: b->byte(), src: block, n: len); |
| 139 | |
| 140 | return offset; |
| 141 | } |
| 142 | |
| 143 | qptrdiff addByteData(const char *block, qsizetype len) |
| 144 | { |
| 145 | return addByteDataImpl(target&: data, targetUsed&: usedData, block, len); |
| 146 | } |
| 147 | |
| 148 | const QtCbor::ByteData *byteData(QtCbor::Element e) const |
| 149 | { |
| 150 | if ((e.flags & QtCbor::Element::HasByteData) == 0) |
| 151 | return nullptr; |
| 152 | |
| 153 | size_t offset = size_t(e.value); |
| 154 | Q_ASSERT((offset % alignof(QtCbor::ByteData)) == 0); |
| 155 | Q_ASSERT(offset + sizeof(QtCbor::ByteData) <= size_t(data.size())); |
| 156 | |
| 157 | auto b = reinterpret_cast<const QtCbor::ByteData *>(data.constData() + offset); |
| 158 | Q_ASSERT(offset + sizeof(*b) + size_t(b->len) <= size_t(data.size())); |
| 159 | return b; |
| 160 | } |
| 161 | const QtCbor::ByteData *byteData(qsizetype idx) const |
| 162 | { |
| 163 | return byteData(e: elements.at(i: idx)); |
| 164 | } |
| 165 | |
| 166 | QCborContainerPrivate *containerAt(qsizetype idx, QCborValue::Type type) const |
| 167 | { |
| 168 | const QtCbor::Element &e = elements.at(i: idx); |
| 169 | if (e.type != type || (e.flags & QtCbor::Element::IsContainer) == 0) |
| 170 | return nullptr; |
| 171 | return e.container; |
| 172 | } |
| 173 | |
| 174 | void replaceAt_complex(QtCbor::Element &e, const QCborValue &value, ContainerDisposition disp); |
| 175 | void replaceAt_internal(QtCbor::Element &e, const QCborValue &value, ContainerDisposition disp) |
| 176 | { |
| 177 | if (value.container) |
| 178 | return replaceAt_complex(e, value, disp); |
| 179 | |
| 180 | e = { value.value_helper(), value.type() }; |
| 181 | if (value.isContainer()) |
| 182 | e.container = nullptr; |
| 183 | } |
| 184 | void replaceAt(qsizetype idx, const QCborValue &value, ContainerDisposition disp = CopyContainer) |
| 185 | { |
| 186 | QtCbor::Element &e = elements[idx]; |
| 187 | if (e.flags & QtCbor::Element::IsContainer) { |
| 188 | e.container->deref(); |
| 189 | e.container = nullptr; |
| 190 | e.flags = {}; |
| 191 | } else if (auto b = byteData(e)) { |
| 192 | usedData -= b->len + sizeof(QtCbor::ByteData); |
| 193 | } |
| 194 | replaceAt_internal(e, value, disp); |
| 195 | } |
| 196 | void insertAt(qsizetype idx, const QCborValue &value, ContainerDisposition disp = CopyContainer) |
| 197 | { |
| 198 | replaceAt_internal(e&: *elements.insert(i: idx, t: {}), value, disp); |
| 199 | } |
| 200 | |
| 201 | void append(QtCbor::Undefined) |
| 202 | { |
| 203 | elements.append(t: QtCbor::Element()); |
| 204 | } |
| 205 | void append(qint64 value) |
| 206 | { |
| 207 | elements.append(t: QtCbor::Element(value , QCborValue::Integer)); |
| 208 | } |
| 209 | void append(QCborTag tag) |
| 210 | { |
| 211 | elements.append(t: QtCbor::Element(qint64(tag), QCborValue::Tag)); |
| 212 | } |
| 213 | void appendByteData(const char *data, qsizetype len, QCborValue::Type type, |
| 214 | QtCbor::Element::ValueFlags = {}) |
| 215 | { |
| 216 | elements.append(t: QtCbor::Element(addByteData(block: data, len), type, |
| 217 | QtCbor::Element::HasByteData | extraFlags)); |
| 218 | } |
| 219 | void appendAsciiString(const QString &s); |
| 220 | void appendAsciiString(const char *str, qsizetype len) |
| 221 | { |
| 222 | appendByteData(data: str, len, type: QCborValue::String, extraFlags: QtCbor::Element::StringIsAscii); |
| 223 | } |
| 224 | void appendUtf8String(const char *str, qsizetype len) |
| 225 | { |
| 226 | appendByteData(data: str, len, type: QCborValue::String); |
| 227 | } |
| 228 | void append(QLatin1StringView s) |
| 229 | { |
| 230 | if (!QtPrivate::isAscii(s)) |
| 231 | return appendNonAsciiString(s: QString(s)); |
| 232 | |
| 233 | // US-ASCII is a subset of UTF-8, so we can keep in 8-bit |
| 234 | appendByteData(data: s.latin1(), len: s.size(), type: QCborValue::String, |
| 235 | extraFlags: QtCbor::Element::StringIsAscii); |
| 236 | } |
| 237 | void appendAsciiString(QStringView s); |
| 238 | void appendNonAsciiString(QStringView s); |
| 239 | |
| 240 | void append(const QString &s) |
| 241 | { |
| 242 | append(s: qToStringViewIgnoringNull(s)); |
| 243 | } |
| 244 | |
| 245 | void append(QStringView s) |
| 246 | { |
| 247 | if (QtPrivate::isAscii(s)) |
| 248 | appendAsciiString(s); |
| 249 | else |
| 250 | appendNonAsciiString(s); |
| 251 | } |
| 252 | void append(const QCborValue &v) |
| 253 | { |
| 254 | insertAt(idx: elements.size(), value: v); |
| 255 | } |
| 256 | |
| 257 | QByteArray byteArrayAt(qsizetype idx) const |
| 258 | { |
| 259 | const auto &e = elements.at(i: idx); |
| 260 | const auto data = byteData(e); |
| 261 | if (!data) |
| 262 | return QByteArray(); |
| 263 | return data->toByteArray(); |
| 264 | } |
| 265 | QString stringAt(qsizetype idx) const |
| 266 | { |
| 267 | const auto &e = elements.at(i: idx); |
| 268 | const auto data = byteData(e); |
| 269 | if (!data) |
| 270 | return QString(); |
| 271 | if (e.flags & QtCbor::Element::StringIsUtf16) |
| 272 | return data->toString(); |
| 273 | if (e.flags & QtCbor::Element::StringIsAscii) |
| 274 | return data->asLatin1(); |
| 275 | return data->toUtf8String(); |
| 276 | } |
| 277 | |
| 278 | static void resetValue(QCborValue &v) |
| 279 | { |
| 280 | v.container = nullptr; |
| 281 | } |
| 282 | |
| 283 | static QCborValue makeValue(QCborValue::Type type, qint64 n, QCborContainerPrivate *d = nullptr, |
| 284 | ContainerDisposition disp = CopyContainer) |
| 285 | { |
| 286 | QCborValue result(type); |
| 287 | result.n = n; |
| 288 | result.container = d; |
| 289 | if (d && disp == CopyContainer) |
| 290 | d->ref.ref(); |
| 291 | return result; |
| 292 | } |
| 293 | |
| 294 | QCborValue valueAt(qsizetype idx) const |
| 295 | { |
| 296 | const auto &e = elements.at(i: idx); |
| 297 | |
| 298 | if (e.flags & QtCbor::Element::IsContainer) { |
| 299 | if (e.type == QCborValue::Tag && e.container->elements.size() != 2) { |
| 300 | // invalid tags can be created due to incomplete parsing |
| 301 | return makeValue(type: QCborValue::Invalid, n: 0, d: nullptr); |
| 302 | } |
| 303 | return makeValue(type: e.type, n: -1, d: e.container); |
| 304 | } else if (e.flags & QtCbor::Element::HasByteData) { |
| 305 | return makeValue(type: e.type, n: idx, d: const_cast<QCborContainerPrivate *>(this)); |
| 306 | } |
| 307 | return makeValue(type: e.type, n: e.value); |
| 308 | } |
| 309 | QCborValue (QtCbor::Element e); |
| 310 | QCborValue (qsizetype idx) |
| 311 | { |
| 312 | QtCbor::Element e; |
| 313 | qSwap(value1&: e, value2&: elements[idx]); |
| 314 | |
| 315 | if (e.flags & QtCbor::Element::IsContainer) { |
| 316 | if (e.type == QCborValue::Tag && e.container->elements.size() != 2) { |
| 317 | // invalid tags can be created due to incomplete parsing |
| 318 | e.container->deref(); |
| 319 | return makeValue(type: QCborValue::Invalid, n: 0, d: nullptr); |
| 320 | } |
| 321 | return makeValue(type: e.type, n: -1, d: e.container, disp: MoveContainer); |
| 322 | } else if (e.flags & QtCbor::Element::HasByteData) { |
| 323 | return extractAt_complex(e); |
| 324 | } |
| 325 | return makeValue(type: e.type, n: e.value); |
| 326 | } |
| 327 | |
| 328 | static QtCbor::Element elementFromValue(const QCborValue &value) |
| 329 | { |
| 330 | if (value.n >= 0 && value.container) |
| 331 | return value.container->elements.at(i: value.n); |
| 332 | |
| 333 | QtCbor::Element e; |
| 334 | e.value = value.n; |
| 335 | e.type = value.t; |
| 336 | if (value.container) { |
| 337 | e.container = value.container; |
| 338 | e.flags = QtCbor::Element::IsContainer; |
| 339 | } |
| 340 | return e; |
| 341 | } |
| 342 | |
| 343 | static int compareUtf8(const QtCbor::ByteData *b, QLatin1StringView s) |
| 344 | { |
| 345 | return QUtf8::compareUtf8(utf8: QByteArrayView(b->byte(), b->len), s); |
| 346 | } |
| 347 | |
| 348 | static int compareUtf8(const QtCbor::ByteData *b, QStringView s) |
| 349 | { |
| 350 | return QUtf8::compareUtf8(utf8: QByteArrayView(b->byte(), b->len), utf16: s); |
| 351 | } |
| 352 | |
| 353 | template<typename String> |
| 354 | int stringCompareElement(const QtCbor::Element &e, String s, QtCbor::Comparison mode) const |
| 355 | { |
| 356 | if (e.type != QCborValue::String) |
| 357 | return int(e.type) - int(QCborValue::String); |
| 358 | |
| 359 | const QtCbor::ByteData *b = byteData(e); |
| 360 | if (!b) |
| 361 | return s.isEmpty() ? 0 : -1; |
| 362 | |
| 363 | if (e.flags & QtCbor::Element::StringIsUtf16) { |
| 364 | if (mode == QtCbor::Comparison::ForEquality) |
| 365 | return b->asStringView() == s ? 0 : 1; |
| 366 | return b->asStringView().compare(s); |
| 367 | } |
| 368 | return compareUtf8(b, s); |
| 369 | } |
| 370 | |
| 371 | template<typename String> |
| 372 | bool stringEqualsElement(const QtCbor::Element &e, String s) const |
| 373 | { |
| 374 | return stringCompareElement(e, s, QtCbor::Comparison::ForEquality) == 0; |
| 375 | } |
| 376 | |
| 377 | template<typename String> |
| 378 | bool stringEqualsElement(qsizetype idx, String s) const |
| 379 | { |
| 380 | return stringEqualsElement(elements.at(i: idx), s); |
| 381 | } |
| 382 | |
| 383 | static int compareElement_helper(const QCborContainerPrivate *c1, QtCbor::Element e1, |
| 384 | const QCborContainerPrivate *c2, QtCbor::Element e2, |
| 385 | QtCbor::Comparison mode) noexcept; |
| 386 | int compareElement(qsizetype idx, const QCborValue &value, QtCbor::Comparison mode) const |
| 387 | { |
| 388 | auto &e1 = elements.at(i: idx); |
| 389 | auto e2 = elementFromValue(value); |
| 390 | return compareElement_helper(c1: this, e1, c2: value.container, e2, mode); |
| 391 | } |
| 392 | |
| 393 | void removeAt(qsizetype idx) |
| 394 | { |
| 395 | replaceAt(idx, value: {}); |
| 396 | elements.remove(i: idx); |
| 397 | } |
| 398 | |
| 399 | // doesn't apply to JSON |
| 400 | template <typename KeyType> QCborValueConstRef findCborMapKey(KeyType key) |
| 401 | { |
| 402 | qsizetype i = 0; |
| 403 | for ( ; i < elements.size(); i += 2) { |
| 404 | const auto &e = elements.at(i); |
| 405 | bool equals; |
| 406 | if constexpr (std::is_same_v<std::decay_t<KeyType>, QCborValue>) { |
| 407 | equals = (compareElement(idx: i, value: key, mode: QtCbor::Comparison::ForEquality) == 0); |
| 408 | } else if constexpr (std::is_integral_v<KeyType>) { |
| 409 | equals = (e.type == QCborValue::Integer && e.value == key); |
| 410 | } else { |
| 411 | // assume it's a string |
| 412 | equals = stringEqualsElement(i, key); |
| 413 | } |
| 414 | if (equals) |
| 415 | break; |
| 416 | } |
| 417 | return { this, i + 1 }; |
| 418 | } |
| 419 | template <typename KeyType> static QCborValue findCborMapKey(const QCborValue &self, KeyType key) |
| 420 | { |
| 421 | if (self.isMap() && self.container) { |
| 422 | qsizetype idx = self.container->findCborMapKey(key).i; |
| 423 | if (idx < self.container->elements.size()) |
| 424 | return self.container->valueAt(idx); |
| 425 | } |
| 426 | return QCborValue(); |
| 427 | } |
| 428 | template <typename KeyType> static QCborValueRef |
| 429 | findOrAddMapKey(QCborContainerPrivate *container, KeyType key) |
| 430 | { |
| 431 | qsizetype size = 0; |
| 432 | qsizetype index = size + 1; |
| 433 | if (container) { |
| 434 | size = container->elements.size(); |
| 435 | index = container->findCborMapKey<KeyType>(key).i; // returns size + 1 if not found |
| 436 | } |
| 437 | Q_ASSERT(index & 1); |
| 438 | Q_ASSERT((size & 1) == 0); |
| 439 | |
| 440 | container = detach(d: container, reserved: qMax(a: index + 1, b: size)); |
| 441 | Q_ASSERT(container); |
| 442 | Q_ASSERT((container->elements.size() & 1) == 0); |
| 443 | |
| 444 | if (index >= size) { |
| 445 | container->append(key); |
| 446 | container->append(v: QCborValue()); |
| 447 | } |
| 448 | Q_ASSERT(index < container->elements.size()); |
| 449 | return { container, index }; |
| 450 | } |
| 451 | template <typename KeyType> static QCborValueRef findOrAddMapKey(QCborMap &map, KeyType key); |
| 452 | template <typename KeyType> static QCborValueRef findOrAddMapKey(QCborValue &self, KeyType key); |
| 453 | template <typename KeyType> static QCborValueRef findOrAddMapKey(QCborValueRef self, KeyType key); |
| 454 | |
| 455 | #if QT_CONFIG(cborstreamreader) |
| 456 | void decodeValueFromCbor(QCborStreamReader &reader, int remainingStackDepth); |
| 457 | void decodeStringFromCbor(QCborStreamReader &reader); |
| 458 | static inline void setErrorInReader(QCborStreamReader &reader, QCborError error); |
| 459 | #endif |
| 460 | }; |
| 461 | |
| 462 | QT_END_NAMESPACE |
| 463 | |
| 464 | #endif // QCBORVALUE_P_H |
| 465 | |