Commit d67b8273 authored by topjohnwu's avatar topjohnwu

Rewrite compression with OOP

parent 660e0dc0
This diff is collapsed.
#pragma once
#include <functional>
#include <zlib.h>
#include <bzlib.h>
#include <lzma.h>
#include <lz4.h>
#include <lz4frame.h>
#include <lz4hc.h>
#include "format.h"
#define CHUNK 0x40000
class Compression {
public:
virtual ~Compression() = default;
void set_outfn(std::function<void(const void *, size_t)> &&fn);
void set_outfd(int fd);
int64_t one_step(int outfd, const void *in, size_t size);
virtual bool update(const void *in, size_t size) = 0;
virtual uint64_t finalize() = 0;
template <class T>
static int64_t one_step(int outfd, const void *in, size_t size) {
T cmp;
return cmp.one_step(outfd, in, size);
}
protected:
Compression();
std::function<void (const void*, size_t)> fn;
};
class GZStream : public Compression {
public:
bool update(const void *in, size_t size) override;
uint64_t finalize() override;
protected:
explicit GZStream(int mode);
private:
int mode;
z_stream strm;
uint8_t outbuf[CHUNK];
bool update(const void *in, size_t size, int flush);
};
class GZDecoder : public GZStream {
public:
GZDecoder() : GZStream(0) {};
};
class GZEncoder : public GZStream {
public:
GZEncoder() : GZStream(1) {};
};
class BZStream : public Compression {
public:
bool update(const void *in, size_t size) override;
uint64_t finalize() override;
protected:
explicit BZStream(int mode);
private:
int mode;
bz_stream strm;
char outbuf[CHUNK];
bool update(const void *in, size_t size, int flush);
};
class BZDecoder : public BZStream {
public:
BZDecoder() : BZStream(0) {};
};
class BZEncoder : public BZStream {
public:
BZEncoder() : BZStream(1) {};
};
class LZMAStream : public Compression {
public:
bool update(const void *in, size_t size) override;
uint64_t finalize() override;
protected:
explicit LZMAStream(int mode);
private:
int mode;
lzma_stream strm;
uint8_t outbuf[CHUNK];
bool update(const void *in, size_t size, lzma_action flush);
};
class LZMADecoder : public LZMAStream {
public:
LZMADecoder() : LZMAStream(0) {}
};
class XZEncoder : public LZMAStream {
public:
XZEncoder() : LZMAStream(1) {}
};
class LZMAEncoder : public LZMAStream {
public:
LZMAEncoder() : LZMAStream(2) {}
};
class LZ4FDecoder : public Compression {
public:
LZ4FDecoder();
~LZ4FDecoder() override;
bool update(const void *in, size_t size) override;
uint64_t finalize() override;
private:
LZ4F_decompressionContext_t ctx;
uint8_t *outbuf;
size_t outCapacity;
uint64_t total;
void read_header(const uint8_t *&in, size_t &size);
};
class LZ4FEncoder : public Compression {
public:
LZ4FEncoder();
~LZ4FEncoder() override;
bool update(const void *in, size_t size) override;
uint64_t finalize() override;
private:
LZ4F_compressionContext_t ctx;
uint8_t *outbuf;
size_t outCapacity;
uint64_t total;
void write_header();
};
#define LZ4_UNCOMPRESSED 0x800000
#define LZ4_COMPRESSED LZ4_COMPRESSBOUND(LZ4_UNCOMPRESSED)
class LZ4Decoder : public Compression {
public:
LZ4Decoder();
~LZ4Decoder() override;
bool update(const void *in, size_t size) override;
uint64_t finalize() override;
private:
char *outbuf;
char *buf;
bool init;
unsigned block_sz;
int buf_off;
uint64_t total;
};
class LZ4Encoder : public Compression {
public:
LZ4Encoder();
~LZ4Encoder() override;
bool update(const void *in, size_t size) override;
uint64_t finalize() override;
private:
char *outbuf;
char *buf;
bool init;
int buf_off;
uint64_t out_total;
unsigned in_total;
};
Compression *get_encoder(format_t type);
Compression *get_decoder(format_t type);
...@@ -23,13 +23,8 @@ void decompress(char *from, const char *to); ...@@ -23,13 +23,8 @@ void decompress(char *from, const char *to);
int dtb_commands(const char *cmd, int argc, char *argv[]); int dtb_commands(const char *cmd, int argc, char *argv[]);
// Compressions // Compressions
size_t gzip(int mode, int fd, const void *buf, size_t size); int64_t compress(format_t type, int fd, const void *from, size_t size);
size_t lzma(int mode, int fd, const void *buf, size_t size); int64_t decompress(format_t type, int fd, const void *from, size_t size);
size_t lz4(int mode, int fd, const uint8_t *buf, size_t size);
size_t bzip2(int mode, int fd, const void *buf, size_t size);
size_t lz4_legacy(int mode, int fd, const uint8_t *buf, size_t size);
long long compress(format_t type, int fd, const void *from, size_t size);
long long decompress(format_t type, int fd, const void *from, size_t size);
// Pattern // Pattern
int patch_verity(void **buf, uint32_t *size, int patch); int patch_verity(void **buf, uint32_t *size, int patch);
......
...@@ -139,13 +139,13 @@ int main(int argc, char *argv[]) { ...@@ -139,13 +139,13 @@ int main(int argc, char *argv[]) {
} else if (argc > 2 && strcmp(argv[1], "--repack") == 0) { } else if (argc > 2 && strcmp(argv[1], "--repack") == 0) {
repack(argv[2], argc > 3 ? argv[3] : NEW_BOOT); repack(argv[2], argc > 3 ? argv[3] : NEW_BOOT);
} else if (argc > 2 && strcmp(argv[1], "--decompress") == 0) { } else if (argc > 2 && strcmp(argv[1], "--decompress") == 0) {
decompress(argv[2], argc > 3 ? argv[3] : NULL); decompress(argv[2], argc > 3 ? argv[3] : nullptr);
} else if (argc > 2 && strncmp(argv[1], "--compress", 10) == 0) { } else if (argc > 2 && strncmp(argv[1], "--compress", 10) == 0) {
const char *method; const char *method;
method = strchr(argv[1], '='); method = strchr(argv[1], '=');
if (method == NULL) method = "gzip"; if (method == nullptr) method = "gzip";
else method++; else method++;
compress(method, argv[2], argc > 3 ? argv[3] : NULL); compress(method, argv[2], argc > 3 ? argv[3] : nullptr);
} else if (argc > 4 && strcmp(argv[1], "--hexpatch") == 0) { } else if (argc > 4 && strcmp(argv[1], "--hexpatch") == 0) {
hexpatch(argv[2], argv[3], argv[4]); hexpatch(argv[2], argv[3], argv[4]);
} else if (argc > 2 && strcmp(argv[1], "--cpio") == 0) { } else if (argc > 2 && strcmp(argv[1], "--cpio") == 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment