diff --git a/native/src/base/include/stream.hpp b/native/src/base/include/stream.hpp index 423ea5bd4..857f9a99d 100644 --- a/native/src/base/include/stream.hpp +++ b/native/src/base/include/stream.hpp @@ -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; +using out_strm_ptr = std::unique_ptr; // 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; +using filter_strm_ptr = std::unique_ptr; // 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; + +// 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 - byte_stream(Byte *&buf, size_t &len) : byte_stream(reinterpret_cast(buf), len) {} + byte_channel(Byte *&buf, size_t &len) : byte_channel(reinterpret_cast(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 -sFILE make_stream_fp(Args &&... args) { - return make_stream_fp(stream_ptr(new T(std::forward(args)...))); +sFILE make_channel_fp(Args &&... args) { + return make_channel_fp(channel_ptr(new T(std::forward(args)...))); } diff --git a/native/src/base/stream.cpp b/native/src/base/stream.cpp index dcde87ec4..7ee7c9c47 100644 --- a/native/src/base/stream.cpp +++ b/native/src/base/stream.cpp @@ -7,40 +7,35 @@ using namespace std; static int strm_read(void *v, char *buf, int len) { - auto strm = static_cast(v); + auto strm = static_cast(v); return strm->read(buf, len); } static int strm_write(void *v, const char *buf, int len) { - auto strm = static_cast(v); + auto strm = static_cast(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(v); + auto strm = static_cast(v); return strm->seek(off, whence); } static int strm_close(void *v) { - auto strm = static_cast(v); + auto strm = static_cast(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 { diff --git a/native/src/boot/bootimg.cpp b/native/src/boot/bootimg.cpp index 631472101..0200a4f28 100644 --- a/native/src/boot/bootimg.cpp +++ b/native/src/boot/bootimg.cpp @@ -19,14 +19,14 @@ uint64_t dyn_img_hdr::j64 = 0; #define PADDING 15 static void decompress(format_t type, int fd, const void *in, size_t size) { - auto ptr = get_decoder(type, make_unique(fd)); + auto ptr = get_decoder(type, make_unique(fd)); ptr->write(in, size, true); } static off_t compress(format_t type, int fd, const void *in, size_t size) { auto prev = lseek(fd, 0, SEEK_CUR); { - auto strm = get_encoder(type, make_unique(fd)); + auto strm = get_encoder(type, make_unique(fd)); strm->write(in, size, true); } auto now = lseek(fd, 0, SEEK_CUR); diff --git a/native/src/boot/compress.cpp b/native/src/boot/compress.cpp index 94fda7b12..5ad03d115 100644 --- a/native/src/boot/compress.cpp +++ b/native/src/boot/compress.cpp @@ -23,12 +23,7 @@ constexpr size_t CHUNK = 0x40000; constexpr size_t LZ4_UNCOMPRESSED = 0x800000; constexpr size_t LZ4_COMPRESSED = LZ4_COMPRESSBOUND(LZ4_UNCOMPRESSED); -class out_stream : public filter_stream { - using filter_stream::filter_stream; - using stream::read; -}; - -class gz_strm : public out_stream { +class gz_strm : public filter_out_stream { public: bool write(const void *buf, size_t len) override { return len == 0 || do_write(buf, len, Z_NO_FLUSH); @@ -56,8 +51,8 @@ protected: COPY } mode; - gz_strm(mode_t mode, stream_ptr &&base) : - out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} { + gz_strm(mode_t mode, out_strm_ptr &&base) : + filter_out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} { switch(mode) { case DECODE: inflateInit2(&strm, 15 | 16); @@ -146,17 +141,17 @@ private: class gz_decoder : public gz_strm { public: - explicit gz_decoder(stream_ptr &&base) : gz_strm(DECODE, std::move(base)) {}; + explicit gz_decoder(out_strm_ptr &&base) : gz_strm(DECODE, std::move(base)) {}; }; class gz_encoder : public gz_strm { public: - explicit gz_encoder(stream_ptr &&base) : gz_strm(ENCODE, std::move(base)) {}; + explicit gz_encoder(out_strm_ptr &&base) : gz_strm(ENCODE, std::move(base)) {}; }; class zopfli_encoder : public chunk_out_stream { public: - explicit zopfli_encoder(stream_ptr &&base) : + explicit zopfli_encoder(out_strm_ptr &&base) : chunk_out_stream(std::move(base), ZOPFLI_MASTER_BLOCK_SIZE), zo{}, out(nullptr), outsize(0), crc(crc32_z(0L, Z_NULL, 0)), in_total(0), bp(0) { ZopfliInitOptions(&zo); @@ -229,7 +224,7 @@ private: unsigned char bp; }; -class bz_strm : public out_stream { +class bz_strm : public filter_out_stream { public: bool write(const void *buf, size_t len) override { return len == 0 || do_write(buf, len, BZ_RUN); @@ -253,8 +248,8 @@ protected: ENCODE } mode; - bz_strm(mode_t mode, stream_ptr &&base) : - out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} { + bz_strm(mode_t mode, out_strm_ptr &&base) : + filter_out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} { switch(mode) { case DECODE: BZ2_bzDecompressInit(&strm, 0, 0); @@ -299,15 +294,15 @@ private: class bz_decoder : public bz_strm { public: - explicit bz_decoder(stream_ptr &&base) : bz_strm(DECODE, std::move(base)) {}; + explicit bz_decoder(out_strm_ptr &&base) : bz_strm(DECODE, std::move(base)) {}; }; class bz_encoder : public bz_strm { public: - explicit bz_encoder(stream_ptr &&base) : bz_strm(ENCODE, std::move(base)) {}; + explicit bz_encoder(out_strm_ptr &&base) : bz_strm(ENCODE, std::move(base)) {}; }; -class lzma_strm : public out_stream { +class lzma_strm : public filter_out_stream { public: bool write(const void *buf, size_t len) override { return len == 0 || do_write(buf, len, LZMA_RUN); @@ -325,8 +320,8 @@ protected: ENCODE_LZMA } mode; - lzma_strm(mode_t mode, stream_ptr &&base) : - out_stream(std::move(base)), mode(mode), strm(LZMA_STREAM_INIT), outbuf{0} { + lzma_strm(mode_t mode, out_strm_ptr &&base) : + filter_out_stream(std::move(base)), mode(mode), strm(LZMA_STREAM_INIT), outbuf{0} { lzma_options_lzma opt; // Initialize preset @@ -377,23 +372,23 @@ private: class lzma_decoder : public lzma_strm { public: - explicit lzma_decoder(stream_ptr &&base) : lzma_strm(DECODE, std::move(base)) {} + explicit lzma_decoder(out_strm_ptr &&base) : lzma_strm(DECODE, std::move(base)) {} }; class xz_encoder : public lzma_strm { public: - explicit xz_encoder(stream_ptr &&base) : lzma_strm(ENCODE_XZ, std::move(base)) {} + explicit xz_encoder(out_strm_ptr &&base) : lzma_strm(ENCODE_XZ, std::move(base)) {} }; class lzma_encoder : public lzma_strm { public: - explicit lzma_encoder(stream_ptr &&base) : lzma_strm(ENCODE_LZMA, std::move(base)) {} + explicit lzma_encoder(out_strm_ptr &&base) : lzma_strm(ENCODE_LZMA, std::move(base)) {} }; -class LZ4F_decoder : public out_stream { +class LZ4F_decoder : public filter_out_stream { public: - explicit LZ4F_decoder(stream_ptr &&base) : - out_stream(std::move(base)), ctx(nullptr), outbuf(nullptr), outCapacity(0) { + explicit LZ4F_decoder(out_strm_ptr &&base) : + filter_out_stream(std::move(base)), ctx(nullptr), outbuf(nullptr), outCapacity(0) { LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION); } @@ -443,10 +438,10 @@ private: size_t outCapacity; }; -class LZ4F_encoder : public out_stream { +class LZ4F_encoder : public filter_out_stream { public: - explicit LZ4F_encoder(stream_ptr &&base) : - out_stream(std::move(base)), ctx(nullptr), out_buf(nullptr), outCapacity(0) { + explicit LZ4F_encoder(out_strm_ptr &&base) : + filter_out_stream(std::move(base)), ctx(nullptr), out_buf(nullptr), outCapacity(0) { LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); } @@ -509,7 +504,7 @@ private: class LZ4_decoder : public chunk_out_stream { public: - explicit LZ4_decoder(stream_ptr &&base) : + explicit LZ4_decoder(out_strm_ptr &&base) : chunk_out_stream(std::move(base), LZ4_COMPRESSED, sizeof(block_sz)), out_buf(new char[LZ4_UNCOMPRESSED]), block_sz(0) {} @@ -556,7 +551,7 @@ private: class LZ4_encoder : public chunk_out_stream { public: - explicit LZ4_encoder(stream_ptr &&base, bool lg) : + explicit LZ4_encoder(out_strm_ptr &&base, bool lg) : chunk_out_stream(std::move(base), LZ4_UNCOMPRESSED), out_buf(new char[LZ4_COMPRESSED]), lg(lg), in_total(0) { bwrite("\x02\x21\x4c\x18", 4); @@ -590,7 +585,7 @@ private: uint32_t in_total; }; -filter_strm_ptr get_encoder(format_t type, stream_ptr &&base) { +filter_strm_ptr get_encoder(format_t type, out_strm_ptr &&base) { switch (type) { case XZ: return make_unique(std::move(base)); @@ -612,7 +607,7 @@ filter_strm_ptr get_encoder(format_t type, stream_ptr &&base) { } } -filter_strm_ptr get_decoder(format_t type, stream_ptr &&base) { +filter_strm_ptr get_decoder(format_t type, out_strm_ptr &&base) { switch (type) { case XZ: case LZMA: @@ -636,7 +631,7 @@ void decompress(char *infile, const char *outfile) { bool rm_in = false; FILE *in_fp = in_std ? stdin : xfopen(infile, "re"); - stream_ptr strm; + out_strm_ptr strm; char buf[4096]; size_t len; @@ -669,7 +664,7 @@ void decompress(char *infile, const char *outfile) { } FILE *out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we"); - strm = get_decoder(type, make_unique(out_fp)); + strm = get_decoder(type, make_unique(out_fp)); if (ext) *ext = '.'; } if (!strm->write(buf, len)) @@ -710,7 +705,7 @@ void compress(const char *method, const char *infile, const char *outfile) { out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we"); } - auto strm = get_encoder(fmt, make_unique(out_fp)); + auto strm = get_encoder(fmt, make_unique(out_fp)); char buf[4096]; size_t len; @@ -735,7 +730,7 @@ bool decompress(const unsigned char *in, uint64_t in_size, int fd) { return false; } - auto strm = get_decoder(type, make_unique(fd)); + auto strm = get_decoder(type, make_unique(fd)); if (!strm->write(in, in_size)) { return false; } diff --git a/native/src/boot/compress.hpp b/native/src/boot/compress.hpp index 18e60cad7..10b1311c0 100644 --- a/native/src/boot/compress.hpp +++ b/native/src/boot/compress.hpp @@ -4,9 +4,9 @@ #include "format.hpp" -filter_strm_ptr get_encoder(format_t type, stream_ptr &&base); +filter_strm_ptr get_encoder(format_t type, out_strm_ptr &&base); -filter_strm_ptr get_decoder(format_t type, stream_ptr &&base); +filter_strm_ptr get_decoder(format_t type, out_strm_ptr &&base); void compress(const char *method, const char *infile, const char *outfile); diff --git a/native/src/sepolicy/policydb.cpp b/native/src/sepolicy/policydb.cpp index 3852c73b4..be8862976 100644 --- a/native/src/sepolicy/policydb.cpp +++ b/native/src/sepolicy/policydb.cpp @@ -240,7 +240,7 @@ bool sepolicy::to_file(const char *file) { // No partial writes are allowed to /sys/fs/selinux/load, thus the reason why we // first dump everything into memory, then directly call write system call - auto fp = make_stream_fp(data, len); + auto fp = make_channel_fp(data, len); run_finally fin([=]{ free(data); }); policy_file_t pf;