summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/arch/register.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/pychrysalide/arch/register.c')
-rw-r--r--plugins/pychrysalide/arch/register.c361
1 files changed, 48 insertions, 313 deletions
diff --git a/plugins/pychrysalide/arch/register.c b/plugins/pychrysalide/arch/register.c
index 615a5b7..7139e47 100644
--- a/plugins/pychrysalide/arch/register.c
+++ b/plugins/pychrysalide/arch/register.c
@@ -2,7 +2,7 @@
/* Chrysalide - Outil d'analyse de fichiers binaires
* register.c - équivalent Python du fichier "arch/register.c"
*
- * Copyright (C) 2019 Cyrille Bagard
+ * Copyright (C) 2019-2025 Cyrille Bagard
*
* This file is part of Chrysalide.
*
@@ -31,32 +31,27 @@
#include <i18n.h>
#include <arch/register-int.h>
-#include <plugins/dt.h>
#include "../access.h"
#include "../helpers.h"
-#include "../analysis/storage/serialize.h"
+#include "../glibext/comparable.h"
+#include "../glibext/hashable.h"
+#include "../glibext/serialize.h"
+#include "../glibext/strbuilder.h"
/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */
-/* Accompagne la création d'une instance dérivée en Python. */
-static PyObject *py_arch_register_new(PyTypeObject *, PyObject *, PyObject *);
-
/* Initialise la classe des registres. */
-static void py_arch_register_init_gclass(GArchRegisterClass *, gpointer);
-
-/* Produit une empreinte à partir d'un registre. */
-static guint py_arch_register___hash___wrapper(const GArchRegister *);
+static int py_arch_register_init_gclass(GArchRegisterClass *, PyTypeObject *);
-/* Compare un registre avec un autre. */
-static int py_arch_register___cmp___wrapper(const GArchRegister *, const GArchRegister *);
+CREATE_DYN_ABSTRACT_CONSTRUCTOR(arch_register, G_TYPE_ARCH_REGISTER);
-/* Traduit un registre en version humainement lisible. */
-static void py_arch_register_print_wrapper(const GArchRegister *, GBufferLine *);
+/* Initialise une instance sur la base du dérivé de GObject. */
+static int py_arch_register_init(PyObject *, PyObject *, PyObject *);
/* Indique si le registre correspond à ebp ou similaire. */
static bool py_arch_register_is_base_pointer_wrapper(const GArchRegister *);
@@ -69,9 +64,6 @@ static bool py_arch_register_is_stack_pointer_wrapper(const GArchRegister *);
/* ---------------------------- PUR REGISTRE DU MATERIEL ---------------------------- */
-/* Effectue une comparaison avec un objet Python 'ArchRegister'. */
-static PyObject *py_arch_register_richcompare(PyObject *, PyObject *, int);
-
/* Indique si le registre correspond à ebp ou similaire. */
static PyObject *py_arch_register_is_base_pointer(PyObject *, void *);
@@ -87,88 +79,8 @@ static PyObject *py_arch_register_is_stack_pointer(PyObject *, void *);
/******************************************************************************
* *
-* Paramètres : type = type du nouvel objet à mettre en place. *
-* args = éventuelle liste d'arguments. *
-* kwds = éventuel dictionnaire de valeurs mises à disposition. *
-* *
-* Description : Accompagne la création d'une instance dérivée en Python. *
-* *
-* Retour : Nouvel objet Python mis en place ou NULL en cas d'échec. *
-* *
-* Remarques : - *
-* *
-******************************************************************************/
-
-static PyObject *py_arch_register_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
-{
- PyObject *result; /* Objet à retourner */
- PyTypeObject *base; /* Type de base à dériver */
- bool first_time; /* Evite les multiples passages*/
- GType gtype; /* Nouveau type de registre */
- bool status; /* Bilan d'un enregistrement */
-
-#define ARCH_REGISTER_DOC \
- "The ArchRegister object aims to get subclassed to create" \
- " registers suitable for new architectures.\n" \
- "\n" \
- "Calls to the *__init__* constructor of this abstract object expect"\
- " no particular argument.\n" \
- "\n" \
- "The following methods have to be defined for new classes:\n" \
- "* pychrysalide.arch.ArchRegister.__hash__();\n" \
- "* pychrysalide.arch.ArchRegister.__cmp__();\n" \
- "* pychrysalide.arch.ArchRegister._print();\n" \
- "* pychrysalide.arch.ArchRegister._is_base_pointer();\n" \
- "* pychrysalide.arch.ArchRegister._is_stack_pointer().\n" \
- "\n" \
- "Chrysalide creates an internal glue to provide rich comparisons" \
- " for registers based on the old-style *__cmp__* function."
-
- /* Validations diverses */
-
- base = get_python_arch_register_type();
-
- if (type == base)
- {
- result = NULL;
- PyErr_Format(PyExc_RuntimeError, _("%s is an abstract class"), type->tp_name);
- goto exit;
- }
-
- /* Mise en place d'un type dédié */
-
- first_time = (g_type_from_name(type->tp_name) == 0);
-
- gtype = build_dynamic_type(G_TYPE_ARCH_REGISTER, type->tp_name,
- (GClassInitFunc)py_arch_register_init_gclass, NULL, NULL);
-
- if (first_time)
- {
- status = register_class_for_dynamic_pygobject(gtype, type);
-
- if (!status)
- {
- result = NULL;
- goto exit;
- }
-
- }
-
- /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */
-
- result = PyType_GenericNew(type, args, kwds);
-
- exit:
-
- return result;
-
-}
-
-
-/******************************************************************************
-* *
-* Paramètres : class = classe à initialiser. *
-* unused = données non utilisées ici. *
+* Paramètres : gclass = classe GLib à initialiser. *
+* pyclass = classe Python à initialiser. *
* *
* Description : Initialise la classe des registres. *
* *
@@ -178,190 +90,51 @@ static PyObject *py_arch_register_new(PyTypeObject *type, PyObject *args, PyObje
* *
******************************************************************************/
-static void py_arch_register_init_gclass(GArchRegisterClass *class, gpointer unused)
-{
- class->hash = py_arch_register___hash___wrapper;
- class->compare = py_arch_register___cmp___wrapper;
- class->print = py_arch_register_print_wrapper;
- class->is_bp = py_arch_register_is_base_pointer_wrapper;
- class->is_sp = py_arch_register_is_stack_pointer_wrapper;
-
-}
-
-
-/******************************************************************************
-* *
-* Paramètres : reg = registre visé par la procédure. *
-* *
-* Description : Produit une empreinte à partir d'un registre. *
-* *
-* Retour : Bilan de l'opération. *
-* *
-* Remarques : - *
-* *
-******************************************************************************/
-
-static guint py_arch_register___hash___wrapper(const GArchRegister *reg)
-{
- guint result; /* Empreinte à retourner */
- PyGILState_STATE gstate; /* Sauvegarde d'environnement */
- PyObject *pyobj; /* Objet Python concerné */
- PyObject *pyret; /* Bilan de consultation */
-
-#define ARCH_REGISTER_HASH_WRAPPER PYTHON_WRAPPER_DEF \
-( \
- __hash__, "$self, /", \
- METH_NOARGS, \
- "Abstract method used to produce a hash of the object. The" \
- " result must be an integer value." \
-)
-
- result = 0;
-
- gstate = PyGILState_Ensure();
-
- pyobj = pygobject_new(G_OBJECT(reg));
-
- if (has_python_method(pyobj, "__hash__"))
- {
- pyret = run_python_method(pyobj, "__hash__", NULL);
-
- if (pyret != NULL)
- {
- if (PyLong_Check(pyret))
- result = PyLong_AsUnsignedLong(pyret);
-
- Py_DECREF(pyret);
-
- }
-
- }
-
- Py_DECREF(pyobj);
-
- PyGILState_Release(gstate);
-
- return result;
-
-}
-
-
-/******************************************************************************
-* *
-* Paramètres : a = premier registre à consulter. *
-* b = second registre à consulter. *
-* *
-* Description : Compare un registre avec un autre. *
-* *
-* Retour : Bilan de la comparaison. *
-* *
-* Remarques : - *
-* *
-******************************************************************************/
-
-static int py_arch_register___cmp___wrapper(const GArchRegister *a, const GArchRegister *b)
+static int py_arch_register_init_gclass(GArchRegisterClass *gclass, PyTypeObject *pyclass)
{
- int result; /* Empreinte à retourner */
- PyGILState_STATE gstate; /* Sauvegarde d'environnement */
- PyObject *pyobj; /* Objet Python concerné */
- PyObject *args; /* Arguments pour l'appel */
- PyObject *pyret; /* Bilan de consultation */
-
-#define ARCH_REGISTER_CMP_WRAPPER PYTHON_WRAPPER_DEF \
-( \
- __cmp__, "$self, other, /", \
- METH_VARARGS, \
- "Abstract method used to compare the register with another" \
- " one. This second object is always an" \
- " pychrysalide.arch.ArchRegister instance.\n" \
- "\n" \
- " This is the Python old-style comparison method, but" \
- " Chrysalide provides a glue to automatically build a rich" \
- " version of this function." \
-)
-
- result = 0;
-
- gstate = PyGILState_Ensure();
-
- pyobj = pygobject_new(G_OBJECT(a));
-
- if (has_python_method(pyobj, "__cmp__"))
- {
- args = PyTuple_New(1);
- PyTuple_SetItem(args, 0, pygobject_new(G_OBJECT(b)));
-
- pyret = run_python_method(pyobj, "__cmp__", args);
-
- if (pyret != NULL)
- {
- if (PyLong_Check(pyret))
- result = PyLong_AsLong(pyret);
- }
+ PY_CLASS_SET_WRAPPER(gclass->is_bp, py_arch_register_is_base_pointer_wrapper);
+ PY_CLASS_SET_WRAPPER(gclass->is_sp, py_arch_register_is_stack_pointer_wrapper);
- Py_DECREF(args);
-
- Py_XDECREF(pyret);
-
- }
-
- Py_DECREF(pyobj);
-
- PyGILState_Release(gstate);
-
- return result;
+ return 0;
}
/******************************************************************************
* *
-* Paramètres : reg = registre visé par la procédure. *
+* Paramètres : self = objet à initialiser (théoriquement). *
+* args = arguments fournis à l'appel. *
+* kwds = arguments de type key=val fournis. *
* *
-* Description : Traduit un registre en version humainement lisible. *
+* Description : Initialise une instance sur la base du dérivé de GObject. *
* *
-* Retour : - *
+* Retour : 0. *
* *
* Remarques : - *
* *
******************************************************************************/
-static void py_arch_register_print_wrapper(const GArchRegister *reg, GBufferLine *line)
+static int py_arch_register_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyGILState_STATE gstate; /* Sauvegarde d'environnement */
- PyObject *pyobj; /* Objet Python concerné */
- PyObject *args; /* Arguments pour l'appel */
- PyObject *pyret; /* Bilan de consultation */
-
-#define ARCH_REGISTER_PRINT_WRAPPER PYTHON_WRAPPER_DEF \
-( \
- _print, "$self, line, /", \
- METH_VARARGS, \
- "Abstract method used to print the register into a rendering" \
- " line, which is a provided pychrysalide.glibext.BufferLine" \
- " instance." \
-)
-
- gstate = PyGILState_Ensure();
-
- pyobj = pygobject_new(G_OBJECT(reg));
-
- if (has_python_method(pyobj, "_print"))
- {
- args = PyTuple_New(1);
- PyTuple_SetItem(args, 0, pygobject_new(G_OBJECT(line)));
-
- pyret = run_python_method(pyobj, "_print", args);
-
- Py_DECREF(args);
+ int ret; /* Bilan de lecture des args. */
- Py_XDECREF(pyret);
+#define ARCH_REGISTER_DOC \
+ "The ArchRegister object aims to get subclassed in order to create" \
+ " registers suitable for new architectures.\n" \
+ "\n" \
+ "Calls to the *__init__* constructor of this abstract object expect"\
+ " no particular argument.\n" \
+ "\n" \
+ "The following methods may to be implemnted for new classes:\n" \
+ "* pychrysalide.arch.ArchRegister._is_base_pointer();\n" \
+ "* pychrysalide.arch.ArchRegister._is_stack_pointer().\n"
- }
+ /* Initialisation d'un objet GLib */
- Py_DECREF(pyobj);
+ ret = forward_pygobjet_init(self);
+ if (ret == -1) return -1;
- PyGILState_Release(gstate);
+ return 0;
}
@@ -486,51 +259,6 @@ static bool py_arch_register_is_stack_pointer_wrapper(const GArchRegister *reg)
/******************************************************************************
* *
-* Paramètres : a = premier object Python à consulter. *
-* b = second object Python à consulter. *
-* op = type de comparaison menée. *
-* *
-* Description : Effectue une comparaison avec un objet Python 'ArchRegister'.*
-* *
-* Retour : Bilan de l'opération. *
-* *
-* Remarques : - *
-* *
-******************************************************************************/
-
-static PyObject *py_arch_register_richcompare(PyObject *a, PyObject *b, int op)
-{
- PyObject *result; /* Bilan à retourner */
- int ret; /* Bilan de lecture des args. */
- const GArchRegister *reg_a; /* Premier élément à traiter */
- const GArchRegister *reg_b; /* Second élément à traiter */
- int status; /* Résultat d'une comparaison */
-
- ret = PyObject_IsInstance(b, (PyObject *)get_python_arch_register_type());
- if (!ret)
- {
- result = Py_NotImplemented;
- goto cmp_done;
- }
-
- reg_a = G_ARCH_REGISTER(pygobject_get(a));
- reg_b = G_ARCH_REGISTER(pygobject_get(b));
-
- status = py_arch_register___cmp___wrapper(reg_a, reg_b);
-
- result = status_to_rich_cmp_state(status, op);
-
- cmp_done:
-
- Py_INCREF(result);
-
- return result;
-
-}
-
-
-/******************************************************************************
-* *
* Paramètres : self = objet Python concerné par l'appel. *
* closure = non utilisé ici. *
* *
@@ -618,9 +346,6 @@ static PyObject *py_arch_register_is_stack_pointer(PyObject *self, void *closure
PyTypeObject *get_python_arch_register_type(void)
{
static PyMethodDef py_arch_register_methods[] = {
- ARCH_REGISTER_HASH_WRAPPER,
- ARCH_REGISTER_CMP_WRAPPER,
- ARCH_REGISTER_PRINT_WRAPPER,
ARCH_REGISTER_IS_BASE_POINTER_WRAPPER,
ARCH_REGISTER_IS_STACK_POINTER_WRAPPER,
{ NULL }
@@ -643,11 +368,10 @@ PyTypeObject *get_python_arch_register_type(void)
.tp_doc = ARCH_REGISTER_DOC,
- .tp_richcompare = py_arch_register_richcompare,
-
.tp_methods = py_arch_register_methods,
.tp_getset = py_arch_register_getseters,
+ .tp_init = py_arch_register_init,
.tp_new = py_arch_register_new,
};
@@ -683,9 +407,20 @@ bool ensure_python_arch_register_is_registered(void)
dict = PyModule_GetDict(module);
+ if (!ensure_python_comparable_object_is_registered())
+ return false;
+
+ if (!ensure_python_hashable_object_is_registered())
+ return false;
+
if (!ensure_python_serializable_object_is_registered())
return false;
+ if (!ensure_python_string_builder_is_registered())
+ return false;
+
+ pyg_register_class_init(G_TYPE_ARCH_REGISTER, (PyGClassInitFunc)py_arch_register_init_gclass);
+
if (!register_class_for_pygobject(dict, G_TYPE_ARCH_REGISTER, type))
return false;