mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-22 16:07:39 +00:00
Simplify compress code
This commit is contained in:
parent
912c188b53
commit
9a95652034
@ -13,18 +13,11 @@
|
|||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
#define windowBits 15
|
|
||||||
#define ZLIB_GZIP 16
|
|
||||||
#define memLevel 8
|
|
||||||
#define CHUNK 0x40000
|
#define CHUNK 0x40000
|
||||||
|
|
||||||
#define LZ4_HEADER_SIZE 19
|
|
||||||
#define LZ4_FOOTER_SIZE 4
|
|
||||||
#define LZ4_LEGACY_BLOCKSIZE 0x800000
|
|
||||||
|
|
||||||
// Mode: 0 = decode; 1 = encode
|
// Mode: 0 = decode; 1 = encode
|
||||||
size_t gzip(int mode, int fd, const void *buf, size_t size) {
|
size_t gzip(int mode, int fd, const void *buf, size_t size) {
|
||||||
size_t ret = 0, flush, have, pos = 0, total = 0;
|
size_t ret = 0, have, total = 0;
|
||||||
z_stream strm;
|
z_stream strm;
|
||||||
unsigned char out[CHUNK];
|
unsigned char out[CHUNK];
|
||||||
|
|
||||||
@ -34,48 +27,36 @@ size_t gzip(int mode, int fd, const void *buf, size_t size) {
|
|||||||
|
|
||||||
switch(mode) {
|
switch(mode) {
|
||||||
case 0:
|
case 0:
|
||||||
ret = inflateInit2(&strm, windowBits | ZLIB_GZIP);
|
ret = inflateInit2(&strm, 15 | 16);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
ret = deflateInit2(&strm, 9, Z_DEFLATED, windowBits | ZLIB_GZIP, memLevel, Z_DEFAULT_STRATEGY);
|
ret = deflateInit2(&strm, 9, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret != Z_OK)
|
if (ret != Z_OK)
|
||||||
LOGE("Unable to init zlib stream\n");
|
LOGE("Unable to init zlib stream\n");
|
||||||
|
|
||||||
do {
|
strm.next_in = (void *) buf;
|
||||||
strm.next_in = (void *) buf + pos;
|
strm.avail_in = size;
|
||||||
if (pos + CHUNK >= size) {
|
|
||||||
strm.avail_in = size - pos;
|
|
||||||
flush = Z_FINISH;
|
|
||||||
} else {
|
|
||||||
strm.avail_in = CHUNK;
|
|
||||||
flush = Z_NO_FLUSH;
|
|
||||||
}
|
|
||||||
pos += strm.avail_in;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
strm.avail_out = CHUNK;
|
strm.avail_out = CHUNK;
|
||||||
strm.next_out = out;
|
strm.next_out = out;
|
||||||
switch(mode) {
|
switch(mode) {
|
||||||
case 0:
|
case 0:
|
||||||
ret = inflate(&strm, flush);
|
ret = inflate(&strm, Z_FINISH);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
ret = deflate(&strm, flush);
|
ret = deflate(&strm, Z_FINISH);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (ret == Z_STREAM_ERROR)
|
if (ret == Z_STREAM_ERROR)
|
||||||
LOGE("Error when running gzip\n");
|
LOGE("Error when running gzip\n");
|
||||||
|
|
||||||
have = CHUNK - strm.avail_out;
|
have = CHUNK - strm.avail_out;
|
||||||
total += xwrite(fd, out, have);
|
total += xwrite(fd, out, have);
|
||||||
|
|
||||||
} while (strm.avail_out == 0);
|
} while (strm.avail_out == 0);
|
||||||
|
|
||||||
} while(pos < size);
|
|
||||||
|
|
||||||
switch(mode) {
|
switch(mode) {
|
||||||
case 0:
|
case 0:
|
||||||
inflateEnd(&strm);
|
inflateEnd(&strm);
|
||||||
@ -89,12 +70,11 @@ size_t gzip(int mode, int fd, const void *buf, size_t size) {
|
|||||||
|
|
||||||
// Mode: 0 = decode xz/lzma; 1 = encode xz; 2 = encode lzma
|
// Mode: 0 = decode xz/lzma; 1 = encode xz; 2 = encode lzma
|
||||||
size_t lzma(int mode, int fd, const void *buf, size_t size) {
|
size_t lzma(int mode, int fd, const void *buf, size_t size) {
|
||||||
size_t have, pos = 0, total = 0;
|
size_t have, total = 0;
|
||||||
lzma_ret ret = 0;
|
lzma_ret ret = 0;
|
||||||
lzma_stream strm = LZMA_STREAM_INIT;
|
lzma_stream strm = LZMA_STREAM_INIT;
|
||||||
lzma_options_lzma opt;
|
lzma_options_lzma opt;
|
||||||
lzma_action action;
|
unsigned char out[CHUNK];
|
||||||
unsigned char out[BUFSIZ];
|
|
||||||
|
|
||||||
// Initialize preset
|
// Initialize preset
|
||||||
lzma_lzma_preset(&opt, 9);
|
lzma_lzma_preset(&opt, 9);
|
||||||
@ -119,29 +99,18 @@ size_t lzma(int mode, int fd, const void *buf, size_t size) {
|
|||||||
if (ret != LZMA_OK)
|
if (ret != LZMA_OK)
|
||||||
LOGE("Unable to init lzma stream\n");
|
LOGE("Unable to init lzma stream\n");
|
||||||
|
|
||||||
do {
|
strm.next_in = buf;
|
||||||
strm.next_in = buf + pos;
|
strm.avail_in = size;
|
||||||
if (pos + BUFSIZ >= size) {
|
|
||||||
strm.avail_in = size - pos;
|
|
||||||
action = LZMA_FINISH;
|
|
||||||
} else {
|
|
||||||
strm.avail_in = BUFSIZ;
|
|
||||||
action = LZMA_RUN;
|
|
||||||
}
|
|
||||||
pos += strm.avail_in;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
strm.avail_out = BUFSIZ;
|
strm.avail_out = CHUNK;
|
||||||
strm.next_out = out;
|
strm.next_out = out;
|
||||||
ret = lzma_code(&strm, action);
|
ret = lzma_code(&strm, LZMA_FINISH);
|
||||||
have = BUFSIZ - strm.avail_out;
|
|
||||||
total += xwrite(fd, out, have);
|
|
||||||
} while (strm.avail_out == 0 && ret == LZMA_OK);
|
|
||||||
|
|
||||||
if (ret != LZMA_OK && ret != LZMA_STREAM_END)
|
if (ret != LZMA_OK && ret != LZMA_STREAM_END)
|
||||||
LOGE("LZMA error %d!\n", ret);
|
LOGE("LZMA error %d!\n", ret);
|
||||||
|
have = CHUNK - strm.avail_out;
|
||||||
} while (pos < size);
|
total += xwrite(fd, out, have);
|
||||||
|
} while (strm.avail_out == 0);
|
||||||
|
|
||||||
lzma_end(&strm);
|
lzma_end(&strm);
|
||||||
return total;
|
return total;
|
||||||
@ -263,7 +232,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) {
|
|||||||
|
|
||||||
// Mode: 0 = decode; 1 = encode
|
// Mode: 0 = decode; 1 = encode
|
||||||
size_t bzip2(int mode, int fd, const void* buf, size_t size) {
|
size_t bzip2(int mode, int fd, const void* buf, size_t size) {
|
||||||
size_t ret = 0, action, have, pos = 0, total = 0;
|
size_t ret = 0, have, total = 0;
|
||||||
bz_stream strm;
|
bz_stream strm;
|
||||||
char out[CHUNK];
|
char out[CHUNK];
|
||||||
|
|
||||||
@ -283,16 +252,8 @@ size_t bzip2(int mode, int fd, const void* buf, size_t size) {
|
|||||||
if (ret != BZ_OK)
|
if (ret != BZ_OK)
|
||||||
LOGE("Unable to init bzlib stream\n");
|
LOGE("Unable to init bzlib stream\n");
|
||||||
|
|
||||||
do {
|
strm.next_in = (void *) buf;
|
||||||
strm.next_in = (char *) buf + pos;
|
strm.avail_in = size;
|
||||||
if (pos + CHUNK >= size) {
|
|
||||||
strm.avail_in = size - pos;
|
|
||||||
action = BZ_FINISH;
|
|
||||||
} else {
|
|
||||||
strm.avail_in = CHUNK;
|
|
||||||
action = BZ_RUN;
|
|
||||||
}
|
|
||||||
pos += strm.avail_in;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
strm.avail_out = CHUNK;
|
strm.avail_out = CHUNK;
|
||||||
@ -302,17 +263,13 @@ size_t bzip2(int mode, int fd, const void* buf, size_t size) {
|
|||||||
ret = BZ2_bzDecompress(&strm);
|
ret = BZ2_bzDecompress(&strm);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
ret = BZ2_bzCompress(&strm, action);
|
ret = BZ2_bzCompress(&strm, BZ_FINISH);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
have = CHUNK - strm.avail_out;
|
have = CHUNK - strm.avail_out;
|
||||||
total += xwrite(fd, out, have);
|
total += xwrite(fd, out, have);
|
||||||
|
|
||||||
} while (strm.avail_out == 0);
|
} while (strm.avail_out == 0);
|
||||||
|
|
||||||
} while(pos < size);
|
|
||||||
|
|
||||||
switch(mode) {
|
switch(mode) {
|
||||||
case 0:
|
case 0:
|
||||||
BZ2_bzDecompressEnd(&strm);
|
BZ2_bzDecompressEnd(&strm);
|
||||||
@ -324,6 +281,8 @@ size_t bzip2(int mode, int fd, const void* buf, size_t size) {
|
|||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define LZ4_LEGACY_BLOCKSIZE 0x800000
|
||||||
|
|
||||||
// Mode: 0 = decode; 1 = encode
|
// Mode: 0 = decode; 1 = encode
|
||||||
size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) {
|
size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) {
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user