/* OpenIDA - Outil d'analyse de fichiers binaires * py_binary.c - intermédiaire des binaires pour Python * * Copyright (C) 2009 Cyrille Bagard * * This file is part of OpenIDA. * * OpenIDA is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * OpenIDA is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "py_binary.h" #include #include "py_line.h" typedef struct { PyObject_HEAD GLoadedBinary *binary; /* Instance réelle rattachée */ } pybinary; #define _(str) str /* Fournit la description du type 'binary' pour Python. */ PyTypeObject *pybinary_get_type(void); /* Fournit les lignes de rendu associé pour Python. */ static PyObject *pybinary_get_lines(pybinary *); static void pybinary_dealloc(pybinary* self) { #if 0 Py_XDECREF(self->first); Py_XDECREF(self->last); #endif //printf("dealloc\n"); #if PY_VERSION_HEX < 0x03000000 self->ob_type->tp_free((PyObject*)self); #else Py_TYPE(self)->tp_free((PyObject*)self); #endif //printf("dealloc::end\n"); } static PyObject * pybinary_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { pybinary *self; printf("creating a new binary\n"); self = (pybinary *)type->tp_alloc(type, 0); if (self != NULL) { #if 0 self->first = PyString_FromString("");//PyUnicode_FromString(""); if (self->first == NULL) { Py_DECREF(self); return NULL; } self->last = PyString_FromString("");//PyUnicode_FromString(""); if (self->last == NULL) { Py_DECREF(self); return NULL; } self->number = 0; #endif } return (PyObject *)self; } /****************************************************************************** * * * Paramètres : binary = modèle à représenter en Python. * * * * Description : Initialise le greffon permettant l'usage de Python. * * * * Retour : Nouvelle instance d'objet Python ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ PyObject *pybinary_new_from_existing(GLoadedBinary *binary) { pybinary *result; /* Nouvelle instance à renvoyer*/ PyTypeObject *type; /* Type d'objet à créer */ printf("EXISTING :: %p\n", binary); type = pybinary_get_type(); result = (pybinary *)type->tp_alloc(type, 0); if (result != NULL) result->binary = binary; return (PyObject *)result; } static int pybinary_init(pybinary *self, PyObject *args, PyObject *kwds) { #if 0 PyObject *first=NULL, *last=NULL, *tmp; static char *kwlist[] = {"first", "last", "number", NULL}; printf("pybinary_init\n"); if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSi", kwlist, &first, &last, &self->number)) return -1; if (first) { tmp = self->first; Py_INCREF(first); self->first = first; Py_DECREF(tmp); } if (last) { tmp = self->last; Py_INCREF(last); self->last = last; Py_DECREF(tmp); } #endif return 0; } static PyMemberDef pybinary_members[] = { #if 0 {"number", T_INT, offsetof(pybinary, number), 0, "noddy number"}, #endif {NULL} /* Sentinel */ }; /****************************************************************************** * * * Paramètres : self = instance manipulée à traiter. * * * * Description : Fournit les lignes de rendu associé pour Python. * * * * Retour : Nouvelle instance d'objet Python ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *pybinary_get_lines(pybinary *self) { PyObject *result; /* Liste à retourner */ GRenderingLine *lines; /* Liste récupérée à convertir */ lines = g_loaded_binary_get_lines(self->binary); result = pyline_new_from_existing(lines, lines); return result; } #if 0 static PyObject * pybinary_name(pybinary* self) { static PyObject *format = NULL; PyObject *args, *result; if (format == NULL) { format = PyString_FromString("%s %s");//PyUnicode_FromString("%s %s"); if (format == NULL) return NULL; } args = Py_BuildValue("OO", self->first, self->last); if (args == NULL) return NULL; result = PyUnicode_Format(format, args); Py_DECREF(args); return result; } #endif static PyMethodDef pybinary_methods[] = { #if 0 {"name", (PyCFunction)pybinary_name, METH_NOARGS, "Return the name, combining the first and last name" }, #endif { "lines", (PyCFunction)pybinary_get_lines, METH_NOARGS, "Provide a list of associated rendering lines." }, {NULL} /* Sentinel */ }; /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit la description du type 'binary' pour Python. * * * * Retour : Adresse de la description du type 'binary'. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *pybinary_get_type(void) { static PyTypeObject result = { PyObject_HEAD_INIT(NULL) #if PY_VERSION_HEX < 0x03000000 0, /*ob_size*/ #endif "noddy.pybinary", /* tp_name */ sizeof(pybinary), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)pybinary_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved / tp_compare */ 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ "pybinary objects", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ pybinary_methods, /* tp_methods */ pybinary_members, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ (initproc)pybinary_init, /* tp_init */ 0, /* tp_alloc */ pybinary_new, /* tp_new */ }; return &result; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Ajoute l'objet 'binary' au module Python. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ bool add_binary_to_python_module(PyObject *module) { int ret; /* Bilan d'un appel */ if (PyType_Ready(pybinary_get_type()) < 0) return false; printf("Adding binary type\n"); Py_INCREF(pybinary_get_type()); PyModule_AddObject(module, "binary", (PyObject *)pybinary_get_type()); return true; /* FIXME */ }