do not chunk source in qt_inflateGZipDataFrom() function

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-02-02 17:09:25 +02:00
parent 11695cf6dd
commit bb1d544157

View file

@ -52,22 +52,13 @@ QSvgTinyDocument::~QSvgTinyDocument()
}
#ifdef QT_BUILD_INTERNAL
Q_AUTOTEST_EXPORT QByteArray qt_inflateGZipDataFrom(QIODevice *device)
Q_AUTOTEST_EXPORT QByteArray qt_inflateGZipDataFrom(const QByteArray &contents)
#else
static QByteArray qt_inflateGZipDataFrom(QIODevice *device)
static QByteArray qt_inflateGZipDataFrom(const QByteArray &contents)
#endif
{
Q_ASSERT(device);
if (!device->isOpen()) {
device->open(QIODevice::ReadOnly);
}
Q_ASSERT(device->isOpen() && device->isReadable());
int zlibResult = Z_OK;
QSTACKARRAY(char, source, QT_BUFFSIZE);
QSTACKARRAY(char, source, contents.size());
::memcpy(source, contents.constData(), contents.size() * sizeof(char));
QByteArray destination;
// Initialize zlib stream struct
@ -86,18 +77,8 @@ static QByteArray qt_inflateGZipDataFrom(QIODevice *device)
return QByteArray();
}
bool stillMoreWorkToDo = true;
while (stillMoreWorkToDo) {
if (!zlibStream.avail_in) {
qint64 readsize = device->read(source, QT_BUFFSIZE);
if (readsize <= 0)
break;
zlibStream.avail_in = readsize;
zlibStream.avail_in = contents.size();
zlibStream.next_in = reinterpret_cast<Bytef*>(source);
}
do {
// Prepare the destination buffer
@ -107,7 +88,7 @@ static QByteArray qt_inflateGZipDataFrom(QIODevice *device)
destination.data() + oldSize - zlibStream.avail_out);
zlibStream.avail_out += QT_BUFFSIZE;
zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
int zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
switch (zlibResult) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
@ -125,13 +106,6 @@ static QByteArray qt_inflateGZipDataFrom(QIODevice *device)
// it means we have to provide more data, so exit the loop here
} while (!zlibStream.avail_out);
if (zlibResult == Z_STREAM_END) {
// Make sure there are no more members to process before exiting
if (!(zlibStream.avail_in && inflateReset(&zlibStream) == Z_OK))
stillMoreWorkToDo = false;
}
}
// Chop off trailing space in the buffer
destination.chop(zlibStream.avail_out);
@ -142,7 +116,7 @@ static QByteArray qt_inflateGZipDataFrom(QIODevice *device)
QSvgTinyDocument * QSvgTinyDocument::load(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
if (Q_UNLIKELY(!file.open(QFile::ReadOnly))) {
qWarning("Cannot open file '%s', because: %s",
qPrintable(fileName), qPrintable(file.errorString()));
return nullptr;
@ -155,20 +129,18 @@ QSvgTinyDocument * QSvgTinyDocument::load(const QByteArray &contents)
{
// Check for gzip magic number and inflate if appropriate
if (contents.startsWith("\x1f\x8b")) {
QBuffer buffer;
buffer.setData(contents.constData(), contents.size());
return load(qt_inflateGZipDataFrom(&buffer));
return load(qt_inflateGZipDataFrom(contents));
}
QSvgHandler handler(contents);
QSvgTinyDocument *doc = nullptr;
if (handler.ok()) {
doc = handler.document();
doc->m_animationDuration = handler.animationDuration();
} else {
if (Q_UNLIKELY(!handler.ok())) {
qWarning("Cannot read SVG, because: %s (line %d)",
qPrintable(handler.errorString()), handler.lineNumber());
} else {
doc = handler.document();
doc->m_animationDuration = handler.animationDuration();
}
return doc;
}
@ -178,12 +150,12 @@ QSvgTinyDocument * QSvgTinyDocument::load(QXmlStreamReader *contents)
QSvgHandler handler(contents);
QSvgTinyDocument *doc = nullptr;
if (handler.ok()) {
doc = handler.document();
doc->m_animationDuration = handler.animationDuration();
} else {
if (Q_UNLIKELY(!handler.ok())) {
qWarning("Cannot read SVG, because: %s (line %d)",
qPrintable(handler.errorString()), handler.lineNumber());
} else {
doc = handler.document();
doc->m_animationDuration = handler.animationDuration();
}
return doc;
}