stm32/mboot/gzstream: Fix lost data decompressing final part of file.
Prior to this fix, the final piece of data in a compressed file may have
been lost when decompressing.
typedef struct _gz_stream_t {
void *stream_data;
stream_read_t stream_read;
- TINF_DATA tinf;
+ struct uzlib_uncomp tinf;
uint8_t buf[512];
uint8_t dict[DICT_SIZE];
} gz_stream_t;
@@ -66,7 +66,7 @@ int gz_stream_init(void *stream_data, stream_read_t stream_read) {
gz_stream.stream_read = stream_read;
memset(&gz_stream.tinf, 0, sizeof(gz_stream.tinf));
- gz_stream.tinf.readSource = gz_stream_read_src;
+ gz_stream.tinf.source_read_cb = gz_stream_read_src;
int st = uzlib_gzip_parse_header(&gz_stream.tinf);
if (st != TINF_OK) {
@@ -79,15 +79,21 @@ int gz_stream_init(void *stream_data, stream_read_t stream_read) {
}
int gz_stream_read(size_t len, uint8_t *buf) {
+ if (gz_stream.tinf.source == NULL && gz_stream.tinf.source_read_cb == NULL) {
+ // End of stream.
+ return 0;
+ }
gz_stream.tinf.dest = buf;
gz_stream.tinf.dest_limit = buf + len;
int st = uzlib_uncompress_chksum(&gz_stream.tinf);
- if (st == TINF_DONE) {
- return 0;
- }
if (st < 0) {
return st;
}
+ if (st == TINF_DONE) {
+ // Indicate end-of-stream for subsequent calls.
+ gz_stream.tinf.source = NULL;
+ gz_stream.tinf.source_read_cb = NULL;
+ }
return gz_stream.tinf.dest - buf;
}
