From 3df3d5423af01f63103ec671e1a7ef2a07836431 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 2 Dec 2015 09:31:29 +0530 Subject: [PATCH] MOBI Input: Warn about corrupted trailing data entries, instead of aborting. Getting some, even partially corrupted text is better than no text. See #1521830 (Calibre will not open or convert good mobi file) --- src/calibre/ebooks/mobi/reader/mobi6.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/mobi/reader/mobi6.py b/src/calibre/ebooks/mobi/reader/mobi6.py index 25fc4b6ffd..baa93352ea 100644 --- a/src/calibre/ebooks/mobi/reader/mobi6.py +++ b/src/calibre/ebooks/mobi/reader/mobi6.py @@ -38,6 +38,7 @@ class MobiReader(object): self.log = log self.debug = debug self.embedded_mi = None + self.warned_about_trailing_entry_corruption = False self.base_css_rules = textwrap.dedent(''' body { text-align: justify } @@ -740,12 +741,21 @@ class MobiReader(object): flags = self.book_header.extra_flags >> 1 while flags: if flags & 1: - num += sizeof_trailing_entry(data, size - num) + try: + num += sizeof_trailing_entry(data, size - num) + except IndexError: + self.warn_about_trailing_entry_corruption() + return 0 flags >>= 1 if self.book_header.extra_flags & 1: num += (ord(data[size - num - 1]) & 0x3) + 1 return num + def warn_about_trailing_entry_corruption(self): + if not self.warned_about_trailing_entry_corruption: + self.warned_about_trailing_entry_corruption = True + self.log.warn('The trailing data entries in this MOBI file are corrupted, you might see corrupted text in the output') + def text_section(self, index): data = self.sections[index][0] trail_size = self.sizeof_trailing_entries(data)