about summary refs log tree commit diff stats
path: root/example/expression/manip_expression6.py
blob: 079faf7672f34de8d310f1b1ae16cb86cfb8b101 (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from miasm.expression.expression import *
from miasm.expression.expression_helper import *
import os

filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
    execfile(filename)

a = ExprId('a')
b = ExprId('b')
c = ExprId('c')
d = ExprId('d')


x = ExprMem(a+b+ExprInt32(0x42))

def replace_expr(e):
    #print 'visit', e
    dct = {c+ExprInt32(0x42):d,
           a+b:c,}
    if e in dct:
        return dct[e]
    return e


print x
y = x.visit(replace_expr)
print y
print x.copy()
print y.copy()
print y == y.copy()
print repr(y), repr(y.copy())


z = ExprCompose([(a[5:9], 0, 8), (b, 8, 24), (x, 24, 32)])
print z
print z.copy()
print z[:31].copy().visit(replace_expr)

print 'replace'
print x.replace_expr({c+ExprInt32(0x42):d,
                      a+b:c,})
print z.replace_expr({c+ExprInt32(0x42):d,
                      a+b:c,})


u = z.copy()
print u
u.args[-1][0].arg.args[1].arg = uint32(0x45)
print u
print z
print u == z

to_test = [(ExprInt32(5)+c+a+b-a+ExprInt32(1)-ExprInt32(5)),
           a+b+c-a-b-c+a,
           a+a+b+c-(a+(b+c)),
           c^b^a^c^b,
           a^ExprInt32(0),
           (a+b)-b,
           -(ExprInt32(0)-((a+b)-b)),

           ExprOp('<<<', a, ExprInt32(32)),
           ExprOp('>>>', a, ExprInt32(32)),
           ExprOp('>>>', a, ExprInt32(0)),
           ExprOp('<<', a, ExprInt32(0)),

           ExprOp('<<<', a, ExprOp('<<<', b, c)),
           ExprOp('<<<', ExprOp('<<<', a, b), c),
           ExprOp('<<<', ExprOp('>>>', a, b), c),
           ExprOp('>>>', ExprOp('<<<', a, b), c),
           ExprOp('>>>', ExprOp('<<<', a, b), b),


           ExprOp('>>>', ExprOp('<<<', a, ExprInt32(10)), ExprInt32(2)),

           ExprOp('>>>', ExprOp('<<<', a, ExprInt32(10)), ExprInt32(2)) ^ ExprOp('>>>', ExprOp('<<<', a, ExprInt32(10)), ExprInt32(2)),
           ExprOp(">>", (a & ExprInt32(0xF)), ExprInt32(0x15)),
           ExprOp("==", ExprInt32(12), ExprInt32(10)),
           ExprOp("==", ExprInt32(12), ExprInt32(12)),
           ExprOp("==", a|ExprInt32(12), ExprInt32(0)),
           ExprOp("==", a|ExprInt32(12), ExprInt32(14)),
           ExprOp("parity", ExprInt32(0xf)),
           ExprOp("parity", ExprInt32(0xe)),
           ExprInt32(0x4142)[:32],
           ExprInt32(0x4142)[:8],
           ExprInt32(0x4142)[8:16],
           a[:32],
           a[:8][:8],
           a[:16][:8],
           a[8:16][:8],
           a[8:32][:8],
           a[:16][8:16],
           ExprCompose([(a, 0, 32)]),
           ExprCompose([(a[:16], 0, 16)]),
           ExprCompose([(a[:16], 0, 16), (a, 16, 32)]),
           ExprCompose([(a[:16], 0, 16), (a[16:32], 16, 32)]),

           ExprMem(a)[:32],
           ExprMem(a)[:16],
           ]


for e in to_test:
    print "#"*80
    print e
    print e.visit(expr_simp)