From 444d9990106623c0c11a8d2aa1ed9a9a2b575a61 Mon Sep 17 00:00:00 2001 From: ulmus-scott Date: Mon, 27 Dec 2021 23:36:12 -0500 Subject: [PATCH] create unziputil from mythcoreutil --- .../libmythbase/http/mythhttpencoding.cpp | 2 +- mythtv/libs/libmythbase/libmythbase.pro | 3 + mythtv/libs/libmythbase/mythcoreutil.cpp | 185 ----------------- mythtv/libs/libmythbase/mythcoreutil.h | 8 - .../test/test_unzip/test_unzip.cpp | 2 +- mythtv/libs/libmythbase/unziputil.cpp | 195 ++++++++++++++++++ mythtv/libs/libmythbase/unziputil.h | 17 ++ mythtv/libs/libmythmetadata/musicmetadata.cpp | 2 +- mythtv/libs/libmythupnp/httprequest.cpp | 2 +- .../mythbackend/backendhousekeeper.cpp | 2 +- mythtv/programs/mythbackend/httpconfig.cpp | 2 +- mythtv/programs/mythfrontend/themechooser.cpp | 1 + 12 files changed, 222 insertions(+), 199 deletions(-) create mode 100644 mythtv/libs/libmythbase/unziputil.cpp create mode 100644 mythtv/libs/libmythbase/unziputil.h diff --git a/mythtv/libs/libmythbase/http/mythhttpencoding.cpp b/mythtv/libs/libmythbase/http/mythhttpencoding.cpp index f225d9c0977..fc1709aed20 100644 --- a/mythtv/libs/libmythbase/http/mythhttpencoding.cpp +++ b/mythtv/libs/libmythbase/http/mythhttpencoding.cpp @@ -1,6 +1,6 @@ // MythTV #include "mythlogging.h" -#include "mythcoreutil.h" +#include "unziputil.h" #include "http/mythmimedatabase.h" #include "http/mythhttpdata.h" #include "http/mythhttpfile.h" diff --git a/mythtv/libs/libmythbase/libmythbase.pro b/mythtv/libs/libmythbase/libmythbase.pro index 887e122d9a6..20cd8cef760 100644 --- a/mythtv/libs/libmythbase/libmythbase.pro +++ b/mythtv/libs/libmythbase/libmythbase.pro @@ -40,6 +40,7 @@ HEADERS += cleanupguard.h portchecker.h HEADERS += mythsorthelper.h mythdbcheck.h HEADERS += mythpower.h HEADERS += configuration.h +HEADERS += unziputil.h SOURCES += mthread.cpp mthreadpool.cpp SOURCES += mythsocket.cpp @@ -66,6 +67,7 @@ SOURCES += mythsorthelper.cpp dbcheckcommon.cpp SOURCES += mythpower.cpp SOURCES += configuration.cpp SOURCES += mythversion.cpp +SOURCES += unziputil.cpp HEADERS += http/mythhttpcommon.h HEADERS += http/mythhttptypes.h @@ -174,6 +176,7 @@ inc.files += remotefile.h mythsystemlegacy.h mythtypes.h inc.files += threadedfilewriter.h mythsingledownload.h mythsession.h inc.files += mythsorthelper.h mythdbcheck.h inc.files += mythrandom.h +inc.files += unziputil.h # Allow both #include and #include inc2.path = $${PREFIX}/include/mythtv/libmythbase diff --git a/mythtv/libs/libmythbase/mythcoreutil.cpp b/mythtv/libs/libmythbase/mythcoreutil.cpp index 2f75275b785..5d1336b2097 100644 --- a/mythtv/libs/libmythbase/mythcoreutil.cpp +++ b/mythtv/libs/libmythbase/mythcoreutil.cpp @@ -1,7 +1,6 @@ #include "mythcoreutil.h" // POSIX -#include #include #include @@ -31,7 +30,6 @@ // libmythbase headers #include "mythcorecontext.h" #include "mythlogging.h" -#include "unzip2.h" /** \fn getDiskSpace(const QString&,long long&,long long&) * \brief Returns free space on disk containing file in KiB, @@ -69,189 +67,6 @@ int64_t getDiskSpace(const QString &file_on_disk, return freespace; } -bool extractZIP(QString zipFile, QString outDir) -{ - UnZip unzip(std::move(zipFile)); - return unzip.extractFile(std::move(outDir)); -} - -bool gzipFile(const QString &inFilename, const QString &gzipFilename) -{ - QFile infile(inFilename); - QFile outfile(gzipFilename); - - if (!infile.open(QIODevice::ReadOnly)) - { - LOG(VB_GENERAL, LOG_ERR, QString("gzipFile(): Error opening file for reading '%1'").arg(inFilename)); - return false; - } - - if (!outfile.open(QIODevice::WriteOnly)) - { - LOG(VB_GENERAL, LOG_ERR, QString("gzipFile(): Error opening file for writing '%1'").arg(gzipFilename)); - infile.close(); - return false; - } - - QByteArray uncompressedData = infile.readAll(); - QByteArray compressedData = gzipCompress(uncompressedData); - - if (!outfile.write(compressedData)) - { - LOG(VB_GENERAL, LOG_ERR, QString("gzipFile(): Error while writing to '%1'").arg(gzipFilename)); - infile.close(); - outfile.close(); - return false; - } - - infile.close(); - outfile.close(); - - return true; -} - -bool gunzipFile(const QString &gzipFilename, const QString &outFilename) -{ - QFile infile(gzipFilename); - QFile outfile(outFilename); - - if (!infile.open(QIODevice::ReadOnly)) - { - LOG(VB_GENERAL, LOG_ERR, QString("gunzipFile(): Error opening file for reading '%1'").arg(gzipFilename)); - return false; - } - - if (!outfile.open(QIODevice::WriteOnly)) - { - LOG(VB_GENERAL, LOG_ERR, QString("gunzipFile(): Error opening file for writing '%1'").arg(outFilename)); - infile.close(); - return false; - } - - QByteArray compressedData = infile.readAll(); - QByteArray uncompressedData = gzipUncompress(compressedData); - - if (outfile.write(uncompressedData) < uncompressedData.size()) - { - LOG(VB_GENERAL, LOG_ERR, QString("gunzipFile(): Error while writing to '%1'").arg(outFilename)); - infile.close(); - outfile.close(); - return false; - } - - infile.close(); - outfile.close(); - - return true; -} - -QByteArray gzipCompress(const QByteArray& data) -{ - if (data.length() == 0) - return QByteArray(); - - std::array out {}; - - // allocate inflate state - z_stream strm {}; - - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - strm.avail_in = data.length(); - strm.next_in = (Bytef*)(data.data()); - - int ret = deflateInit2(&strm, - Z_DEFAULT_COMPRESSION, - Z_DEFLATED, - 15 + 16, - 8, - Z_DEFAULT_STRATEGY ); // gzip encoding - if (ret != Z_OK) - return QByteArray(); - - QByteArray result; - - // run deflate() - do - { - strm.avail_out = out.size(); - strm.next_out = (Bytef*)(out.data()); - - ret = deflate(&strm, Z_FINISH); - - Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered - - switch (ret) - { - case Z_NEED_DICT: - case Z_DATA_ERROR: - case Z_MEM_ERROR: - (void)deflateEnd(&strm); - return QByteArray(); - } - - result.append(out.data(), out.size() - strm.avail_out); - } - while (strm.avail_out == 0); - - // clean up and return - - deflateEnd(&strm); - - return result; -} - -QByteArray gzipUncompress(const QByteArray &data) -{ - if (data.length() == 0) - return QByteArray(); - - std::array out {}; - - // allocate inflate state - z_stream strm; - strm.total_in = 0; - strm.total_out = 0; - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - strm.avail_in = data.length(); - strm.next_in = (Bytef*)(data.data()); - - int ret = inflateInit2(&strm, 15 + 16); - - if (ret != Z_OK) - return QByteArray(); - - QByteArray result; - - do - { - strm.avail_out = out.size(); - strm.next_out = (Bytef*)out.data(); - ret = inflate(&strm, Z_NO_FLUSH); - - Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered - - switch (ret) - { - case Z_NEED_DICT: - case Z_DATA_ERROR: - case Z_MEM_ERROR: - (void) deflateEnd(&strm); - return QByteArray(); - } - - result.append(out.data(), out.size() - strm.avail_out); - } - while (strm.avail_out == 0); - - (void) inflateEnd(& strm); - - return result; -} - static QString downloadRemoteFile(const QString &cmd, const QString &url, const QString &storageGroup, const QString &filename) diff --git a/mythtv/libs/libmythbase/mythcoreutil.h b/mythtv/libs/libmythbase/mythcoreutil.h index e7db402b593..601d5ba88ba 100644 --- a/mythtv/libs/libmythbase/mythcoreutil.h +++ b/mythtv/libs/libmythbase/mythcoreutil.h @@ -9,14 +9,6 @@ MBASE_PUBLIC int64_t getDiskSpace(const QString &file_on_disk, int64_t &total, int64_t &used); - MBASE_PUBLIC bool extractZIP(QString zipFile, QString outDir); - - MBASE_PUBLIC bool gzipFile(const QString &inFilename, const QString &zipFilename); - MBASE_PUBLIC bool gunzipFile(const QString &zipFilename, const QString &outFilename); - - MBASE_PUBLIC QByteArray gzipCompress(const QByteArray &data); - MBASE_PUBLIC QByteArray gzipUncompress(const QByteArray &data); - MBASE_PUBLIC QString RemoteDownloadFile(const QString &url, const QString &storageGroup, const QString &filename = ""); diff --git a/mythtv/libs/libmythbase/test/test_unzip/test_unzip.cpp b/mythtv/libs/libmythbase/test/test_unzip/test_unzip.cpp index 75a8c7ec569..78e18bc55b2 100644 --- a/mythtv/libs/libmythbase/test/test_unzip/test_unzip.cpp +++ b/mythtv/libs/libmythbase/test/test_unzip/test_unzip.cpp @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include -#include "mythcoreutil.h" +#include "unziputil.h" #include "test_unzip.h" #include diff --git a/mythtv/libs/libmythbase/unziputil.cpp b/mythtv/libs/libmythbase/unziputil.cpp new file mode 100644 index 00000000000..de727cc12f0 --- /dev/null +++ b/mythtv/libs/libmythbase/unziputil.cpp @@ -0,0 +1,195 @@ +#include "unziputil.h" + +#include + +// Qt headers +#include +#include + +// libmythbase headers +#include "mythlogging.h" +#include "unzip2.h" + +bool extractZIP(QString zipFile, QString outDir) +{ + UnZip unzip(std::move(zipFile)); + return unzip.extractFile(std::move(outDir)); +} + +bool gzipFile(const QString &inFilename, const QString &gzipFilename) +{ + QFile infile(inFilename); + QFile outfile(gzipFilename); + + if (!infile.open(QIODevice::ReadOnly)) + { + LOG(VB_GENERAL, LOG_ERR, QString("gzipFile(): Error opening file for reading '%1'").arg(inFilename)); + return false; + } + + if (!outfile.open(QIODevice::WriteOnly)) + { + LOG(VB_GENERAL, LOG_ERR, QString("gzipFile(): Error opening file for writing '%1'").arg(gzipFilename)); + infile.close(); + return false; + } + + QByteArray uncompressedData = infile.readAll(); + QByteArray compressedData = gzipCompress(uncompressedData); + + if (!outfile.write(compressedData)) + { + LOG(VB_GENERAL, LOG_ERR, QString("gzipFile(): Error while writing to '%1'").arg(gzipFilename)); + infile.close(); + outfile.close(); + return false; + } + + infile.close(); + outfile.close(); + + return true; +} + +bool gunzipFile(const QString &gzipFilename, const QString &outFilename) +{ + QFile infile(gzipFilename); + QFile outfile(outFilename); + + if (!infile.open(QIODevice::ReadOnly)) + { + LOG(VB_GENERAL, LOG_ERR, QString("gunzipFile(): Error opening file for reading '%1'").arg(gzipFilename)); + return false; + } + + if (!outfile.open(QIODevice::WriteOnly)) + { + LOG(VB_GENERAL, LOG_ERR, QString("gunzipFile(): Error opening file for writing '%1'").arg(outFilename)); + infile.close(); + return false; + } + + QByteArray compressedData = infile.readAll(); + QByteArray uncompressedData = gzipUncompress(compressedData); + + if (outfile.write(uncompressedData) < uncompressedData.size()) + { + LOG(VB_GENERAL, LOG_ERR, QString("gunzipFile(): Error while writing to '%1'").arg(outFilename)); + infile.close(); + outfile.close(); + return false; + } + + infile.close(); + outfile.close(); + + return true; +} + +QByteArray gzipCompress(const QByteArray& data) +{ + if (data.length() == 0) + return QByteArray(); + + std::array out {}; + + // allocate inflate state + z_stream strm {}; + + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = data.length(); + strm.next_in = (Bytef*)(data.data()); + + int ret = deflateInit2(&strm, + Z_DEFAULT_COMPRESSION, + Z_DEFLATED, + 15 + 16, + 8, + Z_DEFAULT_STRATEGY ); // gzip encoding + if (ret != Z_OK) + return QByteArray(); + + QByteArray result; + + // run deflate() + do + { + strm.avail_out = out.size(); + strm.next_out = (Bytef*)(out.data()); + + ret = deflate(&strm, Z_FINISH); + + Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered + + switch (ret) + { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: + (void)deflateEnd(&strm); + return QByteArray(); + } + + result.append(out.data(), out.size() - strm.avail_out); + } + while (strm.avail_out == 0); + + // clean up and return + + deflateEnd(&strm); + + return result; +} + +QByteArray gzipUncompress(const QByteArray &data) +{ + if (data.length() == 0) + return QByteArray(); + + std::array out {}; + + // allocate inflate state + z_stream strm; + strm.total_in = 0; + strm.total_out = 0; + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = data.length(); + strm.next_in = (Bytef*)(data.data()); + + int ret = inflateInit2(&strm, 15 + 16); + + if (ret != Z_OK) + return QByteArray(); + + QByteArray result; + + do + { + strm.avail_out = out.size(); + strm.next_out = (Bytef*)out.data(); + ret = inflate(&strm, Z_NO_FLUSH); + + Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered + + switch (ret) + { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: + (void) deflateEnd(&strm); + return QByteArray(); + } + + result.append(out.data(), out.size() - strm.avail_out); + } + while (strm.avail_out == 0); + + (void) inflateEnd(& strm); + + return result; +} + diff --git a/mythtv/libs/libmythbase/unziputil.h b/mythtv/libs/libmythbase/unziputil.h new file mode 100644 index 00000000000..58d5f23309f --- /dev/null +++ b/mythtv/libs/libmythbase/unziputil.h @@ -0,0 +1,17 @@ +#ifndef UNZIPUTIL_H_ +#define UNZIPUTIL_H_ + +#include +#include + +#include "mythbaseexp.h" + +MBASE_PUBLIC bool extractZIP(QString zipFile, QString outDir); + +MBASE_PUBLIC bool gzipFile(const QString &inFilename, const QString &zipFilename); +MBASE_PUBLIC bool gunzipFile(const QString &zipFilename, const QString &outFilename); + +MBASE_PUBLIC QByteArray gzipCompress(const QByteArray &data); +MBASE_PUBLIC QByteArray gzipUncompress(const QByteArray &data); + +#endif // UNZIPUTIL_H_ diff --git a/mythtv/libs/libmythmetadata/musicmetadata.cpp b/mythtv/libs/libmythmetadata/musicmetadata.cpp index efebce874c2..bd52e940e9a 100644 --- a/mythtv/libs/libmythmetadata/musicmetadata.cpp +++ b/mythtv/libs/libmythmetadata/musicmetadata.cpp @@ -20,7 +20,7 @@ #include "remotefile.h" #include "storagegroup.h" #include "mythsystem.h" -#include "mythcoreutil.h" +#include "unziputil.h" // mythbase #include "mythsorthelper.h" diff --git a/mythtv/libs/libmythupnp/httprequest.cpp b/mythtv/libs/libmythupnp/httprequest.cpp index c5c4163363a..40c9fd7a86e 100644 --- a/mythtv/libs/libmythupnp/httprequest.cpp +++ b/mythtv/libs/libmythupnp/httprequest.cpp @@ -40,7 +40,7 @@ #include "mythdate.h" #include "mythcorecontext.h" #include "mythtimer.h" -#include "mythcoreutil.h" +#include "unziputil.h" #include "configuration.h" #include "serializers/xmlSerializer.h" diff --git a/mythtv/programs/mythbackend/backendhousekeeper.cpp b/mythtv/programs/mythbackend/backendhousekeeper.cpp index eacc327f941..d9c929746aa 100644 --- a/mythtv/programs/mythbackend/backendhousekeeper.cpp +++ b/mythtv/programs/mythbackend/backendhousekeeper.cpp @@ -19,7 +19,7 @@ #include "exitcodes.h" #include "mythsystemlegacy.h" #include "mythversion.h" -#include "mythcoreutil.h" +#include "unziputil.h" #include "programtypes.h" #include "recordingtypes.h" #include "mythcorecontext.h" diff --git a/mythtv/programs/mythbackend/httpconfig.cpp b/mythtv/programs/mythbackend/httpconfig.cpp index a9a3296701e..aad2db8981a 100644 --- a/mythtv/programs/mythbackend/httpconfig.cpp +++ b/mythtv/programs/mythbackend/httpconfig.cpp @@ -15,7 +15,7 @@ #include "mythdirs.h" #include "storagegroup.h" #include "mythdownloadmanager.h" -#include "mythcoreutil.h" +#include "unziputil.h" HttpConfig::HttpConfig() : HttpServerExtension("HttpConfig", QString()) { diff --git a/mythtv/programs/mythfrontend/themechooser.cpp b/mythtv/programs/mythfrontend/themechooser.cpp index 16fe9a6abde..501e6821647 100644 --- a/mythtv/programs/mythfrontend/themechooser.cpp +++ b/mythtv/programs/mythfrontend/themechooser.cpp @@ -13,6 +13,7 @@ // MythTV headers #include "mythcorecontext.h" #include "mythcoreutil.h" +#include "unziputil.h" // for extractZIP #include "mthreadpool.h" #include "remotefile.h" #include "mythdownloadmanager.h"