Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qdataurl.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qplatformdefs.h"
5#include "qurl.h"
6#include "private/qdataurl_p.h"
7
9
10using namespace Qt::Literals;
11
18Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray &payload)
19{
20 if (uri.scheme() != "data"_L1 || !uri.host().isEmpty())
21 return false;
22
23 mimeType = QStringLiteral("text/plain;charset=US-ASCII");
24
25 // the following would have been the correct thing, but
26 // reality often differs from the specification. People have
27 // data: URIs with ? and #
28 //QByteArray data = QByteArray::fromPercentEncoding(uri.path(QUrl::FullyEncoded).toLatin1());
30
31 // parse it:
32 const qsizetype pos = data.indexOf(',');
33 if (pos != -1) {
34 payload = data.mid(pos + 1);
36 data = data.trimmed();
37
38 // find out if the payload is encoded in Base64
40 payload = QByteArray::fromBase64(payload);
41 data.chop(7);
42 }
43
45 qsizetype i = 7; // strlen("charset")
46 while (data.at(i) == ' ')
47 ++i;
48 if (data.at(i) == '=')
49 data.prepend("text/plain;");
50 }
51
52 if (!data.isEmpty())
54
55 }
56
57 return true;
58}
59
\inmodule QtCore
Definition qbytearray.h:57
static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent='%')
void truncate(qsizetype pos)
Truncates the byte array at index position pos.
static QByteArray fromBase64(const QByteArray &base64, Base64Options options=Base64Encoding)
QByteArray mid(qsizetype index, qsizetype len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos.
bool endsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
bool startsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QByteArray toLatin1() const &
Definition qstring.h:559
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
\inmodule QtCore
Definition qurl.h:94
QString url(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2814
QString host(ComponentFormattingOptions=FullyDecoded) const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition qurl.cpp:2337
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1983
@ RemoveScheme
Definition qurl.h:105
@ FullyEncoded
Definition qurl.h:129
Combined button and popup list for selecting options.
@ CaseInsensitive
Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray &payload)
Definition qdataurl.cpp:18
const char * mimeType
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define QStringLiteral(str)
ptrdiff_t qsizetype
Definition qtypes.h:70