Skip to content

Commit

Permalink
Merge pull request #21 from cclauss/fix-undefined-names
Browse files Browse the repository at this point in the history
Fix undefined names in Python 3
  • Loading branch information
joxeankoret authored Nov 27, 2018
2 parents 47e9940 + 4e60d26 commit 5d96d69
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 23 deletions.
11 changes: 11 additions & 0 deletions exporters/SimpleEval.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@

from decimal import Decimal

try:
long # Python 2
except NameError:
long = int # Python 3

try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3


#-------------------------------------------------------------------------------
__version__ = '1.0'
__all__ = [
Expand Down
3 changes: 1 addition & 2 deletions exporters/base_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def constant_filter(value):
return False

#no single bits sets - mostly defines / flags
for i in xrange(64):
for i in range(64):
if value == (1 << i):
return False

Expand Down Expand Up @@ -699,4 +699,3 @@ def export(self):

def export_one(self, filename, args, is_c):
raise Exception("Not implemented in the inherited class")

25 changes: 16 additions & 9 deletions exporters/kfuzzy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""
from __future__ import print_function

import os
import sys
Expand All @@ -37,6 +38,12 @@ def modsum(buf):
except ImportError:
pass

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


class CFileStr(str):
fd = None

Expand Down Expand Up @@ -269,13 +276,13 @@ def hash_bytes(self, bytes, aggresive = False):
return hash1 + ";" + hash2 + ";" + hash3

def hash_file(self, filename, aggresive = False):
f = file(filename, "rb")
f = open(filename, "rb")
f.seek(0, 2)
size = f.tell()

if size > self.big_file_size:
print
print "Warning! Support for big files (%d MB > %d MB) is broken!" % (size/1024/1024, self.big_file_size / 1024 / 1024)
print()
print("Warning! Support for big files (%d MB > %d MB) is broken!" % (size/1024/1024, self.big_file_size / 1024 / 1024))
fbytes = CFileStr(f)
else:
f.seek(0)
Expand Down Expand Up @@ -331,27 +338,27 @@ def __init__(self, bytes):
self._kfd.algorithm = self._kfd.simplified

def usage():
print "Usage:", sys.argv[0], "<filename>"
print("Usage:", sys.argv[0], "<filename>")

def main(path):
hash = CKoretFuzzyHashing()
#hash.algorithm = hash._fast_hash

if os.path.isdir(path):
print "Signature;Simple Signature;Reverse Signature;Filename"
print("Signature;Simple Signature;Reverse Signature;Filename")
for root, dirs, files in os.walk(path):
for name in files:
tmp = os.path.join(root, name)
try:
ret = hash.hash_file(tmp, True)
print "%s;%s" % (ret, tmp)
print("%s;%s" % (ret, tmp))
except:
print "***ERROR with file %s" % tmp
print sys.exc_info()[1]
print("***ERROR with file %s" % tmp)
print(sys.exc_info()[1])
else:
hash = CKoretFuzzyHashing()
ret = hash.hash_file(path, True)
print "%s;%s" % (path, ret)
print("%s;%s" % (path, ret))

if __name__ == "__main__":
if len(sys.argv) == 1:
Expand Down
12 changes: 9 additions & 3 deletions exporters/simple_macro_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

from __future__ import print_function
import os
import re
import sys
Expand All @@ -26,6 +27,11 @@
from SimpleEval import SimpleEval
from kfuzzy import CKoretFuzzyHashing

try:
long # Python 2
except NameError:
long = int # Python 3

#-------------------------------------------------------------------------------
MACROS_REGEXP = '\W*#\W*define\W+([a-z0-9_]+)\W+([a-z0-9_]+)'
DEFAULT_ENUM = ""
Expand Down Expand Up @@ -122,16 +128,16 @@ def extract_from_buffer(self, buf):

#-------------------------------------------------------------------------------
def usage():
print "Usage: %s <source file>" % sys.argv[0]
print("Usage: %s <source file>" % sys.argv[0])

#-------------------------------------------------------------------------------
def main(filename):
extractor = CMacroExtractor()
enums = extractor.extract(filename)
for name in enums:
src = enums[name]
print src
print
print(src)
print()

if __name__ == "__main__":
if len(sys.argv) == 1:
Expand Down
11 changes: 4 additions & 7 deletions ml/pigaios_create_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import sqlite3
import numpy as np

#-------------------------------------------------------------------------------
# Hack for Python3 compat
try:
INTEGER_TYPES = (int, long)
long # Python 2
except NameError:
long = int
INTEGER_TYPES = (int,)
long = int # Python 3


#-------------------------------------------------------------------------------
_DEBUG = False
Expand Down Expand Up @@ -143,7 +141,7 @@ def get_compare_functions_data(self, row, src_id, bin_id, heur):

if field == "switchs_json":
ret[field] = int(row["src_%s" % field] == row["bin_%s" % field])
elif type(row["src_%s" % field]) in INTEGER_TYPES:
elif type(row["src_%s" % field]) in (int, long):
ret["src_%s" % field] = int(row["src_%s" % field])
ret["bin_%s" % field] = int(row["bin_%s" % field])
ret["%s_diff" % field] = abs(row["src_%s" % field] - row["bin_%s" % field])
Expand Down Expand Up @@ -243,4 +241,3 @@ def main(src_db, bin_db, dataset):
main(sys.argv[1], sys.argv[2], sys.argv[3])
else:
usage()

11 changes: 10 additions & 1 deletion sourceimp_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@

from others.py3compat import INTEGER_TYPES

try:
reload # Python 2
except NameError: # Python 3
from importlib import reload

try:
from sourcexp_ida import log
from_ida = True
Expand All @@ -46,6 +51,11 @@
except ImportError:
has_ml = False

try:
long # Python 2
except NameError:
long = int # Python 3

#-----------------------------------------------------------------------
def sourceimp_log(msg):
print("[%s] %s" % (time.asctime(), msg))
Expand Down Expand Up @@ -869,4 +879,3 @@ def choose_best_matches(self, is_final = False):
if tmp_id != src_id:
if _DEBUG: self.dubious_matches[src_id] = self.best_matches[src_id]
del self.best_matches[src_id]

7 changes: 6 additions & 1 deletion sourceimp_ida.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
reg_write_string)

import sourceimp_core

try:
reload # Python 2
except NameError: # Python 3
from importlib import reload

reload(sourceimp_core)

from sourceimp_core import *
Expand Down Expand Up @@ -583,4 +589,3 @@ def main():
raise
finally:
hide_wait_box()

0 comments on commit 5d96d69

Please sign in to comment.