/* 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); }