mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-06-12 13:17:39 +02:00
Migrate to generic stream implementation
This commit is contained in:
@ -30,7 +30,7 @@ struct cpio_entry : public cpio_entry_base {
|
||||
explicit cpio_entry(const char *name, uint32_t mode) : filename(name) {
|
||||
this->mode = mode;
|
||||
}
|
||||
explicit cpio_entry(cpio_newc_header *h) : cpio_entry_base(h) {}
|
||||
explicit cpio_entry(const cpio_newc_header *h) : cpio_entry_base(h) {}
|
||||
|
||||
~cpio_entry() override { free(data); };
|
||||
};
|
||||
@ -48,7 +48,7 @@ public:
|
||||
protected:
|
||||
entry_map entries;
|
||||
void rm(entry_map::iterator &it);
|
||||
void output(OutStream &out);
|
||||
void dump(FILE *out);
|
||||
};
|
||||
|
||||
class cpio_rw : public cpio {
|
||||
@ -64,7 +64,7 @@ public:
|
||||
protected:
|
||||
void insert(cpio_entry *e);
|
||||
void mv(entry_map::iterator &it, const char *to);
|
||||
void load_cpio(char *buf, size_t sz);
|
||||
void load_cpio(const char *buf, size_t sz);
|
||||
};
|
||||
|
||||
class cpio_mmap : public cpio {
|
||||
|
@ -15,8 +15,6 @@ FILE *open_stream(Args &&... args) {
|
||||
return open_stream(new T(args...));
|
||||
}
|
||||
|
||||
/* Base classes */
|
||||
|
||||
class stream {
|
||||
public:
|
||||
virtual int read(void *buf, size_t len);
|
||||
@ -26,17 +24,17 @@ public:
|
||||
virtual ~stream() = default;
|
||||
};
|
||||
|
||||
// Delegates all operations to the base FILE pointer
|
||||
class filter_stream : public stream {
|
||||
public:
|
||||
filter_stream(FILE *fp) : fp(fp) {}
|
||||
int close() override { return fclose(fp); }
|
||||
virtual ~filter_stream() { close(); }
|
||||
~filter_stream() override { if (fp) close(); }
|
||||
|
||||
void set_base(FILE *f) {
|
||||
if (fp) fclose(fp);
|
||||
fp = f;
|
||||
}
|
||||
int read(void *buf, size_t len) override;
|
||||
int write(const void *buf, size_t len) override;
|
||||
int close() override;
|
||||
|
||||
void set_base(FILE *f);
|
||||
template <class T, class... Args >
|
||||
void set_base(Args&&... args) {
|
||||
set_base(open_stream<T>(args...));
|
||||
@ -46,18 +44,7 @@ protected:
|
||||
FILE *fp;
|
||||
};
|
||||
|
||||
class filter_in_stream : public filter_stream {
|
||||
public:
|
||||
filter_in_stream(FILE *fp = nullptr) : filter_stream(fp) {}
|
||||
int read(void *buf, size_t len) override { return fread(buf, len, 1, fp); }
|
||||
};
|
||||
|
||||
class filter_out_stream : public filter_stream {
|
||||
public:
|
||||
filter_out_stream(FILE *fp = nullptr) : filter_stream(fp) {}
|
||||
int write(const void *buf, size_t len) override { return fwrite(buf, len, 1, fp); }
|
||||
};
|
||||
|
||||
// Handy interface for classes that need custom seek logic
|
||||
class seekable_stream : public stream {
|
||||
protected:
|
||||
size_t _pos = 0;
|
||||
@ -66,8 +53,7 @@ protected:
|
||||
virtual size_t end_pos() = 0;
|
||||
};
|
||||
|
||||
/* Concrete classes */
|
||||
|
||||
// Byte stream that dynamically allocates memory
|
||||
class byte_stream : public seekable_stream {
|
||||
public:
|
||||
byte_stream(uint8_t *&buf, size_t &len);
|
||||
@ -76,7 +62,6 @@ public:
|
||||
int read(void *buf, size_t len) override;
|
||||
int write(const void *buf, size_t len) override;
|
||||
off_t seek(off_t off, int whence) override;
|
||||
virtual ~byte_stream() = default;
|
||||
|
||||
private:
|
||||
uint8_t *&_buf;
|
||||
@ -87,101 +72,14 @@ private:
|
||||
size_t end_pos() override { return _len; }
|
||||
};
|
||||
|
||||
class fd_stream : stream {
|
||||
// File stream but does not close the file descriptor at any time
|
||||
class fd_stream : public stream {
|
||||
public:
|
||||
fd_stream(int fd) : fd(fd) {}
|
||||
int read(void *buf, size_t len) override;
|
||||
int write(const void *buf, size_t len) override;
|
||||
off_t seek(off_t off, int whence) override;
|
||||
virtual ~fd_stream() = default;
|
||||
|
||||
private:
|
||||
int fd;
|
||||
};
|
||||
|
||||
/* TODO: Replace classes below to new implementation */
|
||||
|
||||
class OutStream {
|
||||
public:
|
||||
virtual bool write(const void *buf, size_t len) = 0;
|
||||
virtual ~OutStream() = default;
|
||||
};
|
||||
|
||||
typedef std::unique_ptr<OutStream> strm_ptr;
|
||||
|
||||
class FilterOutStream : public OutStream {
|
||||
public:
|
||||
FilterOutStream() = default;
|
||||
|
||||
FilterOutStream(strm_ptr &&ptr) : out(std::move(ptr)) {}
|
||||
|
||||
void setOut(strm_ptr &&ptr) { out = std::move(ptr); }
|
||||
|
||||
OutStream *getOut() { return out.get(); }
|
||||
|
||||
bool write(const void *buf, size_t len) override {
|
||||
return out ? out->write(buf, len) : false;
|
||||
}
|
||||
|
||||
protected:
|
||||
strm_ptr out;
|
||||
};
|
||||
|
||||
class FDOutStream : public OutStream {
|
||||
public:
|
||||
FDOutStream(int fd, bool close = false) : fd(fd), close(close) {}
|
||||
|
||||
bool write(const void *buf, size_t len) override {
|
||||
return ::write(fd, buf, len) == len;
|
||||
}
|
||||
|
||||
~FDOutStream() override {
|
||||
if (close)
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
protected:
|
||||
int fd;
|
||||
bool close;
|
||||
};
|
||||
|
||||
class BufOutStream : public OutStream {
|
||||
public:
|
||||
BufOutStream() : buf(nullptr), off(0), cap(0) {};
|
||||
|
||||
bool write(const void *b, size_t len) override {
|
||||
bool resize = false;
|
||||
while (off + len > cap) {
|
||||
cap = cap ? cap << 1 : 1 << 19;
|
||||
resize = true;
|
||||
}
|
||||
if (resize)
|
||||
buf = (char *) xrealloc(buf, cap);
|
||||
memcpy(buf + off, b, len);
|
||||
off += len;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename bytes, typename length>
|
||||
void release(bytes *&b, length &len) {
|
||||
b = buf;
|
||||
len = off;
|
||||
buf = nullptr;
|
||||
off = cap = 0;
|
||||
}
|
||||
|
||||
template <typename bytes, typename length>
|
||||
void getbuf(bytes *&b, length &len) const {
|
||||
b = buf;
|
||||
len = off;
|
||||
}
|
||||
|
||||
~BufOutStream() override {
|
||||
free(buf);
|
||||
}
|
||||
|
||||
protected:
|
||||
char *buf;
|
||||
size_t off;
|
||||
size_t cap;
|
||||
};
|
||||
|
Reference in New Issue
Block a user