View Single Post
Old 09-19-2013, 01:32 PM   #576
NiLuJe
BLAM!
NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.
 
NiLuJe's Avatar
 
Posts: 13,477
Karma: 26012494
Join Date: Jun 2010
Location: Paris, France
Device: Kindle 2i, 3g, 4, 5w, PW, PW2, PW5; Kobo H2O, Forma, Elipsa, Sage, C2E
Another small patch that follows this Calibre commit, because I managed to dig up a file like this (one of the very first Kindle book I purchased, an old Mobi v6 file), and, yep, KindleUnpack blows up on it .

(The patch from #563 is folded into this, because I'm lazy )

Code:
--- KindleUnpack_v62-ori/lib/kindleunpack.py	2013-04-22 22:04:34.000000000 +0200
+++ KindleUnpack_v62-patch/lib/kindleunpack.py	2013-09-19 19:25:29.674186532 +0200
@@ -1006,6 +1006,10 @@ class MobiHeader:
                         addValue(name, str(value))
                     elif size == 12:
                         value, = struct.unpack('>L',content)
+                        # Handle NULL ptr for *Offset...
+                        if name[-6:] == 'Offset':
+                            if value == 0xffffffff:
+                                value = 0
                         addValue(name, str(value))
                     else:
                         addValue(name, content.encode('hex'))
@@ -1229,6 +1233,16 @@ def process_all_mobi_headers(files, sect
             # if reach here should be an image but double check to make sure
             # Get the proper file extension
             imgtype = imghdr.what(None, data)
+            # imghdr only checks for JFIF or Exif JPEG files. Apparently, there are some with only the magic JPEG bytes out there...
+            # ImageMagick handle those, so, do it too.
+            if imgtype is None and data[0:2] == b'\xFF\xD8':
+                # Get last non-null bytes
+                last = len(data)
+                while (data[last-1:last] == b'\x00'):
+                    last-=1
+                # Be extra safe, check the trailing bytes, too.
+                if data[last-2:last] == b'\xFF\xD9':
+                    imgtype = "jpeg"
             if imgtype is None:
                 print "Warning: Section %s does not contain a recognised resource" % i
                 imgnames.append(None)

Last edited by NiLuJe; 09-19-2013 at 01:35 PM.
NiLuJe is offline   Reply With Quote