diff options
Diffstat (limited to 'plugins/pychrysalide/core/processors.c')
-rw-r--r-- | plugins/pychrysalide/core/processors.c | 248 |
1 files changed, 248 insertions, 0 deletions
diff --git a/plugins/pychrysalide/core/processors.c b/plugins/pychrysalide/core/processors.c new file mode 100644 index 0000000..341c816 --- /dev/null +++ b/plugins/pychrysalide/core/processors.c @@ -0,0 +1,248 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * processors.c - équivalent Python du fichier "core/processors.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 "processors.h" + + +#include <pygobject.h> + + +#include <i18n.h> +#include <arch/processor.h> +#include <core/processors.h> + + +#include "../access.h" +#include "../helpers.h" +#include "../pychrysa.h" +#include "../arch/processor.h" + + + +/* Enregistre un processeur pour une architecture donnée. */ +static PyObject *py_processors_register_type(PyObject *, PyObject *); + +/* Fournit le nom humain de l'architecture visée. */ +static PyObject *py_processors_get_description(PyObject *, PyObject *); + +/* Fournit le processeur d'architecture correspondant à un nom. */ +static PyObject *py_processors_get_for_name(PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* args = arguments fournis à l'appel. * +* * +* Description : Enregistre un processeur pour une architecture donnée. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_processors_register_type(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + const char *name; /* Nom technique de processeur */ + const char *desc; /* description humaine liée */ + PyObject *type; /* Type d'une instance future */ + int ret; /* Bilan de lecture des args. */ + PyObject *new_args; /* Nouveaux arguments épurés */ + PyObject *new_kwds; /* Nouveau dictionnaire épuré */ + PyObject *dummy; /* Coquille vide pour analyse */ + GType instance; /* Type pour futures instances */ + bool status; /* Bilan d'un enregistrement */ + + ret = PyArg_ParseTuple(args, "ssO!", &name, &desc, &PyType_Type, &type); + if (!ret) return NULL; + + ret = PyObject_IsSubclass(type, (PyObject *)get_python_arch_processor_type());; + if (ret == -1) return NULL; + + if (ret != 1) + { + PyErr_SetString(PyExc_TypeError, _("The new processor should be a subclass of the ArchProcessor type.")); + return NULL; + } + + /** + * Comme le type GLib n'est initié et enregistré qu'à la création d'une première instance, + * on force sa mise en place ici, afin de ne pas contraidre l'utilisateur à le faire + * lui même via un appel du style : + * + * register_processor('aaa', 'BBB CCC', type(AaaProcessor())) + */ + + new_args = PyTuple_New(0); + new_kwds = PyDict_New(); + + dummy = PyObject_Call(type, new_args, new_kwds); + + Py_DECREF(new_kwds); + Py_DECREF(new_args); + + if (dummy == NULL) return NULL; + + instance = pyg_type_from_object_strict((PyObject *)Py_TYPE(dummy), TRUE); + assert(instance != 0 && instance != G_TYPE_ARCH_PROCESSOR); + + Py_DECREF(dummy); + + status = register_processor_type(name, desc, instance); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* args = arguments fournis à l'appel. * +* * +* Description : Fournit le nom humain de l'architecture visée. * +* * +* Retour : Désignation humaine trouvée ou NULL. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_processors_get_description(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + const char *name; /* Nom technique de processeur */ + int ret; /* Bilan de lecture des args. */ + const char *desc; /* Description humaine obtenue */ + + ret = PyArg_ParseTuple(args, "s", &name); + if (!ret) return NULL; + + desc = get_arch_processor_description(name); + + if (desc != NULL) + result = PyUnicode_FromString(desc); + + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* args = arguments fournis à l'appel. * +* * +* Description : Fournit le processeur d'architecture correspondant à un nom. * +* * +* Retour : Processeur d'architecture trouvé. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_processors_get_for_name(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + const char *name; /* Nom technique de processeur */ + int ret; /* Bilan de lecture des args. */ + GArchProcessor *proc; /* Instance mise en place */ + + ret = PyArg_ParseTuple(args, "s", &name); + if (!ret) return NULL; + + proc = get_arch_processor_for_name(name); + + if (proc != NULL) + { + result = pygobject_new(G_OBJECT(proc)); + g_object_unref(G_OBJECT(proc)); + } + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Définit une extension du module 'core' à compléter. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool populate_core_module_with_processors(void) +{ + bool result; /* Bilan à retourner */ + PyObject *module; /* Module à recompléter */ + + static PyMethodDef py_processors_methods[] = { + { + "register_processor", py_processors_register_type, + METH_VARARGS, + "register_type(name, desc, cls, /)\n--\n\nRegister an an architecture processor by its name, description and class type." + }, + { + "get_processor_description", py_processors_get_description, + METH_VARARGS, + "get_processor_description(name, /)\n--\n\nProvide the description of a given architecture processor." + }, + { + "get_processor_for_name", py_processors_get_for_name, + METH_VARARGS, + "get_processor_for_name(name, /)\n--\n\nProvide an instance of an architecture processor for a given name." + }, + { NULL } + + }; + + module = get_access_to_python_module("pychrysalide.core"); + + result = register_python_module_methods(module, py_processors_methods); + + return result; + +} |