From 7cbdd17b441b35d48624956aa438bde69f18bc37 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Wed, 31 Mar 2010 21:12:35 +0000 Subject: Implemented first steps to a Python plugins support. git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@146 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a --- ChangeLog | 75 +++ configure.ac | 2 + plugins/pyoida/Makefile.am | 7 +- plugins/pyoida/analysis/Makefile.am | 11 +- plugins/pyoida/analysis/binary.c | 279 +++++++++++ plugins/pyoida/analysis/binary.h | 44 ++ plugins/pyoida/analysis/exporter-int.h | 53 ++ plugins/pyoida/analysis/exporter.c | 284 +++++++++++ plugins/pyoida/analysis/exporter.h | 44 ++ plugins/pyoida/analysis/line.c | 793 ++++++++++++++++++++++++++++++ plugins/pyoida/analysis/line.h | 61 +++ plugins/pyoida/analysis/module.c | 76 +++ plugins/pyoida/analysis/module.h | 39 ++ plugins/pyoida/analysis/roptions.c | 272 ++++++++++ plugins/pyoida/analysis/roptions.h | 45 ++ plugins/pyoida/arch/Makefile.am | 17 + plugins/pyoida/arch/archbase.c | 121 +++++ plugins/pyoida/arch/archbase.h | 44 ++ plugins/pyoida/arch/module.c | 72 +++ plugins/pyoida/arch/module.h | 39 ++ plugins/pyoida/arch/processor.c | 314 ++++++++++++ plugins/pyoida/arch/processor.h | 39 ++ plugins/pyoida/format/Makefile.am | 16 + plugins/pyoida/format/executable.c | 223 +++++++++ plugins/pyoida/format/executable.h | 47 ++ plugins/pyoida/format/module.c | 68 +++ plugins/pyoida/format/module.h | 39 ++ plugins/pyoida/plugin.c | 555 +++++++++++++++++++++ plugins/pyoida/plugin.h | 88 ++++ plugins/pyoida/pyoida.c | 69 ++- plugins/python/lnxsyscalls/lnxsyscalls.py | 517 +++++++++++++++++++ src/analysis/exporter-int.h | 2 +- src/analysis/line-int.h | 3 + src/analysis/line.c | 75 +++ src/analysis/line.h | 9 + src/analysis/line_code.c | 37 +- src/common/Makefile.am | 1 + src/common/dllist.h | 7 +- src/common/environment.c | 94 ++++ src/common/environment.h | 40 ++ src/plugins/Makefile.am | 1 + src/plugins/pglist.c | 24 + src/plugins/plugin-def.h | 3 +- src/plugins/plugin-int.h | 74 +++ src/plugins/plugin.c | 36 +- 45 files changed, 4700 insertions(+), 59 deletions(-) create mode 100644 plugins/pyoida/analysis/binary.c create mode 100644 plugins/pyoida/analysis/binary.h create mode 100644 plugins/pyoida/analysis/exporter-int.h create mode 100644 plugins/pyoida/analysis/exporter.c create mode 100644 plugins/pyoida/analysis/exporter.h create mode 100644 plugins/pyoida/analysis/line.c create mode 100644 plugins/pyoida/analysis/line.h create mode 100644 plugins/pyoida/analysis/module.c create mode 100644 plugins/pyoida/analysis/module.h create mode 100644 plugins/pyoida/analysis/roptions.c create mode 100644 plugins/pyoida/analysis/roptions.h create mode 100644 plugins/pyoida/arch/Makefile.am create mode 100644 plugins/pyoida/arch/archbase.c create mode 100644 plugins/pyoida/arch/archbase.h create mode 100644 plugins/pyoida/arch/module.c create mode 100644 plugins/pyoida/arch/module.h create mode 100644 plugins/pyoida/arch/processor.c create mode 100644 plugins/pyoida/arch/processor.h create mode 100644 plugins/pyoida/format/Makefile.am create mode 100644 plugins/pyoida/format/executable.c create mode 100644 plugins/pyoida/format/executable.h create mode 100644 plugins/pyoida/format/module.c create mode 100644 plugins/pyoida/format/module.h create mode 100644 plugins/pyoida/plugin.c create mode 100644 plugins/pyoida/plugin.h create mode 100644 plugins/python/lnxsyscalls/lnxsyscalls.py create mode 100644 src/common/environment.c create mode 100644 src/common/environment.h create mode 100644 src/plugins/plugin-int.h diff --git a/ChangeLog b/ChangeLog index e6a92f4..f582bb3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,78 @@ +10-03-31 Cyrille Bagard + + * configure.ac: + Add the new Makefiles from the 'plugins/pyoida/*' directories to + AC_CONFIG_FILES. + + * plugins/pyoida/analysis/binary.c: + * plugins/pyoida/analysis/binary.h: + * plugins/pyoida/analysis/exporter.c: + * plugins/pyoida/analysis/exporter.h: + * plugins/pyoida/analysis/exporter-int.h: + * plugins/pyoida/analysis/line.c: + * plugins/pyoida/analysis/line.h: + * plugins/pyoida/analysis/Makefile.am: + * plugins/pyoida/analysis/module.c: + * plugins/pyoida/analysis/module.h: + * plugins/pyoida/analysis/roptions.c: + * plugins/pyoida/analysis/roptions.h: + * plugins/pyoida/arch/archbase.c: + * plugins/pyoida/arch/archbase.h: + * plugins/pyoida/arch/Makefile.am: + * plugins/pyoida/arch/module.c: + * plugins/pyoida/arch/module.h: + * plugins/pyoida/arch/processor.c: + * plugins/pyoida/arch/processor.h: + * plugins/pyoida/format/executable.c: + * plugins/pyoida/format/executable.h: + * plugins/pyoida/format/Makefile.am: + * plugins/pyoida/format/module.c: + * plugins/pyoida/format/module.h: + * plugins/pyoida/Makefile.am: + * plugins/pyoida/plugin.c: + * plugins/pyoida/plugin.h: + * plugins/pyoida/pyoida.c: + * plugins/python/lnxsyscalls/lnxsyscalls.py: + New entries: implement first steps to a Python plugins support. + + * src/analysis/exporter-int.h: + Typo. + + * src/analysis/line.c: + Add functions to set or get comments. Provide the previous line + of a given one. + + * src/analysis/line_code.c: + Improve the speed of exporting to GTK buffers. + + * src/analysis/line.h: + * src/analysis/line-int.h: + Add functions to set or get comments. Provide the previous line + of a given one. + + * src/common/dllist.h: + Add a function to retrieve the previous item. + + * src/common/environment.c: + * src/common/environment.h: + New entries: deal with environment variables. + + * src/common/Makefile.am: + Add environment.[ch] to libcommon_la_SOURCES. + + * src/plugins/Makefile.am: + Add plugin-int.h to libplugins_la_SOURCES. + + * src/plugins/pglist.c: + Allow extern code to add plugins. + + * src/plugins/plugin.c: + * src/plugins/plugin-def.h: + Move some parts of the plugins definition. + + * src/plugins/plugin-int.h: + New entry: move some parts of the plugins definition. + 10-03-28 Cyrille Bagard * src/analysis/binary.c: diff --git a/configure.ac b/configure.ac index 11a178d..502e4c3 100644 --- a/configure.ac +++ b/configure.ac @@ -235,6 +235,8 @@ AC_CONFIG_FILES([Makefile plugins/govm/Makefile plugins/pyoida/Makefile plugins/pyoida/analysis/Makefile + plugins/pyoida/arch/Makefile + plugins/pyoida/format/Makefile plugins/stackvars/Makefile src/Makefile src/analysis/Makefile diff --git a/plugins/pyoida/Makefile.am b/plugins/pyoida/Makefile.am index 1dd0d1f..923d8c9 100644 --- a/plugins/pyoida/Makefile.am +++ b/plugins/pyoida/Makefile.am @@ -2,11 +2,14 @@ pkglib_LTLIBRARIES = pyoida.la pyoida_la_SOURCES = \ + plugin.h plugin.c \ py_log.h py_log.c \ pyoida.h pyoida.c pyoida_la_LIBADD = \ - analysis/libpyoidaanalysis.la + analysis/libpyoidaanalysis.la \ + arch/libpyoidaarch.la \ + format/libpyoidaformat.la pyoida_la_LDFLAGS = -module -avoid-version $(LIBGTK_LIBS) $(LIBXML_LIBS) $(LIBPYTHON_LIBS) \ -L../../src/panels/ -lpanels -L../../src/.libs -loidadisass -loidagtkext \ @@ -19,4 +22,4 @@ AM_CPPFLAGS = AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) -SUBDIRS = analysis +SUBDIRS = analysis arch format diff --git a/plugins/pyoida/analysis/Makefile.am b/plugins/pyoida/analysis/Makefile.am index df49499..e67f188 100644 --- a/plugins/pyoida/analysis/Makefile.am +++ b/plugins/pyoida/analysis/Makefile.am @@ -2,15 +2,18 @@ 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 + binary.h binary.c \ + exporter-int.h \ + exporter.h exporter.c \ + line.h line.c \ + module.h module.c \ + roptions.h roptions.c libpyoidaanalysis_la_LDFLAGS = -INCLUDES = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) +INCLUDES = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) -I../../../src AM_CPPFLAGS = diff --git a/plugins/pyoida/analysis/binary.c b/plugins/pyoida/analysis/binary.c new file mode 100644 index 0000000..d378496 --- /dev/null +++ b/plugins/pyoida/analysis/binary.c @@ -0,0 +1,279 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * binary.c - équivalent Python du fichier "analysis/binary.h" + * + * Copyright (C) 2010 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 "binary.h" + + +#include "line.h" +#include "../format/executable.h" + + + + +/* Classe 'analysis.binary' pour Python */ +typedef struct _py_binary +{ + PyObject_HEAD + + GOpenidaBinary *binary; /* Référence GLib */ + +} py_binary; + + + + +/* Crée un nouvel objet Python de type 'py_binary'. */ +static PyObject *py_binary_new(PyTypeObject *, PyObject *, PyObject *); + + + + +/* Fournit le format de fichier reconnu dans le contenu binaire. */ +static PyObject *py_binary_get_format(py_binary *); + +/* Fournit les lignes de rendu associé pour Python. */ +static PyObject *py_binary_get_lines(py_binary *); + + + +/* Fournit le type d'objet 'analysis.binary' pour Python. */ +static PyTypeObject *get_analysis_binary_type(void); + + + + +/****************************************************************************** +* * +* Paramètres : type = type de l'objet à instancier. * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Crée un nouvel objet Python de type 'py_binary'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_binary_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + py_binary *result; /* Instance à retourner */ + + result = (py_binary *)type->tp_alloc(type, 0); + + return (PyObject *)result; + +} + + +/****************************************************************************** +* * +* Paramètres : binary = objet GLib existant à transposer. * +* * +* Description : Crée un nouvel objet Python de type 'py_binary'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *py_binary_new_from_existing(GOpenidaBinary *binary) +{ + py_binary *result; /* Instance à retourner */ + PyTypeObject *type; /* Type Python à instancier */ + + result = (py_binary *)g_object_get_data(G_OBJECT(binary), "python_object"); + + if (result == NULL) + { + type = get_analysis_binary_type(); + + result = (py_binary *)type->tp_alloc(type, 0); + + result->binary = binary; + g_object_ref(binary); + + g_object_set_data(G_OBJECT(binary), "python_object", result); + + } + else Py_INCREF((PyObject *)result); + + return (PyObject *)result; + +} + + + + + +/****************************************************************************** +* * +* Paramètres : self = instance manipulée à traiter. * +* * +* Description : Fournit le format de fichier reconnu dans le contenu binaire.* +* * +* Retour : Nouvelle instance d'objet Python ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_binary_get_format(py_binary *self) +{ + PyObject *result; /* Liste à retourner */ + GExeFormat *format; /* Format récupéré à convertir */ + + format = g_openida_binary_get_format(self->binary); + + result = py_executable_convert(format); + + return result; + +} + + +/****************************************************************************** +* * +* 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 *py_binary_get_lines(py_binary *self) +{ + PyObject *result; /* Liste à retourner */ + GRenderingLine *lines; /* Liste récupérée à convertir */ + + lines = g_openida_binary_get_lines(self->binary); + + result = py_line_new_from_existing(lines); + + return result; + +} + + + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit le type d'objet 'analysis.binary' pour Python. * +* * +* Retour : Adresse du type vivant à manipuler. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyTypeObject *get_analysis_binary_type(void) +{ + static PyTypeObject *result = NULL; /* Type pour objet à retourner */ + + static PyMethodDef py_binary_methods[] = { + { + "get_format", (PyCFunction)py_binary_get_format, + METH_NOARGS, + "Give the file format recognized in the binary content." + }, + { + "get_lines", (PyCFunction)py_binary_get_lines, + METH_NOARGS, + "Provide the rendering lines used by the binary." + }, + { NULL } + }; + + static PyGetSetDef py_binary_getset[] = { + { NULL } + }; + + static PyTypeObject py_binary_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pyoida.analysis.Binary", + .tp_basicsize = sizeof(py_binary), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyOIDA loaded binary to analyse", + + .tp_methods = py_binary_methods, + .tp_getset = py_binary_getset, + .tp_new = (newfunc)py_binary_new + + }; + + if (result == NULL) result = &py_binary_type; + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'analysis.binary' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_analysis_binary_to_python_module(PyObject *module) +{ + PyTypeObject *py_binary_type; /* Type défini pour Python */ + int ret; /* Bilan d'un appel */ + + py_binary_type = get_analysis_binary_type(); + + if (PyType_Ready(py_binary_type) < 0) + return false; + + Py_INCREF(py_binary_type); + ret = PyModule_AddObject(module, "Binary", (PyObject *)py_binary_type); + + return (ret == 0); + +} diff --git a/plugins/pyoida/analysis/binary.h b/plugins/pyoida/analysis/binary.h new file mode 100644 index 0000000..1500a06 --- /dev/null +++ b/plugins/pyoida/analysis/binary.h @@ -0,0 +1,44 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * binary.h - prototypes pour l'équivalent Python du fichier "analysis/binary.h" + * + * Copyright (C) 2010 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_ANALYSIS_BINARY_H +#define _PLUGINS_PYOIDA_ANALYSIS_BINARY_H + + +#include +#include + +#include + + + +/* Crée un nouvel objet Python de type 'py_binary'. */ +PyObject *py_binary_new_from_existing(GOpenidaBinary *); + +/* Ajoute l'objet 'analysis.binary' au module Python. */ +bool add_analysis_binary_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_ANALYSIS_BINARY_H */ diff --git a/plugins/pyoida/analysis/exporter-int.h b/plugins/pyoida/analysis/exporter-int.h new file mode 100644 index 0000000..2498d4b --- /dev/null +++ b/plugins/pyoida/analysis/exporter-int.h @@ -0,0 +1,53 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * exporter-int.h - prototypes internes pour l'équivalent Python du fichier "analysis/exporter.h" + * + * Copyright (C) 2010 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_ANALYSIS_EXPORTER_INT_H +#define _PLUGINS_PYOIDA_ANALYSIS_EXPORTER_INT_H + + +#include + +#include + + + +/* Classe 'analysis.exporter' pour Python */ +typedef struct _py_exporter +{ + PyObject_HEAD + + GContentExporter *glib; /* Interface version GLib */ + +} py_exporter; + + +#define PY_EXPORTER(exp) ((py_exporter *)exp) + + +/* Fournit le type d'objet 'analysis.exporter' pour Python. */ +PyTypeObject *get_analysis_exporter_type(void); + + + +#endif /* _PLUGINS_PYOIDA_ANALYSIS_EXPORTER_INT_H */ diff --git a/plugins/pyoida/analysis/exporter.c b/plugins/pyoida/analysis/exporter.c new file mode 100644 index 0000000..8c49542 --- /dev/null +++ b/plugins/pyoida/analysis/exporter.c @@ -0,0 +1,284 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * exporter.c - équivalent Python du fichier "analysis/exporter.h" + * + * Copyright (C) 2010 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 "exporter.h" + + +#include +#include +#include + + +#include "exporter-int.h" +#include "../analysis/roptions.h" + + + + +#define PY_EXPORT_TEXT_BUFLEN 64 + + +/* Crée un nouvel objet Python de type 'py_exporter'. */ +static PyObject *py_exporter_new(PyTypeObject *, PyObject *, PyObject *); + + + + +/* Fournit le texte correspondant à un contenu rendu. */ +static PyObject *py_exporter_get_text(py_exporter *, PyObject *); + + + + + +/****************************************************************************** +* * +* Paramètres : type = type de l'objet à instancier. * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Crée un nouvel objet Python de type 'py_exporter'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_exporter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + py_exporter *result; /* Instance à retourner */ + + result = (py_exporter *)type->tp_alloc(type, 0); + + return (PyObject *)result; + +} + + +/****************************************************************************** +* * +* Paramètres : exporter = objet GLib existant à transposer. * +* * +* Description : Crée un nouvel objet Python de type 'py_exporter'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *py_exporter_new_from_existing(GContentExporter *exporter) +{ + py_exporter *result; /* Instance à retourner */ + PyTypeObject *type; /* Type Python à instancier */ + + result = (py_exporter *)g_object_get_data(G_OBJECT(exporter), "python_object"); + + if (result == NULL) + { + type = get_analysis_exporter_type(); + + result = (py_exporter *)type->tp_alloc(type, 0); + + result->glib = exporter; + g_object_ref(exporter); + + g_object_set_data(G_OBJECT(exporter), "python_object", result); + + } + else Py_INCREF((PyObject *)result); + + return (PyObject *)result; + +} + + + + + +/****************************************************************************** +* * +* Paramètres : self = instance manipulée à traiter. * +* args = arguments fournis à l'appel. * +* * +* Description : Fournit le texte correspondant à un contenu rendu. * +* * +* Retour : Chaîne de caractères Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_exporter_get_text(py_exporter *self, PyObject *args) +{ + PyObject *result; /* Liste à retourner */ + PyObject *roptions; /* Options de rendu jointes */ + int ret; /* Bilan de lecture des args. */ + GRenderingOptions *_roptions; /* Version GLib des options */ + int fd[2]; /* Tube pour récupérer le code */ + FILE *stream; /* Flux (ré)ouvert en écriture */ + char buffer[PY_EXPORT_TEXT_BUFLEN]; /* Tampon pour la lecture */ + ssize_t len; /* Nombre de caractères lus */ + PyObject *src; /* Source ajoutée au résultat */ + + ret = PyArg_ParseTuple(args, "O", &roptions); + if (!ret) return Py_None; + + _roptions = py_rendering_options_get_glib_instance(roptions); + + result = Py_None; + + ret = pipe2(fd, O_NONBLOCK); + //if (ret ... perror("pipe"); goto pegt_pipe_error; + + + stream = fdopen(fd[1], "w"); + if (stream == NULL) + { + perror("stream"); + goto pegt_stream_error; + } + + g_content_exporter_add_text(self->glib, _roptions, MRD_BLOCK/* FIXME*/, stream); + fflush(stream); + + do + { + memset(buffer, 0, PY_EXPORT_TEXT_BUFLEN); + len = read(fd[0], buffer, PY_EXPORT_TEXT_BUFLEN); + + src = PyString_FromString(buffer); + + if (result == Py_None) result = src; + else PyString_ConcatAndDel(&result, src); + + } + while (len > 0); + + fclose(stream); + + pegt_stream_error: + + close(fd[0]); + //close(fd[1]); + + pegt_pipe_error: + + return result; + +} + + + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit le type d'objet 'analysis.exporter' pour Python. * +* * +* Retour : Adresse du type vivant à manipuler. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_analysis_exporter_type(void) +{ + static PyTypeObject *result = NULL; /* Type pour objet à retourner */ + + static PyMethodDef py_exporter_methods[] = { + { + "get_text", (PyCFunction)py_exporter_get_text, + METH_VARARGS, + "Provide the text version of the rendered content." + }, + { NULL } + }; + + static PyGetSetDef py_exporter_getset[] = { + { NULL } + }; + + static PyTypeObject py_exporter_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pyoida.analysis.Exporter", + .tp_basicsize = sizeof(py_exporter), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyOIDA exporter interface", + + .tp_methods = py_exporter_methods, + .tp_getset = py_exporter_getset, + .tp_new = (newfunc)py_exporter_new + + }; + + if (result == NULL) result = &py_exporter_type; + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'analysis.exporter' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_analysis_exporter_to_python_module(PyObject *module) +{ + PyTypeObject *py_exporter_type; /* Type défini pour Python */ + int ret; /* Bilan d'un appel */ + + py_exporter_type = get_analysis_exporter_type(); + + if (PyType_Ready(py_exporter_type) < 0) + return false; + + Py_INCREF(py_exporter_type); + ret = PyModule_AddObject(module, "Exporter", (PyObject *)py_exporter_type); + + return (ret == 0); + +} diff --git a/plugins/pyoida/analysis/exporter.h b/plugins/pyoida/analysis/exporter.h new file mode 100644 index 0000000..4668934 --- /dev/null +++ b/plugins/pyoida/analysis/exporter.h @@ -0,0 +1,44 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * exporter.h - prototypes pour l'équivalent Python du fichier "analysis/exporter.h" + * + * Copyright (C) 2010 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_ANALYSIS_EXPORTER_H +#define _PLUGINS_PYOIDA_ANALYSIS_EXPORTER_H + + +#include +#include + +#include + + + +/* Crée un nouvel objet Python de type 'py_exporter'. */ +PyObject *py_exporter_new_from_existing(GContentExporter *); + +/* Ajoute l'objet 'analysis.exporter' au module Python. */ +bool add_analysis_exporter_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_ANALYSIS_EXPORTER_H */ diff --git a/plugins/pyoida/analysis/line.c b/plugins/pyoida/analysis/line.c new file mode 100644 index 0000000..fd697e2 --- /dev/null +++ b/plugins/pyoida/analysis/line.c @@ -0,0 +1,793 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * line.c - équivalent Python du fichier "analysis/line.h" + * + * Copyright (C) 2010 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 "line.h" + + +#include "exporter-int.h" +#include "../arch/archbase.h" + + + + + + + +/* Classe 'analysis.line' pour Python */ +typedef struct _py_line +{ + py_exporter parent; /* A laisser en premier */ + + GRenderingLine *line; /* Référence GLib */ + +} py_line; + + + + +/* Crée un nouvel objet Python de type 'py_line'. */ +static PyObject *py_line_new(PyTypeObject *, PyObject *, PyObject *); + + + + + + +/* Fournit le type d'objet 'analysis.line' pour Python. */ +static PyTypeObject *get_analysis_line_type(void); + + + +/* Prépare un parcours de lignes. */ +static PyObject *py_line_get_iter(py_line *); + +/* Prépare un parcours de lignes en sens inverse. */ +static PyObject *py_line_get_riter(py_line *); + + + +/* Fournit l'adresse physique ou en mémoire d'une ligne. */ +static PyObject *py_line_get_address(py_line *, void *); + +/* Fournit le commentaire associé à la ligne s'il existe. */ +static PyObject *py_line_get_comment(py_line *, void *); + +/* Définit ou supprime un commentaire pour la ligne indiquée. */ +static int py_line_set_comment(py_line *, PyObject *, void *); + + + + + +/* ---------------------- ITERATEUR POUR LE PARCOURS DE LIGNES ---------------------- */ + + +/* Itérateur pour lignes de rendu */ +typedef struct _py_line_iter +{ + PyObject_HEAD /* A laisser en premier */ + + GRenderingLine *head; /* Liste à parcourir */ + GRenderingLine *cur; /* Point de parcours courant */ + +} py_line_iter; + + +/* Fournit le type d'objet 'analysis.LineIter' pour Python. */ +static PyTypeObject *get_analysis_line_iter_type(void); + +/* Prépare l'itérateur pour un parcours de lignes de rendu. */ +static PyObject *py_line_iter_new(GRenderingLine *); + +/* Libère la mémoire occupée par un itérateur de 'py_line'. */ +static void py_line_iter_dealloc(py_line_iter *); + +/* Fournit l'élément suivant dans un parcours de lignes. */ +static PyObject *py_line_iter_get_next(py_line_iter *); + + + +/* ---------------------- ITERATEUR POUR LE PARCOURS DE LIGNES ---------------------- */ + + +/* Itérateur pour lignes de rendu */ +typedef py_line_iter py_line_riter; + + +/* Fournit le type d'objet 'analysis.LineRevIter' pour Python. */ +static PyTypeObject *get_analysis_line_riter_type(void); + +/* Prépare l'itérateur pour un parcours de lignes de rendu. */ +static PyObject *py_line_riter_new(GRenderingLine *); + +/* Libère la mémoire occupée par un itérateur de 'py_line'. */ +static void py_line_riter_dealloc(py_line_riter *); + +/* Fournit l'élément précédant dans un parcours de lignes. */ +static PyObject *py_line_riter_get_prev(py_line_riter *); + + + + + + + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : type = type de l'objet à instancier. * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Crée un nouvel objet Python de type 'py_line'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_line_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + py_line *result; /* Instance à retourner */ + + result = (py_line *)type->tp_alloc(type, 0); + + return (PyObject *)result; + +} + + +/****************************************************************************** +* * +* Paramètres : line = objet GLib existant à transposer. * +* * +* Description : Crée un nouvel objet Python de type 'py_line'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *py_line_new_from_existing(GRenderingLine *line) +{ + py_line *result; /* Instance à retourner */ + PyTypeObject *type; /* Type Python à instancier */ + + result = (py_line *)g_object_get_data(G_OBJECT(line), "python_object"); + + if (result == NULL) + { + type = get_analysis_line_type(); + + result = (py_line *)type->tp_alloc(type, 0); + + PY_EXPORTER(result)->glib = G_CONTENT_EXPORTER(line); /* TODO : move me ! */ + + result->line = line; + g_object_ref(line); + + g_object_set_data(G_OBJECT(line), "python_object", result); + + } + else Py_INCREF((PyObject *)result); + + return (PyObject *)result; + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit le type d'objet 'analysis.line' pour Python. * +* * +* Retour : Adresse du type vivant à manipuler. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyTypeObject *get_analysis_line_type(void) +{ + static PyTypeObject *result = NULL; /* Type pour objet à retourner */ + + static PyMethodDef py_line_methods[] = { + { + "__reversed__", (PyCFunction)py_line_get_riter, + METH_NOARGS, + "Return a reverse iterator over the rendering lines." + }, + { NULL } + }; + + static PyGetSetDef py_line_getset[] = { + { + "address", + (getter)py_line_get_address, + (setter)NULL, + "Get the physical or virtual address relative to the line." + }, + { + "comment", + (getter)py_line_get_comment, + (setter)py_line_set_comment, + "Get or set a comment for the rendering line." + }, + { NULL } + }; + + static PyTypeObject py_line_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pyoida.analysis.Line", + .tp_basicsize = sizeof(py_line), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyOIDA rendering line", + + .tp_iter = (getiterfunc)py_line_get_iter, + + .tp_methods = py_line_methods, + .tp_getset = py_line_getset, + .tp_new = (newfunc)py_line_new + + }; + + if (result == NULL) + { + py_line_type.tp_base = get_analysis_exporter_type(); + result = &py_line_type; + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'analysis.line' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_analysis_line_to_python_module(PyObject *module) +{ + PyTypeObject *py_line_type; /* Type défini pour Python */ + int ret; /* Bilan d'un appel */ + + py_line_type = get_analysis_line_type(); + + if (PyType_Ready(py_line_type) < 0) + return false; + + Py_INCREF(py_line_type); + ret = PyModule_AddObject(module, "Line", (PyObject *)py_line_type); + + return (ret == 0); + +} + + + + + + + + +/****************************************************************************** +* * +* 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 *py_line_get_iter(py_line *self) +{ + return (PyObject *)py_line_iter_new(self->line); + +} + + +/****************************************************************************** +* * +* Paramètres : self = instance manipulée à traiter. * +* * +* Description : Prépare un parcours de lignes en sens inverse. * +* * +* Retour : Point de départ d'un parcours. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_line_get_riter(py_line *self) +{ + return (PyObject *)py_line_riter_new(self->line); + +} + + + + + +/****************************************************************************** +* * +* Paramètres : self = classe présentant une ligne de rendu. * +* data = adresse non utilisée ici. * +* * +* Description : Fournit l'adresse physique ou en mémoire d'une ligne. * +* * +* Retour : Position physique ou en mémoire associée à la ligne. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_line_get_address(py_line *self, void *data) +{ + vmpa_t value; /* Adresse à convertir */ + + value = get_rendering_line_address(self->line); + + return py_vmpa_new_from_existing(value); + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe présentant une ligne de rendu. * +* data = adresse non utilisée ici. * +* * +* Description : Fournit le commentaire associé à la ligne s'il existe. * +* * +* Retour : Chaîne de caractères ou NULL. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_line_get_comment(py_line *self, void *data) +{ + const char *comment; /* Commentaire à transmettre */ + + comment = get_rendering_line_comment(self->line); + + return Py_BuildValue("s", comment); + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe présentant des options de représentation. * +* value = nouvelle valeur affectée. * +* data = adresse non utilisée ici. * +* * +* Description : Définit ou supprime un commentaire pour la ligne indiquée. * +* * +* Retour : Bilan de la mise à jour. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_line_set_comment(py_line *self, PyObject *value, void *data) +{ + char *comment; /* Commentaire à définir */ + + comment = PyString_AsString(value); + /* FIXME : nil pour supprimer ? */ + + set_rendering_line_comment(self->line, comment); + + return 0; + +} + + + + + + + + + + + + + + + +/* ---------------------------------------------------------------------------------- */ +/* ITERATEUR POUR LE PARCOURS DE LIGNES */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit le type d'objet 'analysis.LineIter' pour Python. * +* * +* Retour : Adresse du type vivant à manipuler. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyTypeObject *get_analysis_line_iter_type(void) +{ + static PyTypeObject *result = NULL; /* Type pour objet à retourner */ + + static PyTypeObject py_line_iter_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pyoida.analysis.LineIter", + .tp_basicsize = sizeof(py_line_iter), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyOIDA iterator for rendering lines", + + .tp_iter = PyObject_SelfIter, + .tp_iternext = (iternextfunc)py_line_iter_get_next + + }; + + if (result == NULL) result = &py_line_iter_type; + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'analysis.LineIter' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_analysis_line_iter_to_python_module(PyObject *module) +{ + PyTypeObject *py_line_iter_type; /* Type défini pour Python */ + int ret; /* Bilan d'un appel */ + + py_line_iter_type = get_analysis_line_iter_type(); + + if (PyType_Ready(py_line_iter_type) < 0) + return false; + + Py_INCREF(py_line_iter_type); + ret = PyModule_AddObject(module, "Iter", (PyObject *)py_line_iter_type); + + return (ret == 0); + +} + + +/****************************************************************************** +* * +* 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 *py_line_iter_new(GRenderingLine *head) +{ + py_line_iter *result; /* Nouvelle instance à renvoyer*/ + PyTypeObject *type; /* Type d'objet à créer */ + + type = get_analysis_line_iter_type(); + + result = (py_line_iter *)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 'py_line'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_line_iter_dealloc(py_line_iter *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 *py_line_iter_get_next(py_line_iter *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 = py_line_new_from_existing(next); + Py_INCREF(result); + } + else result = NULL; + + return (PyObject *)result; + +} + +/* ---------------------------------------------------------------------------------- */ +/* ITERATEUR POUR LE PARCOURS DE LIGNES */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit le type d'objet 'analysis.LineRevIter' pour Python. * +* * +* Retour : Adresse du type vivant à manipuler. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyTypeObject *get_analysis_line_riter_type(void) +{ + static PyTypeObject *result = NULL; /* Type pour objet à retourner */ + + static PyTypeObject py_line_riter_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pyoida.analysis.LineRevIter", + .tp_basicsize = sizeof(py_line_riter), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyOIDA iterator for rendering lines", + + .tp_iter = PyObject_SelfIter, + .tp_iternext = (iternextfunc)py_line_riter_get_prev + + }; + + if (result == NULL) result = &py_line_riter_type; + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'analysis.LineRevIter' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_analysis_line_riter_to_python_module(PyObject *module) +{ + PyTypeObject *py_line_riter_type; /* Type défini pour Python */ + int ret; /* Bilan d'un appel */ + + py_line_riter_type = get_analysis_line_riter_type(); + + if (PyType_Ready(py_line_riter_type) < 0) + return false; + + Py_INCREF(py_line_riter_type); + ret = PyModule_AddObject(module, "RevIter", (PyObject *)py_line_riter_type); + + return (ret == 0); + +} + + +/****************************************************************************** +* * +* 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 *py_line_riter_new(GRenderingLine *head) +{ + py_line_riter *result; /* Nouvelle instance à renvoyer*/ + PyTypeObject *type; /* Type d'objet à créer */ + + type = get_analysis_line_riter_type(); + + result = (py_line_riter *)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 'py_line'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_line_riter_dealloc(py_line_riter *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 précédant dans un parcours de lignes. * +* * +* Retour : Point suivant du parcours ou NULL. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_line_riter_get_prev(py_line_riter *self) +{ + PyObject *result; /* Elément à retourner */ + GRenderingLine *prev; /* Elément réel précédant */ + + if (self->cur == NULL) prev = g_rendering_line_get_last_iter(self->head, NULL); + else prev = g_rendering_line_get_prev_iter(self->head, self->cur, NULL); + + if (prev != NULL) + { + self->cur = prev; + result = py_line_new_from_existing(prev); + Py_INCREF(result); + } + else result = NULL; + + return (PyObject *)result; + +} diff --git a/plugins/pyoida/analysis/line.h b/plugins/pyoida/analysis/line.h new file mode 100644 index 0000000..e71596c --- /dev/null +++ b/plugins/pyoida/analysis/line.h @@ -0,0 +1,61 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * line.h - prototypes pour l'équivalent Python du fichier "analysis/line.h" + * + * Copyright (C) 2010 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_ANALYSIS_LINE_H +#define _PLUGINS_PYOIDA_ANALYSIS_LINE_H + + +#include +#include + +#include + + + +/* Crée un nouvel objet Python de type 'py_line'. */ +PyObject *py_line_new_from_existing(GRenderingLine *); + +/* Ajoute l'objet 'analysis.line' au module Python. */ +bool add_analysis_line_to_python_module(PyObject *); + + + + +/* ---------------------- ITERATEUR POUR LE PARCOURS DE LIGNES ---------------------- */ + + +/* Ajoute l'objet 'analysis.LineIter' au module Python. */ +bool add_analysis_line_iter_to_python_module(PyObject *); + + + +/* ---------------------- ITERATEUR POUR LE PARCOURS DE LIGNES ---------------------- */ + + +/* Ajoute l'objet 'analysis.LineRevIter' au module Python. */ +bool add_analysis_line_riter_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_ANALYSIS_LINE_H */ diff --git a/plugins/pyoida/analysis/module.c b/plugins/pyoida/analysis/module.c new file mode 100644 index 0000000..8f07002 --- /dev/null +++ b/plugins/pyoida/analysis/module.c @@ -0,0 +1,76 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * module.c - intégration du répertoire analysis en tant que module + * + * Copyright (C) 2010 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 "module.h" + + +#include "binary.h" +#include "exporter.h" +#include "line.h" +#include "roptions.h" + + + + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute le module 'analysis' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_analysis_module_to_python_module(PyObject *super) +{ + bool result; + PyObject *module; + int ret; /* Bilan d'un appel */ + + static PyMethodDef py_analysis_methods[] = { + { NULL } + }; + + module = Py_InitModule("pyoida.analysis", py_analysis_methods); + if (module == NULL) return false; + + Py_INCREF(module); + ret = PyModule_AddObject(super, "pyoida.analysis", module); + + result = (ret != 0); + + result &= add_analysis_binary_to_python_module(module); + result &= add_analysis_exporter_to_python_module(module); + result &= add_analysis_line_to_python_module(module); + result &= add_analysis_line_iter_to_python_module(module); + result &= add_analysis_line_riter_to_python_module(module); + result &= add_analysis_roptions_to_python_module(module); + + return result; + +} diff --git a/plugins/pyoida/analysis/module.h b/plugins/pyoida/analysis/module.h new file mode 100644 index 0000000..a8bd329 --- /dev/null +++ b/plugins/pyoida/analysis/module.h @@ -0,0 +1,39 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * module.h - prototypes pour l'intégration du répertoire analysis en tant que module + * + * Copyright (C) 2010 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_ANALYSIS_MODULE_H +#define _PLUGINS_PYOIDA_ANALYSIS_MODULE_H + + +#include +#include + + + +/* Ajoute le module 'analysis' au module Python. */ +bool add_analysis_module_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_ANALYSIS_MODULE_H */ diff --git a/plugins/pyoida/analysis/roptions.c b/plugins/pyoida/analysis/roptions.c new file mode 100644 index 0000000..57a4f52 --- /dev/null +++ b/plugins/pyoida/analysis/roptions.c @@ -0,0 +1,272 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * roptions.c - équivalent Python du fichier "analysis/roptions.h" + * + * Copyright (C) 2010 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 "roptions.h" + + +#include "../format/executable.h" + + + + +/* Classe 'analysis.roptions' pour Python */ +typedef struct _py_rendering_options +{ + PyObject_HEAD + + GRenderingOptions *glib; /* Options réelles manipulées */ + +} py_rendering_options; + + + + +/* Crée un nouvel objet Python de type 'py_rendering_options'. */ +static PyObject *py_rendering_options_new(PyTypeObject *, PyObject *, PyObject *); + + + +/* Indique si les adresses des instructions sont à afficher. */ +static PyObject *py_rendering_options_get_show_address(PyObject *, void *); + +/* Affiche (ou non) les adresses des instructions. */ +static int py_rendering_options_set_show_address(PyObject *, PyObject *, void *); + + + + +/* Affiche (ou non) le code des instructions. */ +static PyObject *py_rendering_options_show_code(PyObject *, PyObject *); + + + + + +/****************************************************************************** +* * +* Paramètres : type = type de l'objet à instancier. * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Crée un nouvel objet Python de type 'py_rendering_options'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_rendering_options_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + py_rendering_options *result; /* Instance à retourner */ + PyObject *executable; /* Format d'exécutable joint */ + int ret; /* Bilan de lecture des args. */ + GExeFormat *_executable; /* Version GLib du format */ + + ret = PyArg_ParseTuple(args, "O", &executable); + if (!ret) return Py_None; + + result = (py_rendering_options *)type->tp_alloc(type, 0); + + _executable = py_executable_get_glib_instance(executable); + + g_object_ref(G_OBJECT(_executable)); + result->glib = g_rendering_options_new(_executable); + + return (PyObject *)result; + +} + + +/****************************************************************************** +* * +* Paramètres : roptions = instance Python dont la référence est à donner. * +* * +* Description : Fournit l'instance GLib d'une instance Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +GRenderingOptions *py_rendering_options_get_glib_instance(PyObject *roptions) +{ + return ((py_rendering_options *)roptions)->glib; + +} + + + + +/****************************************************************************** +* * +* Paramètres : self = classe présentant des options de représentation. * +* data = adresse non utilisée ici. * +* * +* Description : Indique si les adresses des instructions sont à afficher. * +* * +* Retour : Valeur booléenne indiquant le statut d'une option. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_rendering_options_get_show_address(PyObject *self, void *data) +{ + + printf(" -->> get address\n"); + + return Py_BuildValue("i", true); + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe présentant des options de représentation. * +* value = nouvelle valeur affectée. * +* data = adresse non utilisée ici. * +* * +* Description : Affiche (ou non) les adresses des instructions. * +* * +* Retour : Bilan de la mise à jour. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_rendering_options_set_show_address(PyObject *self, PyObject *value, void *data) +{ + printf(" -->> set address\n"); + + return 0; + +} + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : self = classe assurant le lien avec l'éditeur de messages. * +* args = arguments fournis à l'appel. * +* * +* Description : Affiche (ou non) le code des instructions. * +* * +* Retour : Rien en équivalent Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_rendering_options_show_code(PyObject *self, PyObject *args) +{ + int state; /* Nouveau statut d'affichage */ + + if (!PyArg_ParseTuple(args, "i", &state)) + return NULL; + + + + + printf("show code :: %d\n", state); + + return Py_None; + +} + + + + + + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'analysis.roptions' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_analysis_roptions_to_python_module(PyObject *module) +{ + int ret; /* Bilan d'un appel */ + + static PyMethodDef py_rendering_options_methods[] = { + { + "show_code", (PyCFunction)py_rendering_options_show_code, + METH_VARARGS, + "Define if the binary code has to be processed or not." + }, + { NULL } + }; + + static PyGetSetDef py_rendering_options_getset[] = { + { + "show_address", + (getter)py_rendering_options_get_show_address, + (setter)py_rendering_options_set_show_address, + "Define or retrieve if the instruction address need to be shown." + }, + { NULL } + }; + + static PyTypeObject py_rendering_options_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pyoida.analysis.RenderingOptions", + .tp_basicsize = sizeof(py_rendering_options), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyOIDA rendering options", + + .tp_methods = py_rendering_options_methods, + .tp_getset = py_rendering_options_getset, + .tp_new = (newfunc)py_rendering_options_new + + }; + + if (PyType_Ready(&py_rendering_options_type) < 0) + return false; + + Py_INCREF(&py_rendering_options_type); + ret = PyModule_AddObject(module, "RenderingOptions", (PyObject *)&py_rendering_options_type); + + return (ret == 0); + +} diff --git a/plugins/pyoida/analysis/roptions.h b/plugins/pyoida/analysis/roptions.h new file mode 100644 index 0000000..0897211 --- /dev/null +++ b/plugins/pyoida/analysis/roptions.h @@ -0,0 +1,45 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * roptions.h - prototypes pour l'équivalent Python du fichier "analysis/roptions.h" + * + * Copyright (C) 2010 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_ANALYSIS_ROPTIONS_H +#define _PLUGINS_PYOIDA_ANALYSIS_ROPTIONS_H + + +#include +#include + + +#include + + + +/* Fournit l'instance GLib d'une instance Python. */ +GRenderingOptions *py_rendering_options_get_glib_instance(PyObject *); + +/* Ajoute l'objet 'analysis.roptions' au module Python. */ +bool add_analysis_roptions_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_ANALYSIS_ROPTIONS_H */ diff --git a/plugins/pyoida/arch/Makefile.am b/plugins/pyoida/arch/Makefile.am new file mode 100644 index 0000000..d0113ec --- /dev/null +++ b/plugins/pyoida/arch/Makefile.am @@ -0,0 +1,17 @@ + +noinst_LTLIBRARIES = libpyoidaarch.la + +libpyoidaarch_la_SOURCES = \ + archbase.h archbase.c \ + module.h module.c \ + processor.h processor.c + + +libpyoidaarch_la_LDFLAGS = + + +INCLUDES = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) -I../../../src + +AM_CPPFLAGS = + +AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) diff --git a/plugins/pyoida/arch/archbase.c b/plugins/pyoida/arch/archbase.c new file mode 100644 index 0000000..b73e769 --- /dev/null +++ b/plugins/pyoida/arch/archbase.c @@ -0,0 +1,121 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * archbase.c - équivalent Python du fichier "arch/archbase.h" + * + * Copyright (C) 2010 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 "archbase.h" + + + + + +/* Classe 'arch.vmpa' pour Python */ +typedef PyLongObject py_vmpa; + + + +/* Fournit le type d'objet 'arch.vmpa' pour Python. */ +static PyTypeObject *get_arch_vmpa_type(void); + + + + + + +/****************************************************************************** +* * +* Paramètres : value = adresse à convertir en objet Python. * +* * +* Description : Crée un nouvel objet Python de type 'py_vmpa'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *py_vmpa_new_from_existing(vmpa_t value) +{ + return PyLong_FromLongLong(value); + +} + + + + + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit le type d'objet 'arch.vmpa' pour Python. * +* * +* Retour : Adresse du type vivant à manipuler. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyTypeObject *get_arch_vmpa_type(void) +{ + return &PyLong_Type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'arch.vmpa' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_arch_vmpa_to_python_module(PyObject *module) +{ + PyTypeObject *py_vmpa_type; /* Type défini pour Python */ + int ret; /* Bilan d'un appel */ + + py_vmpa_type = get_arch_vmpa_type(); + + if (PyType_Ready(py_vmpa_type) < 0) + return false; + + Py_INCREF(py_vmpa_type); + ret = PyModule_AddObject(module, "Vmpa", (PyObject *)py_vmpa_type); + + return (ret == 0); + +} diff --git a/plugins/pyoida/arch/archbase.h b/plugins/pyoida/arch/archbase.h new file mode 100644 index 0000000..ca621aa --- /dev/null +++ b/plugins/pyoida/arch/archbase.h @@ -0,0 +1,44 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * archbase.h - prototypes pour l'équivalent Python du fichier "arch/archbase.h" + * + * Copyright (C) 2010 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_ARCH_ARCHBASE_H +#define _PLUGINS_PYOIDA_ARCH_ARCHBASE_H + + +#include +#include + +#include + + + +/* Crée un nouvel objet Python de type 'py_vmpa'. */ +PyObject *py_vmpa_new_from_existing(vmpa_t); + +/* Ajoute l'objet 'arch.vmpa' au module Python. */ +bool add_arch_vmpa_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_ARCH_ARCHBASE_H */ diff --git a/plugins/pyoida/arch/module.c b/plugins/pyoida/arch/module.c new file mode 100644 index 0000000..59b0c2c --- /dev/null +++ b/plugins/pyoida/arch/module.c @@ -0,0 +1,72 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * module.c - intégration du répertoire arch en tant que module + * + * Copyright (C) 2010 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 "module.h" + + +#include "archbase.h" +#include "processor.h" + + + + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute le module 'arch' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_arch_module_to_python_module(PyObject *super) +{ + bool result; + PyObject *module; + int ret; /* Bilan d'un appel */ + + static PyMethodDef py_arch_methods[] = { + { NULL } + }; + + module = Py_InitModule("pyoida.arch", py_arch_methods); + if (module == NULL) return false; + + + Py_INCREF(module); + ret = PyModule_AddObject(super, "pyoida.arch", module); + + if (ret != 0) /* ... */; + + result = add_arch_vmpa_to_python_module(module); + result = add_arch_processor_to_python_module(module); + + + return true; + +} diff --git a/plugins/pyoida/arch/module.h b/plugins/pyoida/arch/module.h new file mode 100644 index 0000000..4ae3447 --- /dev/null +++ b/plugins/pyoida/arch/module.h @@ -0,0 +1,39 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * module.h - prototypes pour l'intégration du répertoire arch en tant que module + * + * Copyright (C) 2010 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_ARCH_MODULE_H +#define _PLUGINS_PYOIDA_ARCH_MODULE_H + + +#include +#include + + + +/* Ajoute le module 'arch' au module Python. */ +bool add_arch_module_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_ARCH_MODULE_H */ diff --git a/plugins/pyoida/arch/processor.c b/plugins/pyoida/arch/processor.c new file mode 100644 index 0000000..0a3fe95 --- /dev/null +++ b/plugins/pyoida/arch/processor.c @@ -0,0 +1,314 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * processor.h - prototypes pour l'équivalent Python du fichier "arch/processor.h" + * + * Copyright (C) 2010 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 "processor.h" + + +#include "../../../src/arch/processor.h" + + + +/* ------------------------- TYPAGE DES ENUMERATIONS PYTHON ------------------------- */ + + +/* Définit les constantes pour les types de processeur. */ +bool py_arch_processor_type_define_constants(PyObject *); + +/* Ajoute l'objet 'arch.processor.ArchProcessorType' au module. */ +bool add_arch_processor_type_to_python_module(PyObject *); + + + +/* ------------------------- PARTIE STATIQUE DE PROCESSEURS ------------------------- */ + + + + + + + +/* Classe 'analysis.roptions' pour Python */ +typedef struct _py_processor +{ + PyObject_HEAD + +} py_processor; + + + + + + + + + + +/* ---------------------------------------------------------------------------------- */ +/* TYPAGE DES ENUMERATIONS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : dict = dictionnaire à compléter. * +* * +* Description : Définit les constantes pour les types de processeur. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool py_arch_processor_type_define_constants(PyObject *dict) +{ + int ret; /* Bilan d'un ajout */ + + ret = PyDict_SetItemString(dict, "APT_JVM", PyInt_FromLong(APT_JVM)); + if (ret == -1) return false; + + ret = PyDict_SetItemString(dict, "APT_MIPS", PyInt_FromLong(APT_MIPS)); + if (ret == -1) return false; + + ret = PyDict_SetItemString(dict, "APT_386", PyInt_FromLong(APT_386)); + if (ret == -1) return false; + + ret = PyDict_SetItemString(dict, "APT_COUNT", PyInt_FromLong(APT_COUNT)); + if (ret == -1) return false; + + return true; + +} +PyObject *__test; + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'arch.processor.ArchProcessorType' au module. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_arch_processor_type_to_python_module(PyObject *module) +{ + int ret; /* Bilan d'un appel */ + + static PyTypeObject py_arch_processor_type_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pyoida.arch.processor.ArchProcessorType", + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyOIDA version of the ArchProcessorType enumeration", + + }; + + if (PyType_Ready(&py_arch_processor_type_type) < 0) + return false; + + py_arch_processor_type_define_constants(py_arch_processor_type_type.tp_dict); + + Py_INCREF(&py_arch_processor_type_type); + ret = PyModule_AddObject(module, "ArchProcessorType", (PyObject *)&py_arch_processor_type_type); + + __test = &py_arch_processor_type_type; + + return (ret == 0); + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* PARTIE STATIQUE DE PROCESSEURS */ +/* ---------------------------------------------------------------------------------- */ + + + + + + + + + + + + + + + + + + + + + + + + + +/* ---------------------------------------------------------------------------------- */ +/* PARTIE GRAPHIQUE DES [DE]CHARGEMENTS */ +/* ---------------------------------------------------------------------------------- */ + + + + + + + + + +/* Crée un nouvel objet Python de type 'py_processor'. */ +static PyObject *py_processor_new(PyTypeObject *, PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* Paramètres : type = type de l'objet à instancier. * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Crée un nouvel objet Python de type 'py_processor'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_processor_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + py_processor *result; /* Instance à retourner */ + + result = (py_processor *)type->tp_alloc(type, 0); + + return (PyObject *)result; + +} + + + + + + + + + + +static PyObject *util_FromImport(const char *name, const char *from_item) +{ + PyObject *from_list; + PyObject *module; + PyObject *item; + + /* Make the from list. */ + from_list = PyList_New(1); + PyList_SetItem(from_list, 0, PyString_FromString(from_item)); + + /* Attempt the import, with const correctness removed. */ + module = PyImport_ImportModuleEx((char *)name, NULL, NULL, from_list); + Py_DECREF(from_list); + if (!module) + { + return NULL; + } + + /* Get the from_item from the module. */ + item = PyObject_GetAttrString(module, (char *)from_item); + Py_DECREF(module); + + return item; +} + + + + + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'analysis.roptions' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_arch_processor_to_python_module(PyObject *module) +{ + bool result; /* Bilan à retourner */ + int ret; /* Bilan d'un appel */ + + static PyMethodDef py_processor_methods[] = { + { NULL } + }; + + static PyGetSetDef py_processor_getset[] = { + { NULL } + }; + + static PyTypeObject py_processor_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pyoida.arch.Processor", + .tp_basicsize = sizeof(py_processor), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyOIDA processor for a given architecture", + + .tp_methods = py_processor_methods, + .tp_getset = py_processor_getset, + .tp_new = (newfunc)py_processor_new + + }; + + if (PyType_Ready(&py_processor_type) < 0) + return false; + + //printf("ret import = %p\n", PyImport_ImportModule("pyoida.arch.processor.ArchProcessorType")); + + + + Py_INCREF(&py_processor_type); + ret = PyModule_AddObject(module, "Processor", (PyObject *)&py_processor_type); + + result = add_arch_processor_type_to_python_module(module); + + + return (ret == 0); + +} diff --git a/plugins/pyoida/arch/processor.h b/plugins/pyoida/arch/processor.h new file mode 100644 index 0000000..032d82d --- /dev/null +++ b/plugins/pyoida/arch/processor.h @@ -0,0 +1,39 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * processor.h - prototypes pour l'équivalent Python du fichier "arch/processor.h" + * + * Copyright (C) 2010 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_ARCH_PROCESSOR_H +#define _PLUGINS_PYOIDA_ARCH_PROCESSOR_H + + +#include +#include + + + +/* Ajoute l'objet 'arch.processor' au module Python. */ +bool add_arch_processor_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_ARCH_PROCESSOR_H */ diff --git a/plugins/pyoida/format/Makefile.am b/plugins/pyoida/format/Makefile.am new file mode 100644 index 0000000..74d3e3a --- /dev/null +++ b/plugins/pyoida/format/Makefile.am @@ -0,0 +1,16 @@ + +noinst_LTLIBRARIES = libpyoidaformat.la + +libpyoidaformat_la_SOURCES = \ + executable.h executable.c \ + module.h module.c + + +libpyoidaformat_la_LDFLAGS = + + +INCLUDES = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) -I../../../src + +AM_CPPFLAGS = + +AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) diff --git a/plugins/pyoida/format/executable.c b/plugins/pyoida/format/executable.c new file mode 100644 index 0000000..2f10f71 --- /dev/null +++ b/plugins/pyoida/format/executable.c @@ -0,0 +1,223 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * executable.c - équivalent Python du fichier "format/executable.h" + * + * Copyright (C) 2010 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 "executable.h" + + + + + + + +/* Classe 'format.executable' pour Python */ +typedef struct _py_executable +{ + PyObject_HEAD + + GExeFormat *glib; /* Format d'exécutable GLib */ + +} py_executable; + + + + +/* Crée un nouvel objet Python de type 'py_executable'. */ +static PyObject *py_executable_new(PyTypeObject *, PyObject *, PyObject *); + + + + + +/* Fournit le type d'objet 'format.executable' pour Python. */ +static PyTypeObject *get_format_executable_type(void); + + + + +/****************************************************************************** +* * +* Paramètres : type = type de l'objet à instancier. * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Crée un nouvel objet Python de type 'py_executable'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_executable_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + py_executable *result; /* Instance à retourner */ + + result = (py_executable *)type->tp_alloc(type, 0); + + return (PyObject *)result; + +} + + +/****************************************************************************** +* * +* Paramètres : executable = objet GLib existant à transposer. * +* * +* Description : Convertit une instance GLib en objet Python 'py_executable'. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *py_executable_convert(GExeFormat *executable) +{ + py_executable *result; /* Instance à retourner */ + PyTypeObject *type; /* Type Python à instancier */ + + result = (py_executable *)g_object_get_data(G_OBJECT(executable), "python_object"); + + if (result == NULL) + { + type = get_format_executable_type(); + + result = (py_executable *)type->tp_alloc(type, 0); + + result->glib = executable; + g_object_ref(executable); + + g_object_set_data(G_OBJECT(executable), "python_object", result); + + } + else Py_INCREF((PyObject *)result); + + return (PyObject *)result; + +} + + +/****************************************************************************** +* * +* Paramètres : executable = instance Python dont la référence est à donner. * +* * +* Description : Fournit l'instance GLib d'une instance Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +GExeFormat *py_executable_get_glib_instance(PyObject *executable) +{ + return ((py_executable *)executable)->glib; + +} + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit le type d'objet 'format.executable' pour Python. * +* * +* Retour : Adresse du type vivant à manipuler. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyTypeObject *get_format_executable_type(void) +{ + static PyTypeObject *result = NULL; /* Type pour objet à retourner */ + + static PyMethodDef py_executable_methods[] = { + { NULL } + }; + + static PyGetSetDef py_executable_getset[] = { + { NULL } + }; + + static PyTypeObject py_executable_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pyoida.format.Executable", + .tp_basicsize = sizeof(py_executable), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyOIDA loaded executable to analyse", + + .tp_methods = py_executable_methods, + .tp_getset = py_executable_getset, + .tp_new = (newfunc)py_executable_new + + }; + + if (result == NULL) result = &py_executable_type; + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'format.executable' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_format_executable_to_python_module(PyObject *module) +{ + PyTypeObject *py_executable_type; /* Type défini pour Python */ + int ret; /* Bilan d'un appel */ + + py_executable_type = get_format_executable_type(); + + if (PyType_Ready(py_executable_type) < 0) + return false; + + Py_INCREF(py_executable_type); + ret = PyModule_AddObject(module, "Executable", (PyObject *)py_executable_type); + + return (ret == 0); + +} diff --git a/plugins/pyoida/format/executable.h b/plugins/pyoida/format/executable.h new file mode 100644 index 0000000..577d1f9 --- /dev/null +++ b/plugins/pyoida/format/executable.h @@ -0,0 +1,47 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * executable.h - prototypes pour l'équivalent Python du fichier "format/executable.h" + * + * Copyright (C) 2010 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_FORMAT_EXECUTABLE_H +#define _PLUGINS_PYOIDA_FORMAT_EXECUTABLE_H + + +#include +#include + +#include + + + +/* Convertit une instance GLib en objet Python 'py_executable'. */ +PyObject *py_executable_convert(GExeFormat *); + +/* Fournit l'instance GLib d'une instance Python. */ +GExeFormat *py_executable_get_glib_instance(PyObject *); + +/* Ajoute l'objet 'format.executable' au module Python. */ +bool add_format_executable_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_FORMAT_EXECUTABLE_H */ diff --git a/plugins/pyoida/format/module.c b/plugins/pyoida/format/module.c new file mode 100644 index 0000000..1f15d50 --- /dev/null +++ b/plugins/pyoida/format/module.c @@ -0,0 +1,68 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * module.c - intégration du répertoire format en tant que module + * + * Copyright (C) 2010 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 "module.h" + + +#include "executable.h" + + + + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute le module 'format' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_format_module_to_python_module(PyObject *super) +{ + bool result; + PyObject *module; + int ret; /* Bilan d'un appel */ + + static PyMethodDef py_format_methods[] = { + { NULL } + }; + + module = Py_InitModule("pyoida.format", py_format_methods); + if (module == NULL) return false; + + Py_INCREF(module); + ret = PyModule_AddObject(super, "pyoida.format", module); + + result = (ret != 0); + + result &= add_format_executable_to_python_module(module); + + return result; + +} diff --git a/plugins/pyoida/format/module.h b/plugins/pyoida/format/module.h new file mode 100644 index 0000000..4aa95cf --- /dev/null +++ b/plugins/pyoida/format/module.h @@ -0,0 +1,39 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * module.h - prototypes pour l'intégration du répertoire format en tant que module + * + * Copyright (C) 2010 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_FORMAT_MODULE_H +#define _PLUGINS_PYOIDA_FORMAT_MODULE_H + + +#include +#include + + + +/* Ajoute le module 'format' au module Python. */ +bool add_format_module_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_FORMAT_MODULE_H */ diff --git a/plugins/pyoida/plugin.c b/plugins/pyoida/plugin.c new file mode 100644 index 0000000..23934a9 --- /dev/null +++ b/plugins/pyoida/plugin.c @@ -0,0 +1,555 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * plugin.c - interactions avec un greffon Python + * + * Copyright (C) 2010 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 "plugin.h" + + +#include "../../src/analysis/binary.h" +#include "../../src/plugins/plugin-int.h" + + +#include "analysis/binary.h" + + + + + + + +/* --------------------- INTERFACE INTERNE POUR GREFFONS PYTHON --------------------- */ + + +/* Ligne de représentation de code binaire (instance) */ +struct _GPythonPlugin +{ + GPluginModule parent; /* Instance parente */ + + PyObject *module; /* Script Python chargé */ + +}; + + +/* Ligne de représentation de code binaire (classe) */ +struct _GPythonPluginClass +{ + GPluginModuleClass parent; /* Classe parente */ + +}; + + +/* Initialise la classe des greffons Python. */ +static void g_python_plugin_class_init(GPythonPluginClass *); + +/* Initialise l'instance d'un greffon Python. */ +static void g_python_plugin_init(GPythonPlugin *); + +/* Exécute une action définie sur un binaire chargé. */ +static bool g_python_plugin_execute(GPythonPlugin *, GOpenidaBinary *, PluginAction); + + +/* ------------------------- MODULE PYTHON POUR LES SCRIPTS ------------------------- */ + + +/* Classe plugin pour Python */ +typedef struct _pyoida_plugin +{ + PyObject_HEAD + +} pyoida_plugin; + + + + + + +/* Crée un nouveau greffon Python abstrait. */ +static PyObject *pyoida_plugin_new(PyTypeObject *, PyObject *, PyObject *); + +/* Exécute une action valide pour le greffon Python. */ +static PyObject *pyoida_plugin_run(PyObject *, PyObject *); + + + +/* Définit les constantes pour les greffons en Python. */ +static bool pyoida_plugin_define_constants(PyObject *); + + + + + + + + + + + + + +int +main2(const char *filename, const char *method) +{ + PyObject *pName, *pModule, *pDict, *pFunc; + PyObject *pArgs, *pValue; + int i; + return 0; + pName = PyString_FromString/*PyUnicode_FromString*/(filename); + /* Error checking of pName left out */ + + pModule = PyImport_Import(pName); + Py_DECREF(pName); + + if (pModule != NULL) { + pFunc = PyObject_GetAttrString(pModule, method); + /* pFunc is a new reference */ + + if (pFunc && PyCallable_Check(pFunc)) { + pArgs = PyTuple_New(0/*argc - 3*/); +#if 0 + for (i = 0; i < argc - 3; ++i) { + pValue = PyLong_FromLong(atoi(argv[i + 3])); + if (!pValue) { + Py_DECREF(pArgs); + Py_DECREF(pModule); + fprintf(stderr, "Cannot convert argument\n"); + return 1; + } + /* pValue reference stolen here: */ + PyTuple_SetItem(pArgs, i, pValue); + } +#endif + pValue = PyObject_CallObject(pFunc, pArgs); + Py_DECREF(pArgs); + if (pValue != NULL) { + printf("Result of call: %ld\n", PyLong_AsLong(pValue)); + Py_DECREF(pValue); + } + else { + Py_DECREF(pFunc); + Py_DECREF(pModule); + PyErr_Print(); + fprintf(stderr,"Call failed\n"); + return 1; + } + } + else { + if (PyErr_Occurred()) + PyErr_Print(); + fprintf(stderr, "Cannot find function \"%s\"\n", method); + } + Py_XDECREF(pFunc); + Py_DECREF(pModule); + } + else { + PyErr_Print(); + fprintf(stderr, "Failed to load \"%s\"\n", filename); + return 1; + } + return 0; +} + + + + + + + + + + + + + + + +/* ---------------------------------------------------------------------------------- */ +/* INTERFACE INTERNE POUR GREFFONS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/* Indique le type définit par la GLib pour le greffon Python. */ +G_DEFINE_TYPE(GPythonPlugin, g_python_plugin, G_TYPE_PLUGIN_MODULE); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des greffons Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_python_plugin_class_init(GPythonPluginClass *klass) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : plugin = instance à initialiser. * +* * +* Description : Initialise l'instance d'un greffon Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_python_plugin_init(GPythonPlugin *plugin) +{ + GPluginModule *plugin_parent; /* Instance parente */ + + plugin_parent = G_PLUGIN_MODULE(plugin); + + plugin_parent->exec_on_bin = (execute_action_on_binary_fc)g_python_plugin_execute; + +} + + + + +PyObject *run_python_method(PyObject *module, const char *method, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + PyObject *func; /* Fonction visée */ + + result = NULL; + + func = PyObject_GetAttrString(module, method); + if (func == NULL) return NULL; + + if (PyCallable_Check(func)) + { + result = PyObject_CallObject(func, args); + if (result == NULL) PyErr_Print(); + } + else if (PyErr_Occurred()) PyErr_Print(); + + Py_DECREF(func); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : filename = chemin d'accès au code Python à charger. * +* * +* Description : Crée un greffon à partir de code Python. * +* * +* Retour : Adresse de la structure mise en place ou NULL si erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GPluginModule *g_python_plugin_new(const char *filename) +{ + GPythonPlugin *result; /* Structure à retourner */ + PyObject *name; /* Chemin d'accès pour Python */ + PyObject *module; /* Script Python chargé */ + +#if PY_VERSION_HEX >= 0x03000000 + name = PyUnicode_FromString(filename); +#else + name = PyString_FromString(filename); +#endif + name = PyString_FromString/*PyUnicode_FromString*/(filename); + if (name == NULL) goto gppn_bad_exit; + + module = PyImport_Import(name); + Py_DECREF(name); + + if (module == NULL) + { + PyErr_Print(); + goto gppn_bad_exit; + } + + + + //Py_DECREF(module); + + + + result = g_object_new(G_TYPE_PYTHON_PLUGIN, NULL); + + G_PLUGIN_MODULE(result)->action = PGA_CODE_PROCESS; + + result->module = module; + + return G_PLUGIN_MODULE(result); + + gppn_bad_exit: + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : plugin = greffon de prise en charge à utiliser. * +* binary = représentation binaire à traiter. * +* action = action attendue. * +* * +* Description : Exécute une action définie sur un binaire chargé. * +* * +* Retour : true si une action a été menée, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_python_plugin_execute(GPythonPlugin *plugin, GOpenidaBinary *binary, PluginAction action) +{ + PyObject *args; /* Arguments pour l'appel */ + PyObject *arg; /* Un des arguments de l'appel */ + PyObject *value; /* Valeur obtenue */ + + + + printf("I am running !!"); + + args = PyTuple_New(1); + + + arg = Py_BuildValue("O&", py_binary_new_from_existing, binary); + PyTuple_SetItem(args, 0, arg); + + + value = run_python_method(plugin->module, "get_instance", args); + + if (value != NULL) + { + printf("Result of call: %ld\n", PyLong_AsLong(value)); + Py_DECREF(value); + } + + + Py_DECREF(args); + + + + + + +} + + + + + + +/* ---------------------------------------------------------------------------------- */ +/* MODULE PYTHON POUR LES SCRIPTS */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : type = type de l'objet à instancier. * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Crée un nouveau greffon Python abstrait. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *pyoida_plugin_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + pyoida_plugin *result; /* Instance à retourner */ + + result = (pyoida_plugin *)type->tp_alloc(type, 0); + + return (PyObject *)result; + +} + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : dict = dictionnaire à compléter. * +* * +* Description : Définit les constantes pour les greffons en Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool pyoida_plugin_define_constants(PyObject *dict) +{ + int ret; /* Bilan d'un ajout */ + + ret = PyDict_SetItemString(dict, "PGA_DISASSEMBLE", PyInt_FromLong(PGA_DISASSEMBLE)); + if (ret == -1) return false; + + ret = PyDict_SetItemString(dict, "PGA_CODE_PROCESS", PyInt_FromLong(PGA_CODE_PROCESS)); + if (ret == -1) return false; + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe assurant le lien avec l'éditeur de messages. * +* args = arguments fournis à l'appel. * +* * +* Description : Exécute une action valide pour le greffon Python. * +* * +* Retour : Rien en équivalent Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *pyoida_plugin_run(PyObject *self, PyObject *args) +{ + return Py_None; + +} + + + + + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Ajoute l'objet 'plugin' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_plugin_to_python_module(PyObject *module) +{ + int ret; /* Bilan d'un appel */ + + +static PyMethodDef pyoida_plugin_methods[] = { + { "run", (PyCFunction)pyoida_plugin_run, METH_VARARGS, + "Run the plugin for a specific action." + }, + NULL +}; + + +static PyTypeObject pyoida_plugin_type = { + + PyObject_HEAD_INIT(NULL) + +#if PY_VERSION_HEX < 0x03000000 + 0, /*ob_size*/ +#endif + + "plugin.Plugin", /* tp_name */ + sizeof(pyoida_plugin), /* 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*/ + "PyOIDA Plugin objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + pyoida_plugin_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 */ + 0, /* tp_init */ + 0, /* tp_alloc */ + (newfunc)pyoida_plugin_new, /* tp_new */ +}; + + + + + printf("Adding pyoida pg type...\n"); + + if (PyType_Ready(&pyoida_plugin_type) < 0) + return false; + + printf("Adding pyoida pg type\n"); + + + if (!pyoida_plugin_define_constants(pyoida_plugin_type.tp_dict)) + return false; + + + + + Py_INCREF(&pyoida_plugin_type); + PyModule_AddObject(module, "Plugin", (PyObject *)&pyoida_plugin_type); + + + return true; /* FIXME */ + + +} diff --git a/plugins/pyoida/plugin.h b/plugins/pyoida/plugin.h new file mode 100644 index 0000000..f5d4ab4 --- /dev/null +++ b/plugins/pyoida/plugin.h @@ -0,0 +1,88 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * plugin.h - prototypes pour les interactions avec un greffon Python + * + * Copyright (C) 2010 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_PLUGIN_H +#define _PLUGINS_PYOIDA_PLUGIN_H + + +#include +#include +#include + + +#include "../../src/plugins/plugin.h" + + + +/* --------------------- INTERFACE INTERNE POUR GREFFONS PYTHON --------------------- */ + + + + + +#define G_TYPE_PYTHON_PLUGIN (g_python_plugin_get_type()) +#define G_PYTHON_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_PYTHON_PLUGIN, GPythonPlugin)) +#define G_IS_PYTHON_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_PYTHON_PLUGIN)) +#define G_PYTHON_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_PYTHON_PLUGIN, GPythonPluginClass)) +#define G_IS_PYTHON_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_PYTHON_PLUGIN)) +#define G_PYTHON_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_PYTHON_PLUGIN, GPythonPluginClass)) + + +/* Ligne de représentation de code binaire (instance) */ +typedef struct _GPythonPlugin GPythonPlugin; + +/* Ligne de représentation de code binaire (classe) */ +typedef struct _GPythonPluginClass GPythonPluginClass; + + +/* Indique le type définit par la GLib pour le greffon Python. */ +GType g_python_plugin_get_type(void); + +/* Crée un greffon à partir de code Python. */ +GPluginModule *g_python_plugin_new(const char *); + + + + + + + + +int +main2(const char *filename, const char *method); + + + + + + +/* ------------------------- MODULE PYTHON POUR LES SCRIPTS ------------------------- */ + + +/* Ajoute l'objet 'plugin' au module Python. */ +bool add_plugin_to_python_module(PyObject *); + + + +#endif /* _PLUGINS_PYOIDA_PLUGIN_H */ diff --git a/plugins/pyoida/pyoida.c b/plugins/pyoida/pyoida.c index 860da5b..82f9980 100644 --- a/plugins/pyoida/pyoida.c +++ b/plugins/pyoida/pyoida.c @@ -24,14 +24,26 @@ #include "pyoida.h" - #include +#include "analysis/module.h" +#include "arch/module.h" +#include "format/module.h" + +/* #include "analysis/py_binary.h" #include "analysis/py_line.h" #include "analysis/py_line_code.h" +#include "analysis/roptions.h" +*/ +#include "plugin.h" #include "py_log.h" +#include "../../src/common/environment.h" + + +#include "plugin.h" + static GObject *_ref = NULL; @@ -59,13 +71,6 @@ static PyMethodDef SpamMethods[] = { - - - - - - - /****************************************************************************** * * * Paramètres : ref = espace de référencement global. * @@ -80,11 +85,44 @@ static PyMethodDef SpamMethods[] = { bool init_plugin(GObject *ref) { + GPluginModule *plugin; + int ret; printf("Init pyoida\n"); _ref = ref; + add_to_env_var("PYTHONPATH", "/home/ocb/prog/openida/plugins/python", ";"); + + + Py_Initialize(); + + initpyoida(); + + + + + plugin = g_python_plugin_new("lnxsyscalls/lnxsyscalls"); + add_plugin_to_main_list(plugin); + + + +#if 0 + +#if 0 + //main2("/home/ocb/prog/openida/plugins/pyoida/lnxsyscalls/lnxsyscalls.py", "get_instance"); + main2("lnxsyscalls", "get_instance"); +#else + //main2("/home/ocb/prog/openida/plugins/pyoida/lnxsyscalls/lnxsyscalls.py", "get_instance"); + main2("lnxsyscalls/lnxsyscalls", "get_instance"); +#endif + +#endif + + //Py_Finalize(); + + //exit(-1); + return true; } @@ -114,20 +152,27 @@ PyInit_pyoida(void) /* Python 2.x code */ +typedef struct { + PyObject_HEAD + PyObject *md_dict; +} PyModuleObject; + PyMODINIT_FUNC initpyoida(void) { PyObject *module; - printf("Passage 2\n"); module = Py_InitModule("pyoida", SpamMethods); - add_binary_to_python_module(module); - add_line_to_python_module(module); - add_pycodeline_to_python_module(module); + //add_analysis_roptions_to_python_module(module); + add_analysis_module_to_python_module(module); + add_arch_module_to_python_module(module); + add_format_module_to_python_module(module); + add_log_to_python_module(module); + add_plugin_to_python_module(module); } diff --git a/plugins/python/lnxsyscalls/lnxsyscalls.py b/plugins/python/lnxsyscalls/lnxsyscalls.py new file mode 100644 index 0000000..92d5763 --- /dev/null +++ b/plugins/python/lnxsyscalls/lnxsyscalls.py @@ -0,0 +1,517 @@ +#!/usr/bin/python + +from pyoida import Plugin +from pyoida.analysis import RenderingOptions +from pyoida.arch import ArchProcessorType + + +class SysCallDB(): + """xxx""" + + def __init__(self): + + self.__syscalls = [ + "__NR_restart_syscall", + "__NR_exit", + "__NR_fork", + "__NR_read", + "__NR_write", + "__NR_open", + "__NR_close", + "__NR_waitpid", + "__NR_creat", + "__NR_link", + "__NR_unlink", + "__NR_execve", + "__NR_chdir", + "__NR_time", + "__NR_mknod", + "__NR_chmod", + "__NR_lchown", + "__NR_break", + "__NR_oldstat", + "__NR_lseek", + "__NR_getpid", + "__NR_mount", + "__NR_umount", + "__NR_setuid", + "__NR_getuid", + "__NR_stime", + "__NR_ptrace", + "__NR_alarm", + "__NR_oldfstat", + "__NR_pause", + "__NR_utime", + "__NR_stty", + "__NR_gtty", + "__NR_access", + "__NR_nice", + "__NR_ftime", + "__NR_sync", + "__NR_kill", + "__NR_rename", + "__NR_mkdir", + "__NR_rmdir", + "__NR_dup", + "__NR_pipe", + "__NR_times", + "__NR_prof", + "__NR_brk", + "__NR_setgid", + "__NR_getgid", + "__NR_signal", + "__NR_geteuid", + "__NR_getegid", + "__NR_acct", + "__NR_umount2", + "__NR_lock", + "__NR_ioctl", + "__NR_fcntl", + "__NR_mpx", + "__NR_setpgid", + "__NR_ulimit", + "__NR_oldolduname", + "__NR_umask", + "__NR_chroot", + "__NR_ustat", + "__NR_dup2", + "__NR_getppid", + "__NR_getpgrp", + "__NR_setsid", + "__NR_sigaction", + "__NR_sgetmask", + "__NR_ssetmask", + "__NR_setreuid", + "__NR_setregid", + "__NR_sigsuspend", + "__NR_sigpending", + "__NR_sethostname", + "__NR_setrlimit", + "__NR_getrlimit", + "__NR_getrusage", + "__NR_gettimeofday", + "__NR_settimeofday", + "__NR_getgroups", + "__NR_setgroups", + "__NR_select", + "__NR_symlink", + "__NR_oldlstat", + "__NR_readlink", + "__NR_uselib", + "__NR_swapon", + "__NR_reboot", + "__NR_readdir", + "__NR_mmap", + "__NR_munmap", + "__NR_truncate", + "__NR_ftruncate", + "__NR_fchmod", + "__NR_fchown", + "__NR_getpriority", + "__NR_setpriority", + "__NR_profil", + "__NR_statfs", + "__NR_fstatfs", + "__NR_ioperm", + "__NR_socketcall", + "__NR_syslog", + "__NR_setitimer", + "__NR_getitimer", + "__NR_stat", + "__NR_lstat", + "__NR_fstat", + "__NR_olduname", + "__NR_iopl", + "__NR_vhangup", + "__NR_idle", + "__NR_vm86old", + "__NR_wait4", + "__NR_swapoff", + "__NR_sysinfo", + "__NR_ipc", + "__NR_fsync", + "__NR_sigreturn", + "__NR_clone", + "__NR_setdomainname", + "__NR_uname", + "__NR_modify_ldt", + "__NR_adjtimex", + "__NR_mprotect", + "__NR_sigprocmask", + "__NR_create_module", + "__NR_init_module", + "__NR_delete_module", + "__NR_get_kernel_syms", + "__NR_quotactl", + "__NR_getpgid", + "__NR_fchdir", + "__NR_bdflush", + "__NR_sysfs", + "__NR_personality", + "__NR_afs_syscall", + "__NR_setfsuid", + "__NR_setfsgid", + "__NR__llseek", + "__NR_getdents", + "__NR__newselect", + "__NR_flock", + "__NR_msync", + "__NR_readv", + "__NR_writev", + "__NR_getsid", + "__NR_fdatasync", + "__NR__sysctl", + "__NR_mlock", + "__NR_munlock", + "__NR_mlockall", + "__NR_munlockall", + "__NR_sched_setparam", + "__NR_sched_getparam", + "__NR_sched_setscheduler", + "__NR_sched_getscheduler", + "__NR_sched_yield", + "__NR_sched_get_priority_max", + "__NR_sched_get_priority_min", + "__NR_sched_rr_get_interval", + "__NR_nanosleep", + "__NR_mremap", + "__NR_setresuid", + "__NR_getresuid", + "__NR_vm86", + "__NR_query_module", + "__NR_poll", + "__NR_nfsservctl", + "__NR_setresgid", + "__NR_getresgid", + "__NR_prctl", + "__NR_rt_sigreturn", + "__NR_rt_sigaction", + "__NR_rt_sigprocmask", + "__NR_rt_sigpending", + "__NR_rt_sigtimedwait", + "__NR_rt_sigqueueinfo", + "__NR_rt_sigsuspend", + "__NR_pread64", + "__NR_pwrite64", + "__NR_chown", + "__NR_getcwd", + "__NR_capget", + "__NR_capset", + "__NR_sigaltstack", + "__NR_sendfile", + "__NR_getpmsg", + "__NR_putpmsg", + "__NR_vfork", + "__NR_ugetrlimit", + "__NR_mmap2", + "__NR_truncate64", + "__NR_ftruncate64", + "__NR_stat64", + "__NR_lstat64", + "__NR_fstat64", + "__NR_lchown32", + "__NR_getuid32", + "__NR_getgid32", + "__NR_geteuid32", + "__NR_getegid32", + "__NR_setreuid32", + "__NR_setregid32", + "__NR_getgroups32", + "__NR_setgroups32", + "__NR_fchown32", + "__NR_setresuid32", + "__NR_getresuid32", + "__NR_setresgid32", + "__NR_getresgid32", + "__NR_chown32", + "__NR_setuid32", + "__NR_setgid32", + "__NR_setfsuid32", + "__NR_setfsgid32", + "__NR_pivot_root", + "__NR_mincore", + "__NR_madvise", + "__NR_madvise1", + "__NR_getdents64", + "__NR_fcntl64", + "__NR_unused", + "__NR_gettid", + "__NR_readahead", + "__NR_setxattr", + "__NR_lsetxattr", + "__NR_fsetxattr", + "__NR_getxattr", + "__NR_lgetxattr", + "__NR_fgetxattr", + "__NR_listxattr", + "__NR_llistxattr", + "__NR_flistxattr", + "__NR_removexattr", + "__NR_lremovexattr", + "__NR_fremovexattr", + "__NR_tkill", + "__NR_sendfile64", + "__NR_futex", + "__NR_sched_setaffinity", + "__NR_sched_getaffinity", + "__NR_set_thread_area", + "__NR_get_thread_area", + "__NR_io_setup", + "__NR_io_destroy", + "__NR_io_getevents", + "__NR_io_submit", + "__NR_io_cancel", + "__NR_fadvise64", + "__NR_unused", + "__NR_exit_group", + "__NR_lookup_dcookie", + "__NR_epoll_create", + "__NR_epoll_ctl", + "__NR_epoll_wait", + "__NR_remap_file_pages", + "__NR_set_tid_address", + "__NR_timer_create", + "__NR_timer_settime", + "__NR_timer_gettime", + "__NR_timer_getoverrun", + "__NR_timer_delete", + "__NR_clock_settime", + "__NR_clock_gettime", + "__NR_clock_getres", + "__NR_clock_nanosleep", + "__NR_statfs64", + "__NR_fstatfs64", + "__NR_tgkill", + "__NR_utimes", + "__NR_fadvise64_64", + "__NR_vserver", + "__NR_mbind", + "__NR_get_mempolicy", + "__NR_set_mempolicy", + "__NR_mq_open", + "__NR_mq_unlink", + "__NR_mq_timedsend", + "__NR_mq_timedreceive", + "__NR_mq_notify", + "__NR_mq_getsetattr", + "__NR_kexec_load", + "__NR_waitid", + "__NR_sys_setaltroot", + "__NR_add_key", + "__NR_request_key", + "__NR_keyctl", + "__NR_ioprio_set", + "__NR_ioprio_get", + "__NR_inotify_init", + "__NR_inotify_add_watch", + "__NR_inotify_rm_watch", + "__NR_migrate_pages", + "__NR_openat", + "__NR_mkdirat", + "__NR_mknodat", + "__NR_fchownat", + "__NR_futimesat", + "__NR_fstatat64", + "__NR_unlinkat", + "__NR_renameat", + "__NR_linkat", + "__NR_symlinkat", + "__NR_readlinkat", + "__NR_fchmodat", + "__NR_faccessat", + "__NR_pselect6", + "__NR_ppoll", + "__NR_unshare", + "__NR_set_robust_list", + "__NR_get_robust_list", + "__NR_splice", + "__NR_sync_file_range", + "__NR_tee", + "__NR_vmsplice", + "__NR_move_pages", + "__NR_getcpu", + "__NR_epoll_pwait", + "__NR_utimensat", + "__NR_signalfd", + "__NR_timerfd_create", + "__NR_eventfd", + "__NR_fallocate", + "__NR_timerfd_settime", + "__NR_timerfd_gettime", + "__NR_signalfd4", + "__NR_eventfd2", + "__NR_epoll_create1", + "__NR_dup3", + "__NR_pipe2", + "__NR_inotify_init1", + "__NR_preadv", + "__NR_pwritev", + "__NR_rt_tgsigqueueinfo", + "__NR_perf_event_open" + ] + + self.__args = { + "sys_restart_syscall" : [], + "sys_exit" : ["int error_code"], + "sys_write" : ["unsigned int fd", "const char __user *buf", "size_t count"] + } + + def get_syscall_name(self, index): + """Convert a syscall index into a human name.""" + + return self.__syscalls[index] + + + def get_syscall_args(self, name): + """Provide the function name, the needed registers and the used arguments of a syscall.""" + + name = name.replace("__NR_", "sys_") + + try: + args = self.__args[name] + except: + return None + + targets = {} + + if len(args) >= 4: + + pass + + else: + + registers = ["ebx", "ecx", "edx", "esi", "edi"] + + i = 0 + + for val in args: + targets[registers[i]] = val + i = i + 1 + + return [name, targets] + + + + +class LinuxSysCallsPlugin(Plugin): + """A simple example class""" + + + + def __rendering_options(self, binary): + """Build the options needed to get the ASM code only.""" + + exe = binary.get_format() + + ropts = RenderingOptions(exe) + + ropts.show_code(True) + ropts.show_address = False + + return ropts + + def __is_syscall(self, code): + """Tell weither the ASM line of code is a syscall or not.""" + + result = False + + if code.find("int") != -1: + + list = code.split("\t") + + if len(list) == 2: + result = list == ["int", "0x80"] + + return result + + def __find_used_register(self, ropts, start, target): + """Look for a given register used to supply argument for a syscall.""" + + for found in reversed(start): + + code = found.get_text(ropts) + parts = code.expandtabs(1).replace(",", "").split(" ") + + # Next registers are used by a new interruption + if parts[0] == "int": + continue + + # No operand + if len(parts) == 1: + continue + + # Only simple cases are processed... + if parts[1] == target: + if len(parts) == 2: + return None + else: + return [found, parts[2]] + + return None + + def run(self, binary): + """Resolve all registers / interruptions involved in a syscall.""" + + db = SysCallDB() + + ropts = self.__rendering_options(binary) + + lines = binary.get_lines() + + for line in lines: + code = line.get_text(ropts) + if self.__is_syscall(code): + + # What kind of syscall ? + + line_eax = self.__find_used_register(ropts, line, "eax") + + if line_eax is None: + line.comment = "== UNIX Syscall ==" + continue + + index = int(line_eax[1], 16) + name = db.get_syscall_name(index) + + line_eax[0].comment = name + + # What function / arguments ? + + targets = db.get_syscall_args(name) + + if targets == None: + + line.comment = "== UNIX Syscall ==" + continue + + else: + + line.comment = "== UNIX Syscall to %s() ==" % (targets[0]) + + for reg, arg in targets[1].items(): + + found = self.__find_used_register(ropts, line, reg) + + if found != None: + found[0].comment = arg + + + + + + +def get_instance(binary): + + print " --- toto from Python ---" + + + + a = LinuxSysCallsPlugin() + + a.run(binary) + + return Plugin.PGA_CODE_PROCESS + + + + diff --git a/src/analysis/exporter-int.h b/src/analysis/exporter-int.h index 4219be2..3218bcf 100644 --- a/src/analysis/exporter-int.h +++ b/src/analysis/exporter-int.h @@ -1,6 +1,6 @@ /* OpenIDA - Outil d'analyse de fichiers binaires - * line-int.h - prototypes pour la traduction humaine des lignes de rendus + * exporter-int.h - prototypes pour la traduction humaine des lignes de rendus * * Copyright (C) 2009 Cyrille Bagard * diff --git a/src/analysis/line-int.h b/src/analysis/line-int.h index 9c8e52a..e240815 100644 --- a/src/analysis/line-int.h +++ b/src/analysis/line-int.h @@ -43,6 +43,8 @@ struct _GRenderingLine vmpa_t offset; /* Position en mémoire/physique*/ off_t length; /* Nombre d'adresses associées */ + char *comment; /* Texte à afficher */ + RenderingLineType type; /* Type de représentation */ RenderingLineFlag flags; /* Extension d'informations */ @@ -57,6 +59,7 @@ struct _GRenderingLine #define lines_list_last(head) dl_list_last(head, GRenderingLine, link) #define lines_list_next_iter(iter, head) dl_list_next_iter(iter, head, GRenderingLine, link) +#define lines_list_prev_iter(iter, head) dl_list_prev_iter(iter, head, GRenderingLine, link) #define lines_list_add_before(new, head, pos) dl_list_add_before(new, head, pos, link) #define lines_list_add_tail(new, head) dl_list_add_tail(new, head, GRenderingLine, link) #define lines_list_for_each(pos, head) dl_list_for_each(pos, head, GRenderingLine, link) diff --git a/src/analysis/line.c b/src/analysis/line.c index 24a107b..a3ba7a5 100644 --- a/src/analysis/line.c +++ b/src/analysis/line.c @@ -150,6 +150,49 @@ off_t get_rendering_line_length(const GRenderingLine *line) * * * Paramètres : line = ligne dont les informations sont à consulter. * * * +* Description : Fournit le commentaire associé à la ligne s'il existe. * +* * +* Retour : Chaîne de caractères ou NULL. * +* * +* Remarques : - * +* * +******************************************************************************/ + +const char *get_rendering_line_comment(const GRenderingLine *line) +{ + return line->comment; + +} + + +/****************************************************************************** +* * +* Paramètres : line = ligne dont les informations sont à consulter. * +* comment = nouveau commentaire à insérer ou NULL. * +* * +* Description : Définit ou supprime un commentaire pour la ligne indiquée. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void set_rendering_line_comment(GRenderingLine *line, const char *comment) +{ + if (line->comment != NULL) + free(line->comment); + + if (comment == NULL) line->comment = NULL; + else line->comment = strdup(comment); + +} + + +/****************************************************************************** +* * +* Paramètres : line = ligne dont les informations sont à consulter. * +* * * Description : Fournit le type d'une ligne. * * * * Retour : Type de la ligne fournie. * @@ -463,6 +506,38 @@ GRenderingLine *g_rendering_line_get_next_iter(GRenderingLine *lines, const GRen /****************************************************************************** * * * Paramètres : lines = liste de lignes de représentation à actualiser. * +* : iter = position actuelle dans la liste. * +* last = dernière élément imposé du parcours ou NULL. * +* * +* Description : Fournit l'élement précédant un autre pour un parcours. * +* * +* Retour : Elément suivant ou NULL si aucun. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GRenderingLine *g_rendering_line_get_prev_iter(GRenderingLine *lines, const GRenderingLine *iter, const GRenderingLine *last) +{ + GRenderingLine *result; /* Elément suivant à renvoyer */ + + if (iter == NULL) + { + if (last != NULL) iter = last; + else iter = lines_list_last(lines); + } + + if (iter == lines) result = NULL; + else result = lines_list_prev_iter(iter, lines); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : lines = liste de lignes de représentation à actualiser. * * last = dernière élément imposé du parcours ou NULL. * * * * Description : Fournit le dernier élément d'une liste de lignes. * diff --git a/src/analysis/line.h b/src/analysis/line.h index 20d6234..5061dcd 100644 --- a/src/analysis/line.h +++ b/src/analysis/line.h @@ -78,6 +78,12 @@ vmpa_t get_rendering_line_address(const GRenderingLine *); /* Fournit la longueur du code représenté par une ligne. */ off_t get_rendering_line_length(const GRenderingLine *); +/* Fournit le commentaire associé à la ligne s'il existe. */ +const char *get_rendering_line_comment(const GRenderingLine *); + +/* Définit ou supprime un commentaire pour la ligne indiquée. */ +void set_rendering_line_comment(GRenderingLine *, const char *); + /* Fournit le type d'une ligne. */ RenderingLineType get_rendering_line_type(const GRenderingLine *); @@ -119,6 +125,9 @@ void g_rendering_line_insert_into_lines(GRenderingLine **, GRenderingLine *, boo /* Fournit l'élement suivant un autre pour un parcours. */ GRenderingLine *g_rendering_line_get_next_iter(GRenderingLine *, const GRenderingLine *, const GRenderingLine *); +/* Fournit l'élement précédant un autre pour un parcours. */ +GRenderingLine *g_rendering_line_get_prev_iter(GRenderingLine *, const GRenderingLine *, const GRenderingLine *); + /* Fournit le dernier élément d'une liste de lignes. */ GRenderingLine *g_rendering_line_get_last_iter(GRenderingLine *, GRenderingLine *); diff --git a/src/analysis/line_code.c b/src/analysis/line_code.c index 24f3ec9..7c80074 100644 --- a/src/analysis/line_code.c +++ b/src/analysis/line_code.c @@ -136,7 +136,8 @@ static void g_code_line_init(GCodeLine *line) static void g_code_line_add_text(GCodeLine *line, GRenderingOptions *options, MainRendering rendering, FILE *stream) { - GContentExporter *exporter; /* Autre vision de la ligne */ + GContentExporter *exporter; /* Autre vision de la ligne #1 */ + GRenderingLine *basic; /* Autre vision de la ligne #2 */ bool show_address; /* Affichage de l'adresse ? */ bool show_code; /* Affichage du code brut ? */ MemoryDataSize msize; /* Taille du bus d'adresses */ @@ -149,6 +150,7 @@ static void g_code_line_add_text(GCodeLine *line, GRenderingOptions *options, Ma off_t i; /* Boucle de parcours */ exporter = G_CONTENT_EXPORTER(line); + basic = G_RENDERING_LINE(line); show_address = g_rendering_options_has_to_show_address(options, rendering); show_code = g_rendering_options_has_to_show_code(options, rendering); @@ -195,6 +197,16 @@ static void g_code_line_add_text(GCodeLine *line, GRenderingOptions *options, Ma g_content_exporter_add_text(G_CONTENT_EXPORTER(line->instr), options, rendering, stream); + /* Commentaire ? */ + + if (basic->comment != NULL) + { + g_content_exporter_insert_text(exporter, stream, "\t", 1, RTT_NONE); + g_content_exporter_insert_text(exporter, stream, "; ", 2, RTT_COMMENT); + g_content_exporter_insert_text(exporter, stream, basic->comment, + strlen(basic->comment), RTT_COMMENT); + } + } @@ -216,6 +228,8 @@ static void g_code_line_add_text(GCodeLine *line, GRenderingOptions *options, Ma static void g_code_line_add_to_gtk_buffer(GCodeLine *line, MainRendering rendering, GtkTextBuffer *buffer, GtkTextIter *iter, size_t lengths[SAR_COUNT]) { + GContentExporter *exporter; /* Autre vision de la ligne #1 */ + GRenderingLine *basic; /* Autre vision de la ligne #2 */ bool show_address; /* Affichage de l'adresse ? */ bool show_code; /* Affichage du code brut ? */ MemoryDataSize msize; /* Taille du bus d'adresses */ @@ -227,6 +241,9 @@ static void g_code_line_add_to_gtk_buffer(GCodeLine *line, MainRendering renderi char *bin_code; /* Tampon du code binaire */ off_t i; /* Boucle de parcours */ + exporter = G_CONTENT_EXPORTER(line); + basic = G_RENDERING_LINE(line); + show_address = g_rendering_options_has_to_show_address(line->options, rendering); show_code = g_rendering_options_has_to_show_code(line->options, rendering); @@ -239,10 +256,10 @@ static void g_code_line_add_to_gtk_buffer(GCodeLine *line, MainRendering renderi len = vmpa_to_string(G_RENDERING_LINE(line)->offset, msize, address); lengths[SAR_ADDRESS] = len; - g_content_exporter_insert_with_gtk_tag(G_CONTENT_EXPORTER(line), buffer, iter, + g_content_exporter_insert_with_gtk_tag(exporter, buffer, iter, address, len, RTT_NONE); - g_content_exporter_insert_with_gtk_tag(G_CONTENT_EXPORTER(line), buffer, iter, + g_content_exporter_insert_with_gtk_tag(exporter, buffer, iter, "\t", 1, RTT_NONE); } @@ -266,12 +283,12 @@ static void g_code_line_add_to_gtk_buffer(GCodeLine *line, MainRendering renderi snprintf(&bin_code[i * (2 + 1)], 3, "%02hhx", content[bin_offset + i]); } - g_content_exporter_insert_with_gtk_tag(G_CONTENT_EXPORTER(line), buffer, iter, + g_content_exporter_insert_with_gtk_tag(exporter, buffer, iter, bin_code, bin_len * 3 - 1, RTT_RAW_CODE); free(bin_code); - g_content_exporter_insert_with_gtk_tag(G_CONTENT_EXPORTER(line), buffer, iter, + g_content_exporter_insert_with_gtk_tag(exporter, buffer, iter, "\t", 1, RTT_NONE); } @@ -284,6 +301,16 @@ static void g_code_line_add_to_gtk_buffer(GCodeLine *line, MainRendering renderi lengths[SAR_INSTRUCTION] = MAX(lengths[SAR_INSTRUCTION], 4 /* FIXME */); + /* Commentaire ? */ + + if (basic->comment != NULL) + { + g_content_exporter_insert_with_gtk_tag(exporter, buffer, iter, "\t", 1, RTT_NONE); + g_content_exporter_insert_with_gtk_tag(exporter, buffer, iter, "; ", 2, RTT_COMMENT); + g_content_exporter_insert_with_gtk_tag(exporter, buffer, iter, basic->comment, + strlen(basic->comment), RTT_COMMENT); + } + } diff --git a/src/common/Makefile.am b/src/common/Makefile.am index c4937dd..7aa255e 100755 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -4,6 +4,7 @@ lib_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = \ dllist.h dllist.c \ endianness.h endianness.c \ + environment.h environment.c \ extstr.h extstr.c \ macros.h \ xml.h xml.c diff --git a/src/common/dllist.h b/src/common/dllist.h index 499151e..504291e 100644 --- a/src/common/dllist.h +++ b/src/common/dllist.h @@ -121,6 +121,10 @@ void __dl_list_del(dl_list_item *, dl_list_head *); (iter->member.next == &head->member ? \ NULL : container_of(iter->member.next, type, member)) +#define dl_list_prev_iter(iter, head, type, member) \ + (&iter->member == &head->member ? \ + NULL : container_of(iter->member.prev, type, member)) + #define dl_list_for_each(pos, head, type, member) \ for (pos = head; \ pos != NULL; \ @@ -140,8 +144,7 @@ void __dl_list_del(dl_list_item *, dl_list_head *); #define dl_list_for_each_rev(pos, head, type, member) \ for (pos = dl_list_last(head, type, member); \ pos != NULL; \ - pos = (pos == head ? \ - NULL : container_of(pos->member.prev, type, member))) + pos = dl_list_prev_iter(pos, (head), type, member)) diff --git a/src/common/environment.c b/src/common/environment.c new file mode 100644 index 0000000..a0fa568 --- /dev/null +++ b/src/common/environment.c @@ -0,0 +1,94 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * environment.c - manipulations des variables d'environnement. + * + * Copyright (C) 2010 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 Foobar. If not, see . + */ + + +#include "environment.h" + + +#include +#include +#include + + +#include "extstr.h" + + + +/****************************************************************************** +* * +* Paramètres : name = désignation de la variable à traiter. * +* * +* Description : Fournit le contenu d'une variable d'environnement. * +* * +* Retour : Chaîne à libérer de la mémoire après usage. * +* * +* Remarques : - * +* * +******************************************************************************/ + +char *get_env_var(const char *name) +{ + char *result; /* Chaîne à retourner */ + + result = getenv(name); + + if (result == NULL) result = strdup(""); + else result = strdup(name); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : name = désignation de la variable à traiter. * +* value = valeur à ajouter à la variable. * +* sep = séparateur entre champs. * +* * +* Description : Complète le contenu d'une variable d'environnement. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_to_env_var(const char *name, const char *value, const char *sep) +{ + char *content; /* Contenu final à définir */ + int ret; /* Bilan d'un appel système */ + + content = get_env_var(name); + + if (strlen(content) > 0) content = stradd(content, sep); + + content = stradd(content, value); + + ret = setenv(name, content, 1); + if (ret != 0) perror(setenv); + + free(content); + + return (ret == 0); + +} diff --git a/src/common/environment.h b/src/common/environment.h new file mode 100644 index 0000000..a96c6eb --- /dev/null +++ b/src/common/environment.h @@ -0,0 +1,40 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * environment.h - prototypes pour la manipulations des variables d'environnement. + * + * Copyright (C) 2010 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 Foobar. If not, see . + */ + + +#ifndef _COMMON_ENVIRONMENT_H +#define _COMMON_ENVIRONMENT_H + + +#include + + + +/* Fournit le contenu d'une variable d'environnement. */ +char *get_env_var(const char *); + +/* Complète le contenu d'une variable d'environnement. */ +bool add_to_env_var(const char *, const char *, const char *); + + + +#endif /* _COMMON_ENVIRONMENT_H */ diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am index 36fc9b4..9a76f74 100644 --- a/src/plugins/Makefile.am +++ b/src/plugins/Makefile.am @@ -4,6 +4,7 @@ lib_LTLIBRARIES = libplugins.la libplugins_la_SOURCES = \ pglist.h pglist.c \ plugin-def.h \ + plugin-int.h \ plugin.h plugin.c libplugins_la_CFLAGS = $(AM_CFLAGS) diff --git a/src/plugins/pglist.c b/src/plugins/pglist.c index 6d47f57..f242cba 100644 --- a/src/plugins/pglist.c +++ b/src/plugins/pglist.c @@ -233,3 +233,27 @@ GPluginModule **get_all_plugins_for_action(PluginAction action, size_t *count) return result; } + + +/****************************************************************************** +* * +* Paramètres : plugin = greffon à ajouter aux autres disponibles. * +* * +* Description : Ajoute un greffon à la liste principale de greffons. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void add_plugin_to_main_list(GPluginModule *plugin) +{ + plugins_list *list; /* Liste à modifier */ + + list = &_list; + + list->plugins = (GPluginModule **)realloc(list->plugins, ++list->plugins_count * sizeof(GPluginModule *)); + list->plugins[list->plugins_count - 1] = plugin; + +} diff --git a/src/plugins/plugin-def.h b/src/plugins/plugin-def.h index f8f8fe3..1b47bd6 100644 --- a/src/plugins/plugin-def.h +++ b/src/plugins/plugin-def.h @@ -41,13 +41,14 @@ typedef enum _PluginAction +struct _GPluginModule; /* Fournit une indication sur le type d'opération(s) menée(s). */ typedef PluginAction (* get_plugin_action_fc) (void); /* Exécute une action définie sur un binaire chargé. */ -typedef bool (* execute_action_on_binary_fc) (GOpenidaBinary *, PluginAction); +typedef bool (* execute_action_on_binary_fc) (struct _GPluginModule *, GOpenidaBinary *, PluginAction); diff --git a/src/plugins/plugin-int.h b/src/plugins/plugin-int.h new file mode 100644 index 0000000..7e2703b --- /dev/null +++ b/src/plugins/plugin-int.h @@ -0,0 +1,74 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * elf-int.h - prototypes pour les structures internes du format ELF + * + * Copyright (C) 2008 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 Foobar. If not, see . + */ + + +#ifndef _FORMAT_PLUGINS_PLUGIN_INT_H +#define _FORMAT_PLUGINS_PLUGIN_INT_H + + +#include + + +#include "plugin-def.h" + + + + +/* Procède à l'initialisation du greffon */ +typedef bool (* init_plugin_fc) (GObject *); + + + +/* Greffon pour OpenIDA (instance) */ +struct _GPluginModule +{ + GObject parent; /* A laisser en premier */ + + GModule *module; /* Abstration de manipulation */ + + PluginAction action; /* Opération(s) menée(s) */ + + init_plugin_fc init; /* Procédure d'initialisation */ + + execute_action_on_binary_fc exec_on_bin;/* Action sur un binaire */ + +}; + + +/* Greffon pour OpenIDA (classe) */ +struct _GPluginModuleClass +{ + GObjectClass parent; /* A laisser en premier */ + +}; + + + + + +/* Ajoute un greffon à la liste principale de greffons. */ +void add_plugin_to_main_list(GPluginModule *); + + + + +#endif /* _FORMAT_PLUGINS_PLUGIN_INT_H */ diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index e6e4a1a..ed4c39d 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -1,6 +1,6 @@ /* OpenIDA - Outil d'analyse de fichiers binaires - * plugin.h - interactions avec un greffon donné + * plugin.c - interactions avec un greffon donné * * Copyright (C) 2009 Cyrille Bagard * @@ -29,38 +29,10 @@ #include +#include "plugin-int.h" -/* Procède à l'initialisation du greffon */ -typedef bool (* init_plugin_fc) (GObject *); - - - -/* Greffon pour OpenIDA (instance) */ -struct _GPluginModule -{ - GObject parent; /* A laisser en premier */ - - GModule *module; /* Abstration de manipulation */ - - PluginAction action; /* Opération(s) menée(s) */ - - init_plugin_fc init; /* Procédure d'initialisation */ - - execute_action_on_binary_fc exec_on_bin;/* Action sur un binaire */ - -}; - - -/* Greffon pour OpenIDA (classe) */ -struct _GPluginModuleClass -{ - GObjectClass parent; /* A laisser en premier */ - -}; - - /* Initialise la classe des greffons. */ static void g_plugin_module_class_init(GPluginModuleClass *); @@ -135,7 +107,7 @@ GPluginModule *g_plugin_module_new(const gchar *filename, GObject *ref) result->module = g_module_open(filename, G_MODULE_BIND_LAZY); -#if 1 +#if 0 if (!g_module_symbol(result->module, "get_plugin_action", (gpointer *)&__get_action)) { printf("Err plugin get_action sym\n"); @@ -213,6 +185,6 @@ PluginAction g_plugin_module_get_action(const GPluginModule *plugin) bool g_plugin_module_execute_action_on_binary(const GPluginModule *plugin, GOpenidaBinary *binary, PluginAction action) { - return plugin->exec_on_bin(binary, action); + return plugin->exec_on_bin(plugin, binary, action); } -- cgit v0.11.2-87-g4458