summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qbytearray.h
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2019-06-23 15:12:57 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2019-12-05 21:04:50 +0100
commitb19220d17fa66de5ded41690ffff263ee2af5c63 (patch)
tree03e261196dc109fbcc48a8ffbe15945a5a9c167d /src/corelib/text/qbytearray.h
parentccef2c33b284f2fd0bc08d518398af58214b020f (diff)
QByteArray: add a strict mode to fromBase64
QByteArray::fromBase64 was liberal in its input, simply skipping over invalid characters. As a side-effect of this, it had no error reporting, meaning it could not be used to convert fromBase64 _and_ validate the input in one go. Add more option flags to make fromBase64 strictly validate its input. Since we want to know whether it has succeeded or not, and the existing fromBase64 overloads do not allow for that, introduce a new function that returns an optional-like datatype. While at it: base64 decoding can be done in-place; add an rvalue overload to enable this use case. [ChangeLog][QtCore][QByteArray] Added the new fromBase64Encoding function. [ChangeLog][QtCore][QByteArray] Added new flags to make fromBase64 / fromBase64Encoding strictly validate their input, instead of skipping over invalid characters. Change-Id: I99cd5f2230f3d62970b28b4cb102913301da6ccd Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qbytearray.h')
-rw-r--r--src/corelib/text/qbytearray.h60
1 files changed, 59 insertions, 1 deletions
diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h
index 7c571706d8..4fa17cf58b 100644
--- a/src/corelib/text/qbytearray.h
+++ b/src/corelib/text/qbytearray.h
@@ -164,10 +164,20 @@ public:
Base64UrlEncoding = 1,
KeepTrailingEquals = 0,
- OmitTrailingEquals = 2
+ OmitTrailingEquals = 2,
+
+ IgnoreBase64DecodingErrors = 0,
+ AbortOnBase64DecodingErrors = 4,
};
Q_DECLARE_FLAGS(Base64Options, Base64Option)
+ enum class Base64DecodingStatus {
+ Ok,
+ IllegalInputLength,
+ IllegalCharacter,
+ IllegalPadding,
+ };
+
inline QByteArray() noexcept;
QByteArray(const char *, int size = -1);
QByteArray(int size, char c);
@@ -379,6 +389,10 @@ public:
Q_REQUIRED_RESULT static QByteArray number(qulonglong, int base = 10);
Q_REQUIRED_RESULT static QByteArray number(double, char f = 'g', int prec = 6);
Q_REQUIRED_RESULT static QByteArray fromRawData(const char *, int size);
+
+ class FromBase64Result;
+ Q_REQUIRED_RESULT static FromBase64Result fromBase64Encoding(QByteArray &&base64, Base64Options options = Base64Encoding);
+ Q_REQUIRED_RESULT static FromBase64Result fromBase64Encoding(const QByteArray &base64, Base64Options options = Base64Encoding);
Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64, Base64Options options);
Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64); // ### Qt6 merge with previous
Q_REQUIRED_RESULT static QByteArray fromHex(const QByteArray &hexEncoded);
@@ -749,6 +763,50 @@ inline QByteArray qUncompress(const QByteArray& data)
Q_DECLARE_SHARED(QByteArray)
+class QByteArray::FromBase64Result
+{
+public:
+ QByteArray decoded;
+ QByteArray::Base64DecodingStatus decodingStatus;
+
+ void swap(QByteArray::FromBase64Result &other) noexcept
+ {
+ qSwap(decoded, other.decoded);
+ qSwap(decodingStatus, other.decodingStatus);
+ }
+
+ explicit operator bool() const noexcept { return decodingStatus == QByteArray::Base64DecodingStatus::Ok; }
+
+#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(Q_QDOC)
+ QByteArray &operator*() & noexcept { return decoded; }
+ const QByteArray &operator*() const & noexcept { return decoded; }
+ QByteArray &&operator*() && noexcept { return std::move(decoded); }
+#else
+ QByteArray &operator*() noexcept { return decoded; }
+ const QByteArray &operator*() const noexcept { return decoded; }
+#endif
+};
+
+Q_DECLARE_SHARED(QByteArray::FromBase64Result)
+
+inline bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
+{
+ if (lhs.decodingStatus != rhs.decodingStatus)
+ return false;
+
+ if (lhs.decodingStatus == QByteArray::Base64DecodingStatus::Ok && lhs.decoded != rhs.decoded)
+ return false;
+
+ return true;
+}
+
+inline bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
+{
+ return !operator==(lhs, rhs);
+}
+
+Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QByteArray::FromBase64Result &key, uint seed = 0) noexcept;
+
QT_END_NAMESPACE
#endif // QBYTEARRAY_H