Make write return something sane

This commit is contained in:
topjohnwu 2020-01-06 17:09:09 +08:00
parent 2262af728e
commit 242e64d72f

View File

@ -73,6 +73,7 @@ private:
uint8_t outbuf[CHUNK]; uint8_t outbuf[CHUNK];
int write(const void *buf, size_t len, int flush) { int write(const void *buf, size_t len, int flush) {
int ret = 0;
strm.next_in = (Bytef *) buf; strm.next_in = (Bytef *) buf;
strm.avail_in = len; strm.avail_in = len;
do { do {
@ -91,9 +92,9 @@ private:
LOGW("gzip %s failed (%d)\n", mode ? "encode" : "decode", code); LOGW("gzip %s failed (%d)\n", mode ? "encode" : "decode", code);
return -1; return -1;
} }
bwrite(outbuf, sizeof(outbuf) - strm.avail_out); ret += bwrite(outbuf, sizeof(outbuf) - strm.avail_out);
} while (strm.avail_out == 0); } while (strm.avail_out == 0);
return len; return ret;
} }
}; };
@ -147,6 +148,7 @@ private:
char outbuf[CHUNK]; char outbuf[CHUNK];
int write(const void *buf, size_t len, int flush) { int write(const void *buf, size_t len, int flush) {
int ret = 0;
strm.next_in = (char *) buf; strm.next_in = (char *) buf;
strm.avail_in = len; strm.avail_in = len;
do { do {
@ -165,9 +167,9 @@ private:
LOGW("bzip2 %s failed (%d)\n", mode ? "encode" : "decode", code); LOGW("bzip2 %s failed (%d)\n", mode ? "encode" : "decode", code);
return -1; return -1;
} }
bwrite(outbuf, sizeof(outbuf) - strm.avail_out); ret += bwrite(outbuf, sizeof(outbuf) - strm.avail_out);
} while (strm.avail_out == 0); } while (strm.avail_out == 0);
return len; return ret;
} }
}; };
@ -229,6 +231,7 @@ private:
uint8_t outbuf[CHUNK]; uint8_t outbuf[CHUNK];
int write(const void *buf, size_t len, lzma_action flush) { int write(const void *buf, size_t len, lzma_action flush) {
int ret = 0;
strm.next_in = (uint8_t *) buf; strm.next_in = (uint8_t *) buf;
strm.avail_in = len; strm.avail_in = len;
do { do {
@ -239,9 +242,9 @@ private:
LOGW("LZMA %s failed (%d)\n", mode ? "encode" : "decode", code); LOGW("LZMA %s failed (%d)\n", mode ? "encode" : "decode", code);
return -1; return -1;
} }
bwrite(outbuf, sizeof(outbuf) - strm.avail_out); ret += bwrite(outbuf, sizeof(outbuf) - strm.avail_out);
} while (strm.avail_out == 0); } while (strm.avail_out == 0);
return len; return ret;
} }
}; };
@ -272,7 +275,7 @@ public:
} }
int write(const void *buf, size_t len) override { int write(const void *buf, size_t len) override {
auto ret = len; int ret = 0;
auto inbuf = reinterpret_cast<const uint8_t *>(buf); auto inbuf = reinterpret_cast<const uint8_t *>(buf);
if (!outbuf) if (!outbuf)
read_header(inbuf, len); read_header(inbuf, len);
@ -288,7 +291,7 @@ public:
} }
len -= read; len -= read;
inbuf += read; inbuf += read;
bwrite(outbuf, write); ret += bwrite(outbuf, write);
} while (len != 0 || write != 0); } while (len != 0 || write != 0);
return ret; return ret;
} }
@ -323,9 +326,9 @@ public:
} }
int write(const void *buf, size_t len) override { int write(const void *buf, size_t len) override {
auto ret = len; int ret = 0;
if (!outbuf) if (!outbuf)
write_header(); ret += write_header();
if (len == 0) if (len == 0)
return 0; return 0;
auto inbuf = reinterpret_cast<const uint8_t *>(buf); auto inbuf = reinterpret_cast<const uint8_t *>(buf);
@ -339,7 +342,7 @@ public:
} }
len -= read; len -= read;
inbuf += read; inbuf += read;
bwrite(outbuf, write); ret += bwrite(outbuf, write);
} while (len != 0); } while (len != 0);
return ret; return ret;
} }
@ -358,7 +361,7 @@ private:
static constexpr size_t BLOCK_SZ = 1 << 22; static constexpr size_t BLOCK_SZ = 1 << 22;
void write_header() { int write_header() {
LZ4F_preferences_t prefs { LZ4F_preferences_t prefs {
.autoFlush = 1, .autoFlush = 1,
.compressionLevel = 9, .compressionLevel = 9,
@ -372,7 +375,7 @@ private:
outCapacity = LZ4F_compressBound(BLOCK_SZ, &prefs); outCapacity = LZ4F_compressBound(BLOCK_SZ, &prefs);
outbuf = new uint8_t[outCapacity]; outbuf = new uint8_t[outCapacity];
size_t write = LZ4F_compressBegin(ctx, outbuf, outCapacity, &prefs); size_t write = LZ4F_compressBegin(ctx, outbuf, outCapacity, &prefs);
bwrite(outbuf, write); return bwrite(outbuf, write);
} }
}; };
@ -388,7 +391,7 @@ public:
} }
int write(const void *in, size_t size) override { int write(const void *in, size_t size) override {
auto ret = size; int ret = 0;
auto inbuf = static_cast<const char *>(in); auto inbuf = static_cast<const char *>(in);
if (!init) { if (!init) {
// Skip magic // Skip magic
@ -420,7 +423,7 @@ public:
LOGW("LZ4HC decompression failure (%d)\n", write); LOGW("LZ4HC decompression failure (%d)\n", write);
return -1; return -1;
} }
bwrite(out_buf, write); ret += bwrite(out_buf, write);
// Reset // Reset
buf_off = 0; buf_off = 0;
@ -446,12 +449,13 @@ private:
class LZ4_encoder : public cpr_stream { class LZ4_encoder : public cpr_stream {
public: public:
explicit LZ4_encoder(stream_ptr &&base) explicit LZ4_encoder(stream_ptr &&base)
: cpr_stream(std::move(base)), outbuf(new char[LZ4_COMPRESSED]), buf(new char[LZ4_UNCOMPRESSED]), : cpr_stream(std::move(base)), outbuf(new char[LZ4_COMPRESSED]),
init(false), buf_off(0), in_total(0) {} buf(new char[LZ4_UNCOMPRESSED]), init(false), buf_off(0), in_total(0) {}
int write(const void *in, size_t size) override { int write(const void *in, size_t size) override {
int ret = 0;
if (!init) { if (!init) {
bwrite("\x02\x21\x4c\x18", 4); ret += bwrite("\x02\x21\x4c\x18", 4);
init = true; init = true;
} }
if (size == 0) if (size == 0)
@ -459,21 +463,18 @@ public:
in_total += size; in_total += size;
const char *inbuf = (const char *) in; const char *inbuf = (const char *) in;
size_t consumed; size_t consumed;
int write;
do { do {
if (buf_off + size >= LZ4_UNCOMPRESSED) { if (buf_off + size >= LZ4_UNCOMPRESSED) {
consumed = LZ4_UNCOMPRESSED - buf_off; consumed = LZ4_UNCOMPRESSED - buf_off;
memcpy(buf + buf_off, inbuf, consumed); memcpy(buf + buf_off, inbuf, consumed);
inbuf += consumed; inbuf += consumed;
size -= consumed; size -= consumed;
buf_off = LZ4_UNCOMPRESSED;
write = LZ4_compress_HC(buf, outbuf, LZ4_UNCOMPRESSED, LZ4_COMPRESSED, 9); if (int written = write_block(); written < 0)
if (write == 0) { return -1;
LOGW("LZ4HC compression failure\n"); else
return false; ret += written;
}
bwrite(&write, sizeof(write));
bwrite(outbuf, write);
// Reset buffer // Reset buffer
buf_off = 0; buf_off = 0;
@ -484,15 +485,12 @@ public:
size = 0; size = 0;
} }
} while (size != 0); } while (size != 0);
return true; return ret;
} }
~LZ4_encoder() override { ~LZ4_encoder() override {
if (buf_off) { if (buf_off)
int write = LZ4_compress_HC(buf, outbuf, buf_off, LZ4_COMPRESSED, 9); write_block();
bwrite(&write, sizeof(write));
bwrite(outbuf, write);
}
bwrite(&in_total, sizeof(in_total)); bwrite(&in_total, sizeof(in_total));
delete[] outbuf; delete[] outbuf;
delete[] buf; delete[] buf;
@ -504,6 +502,17 @@ private:
bool init; bool init;
int buf_off; int buf_off;
unsigned in_total; unsigned in_total;
int write_block() {
int written = LZ4_compress_HC(buf, outbuf, buf_off, LZ4_COMPRESSED, 9);
if (written == 0) {
LOGW("LZ4HC compression failure\n");
return -1;
}
bwrite(&written, sizeof(written));
bwrite(outbuf, written);
return written + sizeof(written);
}
}; };
stream_ptr get_encoder(format_t type, stream_ptr &&base) { stream_ptr get_encoder(format_t type, stream_ptr &&base) {