From ce27af7f442d1fa580311eac83bc44d7db4e0d05 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 28 Oct 2018 00:54:30 +0200
Subject: Accepted integer values as usable addresses for some Python bindings.

---
 plugins/pychrysalide/arch/processor.c    | 87 +++++++++++++++++++++++++++-----
 plugins/pychrysalide/arch/processor.h    | 25 +++++++++
 plugins/pychrysalide/format/executable.c | 82 +++++++++++++++++++++++++++++-
 plugins/pychrysalide/format/executable.h | 25 +++++++++
 plugins/pychrysalide/format/format.c     | 46 ++++++++---------
 5 files changed, 226 insertions(+), 39 deletions(-)

diff --git a/plugins/pychrysalide/arch/processor.c b/plugins/pychrysalide/arch/processor.c
index 3ac18fa..6466fc9 100644
--- a/plugins/pychrysalide/arch/processor.c
+++ b/plugins/pychrysalide/arch/processor.c
@@ -32,9 +32,6 @@
 #include <i18n.h>
 
 
-#include <arch/processor.h>
-
-
 #include "instriter.h"
 #include "instruction.h"
 #include "vmpa.h"
@@ -49,6 +46,7 @@
 
 
 
+/* ---------------------------- DEFINITION DE PROCESSEUR ---------------------------- */
 
 
 /* Indique si l'architecture possède un espace virtuel ou non. */
@@ -349,22 +347,19 @@ static int py_arch_processor_set_instrs(PyObject *self, PyObject *value, void *c
 static PyObject *py_arch_processor_find_instr_by_addr(PyObject *self, PyObject *args)
 {
     PyObject *result;                       /* Instance à retourner        */
-    PyObject *addr_obj;                     /* Objet pour une localisation */
-    int ret;                                /* Bilan de lecture des args.  */
     GArchProcessor *proc;                   /* Processeur manipulé         */
-    vmpa2t *addr;                           /* Localisation à retrouver    */
+    proc_cv_info_t conv;                    /* Informations de conversion  */
+    int ret;                                /* Bilan de lecture des args.  */
     GArchInstruction *found;                /* Instruction liée trouvée    */
 
-    ret = PyArg_ParseTuple(args, "O", &addr_obj);
-    if (!ret) return NULL;
+    proc = G_ARCH_PROCESSOR(pygobject_get(self));
 
-    ret = PyObject_IsInstance(addr_obj, (PyObject *)get_python_vmpa_type());
-    if (!ret) return NULL;
+    conv.proc = proc;
 
-    proc = G_ARCH_PROCESSOR(pygobject_get(self));
-    addr = get_internal_vmpa(addr_obj);
+    ret = PyArg_ParseTuple(args, "O&", convert_to_vmpa_using_processor, &conv);
+    if (!ret) return NULL;
 
-    found = g_arch_processor_find_instr_by_address(proc, addr);
+    found = g_arch_processor_find_instr_by_address(proc, conv.vmpa);
 
     if (found != NULL)
     {
@@ -384,7 +379,9 @@ static PyObject *py_arch_processor_find_instr_by_addr(PyObject *self, PyObject *
 
 
 
-
+/* ---------------------------------------------------------------------------------- */
+/*                              DEFINITION DE PROCESSEUR                              */
+/* ---------------------------------------------------------------------------------- */
 
 
 /******************************************************************************
@@ -515,3 +512,65 @@ bool ensure_python_arch_processor_is_registered(void)
     return true;
 
 }
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/*                              TRADUCTION D'EMPLACEMENT                              */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : obj  = objet Python à convertir en emplacement.              *
+*                info = informations utiles à l'opération.                    *
+*                                                                             *
+*  Description : Réalise une conversion d'un objet Python en localisation.    *
+*                                                                             *
+*  Retour      : Bilan de l'opération.                                        *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+int convert_to_vmpa_using_processor(PyObject *obj, proc_cv_info_t *info)
+{
+    int result;                             /* Bilan à retourner           */
+    int ret;                                /* Bilan d'une consultation    */
+
+    ret = PyObject_IsInstance(obj, (PyObject *)get_python_vmpa_type());
+
+    if (ret)
+    {
+        info->vmpa = get_internal_vmpa(obj);
+        result = 1;
+    }
+
+    else
+    {
+        ret = PyObject_IsInstance(obj, (PyObject *)&PyLong_Type);
+
+        if (ret)
+        {
+            info->vmpa = &info->tmp;
+
+            if (g_arch_processor_has_virtual_space(info->proc))
+                init_vmpa(info->vmpa, VMPA_NO_PHYSICAL, PyLong_AsUnsignedLongLong(obj));
+            else
+                init_vmpa(info->vmpa, PyLong_AsUnsignedLongLong(obj), VMPA_NO_VIRTUAL);
+
+            result = 1;
+
+        }
+
+        else
+            result = 0;
+
+    }
+
+    if (result == 0)
+        PyErr_Format(PyExc_TypeError, _("unable to convert object to VMPA location"));
+
+    return result;
+
+}
diff --git a/plugins/pychrysalide/arch/processor.h b/plugins/pychrysalide/arch/processor.h
index 2db8950..22331fc 100644
--- a/plugins/pychrysalide/arch/processor.h
+++ b/plugins/pychrysalide/arch/processor.h
@@ -30,6 +30,12 @@
 #include <stdbool.h>
 
 
+#include <arch/processor.h>
+
+
+
+/* ---------------------------- DEFINITION DE PROCESSEUR ---------------------------- */
+
 
 /* Fournit un accès à une définition de type à diffuser. */
 PyTypeObject *get_python_arch_processor_type(void);
@@ -39,4 +45,23 @@ bool ensure_python_arch_processor_is_registered(void);
 
 
 
+/* ---------------------------- TRADUCTION D'EMPLACEMENT ---------------------------- */
+
+
+/* Informations utiles à une traduction */
+typedef struct _proc_cv_info_t
+{
+    GArchProcessor *proc;                   /* Eventuel processeur indiqué */
+    vmpa2t *vmpa;                           /* Emplacement à définir       */
+
+    vmpa2t tmp;                             /* Eventuel stockage temporaire*/
+
+} proc_cv_info_t;
+
+
+/* Réalise une conversion d'un objet Python en localisation. */
+int convert_to_vmpa_using_processor(PyObject *, proc_cv_info_t *);
+
+
+
 #endif  /* _PLUGINS_PYCHRYSALIDE_ARCH_PROCESSOR_H */
diff --git a/plugins/pychrysalide/format/executable.c b/plugins/pychrysalide/format/executable.c
index 8e24699..0346ecb 100644
--- a/plugins/pychrysalide/format/executable.c
+++ b/plugins/pychrysalide/format/executable.c
@@ -28,16 +28,21 @@
 #include <pygobject.h>
 
 
-#include <format/executable.h>
+#include <i18n.h>
+#include <core/processors.h>
 
 
 #include "format.h"
 #include "../access.h"
 #include "../helpers.h"
+#include "../arch/processor.h"
 #include "../arch/vmpa.h"
 
 
 
+/* ------------------------ DECLARATION DE FORMAT EXECUTABLE ------------------------ */
+
+
 /* Fournit l'emplacement correspondant à une position physique. */
 static PyObject *py_exe_format_translate_offset_into_vmpa(PyObject *, PyObject *);
 
@@ -46,6 +51,11 @@ static PyObject *py_exe_format_translate_address_into_vmpa(PyObject *, PyObject
 
 
 
+/* ---------------------------------------------------------------------------------- */
+/*                          DECLARATION DE FORMAT EXECUTABLE                          */
+/* ---------------------------------------------------------------------------------- */
+
+
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : self = description de l'exécutable à consulter.              *
@@ -224,3 +234,73 @@ bool ensure_python_executable_format_is_registered(void)
     return true;
 
 }
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/*                              TRADUCTION D'EMPLACEMENT                              */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : obj  = objet Python à convertir en emplacement.              *
+*                info = informations utiles à l'opération.                    *
+*                                                                             *
+*  Description : Réalise une conversion d'un objet Python en localisation.    *
+*                                                                             *
+*  Retour      : Bilan de l'opération.                                        *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+int convert_to_vmpa_using_executable(PyObject *obj, exe_cv_info_t *info)
+{
+    int result;                             /* Bilan à retourner           */
+    int ret;                                /* Bilan d'une consultation    */
+    const char *arch;                       /* Architecture d'exécution    */
+    proc_cv_info_t conv;                    /* Informations de conversion  */
+
+    ret = PyObject_IsInstance(obj, (PyObject *)get_python_vmpa_type());
+
+    if (ret)
+    {
+        info->vmpa = get_internal_vmpa(obj);
+        result = 1;
+    }
+
+    else if (info->format != NULL)
+    {
+        arch = g_exe_format_get_target_machine(info->format);
+
+        conv.proc = get_arch_processor_for_type(arch);
+
+        if (conv.proc != NULL)
+        {
+            result = convert_to_vmpa_using_processor(obj, &conv);
+
+            if (result == 1)
+            {
+                info->vmpa = conv.vmpa;
+                copy_vmpa(&info->tmp, &conv.tmp);
+            }
+
+            g_object_unref(G_OBJECT(conv.proc));
+
+        }
+
+        else
+            result = 0;
+
+    }
+
+    else
+        result = 0;
+
+    if (result == 0)
+        PyErr_Format(PyExc_TypeError, _("unable to convert object to VMPA location"));
+
+    return result;
+
+}
diff --git a/plugins/pychrysalide/format/executable.h b/plugins/pychrysalide/format/executable.h
index 8a952cd..d2308e0 100644
--- a/plugins/pychrysalide/format/executable.h
+++ b/plugins/pychrysalide/format/executable.h
@@ -30,6 +30,12 @@
 #include <stdbool.h>
 
 
+#include <format/executable.h>
+
+
+
+/* ------------------------ DECLARATION DE FORMAT EXECUTABLE ------------------------ */
+
 
 /* Fournit un accès à une définition de type à diffuser. */
 PyTypeObject *get_python_executable_format_type(void);
@@ -39,4 +45,23 @@ bool ensure_python_executable_format_is_registered(void);
 
 
 
+/* ---------------------------- TRADUCTION D'EMPLACEMENT ---------------------------- */
+
+
+/* Informations utiles à une traduction */
+typedef struct _exe_cv_info_t
+{
+    GExeFormat *format;                     /* Eventuel format indiqué     */
+    vmpa2t *vmpa;                           /* Emplacement à définir       */
+
+    vmpa2t tmp;                             /* Eventuel stockage temporaire*/
+
+} exe_cv_info_t;
+
+
+/* Réalise une conversion d'un objet Python en localisation. */
+int convert_to_vmpa_using_executable(PyObject *, exe_cv_info_t *);
+
+
+
 #endif  /* _PLUGINS_PYCHRYSALIDE_FORMAT_EXECUTABLE_H */
diff --git a/plugins/pychrysalide/format/format.c b/plugins/pychrysalide/format/format.c
index af881af..154bef3 100644
--- a/plugins/pychrysalide/format/format.c
+++ b/plugins/pychrysalide/format/format.c
@@ -31,6 +31,7 @@
 #include <format/format.h>
 
 
+#include "executable.h"
 #include "symbol.h"
 #include "symiter.h"
 #include "../access.h"
@@ -239,21 +240,20 @@ static PyObject *py_binary_format_find_symbol_by_label(PyObject *self, PyObject
 static PyObject *py_binary_format_find_symbol_at(PyObject *self, PyObject *args)
 {
     PyObject *result;                       /* Valeur à retourner          */
-    PyObject *py_vmpa;                      /* Localisation version Python */
-    int ret;                                /* Bilan de lecture des args.  */
     GBinFormat *format;                     /* Format de binaire manipulé  */
+    exe_cv_info_t conv;                     /* Informations de conversion  */
+    int ret;                                /* Bilan de lecture des args.  */
     GBinSymbol *symbol;                     /* Enventuel symbole trouvé    */
     bool found;                             /* Bilan de la recherche       */
 
-    ret = PyArg_ParseTuple(args, "O", &py_vmpa);
-    if (!ret) return NULL;
+    format = G_BIN_FORMAT(pygobject_get(self));
 
-    ret = PyObject_IsInstance(py_vmpa, (PyObject *)get_python_vmpa_type());
-    if (!ret) return NULL;
+    conv.format = G_IS_EXE_FORMAT(format) ? G_EXE_FORMAT(format) : NULL;
 
-    format = G_BIN_FORMAT(pygobject_get(self));
+    ret = PyArg_ParseTuple(args, "O&", convert_to_vmpa_using_executable, &conv);
+    if (!ret) return NULL;
 
-    found = g_binary_format_find_symbol_at(format, get_internal_vmpa(py_vmpa), &symbol);
+    found = g_binary_format_find_symbol_at(format, conv.vmpa, &symbol);
 
     if (found)
     {
@@ -287,21 +287,20 @@ static PyObject *py_binary_format_find_symbol_at(PyObject *self, PyObject *args)
 static PyObject *py_binary_format_find_next_symbol_at(PyObject *self, PyObject *args)
 {
     PyObject *result;                       /* Valeur à retourner          */
-    PyObject *py_vmpa;                      /* Localisation version Python */
-    int ret;                                /* Bilan de lecture des args.  */
     GBinFormat *format;                     /* Format de binaire manipulé  */
+    exe_cv_info_t conv;                     /* Informations de conversion  */
+    int ret;                                /* Bilan de lecture des args.  */
     GBinSymbol *symbol;                     /* Enventuel symbole trouvé    */
     bool found;                             /* Bilan de la recherche       */
 
-    ret = PyArg_ParseTuple(args, "O", &py_vmpa);
-    if (!ret) return NULL;
+    format = G_BIN_FORMAT(pygobject_get(self));
 
-    ret = PyObject_IsInstance(py_vmpa, (PyObject *)get_python_vmpa_type());
-    if (!ret) return NULL;
+    conv.format = G_IS_EXE_FORMAT(format) ? G_EXE_FORMAT(format) : NULL;
 
-    format = G_BIN_FORMAT(pygobject_get(self));
+    ret = PyArg_ParseTuple(args, "O&", convert_to_vmpa_using_executable, &conv);
+    if (!ret) return NULL;
 
-    found = g_binary_format_find_next_symbol_at(format, get_internal_vmpa(py_vmpa), &symbol);
+    found = g_binary_format_find_next_symbol_at(format, conv.vmpa, &symbol);
 
     if (found)
     {
@@ -335,23 +334,22 @@ static PyObject *py_binary_format_find_next_symbol_at(PyObject *self, PyObject *
 static PyObject *py_binary_format_resolve_symbol(PyObject *self, PyObject *args)
 {
     PyObject *result;                       /* Valeur à retourner          */
-    PyObject *py_vmpa;                      /* Localisation version Python */
+    GBinFormat *format;                     /* Format de binaire manipulé  */
+    exe_cv_info_t conv;                     /* Informations de conversion  */
     int strict;                             /* Tolérance acceptée          */
     int ret;                                /* Bilan de lecture des args.  */
-    GBinFormat *format;                     /* Format de binaire manipulé  */
     GBinSymbol *symbol;                     /* Enventuel symbole trouvé    */
     phys_t diff;                            /* Décalage éventuel mesuré    */
     bool found;                             /* Bilan de la recherche       */
 
-    ret = PyArg_ParseTuple(args, "Op", &py_vmpa, &strict);
-    if (!ret) return NULL;
+    format = G_BIN_FORMAT(pygobject_get(self));
 
-    ret = PyObject_IsInstance(py_vmpa, (PyObject *)get_python_vmpa_type());
-    if (!ret) return NULL;
+    conv.format = G_IS_EXE_FORMAT(format) ? G_EXE_FORMAT(format) : NULL;
 
-    format = G_BIN_FORMAT(pygobject_get(self));
+    ret = PyArg_ParseTuple(args, "O&p", convert_to_vmpa_using_executable, &conv, &strict);
+    if (!ret) return NULL;
 
-    found = g_binary_format_resolve_symbol(format, get_internal_vmpa(py_vmpa), strict, &symbol, &diff);
+    found = g_binary_format_resolve_symbol(format, conv.vmpa, strict, &symbol, &diff);
 
     if (found)
     {
-- 
cgit v0.11.2-87-g4458