about summary refs log tree commit diff stats
path: root/example/expression/expr_c.py
diff options
context:
space:
mode:
Diffstat (limited to 'example/expression/expr_c.py')
-rw-r--r--example/expression/expr_c.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/example/expression/expr_c.py b/example/expression/expr_c.py
new file mode 100644
index 00000000..b3e59658
--- /dev/null
+++ b/example/expression/expr_c.py
@@ -0,0 +1,70 @@
+"""
+Parse C expression to access variables and retrieve information:
+* Miasm expression to access this variable
+* variable type
+"""
+
+from miasm2.core.ctypesmngr import CTypesManagerNotPacked
+from miasm2.arch.x86.ctype import CTypeAMD64_unk
+from miasm2.core.objc import CHandler
+from miasm2.core.objc import ObjCPtr
+from miasm2.expression.expression import ExprId
+
+
+def test():
+    """
+    C manipulation example
+    """
+
+    # Digest C informations
+    text = """
+    struct line {
+            char color[20];
+            int size;
+    };
+
+    struct rectangle {
+            unsigned int width;
+            unsigned int length;
+            struct line* line;
+    };
+    """
+
+    # Type manager for x86 64: structures not packed
+    my_types = CTypeAMD64_unk()
+    types_mngr = CTypesManagerNotPacked(my_types.types)
+
+    # Add C types definition
+    types_mngr.add_c_decl(text)
+
+    # Create the ptr variable with type "struct rectangle*"
+    void_ptr = types_mngr.void_ptr
+    rectangle = types_mngr.get_type(('rectangle',))
+    ptr_rectangle = ObjCPtr('noname', rectangle,
+                            void_ptr.align, void_ptr.size)
+
+
+    ptr = ExprId('ptr', 64)
+    expr_types = {ptr.name: ptr_rectangle}
+
+    mychandler = CHandler(types_mngr, expr_types)
+
+
+    # Parse some C accesses
+    c_acceses = ["ptr->width",
+                 "ptr->length",
+                 "ptr->line",
+                 "ptr->line->color",
+                 "ptr->line->color[3]",
+                 "ptr->line->size"
+                ]
+
+    for c_str in c_acceses:
+        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
+
+if __name__ == '__main__':
+    test()