Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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());
29 const QByteArray dataArray =
31 QByteArrayView data = dataArray;
32
33 // parse it:
34 const qsizetype pos = data.indexOf(',');
35 if (pos != -1) {
36 payload = data.mid(pos + 1).toByteArray();
38 data = data.trimmed();
39
40 // find out if the payload is encoded in Base64
41 constexpr auto base64 = ";base64"_L1;
43 payload = QByteArray::fromBase64(payload);
44 data.chop(base64.size());
45 }
46
47 QLatin1StringView textPlain;
48 constexpr auto charset = "charset"_L1;
50 qsizetype i = charset.size();
51 while (data.at(i) == ' ')
52 ++i;
53 if (data.at(i) == '=')
54 textPlain = "text/plain;"_L1;
55 }
56
57 if (!data.isEmpty())
58 mimeType = textPlain + QLatin1StringView(data.trimmed());
59 }
60
61 return true;
62}
63
qsizetype indexOf(QByteArrayView a, qsizetype from=0) const noexcept
\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 &
bool endsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr qsizetype size() const noexcept
bool startsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QByteArray toLatin1() const &
Definition qstring.h:630
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
\inmodule QtCore
Definition qurl.h:94
QString url(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2817
QString host(ComponentFormattingOptions=FullyDecoded) const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition qurl.cpp:2340
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1991
@ 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:165