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)