/* Chrysalide - Outil d'analyse de fichiers binaires * vmpa.c - équivalent Python du fichier "arch/vmpa.c" * * Copyright (C) 2014 Cyrille Bagard * * This file is part of Chrysalide. * * 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 "vmpa.h" #include typedef struct _py_vmpa_t { PyObject_HEAD vmpa2_t addr; } py_vmpa_t; /* Fournit une représentation d'une variable 'vmpa_t'. */ static PyObject *py_vmpa_to_str(PyObject *); /* Effectue une conversion d'un objet Python en type 'vmpa_t'. */ static bool convert_pyobj_to_vmpa(PyObject *, vmpa2_t *); /* Effectue une opération de type 'add' avec le type 'vmpa'. */ static PyObject *py_vmpa_nb_add(PyObject *, PyObject *); /****************************************************************************** * * * Paramètres : obj = objet Python à traiter. * * * * Description : Fournit une représentation d'une variable 'vmpa_t'. * * * * Retour : Chaîne de caractère pour Python. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_vmpa_to_str(PyObject *obj) { vmpa2_t *addr; addr = &((py_vmpa_t *)obj)->addr; return PyUnicode_FromFormat("", addr->physical, addr->virtual); } /****************************************************************************** * * * Paramètres : obj = objet Python à tenter de convertir. * * addr = structure équivalente pour Chrysalide. * * * * Description : Effectue une conversion d'un objet Python en type 'vmpa_t'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool convert_pyobj_to_vmpa(PyObject *obj, vmpa2_t *addr) { bool result; /* Résulats à retourner */ PyTypeObject *py_vmpa_type; /* Type Python pour 'vmpa' */ int ret; /* Bilan d'un appel */ PY_LONG_LONG value; /* Valeur de type générique */ int overflow; /* Détection d'une grosse val. */ result = false; py_vmpa_type = get_python_vmpa_type(); ret = PyObject_IsInstance(obj, (PyObject *)py_vmpa_type); /* S'il n'y a rien à faire... */ if (ret == 1) { *addr = ((py_vmpa_t *)obj)->addr; result = true; } /* Sinon on demande à Python... */ else { value = PyLong_AsLongLongAndOverflow(obj, &overflow); if (value == -1 && (overflow == 1 || PyErr_Occurred())) PyErr_Clear(); else { init_vmpa(addr, value, value); result = true; } } return result; } /****************************************************************************** * * * Paramètres : o1 = premier élément concerné par l'opération. * * o2 = second élément concerné par l'opération. * * * * Description : Effectue une opération de type 'add' avec le type 'vmpa'. * * * * Retour : Résultat de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_vmpa_nb_add(PyObject *o1, PyObject *o2) { PyObject *result; /* Résultat à retourner */ vmpa2_t addr1; /* Première adresse à traiter */ vmpa2_t addr2; /* Seconde adresse à traiter */ PyTypeObject *py_vmpa_type; /* Type Python pour 'vmpa' */ if (!convert_pyobj_to_vmpa(o1, &addr1)) return NULL; if (!convert_pyobj_to_vmpa(o2, &addr2)) return NULL; py_vmpa_type = get_python_vmpa_type(); result = PyObject_CallObject((PyObject *)py_vmpa_type, NULL); init_vmpa(&((py_vmpa_t *)result)->addr, addr1.physical + addr2.physical, addr1.virtual + addr2.virtual); return result; } void log_simple_message(/*LogMessageType*/ int type, const char *msg) { } void log_variadic_message(/*LogMessageType*/ int type, const char *fmt, ...) { } void change_editor_items_current_view_content(void/*GtkViewPanel*/ *view) { } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_vmpa_type(void) { static PyNumberMethods py_vmpa_nb_proto = { .nb_add = py_vmpa_nb_add, /* binaryfunc nb_add; binaryfunc nb_subtract; binaryfunc nb_multiply; binaryfunc nb_remainder; binaryfunc nb_divmod; ternaryfunc nb_power; unaryfunc nb_negative; unaryfunc nb_positive; unaryfunc nb_absolute; inquiry nb_bool; unaryfunc nb_invert; binaryfunc nb_lshift; binaryfunc nb_rshift; binaryfunc nb_and; binaryfunc nb_xor; binaryfunc nb_or; unaryfunc nb_int; void *nb_reserved; unaryfunc nb_float; binaryfunc nb_inplace_add; binaryfunc nb_inplace_subtract; binaryfunc nb_inplace_multiply; binaryfunc nb_inplace_remainder; ternaryfunc nb_inplace_power; binaryfunc nb_inplace_lshift; binaryfunc nb_inplace_rshift; binaryfunc nb_inplace_and; binaryfunc nb_inplace_xor; binaryfunc nb_inplace_or; binaryfunc nb_floor_divide; binaryfunc nb_true_divide; binaryfunc nb_inplace_floor_divide; binaryfunc nb_inplace_true_divide; unaryfunc nb_index; */ }; static PyTypeObject py_vmpa_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.arch.vmpa", .tp_basicsize = sizeof(py_vmpa_t), .tp_as_number = &py_vmpa_nb_proto, .tp_str = py_vmpa_to_str, .tp_flags = Py_TPFLAGS_DEFAULT, .tp_doc = "Python object for vmpa_t" }; return &py_vmpa_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.arch.vmpa'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool register_python_vmpa(PyObject *module) { PyTypeObject *py_vmpa_type; /* Type Python pour 'vmpa' */ int ret; /* Bilan d'un appel */ py_vmpa_type = get_python_vmpa_type(); py_vmpa_type->tp_new = PyType_GenericNew; if (PyType_Ready(py_vmpa_type) != 0) return false; Py_INCREF(py_vmpa_type); ret = PyModule_AddObject(module, "vmpa", (PyObject *)py_vmpa_type); return (ret == 0); }