about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorserpilliere <devnull@localhost>2012-07-30 20:45:29 +0200
committerserpilliere <devnull@localhost>2012-07-30 20:45:29 +0200
commitac28bc813ab39c8a56d65feacd952ece59ae7eac (patch)
tree9bd582f0da8505e2b4c2b614c5021ccd5bdb322f
parenta95d1b7bd1d8e8ca349c45fb83987a4baaf99025 (diff)
downloadmiasm-ac28bc813ab39c8a56d65feacd952ece59ae7eac.tar.gz
miasm-ac28bc813ab39c8a56d65feacd952ece59ae7eac.zip
emul_lib: fix vm_set_mem on multiple pages
-rw-r--r--miasm/tools/emul_lib/libcodenat_interface.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/miasm/tools/emul_lib/libcodenat_interface.c b/miasm/tools/emul_lib/libcodenat_interface.c
index e0307d37..a0eeb4b3 100644
--- a/miasm/tools/emul_lib/libcodenat_interface.c
+++ b/miasm/tools/emul_lib/libcodenat_interface.c
@@ -677,6 +677,7 @@ PyObject* _vm_set_mem(PyObject *addr, PyObject *item_str)
     Py_ssize_t length;
     int ret = 0x1337;
     unsigned int val;
+    unsigned int l;
 
     struct memory_page_node * mpn;
 
@@ -690,15 +691,25 @@ PyObject* _vm_set_mem(PyObject *addr, PyObject *item_str)
 	    RAISE(PyExc_TypeError,"arg1 must be int");
     }
 
-
     if(!PyString_Check(item_str))
        RAISE(PyExc_TypeError,"arg must be str");
 
     buf_size = PyString_Size(item_str);
     PyString_AsStringAndSize(item_str, &buf_data, &length);
 
-    mpn = get_memory_page_from_address(val);
-    memcpy(mpn->ad_hp + (val-mpn->ad), buf_data, buf_size);
+    /* read is multiple page wide */
+    while (buf_size){
+	    mpn = get_memory_page_from_address(val);
+	    if (!mpn){
+		    PyErr_SetString(PyExc_RuntimeError, "cannot find address");
+		    return 0;
+	    }
+	    l = MIN(buf_size, mpn->size - (val-mpn->ad));
+	    memcpy(mpn->ad_hp + (val-mpn->ad), buf_data, l);
+	    buf_data += l;
+	    val += l;
+	    buf_size -= l;
+    }
 
     return PyInt_FromLong((long)ret);
 }