/* Chrysalide - Outil d'analyse de fichiers binaires * block.c - équivalent Python du fichier "analysis/block.c" * * Copyright (C) 2013-2017 Cyrille Bagard * * This file is part of Chrysalide. * * Chrysalide 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. * * Chrysalide 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 "block.h" #include #include #include "../access.h" #include "../helpers.h" #include "../arch/vmpa.h" /* ----------------------------- BLOC DE CODE GENERIQUE ----------------------------- */ /* Indique l'indice d'intégration du bloc dans une liste. */ static PyObject *py_code_block_get_index(PyObject *, void *); /* Fournit le rang du bloc de code dans le flot d'exécution. */ static PyObject *py_code_block_get_rank(PyObject *, void *); /* ------------------------- REGROUPEMENT EN LISTE DE BLOCS ------------------------- */ /* Recherche un bloc de code débutant à une adresse donnée. */ static PyObject *py_block_list_find_by_starting_addr(PyObject *, PyObject *); /* Fournit l'ensemble des blocs de code inclus dans une liste. */ static PyObject *py_block_list_get_items(PyObject *, void *); /* ---------------------------------------------------------------------------------- */ /* BLOC DE CODE GENERIQUE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Indique l'indice d'intégration du bloc dans une liste. * * * * Retour : Indice valide dans une liste. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_code_block_get_index(PyObject *self, void *closure) { PyObject *result; /* Trouvailles à retourner */ GCodeBlock *block; /* Bloc de code à consulter */ size_t value; /* Valeur à transmettre */ block = G_CODE_BLOCK(pygobject_get(self)); value = g_code_block_get_index(block); result = PyLong_FromSize_t(value); return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit le rang du bloc de code dans le flot d'exécution. * * * * Retour : Indice supérieur ou égal à zéro. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_code_block_get_rank(PyObject *self, void *closure) { PyObject *result; /* Trouvailles à retourner */ GCodeBlock *block; /* Bloc de code à consulter */ size_t value; /* Valeur à transmettre */ block = G_CODE_BLOCK(pygobject_get(self)); value = g_code_block_get_rank(block); result = PyLong_FromSize_t(value); return result; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_code_block_type(void) { static PyMethodDef py_code_block_methods[] = { { NULL } }; static PyGetSetDef py_code_block_getseters[] = { { "index", py_code_block_get_index, NULL, "Index of the code block in the parent list, if any.", NULL }, { "rank", py_code_block_get_rank, NULL, "Rang of the code block.", NULL }, { NULL } }; static PyTypeObject py_code_block_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.analysis.CodeBlock", .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_doc = "PyChrysalide code block", .tp_methods = py_code_block_methods, .tp_getset = py_code_block_getseters, }; return &py_code_block_type; } /****************************************************************************** * * * Paramètres : - * * * * Description : Prend en charge l'objet 'pychrysalide.analysis.CodeBlock'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_code_block_is_registered(void) { PyTypeObject *type; /* Type Python 'InstrBlock' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ type = get_python_code_block_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { module = get_access_to_python_module("pychrysalide.analysis"); dict = PyModule_GetDict(module); if (!register_class_for_pygobject(dict, G_TYPE_CODE_BLOCK, type, &PyGObject_Type)) return false; } return true; } /* ---------------------------------------------------------------------------------- */ /* REGROUPEMENT EN LISTE DE BLOCS */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : self = classe représentant une liste de blocs de code. * * args = arguments fournis à l'appel. * * * * Description : Recherche un bloc de code débutant à une adresse donnée. * * * * Retour : Bloc de code trouvé ou None si aucun. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_block_list_find_by_starting_addr(PyObject *self, PyObject *args) { PyObject *result; /* Conclusion à retourner */ vmpa2t addr; /* Emplacement ciblé */ int ret; /* Bilan de lecture des args. */ GBlockList *list; /* Liste de blocs manipulée */ GCodeBlock *found; /* Eventuel bloc trouvé */ ret = PyArg_ParseTuple(args, "O&", convert_any_to_vmpa, &addr); if (!ret) return NULL; list = G_BLOCK_LIST(pygobject_get(self)); found = g_block_list_find_by_starting_addr(list, &addr); if (found != NULL) { result = pygobject_new(G_OBJECT(found)); g_object_unref(G_OBJECT(found)); } else { result = Py_None; Py_INCREF(result); } return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit l'ensemble des blocs de code inclus dans une liste. * * * * Retour : Liste de blocs de code. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_block_list_get_items(PyObject *self, void *closure) { PyObject *result; /* Trouvailles à retourner */ GBlockList *list; /* Liste de blocs manipulée */ size_t count; /* Nombre de blocs présents */ size_t i; /* Boucle de parcours */ GCodeBlock *block; /* Bloc de code à intégrer */ list = G_BLOCK_LIST(pygobject_get(self)); count = g_block_list_count_blocks(list); result = PyTuple_New(count); for (i = 0; i < count; i++) { block = g_block_list_get_block(list, i); PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(block))); g_object_unref(G_OBJECT(block)); } return result; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_block_list_type(void) { static PyMethodDef py_block_list_methods[] = { { "find_by_starting_addr", py_block_list_find_by_starting_addr, METH_VARARGS, "find_by_starting_addr($self, addr, /)\n--\n\nFind a code block starting at a given address." }, { NULL } }; static PyGetSetDef py_block_list_getseters[] = { { "items", py_block_list_get_items, NULL, "All code blocks included in the list.", NULL }, { NULL } }; static PyTypeObject py_block_list_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.analysis.BlockList", .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_doc = "PyChrysalide basic block", .tp_methods = py_block_list_methods, .tp_getset = py_block_list_getseters, }; return &py_block_list_type; } /****************************************************************************** * * * Paramètres : - * * * * Description : Prend en charge l'objet 'pychrysalide.analysis.BlockList'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_block_list_is_registered(void) { PyTypeObject *type; /* Type Python 'BlockList' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ type = get_python_block_list_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { module = get_access_to_python_module("pychrysalide.analysis"); dict = PyModule_GetDict(module); if (!register_class_for_pygobject(dict, G_TYPE_BLOCK_LIST, type, &PyGObject_Type)) return false; } return true; }