diff options
Diffstat (limited to 'plugins/pychrysa/glibext/codebuffer.c')
-rw-r--r-- | plugins/pychrysa/glibext/codebuffer.c | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/plugins/pychrysa/glibext/codebuffer.c b/plugins/pychrysa/glibext/codebuffer.c new file mode 100644 index 0000000..a090e27 --- /dev/null +++ b/plugins/pychrysa/glibext/codebuffer.c @@ -0,0 +1,159 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * codebuffer.c - équivalent Python du fichier "glibext/gcodebuffer.h" + * + * Copyright (C) 2012 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 "codebuffer.h" + + +#include <pygobject.h> + + +#include "../quirks.h" + + + +/* Retrouve une ligne au sein d'un tampon avec une adresse. */ +static PyObject *py_code_buffer_find_line_by_addr(PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* Paramètres : binary = instance existante GLib. * +* * +* Description : Crée un nouvel objet Python de type 'Codebuffer'. * +* * +* Retour : Instance Python mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *py_code_buffer_from_c(GCodeBuffer *buffer) +{ + PyObject *module; /* Module d'appartenance */ + PyTypeObject *type; /* Type Python correspondant */ + + module = PyImport_ImportModule("pychrysalide.glibext"); + type = (PyTypeObject *)PyObject_GetAttrString(module, "CodeBuffer"); + Py_DECREF(module); + + pychrysalide_set_instance_data(G_OBJECT(buffer), type); + + return pygobject_new(G_OBJECT(buffer)); + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant un tampon de code. * +* args = arguments fournis à l'appel. * +* * +* Description : Retrouve une ligne au sein d'un tampon avec une adresse. * +* * +* Retour : Instance de la ligne trouvée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_code_buffer_find_line_by_addr(PyObject *self, PyObject *args) +{ + PyObject *result; /* Trouvailles à retourner */ + GCodeBuffer *buffer; /* Version native */ + vmpa_t addr; /* Adresse visée par l'opérat° */ + int ret; /* Bilan de lecture des args. */ + GBufferLine *line; /* Ligne trouvée */ + + buffer = G_CODE_BUFFER(pygobject_get(self)); + + ret = PyArg_ParseTuple(args, "K", &addr); + if (!ret) return Py_None; + + line = g_code_buffer_find_line_by_addr(buffer, addr); + if (line == NULL) return Py_None; + + result = py_buffer_line_from_c(line); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.glibext.Codebuffer'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_code_buffer(PyObject *module) +{ + PyObject *pygobj_mod; /* Module Python-GObject */ + int ret; /* Bilan d'un appel */ + + static PyMethodDef py_code_buffer_methods[] = { + { + "find_line_by_addr", (PyCFunction)py_code_buffer_find_line_by_addr, + METH_VARARGS, + "Find a buffer line with a given address." + }, + { NULL } + }; + + static PyTypeObject py_code_buffer_type = { + + PyObject_HEAD_INIT(NULL) + + .tp_name = "pychrysalide.glibext.CodeBuffer", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyChrysalide code buffer", + + .tp_methods = py_code_buffer_methods + + }; + + pygobj_mod = PyImport_ImportModule("gobject"); + if (pygobj_mod == NULL) return false; + + py_code_buffer_type.tp_base = (PyTypeObject *)PyObject_GetAttrString(pygobj_mod, "GObject"); + Py_DECREF(pygobj_mod); + + if (PyType_Ready(&py_code_buffer_type) < 0) + return false; + + Py_INCREF(&py_code_buffer_type); + ret = PyModule_AddObject(module, "CodeBuffer", (PyObject *)&py_code_buffer_type); + + return (ret == 0); + +} |