diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2018-01-13 22:37:31 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2018-01-13 22:37:31 (GMT) |
commit | 23b9c6e68bbe5f0531f9a9408c2deb9f897701dc (patch) | |
tree | 3804d6c21c9cd5e291cb8c7853607cdda992d125 /plugins/pychrysa/format/format.c | |
parent | a6975c1d754a1ba5bfb9e23f0b26692c746e6f9c (diff) |
Created a real iterator for symbols.
Diffstat (limited to 'plugins/pychrysa/format/format.c')
-rw-r--r-- | plugins/pychrysa/format/format.c | 228 |
1 files changed, 7 insertions, 221 deletions
diff --git a/plugins/pychrysa/format/format.c b/plugins/pychrysa/format/format.c index aed9664..5920b27 100644 --- a/plugins/pychrysa/format/format.c +++ b/plugins/pychrysa/format/format.c @@ -31,36 +31,12 @@ #include <format/format.h> +#include "symiter.h" #include "../helpers.h" #include "../arch/vmpa.h" -/* ------------------------ PARCOURS DE SYMBOLES DE BINAIRES ------------------------ */ - - -/* Parcours des symboles présents dans un binaire */ -typedef struct _pyBinSymbolIterator -{ - PyObject_HEAD /* A laisser en premier */ - - GBinFormat *format; /* Format binaire à consulter */ - size_t next; /* Symbole binaire à présenter */ - -} pyBinSymbolIterator; - - -/* Prend acte d'un compteur de référence à 0. */ -static void py_binary_symbol_iterator_dealloc(PyObject *); - -/* Fournit un itérateur pour symboles de format binaire. */ -static PyObject *py_binary_symbol_iterator_next(PyObject *); - -/* Initialise un objet Python de type 'BinSymbolIterator'. */ -static int py_binary_symbol_iterator_init(PyObject *, PyObject *, PyObject *); - - - /* ---------------------------- FORMAT BINAIRE GENERIQUE ---------------------------- */ @@ -104,195 +80,6 @@ static bool define_python_binary_format_constants(PyTypeObject *); /* ---------------------------------------------------------------------------------- */ -/* PARCOURS DE SYMBOLES DE BINAIRES */ -/* ---------------------------------------------------------------------------------- */ - - -/****************************************************************************** -* * -* Paramètres : self = instance Python à libérer de la mémoire. * -* * -* Description : Prend acte d'un compteur de référence à 0. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static void py_binary_symbol_iterator_dealloc(PyObject *self) -{ - pyBinSymbolIterator *iterator; /* Références pour le parcours */ - - /** - * Il aurait été sans doute mieux de reposer ici sur .tp_finalize, - * mais cela semble impliquer de mettre en place tous les mécanismes de GC... - * - * cf. https://docs.python.org/3/extending/newtypes.html#finalization-and-de-allocation - */ - - iterator = (pyBinSymbolIterator *)self; - - g_object_unref(G_OBJECT(iterator->format)); - - Py_TYPE(self)->tp_free((PyObject *)self); - -} - - -/****************************************************************************** -* * -* Paramètres : self = itérateur à manipuler. * -* * -* Description : Fournit un itérateur pour symboles de format binaire. * -* * -* Retour : Instance Python prête à emploi. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static PyObject *py_binary_symbol_iterator_next(PyObject *self) -{ - PyObject *result; /* Instance à retourner */ - pyBinSymbolIterator *iterator; /* Références pour le parcours */ - size_t count; /* Nombre de symboles présents */ - GBinSymbol **symbols; /* Liste de ces mêmes symboles */ - - iterator = (pyBinSymbolIterator *)self; - - symbols = g_binary_format_get_symbols(iterator->format, &count); - - if (iterator->next < count) - { - result = pygobject_new(G_OBJECT(symbols[iterator->next])); - iterator->next++; - } - else - { - PyErr_SetNone(PyExc_StopIteration); - result = NULL; - } - - return result; - -} - - -/****************************************************************************** -* * -* Paramètres : self = objet instancié à initialiser. * -* args = arguments fournis à l'appel. * -* kwds = arguments de type key=val fournis. * -* * -* Description : Initialise un objet Python de type 'BinSymbolIterator'. * -* * -* Retour : Instance Python mise en place. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static int py_binary_symbol_iterator_init(PyObject *self, PyObject *args, PyObject *kwds) -{ - pyBinSymbolIterator *iterator; /* Références pour le parcours */ - PyObject *format; /* Format binaire en Python */ - int ret; /* Bilan de lecture des args. */ - - ret = PyArg_ParseTuple(args, "O", &format); - if (!ret) return -1; - - ret = PyObject_IsInstance(format, (PyObject *)get_python_binary_format_type()); - if (!ret) return -1; - - iterator = (pyBinSymbolIterator *)self; - - iterator->format = G_BIN_FORMAT(pygobject_get(format)); - g_object_ref(G_OBJECT(iterator->format)); - - iterator->next = 0; - - return 0; - -} - - -/****************************************************************************** -* * -* Paramètres : - * -* * -* Description : Fournit un accès à une définition de type à diffuser. * -* * -* Retour : Définition d'objet pour Python. * -* * -* Remarques : - * -* * -******************************************************************************/ - -PyTypeObject *get_python_binary_symbol_iterator_type(void) -{ - static PyTypeObject py_binary_symbol_iterator_type = { - - PyVarObject_HEAD_INIT(NULL, 0) - - .tp_name = "pychrysalide.format.BinSymbolIterator", - .tp_basicsize = sizeof(pyBinSymbolIterator), - - .tp_dealloc = py_binary_symbol_iterator_dealloc, - - .tp_flags = Py_TPFLAGS_DEFAULT, - - .tp_doc = "Iterator for binary symbols", - - .tp_iter = PyObject_SelfIter, - .tp_iternext = py_binary_symbol_iterator_next, - - .tp_init = py_binary_symbol_iterator_init, - - .tp_new = PyType_GenericNew, - - }; - - return &py_binary_symbol_iterator_type; - -} - - -/****************************************************************************** -* * -* Paramètres : module = module dont la définition est à compléter. * -* * -* Description : Prend en charge l'objet 'pychrysalide...BinSymbolIterator'. * -* * -* Retour : Bilan de l'opération. * -* * -* Remarques : - * -* * -******************************************************************************/ - -bool register_python_binary_symbol_iterator(PyObject *module) -{ - PyTypeObject *py_binary_symbol_iterator_type; /* Type Python 'BinSymbolIterator' */ - int ret; /* Bilan d'un appel */ - - py_binary_symbol_iterator_type = get_python_binary_symbol_iterator_type(); - - py_binary_symbol_iterator_type->tp_base = &PyBaseObject_Type; - - if (PyType_Ready(py_binary_symbol_iterator_type) != 0) - return false; - - Py_INCREF(py_binary_symbol_iterator_type); - ret = PyModule_AddObject(module, "BinSymbolIterator", (PyObject *)py_binary_symbol_iterator_type); - if (ret != 0) return false; - - return true; - -} - - - -/* ---------------------------------------------------------------------------------- */ /* FORMAT BINAIRE GENERIQUE */ /* ---------------------------------------------------------------------------------- */ @@ -539,18 +326,17 @@ static PyObject *py_binary_format_get_content(PyObject *self, void *closure) static PyObject *py_binary_format_get_symbols(PyObject *self, void *closure) { - PyObject *result; /* Instance à retourner */ + PyObject *result; /* Instance Python à retourner */ PyTypeObject *iterator_type; /* Type Python de l'itérateur */ - PyObject *args_list; /* Arguments de mise en place */ + PyObject *args; /* Liste des arguments d'appel */ - iterator_type = get_python_binary_symbol_iterator_type(); + iterator_type = get_python_sym_iterator_type(); - Py_INCREF(self); + args = Py_BuildValue("On", self, 0); - args_list = Py_BuildValue("(O)", self); - result = PyObject_CallObject((PyObject *)iterator_type, args_list); + result = PyObject_CallObject((PyObject *)iterator_type, args); - Py_DECREF(args_list); + Py_DECREF(args); return result; |