-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalues.py
90 lines (64 loc) · 2 KB
/
values.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
87
88
89
90
def armored(value):
if not value.will_collapse():
return '({0})'.format(value)
else:
return value
def print_prepared(value):
if isinstance(v1alue, int):
value = hex(value)
else:
value = armored(value)
return value
class Value:
def __and__(self, other):
return BitwiseAndResult(self, other)
def __or__(self, other):
return BitwiseOrResult(self, other)
def will_collapse(self):
raise NotImplementedError
class UnknownValue(Value):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
def will_collapse(self):
return True
class MemoryRead(Value):
def __init__(self, base, offset, size):
self.base = base
self.offset = offset
self.size = size
def will_collapse(self):
return True
def __str__(self):
return '({2})D[{0}+{1}]'.format(self.base, self.offset, 'b' + str(self.size * 8))
class BitwiseAndResult(Value):
def __init__(self, value1, value2):
self.v1, self.v2 = value1, value2
def will_collapse(self):
return isinstance(self.v1, int) and isinstance(self.v2, int)
def __str__(self):
v1 = self.v1
v2 = self.v2
if isinstance(v1, int) and isinstance(v2, int):
return str(v1 & v2)
else:
if isinstance(v2, int):
v2 = hex(v2)
else:
v2 = armored(v2)
return '{0} & {1}'.format(v1, v2)
class BitwiseOrResult(Value):
def __init__(self, value1, value2):
self.v1, self.v2 = value1, value2
def will_collapse(self):
return isinstance(self.v1, int) and isinstance(self.v2, int)
def __str__(self):
v1 = self.v1
v2 = self.v2
if isinstance(v1, int) and isinstance(v2, int):
return str(v1 | v2)
else:
v1 = print_prepared(v1)
v2 = print_prepared(v2)
return '{0} | {1}'.format(v1, v2)