From a18e35cfec34e4f74ca02b94e6308595a66848c2 Mon Sep 17 00:00:00 2001 From: "Marshall T. Vandegrift" Date: Tue, 18 Nov 2008 20:15:32 -0500 Subject: [PATCH] Fix #1045: Recover from decompression errors, skipping bad chunks. --- src/calibre/ebooks/lit/reader.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/lit/reader.py b/src/calibre/ebooks/lit/reader.py index 861c37a51e..fdda7acc91 100644 --- a/src/calibre/ebooks/lit/reader.py +++ b/src/calibre/ebooks/lit/reader.py @@ -771,15 +771,21 @@ class LitReader(object): raise("Reset table entry out of bounds") if bytes_remaining >= window_bytes: lzx.reset() - result.append( - lzx.decompress(content[base:size], window_bytes)) + try: + result.append( + lzx.decompress(content[base:size], window_bytes)) + except lzx.LzxError: + self._warn("LZX decompression error; skipping chunk") bytes_remaining -= window_bytes base = size accum += int32(reset_table[RESET_INTERVAL:]) ofs_entry += 8 if bytes_remaining < window_bytes and bytes_remaining > 0: lzx.reset() - result.append(lzx.decompress(content[base:], bytes_remaining)) + try: + result.append(lzx.decompress(content[base:], bytes_remaining)) + except lzx.LzxError: + self._warn("LZX decompression error; skipping chunk") bytes_remaining = 0 if bytes_remaining > 0: raise LitError("Failed to completely decompress section") @@ -826,6 +832,9 @@ class LitReader(object): if not os.path.isdir(dir): os.makedirs(dir) + def _warn(self, msg): + print "WARNING: %s" % (msg,) + def option_parser(): from calibre.utils.config import OptionParser parser = OptionParser(usage=_('%prog [options] LITFILE'))