diff options
| author | Camille Mougey <commial@gmail.com> | 2017-02-17 15:04:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-02-17 15:04:56 +0100 |
| commit | 688d21da608c32da84e093316ab32a4fa7fc51c8 (patch) | |
| tree | e1f70d61f6d323fce31a9d9b9ed3d9fb4bb3c8be /example/expression/expr_c.py | |
| parent | d30ea3788133fffc294602ee6cce88b407caaf4a (diff) | |
| parent | c3bef0610972451a5eef3accb7da4618794ddff9 (diff) | |
| download | miasm-688d21da608c32da84e093316ab32a4fa7fc51c8.tar.gz miasm-688d21da608c32da84e093316ab32a4fa7fc51c8.zip | |
Merge pull request #495 from serpilliere/objc_feature
Objc feature
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() |