about summary refs log tree commit diff stats
path: root/miasm/core/utils.py
diff options
context:
space:
mode:
authorFabrice Desclaux <fabrice.desclaux@cea.fr>2019-11-02 22:24:23 +0100
committerFabrice Desclaux <fabrice.desclaux@cea.fr>2019-11-12 15:10:44 +0100
commit4dc802e3544e669cfea1d6be8a01ca2a2600dfef (patch)
tree96a79b40469c1db9201ca46b210503d4dcf378c5 /miasm/core/utils.py
parent83e54bd2de945a36ab5ccd4cc5b94817d7cb0112 (diff)
downloadfocaccia-miasm-4dc802e3544e669cfea1d6be8a01ca2a2600dfef.tar.gz
focaccia-miasm-4dc802e3544e669cfea1d6be8a01ca2a2600dfef.zip
Clear get_str_* API
get_str_ansi decoded strings using utf8 and was blindly used for pure
windows function (LoadLibraryA) and for stdlib functions (printf,
strlen, ...) even if strlen does not use utf8...

New API is:

get_win_str_a/get_win_str_w and set_win_str_a/set_win_str_w for
windows (respectively codepage1252/windows utf16) .Those functions
should only be used in windows strings manipulations, so there are taken
out of the jitter.

get_c_str/set_c_str: as those functions are "classic" in OSes, they are
keeped in the jitter.
Diffstat (limited to 'miasm/core/utils.py')
-rw-r--r--miasm/core/utils.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/miasm/core/utils.py b/miasm/core/utils.py
index 9856d4f2..7667a656 100644
--- a/miasm/core/utils.py
+++ b/miasm/core/utils.py
@@ -1,4 +1,5 @@
 from __future__ import print_function
+import sys
 from builtins import range
 import struct
 import inspect
@@ -86,6 +87,21 @@ def force_bytes(value):
         return value
 
 
+def force_str(value):
+    if isinstance(value, str):
+        return value
+    elif isinstance(value, bytes):
+        out = ""
+        for i in range(len(value)):
+            # For Python2/Python3 compatibility
+            c = ord(value[i:i+1])
+            out += chr(c)
+        value = out
+    else:
+        raise ValueError("Unsupported type")
+    return value
+
+
 def iterbytes(string):
     for i in range(len(string)):
         yield string[i:i+1]