/* Chrysalide - Outil d'analyse de fichiers binaires * proto.c - équivalent Python du fichier "analysis/types/proto.c" * * Copyright (C) 2018 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 "proto.h" #include #include #include #include "../type.h" #include "../../access.h" #include "../../helpers.h" /* Crée un nouvel objet Python de type 'ProtoType'. */ static PyObject *py_proto_type_new(PyTypeObject *, PyObject *, PyObject *); /* Ajoute un argument à un prototype. */ static PyObject *py_proto_type_add_arg(PyObject *, PyObject *); /* Fournit le type de retour d'un prototype. */ static PyObject *py_proto_type_get_return_type(PyObject *, void *); /* Définit le type de retour d'un prototype. */ static int py_proto_type_set_return_type(PyObject *, PyObject *, void *); /* Fournit les arguments du prototype. */ static PyObject *py_proto_type_get_args(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 'ProtoType'. * * * * Retour : Instance Python mise en place. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_proto_type_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *result; /* Instance à retourner */ GDataType *dtype; /* Version GLib du type */ dtype = g_proto_type_new(); result = pygobject_new(G_OBJECT(dtype)); g_object_unref(dtype); return result; } /****************************************************************************** * * * Paramètres : self = projet d'étude à manipuler. * * args = arguments accompagnant l'appel. * * * * Description : Ajoute un argument à un prototype. * * * * Retour : Py_None. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_proto_type_add_arg(PyObject *self, PyObject *args) { PyObject *arg_obj; /* Objet du type d'argument */ int ret; /* Bilan de lecture des args. */ GDataType *arg; /* Version GLib du type */ GProtoType *type; /* Version GLib du type */ ret = PyArg_ParseTuple(args, "O!", get_python_data_type_type(), &arg_obj); if (!ret) return NULL; type = G_PROTO_TYPE(pygobject_get(self)); arg = G_DATA_TYPE(pygobject_get(arg_obj)); g_object_ref(G_OBJECT(arg)); g_proto_type_add_arg(type, arg); Py_RETURN_NONE; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit le type de retour d'un prototype. * * * * Retour : Indication sur le type de retour en place. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_proto_type_get_return_type(PyObject *self, void *closure) { PyObject *result; /* Résultat à retourner */ GProtoType *type; /* Version GLib du type */ GDataType *ret; /* Type de retour du prototype */ type = G_PROTO_TYPE(pygobject_get(self)); ret = g_proto_type_get_return_type(type); result = pygobject_new(G_OBJECT(ret)); g_object_unref(ret); return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * value = valeur fournie à intégrer ou prendre en compte. * * closure = non utilisé ici. * * * * Description : Définit le type de retour d'un prototype. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static int py_proto_type_set_return_type(PyObject *self, PyObject *value, void *closure) { GProtoType *type; /* Version GLib du type */ GDataType *ret; /* Type de retour du prototype */ if (!PyObject_IsInstance(value, (PyObject *)get_python_data_type_type())) { PyErr_SetString(PyExc_TypeError, _("The attribute value must be a GDataType.")); return -1; } type = G_PROTO_TYPE(pygobject_get(self)); ret = G_DATA_TYPE(pygobject_get(value)); g_object_ref(ret); g_proto_type_set_return_type(type, ret); return 0; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit les arguments du prototype. * * * * Retour : Liste de types d'arguments. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_proto_type_get_args(PyObject *self, void *closure) { PyObject *result; /* Résultat à retourner */ GProtoType *type; /* Version GLib du type */ size_t count; /* Nombre d'arguments */ size_t i; /* Boucle de parcours */ GDataType *arg; /* Argument du prototype */ type = G_PROTO_TYPE(pygobject_get(self)); count = g_proto_type_count_args(type); result = PyTuple_New(count); for (i = 0; i < count; i++) { arg = g_proto_type_get_arg(type, i); PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(arg))); g_object_unref(arg); } 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_proto_type_type(void) { static PyMethodDef py_proto_type_methods[] = { { "add_arg", py_proto_type_add_arg, METH_VARARGS, "add_arg($self, type, /)\n--\n\nAdd an extra argument to the prototype." }, { NULL } }; static PyGetSetDef py_proto_type_getseters[] = { { "return", py_proto_type_get_return_type, py_proto_type_set_return_type, "Provide the return type of the prototype.", NULL }, { "args", py_proto_type_get_args, NULL, "Give all arguments of the prototype.", NULL }, { NULL } }; static PyTypeObject py_proto_type_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.analysis.types.ProtoType", .tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT, .tp_doc = "PyChrysalide proto type", .tp_methods = py_proto_type_methods, .tp_getset = py_proto_type_getseters, .tp_new = py_proto_type_new }; return &py_proto_type_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.....types.ProtoType'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_proto_type_is_registered(void) { PyTypeObject *type; /* Type Python 'ProtoType' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ type = get_python_proto_type_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { module = get_access_to_python_module("pychrysalide.analysis.types"); dict = PyModule_GetDict(module); if (!ensure_python_data_type_is_registered()) return false; if (!register_class_for_pygobject(dict, G_TYPE_PROTO_TYPE, type, get_python_data_type_type())) return false; } return true; }