X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/f4944c7cf92f243fd5c48361c6012e84f71fb545..51d05cd0a6de05c0adc8986e1d534e426d6f582c:/deflate.c diff --git a/deflate.c b/deflate.c index ecae0d0..186e14c 100644 --- a/deflate.c +++ b/deflate.c @@ -2297,14 +2297,25 @@ int deflate_decompress_data(deflate_decompress_ctx *dctx, &error); if (!dctx->currlentable) goto finished; /* error code set up by mktable */ - dctx->currdisttable = mktable(dctx->lengths + dctx->hlit, - dctx->hdist, + if (dctx->hdist == 1 && dctx->lengths[dctx->hlit] == 0) { + /* + * Special case: if the code length list for the + * backward-distance table contains a single zero + * entry, it means this block will never encode a + * backward distance at all (i.e. it's all + * literals). + */ + dctx->currdisttable = NULL; + } else { + dctx->currdisttable = mktable(dctx->lengths + dctx->hlit, + dctx->hdist, #ifdef ANALYSIS - "distance", + "distance", #endif - &error); - if (!dctx->currdisttable) - goto finished; /* error code set up by mktable */ + &error); + if (!dctx->currdisttable) + goto finished; /* error code set up by mktable */ + } freetable(&dctx->lenlentable); dctx->lenlentable = NULL; dctx->state = INBLK; @@ -2374,7 +2385,8 @@ int deflate_decompress_data(deflate_decompress_ctx *dctx, freetable(&dctx->currlentable); dctx->currlentable = NULL; } - if (dctx->currdisttable != dctx->staticdisttable) { + if (dctx->currdisttable && + dctx->currdisttable != dctx->staticdisttable) { freetable(&dctx->currdisttable); dctx->currdisttable = NULL; } @@ -2396,6 +2408,10 @@ int deflate_decompress_data(deflate_decompress_ctx *dctx, dctx->state = GOTLEN; break; case GOTLEN: + if (!dctx->currdisttable) { + error = DEFLATE_ERR_NODISTTABLE; + goto finished; + } code = huflookup(&dctx->bits, &dctx->nbits, dctx->currdisttable); debug(("recv: dist %d\n", code)); if (code == -1) @@ -2443,9 +2459,16 @@ int deflate_decompress_data(deflate_decompress_ctx *dctx, goto finished; nlen = dctx->bits & 0xFFFF; EATBITS(16); - if (dctx->uncomplen == 0) - dctx->state = OUTSIDEBLK; /* block is empty */ - else + if (dctx->uncomplen != (nlen ^ 0xFFFF)) { + error = DEFLATE_ERR_UNCOMP_HDR; + goto finished; + } + if (dctx->uncomplen == 0) {/* block is empty */ + if (dctx->lastblock) + dctx->state = END; + else + dctx->state = OUTSIDEBLK; + } else dctx->state = UNCOMP_DATA; break; case UNCOMP_DATA: @@ -2458,8 +2481,12 @@ int deflate_decompress_data(deflate_decompress_ctx *dctx, #endif emit_char(dctx, dctx->bits & 0xFF); EATBITS(8); - if (--dctx->uncomplen == 0) - dctx->state = OUTSIDEBLK; /* end of uncompressed block */ + if (--dctx->uncomplen == 0) { /* end of uncompressed block */ + if (dctx->lastblock) + dctx->state = END; + else + dctx->state = OUTSIDEBLK; + } break; case END: /*