diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2018-01-16 19:02:56 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2018-01-16 19:02:56 (GMT) |
commit | 9da8f8b37e3edebc917b4e223dd2447cd7cbc818 (patch) | |
tree | 3f330b13e7ca2a0a163882be3043ca9571f25211 /plugins/pychrysalide/arch/instruction.c | |
parent | eb9b7fd76451db5c9f07a800c0394480e4b88c9c (diff) |
Changed the Python bindings source directory and updated code.
Diffstat (limited to 'plugins/pychrysalide/arch/instruction.c')
-rw-r--r-- | plugins/pychrysalide/arch/instruction.c | 462 |
1 files changed, 462 insertions, 0 deletions
diff --git a/plugins/pychrysalide/arch/instruction.c b/plugins/pychrysalide/arch/instruction.c new file mode 100644 index 0000000..bb977ce --- /dev/null +++ b/plugins/pychrysalide/arch/instruction.c @@ -0,0 +1,462 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * instruction.c - équivalent Python du fichier "arch/instruction.h" + * + * Copyright (C) 2012-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 "instruction.h" + + +#include <assert.h> +#include <pygobject.h> + + +#include <arch/instruction.h> + + +#include "vmpa.h" +#include "../helpers.h" +#include "../glibext/linegen.h" + + + + +/* ------------------- DEFINITION DES LIAISONS ENTRE INSTRUCTIONS ------------------- */ + + + + + +/* --------------------- INSTRUCTIONS D'ARCHITECTURES EN PYTHON --------------------- */ + + + +/* Fournit la place mémoire d'une instruction. */ +static PyObject *py_arch_instruction_get_range(PyObject *, void *); + +/* Définit la localisation d'une instruction. */ +static int py_arch_instruction_set_range(PyObject *, PyObject *, void *); + + + +/* Fournit le nom humain de l'instruction manipulée. */ +static PyObject *py_arch_instruction_get_keyword(PyObject *, void *); + + + +/* Fournit tous les opérandes d'une instruction. */ +static PyObject *py_arch_instruction_get_operands(PyObject *, void *); + + + + + + + + + + +/* ---------------------------------------------------------------------------------- */ +/* DEFINITION DES LIAISONS ENTRE INSTRUCTIONS */ +/* ---------------------------------------------------------------------------------- */ + + + + +/* Fournit les origines d'une instruction donnée. */ +static PyObject *py_arch_instruction_get_sources(PyObject *, void *); + +/* Fournit les destinations d'une instruction donnée. */ +static PyObject *py_arch_instruction_get_destinations(PyObject *, void *); + + + + +/****************************************************************************** +* * +* Paramètres : self = instruction d'architecture à manipuler. * +* unused = adresse non utilisée ici. * +* * +* Description : Fournit les origines d'une instruction donnée. * +* * +* Retour : Nombre de ces origines. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_arch_instruction_get_sources(PyObject *self, void *unused) +{ + PyObject *result; /* Instance à retourner */ + GArchInstruction *instr; /* Version native */ + instr_link_t *source; /* Origine des liens */ + size_t count; /* Nombre de liens présents */ + size_t i; /* Boucle de parcours */ + PyObject *linked; /* Source de lien Python */ + PyObject *type; /* Nature du lien en Python */ + int ret; /* Bilan d'une écriture d'arg. */ + + instr = G_ARCH_INSTRUCTION(pygobject_get(self)); + + g_arch_instruction_lock_src(instr); + + count = g_arch_instruction_count_sources(instr); + + result = PyTuple_New(count); + + for (i = 0; i < count; i++) + { + source = g_arch_instruction_get_source(instr, i); + + linked = pygobject_new(G_OBJECT(source->linked)); + type = PyLong_FromLong(source->type); + + ret = PyTuple_SetItem(result, i, Py_BuildValue("(OO)", linked, type)); + assert(ret == 0); + + } + + g_arch_instruction_unlock_src(instr); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = instruction d'architecture à manipuler. * +* unused = adresse non utilisée ici. * +* * +* Description : Fournit les destinations d'une instruction donnée. * +* * +* Retour : Nombre de ces destinations. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_arch_instruction_get_destinations(PyObject *self, void *unused) +{ + PyObject *result; /* Instance à retourner */ + GArchInstruction *instr; /* Version native */ + instr_link_t *dest; /* Destination des liens */ + size_t count; /* Nombre de liens présents */ + size_t i; /* Boucle de parcours */ + PyObject *linked; /* Destination de lien Python */ + PyObject *type; /* Nature du lien en Python */ + int ret; /* Bilan d'une écriture d'arg. */ + + instr = G_ARCH_INSTRUCTION(pygobject_get(self)); + + g_arch_instruction_lock_dest(instr); + + count = g_arch_instruction_count_destinations(instr); + + result = PyTuple_New(count); + + for (i = 0; i < count; i++) + { + dest = g_arch_instruction_get_destination(instr, i); + + linked = pygobject_new(G_OBJECT(dest->linked)); + type = PyLong_FromLong(dest->type); + + ret = PyTuple_SetItem(result, i, Py_BuildValue("(OO)", linked, type)); + assert(ret == 0); + + } + + g_arch_instruction_unlock_dest(instr); + + return result; + +} + + + + + + + + + + + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une instruction. * +* closure = adresse non utilisée ici. * +* * +* Description : Fournit la place mémoire d'une instruction. * +* * +* Retour : Valeur associée à la propriété consultée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_arch_instruction_get_range(PyObject *self, void *closure) +{ + PyObject *result; /* Conversion à retourner */ + GArchInstruction *instr; /* Version native */ + const mrange_t *range; /* Espace mémoire à exporter */ + + instr = G_ARCH_INSTRUCTION(pygobject_get(self)); + range = g_arch_instruction_get_range(instr); + + result = build_from_internal_mrange(range); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* value = valeur fournie à intégrer ou prendre en compte. * +* closure = adresse non utilisée ici. * +* * +* Description : Définit la localisation d'une instruction. * +* * +* Retour : Bilan de l'opération pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_arch_instruction_set_range(PyObject *self, PyObject *value, void *closure) +{ + int ret; /* Bilan d'analyse */ + mrange_t *range; /* Espace mémoire à manipuler */ + GArchInstruction *instr; /* Version native */ + + ret = PyObject_IsInstance(value, (PyObject *)get_python_mrange_type()); + if (!ret) return -1; + + range = get_internal_mrange(value); + + instr = G_ARCH_INSTRUCTION(pygobject_get(self)); + g_arch_instruction_set_range(instr, range); + + return 0; + +} + + + + + + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une instruction. * +* unused = adresse non utilisée ici. * +* * +* Description : Fournit le nom humain de l'instruction manipulée. * +* * +* Retour : Valeur associée à la propriété consultée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_arch_instruction_get_keyword(PyObject *self, void *unused) +{ + PyObject *result; /* Trouvailles à retourner */ + GArchInstruction *instr; /* Version native */ + const char *kw; /* Valeur récupérée */ + + instr = G_ARCH_INSTRUCTION(pygobject_get(self)); + kw = g_arch_instruction_get_keyword(instr, 0/* FIXME*/); + + result = PyUnicode_FromString(kw); + + return result; + +} + + + + + +/****************************************************************************** +* * +* Paramètres : self = objet représentant une instruction. * +* unused = adresse non utilisée ici. * +* * +* Description : Fournit tous les opérandes d'une instruction. * +* * +* Retour : Valeur associée à la propriété consultée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_arch_instruction_get_operands(PyObject *self, void *unused) +{ + PyObject *result; /* Instance à retourner */ + GArchInstruction *instr; /* Version native */ + size_t count; /* Nombre d'opérandes présents */ + size_t i; /* Boucle de parcours */ + GArchOperand *operand; /* Opérande à manipuler */ + PyObject *opobj; /* Version Python */ + int ret; /* Bilan d'une écriture d'arg. */ + + instr = G_ARCH_INSTRUCTION(pygobject_get(self)); + + g_arch_instruction_lock_operands(instr); + + count = _g_arch_instruction_count_operands(instr); + + result = PyTuple_New(count); + + for (i = 0; i < count; i++) + { + operand = _g_arch_instruction_get_operand(instr, i); + + opobj = pygobject_new(G_OBJECT(operand)); + + ret = PyTuple_SetItem(result, i, Py_BuildValue("O", opobj)); + assert(ret == 0); + + g_object_unref(G_OBJECT(operand)); + + } + + g_arch_instruction_unlock_operands(instr); + + 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_arch_instruction_type(void) +{ + static PyMethodDef py_arch_instruction_methods[] = { + { NULL } + }; + + static PyGetSetDef py_arch_instruction_getseters[] = { + { + "range", py_arch_instruction_get_range, py_arch_instruction_set_range, + "Give access to the memory range covered by the current instruction.", NULL + }, + { + "keyword", (getter)py_arch_instruction_get_keyword, (setter)NULL, + "Give le name of the assembly instruction.", NULL + }, + { + "operands", (getter)py_arch_instruction_get_operands, (setter)NULL, + "Provide the list of instruction attached operands.", NULL + }, + { + "sources", (getter)py_arch_instruction_get_sources, (setter)NULL, + "Provide the instructions list driving to the current instruction." + }, + { + "destinations", (getter)py_arch_instruction_get_destinations, (setter)NULL, + "Provide the instructions list following the current instruction." + }, + { NULL } + }; + + static PyTypeObject py_arch_instruction_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.arch.ArchInstruction", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyChrysalide instruction for a given architecture.", + + .tp_methods = py_arch_instruction_methods, + .tp_getset = py_arch_instruction_getseters, + + }; + + return &py_arch_instruction_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.arch.ArchInstruction'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_arch_instruction(PyObject *module) +{ + PyTypeObject *py_arch_instruction_type; /* Type Python 'ArchInstruc...'*/ + PyObject *dict; /* Dictionnaire du module */ + PyTypeObject *py_line_generator_type; /* Type Python 'LineGenerator' */ + + py_arch_instruction_type = get_python_arch_instruction_type(); + + APPLY_ABSTRACT_FLAG(py_arch_instruction_type); + + dict = PyModule_GetDict(module); + + py_line_generator_type = get_python_line_generator_type(); + + if (!_register_class_for_pygobject(dict, G_TYPE_ARCH_INSTRUCTION, py_arch_instruction_type, + &PyGObject_Type, py_line_generator_type, NULL)) + return false; + + return true; + +} |