| 1 | // Copyright (C) 2022 Intel Corporation. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:critical reason:data-parser |
| 4 | |
| 5 | #ifndef QCBORARRAY_H |
| 6 | #define QCBORARRAY_H |
| 7 | |
| 8 | #include <QtCore/qcborvalue.h> |
| 9 | |
| 10 | #include <initializer_list> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | class QJsonArray; |
| 15 | class QDataStream; |
| 16 | |
| 17 | namespace QJsonPrivate { class Variant; } |
| 18 | |
| 19 | class QCborContainerPrivate; |
| 20 | class Q_CORE_EXPORT QCborArray |
| 21 | { |
| 22 | public: |
| 23 | class ConstIterator; |
| 24 | class Iterator { |
| 25 | QCborValueRef item {}; |
| 26 | friend class ConstIterator; |
| 27 | friend class QCborArray; |
| 28 | Iterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {} |
| 29 | public: |
| 30 | typedef std::random_access_iterator_tag iterator_category; |
| 31 | typedef qsizetype difference_type; |
| 32 | typedef QCborValue value_type; |
| 33 | typedef QCborValueRef reference; |
| 34 | typedef QCborValueRef *pointer; |
| 35 | |
| 36 | constexpr Iterator() = default; |
| 37 | constexpr Iterator(const Iterator &) = default; |
| 38 | Iterator &operator=(const Iterator &other) |
| 39 | { |
| 40 | // rebind the reference |
| 41 | item.d = other.item.d; |
| 42 | item.i = other.item.i; |
| 43 | return *this; |
| 44 | } |
| 45 | |
| 46 | QCborValueRef operator*() const { return item; } |
| 47 | QCborValueRef *operator->() { return &item; } |
| 48 | const QCborValueConstRef *operator->() const { return &item; } |
| 49 | QCborValueRef operator[](qsizetype j) const { return { item.d, item.i + j }; } |
| 50 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 51 | bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 52 | bool operator!=(const Iterator &o) const { return !operator==(o); } |
| 53 | bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 54 | bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 55 | bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 56 | bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 57 | bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 58 | bool operator!=(const ConstIterator &o) const { return !operator==(o); } |
| 59 | bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 60 | bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 61 | bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 62 | bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 63 | #endif |
| 64 | Iterator &operator++() { ++item.i; return *this; } |
| 65 | Iterator operator++(int) { Iterator n = *this; ++item.i; return n; } |
| 66 | Iterator &operator--() { item.i--; return *this; } |
| 67 | Iterator operator--(int) { Iterator n = *this; item.i--; return n; } |
| 68 | Iterator &operator+=(qsizetype j) { item.i += j; return *this; } |
| 69 | Iterator &operator-=(qsizetype j) { item.i -= j; return *this; } |
| 70 | Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + j }); } |
| 71 | Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - j }); } |
| 72 | qsizetype operator-(Iterator j) const { return item.i - j.item.i; } |
| 73 | private: |
| 74 | // Helper functions |
| 75 | static bool comparesEqual_helper(const Iterator &lhs, const Iterator &rhs) noexcept |
| 76 | { |
| 77 | return lhs.item.d == rhs.item.d && lhs.item.i == rhs.item.i; |
| 78 | } |
| 79 | |
| 80 | static bool comparesEqual_helper(const Iterator &lhs, const ConstIterator &rhs) noexcept |
| 81 | { |
| 82 | return lhs.item.d == rhs.item.d && lhs.item.i == rhs.item.i; |
| 83 | } |
| 84 | |
| 85 | static Qt::strong_ordering compareThreeWay_helper(const Iterator &lhs, |
| 86 | const Iterator &rhs) noexcept |
| 87 | { |
| 88 | Q_ASSERT(lhs.item.d == rhs.item.d); |
| 89 | return Qt::compareThreeWay(lhs: lhs.item.i, rhs: rhs.item.i); |
| 90 | } |
| 91 | |
| 92 | static Qt::strong_ordering compareThreeWay_helper(const Iterator &lhs, |
| 93 | const ConstIterator &rhs) noexcept |
| 94 | { |
| 95 | Q_ASSERT(lhs.item.d == rhs.item.d); |
| 96 | return Qt::compareThreeWay(lhs: lhs.item.i, rhs: rhs.item.i); |
| 97 | } |
| 98 | |
| 99 | // Compare friends |
| 100 | friend bool comparesEqual(const Iterator &lhs, const Iterator &rhs) noexcept |
| 101 | { |
| 102 | return comparesEqual_helper(lhs, rhs); |
| 103 | } |
| 104 | friend Qt::strong_ordering compareThreeWay(const Iterator &lhs, |
| 105 | const Iterator &rhs) noexcept |
| 106 | { |
| 107 | return compareThreeWay_helper(lhs, rhs); |
| 108 | } |
| 109 | Q_DECLARE_STRONGLY_ORDERED(Iterator) |
| 110 | friend bool comparesEqual(const Iterator &lhs, const ConstIterator &rhs) noexcept |
| 111 | { |
| 112 | return comparesEqual_helper(lhs, rhs); |
| 113 | } |
| 114 | friend Qt::strong_ordering compareThreeWay(const Iterator &lhs, |
| 115 | const ConstIterator &rhs) noexcept |
| 116 | { |
| 117 | return compareThreeWay_helper(lhs, rhs); |
| 118 | } |
| 119 | Q_DECLARE_STRONGLY_ORDERED(Iterator, ConstIterator) |
| 120 | }; |
| 121 | |
| 122 | class ConstIterator { |
| 123 | QCborValueConstRef item; |
| 124 | friend class Iterator; |
| 125 | friend class QCborArray; |
| 126 | ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {} |
| 127 | public: |
| 128 | typedef std::random_access_iterator_tag iterator_category; |
| 129 | typedef qsizetype difference_type; |
| 130 | typedef const QCborValue value_type; |
| 131 | typedef const QCborValueRef reference; |
| 132 | typedef const QCborValueRef *pointer; |
| 133 | |
| 134 | constexpr ConstIterator() = default; |
| 135 | constexpr ConstIterator(const ConstIterator &) = default; |
| 136 | ConstIterator &operator=(const ConstIterator &other) |
| 137 | { |
| 138 | // rebind the reference |
| 139 | item.d = other.item.d; |
| 140 | item.i = other.item.i; |
| 141 | return *this; |
| 142 | } |
| 143 | |
| 144 | QCborValueConstRef operator*() const { return item; } |
| 145 | const QCborValueConstRef *operator->() const { return &item; } |
| 146 | QCborValueConstRef operator[](qsizetype j) const { return QCborValueRef{ item.d, item.i + j }; } |
| 147 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 148 | bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 149 | bool operator!=(const Iterator &o) const { return !operator==(o); } |
| 150 | bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 151 | bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 152 | bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 153 | bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 154 | bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 155 | bool operator!=(const ConstIterator &o) const { return !operator==(o); } |
| 156 | bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 157 | bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 158 | bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 159 | bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 160 | #endif |
| 161 | ConstIterator &operator++() { ++item.i; return *this; } |
| 162 | ConstIterator operator++(int) { ConstIterator n = *this; ++item.i; return n; } |
| 163 | ConstIterator &operator--() { item.i--; return *this; } |
| 164 | ConstIterator operator--(int) { ConstIterator n = *this; item.i--; return n; } |
| 165 | ConstIterator &operator+=(qsizetype j) { item.i += j; return *this; } |
| 166 | ConstIterator &operator-=(qsizetype j) { item.i -= j; return *this; } |
| 167 | ConstIterator operator+(qsizetype j) const { return ConstIterator({ item.d, item.i + j }); } |
| 168 | ConstIterator operator-(qsizetype j) const { return ConstIterator({ item.d, item.i - j }); } |
| 169 | qsizetype operator-(ConstIterator j) const { return item.i - j.item.i; } |
| 170 | private: |
| 171 | // Helper functions |
| 172 | static bool comparesEqual_helper(const ConstIterator &lhs, |
| 173 | const ConstIterator &rhs) noexcept |
| 174 | { |
| 175 | return lhs.item.d == rhs.item.d && lhs.item.i == rhs.item.i; |
| 176 | } |
| 177 | static Qt::strong_ordering compareThreeWay_helper(const ConstIterator &lhs, |
| 178 | const ConstIterator &rhs) noexcept |
| 179 | { |
| 180 | Q_ASSERT(lhs.item.d == rhs.item.d); |
| 181 | return Qt::compareThreeWay(lhs: lhs.item.i, rhs: rhs.item.i); |
| 182 | } |
| 183 | |
| 184 | // Compare friends |
| 185 | friend bool comparesEqual(const ConstIterator &lhs, const ConstIterator &rhs) noexcept |
| 186 | { |
| 187 | return comparesEqual_helper(lhs, rhs); |
| 188 | } |
| 189 | friend Qt::strong_ordering compareThreeWay(const ConstIterator &lhs, |
| 190 | const ConstIterator &rhs) noexcept |
| 191 | { |
| 192 | return compareThreeWay_helper(lhs, rhs); |
| 193 | } |
| 194 | Q_DECLARE_STRONGLY_ORDERED(ConstIterator) |
| 195 | }; |
| 196 | |
| 197 | typedef qsizetype size_type; |
| 198 | typedef QCborValue value_type; |
| 199 | typedef value_type *pointer; |
| 200 | typedef const value_type *const_pointer; |
| 201 | typedef QCborValue &reference; |
| 202 | typedef const QCborValue &const_reference; |
| 203 | typedef qsizetype difference_type; |
| 204 | |
| 205 | QCborArray() noexcept; |
| 206 | QCborArray(const QCborArray &other) noexcept; |
| 207 | QCborArray(QCborArray &&other) noexcept = default; |
| 208 | QCborArray &operator=(const QCborArray &other) noexcept; |
| 209 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QCborArray) |
| 210 | QCborArray(std::initializer_list<QCborValue> args) |
| 211 | : QCborArray() |
| 212 | { |
| 213 | detach(reserve: qsizetype(args.size())); |
| 214 | for (const QCborValue &v : args) |
| 215 | append(value: v); |
| 216 | } |
| 217 | ~QCborArray(); |
| 218 | |
| 219 | void swap(QCborArray &other) noexcept |
| 220 | { |
| 221 | d.swap(other&: other.d); |
| 222 | } |
| 223 | |
| 224 | QCborValue toCborValue() const { return *this; } |
| 225 | |
| 226 | qsizetype size() const noexcept; |
| 227 | bool isEmpty() const { return size() == 0; } |
| 228 | void clear(); |
| 229 | |
| 230 | QCborValue at(qsizetype i) const; |
| 231 | QCborValue first() const { return at(i: 0); } |
| 232 | QCborValue last() const { return at(i: size() - 1); } |
| 233 | const QCborValue operator[](qsizetype i) const { return at(i); } |
| 234 | QCborValueRef first() { Q_ASSERT(!isEmpty()); return begin()[0]; } |
| 235 | QCborValueRef last() { Q_ASSERT(!isEmpty()); return begin()[size() - 1]; } |
| 236 | QCborValueRef operator[](qsizetype i) |
| 237 | { |
| 238 | if (i >= size()) |
| 239 | insert(i, value: QCborValue()); |
| 240 | return begin()[i]; |
| 241 | } |
| 242 | |
| 243 | void insert(qsizetype i, const QCborValue &value); |
| 244 | void insert(qsizetype i, QCborValue &&value); |
| 245 | void prepend(const QCborValue &value) { insert(i: 0, value); } |
| 246 | void prepend(QCborValue &&value) { insert(i: 0, value: std::move(value)); } |
| 247 | void append(const QCborValue &value) { insert(i: -1, value); } |
| 248 | void append(QCborValue &&value) { insert(i: -1, value: std::move(value)); } |
| 249 | QCborValue (ConstIterator it) { return extract(it: Iterator{ it.item.d, it.item.i }); } |
| 250 | QCborValue (Iterator it); |
| 251 | void removeAt(qsizetype i); |
| 252 | QCborValue takeAt(qsizetype i) { Q_ASSERT(i < size()); return extract(it: begin() + i); } |
| 253 | void removeFirst() { removeAt(i: 0); } |
| 254 | void removeLast() { removeAt(i: size() - 1); } |
| 255 | QCborValue takeFirst() { return takeAt(i: 0); } |
| 256 | QCborValue takeLast() { return takeAt(i: size() - 1); } |
| 257 | |
| 258 | bool contains(const QCborValue &value) const; |
| 259 | |
| 260 | int compare(const QCborArray &other) const noexcept Q_DECL_PURE_FUNCTION; |
| 261 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 262 | bool operator==(const QCborArray &other) const noexcept |
| 263 | { return compare(other) == 0; } |
| 264 | bool operator!=(const QCborArray &other) const noexcept |
| 265 | { return !operator==(other); } |
| 266 | bool operator<(const QCborArray &other) const |
| 267 | { return compare(other) < 0; } |
| 268 | #endif |
| 269 | |
| 270 | typedef Iterator iterator; |
| 271 | typedef ConstIterator const_iterator; |
| 272 | iterator begin() { detach(); return iterator{d.data(), 0}; } |
| 273 | const_iterator constBegin() const { return const_iterator{d.data(), 0}; } |
| 274 | const_iterator begin() const { return constBegin(); } |
| 275 | const_iterator cbegin() const { return constBegin(); } |
| 276 | iterator end() { detach(); return iterator{d.data(), size()}; } |
| 277 | const_iterator constEnd() const { return const_iterator{d.data(), size()}; } |
| 278 | const_iterator end() const { return constEnd(); } |
| 279 | const_iterator cend() const { return constEnd(); } |
| 280 | iterator insert(iterator before, const QCborValue &value) |
| 281 | { insert(i: before.item.i, value); return iterator{d.data(), before.item.i}; } |
| 282 | iterator insert(const_iterator before, const QCborValue &value) |
| 283 | { insert(i: before.item.i, value); return iterator{d.data(), before.item.i}; } |
| 284 | iterator erase(iterator it) { removeAt(i: it.item.i); return iterator{d.data(), it.item.i}; } |
| 285 | iterator erase(const_iterator it) { removeAt(i: it.item.i); return iterator{d.data(), it.item.i}; } |
| 286 | |
| 287 | void push_back(const QCborValue &t) { append(value: t); } |
| 288 | void push_front(const QCborValue &t) { prepend(value: t); } |
| 289 | void pop_front() { removeFirst(); } |
| 290 | void pop_back() { removeLast(); } |
| 291 | bool empty() const { return isEmpty(); } |
| 292 | |
| 293 | // convenience |
| 294 | QCborArray operator+(const QCborValue &v) const |
| 295 | { QCborArray n = *this; n += v; return n; } |
| 296 | QCborArray &operator+=(const QCborValue &v) |
| 297 | { append(value: v); return *this; } |
| 298 | QCborArray &operator<<(const QCborValue &v) |
| 299 | { append(value: v); return *this; } |
| 300 | |
| 301 | static QCborArray fromStringList(const QStringList &list); |
| 302 | static QCborArray fromVariantList(const QVariantList &list); |
| 303 | static QCborArray fromJsonArray(const QJsonArray &array); |
| 304 | static QCborArray fromJsonArray(QJsonArray &&array) noexcept; |
| 305 | QVariantList toVariantList() const; |
| 306 | QJsonArray toJsonArray() const; |
| 307 | |
| 308 | private: |
| 309 | friend Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool |
| 310 | comparesEqual(const QCborArray &lhs, const QCborArray &rhs) noexcept; |
| 311 | friend Qt::strong_ordering compareThreeWay(const QCborArray &lhs, |
| 312 | const QCborArray &rhs) noexcept |
| 313 | { |
| 314 | int c = lhs.compare(other: rhs); |
| 315 | return Qt::compareThreeWay(lhs: c, rhs: 0); |
| 316 | } |
| 317 | Q_DECLARE_STRONGLY_ORDERED(QCborArray) |
| 318 | |
| 319 | static Q_DECL_PURE_FUNCTION bool |
| 320 | comparesEqual_helper(const QCborArray &lhs, const QCborValue &rhs) noexcept; |
| 321 | static Q_DECL_PURE_FUNCTION Qt::strong_ordering |
| 322 | compareThreeWay_helper(const QCborArray &lhs, const QCborValue &rhs) noexcept; |
| 323 | friend bool comparesEqual(const QCborArray &lhs, |
| 324 | const QCborValue &rhs) noexcept |
| 325 | { |
| 326 | return comparesEqual_helper(lhs, rhs); |
| 327 | } |
| 328 | friend Qt::strong_ordering compareThreeWay(const QCborArray &lhs, |
| 329 | const QCborValue &rhs) noexcept |
| 330 | { |
| 331 | return compareThreeWay_helper(lhs, rhs); |
| 332 | } |
| 333 | Q_DECLARE_STRONGLY_ORDERED(QCborArray, QCborValue) |
| 334 | |
| 335 | static Q_DECL_PURE_FUNCTION bool |
| 336 | comparesEqual_helper(const QCborArray &lhs, QCborValueConstRef rhs) noexcept; |
| 337 | static Q_DECL_PURE_FUNCTION Qt::strong_ordering |
| 338 | compareThreeWay_helper(const QCborArray &lhs, QCborValueConstRef rhs) noexcept; |
| 339 | friend bool comparesEqual(const QCborArray &lhs, |
| 340 | const QCborValueConstRef &rhs) noexcept |
| 341 | { |
| 342 | return comparesEqual_helper(lhs, rhs); |
| 343 | } |
| 344 | friend Qt::strong_ordering compareThreeWay(const QCborArray &lhs, |
| 345 | const QCborValueConstRef &rhs) noexcept |
| 346 | { |
| 347 | return compareThreeWay_helper(lhs, rhs); |
| 348 | } |
| 349 | Q_DECLARE_STRONGLY_ORDERED(QCborArray, QCborValueConstRef) |
| 350 | |
| 351 | void detach(qsizetype reserve = 0); |
| 352 | |
| 353 | friend QCborValue; |
| 354 | friend QCborValueRef; |
| 355 | friend class QJsonPrivate::Variant; |
| 356 | explicit QCborArray(QCborContainerPrivate &dd) noexcept; |
| 357 | QExplicitlySharedDataPointer<QCborContainerPrivate> d; |
| 358 | }; |
| 359 | |
| 360 | Q_DECLARE_SHARED(QCborArray) |
| 361 | |
| 362 | inline QCborValue::QCborValue(QCborArray &&a) |
| 363 | : n(-1), container(a.d.take()), t(Array) |
| 364 | { |
| 365 | } |
| 366 | |
| 367 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 368 | inline QCborArray QCborValueRef::toArray() const |
| 369 | { |
| 370 | return concrete().toArray(); |
| 371 | } |
| 372 | |
| 373 | inline QCborArray QCborValueRef::toArray(const QCborArray &a) const |
| 374 | { |
| 375 | return concrete().toArray(defaultValue: a); |
| 376 | } |
| 377 | #endif |
| 378 | |
| 379 | inline QCborArray QCborValueConstRef::toArray() const |
| 380 | { |
| 381 | return concrete().toArray(); |
| 382 | } |
| 383 | |
| 384 | inline QCborArray QCborValueConstRef::toArray(const QCborArray &a) const |
| 385 | { |
| 386 | return concrete().toArray(defaultValue: a); |
| 387 | } |
| 388 | |
| 389 | Q_CORE_EXPORT size_t qHash(const QCborArray &array, size_t seed = 0); |
| 390 | |
| 391 | #if !defined(QT_NO_DEBUG_STREAM) |
| 392 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborArray &a); |
| 393 | #endif |
| 394 | |
| 395 | #ifndef QT_NO_DATASTREAM |
| 396 | #if QT_CONFIG(cborstreamwriter) |
| 397 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborArray &); |
| 398 | #endif |
| 399 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborArray &); |
| 400 | #endif |
| 401 | |
| 402 | QT_END_NAMESPACE |
| 403 | |
| 404 | #endif // QCBORARRAY_H |
| 405 | |