Skip to content

Commit

Permalink
Removed obsolete code, fixed some issues with Python 2/3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
erocarrera committed May 26, 2017
1 parent aa44f2d commit ef87dda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
37 changes: 11 additions & 26 deletions pefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/pefile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit ef87dda

Please sign in to comment.