diff options
| author | serpilliere <devnull@localhost> | 2013-03-12 13:13:51 +0100 |
|---|---|---|
| committer | serpilliere <devnull@localhost> | 2013-03-12 13:13:51 +0100 |
| commit | 3dba1885e0e1464cabd73dadf23dec5f69a25d60 (patch) | |
| tree | 362dc7a45bd7829dc2255104658e56b7bc8c5eb2 /miasm/expression/expression.py | |
| parent | 7b41686343c332ec614098fdf0d66111d0ece120 (diff) | |
| parent | f38372e5e052453c4878cbf0516a23e59b4d712b (diff) | |
| download | focaccia-miasm-3dba1885e0e1464cabd73dadf23dec5f69a25d60.tar.gz focaccia-miasm-3dba1885e0e1464cabd73dadf23dec5f69a25d60.zip | |
merge
Diffstat (limited to 'miasm/expression/expression.py')
| -rw-r--r-- | miasm/expression/expression.py | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/miasm/expression/expression.py b/miasm/expression/expression.py index 6bba7526..8763ab24 100644 --- a/miasm/expression/expression.py +++ b/miasm/expression/expression.py @@ -823,3 +823,106 @@ def ExprInt_from(e, i): return ExprInt(tab_uintsize[e.get_size()](i)) +def get_expr_ids_visit(e, ids): + if isinstance(e, ExprId): + ids.add(e) + return e + +def get_expr_ids(e): + ids = set() + e.visit(lambda x:get_expr_ids_visit(x, ids)) + return ids + +def test_set(e, v, tks, result): + if not v in tks: + return e == v + if v in result and result[v] != e: + return False + result[v] = e + return result + +def MatchExpr(e, m, tks, result = None): + """ + try to match m expression with e expression with tks jokers + result is output dictionnary with matching joker values + """ + if result == None: + result = {} + #print 'match', e, m, tks, result + if m in tks: + return test_set(e, m, tks, result) + if isinstance(e, ExprInt): + return test_set(e, m, tks, result) + elif isinstance(e, ExprId): + return test_set(e, m, tks, result) + elif isinstance(e, ExprOp): + if not isinstance(m, ExprOp): + return False + for a1, a2 in zip(e.args, m.args): + r = MatchExpr(a1, a2, tks, result) + if r == False: + return False + return result + elif isinstance(e, ExprMem): + if not isinstance(m, ExprMem): + return False + if e.size != m.size: + return False + return MatchExpr(e.arg, m.arg, tks, result) + elif isinstance(e, ExprSlice): + if not isinstance(m, ExprSlice): + return False + if e.start != m.start or e.stop != m.stop: + return False + return MatchExpr(e.arg, m.arg, tks, result) + elif isinstance(e, ExprCond): + if not isinstance(m, ExprCond): + return False + r = MatchExpr(e.cond, m.cond, tks, result) + if not r: return False + r = MatchExpr(e.src1, m.src1, tks, result) + if not r: return False + r = MatchExpr(e.src2, m.src2, tks, result) + if not r: return False + return result + elif isinstance(e, ExprCompose): + if not isinstance(m, ExprCompose): + return False + for a1, a2 in zip(e.args, m.args): + if a1[1] != a2[1] or a1[2] != a2[2]: + return False + r = MatchExpr(a1[0], a2[0], tks, result) + if not r: + return False + return result + else: + fds +if __name__ == '__main__': + x = ExprId('x') + y = ExprId('y') + z = ExprId('z') + a = ExprId('a') + b = ExprId('b') + c = ExprId('c') + + print MatchExpr(x, a, [a]) + print MatchExpr(ExprInt32(12), a, [a]) + print MatchExpr(x+y, a, [a]) + print MatchExpr(x+y, a+y, [a]) + print MatchExpr(x+y, x+a, [a]) + print MatchExpr(x+y, a+b, [a, b]) + print MatchExpr(x+ExprId(12), a+b, [a, b]) + print MatchExpr(ExprMem(x), a, [a]) + print MatchExpr(ExprMem(x), ExprMem(a), [a]) + print MatchExpr(x[0:8], a, [a]) + print MatchExpr(x[0:8], a[0:8], [a]) + print MatchExpr(ExprCond(x, y, z), a, [a]) + print MatchExpr(ExprCond(x, y, z), + ExprCond(a, b, c), [a, b, c]) + print MatchExpr(ExprCompose([(x, 0, 8), (y, 8, 16)]), a, [a]) + print MatchExpr(ExprCompose([(x, 0, 8), (y, 8, 16)]), + ExprCompose([(a, 0, 8), (b, 8, 16)]), [a, b]) + + e1 = ExprMem((a&ExprInt32(0xFFFFFFFC))+ExprInt32(0x10), 32) + e2 = ExprMem((a&ExprInt32(0xFFFFFFFC))+b, 32) + print MatchExpr(e1, e2, [b]) |