about summary refs log tree commit diff stats
path: root/example/jitter/example_types.py
diff options
context:
space:
mode:
authorCamille Mougey <commial@gmail.com>2019-03-07 14:37:07 +0100
committerGitHub <noreply@github.com>2019-03-07 14:37:07 +0100
commit4c2320b46250a8d6f8774e1218544b72a154cd8e (patch)
treeb67e7b072439f84109bd39dad8ed7f3f135224f8 /example/jitter/example_types.py
parenteab809932871f91d6f4aa770fc321af9e156e0f5 (diff)
parent26c1075723a02984da6d3bc7423c5c0c43082dc3 (diff)
downloadmiasm-4c2320b46250a8d6f8774e1218544b72a154cd8e.tar.gz
miasm-4c2320b46250a8d6f8774e1218544b72a154cd8e.zip
Merge pull request #990 from serpilliere/support_python2_python3
Support python2 python3
Diffstat (limited to 'example/jitter/example_types.py')
-rwxr-xr-xexample/jitter/example_types.py64
1 files changed, 33 insertions, 31 deletions
diff --git a/example/jitter/example_types.py b/example/jitter/example_types.py
index bcf9bf70..653adaf9 100755
--- a/example/jitter/example_types.py
+++ b/example/jitter/example_types.py
@@ -1,14 +1,16 @@
 #! /usr/bin/env python2
-"""This script is just a short example of common usages for miasm2.core.types.
+"""This script is just a short example of common usages for miasm.core.types.
 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.analysis.machine import Machine
-from miasm2.core.types import MemStruct, Self, Void, Str, Array, Ptr, \
+from miasm.core.utils import iterbytes
+from miasm.analysis.machine import Machine
+from miasm.core.types import MemStruct, Self, Void, Str, Array, Ptr, \
                               Num, Array, set_allocator
-from miasm2.os_dep.common import heap
+from miasm.os_dep.common import heap
 
 # Instantiate a heap
 my_heap = heap()
@@ -145,12 +147,12 @@ 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
+# You can also use miasm.jitter.VmMngr.Vm(), but it does not happen in real
 # life scripts, so here is the usual way:
 jitter = Machine("x86_32").jitter("python")
 vm = jitter.vm
@@ -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 miasm.core.types module doc for ")
+print("more information.")