2019-02-20 20:49:26 -05:00
|
|
|
#include <memory>
|
2019-02-23 04:15:54 -05:00
|
|
|
#include <functional>
|
2017-09-14 23:11:56 +08:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
#include <zlib.h>
|
|
|
|
#include <bzlib.h>
|
|
|
|
#include <lzma.h>
|
|
|
|
#include <lz4.h>
|
|
|
|
#include <lz4frame.h>
|
|
|
|
#include <lz4hc.h>
|
2021-07-29 01:45:09 +02:00
|
|
|
#include <zopfli/util.h>
|
|
|
|
#include <zopfli/deflate.h>
|
2019-11-21 06:08:02 -05:00
|
|
|
|
2022-05-12 02:03:42 -07:00
|
|
|
#include <base.hpp>
|
2017-02-28 21:56:13 +08:00
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "magiskboot.hpp"
|
|
|
|
#include "compress.hpp"
|
2017-02-28 05:37:47 +08:00
|
|
|
|
2019-02-20 20:49:26 -05:00
|
|
|
using namespace std;
|
|
|
|
|
2021-11-23 18:46:06 -08:00
|
|
|
#define bwrite this->base->write
|
2017-11-10 20:25:41 +08:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
constexpr size_t CHUNK = 0x40000;
|
|
|
|
constexpr size_t LZ4_UNCOMPRESSED = 0x800000;
|
|
|
|
constexpr size_t LZ4_COMPRESSED = LZ4_COMPRESSBOUND(LZ4_UNCOMPRESSED);
|
2017-11-10 20:25:41 +08:00
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
class gz_strm : public filter_out_stream {
|
2019-11-21 06:08:02 -05:00
|
|
|
public:
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) override {
|
2021-11-23 18:46:06 -08:00
|
|
|
return len == 0 || do_write(buf, len, Z_NO_FLUSH);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
~gz_strm() override {
|
2021-11-23 18:46:06 -08:00
|
|
|
do_write(nullptr, 0, Z_FINISH);
|
2020-12-30 22:11:24 -08:00
|
|
|
switch(mode) {
|
2021-11-21 04:38:22 -08:00
|
|
|
case DECODE:
|
|
|
|
inflateEnd(&strm);
|
|
|
|
break;
|
|
|
|
case ENCODE:
|
|
|
|
deflateEnd(&strm);
|
|
|
|
break;
|
2023-02-13 02:33:23 +08:00
|
|
|
default:
|
|
|
|
break;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-22 03:01:49 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
protected:
|
2020-12-30 22:11:24 -08:00
|
|
|
enum mode_t {
|
|
|
|
DECODE,
|
2023-02-13 02:33:23 +08:00
|
|
|
ENCODE,
|
|
|
|
WAIT,
|
|
|
|
COPY
|
2020-12-30 22:11:24 -08:00
|
|
|
} mode;
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
gz_strm(mode_t mode, out_strm_ptr &&base) :
|
|
|
|
filter_out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} {
|
2020-12-30 22:11:24 -08:00
|
|
|
switch(mode) {
|
2021-11-21 04:38:22 -08:00
|
|
|
case DECODE:
|
|
|
|
inflateInit2(&strm, 15 | 16);
|
|
|
|
break;
|
|
|
|
case ENCODE:
|
|
|
|
deflateInit2(&strm, 9, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY);
|
|
|
|
break;
|
2023-02-13 02:33:23 +08:00
|
|
|
default:
|
|
|
|
break;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 20:49:26 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
z_stream strm;
|
|
|
|
uint8_t outbuf[CHUNK];
|
|
|
|
|
2021-11-23 18:46:06 -08:00
|
|
|
bool do_write(const void *buf, size_t len, int flush) {
|
2023-02-13 02:33:23 +08:00
|
|
|
if (mode == WAIT) {
|
|
|
|
if (len == 0) return true;
|
|
|
|
Bytef b[1] = {0x1f};
|
|
|
|
if (*(Bytef *)buf == 0x8b) {
|
|
|
|
mode = DECODE;
|
|
|
|
inflateReset(&strm);
|
|
|
|
strm.next_in = b;
|
|
|
|
strm.avail_in = 1;
|
|
|
|
inflate(&strm, flush);
|
|
|
|
} else {
|
|
|
|
mode = COPY;
|
2023-03-14 10:12:23 +08:00
|
|
|
return true;
|
2023-02-13 02:33:23 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
strm.next_in = (Bytef *) buf;
|
|
|
|
strm.avail_in = len;
|
|
|
|
do {
|
|
|
|
int code;
|
|
|
|
strm.next_out = outbuf;
|
|
|
|
strm.avail_out = sizeof(outbuf);
|
|
|
|
switch(mode) {
|
|
|
|
case DECODE:
|
|
|
|
code = inflate(&strm, flush);
|
|
|
|
break;
|
|
|
|
case ENCODE:
|
|
|
|
code = deflate(&strm, flush);
|
|
|
|
break;
|
2023-02-13 02:33:23 +08:00
|
|
|
case COPY:
|
2023-03-14 10:12:23 +08:00
|
|
|
return true;
|
2023-02-13 02:33:23 +08:00
|
|
|
default:
|
|
|
|
// should have been handled
|
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
if (code == Z_STREAM_ERROR) {
|
|
|
|
LOGW("gzip %s failed (%d)\n", mode ? "encode" : "decode", code);
|
2021-11-21 04:38:22 -08:00
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2021-11-21 04:38:22 -08:00
|
|
|
if (!bwrite(outbuf, sizeof(outbuf) - strm.avail_out))
|
|
|
|
return false;
|
2023-02-13 02:33:23 +08:00
|
|
|
if (mode == DECODE && code == Z_STREAM_END) {
|
|
|
|
if (strm.avail_in > 1) {
|
|
|
|
if (strm.next_in[0] == 0x1f && strm.next_in[1] == 0x8b) {
|
|
|
|
// There is still data in the stream, we need to reset the stream
|
|
|
|
// and continue decoding
|
|
|
|
inflateReset(&strm);
|
|
|
|
strm.avail_out = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (strm.avail_in == 1) {
|
|
|
|
if (strm.next_in[0] == 0x1f) {
|
|
|
|
// If there is only one byte left, we need to wait for the next byte
|
|
|
|
// to determine if it is a gzip header
|
|
|
|
mode = WAIT;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The next inflate won't consume any data but fallback
|
|
|
|
// to the previous two conditions
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// There is still data in the stream, we need to copy it
|
|
|
|
mode = COPY;
|
2023-03-14 10:12:23 +08:00
|
|
|
return true;
|
2023-02-13 02:33:23 +08:00
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
} while (strm.avail_out == 0);
|
2021-11-21 04:38:22 -08:00
|
|
|
return true;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
2019-02-20 20:49:26 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
class gz_decoder : public gz_strm {
|
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit gz_decoder(out_strm_ptr &&base) : gz_strm(DECODE, std::move(base)) {};
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
2017-03-05 01:50:36 +08:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
class gz_encoder : public gz_strm {
|
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit gz_encoder(out_strm_ptr &&base) : gz_strm(ENCODE, std::move(base)) {};
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
2019-02-20 20:49:26 -05:00
|
|
|
|
2021-11-23 16:50:08 -08:00
|
|
|
class zopfli_encoder : public chunk_out_stream {
|
2021-07-29 01:45:09 +02:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit zopfli_encoder(out_strm_ptr &&base) :
|
2021-11-23 16:50:08 -08:00
|
|
|
chunk_out_stream(std::move(base), ZOPFLI_MASTER_BLOCK_SIZE),
|
2021-11-23 18:08:14 -08:00
|
|
|
zo{}, out(nullptr), outsize(0), crc(crc32_z(0L, Z_NULL, 0)), in_total(0), bp(0) {
|
2021-08-28 14:07:34 -07:00
|
|
|
ZopfliInitOptions(&zo);
|
|
|
|
|
2022-01-24 23:07:37 -08:00
|
|
|
// This config is already better than gzip -9
|
|
|
|
zo.numiterations = 1;
|
|
|
|
zo.blocksplitting = 0;
|
2021-11-03 16:51:33 +01:00
|
|
|
|
2021-08-28 14:07:34 -07:00
|
|
|
ZOPFLI_APPEND_DATA(31, &out, &outsize); /* ID1 */
|
|
|
|
ZOPFLI_APPEND_DATA(139, &out, &outsize); /* ID2 */
|
|
|
|
ZOPFLI_APPEND_DATA(8, &out, &outsize); /* CM */
|
|
|
|
ZOPFLI_APPEND_DATA(0, &out, &outsize); /* FLG */
|
|
|
|
/* MTIME */
|
|
|
|
ZOPFLI_APPEND_DATA(0, &out, &outsize);
|
|
|
|
ZOPFLI_APPEND_DATA(0, &out, &outsize);
|
|
|
|
ZOPFLI_APPEND_DATA(0, &out, &outsize);
|
|
|
|
ZOPFLI_APPEND_DATA(0, &out, &outsize);
|
|
|
|
|
|
|
|
ZOPFLI_APPEND_DATA(2, &out, &outsize); /* XFL, 2 indicates best compression. */
|
|
|
|
ZOPFLI_APPEND_DATA(3, &out, &outsize); /* OS follows Unix conventions. */
|
2021-07-29 01:45:09 +02:00
|
|
|
}
|
|
|
|
|
2021-08-28 14:07:34 -07:00
|
|
|
~zopfli_encoder() override {
|
2021-11-23 16:50:08 -08:00
|
|
|
finalize();
|
2021-08-28 17:01:08 -07:00
|
|
|
|
|
|
|
/* CRC */
|
2021-11-23 16:50:08 -08:00
|
|
|
ZOPFLI_APPEND_DATA(crc % 256, &out, &outsize);
|
|
|
|
ZOPFLI_APPEND_DATA((crc >> 8) % 256, &out, &outsize);
|
|
|
|
ZOPFLI_APPEND_DATA((crc >> 16) % 256, &out, &outsize);
|
|
|
|
ZOPFLI_APPEND_DATA((crc >> 24) % 256, &out, &outsize);
|
2021-08-28 17:01:08 -07:00
|
|
|
|
|
|
|
/* ISIZE */
|
2021-11-23 16:50:08 -08:00
|
|
|
ZOPFLI_APPEND_DATA(in_total % 256, &out, &outsize);
|
|
|
|
ZOPFLI_APPEND_DATA((in_total >> 8) % 256, &out, &outsize);
|
|
|
|
ZOPFLI_APPEND_DATA((in_total >> 16) % 256, &out, &outsize);
|
|
|
|
ZOPFLI_APPEND_DATA((in_total >> 24) % 256, &out, &outsize);
|
2021-08-28 17:01:08 -07:00
|
|
|
|
|
|
|
bwrite(out, outsize);
|
|
|
|
free(out);
|
2021-07-29 01:45:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:50:08 -08:00
|
|
|
protected:
|
2021-11-23 18:08:14 -08:00
|
|
|
bool write_chunk(const void *buf, size_t len, bool final) override {
|
2021-11-23 16:50:08 -08:00
|
|
|
auto in = static_cast<const unsigned char *>(buf);
|
|
|
|
|
|
|
|
in_total += len;
|
|
|
|
crc = crc32_z(crc, in, len);
|
|
|
|
|
|
|
|
ZopfliDeflatePart(&zo, 2, final, in, 0, len, &bp, &out, &outsize);
|
|
|
|
|
|
|
|
// ZOPFLI_APPEND_DATA is extremely dumb, so we always preserve the
|
|
|
|
// last byte to make sure that realloc is used instead of malloc
|
|
|
|
if (!bwrite(out, outsize - 1))
|
|
|
|
return false;
|
|
|
|
out[0] = out[outsize - 1];
|
|
|
|
outsize = 1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:45:09 +02:00
|
|
|
private:
|
2021-08-28 14:07:34 -07:00
|
|
|
ZopfliOptions zo;
|
|
|
|
unsigned char *out;
|
|
|
|
size_t outsize;
|
2021-11-23 16:50:08 -08:00
|
|
|
unsigned long crc;
|
|
|
|
uint32_t in_total;
|
2021-08-28 14:07:34 -07:00
|
|
|
unsigned char bp;
|
2021-07-29 01:45:09 +02:00
|
|
|
};
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
class bz_strm : public filter_out_stream {
|
2019-11-21 06:08:02 -05:00
|
|
|
public:
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) override {
|
2021-11-23 18:46:06 -08:00
|
|
|
return len == 0 || do_write(buf, len, BZ_RUN);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
~bz_strm() override {
|
|
|
|
switch(mode) {
|
|
|
|
case DECODE:
|
|
|
|
BZ2_bzDecompressEnd(&strm);
|
|
|
|
break;
|
|
|
|
case ENCODE:
|
2021-11-23 18:46:06 -08:00
|
|
|
do_write(nullptr, 0, BZ_FINISH);
|
2020-12-30 22:11:24 -08:00
|
|
|
BZ2_bzCompressEnd(&strm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-22 03:01:49 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
protected:
|
2020-12-30 22:11:24 -08:00
|
|
|
enum mode_t {
|
|
|
|
DECODE,
|
|
|
|
ENCODE
|
|
|
|
} mode;
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
bz_strm(mode_t mode, out_strm_ptr &&base) :
|
|
|
|
filter_out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} {
|
2020-12-30 22:11:24 -08:00
|
|
|
switch(mode) {
|
2021-11-21 04:38:22 -08:00
|
|
|
case DECODE:
|
|
|
|
BZ2_bzDecompressInit(&strm, 0, 0);
|
|
|
|
break;
|
|
|
|
case ENCODE:
|
|
|
|
BZ2_bzCompressInit(&strm, 9, 0, 0);
|
|
|
|
break;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 20:49:26 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
bz_stream strm;
|
|
|
|
char outbuf[CHUNK];
|
|
|
|
|
2021-11-23 18:46:06 -08:00
|
|
|
bool do_write(const void *buf, size_t len, int flush) {
|
2020-12-30 22:11:24 -08:00
|
|
|
strm.next_in = (char *) buf;
|
|
|
|
strm.avail_in = len;
|
|
|
|
do {
|
|
|
|
int code;
|
|
|
|
strm.avail_out = sizeof(outbuf);
|
|
|
|
strm.next_out = outbuf;
|
|
|
|
switch(mode) {
|
2021-11-21 04:38:22 -08:00
|
|
|
case DECODE:
|
|
|
|
code = BZ2_bzDecompress(&strm);
|
|
|
|
break;
|
|
|
|
case ENCODE:
|
|
|
|
code = BZ2_bzCompress(&strm, flush);
|
|
|
|
break;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
if (code < 0) {
|
|
|
|
LOGW("bzip2 %s failed (%d)\n", mode ? "encode" : "decode", code);
|
2021-11-21 04:38:22 -08:00
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2021-11-21 04:38:22 -08:00
|
|
|
if (!bwrite(outbuf, sizeof(outbuf) - strm.avail_out))
|
|
|
|
return false;
|
2022-12-13 11:33:16 +08:00
|
|
|
if (code == BZ_STREAM_END)
|
|
|
|
return true;
|
2020-12-30 22:11:24 -08:00
|
|
|
} while (strm.avail_out == 0);
|
2021-11-21 04:38:22 -08:00
|
|
|
return true;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
2019-02-20 20:49:26 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
class bz_decoder : public bz_strm {
|
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit bz_decoder(out_strm_ptr &&base) : bz_strm(DECODE, std::move(base)) {};
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
2019-02-20 20:49:26 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
class bz_encoder : public bz_strm {
|
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit bz_encoder(out_strm_ptr &&base) : bz_strm(ENCODE, std::move(base)) {};
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
2017-03-05 01:50:36 +08:00
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
class lzma_strm : public filter_out_stream {
|
2019-11-21 06:08:02 -05:00
|
|
|
public:
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) override {
|
2021-11-23 18:46:06 -08:00
|
|
|
return len == 0 || do_write(buf, len, LZMA_RUN);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2019-02-20 02:10:06 -05:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
~lzma_strm() override {
|
2021-11-23 18:46:06 -08:00
|
|
|
do_write(nullptr, 0, LZMA_FINISH);
|
2020-12-30 22:11:24 -08:00
|
|
|
lzma_end(&strm);
|
|
|
|
}
|
2019-11-22 03:01:49 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
protected:
|
2020-12-30 22:11:24 -08:00
|
|
|
enum mode_t {
|
|
|
|
DECODE,
|
|
|
|
ENCODE_XZ,
|
|
|
|
ENCODE_LZMA
|
|
|
|
} mode;
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
lzma_strm(mode_t mode, out_strm_ptr &&base) :
|
|
|
|
filter_out_stream(std::move(base)), mode(mode), strm(LZMA_STREAM_INIT), outbuf{0} {
|
2020-12-30 22:11:24 -08:00
|
|
|
lzma_options_lzma opt;
|
|
|
|
|
|
|
|
// Initialize preset
|
|
|
|
lzma_lzma_preset(&opt, 9);
|
|
|
|
lzma_filter filters[] = {
|
|
|
|
{ .id = LZMA_FILTER_LZMA2, .options = &opt },
|
|
|
|
{ .id = LZMA_VLI_UNKNOWN, .options = nullptr },
|
|
|
|
};
|
|
|
|
|
2021-05-10 18:38:30 -07:00
|
|
|
lzma_ret code;
|
2020-12-30 22:11:24 -08:00
|
|
|
switch(mode) {
|
2021-11-21 04:38:22 -08:00
|
|
|
case DECODE:
|
|
|
|
code = lzma_auto_decoder(&strm, UINT64_MAX, 0);
|
|
|
|
break;
|
|
|
|
case ENCODE_XZ:
|
|
|
|
code = lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32);
|
|
|
|
break;
|
|
|
|
case ENCODE_LZMA:
|
|
|
|
code = lzma_alone_encoder(&strm, &opt);
|
|
|
|
break;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2021-05-10 18:38:30 -07:00
|
|
|
if (code != LZMA_OK) {
|
|
|
|
LOGE("LZMA initialization failed (%d)\n", code);
|
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2019-02-20 02:10:06 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
lzma_stream strm;
|
|
|
|
uint8_t outbuf[CHUNK];
|
|
|
|
|
2021-11-23 18:46:06 -08:00
|
|
|
bool do_write(const void *buf, size_t len, lzma_action flush) {
|
2020-12-30 22:11:24 -08:00
|
|
|
strm.next_in = (uint8_t *) buf;
|
|
|
|
strm.avail_in = len;
|
|
|
|
do {
|
|
|
|
strm.avail_out = sizeof(outbuf);
|
|
|
|
strm.next_out = outbuf;
|
|
|
|
int code = lzma_code(&strm, flush);
|
|
|
|
if (code != LZMA_OK && code != LZMA_STREAM_END) {
|
|
|
|
LOGW("LZMA %s failed (%d)\n", mode ? "encode" : "decode", code);
|
2021-11-21 04:38:22 -08:00
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2021-11-21 04:38:22 -08:00
|
|
|
if (!bwrite(outbuf, sizeof(outbuf) - strm.avail_out))
|
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
} while (strm.avail_out == 0);
|
2021-11-21 04:38:22 -08:00
|
|
|
return true;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class lzma_decoder : public lzma_strm {
|
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit lzma_decoder(out_strm_ptr &&base) : lzma_strm(DECODE, std::move(base)) {}
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class xz_encoder : public lzma_strm {
|
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit xz_encoder(out_strm_ptr &&base) : lzma_strm(ENCODE_XZ, std::move(base)) {}
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class lzma_encoder : public lzma_strm {
|
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit lzma_encoder(out_strm_ptr &&base) : lzma_strm(ENCODE_LZMA, std::move(base)) {}
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
class LZ4F_decoder : public filter_out_stream {
|
2019-11-21 06:08:02 -05:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit LZ4F_decoder(out_strm_ptr &&base) :
|
|
|
|
filter_out_stream(std::move(base)), ctx(nullptr), outbuf(nullptr), outCapacity(0) {
|
2020-12-30 22:11:24 -08:00
|
|
|
LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
~LZ4F_decoder() override {
|
|
|
|
LZ4F_freeDecompressionContext(ctx);
|
|
|
|
delete[] outbuf;
|
|
|
|
}
|
|
|
|
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) override {
|
|
|
|
auto in = reinterpret_cast<const uint8_t *>(buf);
|
|
|
|
if (!outbuf) {
|
|
|
|
size_t read = len;
|
|
|
|
LZ4F_frameInfo_t info;
|
|
|
|
LZ4F_getFrameInfo(ctx, &info, in, &read);
|
|
|
|
switch (info.blockSizeID) {
|
|
|
|
case LZ4F_default:
|
|
|
|
case LZ4F_max64KB: outCapacity = 1 << 16; break;
|
|
|
|
case LZ4F_max256KB: outCapacity = 1 << 18; break;
|
|
|
|
case LZ4F_max1MB: outCapacity = 1 << 20; break;
|
|
|
|
case LZ4F_max4MB: outCapacity = 1 << 22; break;
|
|
|
|
}
|
|
|
|
outbuf = new uint8_t[outCapacity];
|
|
|
|
in += read;
|
|
|
|
len -= read;
|
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
size_t read, write;
|
|
|
|
LZ4F_errorCode_t code;
|
|
|
|
do {
|
|
|
|
read = len;
|
|
|
|
write = outCapacity;
|
2021-11-21 04:38:22 -08:00
|
|
|
code = LZ4F_decompress(ctx, outbuf, &write, in, &read, nullptr);
|
2020-12-30 22:11:24 -08:00
|
|
|
if (LZ4F_isError(code)) {
|
|
|
|
LOGW("LZ4F decode error: %s\n", LZ4F_getErrorName(code));
|
2021-11-21 04:38:22 -08:00
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
len -= read;
|
2021-11-21 04:38:22 -08:00
|
|
|
in += read;
|
|
|
|
if (!bwrite(outbuf, write))
|
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
} while (len != 0 || write != 0);
|
2021-11-21 04:38:22 -08:00
|
|
|
return true;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2019-11-21 06:08:02 -05:00
|
|
|
|
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
LZ4F_decompressionContext_t ctx;
|
|
|
|
uint8_t *outbuf;
|
|
|
|
size_t outCapacity;
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
2019-02-20 02:10:06 -05:00
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
class LZ4F_encoder : public filter_out_stream {
|
2019-11-21 06:08:02 -05:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit LZ4F_encoder(out_strm_ptr &&base) :
|
|
|
|
filter_out_stream(std::move(base)), ctx(nullptr), out_buf(nullptr), outCapacity(0) {
|
2020-12-30 22:11:24 -08:00
|
|
|
LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
|
|
|
|
}
|
|
|
|
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) override {
|
|
|
|
if (!out_buf) {
|
|
|
|
LZ4F_preferences_t prefs {
|
|
|
|
.frameInfo = {
|
|
|
|
.blockSizeID = LZ4F_max4MB,
|
|
|
|
.blockMode = LZ4F_blockIndependent,
|
|
|
|
.contentChecksumFlag = LZ4F_contentChecksumEnabled,
|
|
|
|
.blockChecksumFlag = LZ4F_noBlockChecksum,
|
|
|
|
},
|
|
|
|
.compressionLevel = 9,
|
|
|
|
.autoFlush = 1,
|
|
|
|
};
|
|
|
|
outCapacity = LZ4F_compressBound(BLOCK_SZ, &prefs);
|
|
|
|
out_buf = new uint8_t[outCapacity];
|
|
|
|
size_t write = LZ4F_compressBegin(ctx, out_buf, outCapacity, &prefs);
|
|
|
|
if (!bwrite(out_buf, write))
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
if (len == 0)
|
2021-11-21 04:38:22 -08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
auto in = reinterpret_cast<const uint8_t *>(buf);
|
2020-12-30 22:11:24 -08:00
|
|
|
size_t read, write;
|
|
|
|
do {
|
|
|
|
read = len > BLOCK_SZ ? BLOCK_SZ : len;
|
2021-11-21 04:38:22 -08:00
|
|
|
write = LZ4F_compressUpdate(ctx, out_buf, outCapacity, in, read, nullptr);
|
2020-12-30 22:11:24 -08:00
|
|
|
if (LZ4F_isError(write)) {
|
|
|
|
LOGW("LZ4F encode error: %s\n", LZ4F_getErrorName(write));
|
2021-11-21 04:38:22 -08:00
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
len -= read;
|
2021-11-21 04:38:22 -08:00
|
|
|
in += read;
|
|
|
|
if (!bwrite(out_buf, write))
|
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
} while (len != 0);
|
2021-11-21 04:38:22 -08:00
|
|
|
return true;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
~LZ4F_encoder() override {
|
2021-11-21 04:38:22 -08:00
|
|
|
size_t len = LZ4F_compressEnd(ctx, out_buf, outCapacity, nullptr);
|
2022-02-11 00:01:42 -08:00
|
|
|
if (LZ4F_isError(len)) {
|
|
|
|
LOGE("LZ4F end of frame error: %s\n", LZ4F_getErrorName(len));
|
|
|
|
} else if (!bwrite(out_buf, len)) {
|
|
|
|
LOGE("LZ4F end of frame error: I/O error\n");
|
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
LZ4F_freeCompressionContext(ctx);
|
2021-11-21 04:38:22 -08:00
|
|
|
delete[] out_buf;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2019-02-20 02:10:06 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
LZ4F_compressionContext_t ctx;
|
2021-11-21 04:38:22 -08:00
|
|
|
uint8_t *out_buf;
|
2020-12-30 22:11:24 -08:00
|
|
|
size_t outCapacity;
|
|
|
|
|
|
|
|
static constexpr size_t BLOCK_SZ = 1 << 22;
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
|
|
|
|
2021-11-21 04:38:22 -08:00
|
|
|
class LZ4_decoder : public chunk_out_stream {
|
2019-11-21 06:08:02 -05:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit LZ4_decoder(out_strm_ptr &&base) :
|
2022-02-10 23:49:17 -08:00
|
|
|
chunk_out_stream(std::move(base), LZ4_COMPRESSED, sizeof(block_sz)),
|
2021-11-21 04:38:22 -08:00
|
|
|
out_buf(new char[LZ4_UNCOMPRESSED]), block_sz(0) {}
|
2020-12-30 22:11:24 -08:00
|
|
|
|
|
|
|
~LZ4_decoder() override {
|
2021-11-23 16:50:08 -08:00
|
|
|
finalize();
|
2020-12-30 22:11:24 -08:00
|
|
|
delete[] out_buf;
|
|
|
|
}
|
|
|
|
|
2021-11-21 02:27:52 -08:00
|
|
|
protected:
|
2023-05-20 01:28:10 -07:00
|
|
|
bool write_chunk(const void *buf, size_t len, bool) override {
|
2021-11-21 02:27:52 -08:00
|
|
|
// This is an error
|
|
|
|
if (len != chunk_sz)
|
2021-11-21 04:38:22 -08:00
|
|
|
return false;
|
2021-11-21 02:27:52 -08:00
|
|
|
|
|
|
|
auto in = reinterpret_cast<const char *>(buf);
|
|
|
|
|
|
|
|
if (block_sz == 0) {
|
2022-02-10 23:49:17 -08:00
|
|
|
memcpy(&block_sz, in, sizeof(block_sz));
|
|
|
|
if (block_sz == 0x184C2102) {
|
|
|
|
// This is actually the lz4 magic, read the next 4 bytes
|
|
|
|
block_sz = 0;
|
|
|
|
chunk_sz = sizeof(block_sz);
|
|
|
|
return true;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2022-02-10 23:49:17 -08:00
|
|
|
// Read the next block chunk
|
2021-11-21 02:27:52 -08:00
|
|
|
chunk_sz = block_sz;
|
2021-11-21 04:38:22 -08:00
|
|
|
return true;
|
2021-11-21 02:27:52 -08:00
|
|
|
} else {
|
|
|
|
int r = LZ4_decompress_safe(in, out_buf, block_sz, LZ4_UNCOMPRESSED);
|
|
|
|
chunk_sz = sizeof(block_sz);
|
|
|
|
block_sz = 0;
|
|
|
|
if (r < 0) {
|
|
|
|
LOGW("LZ4HC decompression failure (%d)\n", r);
|
2021-11-21 04:38:22 -08:00
|
|
|
return false;
|
2021-11-21 02:27:52 -08:00
|
|
|
}
|
|
|
|
return bwrite(out_buf, r);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 02:10:06 -05:00
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
char *out_buf;
|
2021-11-21 02:27:52 -08:00
|
|
|
uint32_t block_sz;
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
|
|
|
|
2021-11-21 04:38:22 -08:00
|
|
|
class LZ4_encoder : public chunk_out_stream {
|
2019-11-21 06:08:02 -05:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
explicit LZ4_encoder(out_strm_ptr &&base, bool lg) :
|
2021-11-21 04:38:22 -08:00
|
|
|
chunk_out_stream(std::move(base), LZ4_UNCOMPRESSED),
|
|
|
|
out_buf(new char[LZ4_COMPRESSED]), lg(lg), in_total(0) {
|
2021-11-21 02:27:52 -08:00
|
|
|
bwrite("\x02\x21\x4c\x18", 4);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
~LZ4_encoder() override {
|
2021-11-23 16:50:08 -08:00
|
|
|
finalize();
|
2020-12-30 22:11:24 -08:00
|
|
|
if (lg)
|
|
|
|
bwrite(&in_total, sizeof(in_total));
|
2021-11-21 02:27:52 -08:00
|
|
|
delete[] out_buf;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2019-02-20 02:10:06 -05:00
|
|
|
|
2021-11-21 02:27:52 -08:00
|
|
|
protected:
|
2023-05-20 01:28:10 -07:00
|
|
|
bool write_chunk(const void *buf, size_t len, bool) override {
|
2021-11-23 18:08:14 -08:00
|
|
|
auto in = static_cast<const char *>(buf);
|
|
|
|
uint32_t block_sz = LZ4_compress_HC(in, out_buf, len, LZ4_COMPRESSED, LZ4HC_CLEVEL_MAX);
|
|
|
|
if (block_sz == 0) {
|
2020-12-30 22:11:24 -08:00
|
|
|
LOGW("LZ4HC compression failure\n");
|
2021-11-21 04:38:22 -08:00
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2021-11-23 18:08:14 -08:00
|
|
|
if (bwrite(&block_sz, sizeof(block_sz)) && bwrite(out_buf, block_sz)) {
|
2022-02-11 10:37:59 +08:00
|
|
|
in_total += len;
|
2021-11-23 18:08:14 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2021-11-21 02:27:52 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
char *out_buf;
|
|
|
|
bool lg;
|
2021-11-23 18:08:14 -08:00
|
|
|
uint32_t in_total;
|
2019-11-21 06:08:02 -05:00
|
|
|
};
|
2019-02-20 02:10:06 -05:00
|
|
|
|
2023-05-20 01:28:10 -07:00
|
|
|
out_strm_ptr get_encoder(format_t type, out_strm_ptr &&base) {
|
2020-12-30 22:11:24 -08:00
|
|
|
switch (type) {
|
|
|
|
case XZ:
|
|
|
|
return make_unique<xz_encoder>(std::move(base));
|
|
|
|
case LZMA:
|
|
|
|
return make_unique<lzma_encoder>(std::move(base));
|
|
|
|
case BZIP2:
|
|
|
|
return make_unique<bz_encoder>(std::move(base));
|
|
|
|
case LZ4:
|
|
|
|
return make_unique<LZ4F_encoder>(std::move(base));
|
|
|
|
case LZ4_LEGACY:
|
|
|
|
return make_unique<LZ4_encoder>(std::move(base), false);
|
|
|
|
case LZ4_LG:
|
|
|
|
return make_unique<LZ4_encoder>(std::move(base), true);
|
2021-08-28 17:16:20 -07:00
|
|
|
case ZOPFLI:
|
2021-08-28 14:07:34 -07:00
|
|
|
return make_unique<zopfli_encoder>(std::move(base));
|
2021-08-28 17:16:20 -07:00
|
|
|
case GZIP:
|
2020-12-30 22:11:24 -08:00
|
|
|
default:
|
|
|
|
return make_unique<gz_encoder>(std::move(base));
|
|
|
|
}
|
2019-02-20 02:10:06 -05:00
|
|
|
}
|
|
|
|
|
2023-05-20 01:28:10 -07:00
|
|
|
out_strm_ptr get_decoder(format_t type, out_strm_ptr &&base) {
|
2020-12-30 22:11:24 -08:00
|
|
|
switch (type) {
|
|
|
|
case XZ:
|
|
|
|
case LZMA:
|
|
|
|
return make_unique<lzma_decoder>(std::move(base));
|
|
|
|
case BZIP2:
|
|
|
|
return make_unique<bz_decoder>(std::move(base));
|
|
|
|
case LZ4:
|
|
|
|
return make_unique<LZ4F_decoder>(std::move(base));
|
|
|
|
case LZ4_LEGACY:
|
2021-02-07 17:40:59 -08:00
|
|
|
case LZ4_LG:
|
2020-12-30 22:11:24 -08:00
|
|
|
return make_unique<LZ4_decoder>(std::move(base));
|
2021-08-28 17:16:20 -07:00
|
|
|
case ZOPFLI:
|
2020-12-30 22:11:24 -08:00
|
|
|
case GZIP:
|
|
|
|
default:
|
|
|
|
return make_unique<gz_decoder>(std::move(base));
|
|
|
|
}
|
2019-02-20 02:10:06 -05:00
|
|
|
}
|
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
void decompress(char *infile, const char *outfile) {
|
2020-12-30 22:11:24 -08:00
|
|
|
bool in_std = infile == "-"sv;
|
|
|
|
bool rm_in = false;
|
|
|
|
|
|
|
|
FILE *in_fp = in_std ? stdin : xfopen(infile, "re");
|
2023-05-19 04:19:43 -07:00
|
|
|
out_strm_ptr strm;
|
2020-12-30 22:11:24 -08:00
|
|
|
|
|
|
|
char buf[4096];
|
|
|
|
size_t len;
|
|
|
|
while ((len = fread(buf, 1, sizeof(buf), in_fp))) {
|
|
|
|
if (!strm) {
|
|
|
|
format_t type = check_fmt(buf, len);
|
|
|
|
|
|
|
|
fprintf(stderr, "Detected format: [%s]\n", fmt2name[type]);
|
|
|
|
|
|
|
|
if (!COMPRESSED(type))
|
|
|
|
LOGE("Input file is not a supported compressed type!\n");
|
|
|
|
|
|
|
|
/* If user does not provide outfile, infile has to be either
|
|
|
|
* <path>.[ext], or '-'. Outfile will be either <path> or '-'.
|
|
|
|
* If the input does not have proper format, abort */
|
|
|
|
|
|
|
|
char *ext = nullptr;
|
|
|
|
if (outfile == nullptr) {
|
|
|
|
outfile = infile;
|
|
|
|
if (!in_std) {
|
|
|
|
ext = strrchr(infile, '.');
|
|
|
|
if (ext == nullptr || strcmp(ext, fmt2ext[type]) != 0)
|
|
|
|
LOGE("Input file is not a supported type!\n");
|
|
|
|
|
|
|
|
// Strip out extension and remove input
|
|
|
|
*ext = '\0';
|
|
|
|
rm_in = true;
|
|
|
|
fprintf(stderr, "Decompressing to [%s]\n", outfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we");
|
2023-05-19 04:19:43 -07:00
|
|
|
strm = get_decoder(type, make_unique<fp_channel>(out_fp));
|
2020-12-30 22:11:24 -08:00
|
|
|
if (ext) *ext = '.';
|
|
|
|
}
|
2021-11-21 04:38:22 -08:00
|
|
|
if (!strm->write(buf, len))
|
2020-12-30 22:11:24 -08:00
|
|
|
LOGE("Decompression error!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
strm.reset(nullptr);
|
|
|
|
fclose(in_fp);
|
|
|
|
|
|
|
|
if (rm_in)
|
|
|
|
unlink(infile);
|
2019-02-20 02:10:06 -05:00
|
|
|
}
|
|
|
|
|
2019-11-21 06:08:02 -05:00
|
|
|
void compress(const char *method, const char *infile, const char *outfile) {
|
2021-03-02 23:16:10 -08:00
|
|
|
format_t fmt = name2fmt[method];
|
|
|
|
if (fmt == UNKNOWN)
|
2020-12-30 22:11:24 -08:00
|
|
|
LOGE("Unknown compression method: [%s]\n", method);
|
|
|
|
|
|
|
|
bool in_std = infile == "-"sv;
|
|
|
|
bool rm_in = false;
|
|
|
|
|
|
|
|
FILE *in_fp = in_std ? stdin : xfopen(infile, "re");
|
|
|
|
FILE *out_fp;
|
|
|
|
|
|
|
|
if (outfile == nullptr) {
|
|
|
|
if (in_std) {
|
|
|
|
out_fp = stdout;
|
|
|
|
} else {
|
|
|
|
/* If user does not provide outfile and infile is not
|
|
|
|
* STDIN, output to <infile>.[ext] */
|
|
|
|
string tmp(infile);
|
2021-03-02 23:16:10 -08:00
|
|
|
tmp += fmt2ext[fmt];
|
2020-12-30 22:11:24 -08:00
|
|
|
out_fp = xfopen(tmp.data(), "we");
|
|
|
|
fprintf(stderr, "Compressing to [%s]\n", tmp.data());
|
|
|
|
rm_in = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we");
|
|
|
|
}
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
auto strm = get_encoder(fmt, make_unique<fp_channel>(out_fp));
|
2020-12-30 22:11:24 -08:00
|
|
|
|
|
|
|
char buf[4096];
|
|
|
|
size_t len;
|
|
|
|
while ((len = fread(buf, 1, sizeof(buf), in_fp))) {
|
2021-11-21 04:38:22 -08:00
|
|
|
if (!strm->write(buf, len))
|
2020-12-30 22:11:24 -08:00
|
|
|
LOGE("Compression error!\n");
|
2021-11-21 04:38:22 -08:00
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
|
|
|
|
strm.reset(nullptr);
|
|
|
|
fclose(in_fp);
|
|
|
|
|
|
|
|
if (rm_in)
|
|
|
|
unlink(infile);
|
2019-02-20 02:10:06 -05:00
|
|
|
}
|
2022-12-13 11:33:16 +08:00
|
|
|
|
2023-05-25 23:45:38 -07:00
|
|
|
bool decompress(rust::Slice<const uint8_t> buf, int fd) {
|
|
|
|
format_t type = check_fmt(buf.data(), buf.length());
|
2022-12-13 11:33:16 +08:00
|
|
|
|
|
|
|
if (!COMPRESSED(type)) {
|
2023-05-25 23:45:38 -07:00
|
|
|
LOGE("Input file is not a supported compression format!\n");
|
2022-12-13 11:33:16 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
auto strm = get_decoder(type, make_unique<fd_channel>(fd));
|
2023-05-25 23:45:38 -07:00
|
|
|
if (!strm->write(buf.data(), buf.length())) {
|
2022-12-13 11:33:16 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2023-12-08 23:30:55 +08:00
|
|
|
|
|
|
|
bool xz(rust::Slice<const uint8_t> buf, rust::Vec<uint8_t> &out) {
|
|
|
|
auto strm = get_encoder(XZ, make_unique<rust_vec_channel>(out));
|
|
|
|
if (!strm->write(buf.data(), buf.length())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unxz(rust::Slice<const uint8_t> buf, rust::Vec<uint8_t> &out) {
|
|
|
|
format_t type = check_fmt(buf.data(), buf.length());
|
|
|
|
if (type != XZ) {
|
|
|
|
LOGE("Input file is not in xz format!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto strm = get_decoder(XZ, make_unique<rust_vec_channel>(out));
|
|
|
|
if (!strm->write(buf.data(), buf.length())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|