-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.py
86 lines (67 loc) · 1.91 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import re
import string, random
from subprocess import Popen, PIPE
RE_PATTERN = (
"(.*)_"
+ "(gcc-[.0-9]+|clang-[.0-9]+|"
+ "clang-obfus-[-a-z2]+|"
+ "gcc|clang)_"
+ "((?:x86|arm|mips|mipseb|ppc)_(?:32|64))_"
+ "(O0|O1|O2|O3|Os)_"
+ "(.*)"
)
RESTR = re.compile(RE_PATTERN)
def system(cmd):
proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
o = proc.communicate()[0].decode().strip()
return o
def randstr(length):
return "".join(random.choice(string.ascii_lowercase) for i in range(length))
def gettmpdir():
tmpdir = os.path.join("/tmp", "parsfi_tmp", randstr(10))
while os.path.exists(tmpdir):
tmpdir = os.path.join("/tmp", "parsfi_tmp", randstr(10))
os.makedirs(tmpdir)
return tmpdir
def get_file_type(fname, use_str=False):
BITS_32 = "ELF 32-bit"
BITS_64 = "ELF 64-bit"
ARCH_X86_32 = "Intel 80386"
ARCH_X86_64 = "x86-64"
ARCH_ARM = "ARM"
ARCH_MIPS = "MIPS"
ENDIAN_LSB = "LSB"
ENDIAN_MSB = "MSB"
if use_str:
s = fname
else:
# TODO: detect file type by parsing ELF file structure
fname = os.path.realpath(fname)
s = system('file "{0}"'.format(fname))
if BITS_32 in s:
bits = "32"
elif BITS_64 in s:
bits = "64"
else:
raise NotImplemented
if ARCH_X86_32 in s or ARCH_X86_64 in s:
arch = "x86"
elif ARCH_ARM in s:
arch = "arm"
elif ARCH_MIPS in s:
arch = "mips"
else:
raise NotImplemented
if ENDIAN_LSB in s:
endian = ""
elif ENDIAN_MSB in s:
endian = "eb"
else:
raise NotImplemented
return "{0}{1}_{2}".format(arch, endian, bits)
def get_dirs(base_dir, suffix=""):
src_dir = os.path.join(base_dir, "sources")
out_dir = os.path.join(base_dir, "output" + suffix)
log_dir = os.path.join(base_dir, "logs" + suffix)
return src_dir, out_dir, log_dir