-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters