diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2009-09-19 13:01:04 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2009-09-19 13:01:04 (GMT) |
commit | 859bdb6b51d76058eb1a8bfa619a15978f50b251 (patch) | |
tree | 306ba7f92d84f10c0a7fc8e385cb32b85bb3cb73 /plugins/pyoida/analysis | |
parent | ff4fd84840beca40a88db2ca0ce90e6511fb852b (diff) |
Improved the pyoida module.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@116 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'plugins/pyoida/analysis')
-rw-r--r-- | plugins/pyoida/analysis/Makefile.am | 17 | ||||
-rw-r--r-- | plugins/pyoida/analysis/py_binary.c | 360 | ||||
-rw-r--r-- | plugins/pyoida/analysis/py_binary.h | 45 | ||||
-rw-r--r-- | plugins/pyoida/analysis/py_line-int.h | 53 | ||||
-rw-r--r-- | plugins/pyoida/analysis/py_line.c | 603 | ||||
-rw-r--r-- | plugins/pyoida/analysis/py_line.h | 45 | ||||
-rw-r--r-- | plugins/pyoida/analysis/py_line_code.c | 281 | ||||
-rw-r--r-- | plugins/pyoida/analysis/py_line_code.h | 45 |
8 files changed, 1449 insertions, 0 deletions
diff --git a/plugins/pyoida/analysis/Makefile.am b/plugins/pyoida/analysis/Makefile.am new file mode 100644 index 0000000..df49499 --- /dev/null +++ b/plugins/pyoida/analysis/Makefile.am @@ -0,0 +1,17 @@ + +noinst_LTLIBRARIES = libpyoidaanalysis.la + +libpyoidaanalysis_la_SOURCES = \ + py_binary.h py_binary.c \ + py_line.h py_line.c \ + py_line_code.h py_line_code.c + + +libpyoidaanalysis_la_LDFLAGS = + + +INCLUDES = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) + +AM_CPPFLAGS = + +AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) diff --git a/plugins/pyoida/analysis/py_binary.c b/plugins/pyoida/analysis/py_binary.c new file mode 100644 index 0000000..c8d7446 --- /dev/null +++ b/plugins/pyoida/analysis/py_binary.c @@ -0,0 +1,360 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * py_binary.c - intermédiaire des binaires pour Python + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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 "py_binary.h" + +#include <structmember.h> + + + +#include "py_line.h" + + + +typedef struct { + PyObject_HEAD + + GOpenidaBinary *binary; /* Instance réelle rattachée */ + +} pybinary; + + +#define _(str) str + + + + + +/* Fournit la description du type 'binary' pour Python. */ +PyTypeObject *pybinary_get_type(void); + + + + +/* Fournit les lignes de rendu associé pour Python. */ +static PyObject *pybinary_get_lines(pybinary *); + + + + + + + +static void +pybinary_dealloc(pybinary* self) +{ +#if 0 + Py_XDECREF(self->first); + Py_XDECREF(self->last); +#endif + + //printf("dealloc\n"); + + self->ob_type->tp_free((PyObject*)self); + //Py_TYPE(self)->tp_free((PyObject*)self); + + //printf("dealloc::end\n"); + +} + +static PyObject * +pybinary_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + pybinary *self; + + + printf("creating a new binary\n"); + + self = (pybinary *)type->tp_alloc(type, 0); + if (self != NULL) { +#if 0 + self->first = PyString_FromString("");//PyUnicode_FromString(""); + if (self->first == NULL) + { + Py_DECREF(self); + return NULL; + } + + self->last = PyString_FromString("");//PyUnicode_FromString(""); + if (self->last == NULL) + { + Py_DECREF(self); + return NULL; + } + + self->number = 0; +#endif + } + + return (PyObject *)self; +} + + +/****************************************************************************** +* * +* Paramètres : binary = modèle à représenter en Python. * +* * +* Description : Initialise le greffon permettant l'usage de Python. * +* * +* Retour : Nouvelle instance d'objet Python ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *pybinary_new_from_existing(GOpenidaBinary *binary) +{ + pybinary *result; /* Nouvelle instance à renvoyer*/ + PyTypeObject *type; /* Type d'objet à créer */ + + printf("EXISTING :: %p\n", binary); + + type = pybinary_get_type(); + + result = (pybinary *)type->tp_alloc(type, 0); + + if (result != NULL) + result->binary = binary; + + return (PyObject *)result; + +} + + + + +static int +pybinary_init(pybinary *self, PyObject *args, PyObject *kwds) +{ +#if 0 + PyObject *first=NULL, *last=NULL, *tmp; + + static char *kwlist[] = {"first", "last", "number", NULL}; + + printf("pybinary_init\n"); + + if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSi", kwlist, + &first, &last, + &self->number)) + return -1; + + if (first) { + tmp = self->first; + Py_INCREF(first); + self->first = first; + Py_DECREF(tmp); + } + + if (last) { + tmp = self->last; + Py_INCREF(last); + self->last = last; + Py_DECREF(tmp); + } +#endif + return 0; +} + + + + + + + +static PyMemberDef pybinary_members[] = { +#if 0 + {"number", T_INT, offsetof(pybinary, number), 0, + "noddy number"}, +#endif + {NULL} /* Sentinel */ +}; + + + + + + + +/****************************************************************************** +* * +* Paramètres : self = instance manipulée à traiter. * +* * +* Description : Fournit les lignes de rendu associé pour Python. * +* * +* Retour : Nouvelle instance d'objet Python ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *pybinary_get_lines(pybinary *self) +{ + PyObject *result; /* Liste à retourner */ + GRenderingLine *lines; /* Liste récupérée à convertir */ + + lines = g_openida_binary_get_lines(self->binary); + + result = pyline_new_from_existing(lines, lines); + + return result; + +} + + + + +#if 0 +static PyObject * +pybinary_name(pybinary* self) +{ + static PyObject *format = NULL; + PyObject *args, *result; + + if (format == NULL) { + format = PyString_FromString("%s %s");//PyUnicode_FromString("%s %s"); + if (format == NULL) + return NULL; + } + + args = Py_BuildValue("OO", self->first, self->last); + if (args == NULL) + return NULL; + + result = PyUnicode_Format(format, args); + Py_DECREF(args); + + return result; +} +#endif + +static PyMethodDef pybinary_methods[] = { +#if 0 + {"name", (PyCFunction)pybinary_name, METH_NOARGS, + "Return the name, combining the first and last name" + }, +#endif + { "lines", (PyCFunction)pybinary_get_lines, METH_NOARGS, + "Provide a list of associated rendering lines." + }, + {NULL} /* Sentinel */ +}; + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit la description du type 'binary' pour Python. * +* * +* Retour : Adresse de la description du type 'binary'. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *pybinary_get_type(void) +{ + static PyTypeObject result = { + + PyObject_HEAD_INIT(NULL) + +#if PY_VERSION_HEX < 0x03000000 + 0, /*ob_size*/ +#endif + + "noddy.pybinary", /* tp_name */ + sizeof(pybinary), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)pybinary_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved / tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pybinary objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + pybinary_methods, /* tp_methods */ + pybinary_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)pybinary_init, /* tp_init */ + 0, /* tp_alloc */ + pybinary_new, /* tp_new */ + }; + + return &result; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'binary' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_binary_to_python_module(PyObject *module) +{ + int ret; /* Bilan d'un appel */ + + if (PyType_Ready(pybinary_get_type()) < 0) + return false; + + printf("Adding binary type\n"); + + + + Py_INCREF(pybinary_get_type()); + PyModule_AddObject(module, "binary", (PyObject *)pybinary_get_type()); + + + return true; /* FIXME */ + + +} diff --git a/plugins/pyoida/analysis/py_binary.h b/plugins/pyoida/analysis/py_binary.h new file mode 100644 index 0000000..f87d2a5 --- /dev/null +++ b/plugins/pyoida/analysis/py_binary.h @@ -0,0 +1,45 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * py_binary.h - prototypes pour l'intermédiaire des binaires pour Python + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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_PYOIDA_PY_BINARY_H +#define _PLUGINS_PYOIDA_PY_BINARY_H + + +#include <Python.h> +#include <stdbool.h> + + +#include "../../../src/analysis/binary.h" + + + +/* Initialise le greffon permettant l'usage de Python. */ +PyObject *pybinary_new_from_existing(GOpenidaBinary *); + +/* Ajoute l'objet 'binary' au module Python. */ +bool add_binary_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_PY_BINARY_H */ diff --git a/plugins/pyoida/analysis/py_line-int.h b/plugins/pyoida/analysis/py_line-int.h new file mode 100644 index 0000000..408eae8 --- /dev/null +++ b/plugins/pyoida/analysis/py_line-int.h @@ -0,0 +1,53 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * py_line.h - prototypes internes pour les lignes de représentation en Python + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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_PYOIDA_PY_LINE_INT_H +#define _PLUGINS_PYOIDA_PY_LINE_INT_H + + +#include "py_line.h" + + + +typedef struct { + PyObject_HEAD + + GRenderingLine *head; + + GRenderingLine *line; /* Instance réelle rattachée */ + + vmpa_t address; /* Adresse mise en cache */ + +} PyLine; + + +/* Fournit la description du type 'PyLine' pour Python. */ +PyTypeObject *pyline_get_type(void); + +/* Initialise le greffon permettant l'usage de Python. */ +void pyline_init_from_existing(PyLine *, GRenderingLine *, GRenderingLine *); + + + +#endif /* _PLUGINS_PYOIDA_PY_LINE_INT_H */ diff --git a/plugins/pyoida/analysis/py_line.c b/plugins/pyoida/analysis/py_line.c new file mode 100644 index 0000000..bce8c1e --- /dev/null +++ b/plugins/pyoida/analysis/py_line.c @@ -0,0 +1,603 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * py_line.c - intermédiaire des lignes de représentation pour Python + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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 "py_line.h" + +#include <structmember.h> + + + +#include "../../../src/analysis/line_code.h" + + +#include "py_line-int.h" +#include "py_line_code.h" + + + + + +#define _(str) str + + + + + + + +/* Prépare un parcours de lignes. */ +static PyObject *pyline_get_iter(PyLine *); + + + + + +/* ---------------------- ITERATEUR POUR LE PARCOURS DE LIGNES ---------------------- */ + + +/* Itérateur pour lignes de rendu */ +typedef struct _PyLineIter +{ + PyObject_HEAD /* A laisser en premier */ + + GRenderingLine *head; /* Liste à parcourir */ + GRenderingLine *cur; /* Point de parcours courant */ + +} PyLineIter; + + +/* Fournit la description de l'itérateur 'PyLine' pour Python. */ +static PyTypeObject *pylineiter_get_type(void); + +/* Prépare l'itérateur pour un parcours de lignes de rendu. */ +static PyObject *pylineiter_new(GRenderingLine *); + +/* Libère la mémoire occupée par un itérateur de 'PyLine'. */ +static void pylineiter_dealloc(PyLineIter *); + +/* Fournit l'élément suivant dans un parcours de lignes. */ +static PyObject *pylineiter_get_next(PyLineIter *); + + + + +/* ---------------------- EQUIVALENT PYTHON DES LIGNES DE CODE ---------------------- */ + + +/* Représentation Python d'une ligne de code */ +typedef struct _PyCodeLine +{ + PyLine base; /* A laisser en premier */ + +} PyCodeLine; + + + + + + + + + + + +static void +pyline_dealloc(PyLine *self) +{ +#if 0 + Py_XDECREF(self->first); + Py_XDECREF(self->last); +#endif + + //printf("dealloc\n"); + + + g_object_set_data(G_OBJECT(self->line), "pyline", NULL); + + + self->ob_type->tp_free((PyObject*)self); + //Py_TYPE(self)->tp_free((PyObject*)self); + + //printf("dealloc::end\n"); + +} + +static PyObject * +pyline_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyLine *self; + + + printf("creating a new line\n"); + + self = (PyLine *)type->tp_alloc(type, 0); + if (self != NULL) { +#if 0 + self->first = PyString_FromString("");//PyUnicode_FromString(""); + if (self->first == NULL) + { + Py_DECREF(self); + return NULL; + } + + self->last = PyString_FromString("");//PyUnicode_FromString(""); + if (self->last == NULL) + { + Py_DECREF(self); + return NULL; + } + + self->number = 0; +#endif + } + + return (PyObject *)self; +} + + +/****************************************************************************** +* * +* Paramètres : head = ???? * +* line = modèle à représenter en Python. * +* * +* Description : Initialise le greffon permettant l'usage de Python. * +* * +* Retour : Nouvelle instance d'objet Python ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *pyline_new_from_existing(GRenderingLine *head, GRenderingLine *line) +{ + PyLine *result; /* Nouvelle instance à renvoyer*/ + PyTypeObject *type; /* Type d'objet à créer */ + + result = (PyLine *)g_object_get_data(G_OBJECT(line), "pyline"); + + if (result == NULL) + { + if (G_IS_CODE_LINE(line)) + result = pycodeline_new_from_existing(head, line); + + else + { + type = pyline_get_type(); + + result = (PyLine *)type->tp_alloc(type, 0); + + if (result != NULL) + { + result->head = head; + result->line = line; + + g_object_set_data(G_OBJECT(line), "pyline", result); + + } + + } + + } + + return (PyObject *)result; + +} + + + + +static int +pyline_init(PyLine *self, PyObject *args, PyObject *kwds) +{ +#if 0 + PyObject *first=NULL, *last=NULL, *tmp; + + static char *kwlist[] = {"first", "last", "number", NULL}; + + printf("pyline_init\n"); + + if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSi", kwlist, + &first, &last, + &self->number)) + return -1; + + if (first) { + tmp = self->first; + Py_INCREF(first); + self->first = first; + Py_DECREF(tmp); + } + + if (last) { + tmp = self->last; + Py_INCREF(last); + self->last = last; + Py_DECREF(tmp); + } +#endif + return 0; +} + + + +/****************************************************************************** +* * +* Paramètres : self = instance à initialiser. * +* head = ???? * +* line = modèle à représenter en Python. * +* * +* Description : Initialise le greffon permettant l'usage de Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void pyline_init_from_existing(PyLine *self, GRenderingLine *head, GRenderingLine *line) +{ + self->head = head; + self->line = line; + + self->address = get_rendering_line_address(self->line); + +} + + + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : self = instance manipulée à traiter. * +* * +* Description : Prépare un parcours de lignes. * +* * +* Retour : Point de départ d'un parcours. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *pyline_get_iter(PyLine *self) +{ + return (PyObject *)pylineiter_new(self->head); + +} + + + + + + +#if 0 +static PyObject * +pyline_name(pyline* self) +{ + static PyObject *format = NULL; + PyObject *args, *result; + + if (format == NULL) { + format = PyString_FromString("%s %s");//PyUnicode_FromString("%s %s"); + if (format == NULL) + return NULL; + } + + args = Py_BuildValue("OO", self->first, self->last); + if (args == NULL) + return NULL; + + result = PyUnicode_Format(format, args); + Py_DECREF(args); + + return result; +} +#endif + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit la description du type 'PyLine' pour Python. * +* * +* Retour : Adresse de la description du type 'line'. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *pyline_get_type(void) +{ + + static PyMethodDef pyline_methods[] = { +#if 0 + {"name", (PyCFunction)pyline_name, METH_NOARGS, + "Return the name, combining the first and last name" + }, +#endif + { NULL } +}; + + + static PyMemberDef pyline_members[] = { + { "address", T_ULONGLONG, offsetof(PyLine, address), READONLY, + "physical or virtual address relative to the line." }, + { NULL } +}; + + + static PyTypeObject result = { + + PyObject_HEAD_INIT(NULL) + +#if PY_VERSION_HEX < 0x03000000 + 0, /*ob_size*/ +#endif + + "noddy.pyline", /* tp_name */ + sizeof(PyLine), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)pyline_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved / tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "PyLine objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + (getiterfunc)pyline_get_iter, /* tp_iter */ + 0, /* tp_iternext */ + pyline_methods, /* tp_methods */ + pyline_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)pyline_init, /* tp_init */ + 0, /* tp_alloc */ + pyline_new, /* tp_new */ + }; + + return &result; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'line' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_line_to_python_module(PyObject *module) +{ + int ret; /* Bilan d'un appel */ + + if (PyType_Ready(pyline_get_type()) < 0) + return false; + + if (PyType_Ready(pylineiter_get_type()) < 0) + return false; + + + + + printf("Adding line type\n"); + + + + Py_INCREF(pyline_get_type()); + PyModule_AddObject(module, "line", (PyObject *)pyline_get_type()); + + Py_INCREF(pylineiter_get_type()); + PyModule_AddObject(module, "lineiter", (PyObject *)pylineiter_get_type()); + + + return true; /* FIXME */ + + +} + + + + + + +/* ---------------------------------------------------------------------------------- */ +/* ITERATEUR POUR LE PARCOURS DE LIGNES */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit la description de l'itérateur 'PyLine' pour Python. * +* * +* Retour : Adresse de la description du type 'PyLineIter'. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyTypeObject *pylineiter_get_type(void) +{ + static PyTypeObject result = { + + PyObject_HEAD_INIT(NULL) + +#if PY_VERSION_HEX < 0x03000000 + 0, /* ob_size */ +#endif + + "pyoida.analysis.PyLineIter", /* tp_name */ + sizeof(PyLineIter), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)pylineiter_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved / tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ + "PyLineIter objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + PyObject_SelfIter, /* tp_iter */ + (iternextfunc)pylineiter_get_next, /* tp_iternext */ + 0, /* tp_methods */ + }; + + return &result; + +} + + +/****************************************************************************** +* * +* Paramètres : head = liste à parcourir. * +* * +* Description : Prépare l'itérateur pour un parcours de lignes de rendu. * +* * +* Retour : Instance d'itérateur prête à emploi. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *pylineiter_new(GRenderingLine *head) +{ + PyLineIter *result; /* Nouvelle instance à renvoyer*/ + PyTypeObject *type; /* Type d'objet à créer */ + + type = pylineiter_get_type(); + + result = (PyLineIter *)type->tp_alloc(type, 0); + + if (result != NULL) + { + result->head = head; + result->cur = NULL; + } + + return (PyObject *)result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = instance d'objet à supprimer. * +* * +* Description : Libère la mémoire occupée par un itérateur de 'PyLine'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void pylineiter_dealloc(PyLineIter *self) +{ +#if PY_VERSION_HEX < 0x03000000 + self->ob_type->tp_free((PyObject *)self); +#else + Py_TYPE(self)->tp_free((PyObject *)self); +#endif + +} + + +/****************************************************************************** +* * +* Paramètres : self = instance manipulée à traiter. * +* * +* Description : Fournit l'élément suivant dans un parcours de lignes. * +* * +* Retour : Point suivant du parcours ou NULL. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *pylineiter_get_next(PyLineIter *self) +{ + PyObject *result; /* Elément à retourner */ + GRenderingLine *next; /* Elément réel suivant */ + + if (self->cur == NULL) next = self->head; + else next = g_rendering_line_get_next_iter(self->head, self->cur, NULL); + + if (next != NULL) + { + self->cur = next; + result = pyline_new_from_existing(self->head, next); + } + + else result = NULL; + + return (PyObject *)result; + +} diff --git a/plugins/pyoida/analysis/py_line.h b/plugins/pyoida/analysis/py_line.h new file mode 100644 index 0000000..681e13b --- /dev/null +++ b/plugins/pyoida/analysis/py_line.h @@ -0,0 +1,45 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * py_line.h - prototypes pour l'intermédiaire des lignes de représentation pour Python + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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_PYOIDA_PY_LINE_H +#define _PLUGINS_PYOIDA_PY_LINE_H + + +#include <Python.h> +#include <stdbool.h> + + +#include "../../../src/analysis/line.h" + + + +/* Initialise le greffon permettant l'usage de Python. */ +PyObject *pyline_new_from_existing(GRenderingLine *, GRenderingLine *); + +/* Ajoute l'objet 'line' au module Python. */ +bool add_line_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_PY_LINE_H */ diff --git a/plugins/pyoida/analysis/py_line_code.c b/plugins/pyoida/analysis/py_line_code.c new file mode 100644 index 0000000..26fe513 --- /dev/null +++ b/plugins/pyoida/analysis/py_line_code.c @@ -0,0 +1,281 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * py_line_code.h - prototypes pour l'intermédiaire des lignes de code pour Python + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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 "py_line_code.h" + + +#include "py_line-int.h" + + + +typedef struct { + + PyLine base; /* Classe dérivée */ + +} PyCodeLine; + + + + +/* Fournit la description du type 'PyCodeLine' pour Python. */ +static PyTypeObject *pycodeline_get_type(void); + + + + +static int +pycodeline_init(PyCodeLine *self, PyObject *args, PyObject *kwds); + +static PyObject * +pycodeline_new(PyTypeObject *type, PyObject *args, PyObject *kwds); + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit la description du type 'PyCodeLine' pour Python. * +* * +* Retour : Adresse de la description du type 'PyCodeLine'. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyTypeObject *pycodeline_get_type(void) +{ + static PyMethodDef pycodeline_methods[] = { +#if 0 + {"name", (PyCFunction)pyline_name, METH_NOARGS, + "Return the name, combining the first and last name" + }, +#endif + { NULL } +}; + + static PyTypeObject result = { + + PyObject_HEAD_INIT(NULL) + +#if PY_VERSION_HEX < 0x03000000 + 0, /*ob_size*/ +#endif + + "pyoida.analysis.PyCodeLine", /* tp_name */ + sizeof(PyCodeLine), /* tp_basicsize */ + 0, /* tp_itemsize */ + 0, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved / tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pycodeline objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + pycodeline_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)pycodeline_init, /* tp_init */ + 0, /* tp_alloc */ + pycodeline_new, /* tp_new */ + }; + + return &result; + +} + + + + +static int +pycodeline_init(PyCodeLine *self, PyObject *args, PyObject *kwds) +{ +#if 0 + PyObject *first=NULL, *last=NULL, *tmp; + + static char *kwlist[] = {"first", "last", "number", NULL}; + + printf("pycodeline_init\n"); + + if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSi", kwlist, + &first, &last, + &self->number)) + return -1; + + if (first) { + tmp = self->first; + Py_INCREF(first); + self->first = first; + Py_DECREF(tmp); + } + + if (last) { + tmp = self->last; + Py_INCREF(last); + self->last = last; + Py_DECREF(tmp); + } +#endif + return 0; +} + + + + + +static PyObject * +pycodeline_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyCodeLine *self; + + + printf("creating a new line\n"); + + self = (PyCodeLine *)type->tp_alloc(type, 0); + if (self != NULL) { +#if 0 + self->first = PyString_FromString("");//PyUnicode_FromString(""); + if (self->first == NULL) + { + Py_DECREF(self); + return NULL; + } + + self->last = PyString_FromString("");//PyUnicode_FromString(""); + if (self->last == NULL) + { + Py_DECREF(self); + return NULL; + } + + self->number = 0; +#endif + } + + return (PyObject *)self; +} + + + + + +/****************************************************************************** +* * +* Paramètres : head = ???? * +* line = modèle à représenter en Python. * +* * +* Description : Initialise le greffon permettant l'usage de Python. * +* * +* Retour : Nouvelle instance d'objet Python ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *pycodeline_new_from_existing(GRenderingLine *head, GCodeLine *line) +{ + PyCodeLine *result; /* Nouvelle instance à renvoyer*/ + PyTypeObject *type; /* Type d'objet à créer */ + + result = (PyCodeLine *)g_object_get_data(G_OBJECT(line), "pyobject"); + + if (result == NULL) + { + type = pycodeline_get_type(); + + result = (PyCodeLine *)type->tp_alloc(type, 0); + + if (result != NULL) + { + pyline_init_from_existing((PyLine *)result, head, line); + + g_object_set_data(G_OBJECT(line), "pyobject", result); + + } + + } + + return (PyObject *)result; + +} + + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'line' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_pycodeline_to_python_module(PyObject *module) +{ + + + pycodeline_get_type()->tp_base = pyline_get_type(); + if (PyType_Ready(pycodeline_get_type()) < 0) + return false; + + + Py_INCREF(pycodeline_get_type()); + PyModule_AddObject(module, "codeline", (PyObject *)pycodeline_get_type()); + + + + return true; /* FIXME */ + +} diff --git a/plugins/pyoida/analysis/py_line_code.h b/plugins/pyoida/analysis/py_line_code.h new file mode 100644 index 0000000..8a67b44 --- /dev/null +++ b/plugins/pyoida/analysis/py_line_code.h @@ -0,0 +1,45 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * py_line_code.h - prototypes pour l'intermédiaire des lignes de code pour Python + * + * Copyright (C) 2009 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA 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. + * + * OpenIDA 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_PYOIDA_PY_LINE_CODE_H +#define _PLUGINS_PYOIDA_PY_LINE_CODE_H + + +#include <Python.h> +#include <stdbool.h> + + +#include "../../../src/analysis/line_code.h" + + + +/* Initialise le greffon permettant l'usage de Python. */ +PyObject *pycodeline_new_from_existing(GRenderingLine *, GCodeLine *); + +/* Ajoute l'objet 'PyCodeLine' au module Python. */ +bool add_pycodeline_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_PY_LINE_CODE_H */ |