From 046c2cfac39e686dee65cdf54035dc5a975bc57f Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Mon, 27 Nov 2017 23:18:12 +0100
Subject: Added offset and address translations to the Python bindings.

---
 ChangeLog                            |   6 ++
 plugins/pychrysa/arch/vmpa.c         |  15 +++--
 plugins/pychrysa/format/executable.c | 107 +++++++++++++++++++++++++++++++++++
 3 files changed, 123 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index aaabcbf..ba00eee 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+17-11-27  Cyrille Bagard <nocbos@gmail.com>
+
+	* plugins/pychrysa/arch/vmpa.c:
+	* plugins/pychrysa/format/executable.c:
+	Add offset and address translations to the Python bindings.
+
 17-11-26  Cyrille Bagard <nocbos@gmail.com>
 
 	* src/arch/x86/Makefile.am:
diff --git a/plugins/pychrysa/arch/vmpa.c b/plugins/pychrysa/arch/vmpa.c
index 8d42a78..af1557d 100644
--- a/plugins/pychrysa/arch/vmpa.c
+++ b/plugins/pychrysa/arch/vmpa.c
@@ -159,25 +159,30 @@ static PyObject *py_vmpa_to_str(PyObject *obj)
 {
     PyObject *result;                       /* Chaîne à retourner          */
     vmpa2t *addr;                           /* Véritable adresse manipulée */
-    off_t physical;                         /* Position physique           */
-    uint64_t virtual;                       /* Adresse virtuelle           */
+    phys_t physical;                        /* Position physique           */
+    virt_t virtual;                         /* Adresse virtuelle           */
+    VMPA_BUFFER(phys_str);                  /* Version humaine de position */
+    VMPA_BUFFER(virt_str);                  /* Version humaine d'adresse   */
 
     addr = &((py_vmpa_t *)obj)->addr;
 
     physical = get_phy_addr(addr);
     virtual = get_virt_addr(addr);
 
+    vmpa2_phys_to_string(addr, MDS_UNDEFINED, phys_str, NULL);
+    vmpa2_phys_to_string(addr, MDS_UNDEFINED, virt_str, NULL);
+
     if (physical == VMPA_NO_PHYSICAL && virtual == VMPA_NO_VIRTUAL)
         result = PyUnicode_FromFormat("<phy=None, virt=None>");
 
     else if (physical != VMPA_NO_PHYSICAL && virtual == VMPA_NO_VIRTUAL)
-        result = PyUnicode_FromFormat("<phy=%d, virt=None>", physical);
+        result = PyUnicode_FromFormat("<phy=%s, virt=None>", phys_str);
 
     else if (physical == VMPA_NO_PHYSICAL && virtual != VMPA_NO_VIRTUAL)
-        result = PyUnicode_FromFormat("<phy=None, virt=0x%08x>", virtual);
+        result = PyUnicode_FromFormat("<phy=None, virt=%s>", virt_str);
 
     else
-        result = PyUnicode_FromFormat("<phy=%d, virt=0x%08x>", physical, virtual);
+        result = PyUnicode_FromFormat("<phy=%s, virt=%s>", phys_str, virt_str);
 
     return result;
 
diff --git a/plugins/pychrysa/format/executable.c b/plugins/pychrysa/format/executable.c
index 96b5e82..1b1bfe8 100644
--- a/plugins/pychrysa/format/executable.c
+++ b/plugins/pychrysa/format/executable.c
@@ -33,9 +33,106 @@
 
 #include "format.h"
 #include "../helpers.h"
+#include "../arch/vmpa.h"
 
 
 
+/* Fournit l'emplacement correspondant à une position physique. */
+static PyObject *py_exe_format_translate_offset_into_vmpa(PyObject *, PyObject *);
+
+/* Fournit l'emplacement correspondant à une adresse virtuelle. */
+static PyObject *py_exe_format_translate_address_into_vmpa(PyObject *, PyObject *);
+
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : self = description de l'exécutable à consulter.              *
+*                args = arguments accompagnant l'appel.                       *
+*                                                                             *
+*  Description : Fournit l'emplacement correspondant à une position physique. *
+*                                                                             *
+*  Retour      : Position correspondante ou None.                             *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static PyObject *py_exe_format_translate_offset_into_vmpa(PyObject *self, PyObject *args)
+{
+    PyObject *result;                       /* Instance à retourner        */
+    GExeFormat *format;                     /* Version GLib du format      */
+    unsigned long long off;                 /* Adresse en mémoire virtuelle*/
+    int ret;                                /* Bilan de lecture des args.  */
+    vmpa2t pos;                             /* Position complète déterminée*/
+    bool status;                            /* Bilan de l'opération        */
+
+    format = G_EXE_FORMAT(pygobject_get(self));
+    assert(format != NULL);
+
+    ret = PyArg_ParseTuple(args, "K", &off);
+    if (!ret) return NULL;
+
+    status = g_exe_format_translate_offset_into_vmpa(format, off, &pos);
+
+    if (status)
+        result = build_from_internal_vmpa(&pos);
+
+    else
+    {
+        result = Py_None;
+        Py_INCREF(result);
+    }
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : self = description de l'exécutable à consulter.              *
+*                args = arguments accompagnant l'appel.                       *
+*                                                                             *
+*  Description : Fournit l'emplacement correspondant à une adresse virtuelle. *
+*                                                                             *
+*  Retour      : Position correspondante ou None.                             *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static PyObject *py_exe_format_translate_address_into_vmpa(PyObject *self, PyObject *args)
+{
+    PyObject *result;                       /* Instance à retourner        */
+    GExeFormat *format;                     /* Version GLib du format      */
+    unsigned long long addr;                /* Adresse en mémoire virtuelle*/
+    int ret;                                /* Bilan de lecture des args.  */
+    vmpa2t pos;                             /* Position complète déterminée*/
+    bool status;                            /* Bilan de l'opération        */
+
+    format = G_EXE_FORMAT(pygobject_get(self));
+    assert(format != NULL);
+
+    ret = PyArg_ParseTuple(args, "K", &addr);
+    if (!ret) return NULL;
+
+    status = g_exe_format_translate_address_into_vmpa(format, addr, &pos);
+
+    if (status)
+        result = build_from_internal_vmpa(&pos);
+
+    else
+    {
+        result = Py_None;
+        Py_INCREF(result);
+    }
+
+    return result;
+
+}
+
+
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : -                                                            *
@@ -51,6 +148,16 @@
 PyTypeObject *get_python_executable_format_type(void)
 {
     static PyMethodDef py_exe_format_methods[] = {
+        {
+            "translate_offset_into_vmpa", py_exe_format_translate_offset_into_vmpa,
+            METH_VARARGS,
+            "translate_offset_into_vmpa($self, off, /)\n--\n\nTranslate a physical offset to a full location.."
+        },
+        {
+            "translate_address_into_vmpa", py_exe_format_translate_address_into_vmpa,
+            METH_VARARGS,
+            "translate_address_into_vmpa($self, addr, /)\n--\n\nTranslate a physical offset to a full location.."
+        },
         { NULL }
     };
 
-- 
cgit v0.11.2-87-g4458