/* Chrysalide - Outil d'analyse de fichiers binaires * comment.c - équivalent Python du fichier "analysis/db/items/comment.c" * * Copyright (C) 2014-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 "comment.h" #include #include #include #include #include "../item.h" #include "../../../access.h" #include "../../../helpers.h" #include "../../../arch/vmpa.h" /* Crée un nouvel objet Python de type 'DbComment'. */ static PyObject *py_db_comment_new(PyTypeObject *, PyObject *, PyObject *); /* Associe un contenu statique supplémentaire à un commentaire. */ static PyObject *py_db_comment_add_text(PyObject *, PyObject *); /* Fournit le commentaire associé à un commentaire. */ static PyObject *py_db_comment_get_text(PyObject *, void *); /****************************************************************************** * * * Paramètres : type = type de l'objet à instancier. * * args = arguments fournis à l'appel. * * kwds = arguments de type key=val fournis. * * * * Description : Crée un nouvel objet Python de type 'DbComment'. * * * * Retour : Instance Python mise en place. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_db_comment_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *result; /* Instance à retourner */ int repeatable; /* Note une répétition demandée*/ const char *text; /* Eventuel premier commentaire*/ int before; /* Indication sur l'emplacement*/ vmpa2t *addr; /* Emplacement ciblé */ unsigned long flags; /* Identifiants de ligne visée */ int ret; /* Bilan de lecture des args. */ GDbComment *comment; /* Version GLib du commentaire */ static char *kwlist[] = { "addr", "flags", "repeatable", "text", "before", NULL }; /* Récupération des paramètres */ repeatable = -1; text = NULL; before = -1; ret = PyArg_ParseTupleAndKeywords(args, kwds, "O&k|$psp", kwlist, convert_any_to_vmpa, &addr, &flags, &repeatable, &text, &before); if (!ret) return NULL; /* Vérifications diverses */ if (flags > BLF_ALL) { clean_vmpa_arg(addr); PyErr_SetString(PyExc_ValueError, _("Invalid flag combination")); return NULL; } if ((repeatable == -1 && before == -1) || (repeatable != -1 && before != -1)) { clean_vmpa_arg(addr); PyErr_SetString(PyExc_ValueError, _("repeatable or before has to be defined")); return NULL; } /* Construction */ if (repeatable) comment = g_db_comment_new_inlined(addr, flags, repeatable); else comment = g_db_comment_new_area(addr, flags, text, before); result = pygobject_new(G_OBJECT(comment)); g_object_unref(comment); clean_vmpa_arg(addr); return result; } /****************************************************************************** * * * Paramètres : self = classe représentant un commentaire. * * args = arguments fournis à l'appel. * * * * Description : Associe un contenu statique supplémentaire à un commentaire. * * * * Retour : None. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_db_comment_add_text(PyObject *self, PyObject *args) { const char *text; /* Commentaire complémentaire */ int ret; /* Bilan de lecture des args. */ GDbComment *comment; /* Commentaire à consulter */ ret = PyArg_ParseTuple(args, "s", &text); if (!ret) return NULL; comment = G_DB_COMMENT(pygobject_get(self)); g_db_comment_add_static_text(comment, text); Py_RETURN_NONE; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit le commentaire associé à un commentaire. * * * * Retour : Texte manipulable en Python. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_db_comment_get_text(PyObject *self, void *closure) { PyObject *result; /* Résultat à retourner */ GDbComment *comment; /* Commentaire à consulter */ char *text; /* Contenu textuel associé */ comment = G_DB_COMMENT(pygobject_get(self)); text = g_db_comment_get_text(comment); if (text == NULL) { result = Py_None; Py_INCREF(result); } else { result = PyUnicode_FromString(text); free(text); } 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_db_comment_type(void) { static PyMethodDef py_db_comment_methods[] = { { "add_text", py_db_comment_add_text, METH_VARARGS, "add_text($self, text, /)\n--\n\nAppend extra text to a given comment." }, { NULL } }; static PyGetSetDef py_db_comment_getseters[] = { { "text", py_db_comment_get_text, NULL, "Give access to the content of a given comment.", NULL }, { NULL } }; static PyTypeObject py_db_comment_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.analysis.db.items.DbComment", .tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT, .tp_doc = "PyChrysalide comment for edited binary", .tp_methods = py_db_comment_methods, .tp_getset = py_db_comment_getseters, .tp_new = py_db_comment_new }; return &py_db_comment_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide....db.items.DbComment'.* * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_db_comment_is_registered(void) { PyTypeObject *type; /* Type Python 'DbComment' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ type = get_python_db_comment_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { module = get_access_to_python_module("pychrysalide.analysis.db.items"); dict = PyModule_GetDict(module); if (!ensure_python_db_item_is_registered()) return false; if (!register_class_for_pygobject(dict, G_TYPE_DB_COMMENT, type, get_python_db_item_type())) return false; } return true; }