From f5aaff2b1ed0f4be2d4ac730656c560abadc69eb Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 20 May 2023 01:28:10 -0700 Subject: [PATCH] Cleanup filter_out_stream implementation --- native/src/base/include/stream.hpp | 8 +------- native/src/base/stream.cpp | 25 ++----------------------- native/src/boot/bootimg.cpp | 4 ++-- native/src/boot/compress.cpp | 13 ++++--------- native/src/boot/compress.hpp | 10 ++-------- native/src/boot/lib.rs | 3 ++- 6 files changed, 13 insertions(+), 50 deletions(-) diff --git a/native/src/base/include/stream.hpp b/native/src/base/include/stream.hpp index 857f9a99d..e1127001d 100644 --- a/native/src/base/include/stream.hpp +++ b/native/src/base/include/stream.hpp @@ -18,16 +18,11 @@ using out_strm_ptr = std::unique_ptr; class filter_out_stream : public out_stream { public: filter_out_stream(out_strm_ptr &&base) : base(std::move(base)) {} - bool write(const void *buf, size_t len) override; - virtual bool write(const void *buf, size_t len, bool final); - protected: out_strm_ptr base; }; -using filter_strm_ptr = std::unique_ptr; - // Buffered output stream, writing in chunks class chunk_out_stream : public filter_out_stream { public: @@ -40,7 +35,6 @@ public: ~chunk_out_stream() override { delete[] _buf; } bool write(const void *buf, size_t len) final; - bool write(const void *buf, size_t len, bool final) final; protected: // Classes inheriting this class has to call finalize() in its destructor @@ -63,7 +57,7 @@ struct in_stream { }; // A channel is something that is writable, readable, and seekable -struct channel : public in_stream, public out_stream { +struct channel : public out_stream, public in_stream { virtual off_t seek(off_t off, int whence) = 0; virtual ~channel() = default; }; diff --git a/native/src/base/stream.cpp b/native/src/base/stream.cpp index 7ee7c9c47..ba7c2d00f 100644 --- a/native/src/base/stream.cpp +++ b/native/src/base/stream.cpp @@ -88,15 +88,7 @@ bool filter_out_stream::write(const void *buf, size_t len) { return base->write(buf, len); } -bool filter_out_stream::write(const void *buf, size_t len, bool final) { - return write(buf, len); -} - -bool chunk_out_stream::write(const void *buf, size_t len) { - return write(buf, len, false); -} - -bool chunk_out_stream::write(const void *_in, size_t len, bool final) { +bool chunk_out_stream::write(const void *_in, size_t len) { auto in = static_cast(_in); while (len) { if (buf_off + len >= chunk_sz) { @@ -114,21 +106,8 @@ bool chunk_out_stream::write(const void *_in, size_t len, bool final) { in += chunk_sz; len -= chunk_sz; } - if (!write_chunk(src, chunk_sz, final && len == 0)) + if (!write_chunk(src, chunk_sz, false)) return false; - } else if (final) { - // Final input data, write regardless whether it is chunk sized - if (buf_off) { - memcpy(_buf + buf_off, in, len); - auto avail = buf_off + len; - buf_off = 0; - if (!write_chunk(_buf, avail, true)) - return false; - } else { - if (!write_chunk(in, len, true)) - return false; - } - break; } else { // Buffer internally if (!_buf) { diff --git a/native/src/boot/bootimg.cpp b/native/src/boot/bootimg.cpp index 0200a4f28..64725824c 100644 --- a/native/src/boot/bootimg.cpp +++ b/native/src/boot/bootimg.cpp @@ -20,14 +20,14 @@ uint64_t dyn_img_hdr::j64 = 0; static void decompress(format_t type, int fd, const void *in, size_t size) { auto ptr = get_decoder(type, make_unique(fd)); - ptr->write(in, size, true); + ptr->write(in, size); } 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)); - strm->write(in, size, true); + strm->write(in, size); } auto now = lseek(fd, 0, SEEK_CUR); return now - prev; diff --git a/native/src/boot/compress.cpp b/native/src/boot/compress.cpp index 5ad03d115..fa21d3683 100644 --- a/native/src/boot/compress.cpp +++ b/native/src/boot/compress.cpp @@ -195,9 +195,6 @@ public: protected: bool write_chunk(const void *buf, size_t len, bool final) override { - if (len == 0) - return true; - auto in = static_cast(buf); in_total += len; @@ -514,7 +511,7 @@ public: } protected: - bool write_chunk(const void *buf, size_t len, bool final) override { + bool write_chunk(const void *buf, size_t len, bool) override { // This is an error if (len != chunk_sz) return false; @@ -565,7 +562,7 @@ public: } protected: - bool write_chunk(const void *buf, size_t len, bool final) override { + bool write_chunk(const void *buf, size_t len, bool) override { auto in = static_cast(buf); uint32_t block_sz = LZ4_compress_HC(in, out_buf, len, LZ4_COMPRESSED, LZ4HC_CLEVEL_MAX); if (block_sz == 0) { @@ -585,7 +582,7 @@ private: uint32_t in_total; }; -filter_strm_ptr get_encoder(format_t type, out_strm_ptr &&base) { +out_strm_ptr get_encoder(format_t type, out_strm_ptr &&base) { switch (type) { case XZ: return make_unique(std::move(base)); @@ -607,7 +604,7 @@ filter_strm_ptr get_encoder(format_t type, out_strm_ptr &&base) { } } -filter_strm_ptr get_decoder(format_t type, out_strm_ptr &&base) { +out_strm_ptr get_decoder(format_t type, out_strm_ptr &&base) { switch (type) { case XZ: case LZMA: @@ -721,7 +718,6 @@ void compress(const char *method, const char *infile, const char *outfile) { unlink(infile); } -namespace rust { bool decompress(const unsigned char *in, uint64_t in_size, int fd) { format_t type = check_fmt(in, in_size); @@ -736,4 +732,3 @@ bool decompress(const unsigned char *in, uint64_t in_size, int fd) { } return true; } -} diff --git a/native/src/boot/compress.hpp b/native/src/boot/compress.hpp index 10b1311c0..ea38c4ca4 100644 --- a/native/src/boot/compress.hpp +++ b/native/src/boot/compress.hpp @@ -4,14 +4,8 @@ #include "format.hpp" -filter_strm_ptr get_encoder(format_t type, out_strm_ptr &&base); - -filter_strm_ptr get_decoder(format_t type, out_strm_ptr &&base); - +out_strm_ptr get_encoder(format_t type, out_strm_ptr &&base); +out_strm_ptr get_decoder(format_t type, out_strm_ptr &&base); void compress(const char *method, const char *infile, const char *outfile); - void decompress(char *infile, const char *outfile); - -namespace rust { bool decompress(const unsigned char *in, uint64_t in_size, int fd); -} diff --git a/native/src/boot/lib.rs b/native/src/boot/lib.rs index 25ed2d22d..cf64a6ca8 100644 --- a/native/src/boot/lib.rs +++ b/native/src/boot/lib.rs @@ -6,13 +6,14 @@ pub use payload::*; mod payload; mod update_metadata; -#[cxx::bridge(namespace = "rust")] +#[cxx::bridge] pub mod ffi { extern "C++" { include!("compress.hpp"); pub unsafe fn decompress(in_: *const u8, in_size: u64, fd: i32) -> bool; } + #[namespace = "rust"] extern "Rust" { unsafe fn extract_boot_from_payload( in_path: *const c_char,