Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-09 08:53:41

0001 // Copyright (C) 2021 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:critical reason:data-parser
0004 
0005 #ifndef QBYTEARRAYALGORITHMS_H
0006 #define QBYTEARRAYALGORITHMS_H
0007 
0008 #include <QtCore/qnamespace.h>
0009 
0010 #include <string.h>
0011 #include <stdarg.h>
0012 
0013 #if 0
0014 #pragma qt_class(QByteArrayAlgorithms)
0015 #endif
0016 
0017 QT_BEGIN_NAMESPACE
0018 
0019 class QByteArrayView;
0020 
0021 namespace QtPrivate {
0022 
0023 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0024 bool startsWith(QByteArrayView haystack, QByteArrayView needle) noexcept;
0025 
0026 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0027 bool endsWith(QByteArrayView haystack, QByteArrayView needle) noexcept;
0028 
0029 [[nodiscard]] inline // defined in qbytearrayview.h
0030 qsizetype findByteArray(QByteArrayView haystack, qsizetype from, char needle) noexcept;
0031 
0032 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0033 qsizetype findByteArray(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
0034 
0035 [[nodiscard]] inline // defined in qbytearrayview.h
0036 qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, uchar needle) noexcept;
0037 
0038 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0039 qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept;
0040 
0041 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
0042 qsizetype count(QByteArrayView haystack, QByteArrayView needle) noexcept;
0043 
0044 [[nodiscard]] Q_CORE_EXPORT int compareMemory(QByteArrayView lhs, QByteArrayView rhs);
0045 
0046 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept;
0047 
0048 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf8(QByteArrayView s) noexcept;
0049 
0050 template <typename T>
0051 class ParsedNumber
0052 {
0053     T m_value;
0054     quint32 m_error : 1;
0055     quint32 m_reserved : 31;
0056     void *m_reserved2 = nullptr;
0057 public:
0058     constexpr ParsedNumber() noexcept : m_value(), m_error(true), m_reserved(0) {}
0059     constexpr explicit ParsedNumber(T v) : m_value(v), m_error(false), m_reserved(0) {}
0060 
0061     // minimal optional-like API:
0062     explicit operator bool() const noexcept { return !m_error; }
0063     T &operator*() { Q_ASSERT(*this); return m_value; }
0064     const T &operator*() const { Q_ASSERT(*this); return m_value; }
0065     T *operator->() noexcept { return *this ? &m_value : nullptr; }
0066     const T *operator->() const noexcept { return *this ? &m_value : nullptr; }
0067     template <typename U> // not = T, as that'd allow calls that are incompatible with std::optional
0068     T value_or(U &&u) const { return *this ? m_value : T(std::forward<U>(u)); }
0069 };
0070 
0071 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<double> toDouble(QByteArrayView a) noexcept;
0072 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<float> toFloat(QByteArrayView a) noexcept;
0073 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qlonglong> toSignedInteger(QByteArrayView data, int base);
0074 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber<qulonglong> toUnsignedInteger(QByteArrayView data, int base);
0075 
0076 // QByteArrayView has incomplete type here, and we can't include qbytearrayview.h,
0077 // since it includes qbytearrayalgorithms.h. Use the ByteArrayView template type as
0078 // a workaround.
0079 template <typename T, typename ByteArrayView,
0080           typename = std::enable_if_t<std::is_same_v<ByteArrayView, QByteArrayView>>>
0081 static inline T toIntegral(ByteArrayView data, bool *ok, int base)
0082 {
0083     const auto val = [&] {
0084         if constexpr (std::is_unsigned_v<T>)
0085             return toUnsignedInteger(data, base);
0086         else
0087             return toSignedInteger(data, base);
0088     }();
0089     const bool failed = !val || T(*val) != *val;
0090     if (ok)
0091         *ok = !failed;
0092     if (failed)
0093         return 0;
0094     return T(*val);
0095 }
0096 
0097 } // namespace QtPrivate
0098 
0099 /*****************************************************************************
0100   Safe and portable C string functions; extensions to standard string.h
0101  *****************************************************************************/
0102 
0103 [[nodiscard]] Q_DECL_PURE_FUNCTION Q_CORE_EXPORT
0104 const void *qmemrchr(const void *s, int needle, size_t n) noexcept;
0105 Q_CORE_EXPORT char *qstrdup(const char *);
0106 
0107 inline size_t qstrlen(const char *str)
0108 {
0109     QT_WARNING_PUSH
0110 #if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 900 && Q_CC_GNU < 1000
0111     // spurious compiler warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91490#c6)
0112     // when Q_DECLARE_METATYPE_TEMPLATE_1ARG is used
0113     QT_WARNING_DISABLE_GCC("-Wstringop-overflow")
0114 #endif
0115     return str ? strlen(str) : 0;
0116     QT_WARNING_POP
0117 }
0118 
0119 inline size_t qstrnlen(const char *str, size_t maxlen)
0120 {
0121     if (!str)
0122         return 0;
0123     auto end = static_cast<const char *>(memchr(str, '\0', maxlen));
0124     return end ? end - str : maxlen;
0125 }
0126 
0127 // implemented in qbytearray.cpp
0128 Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
0129 Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, size_t len);
0130 
0131 Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
0132 
0133 inline int qstrncmp(const char *str1, const char *str2, size_t len)
0134 {
0135     return (str1 && str2) ? strncmp(str1, str2, len)
0136         : (str1 ? 1 : (str2 ? -1 : 0));
0137 }
0138 Q_CORE_EXPORT int qstricmp(const char *, const char *);
0139 Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len);
0140 Q_CORE_EXPORT int qstrnicmp(const char *, qsizetype, const char *, qsizetype = -1);
0141 
0142 #ifndef QT_NO_QSNPRINTF // use std::(v)snprintf() from <cstdio> instead
0143 #if QT_DEPRECATED_SINCE(6, 9)
0144 #define QSNPF_DEPR(vsn) \
0145     QT_DEPRECATED_VERSION_X_6_9("Use C++11 std::" #vsn "printf() instead, taking care to " \
0146                                 "ensure that you didn't rely on QString::asprintf() " \
0147                                 "idiosyncrasies that q" #vsn "printf might, but " \
0148                                 "std::" #vsn "printf() does not, support.")
0149 // implemented in qvsnprintf.cpp
0150 QSNPF_DEPR(vsn)
0151 Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
0152     Q_ATTRIBUTE_FORMAT_PRINTF(3, 0);
0153 QSNPF_DEPR(sn)
0154 Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...)
0155     Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
0156 #undef QSNPF_DEPR
0157 #endif // QT_DEPRECATED_SINCE(6, 9)
0158 #endif // QT_NO_QSNPRINTF
0159 
0160 // qChecksum: Internet checksum
0161 Q_CORE_EXPORT quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard = Qt::ChecksumIso3309);
0162 
0163 QT_END_NAMESPACE
0164 
0165 #endif // QBYTEARRAYALGORITHMS_H