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/symiter.c | |
parent | a6975c1d754a1ba5bfb9e23f0b26692c746e6f9c (diff) |
Created a real iterator for symbols.
Diffstat (limited to 'plugins/pychrysa/format/symiter.c')
-rw-r--r-- | plugins/pychrysa/format/symiter.c | 276 |
1 files changed, 276 insertions, 0 deletions
diff --git a/plugins/pychrysa/format/symiter.c b/plugins/pychrysa/format/symiter.c new file mode 100644 index 0000000..db0b744 --- /dev/null +++ b/plugins/pychrysa/format/symiter.c @@ -0,0 +1,276 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * symiter.c - équivalent Python du fichier "format/symiter.c" + * + * Copyright (C) 2017 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 "symiter.h" + + +#include <pygobject.h> + + +#include <format/symiter.h> + + +#include "format.h" + + + +/* Transcription d'un itérateur en Python */ +typedef struct _PySymIterator +{ + PyObject_HEAD; /* A laisser en premier */ + + sym_iter_t *native; /* Version native de l'objet */ + bool first_time; /* Premier élément retourné ? */ + +} PySymIterator; + + +/* Libère de la mémoire un itérateur sur des symboles. */ +static void py_sym_iterator_dealloc(PySymIterator *); + +/* Fournit le symbole qui en suit un autr. */ +static PyObject *py_sym_iterator_next(PySymIterator *); + +/* Initialise un nouvel itérateur. */ +static int py_sym_iterator_init(PySymIterator *, PyObject *, PyObject *); + +/* Construit un nouvel itérateur. */ +static PyObject *py_sym_iterator_new(PyTypeObject *, PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* Paramètres : self = itérateur à supprimer. * +* * +* Description : Libère de la mémoire un itérateur sur des symboles. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_sym_iterator_dealloc(PySymIterator *self) +{ + delete_symbol_iterator(self->native); + + Py_TYPE(self)->tp_free((PyObject *)self); + +} + + +/****************************************************************************** +* * +* Paramètres : self = itérateur à manipuler. * +* * +* Description : Fournit le symbole qui en suit un autre. * +* * +* Retour : Symbole suivant trouvé, ou NULL. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_sym_iterator_next(PySymIterator *self) +{ + PyObject *result; /* Résultat à retourner */ + GBinSymbol *next; /* Symbole suivant */ + + if (self->first_time) + { + next = get_symbol_iterator_current(self->native); + self->first_time = false; + } + + else + next = get_symbol_iterator_next(self->native); + + if (next != NULL) + { + result = pygobject_new(G_OBJECT(next)); + g_object_unref(G_OBJECT(next)); + } + + else + { + PyErr_SetNone(PyExc_StopIteration); + result = NULL; + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = instance d'objet à initialiser. * +* args = arguments passés pour l'appel. * +* kwds = mots clefs éventuellement fournis en complément. * +* * +* Description : Initialise un nouvel itérateur. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_sym_iterator_init(PySymIterator *self, PyObject *args, PyObject *kwds) +{ + int result; /* Bilan à retourner */ + PyObject *fmt_obj; /* Format version Python */ + unsigned long index; /* Indice de premier symbole */ + int ret; /* Bilan de lecture des args. */ + GBinFormat *format; /* Version native du format */ + + result = -1; + + ret = PyArg_ParseTuple(args, "Ok", &fmt_obj, &index); + if (ret == 0) goto psii_exit; + + ret = PyObject_IsInstance(fmt_obj, (PyObject *)get_python_binary_format_type()); + if (!ret) goto psii_exit; + + format = G_BIN_FORMAT(pygobject_get(fmt_obj)); + + self->native = create_symbol_iterator(format, index); + self->first_time = true; + + result = 0; + + psii_exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : type = type d'objet à mettre en place. * +* args = arguments passés pour l'appel. * +* kwds = mots clefs éventuellement fournis en complément. * +* * +* Description : Construit un nouvel itérateur. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_sym_iterator_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PySymIterator *result; /* Nouvelle instance à renvoyer*/ + int ret; /* Bilan de l'initialisation */ + + result = (PySymIterator *)type->tp_alloc(type, 0); + + if (result != NULL) + { + ret = py_sym_iterator_init(result, args, kwds); + + if (ret != 0) + { + Py_DECREF(result); + result = NULL; + } + + } + + return (PyObject *)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_sym_iterator_type(void) +{ + static PyTypeObject py_sym_iterator_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.format.SymIterator", + .tp_basicsize = sizeof(PySymIterator), + + .tp_dealloc = (destructor)py_sym_iterator_dealloc, + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = "Iterator for Chrysalide symbols registered in a given format.", + + .tp_iter = PyObject_SelfIter, + .tp_iternext = (iternextfunc)py_sym_iterator_next, + + .tp_init = (initproc)py_sym_iterator_init, + .tp_new = py_sym_iterator_new, + + }; + + return &py_sym_iterator_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.format.SymIterator'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_sym_iterator(PyObject *module) +{ + PyTypeObject *py_sym_iterator_type; /* Type Python 'BinContent' */ + int ret; /* Bilan d'un appel */ + + py_sym_iterator_type = get_python_sym_iterator_type(); + + if (PyType_Ready(py_sym_iterator_type) < 0) + return false; + + Py_INCREF(py_sym_iterator_type); + ret = PyModule_AddObject(module, "SymIterator", (PyObject *)py_sym_iterator_type); + + return (ret == 0); + +} |