mirror of
https://bitbucket.org/smil3y/katie.git
synced 2025-02-24 02:42:55 +00:00
do not chunk source in qt_inflateGZipDataFrom() function
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
11695cf6dd
commit
bb1d544157
1 changed files with 38 additions and 66 deletions
|
@ -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,51 +77,34 @@ static QByteArray qt_inflateGZipDataFrom(QIODevice *device)
|
|||
return QByteArray();
|
||||
}
|
||||
|
||||
bool stillMoreWorkToDo = true;
|
||||
while (stillMoreWorkToDo) {
|
||||
zlibStream.avail_in = contents.size();
|
||||
zlibStream.next_in = reinterpret_cast<Bytef*>(source);
|
||||
|
||||
if (!zlibStream.avail_in) {
|
||||
qint64 readsize = device->read(source, QT_BUFFSIZE);
|
||||
do {
|
||||
// Prepare the destination buffer
|
||||
int oldSize = destination.size();
|
||||
destination.resize(oldSize + QT_BUFFSIZE);
|
||||
zlibStream.next_out = reinterpret_cast<Bytef*>(
|
||||
destination.data() + oldSize - zlibStream.avail_out);
|
||||
zlibStream.avail_out += QT_BUFFSIZE;
|
||||
|
||||
if (readsize <= 0)
|
||||
break;
|
||||
|
||||
zlibStream.avail_in = readsize;
|
||||
zlibStream.next_in = reinterpret_cast<Bytef*>(source);
|
||||
}
|
||||
|
||||
do {
|
||||
// Prepare the destination buffer
|
||||
int oldSize = destination.size();
|
||||
destination.resize(oldSize + QT_BUFFSIZE);
|
||||
zlibStream.next_out = reinterpret_cast<Bytef*>(
|
||||
destination.data() + oldSize - zlibStream.avail_out);
|
||||
zlibStream.avail_out += QT_BUFFSIZE;
|
||||
|
||||
zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
|
||||
switch (zlibResult) {
|
||||
case Z_NEED_DICT:
|
||||
case Z_DATA_ERROR:
|
||||
case Z_STREAM_ERROR:
|
||||
case Z_MEM_ERROR: {
|
||||
inflateEnd(&zlibStream);
|
||||
qWarning("Error while inflating gzip file: %s",
|
||||
(zlibStream.msg != NULL ? zlibStream.msg : "Unknown error"));
|
||||
destination.chop(zlibStream.avail_out);
|
||||
return destination;
|
||||
}
|
||||
int zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
|
||||
switch (zlibResult) {
|
||||
case Z_NEED_DICT:
|
||||
case Z_DATA_ERROR:
|
||||
case Z_STREAM_ERROR:
|
||||
case Z_MEM_ERROR: {
|
||||
inflateEnd(&zlibStream);
|
||||
qWarning("Error while inflating gzip file: %s",
|
||||
(zlibStream.msg != NULL ? zlibStream.msg : "Unknown error"));
|
||||
destination.chop(zlibStream.avail_out);
|
||||
return destination;
|
||||
}
|
||||
|
||||
// If the output buffer still has more room after calling inflate
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// If the output buffer still has more room after calling inflate
|
||||
// it means we have to provide more data, so exit the loop here
|
||||
} while (!zlibStream.avail_out);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue