/* Chrysalide - Outil d'analyse de fichiers binaires * bufferline.c - équivalent Python du fichier "glibext/gbufferline.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 "bufferline.h" #include #include #include #include #include "../access.h" #include "../helpers.h" #include "../arch/vmpa.h" /* Reconstruit et fournit le texte présent sur une ligne tampon. */ static PyObject *py_buffer_line_get_text(PyObject *, PyObject *); /* Ajoute une propriété particulière à une ligne donnée. */ static PyObject *py_buffer_line_add_flag(PyObject *, PyObject *); /* Retire une propriété particulière à une ligne donnée. */ static PyObject *py_buffer_line_remove_flag(PyObject *, PyObject *); /* Fournit l'emplacement où se situe un symbole. */ static PyObject *py_buffer_line_get_range(PyObject *, void *); /* Renseigne sur les propriétés particulières liées à une ligne. */ static PyObject *py_buffer_line_get_flags(PyObject *, void *); /* Définit les constantes pour les lignes de tampon. */ static bool py_buffer_line_define_constants(PyTypeObject *); /****************************************************************************** * * * Paramètres : self = classe représentant une ligne de tampon. * * args = arguments fournis à l'appel. * * * * Description : Reconstruit et fournit le texte présent sur une ligne tampon.* * * * Retour : Texte reconstruit pour l'occasion. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_buffer_line_get_text(PyObject *self, PyObject *args) { PyObject *result; /* Trouvailles à retourner */ BufferLineColumn first; /* Première colonne à parcourir*/ BufferLineColumn end; /* Dernière colonne à parcourir*/ int markup; /* Besoin de décorations ? */ int ret; /* Bilan de lecture des args. */ GBufferLine *line; /* Version native */ char *text; /* Texte reconstruit à libérer */ ret = PyArg_ParseTuple(args, "IIp", &first, &end, &markup); if (!ret) return NULL; if (first >= BLC_COUNT || end >= BLC_COUNT) { PyErr_SetString(PyExc_ValueError, _("Invalid range in arguments")); return NULL; } line = G_BUFFER_LINE(pygobject_get(self)); text = g_buffer_line_get_text(line, first, end, markup); result = PyUnicode_FromString(text); free(text); return result; } /****************************************************************************** * * * Paramètres : self = classe représentant une ligne de tampon. * * args = arguments fournis à l'appel. * * * * Description : Ajoute une propriété particulière à une ligne donnée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_buffer_line_add_flag(PyObject *self, PyObject *args) { BufferLineFlags flag; /* Drapeau à considérer */ int ret; /* Bilan de lecture des args. */ GBufferLine *line; /* Version native */ ret = PyArg_ParseTuple(args, "I", &flag); if (!ret) return NULL; if ((flag & ~BLF_ALL) != 0) { PyErr_SetString(PyExc_ValueError, _("Invalid flag")); return NULL; } line = G_BUFFER_LINE(pygobject_get(self)); g_buffer_line_add_flag(line, flag); Py_RETURN_NONE; } /****************************************************************************** * * * Paramètres : self = classe représentant une ligne de tampon. * * args = arguments fournis à l'appel. * * * * Description : Retire une propriété particulière à une ligne donnée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_buffer_line_remove_flag(PyObject *self, PyObject *args) { BufferLineFlags flag; /* Drapeau à considérer */ int ret; /* Bilan de lecture des args. */ GBufferLine *line; /* Version native */ ret = PyArg_ParseTuple(args, "I", &flag); if (!ret) return NULL; if ((flag & ~BLF_ALL) != 0) { PyErr_SetString(PyExc_ValueError, _("Invalid flag")); return NULL; } line = G_BUFFER_LINE(pygobject_get(self)); g_buffer_line_remove_flag(line, flag); Py_RETURN_NONE; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Indique la zone mémoire où se situe la ligne. * * * * Retour : Emplacement mémoire virtuel ou physique. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_buffer_line_get_range(PyObject *self, void *closure) { PyObject *result; /* Valeur à retourner */ GBufferLine *line; /* Elément à consulter */ const mrange_t *range; /* Couverture courante */ line = G_BUFFER_LINE(pygobject_get(self)); range = g_buffer_line_get_range(line); result = build_from_internal_mrange(range); return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Renseigne sur les propriétés particulières liées à une ligne.* * * * Retour : Propriétés intégrées. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_buffer_line_get_flags(PyObject *self, void *closure) { PyObject *result; /* Valeur à retourner */ GBufferLine *line; /* Elément à consulter */ BufferLineFlags flags; /* Drapeaux à exporter */ line = G_BUFFER_LINE(pygobject_get(self)); flags = g_buffer_line_get_flags(line); result = PyLong_FromLong(flags); return result; } /****************************************************************************** * * * Paramètres : obj_type = type dont le dictionnaire est à compléter. * * * * Description : Définit les constantes pour les lignes de tampon. * * * * Retour : true en cas de succès de l'opération, false sinon. * * * * Remarques : - * * * ******************************************************************************/ static bool py_buffer_line_define_constants(PyTypeObject *obj_type) { bool result; /* Bilan à retourner */ result = true; result &= PyDict_AddIntMacro(obj_type, BLC_PHYSICAL); result &= PyDict_AddIntMacro(obj_type, BLC_VIRTUAL); result &= PyDict_AddIntMacro(obj_type, BLC_BINARY); result &= PyDict_AddIntMacro(obj_type, BLC_ASSEMBLY_HEAD); result &= PyDict_AddIntMacro(obj_type, BLC_ASSEMBLY); result &= PyDict_AddIntMacro(obj_type, BLC_COMMENTS); result &= PyDict_AddIntMacro(obj_type, BLC_COUNT); result &= PyDict_AddIntMacro(obj_type, BLC_LAST_USED); result &= PyDict_AddIntMacro(obj_type, BLC_INVALID); result &= PyDict_AddIntMacro(obj_type, BLC_MAIN); result &= PyDict_AddIntMacro(obj_type, BLC_FIRST); result &= PyDict_AddIntMacro(obj_type, BLC_DISPLAY); result &= PyDict_AddIntMacro(obj_type, BLF_NONE); result &= PyDict_AddIntMacro(obj_type, BLF_HAS_CODE); result &= PyDict_AddIntMacro(obj_type, BLF_ENTRYPOINT); result &= PyDict_AddIntMacro(obj_type, BLF_BOOKMARK); 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_buffer_line_type(void) { static PyMethodDef py_buffer_line_methods[] = { { "get_text", py_buffer_line_get_text, METH_VARARGS, "get_text($self, first_col, last_col, markup, /)\n--\n\nProvide the text of a buffer line." }, { "add_flag", py_buffer_line_add_flag, METH_VARARGS, "add_flag($self, flag, /)\n--\n\nAdd a flag to the buffer line." }, { "remove_flag", py_buffer_line_remove_flag, METH_VARARGS, "remove_flag($self, flag, /)\n--\n\nRemove a flag from the buffer line." }, { NULL } }; static PyGetSetDef py_buffer_line_getseters[] = { { "range", py_buffer_line_get_range, NULL, "Range covered by the line.", NULL }, { "flags", py_buffer_line_get_flags, NULL, "Current flags of the buffer line.", NULL }, { NULL } }; static PyTypeObject py_buffer_line_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.glibext.BufferLine", .tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT, .tp_doc = "PyChrysalide buffer line", .tp_methods = py_buffer_line_methods, .tp_getset = py_buffer_line_getseters, }; return &py_buffer_line_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.glibext.BufferLine'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_buffer_line_is_registered(void) { PyTypeObject *type; /* Type Python 'BufferLine' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ type = get_python_buffer_line_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { module = get_access_to_python_module("pychrysalide.glibext"); dict = PyModule_GetDict(module); if (!register_class_for_pygobject(dict, G_TYPE_BUFFER_LINE, type, &PyGObject_Type)) return false; if (!py_buffer_line_define_constants(type)) return false; } return true; }