From 597fd045a9cb52d1527609118af6226df5c7c832 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 30 May 2009 17:16:00 -0700 Subject: [PATCH] Align blocks in the indexing records --- src/calibre/ebooks/mobi/writer.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/mobi/writer.py b/src/calibre/ebooks/mobi/writer.py index 4548ead3a5..ba0ca6ead7 100644 --- a/src/calibre/ebooks/mobi/writer.py +++ b/src/calibre/ebooks/mobi/writer.py @@ -97,6 +97,14 @@ def decint(value, direction): bytes[-1] |= 0x80 return ''.join(chr(b) for b in reversed(bytes)) + +def align_block(raw, multiple=4, pad='\0'): + l = len(raw) + extra = l % multiple + if extra == 0: return raw + return raw + pad*(multiple - extra) + + def rescale_image(data, maxsizeb, dimen=None): image = Image.open(StringIO(data)) format = image.format @@ -484,7 +492,8 @@ class MobiWriter(object): last_name = "%4d"%c c += 1 - return indxt.getvalue(), c, indices.getvalue(), last_index, last_name + return align_block(indxt.getvalue()), c, \ + align_block(indices.getvalue()), last_index, last_name def _generate_index(self): @@ -521,12 +530,13 @@ class MobiWriter(object): indx1 = indx1.getvalue() idxt0 = chr(len(last_name)) + last_name + pack('>H', last_index) + idxt0 = align_block(idxt0) indx0 = StringIO() tagx = TAGX['periodical' if self.opts.mobi_periodical else 'chapter'] - tagx = 'TAGX' + pack('>I', 8 + len(tagx)) + tagx + tagx = align_block('TAGX' + pack('>I', 8 + len(tagx)) + tagx) indx0_indices_pos = 0xc0 + len(tagx) + len(idxt0) - indx0_indices = 'IDXT' + pack('>H', 0xc0 + len(tagx)) + indx0_indices = align_block('IDXT' + pack('>H', 0xc0 + len(tagx))) # Generate record header header = StringIO() @@ -622,7 +632,7 @@ class MobiWriter(object): for child in toc.iter(): add_node(child, 'chapter') - return ctoc.getvalue() + return align_block(ctoc.getvalue()) def _generate_images(self): self._oeb.logger.info('Serializing images...')