diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2017-02-13 12:56:25 +0100 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2017-02-17 14:53:45 +0100 |
| commit | de06847ea27d841d885cac5030c75bf63b58f5c6 (patch) | |
| tree | 65994518fa3ed5ff4b23fffa06fb432802a3ec0e /example/expression/expr_c.py | |
| parent | f66a44ed97f792596c84f1110e1d563ac8146766 (diff) | |
| download | miasm-de06847ea27d841d885cac5030c75bf63b58f5c6.tar.gz miasm-de06847ea27d841d885cac5030c75bf63b58f5c6.zip | |
Test: add access_c/expr_c example
Diffstat (limited to 'example/expression/expr_c.py')
| -rw-r--r-- | example/expression/expr_c.py | 70 |
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() |