make nul act as a stream terminator for LZ4 sequences

original commit: 06f4aab43a35b3a3f956cf510c76c0edb4f1a866
This commit is contained in:
Matthew Flatt 2019-03-22 13:52:04 -06:00
parent 605f8f5033
commit c6d3a1dd69

View File

@ -49,6 +49,7 @@ typedef struct lz4File_in {
LZ4F_dctx *dctx; LZ4F_dctx *dctx;
void *in_buffer, *out_buffer; void *in_buffer, *out_buffer;
int in_pos, in_len, out_pos, out_len; int in_pos, in_len, out_pos, out_len;
int frame_ended;
int err; int err;
size_t stream_pos; size_t stream_pos;
off_t init_pos; off_t init_pos;
@ -96,6 +97,7 @@ static glzFile glzdopen_lz4_pos(int fd, const char *mode, off_t init_pos) {
lz4->in_len = 0; lz4->in_len = 0;
lz4->out_len = 0; lz4->out_len = 0;
lz4->out_pos = 0; lz4->out_pos = 0;
lz4->frame_ended = 0;
lz4->err = 0; lz4->err = 0;
lz4->stream_pos = 0; lz4->stream_pos = 0;
lz4->init_pos = init_pos; lz4->init_pos = init_pos;
@ -271,10 +273,15 @@ int glzread(glzFile file, void *buffer, unsigned int count) {
to that buffer: */ to that buffer: */
if (count >= (out_len >> 1)) { if (count >= (out_len >> 1)) {
size_t direct_out_len = count; size_t direct_out_len = count;
if (lz4->frame_ended && ((char *)lz4->in_buffer)[lz4->in_pos] == 0)
return 0; /* count 0 after frame as stream terminator */
amt = LZ4F_decompress(lz4->dctx, amt = LZ4F_decompress(lz4->dctx,
buffer, &direct_out_len, buffer, &direct_out_len,
(char *)lz4->in_buffer + lz4->in_pos, &in_len, (char *)lz4->in_buffer + lz4->in_pos, &in_len,
NULL); NULL);
lz4->frame_ended = (amt == 0);
if (LZ4F_isError(amt)) { if (LZ4F_isError(amt)) {
lz4->err = amt; lz4->err = amt;
@ -292,10 +299,14 @@ int glzread(glzFile file, void *buffer, unsigned int count) {
} }
if (in_len > 0) { if (in_len > 0) {
if (lz4->frame_ended && ((char *)lz4->in_buffer)[lz4->in_pos] == 0)
return 0; /* count 0 after frame as stream terminator */
amt = LZ4F_decompress(lz4->dctx, amt = LZ4F_decompress(lz4->dctx,
lz4->out_buffer, &out_len, lz4->out_buffer, &out_len,
(char *)lz4->in_buffer + lz4->in_pos, &in_len, (char *)lz4->in_buffer + lz4->in_pos, &in_len,
NULL); NULL);
lz4->frame_ended = (amt == 0);
if (LZ4F_isError(amt)) { if (LZ4F_isError(amt)) {
lz4->err = amt; lz4->err = amt;