From 02bbb30efea4980c9d133947cbbf69fb599071ad Mon Sep 17 00:00:00 2001 From: Fabrice Desclaux Date: Mon, 25 Feb 2019 11:09:54 +0100 Subject: Support python2/python3 --- example/jitter/example_types.py | 54 +++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'example/jitter/example_types.py') 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("