Skip to content

Commit

Permalink
Implement QLZ4
Browse files Browse the repository at this point in the history
  • Loading branch information
myst6re committed Sep 7, 2019
1 parent 12b5b38 commit f39dc72
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 4 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "zlib"]
path = zlib
url = https://github.com/madler/zlib.git
[submodule "lz4"]
path = lz4
url = https://github.com/lz4/lz4
5 changes: 4 additions & 1 deletion Deling.pro
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ lessThan(QT_MAJOR_VERSION, 5) {
# Input
HEADERS += MainWindow.h \
PreviewWidget.h \
QLZ4.h \
parameters.h \
Data.h \
Config.h \
Expand Down Expand Up @@ -109,6 +110,7 @@ HEADERS += MainWindow.h \

SOURCES += MainWindow.cpp \
PreviewWidget.cpp \
QLZ4.cpp \
main.cpp \
Data.cpp \
Config.cpp \
Expand Down Expand Up @@ -222,7 +224,8 @@ RESOURCES += Deling.qrc
!win32 {
LIBS += -llz4
} else {
INCLUDEPATH += lz4/lib
INCLUDEPATH += lz4
LIBS += $$_PRO_FILE_PWD_/lz4/liblz4.lib
}

win32 {
Expand Down
88 changes: 88 additions & 0 deletions QLZ4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "QLZ4.h"
#include <lz4.h>

QByteArray QLZ4::result;

const QByteArray &QLZ4::decompressAll(const char *data, int size, bool *ok)
{
if (ok != nullptr) {
*ok = false;
}

if (size <= 8) {
return result;
}

int dstCapacity;

memcpy(&dstCapacity, data + 4, 4);

// Impossible ratio
if (dstCapacity > size * 2000) {
return result;
}

result.resize(dstCapacity * 1000);

int decSize = LZ4_decompress_safe(data + 8, result.data(), size - 8, result.size());

if (decSize < 0) {
return result;
}

if (ok != nullptr) {
*ok = true;
}

result.resize(decSize);

return result;
}

const QByteArray &QLZ4::decompress(const char *data, int size, int max, bool *ok)
{
if (ok != nullptr) {
*ok = false;
}

if (size <= 8) {
return result;
}

result.resize(max + 10);

int decSize = LZ4_decompress_safe_partial(data + 8, result.data(), size - 8, max, result.size());

if (decSize < 0) {
return result;
}

if (ok != nullptr) {
*ok = true;
}

result.resize(decSize);

return result;
}

const QByteArray &QLZ4::compress(const char *data, int size)
{
if (size <= 0) {
result.resize(0);

return result;
}

result.resize(size * 2);

int compSize = LZ4_compress_default(data, result.data() + 8, size, result.size() - 8);

if (compSize > 0) {
result.resize(8 + compSize);
memcpy(result.data(), "4ZL_", 4);
memcpy(result.data() + 4, &size, 4);
}

return result;
}
25 changes: 25 additions & 0 deletions QLZ4.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef QLZ4_H
#define QLZ4_H

#include <QByteArray>

class QLZ4
{
public:
static const QByteArray &decompressAll(const QByteArray &data, bool *ok = nullptr) {
return decompressAll(data.constData(), data.size(), ok);
}
static const QByteArray &decompressAll(const char *data, int size, bool *ok = nullptr);
static const QByteArray &decompress(const QByteArray &data, int max, bool *ok = nullptr) {
return decompress(data.constData(), data.size(), max, ok);
}
static const QByteArray &decompress(const char *data, int size, int max, bool *ok = nullptr);
static const QByteArray &compress(const QByteArray &data) {
return compress(data.constData(), data.size());
}
static const QByteArray &compress(const char *data, int size);
private:
static QByteArray result;
};

#endif // QLZ4_H
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Final Fantasy VIII field archive editor.
- lz4

On Windows, you can either use mingw32 (g++) or msvc to compile.
You need liblz4.dll to run Deling on Windows.

### With Qt Creator

Expand Down
2 changes: 2 additions & 0 deletions deploy.bat
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ for %%l in (%LANGUAGES%) do (
echo "Create %QT_TR_DIR%\qtbase_%%l.qm"
)

xcopy /y %LIB_DIR%\liblz4.dll %OUTPUT_DIR%

rem Deploy Exe
xcopy /y %EXE_PATH% %OUTPUT_DIR%

Expand Down

0 comments on commit f39dc72

Please sign in to comment.