You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Split input and output streams
This commit is contained in:
@@ -6,50 +6,39 @@
|
||||
|
||||
#include "../files.hpp"
|
||||
|
||||
class stream {
|
||||
public:
|
||||
virtual ssize_t read(void *buf, size_t len);
|
||||
virtual ssize_t readFully(void *buf, size_t len);
|
||||
virtual ssize_t readv(const iovec *iov, int iovcnt);
|
||||
virtual bool write(const void *buf, size_t len);
|
||||
struct out_stream {
|
||||
virtual bool write(const void *buf, size_t len) = 0;
|
||||
virtual ssize_t writev(const iovec *iov, int iovcnt);
|
||||
virtual off_t seek(off_t off, int whence);
|
||||
virtual ~stream() = default;
|
||||
virtual ~out_stream() = default;
|
||||
};
|
||||
|
||||
using stream_ptr = std::unique_ptr<stream>;
|
||||
using out_strm_ptr = std::unique_ptr<out_stream>;
|
||||
|
||||
// Delegates all operations to base stream
|
||||
class filter_stream : public stream {
|
||||
class filter_out_stream : public out_stream {
|
||||
public:
|
||||
filter_stream(stream_ptr &&base) : base(std::move(base)) {}
|
||||
filter_out_stream(out_strm_ptr &&base) : base(std::move(base)) {}
|
||||
|
||||
ssize_t read(void *buf, size_t len) override;
|
||||
bool write(const void *buf, size_t len) override;
|
||||
virtual bool write(const void *buf, size_t len, bool final);
|
||||
|
||||
// Seeking while filtering does not make sense
|
||||
off_t seek(off_t off, int whence) final { return stream::seek(off, whence); }
|
||||
|
||||
protected:
|
||||
stream_ptr base;
|
||||
out_strm_ptr base;
|
||||
};
|
||||
|
||||
using filter_strm_ptr = std::unique_ptr<filter_stream>;
|
||||
using filter_strm_ptr = std::unique_ptr<filter_out_stream>;
|
||||
|
||||
// Buffered output stream, writing in chunks
|
||||
class chunk_out_stream : public filter_stream {
|
||||
class chunk_out_stream : public filter_out_stream {
|
||||
public:
|
||||
chunk_out_stream(stream_ptr &&base, size_t buf_sz, size_t chunk_sz)
|
||||
: filter_stream(std::move(base)), chunk_sz(chunk_sz), buf_sz(buf_sz) {}
|
||||
chunk_out_stream(out_strm_ptr &&base, size_t buf_sz, size_t chunk_sz)
|
||||
: filter_out_stream(std::move(base)), chunk_sz(chunk_sz), buf_sz(buf_sz) {}
|
||||
|
||||
chunk_out_stream(stream_ptr &&base, size_t buf_sz = 4096)
|
||||
chunk_out_stream(out_strm_ptr &&base, size_t buf_sz = 4096)
|
||||
: chunk_out_stream(std::move(base), buf_sz, buf_sz) {}
|
||||
|
||||
~chunk_out_stream() override { delete[] _buf; }
|
||||
|
||||
// Reading does not make sense
|
||||
ssize_t read(void *buf, size_t len) final { return stream::read(buf, len); }
|
||||
bool write(const void *buf, size_t len) final;
|
||||
bool write(const void *buf, size_t len, bool final) final;
|
||||
|
||||
@@ -66,12 +55,27 @@ private:
|
||||
uint8_t *_buf = nullptr;
|
||||
};
|
||||
|
||||
// Byte stream that dynamically allocates memory
|
||||
class byte_stream : public stream {
|
||||
struct in_stream {
|
||||
virtual ssize_t read(void *buf, size_t len) = 0;
|
||||
virtual ssize_t readFully(void *buf, size_t len);
|
||||
virtual ssize_t readv(const iovec *iov, int iovcnt);
|
||||
virtual ~in_stream() = default;
|
||||
};
|
||||
|
||||
// A channel is something that is writable, readable, and seekable
|
||||
struct channel : public in_stream, public out_stream {
|
||||
virtual off_t seek(off_t off, int whence) = 0;
|
||||
virtual ~channel() = default;
|
||||
};
|
||||
|
||||
using channel_ptr = std::unique_ptr<channel>;
|
||||
|
||||
// Byte channel that dynamically allocates memory
|
||||
class byte_channel : public channel {
|
||||
public:
|
||||
byte_stream(uint8_t *&buf, size_t &len);
|
||||
byte_channel(uint8_t *&buf, size_t &len);
|
||||
template <class Byte>
|
||||
byte_stream(Byte *&buf, size_t &len) : byte_stream(reinterpret_cast<uint8_t *&>(buf), len) {}
|
||||
byte_channel(Byte *&buf, size_t &len) : byte_channel(reinterpret_cast<uint8_t *&>(buf), len) {}
|
||||
|
||||
ssize_t read(void *buf, size_t len) override;
|
||||
bool write(const void *buf, size_t len) override;
|
||||
@@ -86,17 +90,17 @@ private:
|
||||
void resize(size_t new_pos, bool zero = false);
|
||||
};
|
||||
|
||||
class file_stream : public stream {
|
||||
class file_channel : public channel {
|
||||
public:
|
||||
bool write(const void *buf, size_t len) final;
|
||||
protected:
|
||||
virtual ssize_t do_write(const void *buf, size_t len) = 0;
|
||||
};
|
||||
|
||||
// File stream but does not close the file descriptor at any time
|
||||
class fd_stream : public file_stream {
|
||||
// File channel but does not close the file descriptor at any time
|
||||
class fd_channel : public file_channel {
|
||||
public:
|
||||
fd_stream(int fd) : fd(fd) {}
|
||||
fd_channel(int fd) : fd(fd) {}
|
||||
ssize_t read(void *buf, size_t len) override;
|
||||
ssize_t readv(const iovec *iov, int iovcnt) override;
|
||||
ssize_t writev(const iovec *iov, int iovcnt) override;
|
||||
@@ -108,14 +112,14 @@ private:
|
||||
};
|
||||
|
||||
/* ****************************************
|
||||
* Bridge between stream class and C stdio
|
||||
* Bridge between channel class and C stdio
|
||||
* ****************************************/
|
||||
|
||||
// sFILE -> stream_ptr
|
||||
class fp_stream final : public file_stream {
|
||||
// sFILE -> channel_ptr
|
||||
class fp_channel final : public file_channel {
|
||||
public:
|
||||
fp_stream(FILE *fp = nullptr) : fp(fp, fclose) {}
|
||||
fp_stream(sFILE &&fp) : fp(std::move(fp)) {}
|
||||
fp_channel(FILE *fp = nullptr) : fp(fp, fclose) {}
|
||||
fp_channel(sFILE &&fp) : fp(std::move(fp)) {}
|
||||
ssize_t read(void *buf, size_t len) override;
|
||||
off_t seek(off_t off, int whence) override;
|
||||
protected:
|
||||
@@ -124,10 +128,10 @@ private:
|
||||
sFILE fp;
|
||||
};
|
||||
|
||||
// stream_ptr -> sFILE
|
||||
sFILE make_stream_fp(stream_ptr &&strm);
|
||||
// channel_ptr -> sFILE
|
||||
sFILE make_channel_fp(channel_ptr &&strm);
|
||||
|
||||
template <class T, class... Args>
|
||||
sFILE make_stream_fp(Args &&... args) {
|
||||
return make_stream_fp(stream_ptr(new T(std::forward<Args>(args)...)));
|
||||
sFILE make_channel_fp(Args &&... args) {
|
||||
return make_channel_fp(channel_ptr(new T(std::forward<Args>(args)...)));
|
||||
}
|
||||
|
||||
+24
-43
@@ -7,40 +7,35 @@
|
||||
using namespace std;
|
||||
|
||||
static int strm_read(void *v, char *buf, int len) {
|
||||
auto strm = static_cast<stream *>(v);
|
||||
auto strm = static_cast<channel *>(v);
|
||||
return strm->read(buf, len);
|
||||
}
|
||||
|
||||
static int strm_write(void *v, const char *buf, int len) {
|
||||
auto strm = static_cast<stream *>(v);
|
||||
auto strm = static_cast<channel *>(v);
|
||||
if (!strm->write(buf, len))
|
||||
return -1;
|
||||
return len;
|
||||
}
|
||||
|
||||
static fpos_t strm_seek(void *v, fpos_t off, int whence) {
|
||||
auto strm = static_cast<stream *>(v);
|
||||
auto strm = static_cast<channel *>(v);
|
||||
return strm->seek(off, whence);
|
||||
}
|
||||
|
||||
static int strm_close(void *v) {
|
||||
auto strm = static_cast<stream *>(v);
|
||||
auto strm = static_cast<channel *>(v);
|
||||
delete strm;
|
||||
return 0;
|
||||
}
|
||||
|
||||
sFILE make_stream_fp(stream_ptr &&strm) {
|
||||
sFILE make_channel_fp(channel_ptr &&strm) {
|
||||
auto fp = make_file(funopen(strm.release(), strm_read, strm_write, strm_seek, strm_close));
|
||||
setbuf(fp.get(), nullptr);
|
||||
return fp;
|
||||
}
|
||||
|
||||
ssize_t stream::read(void *buf, size_t len) {
|
||||
LOGE("This stream does not implement read\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t stream::readFully(void *buf, size_t len) {
|
||||
ssize_t in_stream::readFully(void *buf, size_t len) {
|
||||
size_t read_sz = 0;
|
||||
ssize_t ret;
|
||||
do {
|
||||
@@ -55,7 +50,7 @@ ssize_t stream::readFully(void *buf, size_t len) {
|
||||
return read_sz;
|
||||
}
|
||||
|
||||
ssize_t stream::readv(const iovec *iov, int iovcnt) {
|
||||
ssize_t in_stream::readv(const iovec *iov, int iovcnt) {
|
||||
size_t read_sz = 0;
|
||||
for (int i = 0; i < iovcnt; ++i) {
|
||||
auto ret = readFully(iov[i].iov_base, iov[i].iov_len);
|
||||
@@ -66,12 +61,7 @@ ssize_t stream::readv(const iovec *iov, int iovcnt) {
|
||||
return read_sz;
|
||||
}
|
||||
|
||||
bool stream::write(const void *buf, size_t len) {
|
||||
LOGE("This stream does not implement write\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
ssize_t stream::writev(const iovec *iov, int iovcnt) {
|
||||
ssize_t out_stream::writev(const iovec *iov, int iovcnt) {
|
||||
size_t write_sz = 0;
|
||||
for (int i = 0; i < iovcnt; ++i) {
|
||||
if (!write(iov[i].iov_base, iov[i].iov_len))
|
||||
@@ -81,33 +71,24 @@ ssize_t stream::writev(const iovec *iov, int iovcnt) {
|
||||
return write_sz;
|
||||
}
|
||||
|
||||
off_t stream::seek(off_t off, int whence) {
|
||||
LOGE("This stream does not implement seek\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t fp_stream::read(void *buf, size_t len) {
|
||||
ssize_t fp_channel::read(void *buf, size_t len) {
|
||||
auto ret = fread(buf, 1, len, fp.get());
|
||||
return ret ? ret : (ferror(fp.get()) ? -1 : 0);
|
||||
}
|
||||
|
||||
ssize_t fp_stream::do_write(const void *buf, size_t len) {
|
||||
ssize_t fp_channel::do_write(const void *buf, size_t len) {
|
||||
return fwrite(buf, 1, len, fp.get());
|
||||
}
|
||||
|
||||
off_t fp_stream::seek(off_t off, int whence) {
|
||||
off_t fp_channel::seek(off_t off, int whence) {
|
||||
return fseek(fp.get(), off, whence);
|
||||
}
|
||||
|
||||
ssize_t filter_stream::read(void *buf, size_t len) {
|
||||
return base->read(buf, len);
|
||||
}
|
||||
|
||||
bool filter_stream::write(const void *buf, size_t len) {
|
||||
bool filter_out_stream::write(const void *buf, size_t len) {
|
||||
return base->write(buf, len);
|
||||
}
|
||||
|
||||
bool filter_stream::write(const void *buf, size_t len, bool final) {
|
||||
bool filter_out_stream::write(const void *buf, size_t len, bool final) {
|
||||
return write(buf, len);
|
||||
}
|
||||
|
||||
@@ -172,18 +153,18 @@ void chunk_out_stream::finalize() {
|
||||
}
|
||||
}
|
||||
|
||||
byte_stream::byte_stream(uint8_t *&buf, size_t &len) : _buf(buf), _len(len) {
|
||||
byte_channel::byte_channel(uint8_t *&buf, size_t &len) : _buf(buf), _len(len) {
|
||||
buf = nullptr;
|
||||
len = 0;
|
||||
}
|
||||
|
||||
ssize_t byte_stream::read(void *buf, size_t len) {
|
||||
ssize_t byte_channel::read(void *buf, size_t len) {
|
||||
len = std::min((size_t) len, _len - _pos);
|
||||
memcpy(buf, _buf + _pos, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
bool byte_stream::write(const void *buf, size_t len) {
|
||||
bool byte_channel::write(const void *buf, size_t len) {
|
||||
resize(_pos + len);
|
||||
memcpy(_buf + _pos, buf, len);
|
||||
_pos += len;
|
||||
@@ -191,7 +172,7 @@ bool byte_stream::write(const void *buf, size_t len) {
|
||||
return true;
|
||||
}
|
||||
|
||||
off_t byte_stream::seek(off_t off, int whence) {
|
||||
off_t byte_channel::seek(off_t off, int whence) {
|
||||
off_t np;
|
||||
switch (whence) {
|
||||
case SEEK_CUR:
|
||||
@@ -211,7 +192,7 @@ off_t byte_stream::seek(off_t off, int whence) {
|
||||
return np;
|
||||
}
|
||||
|
||||
void byte_stream::resize(size_t new_pos, bool zero) {
|
||||
void byte_channel::resize(size_t new_pos, bool zero) {
|
||||
bool resize = false;
|
||||
size_t old_cap = _cap;
|
||||
while (new_pos > _cap) {
|
||||
@@ -225,27 +206,27 @@ void byte_stream::resize(size_t new_pos, bool zero) {
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t fd_stream::read(void *buf, size_t len) {
|
||||
ssize_t fd_channel::read(void *buf, size_t len) {
|
||||
return ::read(fd, buf, len);
|
||||
}
|
||||
|
||||
ssize_t fd_stream::readv(const iovec *iov, int iovcnt) {
|
||||
ssize_t fd_channel::readv(const iovec *iov, int iovcnt) {
|
||||
return ::readv(fd, iov, iovcnt);
|
||||
}
|
||||
|
||||
ssize_t fd_stream::do_write(const void *buf, size_t len) {
|
||||
ssize_t fd_channel::do_write(const void *buf, size_t len) {
|
||||
return ::write(fd, buf, len);
|
||||
}
|
||||
|
||||
ssize_t fd_stream::writev(const iovec *iov, int iovcnt) {
|
||||
ssize_t fd_channel::writev(const iovec *iov, int iovcnt) {
|
||||
return ::writev(fd, iov, iovcnt);
|
||||
}
|
||||
|
||||
off_t fd_stream::seek(off_t off, int whence) {
|
||||
off_t fd_channel::seek(off_t off, int whence) {
|
||||
return lseek(fd, off, whence);
|
||||
}
|
||||
|
||||
bool file_stream::write(const void *buf, size_t len) {
|
||||
bool file_channel::write(const void *buf, size_t len) {
|
||||
size_t write_sz = 0;
|
||||
ssize_t ret;
|
||||
do {
|
||||
|
||||
Reference in New Issue
Block a user