From d3e295b507a1af10e066d72c386095f9bd8a8623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20B=C3=BCnzli?= Date: Thu, 18 Dec 2014 12:29:31 +0100 Subject: Bug 695746: make LZW decoder slightly more tolerant Ghostscript's LZW decoder accepts invalid LZW code 4096 if it's immediately followed by LZW clear code 256 for handling files produced by certain broken encoders and other common PDF readers seem to have similar error handling. This patch makes MuPDF tolerate such broken files as well. --- source/fitz/filter-lzw.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'source/fitz/filter-lzw.c') diff --git a/source/fitz/filter-lzw.c b/source/fitz/filter-lzw.c index e13b0ef0..16b5253a 100644 --- a/source/fitz/filter-lzw.c +++ b/source/fitz/filter-lzw.c @@ -87,7 +87,7 @@ next_lzwd(fz_stream *stm, int len) break; } - if (next_code >= NUM_CODES && code != LZW_CLEAR) + if (next_code > NUM_CODES && code != LZW_CLEAR) { fz_warn(stm->ctx, "missing clear code in lzw decode"); code = LZW_CLEAR; @@ -106,6 +106,12 @@ next_lzwd(fz_stream *stm, int len) { old_code = code; } + else if (next_code == NUM_CODES) + { + /* TODO: Ghostscript checks for a following LZW_CLEAR before tolerating */ + fz_warn(stm->ctx, "tolerating a single out of range code in lzw decode"); + next_code++; + } else if (code > next_code || next_code >= NUM_CODES) { fz_warn(stm->ctx, "out of range code encountered in lzw decode"); -- cgit v1.2.3