Commit 44ec0bd851e561350f04830f7713e1f0bcb15bf5

Authored by decalage2
1 parent ce32342a

olevba: fixed issue #58 with format() to support Python 2.6

Showing 1 changed file with 19 additions and 19 deletions
oletools/olevba.py
@@ -175,9 +175,10 @@ https://github.com/unixfreak0037/officeparser @@ -175,9 +175,10 @@ https://github.com/unixfreak0037/officeparser
175 # 2016-05-12 CH: - added support for PowerPoint 97-2003 files 175 # 2016-05-12 CH: - added support for PowerPoint 97-2003 files
176 # 2016-06-06 CH: - improved handling of unicode VBA module names 176 # 2016-06-06 CH: - improved handling of unicode VBA module names
177 # 2016-06-07 CH: - added option --relaxed, stricter parsing by default 177 # 2016-06-07 CH: - added option --relaxed, stricter parsing by default
178 -# 2016-06-12 v0.47.1 PL: - fixed small bugs in VBA parsing code 178 +# 2016-06-12 v0.48 PL: - fixed small bugs in VBA parsing code
  179 +# 2016-07-01 PL: - fixed issue #58 with format() to support Python 2.6
179 180
180 -__version__ = '0.47.1' 181 +__version__ = '0.48'
181 182
182 #------------------------------------------------------------------------------ 183 #------------------------------------------------------------------------------
183 # TODO: 184 # TODO:
@@ -307,7 +308,7 @@ class OlevbaBaseException(Exception): @@ -307,7 +308,7 @@ class OlevbaBaseException(Exception):
307 def __init__(self, msg, filename=None, orig_exc=None, **kwargs): 308 def __init__(self, msg, filename=None, orig_exc=None, **kwargs):
308 if orig_exc: 309 if orig_exc:
309 super(OlevbaBaseException, self).__init__(msg + 310 super(OlevbaBaseException, self).__init__(msg +
310 - ' ({})'.format(orig_exc), 311 + ' ({0})'.format(orig_exc),
311 **kwargs) 312 **kwargs)
312 else: 313 else:
313 super(OlevbaBaseException, self).__init__(msg, **kwargs) 314 super(OlevbaBaseException, self).__init__(msg, **kwargs)
@@ -358,8 +359,8 @@ class UnexpectedDataError(OlevbaBaseException): @@ -358,8 +359,8 @@ class UnexpectedDataError(OlevbaBaseException):
358 359
359 def __init__(self, stream_path, variable, expected, value): 360 def __init__(self, stream_path, variable, expected, value):
360 super(UnexpectedDataError, self).__init__(self, 361 super(UnexpectedDataError, self).__init__(self,
361 - 'Unexpected value in {} for variable {}: '  
362 - 'expected {:04X but found {:04X}!' 362 + 'Unexpected value in {0} for variable {1}: '
  363 + 'expected {2:04X} but found {3:04X}!'
363 .format(stream_path, variable, expected, value)) 364 .format(stream_path, variable, expected, value))
364 self.stream_path = stream_path 365 self.stream_path = stream_path
365 self.variable = variable 366 self.variable = variable
@@ -1148,7 +1149,7 @@ def _extract_vba(ole, vba_root, project_path, dir_path, relaxed=False): @@ -1148,7 +1149,7 @@ def _extract_vba(ole, vba_root, project_path, dir_path, relaxed=False):
1148 """ 1149 """
1149 # Open the PROJECT stream: 1150 # Open the PROJECT stream:
1150 project = ole.openstream(project_path) 1151 project = ole.openstream(project_path)
1151 - log.debug('relaxed is {}'.format(relaxed)) 1152 + log.debug('relaxed is %s' % relaxed)
1152 1153
1153 # sample content of the PROJECT stream: 1154 # sample content of the PROJECT stream:
1154 1155
@@ -1577,12 +1578,12 @@ def _extract_vba(ole, vba_root, project_path, dir_path, relaxed=False): @@ -1577,12 +1578,12 @@ def _extract_vba(ole, vba_root, project_path, dir_path, relaxed=False):
1577 code_data = ole.openstream(code_path).read() 1578 code_data = ole.openstream(code_path).read()
1578 break 1579 break
1579 except IOError as ioe: 1580 except IOError as ioe:
1580 - log.debug('failed to open stream VBA/{} ({}), try other name'  
1581 - .format(uni_out(stream_name), ioe)) 1581 + log.debug('failed to open stream VBA/%r (%r), try other name'
  1582 + % (uni_out(stream_name), ioe))
1582 1583
1583 if code_data is None: 1584 if code_data is None:
1584 - log.info("Could not open stream {} of {} ('VBA/' + one of {})!"  
1585 - .format(projectmodule_index, projectmodules_count, 1585 + log.info("Could not open stream %d of %d ('VBA/' + one of %r)!"
  1586 + % (projectmodule_index, projectmodules_count,
1586 '/'.join("'" + uni_out(stream_name) + "'" 1587 '/'.join("'" + uni_out(stream_name) + "'"
1587 for stream_name in try_names))) 1588 for stream_name in try_names)))
1588 if relaxed: 1589 if relaxed:
@@ -1612,7 +1613,7 @@ def _extract_vba(ole, vba_root, project_path, dir_path, relaxed=False): @@ -1612,7 +1613,7 @@ def _extract_vba(ole, vba_root, project_path, dir_path, relaxed=False):
1612 except (UnexpectedDataError, SubstreamOpenError): 1613 except (UnexpectedDataError, SubstreamOpenError):
1613 raise 1614 raise
1614 except Exception as exc: 1615 except Exception as exc:
1615 - log.info('Error parsing module {} of {} in _extract_vba:' 1616 + log.info('Error parsing module {0} of {1} in _extract_vba:'
1616 .format(projectmodule_index, projectmodules_count), 1617 .format(projectmodule_index, projectmodules_count),
1617 exc_info=True) 1618 exc_info=True)
1618 if not relaxed: 1619 if not relaxed:
@@ -1897,7 +1898,7 @@ def print_json(json_dict=None, _json_is_last=False, **json_parts): @@ -1897,7 +1898,7 @@ def print_json(json_dict=None, _json_is_last=False, **json_parts):
1897 'key=value parts but got both)') 1898 'key=value parts but got both)')
1898 elif (json_dict is not None) and (not isinstance(json_dict, dict)): 1899 elif (json_dict is not None) and (not isinstance(json_dict, dict)):
1899 raise ValueError('Invalid json argument: want either single dict or ' 1900 raise ValueError('Invalid json argument: want either single dict or '
1900 - 'key=value parts but got {} instead of dict)' 1901 + 'key=value parts but got {0} instead of dict)'
1901 .format(type(json_dict))) 1902 .format(type(json_dict)))
1902 if json_parts: 1903 if json_parts:
1903 json_dict = json_parts 1904 json_dict = json_parts
@@ -1909,12 +1910,12 @@ def print_json(json_dict=None, _json_is_last=False, **json_parts): @@ -1909,12 +1910,12 @@ def print_json(json_dict=None, _json_is_last=False, **json_parts):
1909 lines = json.dumps(json2ascii(json_dict), check_circular=False, 1910 lines = json.dumps(json2ascii(json_dict), check_circular=False,
1910 indent=4, ensure_ascii=False).splitlines() 1911 indent=4, ensure_ascii=False).splitlines()
1911 for line in lines[:-1]: 1912 for line in lines[:-1]:
1912 - print ' {}'.format(line) 1913 + print ' {0}'.format(line)
1913 if _json_is_last: 1914 if _json_is_last:
1914 - print ' {}'.format(lines[-1]) # print last line without comma 1915 + print ' {0}'.format(lines[-1]) # print last line without comma
1915 print ']' 1916 print ']'
1916 else: 1917 else:
1917 - print ' {},'.format(lines[-1]) # print last line with comma 1918 + print ' {0},'.format(lines[-1]) # print last line with comma
1918 1919
1919 1920
1920 class VBA_Scanner(object): 1921 class VBA_Scanner(object):
@@ -2260,7 +2261,7 @@ class VBA_Parser(object): @@ -2260,7 +2261,7 @@ class VBA_Parser(object):
2260 self.type = TYPE_OpenXML 2261 self.type = TYPE_OpenXML
2261 except OlevbaBaseException as exc: 2262 except OlevbaBaseException as exc:
2262 if self.relaxed: 2263 if self.relaxed:
2263 - log.info('Error {} caught in Zip/OpenXML parsing for file {}' 2264 + log.info('Error {0} caught in Zip/OpenXML parsing for file {1}'
2264 .format(exc, self.filename)) 2265 .format(exc, self.filename))
2265 log.debug('Trace:', exc_info=True) 2266 log.debug('Trace:', exc_info=True)
2266 else: 2267 else:
@@ -2300,7 +2301,7 @@ class VBA_Parser(object): @@ -2300,7 +2301,7 @@ class VBA_Parser(object):
2300 relaxed=self.relaxed)) 2301 relaxed=self.relaxed))
2301 except OlevbaBaseException as exc: 2302 except OlevbaBaseException as exc:
2302 if self.relaxed: 2303 if self.relaxed:
2303 - log.info('Error parsing subfile {}: {}' 2304 + log.info('Error parsing subfile {0}: {1}'
2304 .format(fname, exc)) 2305 .format(fname, exc))
2305 log.debug('Trace:', exc_info=True) 2306 log.debug('Trace:', exc_info=True)
2306 else: 2307 else:
@@ -2583,8 +2584,7 @@ class VBA_Parser(object): @@ -2583,8 +2584,7 @@ class VBA_Parser(object):
2583 data = ole._open(d.isectStart, d.size).read() 2584 data = ole._open(d.isectStart, d.size).read()
2584 log.debug('Read %d bytes' % len(data)) 2585 log.debug('Read %d bytes' % len(data))
2585 if len(data) > 200: 2586 if len(data) > 200:
2586 - log.debug('{}...[much more data]...{}'  
2587 - .format(repr(data[:100]), repr(data[-50:]))) 2587 + log.debug('%r...[much more data]...%r' % (data[:100], data[-50:]))
2588 else: 2588 else:
2589 log.debug(repr(data)) 2589 log.debug(repr(data))
2590 if 'Attribut' in data: 2590 if 'Attribut' in data: