diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-02-25 11:09:54 +0100 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-03-05 16:52:49 +0100 |
| commit | 02bbb30efea4980c9d133947cbbf69fb599071ad (patch) | |
| tree | 3fea6826fcc5354840a27cb1dc99ff31eef81896 /example/jitter/example_types.py | |
| parent | eab809932871f91d6f4aa770fc321af9e156e0f5 (diff) | |
| download | miasm-02bbb30efea4980c9d133947cbbf69fb599071ad.tar.gz miasm-02bbb30efea4980c9d133947cbbf69fb599071ad.zip | |
Support python2/python3
Diffstat (limited to 'example/jitter/example_types.py')
| -rwxr-xr-x | example/jitter/example_types.py | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/example/jitter/example_types.py b/example/jitter/example_types.py index bcf9bf70..d0751bbd 100755 --- a/example/jitter/example_types.py +++ b/example/jitter/example_types.py @@ -4,7 +4,9 @@ For a more complete view of what is possible, tests/core/types.py covers most of the module possibilities, and the module doc gives useful information as well. """ +from __future__ import print_function +from miasm2.core.utils import iterbytes from miasm2.analysis.machine import Machine from miasm2.core.types import MemStruct, Self, Void, Str, Array, Ptr, \ Num, Array, set_allocator @@ -145,9 +147,9 @@ class DataStr(MemStruct): ] -print "This script demonstrates a LinkedList implementation using the types " -print "module in the first part, and how to play with some casts in the second." -print +print("This script demonstrates a LinkedList implementation using the types ") +print("module in the first part, and how to play with some casts in the second.") +print() # A random jitter # You can also use miasm2.jitter.VmMngr.Vm(), but it does not happen in real @@ -172,17 +174,17 @@ assert link.size == 3 # If you get it directly from the VM, it is updated as well raw_size = vm.get_mem(link.get_addr("size"), link.get_type() .get_field_type("size").size) -assert raw_size == '\x03\x00\x00\x00' +assert raw_size == b'\x03\x00\x00\x00' -print "The linked list just built:" -print repr(link), '\n' +print("The linked list just built:") +print(repr(link), '\n') -print "Its uninitialized data elements:" +print("Its uninitialized data elements:") for data in link: # __iter__ returns MemVoids here, just cast them to the real data type real_data = data.cast(DataArray) - print repr(real_data) -print + print(repr(real_data)) +print() # Now let's play with one data data = link.pop(DataArray) @@ -196,9 +198,9 @@ assert data.arrayptr.deref == data.array # Let's say that it is a DataStr: datastr = data.cast(DataStr) -print "First element casted to DataStr:" -print repr(datastr) -print +print("First element casted to DataStr:") +print(repr(datastr)) +print() # data and datastr really share the same memory: data.val1 = 0x34 @@ -212,29 +214,29 @@ memstr = datastr.data.deref # Note that memstr is Str("utf16") memstr.val = 'Miams' -print "Cast data.array to MemStr and set the string value:" -print repr(memstr) -print +print("Cast data.array to MemStr and set the string value:") +print(repr(memstr)) +print() # If you followed, memstr and data.array point to the same object, so: -raw_miams = '\x00'.join('Miams') + '\x00'*3 -raw_miams_array = [ord(c) for c in raw_miams] +raw_miams = 'Miams'.encode('utf-16le') + b'\x00'*2 +raw_miams_array = [ord(c) for c in iterbytes(raw_miams)] assert list(data.array)[:len(raw_miams_array)] == raw_miams_array assert data.array.cast(Str("utf16")) == memstr # Default is "ansi" assert data.array.cast(Str()) != memstr assert data.array.cast(Str("utf16")).val == memstr.val -print "See that the original array has been modified:" -print repr(data) -print +print("See that the original array has been modified:") +print(repr(data)) +print() # Some type manipulation examples, for example let's construct an argv for # a program: # Let's say that we have two arguments, +1 for the program name and +1 for the # final null ptr in argv, the array has 4 elements: argv_t = Array(Ptr("<I", Str()), 4) -print "3 arguments argv type:", argv_t +print("3 arguments argv type:", argv_t) # alloc argv somewhere argv = argv_t.lval(vm) @@ -249,10 +251,10 @@ argv[3].val = 0 # If you changed your mind on the second arg, you could do: argv[2].deref.val = "42" -print "An argv instance:", repr(argv) -print "argv values:", repr([val.deref.val for val in argv[:-1]]) -print +print("An argv instance:", repr(argv)) +print("argv values:", repr([val.deref.val for val in argv[:-1]])) +print() -print "See test/core/types.py and the miasm2.core.types module doc for " -print "more information." +print("See test/core/types.py and the miasm2.core.types module doc for ") +print("more information.") |