Skip to content

Commit

Permalink
create unziputil from mythcoreutil
Browse files Browse the repository at this point in the history
  • Loading branch information
ulmus-scott authored and linuxdude42 committed Apr 8, 2022
1 parent ed49d1b commit 444d999
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 199 deletions.
2 changes: 1 addition & 1 deletion 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"
Expand Down
3 changes: 3 additions & 0 deletions mythtv/libs/libmythbase/libmythbase.pro
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 <blah.h> and #include <libmythbase/blah.h>
inc2.path = $${PREFIX}/include/mythtv/libmythbase
Expand Down
185 changes: 0 additions & 185 deletions mythtv/libs/libmythbase/mythcoreutil.cpp
@@ -1,7 +1,6 @@
#include "mythcoreutil.h"

// POSIX
#include <array>
#include <unistd.h>
#include <fcntl.h>

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 <char,1024> 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<char,1024> 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)
Expand Down
8 changes: 0 additions & 8 deletions mythtv/libs/libmythbase/mythcoreutil.h
Expand Up @@ -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 = "");
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/test/test_unzip/test_unzip.cpp
Expand Up @@ -18,7 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <iostream>
#include "mythcoreutil.h"
#include "unziputil.h"
#include "test_unzip.h"
#include <QTemporaryDir>

Expand Down

0 comments on commit 444d999

Please sign in to comment.