diff --git a/pefile.py b/pefile.py index d7e93a1..395799e 100644 --- a/pefile.py +++ b/pefile.py @@ -23,7 +23,6 @@ from __future__ import division from __future__ import print_function -from past.builtins import cmp, long from builtins import bytes from builtins import chr from builtins import object @@ -55,6 +54,9 @@ PY3 = sys.version_info > (3,) +if PY3: + long = int + def count_zeroes(data): try: # newbytes' count() takes a str in Python 2 @@ -566,28 +568,6 @@ def get_sublang_name_for_lang( lang_value, sublang_value ): return SUBLANG.get(sublang_value, ['*unknown*'])[0] -def convert_to_printable(s): - """Convert string to printable string. - @param s: string. - @return: sanitized string. - """ - printable = True - for c in s: - if c not in string.printable: - printable = False - break - if printable: - return s - else: - new_string = '' - for c in s: - if c in string.printable: - new_string += c - else: - new_string += "\\x%02x" % ord(c) - return new_string - - # Ange Albertini's code to process resources' strings # def parse_strings(data, counter, l): @@ -988,7 +968,9 @@ def dump_dict(self): except ValueError as e: val = '0x%-8X [INVALID TIME]' % val else: - val = b([b for b in val if b != 0]) + val = ''.join(chr(d) if chr(d) in string.printable + else "\\x%02x" % d for d in + [ord(c) if not isinstance(c, int) else c for c in val]) dump_dict[key] = {'FileOffset': self.__field_offsets__[key] + self.__file_offset__, 'Offset': self.__field_offsets__[key], @@ -4237,7 +4219,7 @@ def get_resources_strings(self): if hasattr(resource_id, 'directory'): if hasattr(resource_id.directory, 'strings') and resource_id.directory.strings: for res_string in list(resource_id.directory.strings.values()): - resources_strings.append( res_string ) + resources_strings.append(res_string) return resources_strings @@ -4973,7 +4955,10 @@ def dump_dict(self, dump=None): resource_id_list.append(resource_lang_dict) if hasattr(resource_id.directory, 'strings') and resource_id.directory.strings: for idx, res_string in list(resource_id.directory.strings.items()): - resource_id_list.append(convert_to_printable(res_string)) + resource_id_list.append(res_string.encode( + 'unicode-escape', + 'backslashreplace').decode( + 'ascii')) if ( hasattr(self, 'DIRECTORY_ENTRY_TLS') and diff --git a/tests/pefile_test.py b/tests/pefile_test.py index 2cba082..97891ae 100644 --- a/tests/pefile_test.py +++ b/tests/pefile_test.py @@ -41,6 +41,7 @@ def test_pe_image_regression_test(self): try: pe = pefile.PE(pe_filename) pe_file_data = pe.dump_info() + pe_file_dict_data = pe.dump_dict() # Make sure that it does not fail pe_file_data = pe_file_data.replace('\n\r', '\n') except Exception as excp: print('Failed processing [%s]' % os.path.basename(pe_filename))