about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--miasm/core/utils.py12
-rw-r--r--miasm/loader/new_cstruct.py6
-rw-r--r--miasm/loader/pe.py2
3 files changed, 14 insertions, 6 deletions
diff --git a/miasm/core/utils.py b/miasm/core/utils.py
index 7667a656..37248c40 100644
--- a/miasm/core/utils.py
+++ b/miasm/core/utils.py
@@ -81,10 +81,16 @@ def printable(string):
 
 
 def force_bytes(value):
-    try:
-        return value.encode()
-    except AttributeError:
+    if isinstance(value, bytes):
         return value
+    if not isinstance(value, str):
+        return value
+    out = []
+    for c in value:
+        c = ord(c)
+        assert c < 0x100
+        out.append(c)
+    return bytes(out)
 
 
 def force_str(value):
diff --git a/miasm/loader/new_cstruct.py b/miasm/loader/new_cstruct.py
index ec591aa8..16c947a5 100644
--- a/miasm/loader/new_cstruct.py
+++ b/miasm/loader/new_cstruct.py
@@ -4,6 +4,7 @@ from __future__ import print_function
 import re
 import struct
 
+from miasm.core.utils import force_bytes
 from future.utils import PY3, viewitems, with_metaclass
 
 type2realtype = {}
@@ -213,9 +214,10 @@ class CStruct(with_metaclass(Cstruct_Metaclass, object)):
                 if cpt == None:
                     if value == None:
                         o = struct.calcsize(fmt) * b"\x00"
+                    elif ffmt.endswith('s'):
+                        new_value = force_bytes(value)
+                        o = struct.pack(self.sex + fmt, new_value)
                     else:
-                        if isinstance(value, str):
-                            value = value.encode()
                         o = struct.pack(self.sex + fmt, value)
                 else:
                     o = b""
diff --git a/miasm/loader/pe.py b/miasm/loader/pe.py
index e8208424..cd5d9370 100644
--- a/miasm/loader/pe.py
+++ b/miasm/loader/pe.py
@@ -267,7 +267,7 @@ class DescName(CStruct):
         return name, off + len(name) + 1
 
     def sets(self, value):
-        return bytes(value) + b"\x00"
+        return force_bytes(value) + b"\x00"
 
 
 class ImportByName(CStruct):