diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2020-06-07 20:42:22 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2020-06-07 20:42:22 (GMT) |
commit | ad1caf61fccd0d74be6ac92c41d83e9b0008ff2b (patch) | |
tree | 025d935a91ab84e43eb7a20e6192293ab2e87d55 /plugins/pychrysalide/arch | |
parent | c87020ce7d1cd0a46f8122839c53ffa3e49fd172 (diff) |
Reorganized the operands behaving like proxies.
Diffstat (limited to 'plugins/pychrysalide/arch')
-rw-r--r-- | plugins/pychrysalide/arch/Makefile.am | 1 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/feeder.c | 116 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/module.c | 2 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/operands/Makefile.am | 2 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/operands/feeder.c | 452 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/operands/feeder.h (renamed from plugins/pychrysalide/arch/feeder.h) | 11 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/operands/module.c | 4 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/operands/proxy.c | 383 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/operands/proxy.h | 48 |
9 files changed, 896 insertions, 123 deletions
diff --git a/plugins/pychrysalide/arch/Makefile.am b/plugins/pychrysalide/arch/Makefile.am index 45fd567..cbac988 100644 --- a/plugins/pychrysalide/arch/Makefile.am +++ b/plugins/pychrysalide/arch/Makefile.am @@ -4,7 +4,6 @@ noinst_LTLIBRARIES = libpychrysaarch.la libpychrysaarch_la_SOURCES = \ constants.h constants.c \ context.h context.c \ - feeder.h feeder.c \ instriter.h instriter.c \ instruction.h instruction.c \ module.h module.c \ diff --git a/plugins/pychrysalide/arch/feeder.c b/plugins/pychrysalide/arch/feeder.c deleted file mode 100644 index fbabe80..0000000 --- a/plugins/pychrysalide/arch/feeder.c +++ /dev/null @@ -1,116 +0,0 @@ - -/* Chrysalide - Outil d'analyse de fichiers binaires - * feeder.c - prototypes pour l'équivalent Python du fichier "arch/feeder.c" - * - * Copyright (C) 2018-2019 Cyrille Bagard - * - * This file is part of Chrysalide. - * - * Chrysalide is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * Chrysalide is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - - -#include "feeder.h" - - -#include <assert.h> -#include <pygobject.h> - - -#include <arch/feeder.h> - - -#include "../access.h" -#include "../helpers.h" - - - -/****************************************************************************** -* * -* Paramètres : - * -* * -* Description : Fournit un accès à une définition de type à diffuser. * -* * -* Retour : Définition d'objet pour Python. * -* * -* Remarques : - * -* * -******************************************************************************/ - -PyTypeObject *get_python_proxy_feeder_type(void) -{ - static PyMethodDef py_proxy_feeder_methods[] = { - { NULL } - }; - - static PyGetSetDef py_proxy_feeder_getseters[] = { - { NULL } - }; - - static PyTypeObject py_proxy_feeder_type = { - - PyVarObject_HEAD_INIT(NULL, 0) - - .tp_name = "pychrysalide.arch.ProxyFeeder", - .tp_basicsize = sizeof(PyObject), - - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - - .tp_doc = "PyChrysalide proxy feeder", - - .tp_methods = py_proxy_feeder_methods, - .tp_getset = py_proxy_feeder_getseters - - }; - - return &py_proxy_feeder_type; - -} - - -/****************************************************************************** -* * -* Paramètres : - * -* * -* Description : Prend en charge l'objet 'pychrysalide.arch.ProxyFeeder'. * -* * -* Retour : Bilan de l'opération. * -* * -* Remarques : - * -* * -******************************************************************************/ - -bool ensure_python_proxy_feeder_is_registered(void) -{ - PyTypeObject *type; /* Type Python 'ProxyFeeder' */ - PyObject *module; /* Module à recompléter */ - PyObject *dict; /* Dictionnaire du module */ - - type = get_python_proxy_feeder_type(); - - if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) - { - module = get_access_to_python_module("pychrysalide.arch"); - - dict = PyModule_GetDict(module); - - if (!register_interface_for_pygobject(dict, G_TYPE_PROXY_FEEDER, type)) - return false; - - } - - return true; - -} diff --git a/plugins/pychrysalide/arch/module.c b/plugins/pychrysalide/arch/module.c index 1fa017d..5cd6c17 100644 --- a/plugins/pychrysalide/arch/module.c +++ b/plugins/pychrysalide/arch/module.c @@ -33,7 +33,6 @@ #include "context.h" -#include "feeder.h" #include "instriter.h" #include "instruction.h" #include "operand.h" @@ -168,7 +167,6 @@ bool populate_arch_module(void) result = true; if (result) result = ensure_python_proc_context_is_registered(); - if (result) result = ensure_python_proxy_feeder_is_registered(); if (result) result = ensure_python_instr_iterator_is_registered(); if (result) result = ensure_python_arch_instruction_is_registered(); if (result) result = ensure_python_arch_operand_is_registered(); diff --git a/plugins/pychrysalide/arch/operands/Makefile.am b/plugins/pychrysalide/arch/operands/Makefile.am index 7938320..2074b6b 100644 --- a/plugins/pychrysalide/arch/operands/Makefile.am +++ b/plugins/pychrysalide/arch/operands/Makefile.am @@ -3,8 +3,10 @@ noinst_LTLIBRARIES = libpychrysaarchoperands.la libpychrysaarchoperands_la_SOURCES = \ constants.h constants.c \ + feeder.h feeder.c \ immediate.h immediate.c \ module.h module.c \ + proxy.h proxy.c \ register.h register.c \ rename.h rename.c \ targetable.h targetable.c diff --git a/plugins/pychrysalide/arch/operands/feeder.c b/plugins/pychrysalide/arch/operands/feeder.c new file mode 100644 index 0000000..68fa052 --- /dev/null +++ b/plugins/pychrysalide/arch/operands/feeder.c @@ -0,0 +1,452 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * feeder.c - prototypes pour l'équivalent Python du fichier "arch/operands/feeder.c" + * + * Copyright (C) 2018-2019 Cyrille Bagard + * + * This file is part of Chrysalide. + * + * Chrysalide is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Chrysalide is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "feeder.h" + + +#include <assert.h> +#include <pygobject.h> + + +#include <arch/operands/feeder-int.h> + + +#include "../../access.h" +#include "../../helpers.h" +#include "../../glibext/bufferline.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Procède à l'initialisation de l'interface de génération. */ +static void py_proxy_feeder_interface_init(GProxyFeederIface *, gpointer *); + +/* Compare un fournisseur avec un autre. */ +static int py_proxy_feeder___cmp___wrapper(const GProxyFeeder *, const GProxyFeeder *); + +/* Traduit un fournisseur en version humainement lisible. */ +static void py_proxy_feeder_print_wrapper(const GProxyFeeder *, GBufferLine *); + + + +/* ------------------------- CONNEXION AVEC L'API DE PYTHON ------------------------- */ + + +/* Effectue une comparaison avec un objet Python 'ProxyFeeder'. */ +static PyObject *py_proxy_feeder_richcompare(PyObject *, PyObject *, int); + +/* Traduit un fournisseur en version humainement lisible. */ +static PyObject *py_proxy_feeder_print(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : iface = interface GLib à initialiser. * +* unused = adresse non utilisée ici. * +* * +* Description : Procède à l'initialisation de l'interface de génération. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_proxy_feeder_interface_init(GProxyFeederIface *iface, gpointer *unused) +{ + +#define PROXY_FEEDER_DOC \ + "ProxyFeeder gives an interface for operands which aim to provide" \ + " a dynamic content.\n" \ + "\n" \ + "A typical class declaration for a new implementation looks like:\n" \ + "\n" \ + " class NewImplem(GObject.Object, ProxyFeeder):\n" \ + " ...\n" \ + "\n" \ + "The following methods have to be defined for new implementations:\n" \ + "* pychrysalide.arch.operands.ProxyFeeder._compare();\n" \ + "* pychrysalide.arch.operands.ProxyFeeder._print();\n" \ + + iface->compare = py_proxy_feeder___cmp___wrapper; + iface->print = py_proxy_feeder_print_wrapper; + +} + + +/****************************************************************************** +* * +* Paramètres : a = premier opérande à consulter. * +* b = second opérande à consulter. * +* * +* Description : Compare un fournisseur avec un autre. * +* * +* Retour : Bilan de la comparaison. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_proxy_feeder___cmp___wrapper(const GProxyFeeder *a, const GProxyFeeder *b) +{ + 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 PROXY_FEEDER_CMP_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + __cmp__, "$self, other, /", \ + METH_VARARGS, \ + "Abstract method used to compare the proxy feeder with" \ + " another one. This second object is always an" \ + " pychrysalide.arch.operands.ProxyFeeder 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_DECREF(args); + + Py_XDECREF(pyret); + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : feeder = fournisseur à traiter. * +* line = ligne tampon où imprimer l'élément donné. * +* * +* Description : Traduit un fournisseur en version humainement lisible. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_proxy_feeder_print_wrapper(const GProxyFeeder *feeder, GBufferLine *line) +{ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *args; /* Arguments pour l'appel */ + PyObject *pyret; /* Bilan de consultation */ + +#define PROXY_FEEDER_PRINT_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _print, "$self, line, /", \ + METH_VARARGS, \ + "Abstract method used to generate content into a rendering" \ + " line, which is a provided pychrysalide.glibext.BufferLine" \ + " instance." \ +) + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(feeder)); + + 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_XDECREF(pyret); + + Py_DECREF(args); + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* CONNEXION AVEC L'API DE PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* 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 'ProxyFeeder'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_proxy_feeder_richcompare(PyObject *a, PyObject *b, int op) +{ + PyObject *result; /* Bilan à retourner */ + int ret; /* Bilan de lecture des args. */ + const GProxyFeeder *feeder_a; /* Premier élément à traiter */ + const GProxyFeeder *feeder_b; /* Second élément à traiter */ + int status; /* Résultat d'une comparaison */ + + ret = PyObject_IsInstance(b, (PyObject *)get_python_proxy_feeder_type()); + if (!ret) + { + result = Py_NotImplemented; + goto cmp_done; + } + + feeder_a = G_PROXY_FEEDER(pygobject_get(a)); + feeder_b = G_PROXY_FEEDER(pygobject_get(b)); + + status = py_proxy_feeder___cmp___wrapper(feeder_a, feeder_b); + + result = status_to_rich_cmp_state(status, op); + + cmp_done: + + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant un générateur à manipuler. * +* args = arguments fournis à l'appel. * +* * +* Description : Traduit un fournisseur en version humainement lisible. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_proxy_feeder_print(PyObject *self, PyObject *args) +{ + GBufferLine *line; /* Ligne de rendu à compléter */ + GProxyFeeder *feeder; /* Version native */ + int ret; /* Bilan de lecture des args. */ + +#define PROXY_FEEDER_PRINT_METHOD PYTHON_METHOD_DEF \ +( \ + print, "$self, line, /", \ + METH_VARARGS, py_proxy_feeder, \ + "Produce output into a rendering line.\n" \ + "\n" \ + "The provided line is a pychrysalide.glibext.BufferLine instance." \ +) + + ret = PyArg_ParseTuple(args, "O&", convert_to_buffer_line, &line); + if (!ret) return NULL; + + feeder = G_PROXY_FEEDER(pygobject_get(self)); + + g_proxy_feeder_print(feeder, line); + + Py_RETURN_NONE; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_proxy_feeder_type(void) +{ + static PyMethodDef py_proxy_feeder_methods[] = { + PROXY_FEEDER_CMP_WRAPPER, + PROXY_FEEDER_PRINT_WRAPPER, + PROXY_FEEDER_PRINT_METHOD, + { NULL } + }; + + static PyGetSetDef py_proxy_feeder_getseters[] = { + { NULL } + }; + + static PyTypeObject py_proxy_feeder_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.arch.operands.ProxyFeeder", + .tp_basicsize = sizeof(PyObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = PROXY_FEEDER_DOC, + + .tp_richcompare = py_proxy_feeder_richcompare, + + .tp_methods = py_proxy_feeder_methods, + .tp_getset = py_proxy_feeder_getseters + + }; + + return &py_proxy_feeder_type; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Prend en charge l'objet 'pychrysalide.arch.ProxyFeeder'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_proxy_feeder_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'ProxyFeeder' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + static GInterfaceInfo info = { /* Paramètres d'inscription */ + + .interface_init = (GInterfaceInitFunc)py_proxy_feeder_interface_init, + .interface_finalize = NULL, + .interface_data = NULL, + + }; + + type = get_python_proxy_feeder_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.arch.operands"); + + dict = PyModule_GetDict(module); + + if (!register_interface_for_pygobject_2(dict, G_TYPE_PROXY_FEEDER, type, &info)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en fournisseur intermédiaire. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_proxy_feeder(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_proxy_feeder_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to proxy feeder"); + break; + + case 1: + *((GProxyFeeder **)dst) = G_PROXY_FEEDER(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/arch/feeder.h b/plugins/pychrysalide/arch/operands/feeder.h index 2655bbc..80a5e7d 100644 --- a/plugins/pychrysalide/arch/feeder.h +++ b/plugins/pychrysalide/arch/operands/feeder.h @@ -1,6 +1,6 @@ /* Chrysalide - Outil d'analyse de fichiers binaires - * feeder.h - prototypes pour l'équivalent Python du fichier "arch/feeder.h" + * feeder.h - prototypes pour l'équivalent Python du fichier "arch/operands/feeder.h" * * Copyright (C) 2018 Cyrille Bagard * @@ -22,8 +22,8 @@ */ -#ifndef _PLUGINS_PYCHRYSALIDE_ARCH_FEEDER_H -#define _PLUGINS_PYCHRYSALIDE_ARCH_FEEDER_H +#ifndef _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_FEEDER_H +#define _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_FEEDER_H #include <Python.h> @@ -37,6 +37,9 @@ PyTypeObject *get_python_proxy_feeder_type(void); /* Prend en charge l'objet 'pychrysalide.arch.ProxyFeeder'. */ bool ensure_python_proxy_feeder_is_registered(void); +/* Tente de convertir en fournisseur intermédiaire. */ +int convert_to_proxy_feeder(PyObject *, void *); -#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_FEEDER_H */ + +#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_FEEDER_H */ diff --git a/plugins/pychrysalide/arch/operands/module.c b/plugins/pychrysalide/arch/operands/module.c index cbd0bde..15d7835 100644 --- a/plugins/pychrysalide/arch/operands/module.c +++ b/plugins/pychrysalide/arch/operands/module.c @@ -28,7 +28,9 @@ #include <assert.h> +#include "feeder.h" #include "immediate.h" +#include "proxy.h" #include "register.h" #include "rename.h" #include "targetable.h" @@ -97,8 +99,10 @@ bool populate_arch_operands_module(void) result = true; + if (result) result = ensure_python_proxy_feeder_is_registered(); if (result) result = ensure_python_imm_operand_is_registered(); if (result) result = ensure_python_known_imm_operand_is_registered(); + if (result) result = ensure_python_proxy_operand_is_registered(); if (result) result = ensure_python_register_operand_is_registered(); if (result) result = ensure_python_renamed_operand_is_registered(); if (result) result = ensure_python_renameable_operand_is_registered(); diff --git a/plugins/pychrysalide/arch/operands/proxy.c b/plugins/pychrysalide/arch/operands/proxy.c new file mode 100644 index 0000000..401fe40 --- /dev/null +++ b/plugins/pychrysalide/arch/operands/proxy.c @@ -0,0 +1,383 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * proxy.c - équivalent Python du fichier "arch/operands/proxy.c" + * + * Copyright (C) 2020 Cyrille Bagard + * + * This file is part of Chrysalide. + * + * Chrysalide is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Chrysalide is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "proxy.h" + + +#include <pygobject.h> + + +#include <i18n.h> +#include <arch/operands/proxy-int.h> +#include <plugins/dt.h> + + +#include "feeder.h" +#include "../operand.h" +#include "../../access.h" +#include "../../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Accompagne la création d'une instance dérivée en Python. */ +static PyObject *py_proxy_operand_new(PyTypeObject *, PyObject *, PyObject *); + +/* Initialise la classe des descriptions de fichier binaire. */ +static void py_proxy_operand_init_gclass(GProxyOperandClass *, gpointer); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_proxy_operand_init(PyObject *, PyObject *, PyObject *); + + + +/* ------------------ OPERANDES CONSTITUANT DE PURS INTERMEDIAIRES ------------------ */ + + +/* Fournit le fournisseur représenté par l'opérande. */ +static PyObject *py_proxy_operand_get_feeder(PyObject *, void *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* 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_proxy_operand_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 processeur */ + bool status; /* Bilan d'un enregistrement */ + + /* Validations diverses */ + + base = get_python_proxy_operand_type(); + + if (type == base) + goto simple_way; + + /* Mise en place d'un type dédié */ + + first_time = (g_type_from_name(type->tp_name) == 0); + + gtype = build_dynamic_type(G_TYPE_PROXY_OPERAND, type->tp_name, + (GClassInitFunc)py_proxy_operand_init_gclass, NULL, NULL); + + if (first_time) + { + status = register_class_for_dynamic_pygobject(gtype, type, base); + + if (!status) + { + result = NULL; + goto exit; + } + + } + + /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */ + + simple_way: + + result = PyType_GenericNew(type, args, kwds); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : class = classe à initialiser. * +* unused = données non utilisées ici. * +* * +* Description : Initialise la classe des descriptions de fichier binaire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_proxy_operand_init_gclass(GProxyOperandClass *class, gpointer unused) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_proxy_operand_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + GProxyFeeder *feeder; /* Fournisseur transmis */ + int ret; /* Bilan de lecture des args. */ + PyObject *new_args; /* Nouveaux arguments épurés */ + PyObject *new_kwds; /* Nouveau dictionnaire épuré */ + GProxyOperand *operand; /* Opérande à manipuler */ + +#define PROXY_OPERAND_DOC \ + "The ProxyOperand object behaves like a proxy operand for an object" \ + " which can feed the operand with content.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " ProxyOperand(feeder)" \ + "\n" \ + "Where feeder is an instance implementing the" \ + " pychrysalide.arch.operands.ProxyFeeder interface." + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&", convert_to_proxy_feeder, &feeder); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + new_args = PyTuple_New(0); + new_kwds = PyDict_New(); + + ret = PyGObject_Type.tp_init(self, new_args, new_kwds); + + Py_DECREF(new_kwds); + Py_DECREF(new_args); + + if (ret == -1) return -1; + + /* Eléments de base */ + + operand = G_PROXY_OPERAND(pygobject_get(self)); + + g_object_ref(G_OBJECT(feeder)); + operand->feeder = feeder; + + return 0; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* OPERANDES CONSTITUANT DE PURS INTERMEDIAIRES */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit le fournisseur représenté par l'opérande. * +* * +* Retour : Fournisseur associé à l'opérande. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_proxy_operand_get_feeder(PyObject *self, void *closure) +{ + PyObject *result; /* Instance Python à retourner */ + GProxyOperand *operand; /* Version GLib de l'opérande */ + GProxyFeeder *feeder; /* Fournisseur lié à l'opérande*/ + +#define PROXY_OPERAND_FEEDER_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + feeder, py_proxy_operand, \ + "Give the proxy feeder linked to the operand.\n" \ + "\n" \ + "This feeder is a pychrysalide.arch.operands.ProxyFeeder" \ + " providing content for the operand." \ +) + + operand = G_PROXY_OPERAND(pygobject_get(self)); + + feeder = g_proxy_operand_get_feeder(operand); + + result = pygobject_new(G_OBJECT(feeder)); + g_object_unref(feeder); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_proxy_operand_type(void) +{ + static PyMethodDef py_proxy_operand_methods[] = { + { NULL } + }; + + static PyGetSetDef py_proxy_operand_getseters[] = { + PROXY_OPERAND_FEEDER_ATTRIB, + { NULL } + }; + + static PyTypeObject py_proxy_operand_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.arch.operands.ProxyOperand", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = PROXY_OPERAND_DOC, + + .tp_methods = py_proxy_operand_methods, + .tp_getset = py_proxy_operand_getseters, + + .tp_init = py_proxy_operand_init, + .tp_new = py_proxy_operand_new, + + }; + + return &py_proxy_operand_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.arch.ArchOperand'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_proxy_operand_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'ArchOperand' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_proxy_operand_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.arch.operands"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_PROXY_OPERAND, type, get_python_arch_operand_type())) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en opérande renvoyant vers un élément. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_proxy_operand(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_proxy_operand_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to proxy operand"); + break; + + case 1: + *((GProxyOperand **)dst) = G_PROXY_OPERAND(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/arch/operands/proxy.h b/plugins/pychrysalide/arch/operands/proxy.h new file mode 100644 index 0000000..4fdb6f0 --- /dev/null +++ b/plugins/pychrysalide/arch/operands/proxy.h @@ -0,0 +1,48 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * proxy.h - prototypes pour l'équivalent Python du fichier "arch/operands/proxy.h" + * + * Copyright (C) 2020 Cyrille Bagard + * + * This file is part of Chrysalide. + * + * Chrysalide is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Chrysalide is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_PROXY_H +#define _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_PROXY_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* ------------------ OPERANDES CONSTITUANT DE PURS INTERMEDIAIRES ------------------ */ + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_proxy_operand_type(void); + +/* Prend en charge l'objet 'pychrysalide.arch.operands.ProxyOperand'. */ +bool ensure_python_proxy_operand_is_registered(void); + +/* Tente de convertir en opérande renvoyant vers un élément. */ +int convert_to_proxy_operand(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_PROXY_H */ |