/* Chrysalide - Outil d'analyse de fichiers binaires * linegen.c - équivalent Python du fichier "glibext/linegen.h" * * Copyright (C) 2018-2019 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 "linegen.h" #include #include #include #include "../access.h" #include "../helpers.h" #include "../analysis/content.h" #include "../arch/vmpa.h" #include "../glibext/bufferline.h" /* Indique le nombre de ligne prêtes à être générées. */ static PyObject *py_line_generator_count_lines(PyObject *, PyObject *); /* Retrouve l'emplacement correspondant à une position donnée. */ //static PyObject *py_line_generator_compute_addr(PyObject *, PyObject *); /* Détermine si le conteneur s'inscrit dans une plage donnée. */ //static PyObject *py_line_generator_contains_addr(PyObject *, PyObject *); /* Renseigne sur les propriétés liées à un générateur. */ static PyObject *py_line_generator_get_flags(PyObject *, PyObject *); /* Imprime dans une ligne de rendu le contenu représenté. */ static PyObject *py_line_generator_print(PyObject *, PyObject *); /****************************************************************************** * * * Paramètres : self = classe représentant un générateur à manipuler. * * args = arguments fournis à l'appel. * * * * Description : Indique le nombre de ligne prêtes à être générées. * * * * Retour : Nombre de lignes devant apparaître au final. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_line_generator_count_lines(PyObject *self, PyObject *args) { PyObject *result; /* Décompte à retourner */ GLineGenerator *generator; /* Version native */ size_t count; /* Nombre de lignes présentes */ generator = G_LINE_GENERATOR(pygobject_get(self)); count = g_line_generator_count_lines(generator); result = Py_BuildValue("k", count); return result; } /****************************************************************************** * * * Paramètres : self = classe représentant un générateur à manipuler. * * args = arguments fournis à l'appel. * * * * Description : Retrouve l'emplacement correspondant à une position donnée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ #if 0 static PyObject *py_line_generator_compute_addr(PyObject *self, PyObject *args) { PyObject *result; /* Localisation à retourner */ GLineGenerator *generator; /* Version native */ gint x; /* Position géographique */ size_t index; /* Indice dans le tampon */ size_t repeat; /* Utilisations successives */ int ret; /* Bilan de lecture des args. */ vmpa2t addr; /* Adresse visée par l'opérat° */ generator = G_LINE_GENERATOR(pygobject_get(self)); ret = PyArg_ParseTuple(args, "ikk", &x, &index, &repeat); if (!ret) return NULL; g_line_generator_compute_addr(generator, x, &addr, index, repeat); result = build_from_internal_vmpa(&addr); return result; } #endif /****************************************************************************** * * * Paramètres : self = classe représentant un générateur à manipuler. * * args = arguments fournis à l'appel. * * * * Description : Détermine si le conteneur s'inscrit dans une plage donnée. * * * * Retour : Bilan de la détermination, utilisable en comparaisons. * * * * Remarques : - * * * ******************************************************************************/ #if 0 static PyObject *py_line_generator_contains_addr(PyObject *self, PyObject *args) { GLineGenerator *generator; /* Version native */ PyObject *py_vmpa; /* Localisation version Python */ size_t index; /* Indice dans le tampon */ size_t repeat; /* Utilisations successives */ int ret; /* Bilan de lecture des args. */ vmpa2t *addr; /* Adresse visée par l'opérat° */ generator = G_LINE_GENERATOR(pygobject_get(self)); ret = PyArg_ParseTuple(args, "O!kk", get_python_vmpa_type(), &py_vmpa, &index, &repeat); if (!ret) return NULL; addr = get_internal_vmpa(py_vmpa); if (addr == NULL) return NULL; g_line_generator_contains_addr(generator, addr, index, repeat); Py_RETURN_NONE; } #endif /****************************************************************************** * * * Paramètres : self = classe représentant un générateur à manipuler. * * args = arguments fournis à l'appel. * * * * Description : Renseigne sur les propriétés liées à un générateur. * * * * Retour : Propriétés particulières associées. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_line_generator_get_flags(PyObject *self, PyObject *args) { PyObject *result; /* Propriétés à retourner */ GLineGenerator *generator; /* Version native */ size_t index; /* Indice dans le tampon */ size_t repeat; /* Utilisations successives */ int ret; /* Bilan de lecture des args. */ BufferLineFlags flags; /* Propriétés courantes */ generator = G_LINE_GENERATOR(pygobject_get(self)); ret = PyArg_ParseTuple(args, "kk", &index, &repeat); if (!ret) return NULL; flags = g_line_generator_get_flags(generator, index, repeat); result = Py_BuildValue("I", flags); return result; } /****************************************************************************** * * * Paramètres : self = classe représentant un générateur à manipuler. * * args = arguments fournis à l'appel. * * * * Description : Imprime dans une ligne de rendu le contenu représenté. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_line_generator_print(PyObject *self, PyObject *args) { GLineGenerator *generator; /* Version native */ GBufferLine *line; /* Ligne de rendu à compléter */ size_t index; /* Indice dans le tampon */ size_t repeat; /* Utilisations successives */ GBinContent *content; /* Contenu binaire associé */ int ret; /* Bilan de lecture des args. */ generator = G_LINE_GENERATOR(pygobject_get(self)); ret = PyArg_ParseTuple(args, "O&kkO&", convert_to_buffer_line, &line, &index, &repeat, convert_to_binary_content, &content); if (!ret) return NULL; g_line_generator_print(generator, line, index, repeat, content); Py_RETURN_NONE; } #if 0 /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void python_line_generator_interface_init(GLineGeneratorIface *iface, PyTypeObject *type) { GLineGeneratorIface *parent; /* Défintion parente */ size_t i; /* Boucle de parcours */ PyObject *method; /* Méthode à associer */ static const char *meth_names[] = { "count_lines", "compute_addr", "contains_addr", "get_flags", "print" }; parent = g_type_interface_peek_parent(iface); for (i = 0; i < ARRAY_SIZE(meth_names); i++) { method = NULL; if (type != NULL) method = PyObject_GetAttrString((PyObject *)type, meth_names[i]); if (method != NULL && PyObject_TypeCheck(method, &PyCFunction_Type) == 0) /*iface->iface_method = _wrap_TestInterface__proxy_do_iface_method*/; else { PyErr_Clear(); if (parent != NULL) /*iface->iface_method = parent->iface_method*/; } Py_XDECREF(method); } } #endif /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_line_generator_type(void) { static PyMethodDef py_line_generator_methods[] = { { "count_lines", py_line_generator_count_lines, METH_NOARGS, "count_lines($self, /)\n--\n\nCount the number of lines which can be displayed." }, #if 0 { "compute_addr", py_line_generator_compute_addr, METH_VARARGS, "compute_addr($self, x, index, repeat, /)\n--\n\nReturn the position at a given location." }, { "contains_addr", py_line_generator_contains_addr, METH_VARARGS, "contains_addr($self, addr, index, repeat, /)\n--\n\nTell if the generator contains an address." }, #endif { "get_flags", py_line_generator_get_flags, METH_VARARGS, "get_flags($self, index, repeat, /)\n--\n\nGet the flags of a position from the generator." }, { "print", py_line_generator_print, METH_VARARGS, "print($self, line, index, repeat, content, /)\n--\n\nProduce output into a line from content." }, { NULL } }; static PyGetSetDef py_line_generator_getseters[] = { { NULL } }; static PyTypeObject py_line_generator_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.glibext.LineGenerator", //.tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_doc = "PyChrysalide line content generator", .tp_methods = py_line_generator_methods, .tp_getset = py_line_generator_getseters, }; return &py_line_generator_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.glibext.LineGenerator'.* * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_line_generator_is_registered(void) { PyTypeObject *type; /* Type Python 'LineGenerator' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ type = get_python_line_generator_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { module = get_access_to_python_module("pychrysalide.glibext"); dict = PyModule_GetDict(module); if (!register_interface_for_pygobject(dict, G_TYPE_LINE_GENERATOR, type)) return false; } return true; }