mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-15 18:10:23 +00:00
Migrate to generic stream implementation
This commit is contained in:
@@ -21,19 +21,18 @@ using namespace std;
|
||||
uint32_t dyn_img_hdr::j32 = 0;
|
||||
uint64_t dyn_img_hdr::j64 = 0;
|
||||
|
||||
static int64_t one_step(unique_ptr<Compression> &&ptr, int fd, const void *in, size_t size) {
|
||||
ptr->setOut(make_unique<FDOutStream>(fd));
|
||||
if (!ptr->write(in, size))
|
||||
return -1;
|
||||
return ptr->finalize();
|
||||
}
|
||||
|
||||
static int64_t decompress(format_t type, int fd, const void *in, size_t size) {
|
||||
return one_step(unique_ptr<Compression>(get_decoder(type)), fd, in, size);
|
||||
static void decompress(format_t type, int fd, const void *in, size_t size) {
|
||||
unique_ptr<stream> ptr(get_decoder(type, open_stream<fd_stream>(fd)));
|
||||
ptr->write(in, size);
|
||||
}
|
||||
|
||||
static int64_t compress(format_t type, int fd, const void *in, size_t size) {
|
||||
return one_step(unique_ptr<Compression>(get_encoder(type)), fd, in, size);
|
||||
auto prev = lseek(fd, 0, SEEK_CUR);
|
||||
unique_ptr<stream> ptr(get_encoder(type, open_stream<fd_stream>(fd)));
|
||||
ptr->write(in, size);
|
||||
ptr->close();
|
||||
auto now = lseek(fd, 0, SEEK_CUR);
|
||||
return now - prev;
|
||||
}
|
||||
|
||||
static void dump(void *buf, size_t size, const char *filename) {
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,174 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <zlib.h>
|
||||
#include <bzlib.h>
|
||||
#include <lzma.h>
|
||||
#include <lz4.h>
|
||||
#include <lz4frame.h>
|
||||
#include <lz4hc.h>
|
||||
#include <stream.h>
|
||||
|
||||
#include "format.h"
|
||||
|
||||
#define CHUNK 0x40000
|
||||
|
||||
class Compression : public FilterOutStream {
|
||||
public:
|
||||
virtual uint64_t finalize() = 0;
|
||||
};
|
||||
|
||||
class GZStream : public Compression {
|
||||
public:
|
||||
bool write(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 write(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 write(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 write(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 write(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 write(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 write(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 write(const void *in, size_t size) override;
|
||||
uint64_t finalize() override;
|
||||
|
||||
private:
|
||||
static constexpr size_t BLOCK_SZ = 1 << 22;
|
||||
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 write(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 write(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);
|
||||
filter_stream *get_encoder(format_t type, FILE *fp = nullptr);
|
||||
filter_stream *get_decoder(format_t type, FILE *fp = nullptr);
|
||||
void compress(const char *method, const char *infile, const char *outfile);
|
||||
void decompress(char *infile, const char *outfile);
|
||||
|
@@ -241,14 +241,17 @@ void magisk_cpio::compress() {
|
||||
return;
|
||||
fprintf(stderr, "Compressing cpio -> [%s]\n", RAMDISK_XZ);
|
||||
auto init = entries.extract("init");
|
||||
XZEncoder encoder;
|
||||
encoder.setOut(make_unique<BufOutStream>());
|
||||
output(encoder);
|
||||
encoder.finalize();
|
||||
|
||||
uint8_t *data;
|
||||
size_t len;
|
||||
FILE *fp = open_stream(get_encoder(XZ, open_stream<byte_stream>(data, len)));
|
||||
dump(fp);
|
||||
|
||||
entries.clear();
|
||||
entries.insert(std::move(init));
|
||||
auto xz = new cpio_entry(RAMDISK_XZ, S_IFREG);
|
||||
static_cast<BufOutStream *>(encoder.getOut())->release(xz->data, xz->filesize);
|
||||
xz->data = data;
|
||||
xz->filesize = len;
|
||||
insert(xz);
|
||||
}
|
||||
|
||||
@@ -257,15 +260,16 @@ void magisk_cpio::decompress() {
|
||||
if (it == entries.end())
|
||||
return;
|
||||
fprintf(stderr, "Decompressing cpio [%s]\n", RAMDISK_XZ);
|
||||
LZMADecoder decoder;
|
||||
decoder.setOut(make_unique<BufOutStream>());
|
||||
decoder.write(it->second->data, it->second->filesize);
|
||||
decoder.finalize();
|
||||
|
||||
char *data;
|
||||
size_t len;
|
||||
auto strm = get_decoder(XZ, open_stream<byte_stream>(data, len));
|
||||
strm->write(it->second->data, it->second->filesize);
|
||||
delete strm;
|
||||
|
||||
entries.erase(it);
|
||||
char *buf;
|
||||
size_t sz;
|
||||
static_cast<BufOutStream *>(decoder.getOut())->getbuf(buf, sz);
|
||||
load_cpio(buf, sz);
|
||||
load_cpio(data, len);
|
||||
free(data);
|
||||
}
|
||||
|
||||
int cpio_commands(int argc, char *argv[]) {
|
||||
@@ -338,4 +342,4 @@ int cpio_commands(int argc, char *argv[]) {
|
||||
|
||||
cpio.dump(incpio);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user