LRF Input: Fix a regression in calibre 5 that broke parsing of some LRF files

Fixes #1958115 [Viewing or converting LRF fails with TypeError](https://bugs.launchpad.net/calibre/+bug/1958115)
This commit is contained in:
Kovid Goyal 2022-01-17 12:06:31 +05:30
parent 3488e9107e
commit d72a4a106f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -32,7 +32,7 @@ class LRFObject:
a[i] ^= xorKey a[i] ^= xorKey
i+=1 i+=1
l-=1 l-=1
return a.tostring() return a.tobytes()
@classmethod @classmethod
def parse_empdots(self, tag, f): def parse_empdots(self, tag, f):
@ -799,8 +799,9 @@ class Text(LRFStream):
def initialize(self): def initialize(self):
self.content = collections.deque() self.content = collections.deque()
stream = io.BytesIO(self.stream) s = self.stream or b''
length = len(self.stream) stream = io.BytesIO(s)
length = len(s)
style = self.style.as_dict() style = self.style.as_dict()
current_style = style.copy() current_style = style.copy()
text_tags = set(list(TextAttr.tag_map.keys()) + text_tags = set(list(TextAttr.tag_map.keys()) +
@ -813,7 +814,7 @@ class Text(LRFStream):
# Is there some text before a tag? # Is there some text before a tag?
def find_first_tag(start): def find_first_tag(start):
pos = self.stream.find(b'\xf5', start) pos = s.find(b'\xf5', start)
if pos == -1: if pos == -1:
return -1 return -1
try: try:
@ -830,10 +831,10 @@ class Text(LRFStream):
tag_pos = find_first_tag(start_pos) tag_pos = find_first_tag(start_pos)
if tag_pos >= start_pos: if tag_pos >= start_pos:
if tag_pos > start_pos: if tag_pos > start_pos:
self.add_text(self.stream[start_pos:tag_pos]) self.add_text(s[start_pos:tag_pos])
stream.seek(tag_pos) stream.seek(tag_pos)
else: # No tags in this stream else: # No tags in this stream
self.add_text(self.stream) self.add_text(s)
stream.seek(0, 2) stream.seek(0, 2)
break break
@ -969,8 +970,9 @@ class Canvas(LRFStream):
if hasattr(self, attr): if hasattr(self, attr):
self.attrs[attr] = getattr(self, attr) self.attrs[attr] = getattr(self, attr)
self._contents = [] self._contents = []
stream = io.BytesIO(self.stream) s = self.stream or b''
while stream.tell() < len(self.stream): stream = io.BytesIO(s)
while stream.tell() < len(s):
tag = Tag(stream) tag = Tag(stream)
try: try:
self._contents.append( self._contents.append(
@ -1202,7 +1204,7 @@ class TocLabel:
class TOCObject(LRFStream): class TOCObject(LRFStream):
def initialize(self): def initialize(self):
stream = io.BytesIO(self.stream) stream = io.BytesIO(self.stream or b'')
c = struct.unpack("<H", stream.read(2))[0] c = struct.unpack("<H", stream.read(2))[0]
stream.seek(4*(c+1)) stream.seek(4*(c+1))
self._contents = [] self._contents = []