From cc741597021dd5e9c7ca2caffaf8a15181f68e89 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sat, 13 Jul 2013 12:04:40 +0200 Subject: Base64: Implement the "base64url" encoding and the stripping of '=' The "base64url" encoding is defined in RFC 4648, which is the newest version of Base64. There are also a few situations where the ending '=' is not desired. Change-Id: I9bb9fa55817b57d125189e4e795d6fde97caea6d Reviewed-by: Richard J. Moore --- src/corelib/tools/qbytearray.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/corelib/tools/qbytearray.h') diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 85d1f0ab90..ae8166db81 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -173,6 +173,15 @@ private: typedef QTypedArrayData Data; public: + enum Base64Option { + Base64Encoding = 0, + Base64UrlEncoding = 1, + + KeepTrailingEquals = 0, + OmitTrailingEquals = 2 + }; + Q_DECLARE_FLAGS(Base64Options, Base64Option) + inline QByteArray(); QByteArray(const char *, int size = -1); QByteArray(int size, char c); @@ -317,7 +326,8 @@ public: qulonglong toULongLong(bool *ok = 0, int base = 10) const; float toFloat(bool *ok = 0) const; double toDouble(bool *ok = 0) const; - QByteArray toBase64() const; + QByteArray toBase64(Base64Options options) const; + QByteArray toBase64() const; // ### Qt6 merge with previous QByteArray toHex() const; QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(), const QByteArray &include = QByteArray(), @@ -339,7 +349,8 @@ public: static QByteArray number(qulonglong, int base = 10); static QByteArray number(double, char f = 'g', int prec = 6); static QByteArray fromRawData(const char *, int size); - static QByteArray fromBase64(const QByteArray &base64); + static QByteArray fromBase64(const QByteArray &base64, Base64Options options); + static QByteArray fromBase64(const QByteArray &base64); // ### Qt6 merge with previous static QByteArray fromHex(const QByteArray &hexEncoded); static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%'); @@ -392,6 +403,8 @@ public: inline DataPtr &data_ptr() { return d; } }; +Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options) + inline QByteArray::QByteArray(): d(Data::sharedNull()) { } inline QByteArray::~QByteArray() { if (!d->ref.deref()) Data::deallocate(d); } inline int QByteArray::size() const -- cgit v1.2.3