Skip to content

Commit

Permalink
Utilities: Fix decompression of firmware downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Oct 29, 2024
1 parent 8202e60 commit 10a71fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
40 changes: 26 additions & 14 deletions src/Utilities/Compression/QGCZlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

QGC_LOGGING_CATEGORY(QGCZlibLog, "qgc.compression.qgczlib")

namespace QGCZlib {
namespace QGCZlib
{

bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressedFilename)
{
Expand All @@ -35,7 +36,8 @@ bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressed

QFile outputFile(decompressedFilename);
if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qCWarning(QGCZlibLog) << "open input file failed" << outputFile.fileName() << outputFile.errorString();
qCWarning(QGCZlibLog) << "open output file failed" << outputFile.fileName() << outputFile.errorString();
inputFile.close();
return false;
}

Expand All @@ -48,7 +50,9 @@ bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressed
ret = inflateInit2(&strm, 16 + MAX_WBITS);
if (ret != Z_OK) {
qCWarning(QGCZlibLog) << "inflateInit2 failed:" << ret;
goto Error;
inputFile.close();
outputFile.close();
return false;
}

do {
Expand All @@ -63,28 +67,36 @@ bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressed
strm.next_out = outputBuffer;

ret = inflate(&strm, Z_NO_FLUSH);
if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
qCWarning(QGCZlibLog) << "inflate failed:" << ret;
goto Error;
inflateEnd(&strm);
inputFile.close();
outputFile.close();
return false;
}

const unsigned cBytesInflated = cBuffer - strm.avail_out;
const qint64 cBytesWritten = outputFile.write(reinterpret_cast<char*>(outputBuffer), static_cast<int>(cBytesInflated));
if (cBytesWritten != cBytesInflated) {
if (outputFile.write(reinterpret_cast<char*>(outputBuffer), cBytesInflated) != cBytesInflated) {
qCWarning(QGCZlibLog) << "output file write failed:" << outputFile.fileName() << outputFile.errorString();
goto Error;

inflateEnd(&strm);
inputFile.close();
outputFile.close();
return false;
}
} while (strm.avail_out == 0);

} while (ret != Z_STREAM_END);

Out:
inflateEnd(&strm);
return success;
inputFile.close();
outputFile.close();

Error:
success = false;
goto Out;
if (ret != Z_STREAM_END) {
qCWarning(QGCZlibLog) << "inflate did not reach stream end:" << ret;
return false;
}

return success;
}

} // namespace QGCZlib
6 changes: 4 additions & 2 deletions src/Utilities/Compression/QGCZlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

Q_DECLARE_LOGGING_CATEGORY(QGCZlibLog)

namespace QGCZlib {
namespace QGCZlib
{
/// Decompresses the specified file to the specified directory
/// @param gzippedFileName Fully qualified path to gzip file
/// @param decompressedFilename Fully qualified path to for file to decompress to
/// @return bool Success
bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressedFilename);
} // namespace QGCZlib
}

0 comments on commit 10a71fc

Please sign in to comment.