about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--example/expression/expr_c.py6
-rw-r--r--miasm2/core/objc.py29
-rw-r--r--test/expr_type/test_chandler.py11
3 files changed, 33 insertions, 13 deletions
diff --git a/example/expression/expr_c.py b/example/expression/expr_c.py
index e0825799..37c9f510 100644
--- a/example/expression/expr_c.py
+++ b/example/expression/expr_c.py
@@ -42,7 +42,7 @@ ptr_rectangle = types_mngr.get_objc(CTypePtr(CTypeStruct('rectangle')))
 
 ptr = ExprId('ptr', 64)
 c_context = {ptr.name: ptr_rectangle}
-mychandler = CHandler(types_mngr, {})
+mychandler = CHandler(types_mngr, C_types=c_context)
 
 # Parse some C accesses
 c_acceses = ["ptr->width",
@@ -54,8 +54,8 @@ c_acceses = ["ptr->width",
             ]
 
 for c_str in c_acceses:
-    expr = mychandler.c_to_expr(c_str, c_context)
-    c_type = mychandler.c_to_type(c_str, c_context)
+    expr = mychandler.c_to_expr(c_str)
+    c_type = mychandler.c_to_type(c_str)
     print 'C access:', c_str
     print '\tExpr:', expr
     print '\tType:', c_type
diff --git a/miasm2/core/objc.py b/miasm2/core/objc.py
index 64a935a7..14352c7b 100644
--- a/miasm2/core/objc.py
+++ b/miasm2/core/objc.py
@@ -1584,7 +1584,8 @@ class CHandler(object):
     exprCToExpr_cls = ExprCToExpr
     exprToAccessC_cls = ExprToAccessC
 
-    def __init__(self, types_mngr, expr_types,
+    def __init__(self, types_mngr, expr_types=None,
+                 C_types=None,
                  simplify_c=access_simplifier,
                  enforce_strict_access=True):
         self.exprc2expr = self.exprCToExpr_cls(expr_types, types_mngr)
@@ -1593,7 +1594,12 @@ class CHandler(object):
                                                    enforce_strict_access)
         self.types_mngr = types_mngr
         self.simplify_c = simplify_c
+        if expr_types is None:
+            expr_types = {}
         self.expr_types = expr_types
+        if C_types is None:
+            C_types = {}
+        self.C_types = C_types
 
     def updt_expr_types(self, expr_types):
         """Update expr_types
@@ -1643,32 +1649,41 @@ class CHandler(object):
         return set(access.ctype
                    for access in self.expr_to_c_access(expr, expr_context))
 
-    def c_to_expr_and_type(self, c_str, c_context):
+    def c_to_expr_and_type(self, c_str, c_context=None):
         """Convert a C string expression to a Miasm expression and it's
         corresponding c type
         @c_str: C string
-        @c_context: a dictionary linking known tokens (strings) to its type.
+        @c_context: (optional) dictionary linking known tokens (strings) to its
+        type.
         """
 
         ast = parse_access(c_str)
+        if c_context is None:
+            c_context = self.C_types
         access_c = ast_get_c_access_expr(ast, c_context)
         return self.exprc2expr.get_expr(access_c, c_context)
 
-    def c_to_expr(self, c_str, c_context):
+    def c_to_expr(self, c_str, c_context=None):
         """Convert a C string expression to a Miasm expression
         @c_str: C string
-        @c_context: a dictionary linking known tokens (strings) to its type.
+        @c_context: (optional) dictionary linking known tokens (strings) to its
+        type.
         """
 
+        if c_context is None:
+            c_context = self.C_types
         expr, _ = self.c_to_expr_and_type(c_str, c_context)
         return expr
 
-    def c_to_type(self, c_str, c_context):
+    def c_to_type(self, c_str, c_context=None):
         """Get the type of a C string expression
         @expr: Miasm expression
-        @c_context: a dictionary linking known tokens (strings) to its type.
+        @c_context: (optional) dictionary linking known tokens (strings) to its
+        type.
         """
 
+        if c_context is None:
+            c_context = self.C_types
         _, ctype = self.c_to_expr_and_type(c_str, c_context)
         return ctype
 
diff --git a/test/expr_type/test_chandler.py b/test/expr_type/test_chandler.py
index 9729eb50..09c588cb 100644
--- a/test/expr_type/test_chandler.py
+++ b/test/expr_type/test_chandler.py
@@ -507,7 +507,7 @@ tests = [
 
 ]
 
-mychandler = CHandler(types_mngr, expr_types)
+mychandler = CHandler(types_mngr, expr_types=expr_types, C_types=c_context)
 exprc2expr = ExprCToExpr(expr_types, types_mngr)
 mychandler.updt_expr_types(expr_types)
 
@@ -530,8 +530,8 @@ for (expr, result) in tests:
 
 
     for out_type, out_str in computed:
-        parsed_expr = mychandler.c_to_expr(out_str, c_context)
-        parsed_type = mychandler.c_to_type(out_str, c_context)
+        parsed_expr = mychandler.c_to_expr(out_str)
+        parsed_type = mychandler.c_to_type(out_str)
         print "Access expr:", parsed_expr
         print "Access type:", parsed_type
 
@@ -544,6 +544,11 @@ for (expr, result) in tests:
         assert parsed_expr == parsed_expr_bis
         assert parsed_type == parsed_type_bis
 
+        parsed_expr_3, parsed_type_3 = mychandler.c_to_expr_and_type(out_str)
+        assert parsed_expr_3 is not None
+        assert parsed_expr == parsed_expr_3
+        assert parsed_type == parsed_type_3
+
         expr_new1 = expr_simp(parsed_expr)
         expr_new2 = expr_simp(expr)
         print "\t", expr_new1