Skip to content

Commit 020b11a

Browse files
authored
mz_strm_ppmd.c reader issue: it can't detect EOF (#953)
Fixes #950
1 parent aa00b1d commit 020b11a

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

mz_strm_ppmd.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ static mz_stream_vtbl mz_stream_ppmd_vtbl = {
2929

3030
#define PPMD_PRESET_DEFAULT 9 // Should match default in 7-Zip
3131

32+
// Return values from Ppmd8_DecodeSymbol
33+
#define PPMD_RESULT_EOF (-1)
34+
#define PPMD_RESULT_ERROR (-2)
35+
3236
/***************************************************************************/
3337

3438
typedef struct mz_in_buffer_s {
@@ -148,10 +152,7 @@ static Byte reader(void *p) {
148152
int32_t status;
149153

150154
if ((status = mz_stream_read_uint8((mz_stream_ppmd *)ppmd->stream.base, &b)) != MZ_OK) {
151-
if (b == -1) // EOF
152-
ppmd->end_stream = 1;
153-
else
154-
ppmd->error = status;
155+
ppmd->error = status;
155156
b = 0;
156157
} else
157158
++ppmd->total_in;
@@ -323,19 +324,24 @@ int32_t mz_stream_ppmd_read(void *stream, void *buf, int32_t size) {
323324
for (avail_out = size; avail_out > 0 && avail_in > 0; avail_out--, avail_in--) {
324325
sym = Ppmd8_DecodeSymbol(&ppmd->ppmd8);
325326

326-
if (sym < 0 || ppmd->end_stream || ppmd->error)
327+
/* There are two ways to terminate the loop early:
328+
1. Ppmd8_DecodeSymbol returns a negative number to flag EOF or stream error.
329+
2. ppmd->error gets set to true here when the call to mz_stream_read_uint8
330+
in reader() does not return MZ_OK.
331+
*/
332+
if (sym < 0 || ppmd->error)
327333
break;
328334

329335
*(next_out++) = sym;
330336
++written;
331337
}
332338

333-
if (sym == -1) {
334-
// end_stream
339+
// sym contains the return code from Ppmd8_DecodeSymbol
340+
if (sym == PPMD_RESULT_EOF) {
335341
ppmd->end_stream = 1;
336342

337343
// Drop through and return written bytes
338-
} else if (sym == -2) {
344+
} else if (sym == PPMD_RESULT_ERROR) {
339345
/* Insufficient input data. */
340346
return MZ_STREAM_ERROR;
341347
} else if (ppmd->error) {

0 commit comments

Comments
 (0)