From 238d0172ad7f52243c3f6c68cef52d71454c0b9f Mon Sep 17 00:00:00 2001 From: decalage2 Date: Mon, 11 Feb 2019 22:32:08 +0100 Subject: [PATCH] olevba: fixed parsing of VBA text files on Python 3 (issue #106) --- oletools/olevba.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/oletools/olevba.py b/oletools/olevba.py index a4203f9..f04ff97 100644 --- a/oletools/olevba.py +++ b/oletools/olevba.py @@ -2021,9 +2021,13 @@ def vba_collapse_long_lines(vba_code): :return: str, VBA module code with long lines collapsed """ # TODO: use a regex instead, to allow whitespaces after the underscore? - vba_code = vba_code.replace(' _\r\n', ' ') - vba_code = vba_code.replace(' _\r', ' ') - vba_code = vba_code.replace(' _\n', ' ') + try: + vba_code = vba_code.replace(' _\r\n', ' ') + vba_code = vba_code.replace(' _\r', ' ') + vba_code = vba_code.replace(' _\n', ' ') + except: + log.exception('type(vba_code)=%s' % type(vba_code)) + raise return vba_code @@ -2890,7 +2894,9 @@ class VBA_Parser(object): """ log.info('Opening text file %s' % self.filename) # directly store the source code: - self.vba_code_all_modules = data + # On Python 2, store it as a raw bytes string + # On Python 3, convert it to unicode assuming it was encoded with UTF-8 + self.vba_code_all_modules = bytes2str(data) self.contains_macros = True # set type only if parsing succeeds self.type = TYPE_TEXT -- libgit2 0.21.4