Skip to content

Fix \binN #BDATA parsing in RTF reader #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions pyth/plugins/rtf15/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ def parse(self):
elif nextbyte == b'\\':
control, digits = self.getControl()
self.group.handle(control, digits)

# '\binN #BDATA' is considered a single character as per spec.
# It also must skip decoding, so it's best to handle it here
if control == b'bin':
self.group.flushChars()
self.group.content.append(self.source.read(int(digits)))
continue

else:
self.group.char(nextbyte) # within-group text

Expand Down Expand Up @@ -237,8 +245,9 @@ def flushRun(self):

if self.isImage:
self.block.content.append(
document.Image(self.propStack[-1].copy(),
[b"".join(self.run)]))
document.Image({prop: value for prop, value in self.propStack[-1].items()
if prop not in document.Text.validProperties},
[b"".join(bdata for bdata in self.run if bdata)]))
self.isImage = False
else:
self.block.content.append(
Expand Down Expand Up @@ -312,6 +321,11 @@ def handle_str(self, bit):
self.run.append(bit)


def handle_bytes(self, bit):
# Binary data in Python 3
self.run.append(bit)


def handle_Push(self, _):
self.propStack.append(self.propStack[-1].copy())

Expand Down Expand Up @@ -421,7 +435,7 @@ def handle(self, control, digits):
b'picw', b'pich', b'picwgoal', b'pichgoal', b'picscalex', b'picscaley',
b'picscaled', b'piccropt', b'piccropb', b'piccropr', b'piccropl', b'picbmp',
b'picbpp', b'bin', b'blipupi', b'blipuid', b'bliptag', b'wbitmap']:
self.content.append(ImageMarker(control, digits))
self.content.append(ImageMarker(control.decode('ascii'), digits))
return

handler = getattr(self, 'handle_%s' % control.decode('ascii'), None)
Expand Down