Split input and output streams

This commit is contained in:
topjohnwu 2023-05-19 04:19:43 -07:00
parent 582cad1b8d
commit 57bd450798
6 changed files with 104 additions and 124 deletions

View File

@ -6,50 +6,39 @@
#include "../files.hpp" #include "../files.hpp"
class stream { struct out_stream {
public: virtual bool write(const void *buf, size_t len) = 0;
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);
virtual ssize_t writev(const iovec *iov, int iovcnt); virtual ssize_t writev(const iovec *iov, int iovcnt);
virtual off_t seek(off_t off, int whence); virtual ~out_stream() = default;
virtual ~stream() = default;
}; };
using stream_ptr = std::unique_ptr<stream>; using out_strm_ptr = std::unique_ptr<out_stream>;
// Delegates all operations to base stream // Delegates all operations to base stream
class filter_stream : public stream { class filter_out_stream : public out_stream {
public: 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; bool write(const void *buf, size_t len) override;
virtual bool write(const void *buf, size_t len, bool final); 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: 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 // Buffered output stream, writing in chunks
class chunk_out_stream : public filter_stream { class chunk_out_stream : public filter_out_stream {
public: public:
chunk_out_stream(stream_ptr &&base, size_t buf_sz, size_t chunk_sz) chunk_out_stream(out_strm_ptr &&base, size_t buf_sz, size_t chunk_sz)
: filter_stream(std::move(base)), chunk_sz(chunk_sz), buf_sz(buf_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(std::move(base), buf_sz, buf_sz) {}
~chunk_out_stream() override { delete[] _buf; } ~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) final;
bool write(const void *buf, size_t len, bool final) final; bool write(const void *buf, size_t len, bool final) final;
@ -66,12 +55,27 @@ private:
uint8_t *_buf = nullptr; uint8_t *_buf = nullptr;
}; };
// Byte stream that dynamically allocates memory struct in_stream {
class byte_stream : public 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: public:
byte_stream(uint8_t *&buf, size_t &len); byte_channel(uint8_t *&buf, size_t &len);
template <class Byte> 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; ssize_t read(void *buf, size_t len) override;
bool write(const 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); void resize(size_t new_pos, bool zero = false);
}; };
class file_stream : public stream { class file_channel : public channel {
public: public:
bool write(const void *buf, size_t len) final; bool write(const void *buf, size_t len) final;
protected: protected:
virtual ssize_t do_write(const void *buf, size_t len) = 0; virtual ssize_t do_write(const void *buf, size_t len) = 0;
}; };
// File stream but does not close the file descriptor at any time // File channel but does not close the file descriptor at any time
class fd_stream : public file_stream { class fd_channel : public file_channel {
public: public:
fd_stream(int fd) : fd(fd) {} fd_channel(int fd) : fd(fd) {}
ssize_t read(void *buf, size_t len) override; ssize_t read(void *buf, size_t len) override;
ssize_t readv(const iovec *iov, int iovcnt) override; ssize_t readv(const iovec *iov, int iovcnt) override;
ssize_t writev(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 // sFILE -> channel_ptr
class fp_stream final : public file_stream { class fp_channel final : public file_channel {
public: public:
fp_stream(FILE *fp = nullptr) : fp(fp, fclose) {} fp_channel(FILE *fp = nullptr) : fp(fp, fclose) {}
fp_stream(sFILE &&fp) : fp(std::move(fp)) {} fp_channel(sFILE &&fp) : fp(std::move(fp)) {}
ssize_t read(void *buf, size_t len) override; ssize_t read(void *buf, size_t len) override;
off_t seek(off_t off, int whence) override; off_t seek(off_t off, int whence) override;
protected: protected:
@ -124,10 +128,10 @@ private:
sFILE fp; sFILE fp;
}; };
// stream_ptr -> sFILE // channel_ptr -> sFILE
sFILE make_stream_fp(stream_ptr &&strm); sFILE make_channel_fp(channel_ptr &&strm);
template <class T, class... Args> template <class T, class... Args>
sFILE make_stream_fp(Args &&... args) { sFILE make_channel_fp(Args &&... args) {
return make_stream_fp(stream_ptr(new T(std::forward<Args>(args)...))); return make_channel_fp(channel_ptr(new T(std::forward<Args>(args)...)));
} }

View File

@ -7,40 +7,35 @@
using namespace std; using namespace std;
static int strm_read(void *v, char *buf, int len) { 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); return strm->read(buf, len);
} }
static int strm_write(void *v, const char *buf, int 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)) if (!strm->write(buf, len))
return -1; return -1;
return len; return len;
} }
static fpos_t strm_seek(void *v, fpos_t off, int whence) { 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); return strm->seek(off, whence);
} }
static int strm_close(void *v) { static int strm_close(void *v) {
auto strm = static_cast<stream *>(v); auto strm = static_cast<channel *>(v);
delete strm; delete strm;
return 0; 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)); auto fp = make_file(funopen(strm.release(), strm_read, strm_write, strm_seek, strm_close));
setbuf(fp.get(), nullptr); setbuf(fp.get(), nullptr);
return fp; return fp;
} }
ssize_t stream::read(void *buf, size_t len) { ssize_t in_stream::readFully(void *buf, size_t len) {
LOGE("This stream does not implement read\n");
return -1;
}
ssize_t stream::readFully(void *buf, size_t len) {
size_t read_sz = 0; size_t read_sz = 0;
ssize_t ret; ssize_t ret;
do { do {
@ -55,7 +50,7 @@ ssize_t stream::readFully(void *buf, size_t len) {
return read_sz; 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; size_t read_sz = 0;
for (int i = 0; i < iovcnt; ++i) { for (int i = 0; i < iovcnt; ++i) {
auto ret = readFully(iov[i].iov_base, iov[i].iov_len); 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; return read_sz;
} }
bool stream::write(const void *buf, size_t len) { ssize_t out_stream::writev(const iovec *iov, int iovcnt) {
LOGE("This stream does not implement write\n");
return false;
}
ssize_t stream::writev(const iovec *iov, int iovcnt) {
size_t write_sz = 0; size_t write_sz = 0;
for (int i = 0; i < iovcnt; ++i) { for (int i = 0; i < iovcnt; ++i) {
if (!write(iov[i].iov_base, iov[i].iov_len)) 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; return write_sz;
} }
off_t stream::seek(off_t off, int whence) { ssize_t fp_channel::read(void *buf, size_t len) {
LOGE("This stream does not implement seek\n");
return -1;
}
ssize_t fp_stream::read(void *buf, size_t len) {
auto ret = fread(buf, 1, len, fp.get()); auto ret = fread(buf, 1, len, fp.get());
return ret ? ret : (ferror(fp.get()) ? -1 : 0); 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()); 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); return fseek(fp.get(), off, whence);
} }
ssize_t filter_stream::read(void *buf, size_t len) { bool filter_out_stream::write(const void *buf, size_t len) {
return base->read(buf, len);
}
bool filter_stream::write(const void *buf, size_t len) {
return base->write(buf, 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); 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; buf = nullptr;
len = 0; 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); len = std::min((size_t) len, _len - _pos);
memcpy(buf, _buf + _pos, len); memcpy(buf, _buf + _pos, len);
return 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); resize(_pos + len);
memcpy(_buf + _pos, buf, len); memcpy(_buf + _pos, buf, len);
_pos += len; _pos += len;
@ -191,7 +172,7 @@ bool byte_stream::write(const void *buf, size_t len) {
return true; 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; off_t np;
switch (whence) { switch (whence) {
case SEEK_CUR: case SEEK_CUR:
@ -211,7 +192,7 @@ off_t byte_stream::seek(off_t off, int whence) {
return np; 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; bool resize = false;
size_t old_cap = _cap; size_t old_cap = _cap;
while (new_pos > _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); 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); 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); 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); 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); 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; size_t write_sz = 0;
ssize_t ret; ssize_t ret;
do { do {

View File

@ -19,14 +19,14 @@ uint64_t dyn_img_hdr::j64 = 0;
#define PADDING 15 #define PADDING 15
static void decompress(format_t type, int fd, const void *in, size_t size) { static void decompress(format_t type, int fd, const void *in, size_t size) {
auto ptr = get_decoder(type, make_unique<fd_stream>(fd)); auto ptr = get_decoder(type, make_unique<fd_channel>(fd));
ptr->write(in, size, true); ptr->write(in, size, true);
} }
static off_t compress(format_t type, int fd, const void *in, size_t size) { static off_t compress(format_t type, int fd, const void *in, size_t size) {
auto prev = lseek(fd, 0, SEEK_CUR); auto prev = lseek(fd, 0, SEEK_CUR);
{ {
auto strm = get_encoder(type, make_unique<fd_stream>(fd)); auto strm = get_encoder(type, make_unique<fd_channel>(fd));
strm->write(in, size, true); strm->write(in, size, true);
} }
auto now = lseek(fd, 0, SEEK_CUR); auto now = lseek(fd, 0, SEEK_CUR);

View File

@ -23,12 +23,7 @@ constexpr size_t CHUNK = 0x40000;
constexpr size_t LZ4_UNCOMPRESSED = 0x800000; constexpr size_t LZ4_UNCOMPRESSED = 0x800000;
constexpr size_t LZ4_COMPRESSED = LZ4_COMPRESSBOUND(LZ4_UNCOMPRESSED); constexpr size_t LZ4_COMPRESSED = LZ4_COMPRESSBOUND(LZ4_UNCOMPRESSED);
class out_stream : public filter_stream { class gz_strm : public filter_out_stream {
using filter_stream::filter_stream;
using stream::read;
};
class gz_strm : public out_stream {
public: public:
bool write(const void *buf, size_t len) override { bool write(const void *buf, size_t len) override {
return len == 0 || do_write(buf, len, Z_NO_FLUSH); return len == 0 || do_write(buf, len, Z_NO_FLUSH);
@ -56,8 +51,8 @@ protected:
COPY COPY
} mode; } mode;
gz_strm(mode_t mode, stream_ptr &&base) : gz_strm(mode_t mode, out_strm_ptr &&base) :
out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} { filter_out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} {
switch(mode) { switch(mode) {
case DECODE: case DECODE:
inflateInit2(&strm, 15 | 16); inflateInit2(&strm, 15 | 16);
@ -146,17 +141,17 @@ private:
class gz_decoder : public gz_strm { class gz_decoder : public gz_strm {
public: 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 { class gz_encoder : public gz_strm {
public: 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 { class zopfli_encoder : public chunk_out_stream {
public: public:
explicit zopfli_encoder(stream_ptr &&base) : explicit zopfli_encoder(out_strm_ptr &&base) :
chunk_out_stream(std::move(base), ZOPFLI_MASTER_BLOCK_SIZE), 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) { zo{}, out(nullptr), outsize(0), crc(crc32_z(0L, Z_NULL, 0)), in_total(0), bp(0) {
ZopfliInitOptions(&zo); ZopfliInitOptions(&zo);
@ -229,7 +224,7 @@ private:
unsigned char bp; unsigned char bp;
}; };
class bz_strm : public out_stream { class bz_strm : public filter_out_stream {
public: public:
bool write(const void *buf, size_t len) override { bool write(const void *buf, size_t len) override {
return len == 0 || do_write(buf, len, BZ_RUN); return len == 0 || do_write(buf, len, BZ_RUN);
@ -253,8 +248,8 @@ protected:
ENCODE ENCODE
} mode; } mode;
bz_strm(mode_t mode, stream_ptr &&base) : bz_strm(mode_t mode, out_strm_ptr &&base) :
out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} { filter_out_stream(std::move(base)), mode(mode), strm{}, outbuf{0} {
switch(mode) { switch(mode) {
case DECODE: case DECODE:
BZ2_bzDecompressInit(&strm, 0, 0); BZ2_bzDecompressInit(&strm, 0, 0);
@ -299,15 +294,15 @@ private:
class bz_decoder : public bz_strm { class bz_decoder : public bz_strm {
public: 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 { class bz_encoder : public bz_strm {
public: 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: public:
bool write(const void *buf, size_t len) override { bool write(const void *buf, size_t len) override {
return len == 0 || do_write(buf, len, LZMA_RUN); return len == 0 || do_write(buf, len, LZMA_RUN);
@ -325,8 +320,8 @@ protected:
ENCODE_LZMA ENCODE_LZMA
} mode; } mode;
lzma_strm(mode_t mode, stream_ptr &&base) : lzma_strm(mode_t mode, out_strm_ptr &&base) :
out_stream(std::move(base)), mode(mode), strm(LZMA_STREAM_INIT), outbuf{0} { filter_out_stream(std::move(base)), mode(mode), strm(LZMA_STREAM_INIT), outbuf{0} {
lzma_options_lzma opt; lzma_options_lzma opt;
// Initialize preset // Initialize preset
@ -377,23 +372,23 @@ private:
class lzma_decoder : public lzma_strm { class lzma_decoder : public lzma_strm {
public: 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 { class xz_encoder : public lzma_strm {
public: 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 { class lzma_encoder : public lzma_strm {
public: 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: public:
explicit LZ4F_decoder(stream_ptr &&base) : explicit LZ4F_decoder(out_strm_ptr &&base) :
out_stream(std::move(base)), ctx(nullptr), outbuf(nullptr), outCapacity(0) { filter_out_stream(std::move(base)), ctx(nullptr), outbuf(nullptr), outCapacity(0) {
LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION); LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
} }
@ -443,10 +438,10 @@ private:
size_t outCapacity; size_t outCapacity;
}; };
class LZ4F_encoder : public out_stream { class LZ4F_encoder : public filter_out_stream {
public: public:
explicit LZ4F_encoder(stream_ptr &&base) : explicit LZ4F_encoder(out_strm_ptr &&base) :
out_stream(std::move(base)), ctx(nullptr), out_buf(nullptr), outCapacity(0) { filter_out_stream(std::move(base)), ctx(nullptr), out_buf(nullptr), outCapacity(0) {
LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
} }
@ -509,7 +504,7 @@ private:
class LZ4_decoder : public chunk_out_stream { class LZ4_decoder : public chunk_out_stream {
public: 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)), chunk_out_stream(std::move(base), LZ4_COMPRESSED, sizeof(block_sz)),
out_buf(new char[LZ4_UNCOMPRESSED]), block_sz(0) {} out_buf(new char[LZ4_UNCOMPRESSED]), block_sz(0) {}
@ -556,7 +551,7 @@ private:
class LZ4_encoder : public chunk_out_stream { class LZ4_encoder : public chunk_out_stream {
public: 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), chunk_out_stream(std::move(base), LZ4_UNCOMPRESSED),
out_buf(new char[LZ4_COMPRESSED]), lg(lg), in_total(0) { out_buf(new char[LZ4_COMPRESSED]), lg(lg), in_total(0) {
bwrite("\x02\x21\x4c\x18", 4); bwrite("\x02\x21\x4c\x18", 4);
@ -590,7 +585,7 @@ private:
uint32_t in_total; 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) { switch (type) {
case XZ: case XZ:
return make_unique<xz_encoder>(std::move(base)); return make_unique<xz_encoder>(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) { switch (type) {
case XZ: case XZ:
case LZMA: case LZMA:
@ -636,7 +631,7 @@ void decompress(char *infile, const char *outfile) {
bool rm_in = false; bool rm_in = false;
FILE *in_fp = in_std ? stdin : xfopen(infile, "re"); FILE *in_fp = in_std ? stdin : xfopen(infile, "re");
stream_ptr strm; out_strm_ptr strm;
char buf[4096]; char buf[4096];
size_t len; size_t len;
@ -669,7 +664,7 @@ void decompress(char *infile, const char *outfile) {
} }
FILE *out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we"); FILE *out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we");
strm = get_decoder(type, make_unique<fp_stream>(out_fp)); strm = get_decoder(type, make_unique<fp_channel>(out_fp));
if (ext) *ext = '.'; if (ext) *ext = '.';
} }
if (!strm->write(buf, len)) 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"); out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we");
} }
auto strm = get_encoder(fmt, make_unique<fp_stream>(out_fp)); auto strm = get_encoder(fmt, make_unique<fp_channel>(out_fp));
char buf[4096]; char buf[4096];
size_t len; size_t len;
@ -735,7 +730,7 @@ bool decompress(const unsigned char *in, uint64_t in_size, int fd) {
return false; return false;
} }
auto strm = get_decoder(type, make_unique<fd_stream>(fd)); auto strm = get_decoder(type, make_unique<fd_channel>(fd));
if (!strm->write(in, in_size)) { if (!strm->write(in, in_size)) {
return false; return false;
} }

View File

@ -4,9 +4,9 @@
#include "format.hpp" #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); void compress(const char *method, const char *infile, const char *outfile);

View File

@ -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 // 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 // first dump everything into memory, then directly call write system call
auto fp = make_stream_fp<byte_stream>(data, len); auto fp = make_channel_fp<byte_channel>(data, len);
run_finally fin([=]{ free(data); }); run_finally fin([=]{ free(data); });
policy_file_t pf; policy_file_t pf;