diff options
| author | Camille Mougey <commial@gmail.com> | 2017-05-16 17:39:54 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-16 17:39:54 +0200 |
| commit | 627669759b4bdcfb220f098a141c183621477cd6 (patch) | |
| tree | 356cc62e4b7bb55b31d4ad82f20dc19d58344410 /example/expression/expr_c.py | |
| parent | 3260f7867827195ea7c6ec37bc3a8687ce998f6d (diff) | |
| parent | 47fb50d611b5ba8eb7e4dc827b522123642ca3b4 (diff) | |
| download | miasm-627669759b4bdcfb220f098a141c183621477cd6.tar.gz miasm-627669759b4bdcfb220f098a141c183621477cd6.zip | |
Merge pull request #557 from serpilliere/fix_obj
Fix obj
Diffstat (limited to 'example/expression/expr_c.py')
| -rw-r--r-- | example/expression/expr_c.py | 94 |
1 files changed, 43 insertions, 51 deletions
diff --git a/example/expression/expr_c.py b/example/expression/expr_c.py index b3e59658..7adc7b50 100644 --- a/example/expression/expr_c.py +++ b/example/expression/expr_c.py @@ -4,67 +4,59 @@ Parse C expression to access variables and retrieve information: * variable type """ -from miasm2.core.ctypesmngr import CTypesManagerNotPacked +from miasm2.core.ctypesmngr import CTypeStruct, CAstTypes, CTypePtr from miasm2.arch.x86.ctype import CTypeAMD64_unk -from miasm2.core.objc import CHandler -from miasm2.core.objc import ObjCPtr +from miasm2.core.objc import CTypesManagerNotPacked, CHandler 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) +""" +C manipulation example +""" - # Add C types definition - types_mngr.add_c_decl(text) +# Digest C informations +text = """ +struct line { + char color[20]; + int size; +}; + +struct rectangle { + unsigned int width; + unsigned int length; + struct line* line; +}; +""" - # 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) +# Type manager for x86 64: structures not packed +base_types = CTypeAMD64_unk() +types_ast = CAstTypes() +# Add C types definition +types_ast.add_c_decl(text) - ptr = ExprId('ptr', 64) - expr_types = {ptr.name: ptr_rectangle} +types_mngr = CTypesManagerNotPacked(types_ast, base_types) - mychandler = CHandler(types_mngr, expr_types) +# Create the ptr variable with type "struct rectangle*" +ptr_rectangle = types_mngr.get_objc(CTypePtr(CTypeStruct('rectangle'))) +ptr = ExprId('ptr', 64) +expr_types = {ptr.name: ptr_rectangle} - # Parse some C accesses - c_acceses = ["ptr->width", - "ptr->length", - "ptr->line", - "ptr->line->color", - "ptr->line->color[3]", - "ptr->line->size" - ] +mychandler = CHandler(types_mngr, expr_types) - 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 +# Parse some C accesses +c_acceses = ["ptr->width", + "ptr->length", + "ptr->line", + "ptr->line->color", + "ptr->line->color[3]", + "ptr->line->size" + ] -if __name__ == '__main__': - test() +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 |