Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-12 08:34:40

0001 // Copyright (C) 2022 The Qt Company Ltd.
0002 // Copyright (C) 2016 Intel Corporation.
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 // Qt-Security score:critical reason:data-parser
0005 
0006 #ifndef QBYTEARRAY_H
0007 #define QBYTEARRAY_H
0008 
0009 #include <QtCore/qrefcount.h>
0010 #include <QtCore/qnamespace.h>
0011 #include <QtCore/qarraydata.h>
0012 #include <QtCore/qarraydatapointer.h>
0013 #include <QtCore/qcompare.h>
0014 #include <QtCore/qcontainerfwd.h>
0015 #include <QtCore/qbytearrayalgorithms.h>
0016 #include <QtCore/qbytearrayview.h>
0017 
0018 #include <stdlib.h>
0019 #include <string.h>
0020 
0021 #include <string>
0022 #include <iterator>
0023 
0024 #ifndef QT5_NULL_STRINGS
0025 // Would ideally be off, but in practice breaks too much (Qt 6.0).
0026 #define QT5_NULL_STRINGS 1
0027 #endif
0028 
0029 #ifdef truncate
0030 #error qbytearray.h must be included before any header file that defines truncate
0031 #endif
0032 
0033 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0034 Q_FORWARD_DECLARE_CF_TYPE(CFData);
0035 Q_FORWARD_DECLARE_OBJC_CLASS(NSData);
0036 #endif
0037 
0038 #if defined(Q_OS_WASM) || defined(Q_QDOC)
0039 namespace emscripten {
0040     class val;
0041 }
0042 #endif
0043 
0044 class tst_QByteArray;
0045 
0046 QT_BEGIN_NAMESPACE
0047 
0048 class QString;
0049 class QDataStream;
0050 
0051 using QByteArrayData = QArrayDataPointer<char>;
0052 
0053 #  define QByteArrayLiteral(str) \
0054     (QByteArray(QByteArrayData(nullptr, const_cast<char *>(str), sizeof(str) - 1))) \
0055     /**/
0056 
0057 class Q_CORE_EXPORT QByteArray
0058 {
0059 public:
0060     using DataPointer = QByteArrayData;
0061 private:
0062     typedef QTypedArrayData<char> Data;
0063 
0064     DataPointer d;
0065     static const char _empty;
0066 
0067     friend class ::tst_QByteArray;
0068 
0069     template <typename InputIterator>
0070     using if_input_iterator = QtPrivate::IfIsInputIterator<InputIterator>;
0071 public:
0072     enum Base64Option {
0073         Base64Encoding = 0,
0074         Base64UrlEncoding = 1,
0075 
0076         KeepTrailingEquals = 0,
0077         OmitTrailingEquals = 2,
0078 
0079         IgnoreBase64DecodingErrors = 0,
0080         AbortOnBase64DecodingErrors = 4,
0081     };
0082     Q_DECLARE_FLAGS(Base64Options, Base64Option)
0083 
0084     enum class Base64DecodingStatus {
0085         Ok,
0086         IllegalInputLength,
0087         IllegalCharacter,
0088         IllegalPadding,
0089     };
0090 
0091     inline constexpr QByteArray() noexcept;
0092     QByteArray(const char *, qsizetype size = -1);
0093     QByteArray(qsizetype size, char c);
0094     QByteArray(qsizetype size, Qt::Initialization);
0095     explicit QByteArray(QByteArrayView v) : QByteArray(v.data(), v.size()) {}
0096     inline QByteArray(const QByteArray &) noexcept;
0097     inline ~QByteArray();
0098 
0099     QByteArray &operator=(const QByteArray &) noexcept;
0100     QByteArray &operator=(const char *str);
0101     inline QByteArray(QByteArray && other) noexcept
0102         = default;
0103     QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QByteArray)
0104     inline void swap(QByteArray &other) noexcept
0105     { d.swap(other.d); }
0106 
0107     constexpr bool isEmpty() const noexcept { return size() == 0; }
0108     void resize(qsizetype size);
0109     void resize(qsizetype size, char c);
0110     void resizeForOverwrite(qsizetype size);
0111 
0112     QByteArray &fill(char c, qsizetype size = -1);
0113 
0114     inline qsizetype capacity() const;
0115     inline void reserve(qsizetype size);
0116     inline void squeeze();
0117 
0118 #ifndef QT_NO_CAST_FROM_BYTEARRAY
0119     inline operator const char *() const;
0120     inline operator const void *() const;
0121 #endif
0122 
0123     // Some compilers consider this conversion ambiguous, so
0124     // we're not offering it there:
0125     // * QCC 8.3 on QNX
0126     // * GHS 2022.1.4 on INTEGRITY
0127 #if (!defined(Q_OS_QNX) || !defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY > 803) && \
0128     (!defined(Q_CC_GHS) || !defined(__GHS_VERSION_NUMBER) || __GHS_VERSION_NUMBER > 202214)
0129 # define QT_BYTEARRAY_CONVERTS_TO_STD_STRING_VIEW
0130     Q_IMPLICIT operator std::string_view() const noexcept
0131     { return std::string_view(data(), std::size_t(size())); }
0132 #endif
0133 
0134     inline char *data();
0135     inline const char *data() const noexcept;
0136     const char *constData() const noexcept { return data(); }
0137     inline void detach();
0138     inline bool isDetached() const;
0139     inline bool isSharedWith(const QByteArray &other) const noexcept
0140     { return data() == other.data() && size() == other.size(); }
0141     void clear();
0142 
0143     inline char at(qsizetype i) const;
0144     inline char operator[](qsizetype i) const;
0145     [[nodiscard]] inline char &operator[](qsizetype i);
0146     [[nodiscard]] char front() const { return at(0); }
0147     [[nodiscard]] inline char &front();
0148     [[nodiscard]] char back() const { return at(size() - 1); }
0149     [[nodiscard]] inline char &back();
0150 
0151     QT_CORE_INLINE_SINCE(6, 8)
0152     qsizetype indexOf(char c, qsizetype from = 0) const;
0153     qsizetype indexOf(QByteArrayView bv, qsizetype from = 0) const
0154     { return QtPrivate::findByteArray(qToByteArrayViewIgnoringNull(*this), from, bv); }
0155 
0156     QT_CORE_INLINE_SINCE(6, 8)
0157     qsizetype lastIndexOf(char c, qsizetype from = -1) const;
0158     qsizetype lastIndexOf(QByteArrayView bv) const
0159     { return lastIndexOf(bv, size()); }
0160     qsizetype lastIndexOf(QByteArrayView bv, qsizetype from) const
0161     { return QtPrivate::lastIndexOf(qToByteArrayViewIgnoringNull(*this), from, bv); }
0162 
0163     inline bool contains(char c) const;
0164     inline bool contains(QByteArrayView bv) const;
0165     qsizetype count(char c) const;
0166     qsizetype count(QByteArrayView bv) const
0167     { return QtPrivate::count(qToByteArrayViewIgnoringNull(*this), bv); }
0168 
0169     inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0170 
0171 #if QT_CORE_REMOVED_SINCE(6, 7)
0172     QByteArray left(qsizetype len) const;
0173     QByteArray right(qsizetype len) const;
0174     QByteArray mid(qsizetype index, qsizetype len = -1) const;
0175     QByteArray first(qsizetype n) const;
0176     QByteArray last(qsizetype n) const;
0177     QByteArray sliced(qsizetype pos) const;
0178     QByteArray sliced(qsizetype pos, qsizetype n) const;
0179     QByteArray chopped(qsizetype len) const;
0180 #else
0181     [[nodiscard]] QByteArray left(qsizetype n) const &
0182     {
0183         if (n >= size())
0184             return *this;
0185         return first(qMax(n, 0));
0186     }
0187     [[nodiscard]] QByteArray left(qsizetype n) &&
0188     {
0189         if (n >= size())
0190             return std::move(*this);
0191         return std::move(*this).first(qMax(n, 0));
0192     }
0193     [[nodiscard]] QByteArray right(qsizetype n) const &
0194     {
0195         if (n >= size())
0196             return *this;
0197         return last(qMax(n, 0));
0198     }
0199     [[nodiscard]] QByteArray right(qsizetype n) &&
0200     {
0201         if (n >= size())
0202             return std::move(*this);
0203         return std::move(*this).last(qMax(n, 0));
0204     }
0205     [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) const &;
0206     [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) &&;
0207 
0208     [[nodiscard]] QByteArray first(qsizetype n) const &
0209     { verify(0, n); return sliced(0, n); }
0210     [[nodiscard]] QByteArray last(qsizetype n) const &
0211     { verify(0, n); return sliced(size() - n, n); }
0212     [[nodiscard]] QByteArray sliced(qsizetype pos) const &
0213     { verify(pos, 0); return sliced(pos, size() - pos); }
0214     [[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) const &
0215     { verify(pos, n); return QByteArray(d.data() + pos, n); }
0216     [[nodiscard]] QByteArray chopped(qsizetype len) const &
0217     { verify(0, len); return sliced(0, size() - len); }
0218 
0219     [[nodiscard]] QByteArray first(qsizetype n) &&
0220     {
0221         verify(0, n);
0222         resize(n);      // may detach and allocate memory
0223         return std::move(*this);
0224     }
0225     [[nodiscard]] QByteArray last(qsizetype n) &&
0226     { verify(0, n); return sliced_helper(*this, size() - n, n); }
0227     [[nodiscard]] QByteArray sliced(qsizetype pos) &&
0228     { verify(pos, 0); return sliced_helper(*this, pos, size() - pos); }
0229     [[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) &&
0230     { verify(pos, n); return sliced_helper(*this, pos, n); }
0231     [[nodiscard]] QByteArray chopped(qsizetype len) &&
0232     { verify(0, len); return std::move(*this).first(size() - len); }
0233 #endif
0234 
0235     bool startsWith(QByteArrayView bv) const
0236     { return QtPrivate::startsWith(qToByteArrayViewIgnoringNull(*this), bv); }
0237     bool startsWith(char c) const { return size() > 0 && front() == c; }
0238 
0239     bool endsWith(char c) const { return size() > 0 && back() == c; }
0240     bool endsWith(QByteArrayView bv) const
0241     { return QtPrivate::endsWith(qToByteArrayViewIgnoringNull(*this), bv); }
0242 
0243     bool isUpper() const;
0244     bool isLower() const;
0245 
0246     [[nodiscard]] bool isValidUtf8() const noexcept
0247     {
0248         return QtPrivate::isValidUtf8(qToByteArrayViewIgnoringNull(*this));
0249     }
0250 
0251     void truncate(qsizetype pos);
0252     void chop(qsizetype n);
0253 
0254     QByteArray &slice(qsizetype pos)
0255     { verify(pos, 0); return remove(0, pos); }
0256     QByteArray &slice(qsizetype pos, qsizetype n)
0257     {
0258         verify(pos, n);
0259         if (isNull())
0260             return *this;
0261         resize(pos + n);
0262         return remove(0, pos);
0263     }
0264 
0265 #if !defined(Q_QDOC)
0266     [[nodiscard]] QByteArray toLower() const &
0267     { return toLower_helper(*this); }
0268     [[nodiscard]] QByteArray toLower() &&
0269     { return toLower_helper(*this); }
0270     [[nodiscard]] QByteArray toUpper() const &
0271     { return toUpper_helper(*this); }
0272     [[nodiscard]] QByteArray toUpper() &&
0273     { return toUpper_helper(*this); }
0274     [[nodiscard]] QByteArray trimmed() const &
0275     { return trimmed_helper(*this); }
0276     [[nodiscard]] QByteArray trimmed() &&
0277     { return trimmed_helper(*this); }
0278     [[nodiscard]] QByteArray simplified() const &
0279     { return simplified_helper(*this); }
0280     [[nodiscard]] QByteArray simplified() &&
0281     { return simplified_helper(*this); }
0282 #else
0283     [[nodiscard]] QByteArray toLower() const;
0284     [[nodiscard]] QByteArray toUpper() const;
0285     [[nodiscard]] QByteArray trimmed() const;
0286     [[nodiscard]] QByteArray simplified() const;
0287 #endif
0288 
0289     [[nodiscard]] QByteArray leftJustified(qsizetype width, char fill = ' ', bool truncate = false) const;
0290     [[nodiscard]] QByteArray rightJustified(qsizetype width, char fill = ' ', bool truncate = false) const;
0291 
0292     QByteArray &prepend(char c)
0293     { return insert(0, QByteArrayView(&c, 1)); }
0294     inline QByteArray &prepend(qsizetype count, char c);
0295     QByteArray &prepend(const char *s)
0296     { return insert(0, QByteArrayView(s, qsizetype(qstrlen(s)))); }
0297     QByteArray &prepend(const char *s, qsizetype len)
0298     { return insert(0, QByteArrayView(s, len)); }
0299     QByteArray &prepend(const QByteArray &a);
0300     QByteArray &prepend(QByteArrayView a)
0301     { return insert(0, a); }
0302 
0303     QByteArray &append(char c);
0304     inline QByteArray &append(qsizetype count, char c);
0305     QByteArray &append(const char *s)
0306     { return append(s, -1); }
0307     QByteArray &append(const char *s, qsizetype len)
0308     { return append(QByteArrayView(s, len < 0 ? qsizetype(qstrlen(s)) : len)); }
0309     QByteArray &append(const QByteArray &a);
0310     QByteArray &append(QByteArrayView a)
0311     { return insert(size(), a); }
0312 
0313     QByteArray &assign(QByteArrayView v);
0314     QByteArray &assign(qsizetype n, char c)
0315     {
0316         Q_ASSERT(n >= 0);
0317         return fill(c, n);
0318     }
0319     template <typename InputIterator, if_input_iterator<InputIterator> = true>
0320     QByteArray &assign(InputIterator first, InputIterator last)
0321     {
0322         if constexpr (std::is_same_v<InputIterator, iterator> || std::is_same_v<InputIterator, const_iterator>)
0323             return assign(QByteArrayView(first, last));
0324         d.assign(first, last);
0325         if (d.data())
0326             d.data()[d.size] = '\0';
0327         return *this;
0328     }
0329 
0330     QByteArray &insert(qsizetype i, QByteArrayView data);
0331     inline QByteArray &insert(qsizetype i, const char *s)
0332     { return insert(i, QByteArrayView(s)); }
0333     inline QByteArray &insert(qsizetype i, const QByteArray &data)
0334     { return insert(i, QByteArrayView(data)); }
0335     QByteArray &insert(qsizetype i, qsizetype count, char c);
0336     QByteArray &insert(qsizetype i, char c)
0337     { return insert(i, QByteArrayView(&c, 1)); }
0338     QByteArray &insert(qsizetype i, const char *s, qsizetype len)
0339     { return insert(i, QByteArrayView(s, len)); }
0340 
0341     QByteArray &remove(qsizetype index, qsizetype len);
0342     QByteArray &removeAt(qsizetype pos)
0343     { return size_t(pos) < size_t(size()) ? remove(pos, 1) : *this; }
0344     QByteArray &removeFirst() { return !isEmpty() ? remove(0, 1) : *this; }
0345     QByteArray &removeLast() { return !isEmpty() ? remove(size() - 1, 1) : *this; }
0346 
0347     template <typename Predicate>
0348     QByteArray &removeIf(Predicate pred)
0349     {
0350         removeIf_helper(pred);
0351         return *this;
0352     }
0353 
0354     QByteArray &replace(qsizetype index, qsizetype len, const char *s, qsizetype alen)
0355     { return replace(index, len, QByteArrayView(s, alen)); }
0356     QByteArray &replace(qsizetype index, qsizetype len, QByteArrayView s);
0357     QByteArray &replace(char before, QByteArrayView after)
0358     { return replace(QByteArrayView(&before, 1), after); }
0359     QByteArray &replace(const char *before, qsizetype bsize, const char *after, qsizetype asize)
0360     { return replace(QByteArrayView(before, bsize), QByteArrayView(after, asize)); }
0361     QByteArray &replace(QByteArrayView before, QByteArrayView after);
0362     QByteArray &replace(char before, char after);
0363 
0364     QByteArray &operator+=(char c)
0365     { return append(c); }
0366     QByteArray &operator+=(const char *s)
0367     { return append(s); }
0368     QByteArray &operator+=(const QByteArray &a)
0369     { return append(a); }
0370     QByteArray &operator+=(QByteArrayView a)
0371     { return append(a); }
0372 
0373     QList<QByteArray> split(char sep) const;
0374 
0375     [[nodiscard]] QByteArray repeated(qsizetype times) const;
0376 
0377 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0378 #if QT_CORE_REMOVED_SINCE(6, 8)
0379     QT_ASCII_CAST_WARN inline bool operator==(const QString &s2) const;
0380     QT_ASCII_CAST_WARN inline bool operator!=(const QString &s2) const;
0381     QT_ASCII_CAST_WARN inline bool operator<(const QString &s2) const;
0382     QT_ASCII_CAST_WARN inline bool operator>(const QString &s2) const;
0383     QT_ASCII_CAST_WARN inline bool operator<=(const QString &s2) const;
0384     QT_ASCII_CAST_WARN inline bool operator>=(const QString &s2) const;
0385 #endif // QT_CORE_REMOVED_SINCE(6, 8)
0386 #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0387 
0388     short toShort(bool *ok = nullptr, int base = 10) const;
0389     ushort toUShort(bool *ok = nullptr, int base = 10) const;
0390     int toInt(bool *ok = nullptr, int base = 10) const;
0391     uint toUInt(bool *ok = nullptr, int base = 10) const;
0392     long toLong(bool *ok = nullptr, int base = 10) const;
0393     ulong toULong(bool *ok = nullptr, int base = 10) const;
0394     qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
0395     qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
0396     float toFloat(bool *ok = nullptr) const;
0397     double toDouble(bool *ok = nullptr) const;
0398     QByteArray toBase64(Base64Options options = Base64Encoding) const;
0399     QByteArray toHex(char separator = '\0') const;
0400     QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
0401                                  const QByteArray &include = QByteArray(),
0402                                  char percent = '%') const;
0403     [[nodiscard]] QByteArray percentDecoded(char percent = '%') const;
0404 
0405     inline QByteArray &setNum(short, int base = 10);
0406     inline QByteArray &setNum(ushort, int base = 10);
0407     inline QByteArray &setNum(int, int base = 10);
0408     inline QByteArray &setNum(uint, int base = 10);
0409     inline QByteArray &setNum(long, int base = 10);
0410     inline QByteArray &setNum(ulong, int base = 10);
0411     QByteArray &setNum(qlonglong, int base = 10);
0412     QByteArray &setNum(qulonglong, int base = 10);
0413     inline QByteArray &setNum(float, char format = 'g', int precision = 6);
0414     QByteArray &setNum(double, char format = 'g', int precision = 6);
0415     QByteArray &setRawData(const char *a, qsizetype n);
0416 
0417     [[nodiscard]] static QByteArray number(int, int base = 10);
0418     [[nodiscard]] static QByteArray number(uint, int base = 10);
0419     [[nodiscard]] static QByteArray number(long, int base = 10);
0420     [[nodiscard]] static QByteArray number(ulong, int base = 10);
0421     [[nodiscard]] static QByteArray number(qlonglong, int base = 10);
0422     [[nodiscard]] static QByteArray number(qulonglong, int base = 10);
0423     [[nodiscard]] static QByteArray number(double, char format = 'g', int precision = 6);
0424     [[nodiscard]] static QByteArray fromRawData(const char *data, qsizetype size)
0425     {
0426         return QByteArray(DataPointer::fromRawData(data, size));
0427     }
0428 
0429     class FromBase64Result;
0430     [[nodiscard]] static FromBase64Result fromBase64Encoding(QByteArray &&base64, Base64Options options = Base64Encoding);
0431     [[nodiscard]] static FromBase64Result fromBase64Encoding(const QByteArray &base64, Base64Options options = Base64Encoding);
0432     [[nodiscard]] static QByteArray fromBase64(const QByteArray &base64, Base64Options options = Base64Encoding);
0433     [[nodiscard]] static QByteArray fromHex(const QByteArray &hexEncoded);
0434     [[nodiscard]] static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%');
0435 
0436 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0437     static QByteArray fromCFData(CFDataRef data);
0438     static QByteArray fromRawCFData(CFDataRef data);
0439     CFDataRef toCFData() const Q_DECL_CF_RETURNS_RETAINED;
0440     CFDataRef toRawCFData() const Q_DECL_CF_RETURNS_RETAINED;
0441     static QByteArray fromNSData(const NSData *data);
0442     static QByteArray fromRawNSData(const NSData *data);
0443     NSData *toNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
0444     NSData *toRawNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
0445 #endif
0446 
0447 #if defined(Q_OS_WASM) || defined(Q_QDOC)
0448     static QByteArray fromEcmaUint8Array(emscripten::val uint8array);
0449     emscripten::val toEcmaUint8Array();
0450 #endif
0451 
0452     typedef char *iterator;
0453     typedef const char *const_iterator;
0454     typedef iterator Iterator;
0455     typedef const_iterator ConstIterator;
0456     typedef std::reverse_iterator<iterator> reverse_iterator;
0457     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
0458     iterator begin() { return data(); }
0459     const_iterator begin() const noexcept { return d.data(); }
0460     const_iterator cbegin() const noexcept { return begin(); }
0461     const_iterator constBegin() const noexcept { return begin(); }
0462     iterator end() { return begin() + size(); }
0463     const_iterator end() const noexcept { return begin() + size(); }
0464     const_iterator cend() const noexcept { return end(); }
0465     const_iterator constEnd() const noexcept { return end(); }
0466     reverse_iterator rbegin() { return reverse_iterator(end()); }
0467     reverse_iterator rend() { return reverse_iterator(begin()); }
0468     const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
0469     const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
0470     const_reverse_iterator crbegin() const noexcept { return rbegin(); }
0471     const_reverse_iterator crend() const noexcept { return rend(); }
0472 
0473     // stl compatibility
0474     typedef qsizetype size_type;
0475     typedef qptrdiff difference_type;
0476     typedef const char & const_reference;
0477     typedef char & reference;
0478     typedef char *pointer;
0479     typedef const char *const_pointer;
0480     typedef char value_type;
0481     void push_back(char c)
0482     { append(c); }
0483     void push_back(const char *s)
0484     { append(s); }
0485     void push_back(const QByteArray &a)
0486     { append(a); }
0487     void push_back(QByteArrayView a)
0488     { append(a); }
0489     void push_front(char c)
0490     { prepend(c); }
0491     void push_front(const char *c)
0492     { prepend(c); }
0493     void push_front(const QByteArray &a)
0494     { prepend(a); }
0495     void push_front(QByteArrayView a)
0496     { prepend(a); }
0497     void shrink_to_fit() { squeeze(); }
0498     iterator erase(const_iterator first, const_iterator last);
0499     inline iterator erase(const_iterator it) { return erase(it, it + 1); }
0500     constexpr qsizetype max_size() const noexcept
0501     {
0502         return maxSize();
0503     }
0504 
0505     static QByteArray fromStdString(const std::string &s);
0506     std::string toStdString() const;
0507 
0508     static constexpr qsizetype maxSize() noexcept
0509     {
0510         // -1 to deal with the NUL terminator
0511         return Data::maxSize() - 1;
0512     }
0513     constexpr qsizetype size() const noexcept
0514     {
0515         constexpr size_t MaxSize = maxSize();
0516         Q_PRESUME(size_t(d.size) <= MaxSize);
0517         return d.size;
0518     }
0519 #if QT_DEPRECATED_SINCE(6, 4)
0520     QT_DEPRECATED_VERSION_X_6_4("Use size() or length() instead.")
0521     constexpr qsizetype count() const noexcept { return size(); }
0522 #endif
0523     constexpr qsizetype length() const noexcept { return size(); }
0524     QT_CORE_CONSTEXPR_INLINE_SINCE(6, 4)
0525     bool isNull() const noexcept;
0526 
0527     inline const DataPointer &data_ptr() const { return d; }
0528     inline DataPointer &data_ptr() { return d; }
0529 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
0530     explicit inline QByteArray(const DataPointer &dd) : d(dd) {}
0531 #endif
0532     explicit inline QByteArray(DataPointer &&dd) : d(std::move(dd)) {}
0533 
0534     [[nodiscard]] QByteArray nullTerminated() const &;
0535     [[nodiscard]] QByteArray nullTerminated() &&;
0536     QByteArray &nullTerminate();
0537 
0538 private:
0539     friend bool comparesEqual(const QByteArray &lhs, const QByteArrayView &rhs) noexcept
0540     { return QByteArrayView(lhs) == rhs; }
0541     friend Qt::strong_ordering
0542     compareThreeWay(const QByteArray &lhs, const QByteArrayView &rhs) noexcept
0543     {
0544         const int res = QtPrivate::compareMemory(QByteArrayView(lhs), rhs);
0545         return Qt::compareThreeWay(res, 0);
0546     }
0547     Q_DECLARE_STRONGLY_ORDERED(QByteArray)
0548     Q_DECLARE_STRONGLY_ORDERED(QByteArray, const char *)
0549 #if defined(__GLIBCXX__) && defined(__cpp_lib_three_way_comparison)
0550     // libstdc++ has a bug [0] when `operator const void *()` is preferred over
0551     // `operator<=>()` when calling std::less<> and other similar methods.
0552     // Fix it by explicitly providing relational operators in such case.
0553     // [0]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114153
0554     friend bool operator<(const QByteArray &lhs, const QByteArray &rhs) noexcept
0555     { return is_lt(compareThreeWay(lhs, rhs)); }
0556     friend bool operator<=(const QByteArray &lhs, const QByteArray &rhs) noexcept
0557     { return is_lteq(compareThreeWay(lhs, rhs)); }
0558     friend bool operator>(const QByteArray &lhs, const QByteArray &rhs) noexcept
0559     { return is_gt(compareThreeWay(lhs, rhs)); }
0560     friend bool operator>=(const QByteArray &lhs, const QByteArray &rhs) noexcept
0561     { return is_gteq(compareThreeWay(lhs, rhs)); }
0562 #endif // defined(__GLIBCXX__) && defined(__cpp_lib_three_way_comparison)
0563 
0564     // Check isEmpty() instead of isNull() for backwards compatibility.
0565     friend bool comparesEqual(const QByteArray &lhs, std::nullptr_t) noexcept
0566     { return lhs.isEmpty(); }
0567     friend Qt::strong_ordering compareThreeWay(const QByteArray &lhs, std::nullptr_t) noexcept
0568     { return lhs.isEmpty() ? Qt::strong_ordering::equivalent : Qt::strong_ordering::greater; }
0569     Q_DECLARE_STRONGLY_ORDERED(QByteArray, std::nullptr_t)
0570 
0571     // defined in qstring.cpp
0572     friend Q_CORE_EXPORT bool comparesEqual(const QByteArray &lhs, const QChar &rhs) noexcept;
0573     friend Q_CORE_EXPORT Qt::strong_ordering
0574     compareThreeWay(const QByteArray &lhs, const QChar &rhs) noexcept;
0575     friend Q_CORE_EXPORT bool comparesEqual(const QByteArray &lhs, char16_t rhs) noexcept;
0576     friend Q_CORE_EXPORT Qt::strong_ordering
0577     compareThreeWay(const QByteArray &lhs, char16_t rhs) noexcept;
0578 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0579     Q_DECLARE_STRONGLY_ORDERED(QByteArray, QChar, QT_ASCII_CAST_WARN)
0580     Q_DECLARE_STRONGLY_ORDERED(QByteArray, char16_t, QT_ASCII_CAST_WARN)
0581 #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0582 
0583 
0584     void reallocData(qsizetype alloc, QArrayData::AllocationOption option);
0585     void reallocGrowData(qsizetype n);
0586     void expand(qsizetype i);
0587 
0588     Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
0589                                           [[maybe_unused]] qsizetype n = 1) const
0590     {
0591         Q_ASSERT(pos >= 0);
0592         Q_ASSERT(pos <= d.size);
0593         Q_ASSERT(n >= 0);
0594         Q_ASSERT(n <= d.size - pos);
0595     }
0596 
0597     static QByteArray sliced_helper(QByteArray &a, qsizetype pos, qsizetype n);
0598     static QByteArray toLower_helper(const QByteArray &a);
0599     static QByteArray toLower_helper(QByteArray &a);
0600     static QByteArray toUpper_helper(const QByteArray &a);
0601     static QByteArray toUpper_helper(QByteArray &a);
0602     static QByteArray trimmed_helper(const QByteArray &a);
0603     static QByteArray trimmed_helper(QByteArray &a);
0604     static QByteArray simplified_helper(const QByteArray &a);
0605     static QByteArray simplified_helper(QByteArray &a);
0606     template <typename Predicate>
0607     qsizetype removeIf_helper(Predicate pred)
0608     {
0609         const qsizetype result = d->eraseIf(pred);
0610         if (result > 0)
0611             d.data()[d.size] = '\0';
0612         return result;
0613     }
0614 
0615     friend class QString;
0616     friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, qsizetype nbytes);
0617 
0618     template <typename T> friend qsizetype erase(QByteArray &ba, const T &t);
0619     template <typename Predicate> friend qsizetype erase_if(QByteArray &ba, Predicate pred);
0620 };
0621 
0622 Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options)
0623 
0624 inline constexpr QByteArray::QByteArray() noexcept {}
0625 inline QByteArray::~QByteArray() {}
0626 
0627 inline char QByteArray::at(qsizetype i) const
0628 { verify(i, 1); return d.data()[i]; }
0629 inline char QByteArray::operator[](qsizetype i) const
0630 { verify(i, 1); return d.data()[i]; }
0631 
0632 #ifndef QT_NO_CAST_FROM_BYTEARRAY
0633 inline QByteArray::operator const char *() const
0634 { return data(); }
0635 inline QByteArray::operator const void *() const
0636 { return data(); }
0637 #endif
0638 inline char *QByteArray::data()
0639 {
0640     detach();
0641     Q_ASSERT(d.data());
0642     return d.data();
0643 }
0644 inline const char *QByteArray::data() const noexcept
0645 {
0646 #if QT5_NULL_STRINGS == 1
0647     return d.data() ? d.data() : &_empty;
0648 #else
0649     return d.data();
0650 #endif
0651 }
0652 inline void QByteArray::detach()
0653 { if (d.needsDetach()) reallocData(size(), QArrayData::KeepSize); }
0654 inline bool QByteArray::isDetached() const
0655 { return !d.isShared(); }
0656 inline QByteArray::QByteArray(const QByteArray &a) noexcept : d(a.d)
0657 {}
0658 
0659 inline qsizetype QByteArray::capacity() const { return qsizetype(d.constAllocatedCapacity()); }
0660 
0661 inline void QByteArray::reserve(qsizetype asize)
0662 {
0663     if (d.needsDetach() || asize > capacity() - d.freeSpaceAtBegin())
0664         reallocData(qMax(size(), asize), QArrayData::KeepSize);
0665     if (d.constAllocatedCapacity())
0666         d.setFlag(Data::CapacityReserved);
0667 }
0668 
0669 inline void QByteArray::squeeze()
0670 {
0671     if (!d.isMutable())
0672         return;
0673     if (d.needsDetach() || size() < capacity())
0674         reallocData(size(), QArrayData::KeepSize);
0675     if (d.constAllocatedCapacity())
0676         d.clearFlag(Data::CapacityReserved);
0677 }
0678 
0679 inline char &QByteArray::operator[](qsizetype i)
0680 { verify(i, 1); return data()[i]; }
0681 inline char &QByteArray::front() { return operator[](0); }
0682 inline char &QByteArray::back() { return operator[](size() - 1); }
0683 inline QByteArray &QByteArray::append(qsizetype n, char ch)
0684 { return insert(size(), n, ch); }
0685 inline QByteArray &QByteArray::prepend(qsizetype n, char ch)
0686 { return insert(0, n, ch); }
0687 inline bool QByteArray::contains(char c) const
0688 { return indexOf(c) != -1; }
0689 inline bool QByteArray::contains(QByteArrayView bv) const
0690 { return indexOf(bv) != -1; }
0691 inline int QByteArray::compare(QByteArrayView a, Qt::CaseSensitivity cs) const noexcept
0692 {
0693     return cs == Qt::CaseSensitive ? QtPrivate::compareMemory(*this, a) :
0694                                      qstrnicmp(data(), size(), a.data(), a.size());
0695 }
0696 #if !defined(QT_USE_QSTRINGBUILDER)
0697 inline QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
0698 { return QByteArray(a1) += a2; }
0699 inline QByteArray operator+(QByteArray &&lhs, const QByteArray &rhs)
0700 { return std::move(lhs += rhs); }
0701 inline QByteArray operator+(const QByteArray &a1, const char *a2)
0702 { return QByteArray(a1) += a2; }
0703 inline QByteArray operator+(QByteArray &&lhs, const char *rhs)
0704 { return std::move(lhs += rhs); }
0705 inline QByteArray operator+(const QByteArray &a1, char a2)
0706 { return QByteArray(a1) += a2; }
0707 inline QByteArray operator+(QByteArray &&lhs, char rhs)
0708 { return std::move(lhs += rhs); }
0709 inline QByteArray operator+(const char *a1, const QByteArray &a2)
0710 { return QByteArray(a1) += a2; }
0711 inline QByteArray operator+(char a1, const QByteArray &a2)
0712 { return QByteArray(&a1, 1) += a2; }
0713 Q_WEAK_OVERLOAD
0714 inline QByteArray operator+(const QByteArray &lhs, QByteArrayView rhs)
0715 {
0716     QByteArray tmp{lhs.size() + rhs.size(), Qt::Uninitialized};
0717     return tmp.assign(lhs).append(rhs);
0718 }
0719 Q_WEAK_OVERLOAD
0720 inline QByteArray operator+(QByteArrayView lhs, const QByteArray &rhs)
0721 {
0722     QByteArray tmp{lhs.size() + rhs.size(), Qt::Uninitialized};
0723     return tmp.assign(lhs).append(rhs);
0724 }
0725 #endif // QT_USE_QSTRINGBUILDER
0726 
0727 inline QByteArray &QByteArray::setNum(short n, int base)
0728 { return setNum(qlonglong(n), base); }
0729 inline QByteArray &QByteArray::setNum(ushort n, int base)
0730 { return setNum(qulonglong(n), base); }
0731 inline QByteArray &QByteArray::setNum(int n, int base)
0732 { return setNum(qlonglong(n), base); }
0733 inline QByteArray &QByteArray::setNum(uint n, int base)
0734 { return setNum(qulonglong(n), base); }
0735 inline QByteArray &QByteArray::setNum(long n, int base)
0736 { return setNum(qlonglong(n), base); }
0737 inline QByteArray &QByteArray::setNum(ulong n, int base)
0738 { return setNum(qulonglong(n), base); }
0739 inline QByteArray &QByteArray::setNum(float n, char format, int precision)
0740 { return setNum(double(n), format, precision); }
0741 
0742 #if QT_CORE_INLINE_IMPL_SINCE(6, 4)
0743 QT_CORE_CONSTEXPR_INLINE_SINCE(6, 4)
0744 bool QByteArray::isNull() const noexcept
0745 {
0746     return d.isNull();
0747 }
0748 #endif
0749 #if QT_CORE_INLINE_IMPL_SINCE(6, 8)
0750 qsizetype QByteArray::indexOf(char ch, qsizetype from) const
0751 {
0752     return qToByteArrayViewIgnoringNull(*this).indexOf(ch, from);
0753 }
0754 qsizetype QByteArray::lastIndexOf(char ch, qsizetype from) const
0755 {
0756     return qToByteArrayViewIgnoringNull(*this).lastIndexOf(ch, from);
0757 }
0758 #endif
0759 
0760 #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
0761 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
0762 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &);
0763 #endif
0764 
0765 #ifndef QT_NO_COMPRESS
0766 Q_CORE_EXPORT QByteArray qCompress(const uchar* data, qsizetype nbytes, int compressionLevel = -1);
0767 Q_CORE_EXPORT QByteArray qUncompress(const uchar* data, qsizetype nbytes);
0768 inline QByteArray qCompress(const QByteArray& data, int compressionLevel = -1)
0769 { return qCompress(reinterpret_cast<const uchar *>(data.constData()), data.size(), compressionLevel); }
0770 inline QByteArray qUncompress(const QByteArray& data)
0771 { return qUncompress(reinterpret_cast<const uchar*>(data.constData()), data.size()); }
0772 #endif
0773 
0774 Q_DECLARE_SHARED(QByteArray)
0775 
0776 class QByteArray::FromBase64Result
0777 {
0778 public:
0779     QByteArray decoded;
0780     QByteArray::Base64DecodingStatus decodingStatus;
0781 
0782     void swap(QByteArray::FromBase64Result &other) noexcept
0783     {
0784         decoded.swap(other.decoded);
0785         std::swap(decodingStatus, other.decodingStatus);
0786     }
0787 
0788     explicit operator bool() const noexcept { return decodingStatus == QByteArray::Base64DecodingStatus::Ok; }
0789 
0790 #if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(Q_QDOC)
0791     QByteArray &operator*() & noexcept { return decoded; }
0792     const QByteArray &operator*() const & noexcept { return decoded; }
0793     QByteArray &&operator*() && noexcept { return std::move(decoded); }
0794 #else
0795     QByteArray &operator*() noexcept { return decoded; }
0796     const QByteArray &operator*() const noexcept { return decoded; }
0797 #endif
0798 
0799     friend inline bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
0800     {
0801         if (lhs.decodingStatus != rhs.decodingStatus)
0802             return false;
0803 
0804         if (lhs.decodingStatus == QByteArray::Base64DecodingStatus::Ok && lhs.decoded != rhs.decoded)
0805             return false;
0806 
0807         return true;
0808     }
0809 
0810     friend inline bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
0811     {
0812         return !(lhs == rhs);
0813     }
0814 };
0815 
0816 Q_DECLARE_SHARED(QByteArray::FromBase64Result)
0817 
0818 
0819 Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QByteArray::FromBase64Result &key, size_t seed = 0) noexcept;
0820 
0821 template <typename T>
0822 qsizetype erase(QByteArray &ba, const T &t)
0823 {
0824     return ba.removeIf_helper([&t](const auto &e) { return t == e; });
0825 }
0826 
0827 template <typename Predicate>
0828 qsizetype erase_if(QByteArray &ba, Predicate pred)
0829 {
0830     return ba.removeIf_helper(pred);
0831 }
0832 
0833 //
0834 // QByteArrayView members that require QByteArray:
0835 //
0836 QByteArray QByteArrayView::toByteArray() const
0837 {
0838     return QByteArray(*this);
0839 }
0840 
0841 namespace Qt {
0842 inline namespace Literals {
0843 inline namespace StringLiterals {
0844 
0845 inline QByteArray operator""_ba(const char *str, size_t size) noexcept
0846 {
0847     return QByteArray(QByteArrayData(nullptr, const_cast<char *>(str), qsizetype(size)));
0848 }
0849 
0850 } // StringLiterals
0851 } // Literals
0852 } // Qt
0853 
0854 inline namespace QtLiterals {
0855 #if QT_DEPRECATED_SINCE(6, 8)
0856 
0857 QT_DEPRECATED_VERSION_X_6_8("Use _ba from Qt::StringLiterals namespace instead.")
0858 inline QByteArray operator""_qba(const char *str, size_t size) noexcept
0859 {
0860     return Qt::StringLiterals::operator""_ba(str, size);
0861 }
0862 
0863 #endif // QT_DEPRECATED_SINCE(6, 8)
0864 } // QtLiterals
0865 
0866 QT_END_NAMESPACE
0867 
0868 #endif // QBYTEARRAY_H