diff --git a/src/calibre/ebooks/mobi/debug.py b/src/calibre/ebooks/mobi/debug.py index 12bdb41f4b..1279ba7793 100644 --- a/src/calibre/ebooks/mobi/debug.py +++ b/src/calibre/ebooks/mobi/debug.py @@ -844,6 +844,7 @@ class TextRecord(object): # {{{ def __init__(self, idx, record, extra_data_flags, decompress): self.trailing_data, self.raw = get_trailing_data(record.raw, extra_data_flags) + raw_trailing_bytes = record.raw[len(self.raw):] self.raw = decompress(self.raw) if 0 in self.trailing_data: self.trailing_data['multibyte_overlap'] = self.trailing_data.pop(0) @@ -851,6 +852,7 @@ class TextRecord(object): # {{{ self.trailing_data['indexing'] = self.trailing_data.pop(1) if 2 in self.trailing_data: self.trailing_data['uncrossable_breaks'] = self.trailing_data.pop(2) + self.trailing_data['raw_bytes'] = raw_trailing_bytes self.idx = idx diff --git a/src/calibre/ebooks/mobi/utils.py b/src/calibre/ebooks/mobi/utils.py index 839374af70..6df9db3b3b 100644 --- a/src/calibre/ebooks/mobi/utils.py +++ b/src/calibre/ebooks/mobi/utils.py @@ -191,7 +191,7 @@ def encode_trailing_data(raw): where size is a backwards encoded vwi whose value is the length of the - entire return bytestring. + entire returned bytestring. data is the bytestring passed in as raw. This is the encoding used for trailing data entries at the end of text records. See get_trailing_data() for details. diff --git a/src/calibre/ebooks/mobi/writer2/indexer.py b/src/calibre/ebooks/mobi/writer2/indexer.py index 14c5328622..f121e29835 100644 --- a/src/calibre/ebooks/mobi/writer2/indexer.py +++ b/src/calibre/ebooks/mobi/writer2/indexer.py @@ -14,7 +14,7 @@ from collections import OrderedDict, defaultdict from calibre.ebooks.mobi.writer2 import RECORD_SIZE from calibre.ebooks.mobi.utils import (encint, encode_number_as_hex, - encode_trailing_data, encode_tbs, align_block, utf8_text) + encode_tbs, align_block, utf8_text) class CNCX(object): # {{{ @@ -198,7 +198,7 @@ class TBS(object): # {{{ # This can happen if a record contains only text between # the periodical start and the first section byts = self.type_011 - self.bytestring = encode_trailing_data(byts) + self.bytestring = byts else: depth_map = defaultdict(list) for x in ('starts', 'ends', 'completes'): @@ -209,7 +209,7 @@ class TBS(object): # {{{ self.periodical_tbs(data, first, depth_map) else: if not data: - self.bytestring = encode_trailing_data(b'') + self.bytestring = b'' else: self.book_tbs(data, first) @@ -302,10 +302,10 @@ class TBS(object): # {{{ buf.write(encode_tbs(spanner.index - parent_section_index, {0b0001: 0})) - self.bytestring = encode_trailing_data(buf.getvalue()) + self.bytestring = buf.getvalue() def book_tbs(self, data, first): - self.bytestring = encode_trailing_data(b'') + self.bytestring = b'' # }}} class Indexer(object): # {{{ diff --git a/src/calibre/ebooks/mobi/writer2/main.py b/src/calibre/ebooks/mobi/writer2/main.py index e13afa2ba7..44c471d3d4 100644 --- a/src/calibre/ebooks/mobi/writer2/main.py +++ b/src/calibre/ebooks/mobi/writer2/main.py @@ -99,7 +99,7 @@ class MobiWriter(object): for i in xrange(len(self.records)): if i == 0: continue tbs = self.indexer.get_trailing_byte_sequence(i) - self.records[i] += tbs + self.records[i] += encode_trailing_data(tbs) self.records.extend(self.indexer.records) @property @@ -212,15 +212,15 @@ class MobiWriter(object): if self.compression == PALMDOC: data = compress_doc(data) record = StringIO() - record.write(data) - self.records.append(record.getvalue()) nrecords += 1 data, overlap = self.read_text_record(text) + record.write(data) - # Write information about the mutibyte character overlap, if any + # Write information about the multibyte character overlap, if any record.write(overlap) record.write(pack(b'>B', len(overlap))) + self.records.append(record.getvalue()) self.last_text_record_idx = nrecords