diff options
Diffstat (limited to 'plugins/pychrysa/format/elf')
-rw-r--r-- | plugins/pychrysa/format/elf/elf.c | 94 | ||||
-rw-r--r-- | plugins/pychrysa/format/elf/elf.h | 5 | ||||
-rw-r--r-- | plugins/pychrysa/format/elf/module.c | 36 |
3 files changed, 84 insertions, 51 deletions
diff --git a/plugins/pychrysa/format/elf/elf.c b/plugins/pychrysa/format/elf/elf.c index 3ae8f53..87988ee 100644 --- a/plugins/pychrysa/format/elf/elf.c +++ b/plugins/pychrysa/format/elf/elf.c @@ -28,10 +28,11 @@ #include <pygobject.h> -#include <format/elf/elf-int.h> +#include <format/elf/elf.h> -#include "../../quirks.h" +#include "../executable.h" +#include "../../glibext/bincontent.h" @@ -57,48 +58,43 @@ static PyObject *py_elf_format_new(PyTypeObject *, PyObject *, PyObject *); static PyObject *py_elf_format_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *result; /* Instance à retourner */ - const bin_t *content; /* Données binaires */ - int length; /* Quantité de ces données */ + PyObject *content_obj; /* Objet pour le contenu */ int ret; /* Bilan de lecture des args. */ - GBinFormat *format; /* Version GLib du format */ + GBinContent *content; /* Instance GLib correspondante*/ + GBinFormat *format; /* Création GLib à transmettre */ - ret = PyArg_ParseTuple(args, "s#", &content, &length); - if (!ret) Py_RETURN_NONE; + ret = PyArg_ParseTuple(args, "O", &content_obj); + if (!ret) return NULL; - format = NULL;//g_elf_format_new(content, length); - if (format == NULL) Py_RETURN_NONE; + ret = PyObject_IsInstance(content_obj, (PyObject *)get_python_binary_content_type()); + if (!ret) return NULL; + + content = G_BIN_CONTENT(pygobject_get(content_obj)); + format = g_elf_format_new(content); result = pygobject_new(G_OBJECT(format)); - //g_object_unref(format); + + g_object_unref(format); return (PyObject *)result; } - - - - - - /****************************************************************************** * * -* Paramètres : module = module dont la définition est à compléter. * +* Paramètres : - * * * -* Description : Prend en charge l'objet 'pychrysalide.format.elf.ElfFormat'. * +* Description : Fournit un accès à une définition de type à diffuser. * * * -* Retour : Bilan de l'opération. * +* Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ -bool register_python_elf_format(PyObject *module) +PyTypeObject *get_python_elf_format_type(void) { - PyObject *parent_mod; /* Accès au module parent */ - int ret; /* Bilan d'un appel */ - static PyMethodDef py_elf_format_methods[] = { { NULL } }; @@ -109,12 +105,12 @@ bool register_python_elf_format(PyObject *module) static PyTypeObject py_elf_format_type = { - PyObject_HEAD_INIT(NULL) + PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.format.elf.ElfFormat", .tp_basicsize = sizeof(PyGObject), - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + .tp_flags = Py_TPFLAGS_DEFAULT, .tp_doc = "PyChrysalide Elf format", @@ -124,27 +120,45 @@ bool register_python_elf_format(PyObject *module) }; - parent_mod = PyImport_ImportModule("pychrysalide.format"); - if (parent_mod == NULL) return false; + return &py_elf_format_type; + +} - py_elf_format_type.tp_base = (PyTypeObject *)PyObject_GetAttrString(parent_mod, "ExeFormat"); - Py_DECREF(parent_mod); - if (PyType_Ready(&py_elf_format_type) < 0) - return false; +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.format.elf.ElfFormat'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_elf_format(PyObject *module) +{ + PyTypeObject *py_elf_format_type; /* Type Python 'ElfFormat' */ + int ret; /* Bilan d'un appel */ + PyObject *dict; /* Dictionnaire du module */ - Py_INCREF(&py_elf_format_type); - ret = PyModule_AddObject(module, "ElfFormat", (PyObject *)&py_elf_format_type); + py_elf_format_type = get_python_elf_format_type(); - parent_mod = PyImport_ImportModule("pychrysalide.format"); - if (parent_mod == NULL) return false; + py_elf_format_type->tp_base = get_python_executable_format_type(); + py_elf_format_type->tp_basicsize = py_elf_format_type->tp_base->tp_basicsize; + + if (PyType_Ready(py_elf_format_type) != 0) + return false; - pygobject_register_class(module, "GElfFormat", G_TYPE_ELF_FORMAT, &py_elf_format_type, - Py_BuildValue("(OO)", py_elf_format_type.tp_base, - PyObject_GetAttrString(parent_mod, "BinFormat"))); + Py_INCREF(py_elf_format_type); + ret = PyModule_AddObject(module, "ElfFormat", (PyObject *)py_elf_format_type); + if (ret != 0) return false; - Py_DECREF(parent_mod); + dict = PyModule_GetDict(module); + pygobject_register_class(dict, "ElfFormat", G_TYPE_ELF_FORMAT, py_elf_format_type, + Py_BuildValue("(O)", py_elf_format_type->tp_base)); - return (ret == 0); + return true; } diff --git a/plugins/pychrysa/format/elf/elf.h b/plugins/pychrysa/format/elf/elf.h index 658e58b..d037942 100644 --- a/plugins/pychrysa/format/elf/elf.h +++ b/plugins/pychrysa/format/elf/elf.h @@ -31,8 +31,11 @@ +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_elf_format_type(void); + /* Prend en charge l'objet 'pychrysalide.format.elf.ElfFormat'. */ -bool register_python_elf_format(PyObject *module); +bool register_python_elf_format(PyObject *); diff --git a/plugins/pychrysa/format/elf/module.c b/plugins/pychrysa/format/elf/module.c index d701cfd..ebd34d9 100644 --- a/plugins/pychrysa/format/elf/module.c +++ b/plugins/pychrysa/format/elf/module.c @@ -43,26 +43,42 @@ bool add_format_elf_module_to_python_module(PyObject *super) { - bool result; - PyObject *module; + bool result; /* Bilan à retourner */ + PyObject *module; /* Sous-module mis en place */ int ret; /* Bilan d'un appel */ - static PyMethodDef py_format_elf_methods[] = { - { NULL } + static PyModuleDef py_chrysalide_elf_module = { + + .m_base = PyModuleDef_HEAD_INIT, + + .m_name = "pychrysalide.format.elf", + .m_doc = "Python module for Chrysalide.format.elf", + + .m_size = -1, + }; - module = Py_InitModule("pychrysalide.format.elf", py_format_elf_methods); + result = false; + + module = PyModule_Create(&py_chrysalide_elf_module); if (module == NULL) return false; - Py_INCREF(module); - ret = PyModule_AddObject(super, "pychrysalide.format.elf", module); + ret = PyState_AddModule(super, &py_chrysalide_elf_module); + if (ret != 0) goto loading_failed; + + ret = _PyImport_FixupBuiltin(module, "pychrysalide.format.elf"); + if (ret != 0) goto loading_failed; - result = (ret == 0); + Py_INCREF(module); + ret = PyModule_AddObject(super, "elf", module); + if (ret != 0) goto loading_failed; - if (ret != 0) /* ... */; + result = true; result &= register_python_elf_format(module); - return true; + loading_failed: + + return result; } |