mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2025-04-29 22:24:26 +02:00
fix: gzip file parsing as a stream
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
a39edb3b59
commit
d1c7107575
@ -139,64 +139,80 @@ bool GZip::zip(const QByteArray& uncompressedBytes, QByteArray& compressedBytes)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
GZipStream::GZipStream(const QString& filePath) : GZipStream(new QFile(filePath)) {}
|
int inf(QFile* source, std::function<bool(const QByteArray&)> handleBlock)
|
||||||
|
|
||||||
GZipStream::GZipStream(QFile* file) : m_file(file) {}
|
|
||||||
|
|
||||||
bool GZipStream::initStream()
|
|
||||||
{
|
{
|
||||||
memset(&m_strm, 0, sizeof(m_strm));
|
constexpr auto CHUNK = 16384;
|
||||||
return (inflateInit2(&m_strm, 16 + MAX_WBITS) == Z_OK);
|
int ret;
|
||||||
}
|
unsigned have;
|
||||||
|
z_stream strm;
|
||||||
|
memset(&strm, 0, sizeof(strm));
|
||||||
|
char in[CHUNK];
|
||||||
|
unsigned char out[CHUNK];
|
||||||
|
|
||||||
bool GZipStream::unzipBlockByBlock(QByteArray& uncompressedBytes)
|
ret = inflateInit2(&strm, (16 + MAX_WBITS));
|
||||||
{
|
if (ret != Z_OK)
|
||||||
uncompressedBytes.clear();
|
return ret;
|
||||||
if (!m_file->isOpen()) {
|
|
||||||
if (!m_file->open(QIODevice::ReadOnly)) {
|
/* decompress until deflate stream ends or end of file */
|
||||||
qWarning() << "Failed to open file:" << (m_file->fileName());
|
do {
|
||||||
return false;
|
strm.avail_in = source->read(in, CHUNK);
|
||||||
|
if (source->error()) {
|
||||||
|
(void)inflateEnd(&strm);
|
||||||
|
return Z_ERRNO;
|
||||||
}
|
}
|
||||||
}
|
if (strm.avail_in == 0)
|
||||||
|
break;
|
||||||
|
strm.next_in = reinterpret_cast<Bytef*>(in);
|
||||||
|
|
||||||
if (!m_strm.state && !initStream()) {
|
/* run inflate() on input until output buffer not full */
|
||||||
return false;
|
do {
|
||||||
}
|
strm.avail_out = CHUNK;
|
||||||
|
strm.next_out = out;
|
||||||
|
ret = inflate(&strm, Z_NO_FLUSH);
|
||||||
|
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
|
||||||
|
switch (ret) {
|
||||||
|
case Z_NEED_DICT:
|
||||||
|
ret = Z_DATA_ERROR; /* and fall through */
|
||||||
|
case Z_DATA_ERROR:
|
||||||
|
case Z_MEM_ERROR:
|
||||||
|
(void)inflateEnd(&strm);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
have = CHUNK - strm.avail_out;
|
||||||
|
if (!handleBlock(QByteArray(reinterpret_cast<const char*>(out), have))) {
|
||||||
|
(void)inflateEnd(&strm);
|
||||||
|
return Z_OK;
|
||||||
|
}
|
||||||
|
|
||||||
QByteArray compressedBlock;
|
} while (strm.avail_out == 0);
|
||||||
unsigned int blockSize = 4096;
|
|
||||||
|
|
||||||
compressedBlock = m_file->read(blockSize);
|
/* done when inflate() says it's done */
|
||||||
if (compressedBlock.isEmpty()) {
|
} while (ret != Z_STREAM_END);
|
||||||
return true; // End of file reached
|
|
||||||
}
|
|
||||||
|
|
||||||
bool done = processBlock(compressedBlock, uncompressedBytes);
|
/* clean up and return */
|
||||||
if (inflateEnd(&m_strm) != Z_OK || !done) {
|
(void)inflateEnd(&strm);
|
||||||
return false;
|
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
|
||||||
}
|
|
||||||
return done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GZipStream::processBlock(const QByteArray& compressedBlock, QByteArray& uncompressedBytes)
|
QString zerr(int ret)
|
||||||
{
|
{
|
||||||
m_strm.next_in = (Bytef*)compressedBlock.data();
|
switch (ret) {
|
||||||
m_strm.avail_in = compressedBlock.size();
|
case Z_ERRNO:
|
||||||
|
return QObject::tr("error handling file");
|
||||||
unsigned int uncompLength = uncompressedBytes.size();
|
case Z_STREAM_ERROR:
|
||||||
if (m_strm.total_out >= uncompLength) {
|
return QObject::tr("invalid compression level");
|
||||||
uncompressedBytes.resize(uncompLength * 2);
|
case Z_DATA_ERROR:
|
||||||
uncompLength *= 2;
|
return QObject::tr("invalid or incomplete deflate data");
|
||||||
|
case Z_MEM_ERROR:
|
||||||
|
return QObject::tr("out of memory");
|
||||||
|
case Z_VERSION_ERROR:
|
||||||
|
return QObject::tr("zlib version mismatch!");
|
||||||
}
|
}
|
||||||
|
return {};
|
||||||
m_strm.next_out = reinterpret_cast<Bytef*>(uncompressedBytes.data() + m_strm.total_out);
|
|
||||||
m_strm.avail_out = uncompLength - m_strm.total_out;
|
|
||||||
|
|
||||||
int err = inflate(&m_strm, Z_NO_FLUSH);
|
|
||||||
if (err != Z_OK && err != Z_STREAM_END) {
|
|
||||||
qWarning() << "Decompression failed with error code" << err;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString GZip::readGzFileByBlocks(QFile* source, std::function<bool(const QByteArray&)> handleBlock)
|
||||||
|
{
|
||||||
|
auto ret = inf(source, handleBlock);
|
||||||
|
return zerr(ret);
|
||||||
|
}
|
@ -1,28 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <zlib.h>
|
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
class GZip {
|
namespace GZip {
|
||||||
public:
|
|
||||||
static bool unzip(const QByteArray& compressedBytes, QByteArray& uncompressedBytes);
|
|
||||||
static bool zip(const QByteArray& uncompressedBytes, QByteArray& compressedBytes);
|
|
||||||
};
|
|
||||||
|
|
||||||
class GZipStream {
|
bool unzip(const QByteArray& compressedBytes, QByteArray& uncompressedBytes);
|
||||||
public:
|
bool zip(const QByteArray& uncompressedBytes, QByteArray& compressedBytes);
|
||||||
explicit GZipStream(const QString& filePath);
|
QString readGzFileByBlocks(QFile* source, std::function<bool(const QByteArray&)> handleBlock);
|
||||||
explicit GZipStream(QFile* file);
|
|
||||||
|
|
||||||
// Decompress the next block and return the decompressed data
|
} // namespace GZip
|
||||||
bool unzipBlockByBlock(QByteArray& uncompressedBytes);
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool initStream();
|
|
||||||
|
|
||||||
bool processBlock(const QByteArray& compressedBlock, QByteArray& uncompressedBytes);
|
|
||||||
|
|
||||||
private:
|
|
||||||
QFile* m_file;
|
|
||||||
z_stream m_strm;
|
|
||||||
};
|
|
||||||
|
@ -151,54 +151,13 @@ void OtherLogsPage::on_selectLogBox_currentIndexChanged(const int index)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ReadLineAbstract {
|
|
||||||
public:
|
|
||||||
ReadLineAbstract(QFile* file) : m_file(file)
|
|
||||||
{
|
|
||||||
if (file->fileName().endsWith(".gz"))
|
|
||||||
m_gz = new GZipStream(file);
|
|
||||||
}
|
|
||||||
~ReadLineAbstract() { delete m_gz; }
|
|
||||||
|
|
||||||
QString readLine()
|
|
||||||
{
|
|
||||||
if (!m_gz)
|
|
||||||
return QString::fromUtf8(m_file->readLine());
|
|
||||||
QString line;
|
|
||||||
for (;;) {
|
|
||||||
if (!m_decodedData.isEmpty()) {
|
|
||||||
int newlineIndex = m_decodedData.indexOf('\n');
|
|
||||||
if (newlineIndex != -1) {
|
|
||||||
line += QString::fromUtf8(m_decodedData).left(newlineIndex);
|
|
||||||
m_decodedData.remove(0, newlineIndex + 1);
|
|
||||||
return line;
|
|
||||||
}
|
|
||||||
|
|
||||||
line += QString::fromUtf8(m_decodedData);
|
|
||||||
m_decodedData.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_gz->unzipBlockByBlock(m_decodedData)) { // If error occurs during unzipping
|
|
||||||
m_decodedData.clear();
|
|
||||||
return QObject::tr("The content of the file(%1) could not be decoded.").arg(m_file->fileName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool done() { return m_gz ? m_decodedData.isEmpty() : m_file->atEnd(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
QFile* m_file;
|
|
||||||
GZipStream* m_gz = nullptr;
|
|
||||||
QByteArray m_decodedData;
|
|
||||||
};
|
|
||||||
|
|
||||||
void OtherLogsPage::on_btnReload_clicked()
|
void OtherLogsPage::on_btnReload_clicked()
|
||||||
{
|
{
|
||||||
if (m_currentFile.isEmpty()) {
|
if (m_currentFile.isEmpty()) {
|
||||||
setControlsEnabled(false);
|
setControlsEnabled(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QFile file(FS::PathCombine(m_path, m_currentFile));
|
QFile file(FS::PathCombine(m_path, m_currentFile));
|
||||||
if (!file.open(QFile::ReadOnly)) {
|
if (!file.open(QFile::ReadOnly)) {
|
||||||
setControlsEnabled(false);
|
setControlsEnabled(false);
|
||||||
@ -220,15 +179,9 @@ void OtherLogsPage::on_btnReload_clicked()
|
|||||||
showTooBig();
|
showTooBig();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
auto handleLine = [this](QString line) {
|
||||||
ReadLineAbstract stream(&file);
|
if (line.isEmpty())
|
||||||
|
return false;
|
||||||
// Try to determine a level for each line
|
|
||||||
ui->text->clear();
|
|
||||||
ui->text->setModel(nullptr);
|
|
||||||
m_model->clear();
|
|
||||||
auto line = stream.readLine();
|
|
||||||
while (!stream.done()) { // just read until the model is full or the file ended
|
|
||||||
if (line.back() == '\n')
|
if (line.back() == '\n')
|
||||||
line = line.remove(line.size() - 1, 1);
|
line = line.remove(line.size() - 1, 1);
|
||||||
MessageLevel::Enum level = MessageLevel::Unknown;
|
MessageLevel::Enum level = MessageLevel::Unknown;
|
||||||
@ -245,10 +198,40 @@ void OtherLogsPage::on_btnReload_clicked()
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_model->append(level, line);
|
m_model->append(level, line);
|
||||||
if (m_model->isOverFlow())
|
return m_model->isOverFlow();
|
||||||
break;
|
};
|
||||||
|
|
||||||
line = stream.readLine();
|
// Try to determine a level for each line
|
||||||
|
ui->text->clear();
|
||||||
|
ui->text->setModel(nullptr);
|
||||||
|
m_model->clear();
|
||||||
|
if (file.fileName().endsWith(".gz")) {
|
||||||
|
QString line;
|
||||||
|
auto error = GZip::readGzFileByBlocks(&file, [&line, handleLine](const QByteArray& d) {
|
||||||
|
auto block = d;
|
||||||
|
int newlineIndex = block.indexOf('\n');
|
||||||
|
while (newlineIndex != -1) {
|
||||||
|
line += QString::fromUtf8(block).left(newlineIndex);
|
||||||
|
block.remove(0, newlineIndex + 1);
|
||||||
|
if (handleLine(line)) {
|
||||||
|
line.clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
line.clear();
|
||||||
|
newlineIndex = block.indexOf('\n');
|
||||||
|
}
|
||||||
|
line += QString::fromUtf8(block);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
if (!error.isEmpty()) {
|
||||||
|
setPlainText(tr("The file (%1) encountered an error when reading: %2.").arg(file.fileName(), error));
|
||||||
|
return;
|
||||||
|
} else if (!line.isEmpty()) {
|
||||||
|
handleLine(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (!file.atEnd() && !handleLine(QString::fromUtf8(file.readLine()))) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ui->text->setModel(m_proxy);
|
ui->text->setModel(m_proxy);
|
||||||
ui->text->scrollToBottom();
|
ui->text->scrollToBottom();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user