about summary refs log tree commit diff stats
path: root/example/expression/access_c.py
diff options
context:
space:
mode:
authorFabrice Desclaux <fabrice.desclaux@cea.fr>2017-05-15 21:25:57 +0200
committerFabrice Desclaux <fabrice.desclaux@cea.fr>2017-05-15 21:29:34 +0200
commit47fb50d611b5ba8eb7e4dc827b522123642ca3b4 (patch)
tree356cc62e4b7bb55b31d4ad82f20dc19d58344410 /example/expression/access_c.py
parentd67d9ff0742fcf632a5a955c6e36e8b8cc15ec93 (diff)
downloadmiasm-47fb50d611b5ba8eb7e4dc827b522123642ca3b4.tar.gz
miasm-47fb50d611b5ba8eb7e4dc827b522123642ca3b4.zip
Example: clean code
Diffstat (limited to 'example/expression/access_c.py')
-rw-r--r--example/expression/access_c.py116
1 files changed, 57 insertions, 59 deletions
diff --git a/example/expression/access_c.py b/example/expression/access_c.py
index 8a1e2962..9ba33822 100644
--- a/example/expression/access_c.py
+++ b/example/expression/access_c.py
@@ -127,62 +127,60 @@ class MyCHandler(CHandler):
     exprToAccessC_cls = MyExprToAccessC
 
 
-def test(data):
-    # Digest C informations
-    text = """
-    struct human {
-            unsigned short age;
-            unsigned int height;
-            char name[50];
-    };
-
-    struct ll_human {
-            struct ll_human* next;
-            struct human human;
-    };
-    """
-
-    base_types = CTypeAMD64_unk()
-    types_ast = CAstTypes()
-    types_ast.add_c_decl(text)
-
-    types_mngr = CTypesManagerNotPacked(types_ast, base_types)
-
-    # Analyze binary
-    cont = Container.fallback_container(data, None, addr=0)
-
-    machine = Machine("x86_64")
-    dis_engine, ira = machine.dis_engine, machine.ira
-
-    mdis = dis_engine(cont.bin_stream, symbol_pool=cont.symbol_pool)
-    addr_head = 0
-    blocks = mdis.dis_multibloc(addr_head)
-    lbl_head = mdis.symbol_pool.getby_offset(addr_head)
-
-    ir_arch_a = ira(mdis.symbol_pool)
-    for block in blocks:
-        ir_arch_a.add_bloc(block)
-
-    open('graph_irflow.dot', 'w').write(ir_arch_a.graph.dot())
-
-    # Main function's first argument's type is "struct ll_human*"
-    ptr_llhuman = types_mngr.get_objc(CTypePtr(CTypeStruct('ll_human')))
-    arg0 = ExprId('ptr', 64)
-    ctx = {ir_arch_a.arch.regs.RDI: arg0}
-    expr_types = {arg0.name: ptr_llhuman}
-
-    mychandler = MyCHandler(types_mngr, expr_types)
-
-    for expr in get_funcs_arg0(ctx, ir_arch_a, lbl_head):
-        print "Access:", expr
-        target_types = mychandler.expr_to_types(expr)
-        for target_type in target_types:
-            print '\tType:', target_type
-        c_strs = mychandler.expr_to_c(expr)
-        for c_str in c_strs:
-            print "\tC access:", c_str
-        print
-
-
-if __name__ == '__main__':
-    test(open(sys.argv[1]).read())
+
+data = open(sys.argv[1]).read()
+# Digest C informations
+text = """
+struct human {
+        unsigned short age;
+        unsigned int height;
+        char name[50];
+};
+
+struct ll_human {
+        struct ll_human* next;
+        struct human human;
+};
+"""
+
+base_types = CTypeAMD64_unk()
+types_ast = CAstTypes()
+types_ast.add_c_decl(text)
+
+types_mngr = CTypesManagerNotPacked(types_ast, base_types)
+
+# Analyze binary
+cont = Container.fallback_container(data, None, addr=0)
+
+machine = Machine("x86_64")
+dis_engine, ira = machine.dis_engine, machine.ira
+
+mdis = dis_engine(cont.bin_stream, symbol_pool=cont.symbol_pool)
+addr_head = 0
+blocks = mdis.dis_multibloc(addr_head)
+lbl_head = mdis.symbol_pool.getby_offset(addr_head)
+
+ir_arch_a = ira(mdis.symbol_pool)
+for block in blocks:
+    ir_arch_a.add_bloc(block)
+
+open('graph_irflow.dot', 'w').write(ir_arch_a.graph.dot())
+
+# Main function's first argument's type is "struct ll_human*"
+ptr_llhuman = types_mngr.get_objc(CTypePtr(CTypeStruct('ll_human')))
+arg0 = ExprId('ptr', 64)
+ctx = {ir_arch_a.arch.regs.RDI: arg0}
+expr_types = {arg0.name: ptr_llhuman}
+
+mychandler = MyCHandler(types_mngr, expr_types)
+
+for expr in get_funcs_arg0(ctx, ir_arch_a, lbl_head):
+    print "Access:", expr
+    target_types = mychandler.expr_to_types(expr)
+    for target_type in target_types:
+        print '\tType:', target_type
+    c_strs = mychandler.expr_to_c(expr)
+    for c_str in c_strs:
+        print "\tC access:", c_str
+    print
+