diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2018-04-21 22:00:00 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2018-04-21 22:00:00 (GMT) |
commit | 8eb95d316f7b6fbad0ff798abfe7f70f89e812d2 (patch) | |
tree | 4f310c7ffdb94d48fff236e63c7e6f0ed9f1dee1 /plugins/pychrysalide | |
parent | 315146a49b5570294ca20beca720c4e3f74a86bd (diff) |
Improved the way file formats are detected and loaded.
Diffstat (limited to 'plugins/pychrysalide')
-rw-r--r-- | plugins/pychrysalide/analysis/Makefile.am | 1 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/binary.c | 91 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/loaded.c | 77 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/loading.c | 196 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/loading.h | 56 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/module.c | 3 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/project.c | 93 | ||||
-rw-r--r-- | plugins/pychrysalide/core/Makefile.am | 1 | ||||
-rw-r--r-- | plugins/pychrysalide/core/global.c | 280 | ||||
-rw-r--r-- | plugins/pychrysalide/core/global.h | 42 | ||||
-rw-r--r-- | plugins/pychrysalide/core/module.c | 2 | ||||
-rw-r--r-- | plugins/pychrysalide/format/executable.c | 2 | ||||
-rw-r--r-- | plugins/pychrysalide/plugin.c | 66 |
13 files changed, 821 insertions, 89 deletions
diff --git a/plugins/pychrysalide/analysis/Makefile.am b/plugins/pychrysalide/analysis/Makefile.am index 5c3c46c..61d2c94 100644 --- a/plugins/pychrysalide/analysis/Makefile.am +++ b/plugins/pychrysalide/analysis/Makefile.am @@ -6,6 +6,7 @@ libpychrysaanalysis_la_SOURCES = \ block.h block.c \ content.h content.c \ loaded.h loaded.c \ + loading.h loading.c \ module.h module.c \ project.h project.c \ routine.h routine.c \ diff --git a/plugins/pychrysalide/analysis/binary.c b/plugins/pychrysalide/analysis/binary.c index 6be767c..1ca5b3c 100644 --- a/plugins/pychrysalide/analysis/binary.c +++ b/plugins/pychrysalide/analysis/binary.c @@ -34,8 +34,8 @@ #include <analysis/binary.h> -#include "content.h" #include "../helpers.h" +#include "../format/executable.h" @@ -45,12 +45,6 @@ static PyObject *py_loaded_binary_new(PyTypeObject *, PyObject *, PyObject *); /* Fournit le nom associé à l'élément binaire. */ static PyObject *py_loaded_binary_get_name(PyObject *, void *); -/* Lance l'analyse d'un élément binaire chargé. */ -static PyObject *py_loaded_binary_analyse(PyObject *, PyObject *); - -/* Lance l'analyse d'un binaire chargé et attend sa conclusion. */ -static PyObject *py_loaded_binary_analyse_and_wait(PyObject *, PyObject *); - /* Fournit le format de fichier reconnu dans le contenu binaire. */ static PyObject *py_loaded_binary_get_format(PyObject *, void *); @@ -79,23 +73,18 @@ static PyObject *py_loaded_binary_get_disassembled_cache(PyObject *, void *); static PyObject *py_loaded_binary_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *result; /* Instance à retourner */ - PyObject *content_obj; /* Objet pour le contenu */ + PyObject *format_obj; /* Objet pour le contenu */ int ret; /* Bilan de lecture des args. */ - GBinContent *content; /* Instance GLib correspondante*/ - GLoadedBinary *binary; /* Version GLib du format */ + GExeFormat *format; /* Instance GLib correspondante*/ + GLoadedContent *binary; /* Version GLib du binaire */ - ret = PyArg_ParseTuple(args, "O", &content_obj); + ret = PyArg_ParseTuple(args, "O!", get_python_executable_format_type(), &format_obj); if (!ret) return NULL; - ret = PyObject_IsInstance(content_obj, (PyObject *)get_python_binary_content_type()); - if (ret == 0) - { - PyErr_SetString(PyExc_TypeError, _("Expected a BinContent as argument")); - return NULL; - } + format = G_EXE_FORMAT(pygobject_get(format_obj)); - content = G_BIN_CONTENT(pygobject_get(content_obj)); - binary = g_loaded_binary_new(content); + g_object_ref(G_OBJECT(format)); + binary = g_loaded_binary_new(format); result = pygobject_new(G_OBJECT(binary)); @@ -138,58 +127,6 @@ static PyObject *py_loaded_binary_get_name(PyObject *self, void *closure) /****************************************************************************** * * -* Paramètres : self = contenu binaire à manipuler. * -* args = non utilisé ici. * -* * -* Description : Lance l'analyse d'un élément binaire chargé. * -* * -* Retour : Rien (None). * -* * -* Remarques : - * -* * -******************************************************************************/ - -static PyObject *py_loaded_binary_analyse(PyObject *self, PyObject *args) -{ - GLoadedBinary *binary; /* Version GLib du format */ - - binary = G_LOADED_BINARY(pygobject_get(self)); - - g_loaded_binary_analyse(binary); - - Py_RETURN_NONE; - -} - - -/****************************************************************************** -* * -* Paramètres : self = contenu binaire à manipuler. * -* args = non utilisé ici. * -* * -* Description : Lance l'analyse d'un binaire chargé et attend sa conclusion. * -* * -* Retour : Rien (None). * -* * -* Remarques : - * -* * -******************************************************************************/ - -static PyObject *py_loaded_binary_analyse_and_wait(PyObject *self, PyObject *args) -{ - GLoadedBinary *binary; /* Version GLib du format */ - - binary = G_LOADED_BINARY(pygobject_get(self)); - - g_loaded_binary_analyse_and_wait(binary); - - Py_RETURN_NONE; - -} - - -/****************************************************************************** -* * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * @@ -296,18 +233,6 @@ static PyObject *py_loaded_binary_get_disassembled_cache(PyObject *self, void *c PyTypeObject *get_python_loaded_binary_type(void) { static PyMethodDef py_loaded_binary_methods[] = { - { - "analyse", py_loaded_binary_analyse, - METH_NOARGS, - "analyse(/)\n--\n\nStart the analysis of the loaded binary and " \ - "send a \"disassembly-done\" signal when done." - }, - { - "analyse_and_wait", py_loaded_binary_analyse_and_wait, - METH_NOARGS, - "analyse_and_wait(/)\n--\n\nRun the analysis of the loaded binary and " \ - "wait for its completion." - }, { NULL } }; diff --git a/plugins/pychrysalide/analysis/loaded.c b/plugins/pychrysalide/analysis/loaded.c index b38025a..328bf7b 100644 --- a/plugins/pychrysalide/analysis/loaded.c +++ b/plugins/pychrysalide/analysis/loaded.c @@ -36,6 +36,12 @@ +/* Lance l'analyse propre à l'élément chargé. */ +static PyObject *py_loaded_content_analyze(PyObject *, PyObject *); + +/* Lance l'analyse de l'élément chargé et attend sa conclusion. */ +static PyObject *py_loaded_content_analyze_and_wait(PyObject *, PyObject *); + /* Détermine le nombre de vues disponibles pour un contenu. */ static PyObject *py_loaded_content_count_views(PyObject *, PyObject *); @@ -43,6 +49,63 @@ static PyObject *py_loaded_content_count_views(PyObject *, PyObject *); /****************************************************************************** * * +* Paramètres : self = contenu binaire à manipuler. * +* args = non utilisé ici. * +* * +* Description : Lance l'analyse propre à l'élément chargé. * +* * +* Retour : Rien (None). * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_loaded_content_analyze(PyObject *self, PyObject *args) +{ + GLoadedContent *content; /* Version GLib de l'élément */ + + content = G_LOADED_CONTENT(pygobject_get(self)); + + g_loaded_content_analyze(content); + + Py_RETURN_NONE; + +} + + +/****************************************************************************** +* * +* Paramètres : self = contenu binaire à manipuler. * +* args = non utilisé ici. * +* * +* Description : Lance l'analyse de l'élément chargé et attend sa conclusion. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_loaded_content_analyze_and_wait(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GLoadedContent *content; /* Version GLib de l'élément */ + bool status; /* Bilan de l'opération */ + + content = G_LOADED_CONTENT(pygobject_get(self)); + + status = g_loaded_content_analyze_and_wait(content); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * * Paramètres : self = contenu chargé à manipuler. * * args = non utilisé ici. * * * @@ -57,7 +120,7 @@ static PyObject *py_loaded_content_count_views(PyObject *, PyObject *); static PyObject *py_loaded_content_count_views(PyObject *self, PyObject *args) { PyObject *result; /* Instance à retourner */ - GLoadedContent *content; /* Version GLib du format */ + GLoadedContent *content; /* Version GLib de l'élément */ size_t count; /* Quantité à retourner */ content = G_LOADED_CONTENT(pygobject_get(self)); @@ -87,6 +150,18 @@ PyTypeObject *get_python_loaded_content_type(void) { static PyMethodDef py_loaded_content_methods[] = { { + "analyze", py_loaded_content_analyze, + METH_NOARGS, + "analyze($self, /)\n--\n\nStart the analysis of the loaded binary and " \ + "send a \"disassembly-done\" signal when done." + }, + { + "analyze_and_wait", py_loaded_content_analyze_and_wait, + METH_NOARGS, + "analyze_and_wait($self, /)\n--\n\nRun the analysis of the loaded binary and " \ + "wait for its completion." + }, + { "count_views", py_loaded_content_count_views, METH_NOARGS, "count_views($self, /)\n--\n\nCompute the quantity of available views." diff --git a/plugins/pychrysalide/analysis/loading.c b/plugins/pychrysalide/analysis/loading.c new file mode 100644 index 0000000..fca9929 --- /dev/null +++ b/plugins/pychrysalide/analysis/loading.c @@ -0,0 +1,196 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * loading.c - équivalent Python du fichier "analysis/loading.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 "loading.h" + + +#include <pygobject.h> + + +#include <analysis/loading.h> + + +#include "../helpers.h" + + + +/* --------------------- EXPLORATION NON BLOQUANTE DES CONTENUS --------------------- */ + + + +/* ------------------- RESOLUTION DE CONTENUS BINAIRES EN CHARGES ------------------- */ + + + +/* ---------------------------------------------------------------------------------- */ +/* EXPLORATION NON BLOQUANTE DES CONTENUS */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_content_explorer_type(void) +{ + static PyMethodDef py_content_explorer_methods[] = { + { NULL } + }; + + static PyGetSetDef py_content_explorer_getseters[] = { + { NULL } + }; + + static PyTypeObject py_content_explorer_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.analysis.ContentExplorer", + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyChrysalide content explorer", + + .tp_methods = py_content_explorer_methods, + .tp_getset = py_content_explorer_getseters + + }; + + return &py_content_explorer_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide...ContentExplorer'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_content_explorer(PyObject *module) +{ + PyTypeObject *py_content_explorer_type; /* Type 'ContentExplorer' */ + PyObject *dict; /* Dictionnaire du module */ + + py_content_explorer_type = get_python_content_explorer_type(); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_CONTENT_EXPLORER, py_content_explorer_type, &PyGObject_Type)) + return false; + + return true; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* RESOLUTION DE CONTENUS BINAIRES EN CHARGES */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_content_resolver_type(void) +{ + static PyMethodDef py_content_resolver_methods[] = { + { NULL } + }; + + static PyGetSetDef py_content_resolver_getseters[] = { + { NULL } + }; + + static PyTypeObject py_content_resolver_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.analysis.ContentResolver", + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyChrysalide content resolver", + + .tp_methods = py_content_resolver_methods, + .tp_getset = py_content_resolver_getseters + + }; + + return &py_content_resolver_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide...ContentResolver'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_content_resolver(PyObject *module) +{ + PyTypeObject *py_content_resolver_type; /* Type 'ContentResolver' */ + PyObject *dict; /* Dictionnaire du module */ + + py_content_resolver_type = get_python_content_resolver_type(); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_CONTENT_RESOLVER, py_content_resolver_type, &PyGObject_Type)) + return false; + + return true; + +} diff --git a/plugins/pychrysalide/analysis/loading.h b/plugins/pychrysalide/analysis/loading.h new file mode 100644 index 0000000..56cdd21 --- /dev/null +++ b/plugins/pychrysalide/analysis/loading.h @@ -0,0 +1,56 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * loading.h - prototypes pour l'équivalent Python du fichier "analysis/loading.h" + * + * 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 + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_ANALYSIS_LOADING_H +#define _PLUGINS_PYCHRYSALIDE_ANALYSIS_LOADING_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* --------------------- EXPLORATION NON BLOQUANTE DES CONTENUS --------------------- */ + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_content_explorer_type(void); + +/* Prend en charge l'objet 'pychrysalide.analysis.ContentExplorer'. */ +bool register_python_content_explorer(PyObject *); + + + +/* ------------------- RESOLUTION DE CONTENUS BINAIRES EN CHARGES ------------------- */ + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_content_resolver_type(void); + +/* Prend en charge l'objet 'pychrysalide.analysis.ContentResolver'. */ +bool register_python_content_resolver(PyObject *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_LOADING_H */ diff --git a/plugins/pychrysalide/analysis/module.c b/plugins/pychrysalide/analysis/module.c index a77121b..c9ab1e5 100644 --- a/plugins/pychrysalide/analysis/module.c +++ b/plugins/pychrysalide/analysis/module.c @@ -32,6 +32,7 @@ #include "block.h" #include "content.h" #include "loaded.h" +#include "loading.h" #include "project.h" #include "routine.h" #include "type.h" @@ -90,6 +91,8 @@ bool add_analysis_module_to_python_module(PyObject *super) result &= register_python_binary_content(module); result &= register_python_loaded_content(module); + result &= register_python_content_explorer(module); + result &= register_python_content_resolver(module); result &= register_python_loaded_binary(module); result &= register_python_instr_block(module); result &= register_python_binary_routine(module); diff --git a/plugins/pychrysalide/analysis/project.c b/plugins/pychrysalide/analysis/project.c index cd7578d..fa7de72 100644 --- a/plugins/pychrysalide/analysis/project.c +++ b/plugins/pychrysalide/analysis/project.c @@ -32,18 +32,66 @@ #include <analysis/project.h> +#include "loaded.h" #include "../helpers.h" +/* Crée un nouvel objet Python de type 'StudyProject'. */ +static PyObject *py_study_project_new(PyTypeObject *, PyObject *, PyObject *); + /* Procède à l'enregistrement d'un projet donné. */ static PyObject *py_study_project_save(PyObject *, PyObject *); +/* Attache un contenu donné à un projet donné. */ +static PyObject *py_study_project_attach_content(PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* 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 'StudyProject'. * +* * +* Retour : Instance Python mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_study_project_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Instance à retourner */ + const char *filename; /* Destination de la sauvegarde*/ + int ret; /* Bilan de lecture des args. */ + GStudyProject *project; /* Version GLib du projet */ + + filename = NULL; + + ret = PyArg_ParseTuple(args, "|s", &filename); + if (!ret) return NULL; + + if (filename != NULL) + project = g_study_project_open(filename); + else + project = g_study_project_new(); + + result = pygobject_new(G_OBJECT(project)); + + if (project != NULL) + g_object_unref(project); + + return result; + +} /****************************************************************************** * * -* Paramètres : self = contenu binaire à manipuler. * +* Paramètres : self = projet d'étude à manipuler. * * args = arguments accompagnant l'appel. * * * * Description : Procède à l'enregistrement d'un projet donné. * @@ -80,6 +128,41 @@ static PyObject *py_study_project_save(PyObject *self, PyObject *args) /****************************************************************************** * * +* Paramètres : self = projet d'étude à manipuler. * +* args = arguments accompagnant l'appel. * +* * +* Description : Attache un contenu donné à un projet donné. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_study_project_attach_content(PyObject *self, PyObject *args) +{ + GStudyProject *project; /* Version GLib du format */ + PyObject *content_obj; /* Objet pour le contenu */ + int ret; /* Bilan de lecture des args. */ + GLoadedContent *content; /* Instance GLib correspondante*/ + + project = G_STUDY_PROJECT(pygobject_get(self)); + assert(project != NULL); + + ret = PyArg_ParseTuple(args, "O!", get_python_loaded_content_type(), &content_obj); + if (!ret) return NULL; + + content = G_LOADED_CONTENT(pygobject_get(content_obj)); + + g_study_project_attach_content(project, content); + + Py_RETURN_NONE; + +} + + +/****************************************************************************** +* * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * @@ -98,6 +181,11 @@ PyTypeObject *get_python_study_project_type(void) METH_VARARGS, "save($self, filename, /)\n--\n\nSave the project into a given file." }, + { + "attach", py_study_project_attach_content, + METH_VARARGS, + "attach($self, loaded, /)\n--\n\nAdd a loaded content to the project." + }, { NULL } }; @@ -116,7 +204,8 @@ PyTypeObject *get_python_study_project_type(void) .tp_doc = "PyChrysalide study project", .tp_methods = py_study_project_methods, - .tp_getset = py_study_project_getseters + .tp_getset = py_study_project_getseters, + .tp_new = py_study_project_new }; diff --git a/plugins/pychrysalide/core/Makefile.am b/plugins/pychrysalide/core/Makefile.am index bc21d77..999674d 100644 --- a/plugins/pychrysalide/core/Makefile.am +++ b/plugins/pychrysalide/core/Makefile.am @@ -4,6 +4,7 @@ noinst_LTLIBRARIES = libpychrysacore.la libpychrysacore_la_SOURCES = \ demanglers.h demanglers.c \ formats.h formats.c \ + global.h global.c \ logs.h logs.c \ module.h module.c \ params.h params.c diff --git a/plugins/pychrysalide/core/global.c b/plugins/pychrysalide/core/global.c new file mode 100644 index 0000000..0fe767f --- /dev/null +++ b/plugins/pychrysalide/core/global.c @@ -0,0 +1,280 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * global.c - équivalent Python du fichier "core/global.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 "global.h" + + +#include <pygobject.h> + + +#include <core/global.h> + + +#include "../helpers.h" +#include "../analysis/project.h" + + + +/* Fournit l'adresse de l'explorateur de contenus courant. */ +static PyObject *py_global_get_content_explorer(PyObject *, void *); + +/* Fournit l'adresse du résolveur de contenus courant. */ +static PyObject *py_global_get_content_resolver(PyObject *, void *); + +/* Fournit l'adresse du projet courant. */ +static PyObject *py_global_get_current_project(PyObject *, void *); + +/* Définit l'adresse du projet courant. */ +static int py_global_set_current_project(PyObject *, PyObject *, void *); + + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit l'adresse de l'explorateur de contenus courant. * +* * +* Retour : Adresse de l'explorateur global ou None si aucun (!). * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_global_get_content_explorer(PyObject *self, void *closure) +{ + PyObject *result; /* Instance Python à retourner */ + GContentExplorer *explorer; /* Gestionnaire natif récupéré */ + + explorer = get_current_content_explorer(); + + if (explorer != NULL) + { + result = pygobject_new(G_OBJECT(explorer)); + g_object_unref(G_OBJECT(explorer)); + } + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit l'adresse du résolveur de contenus courant. * +* * +* Retour : Adresse du résolveur global ou None si aucun (!). * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_global_get_content_resolver(PyObject *self, void *closure) +{ + PyObject *result; /* Instance Python à retourner */ + GContentResolver *resolver; /* Gestionnaire natif récupéré */ + + resolver = get_current_content_resolver(); + + if (resolver != NULL) + { + result = pygobject_new(G_OBJECT(resolver)); + g_object_unref(G_OBJECT(resolver)); + } + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit l'adresse du projet courant. * +* * +* Retour : Adresse du résolveur global ou None si aucun. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_global_get_current_project(PyObject *self, void *closure) +{ + PyObject *result; /* Instance Python à retourner */ + GStudyProject *project; /* Projet courant récupéré */ + + project = get_current_project(); + + if (project != NULL) + { + result = pygobject_new(G_OBJECT(project)); + g_object_unref(G_OBJECT(project)); + } + else + { + result = Py_None; + Py_INCREF(result); + } + + printf("result: %p (project=%p)\n", result, project); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* value = valeur fournie à intégrer ou prendre en compte. * +* closure = adresse non utilisée ici. * +* * +* Description : Définit l'adresse du projet courant. * +* * +* Retour : Bilan de l'opération pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_global_set_current_project(PyObject *self, PyObject *value, void *closure) +{ + int ret; /* Bilan d'analyse */ + GStudyProject *project; /* Version GLib du format */ + + ret = PyObject_IsInstance(value, (PyObject *)get_python_study_project_type()); + if (!ret) return -1; + + project = G_STUDY_PROJECT(pygobject_get(value)); + + g_object_ref(G_OBJECT(project)); + + set_current_project(project); + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_global_type(void) +{ + static PyMethodDef py_global_methods[] = { + { NULL } + }; + + static PyGetSetDef py_global_getseters[] = { + { + "content_explorer", py_global_get_content_explorer, NULL, + "Get the global exploration manager discovering contents.", NULL + }, + { + "content_resolver", py_global_get_content_resolver, NULL, + "Get the global resolution manager translating binary contents into loaded contents.", NULL + }, + { + "current_project", py_global_get_current_project, py_global_set_current_project, + "Get or set the current global project.", NULL + }, + { NULL } + }; + + static PyTypeObject py_global_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.core._global", + .tp_basicsize = sizeof(PyObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = "Access to the global properties", + + .tp_methods = py_global_methods, + .tp_getset = py_global_getseters + + }; + + return &py_global_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.core._global'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_global(PyObject *module) +{ + PyTypeObject *py_global_type; /* Type Python de 'global' */ + int ret; /* Bilan d'un appel */ + + py_global_type = get_python_global_type(); + + py_global_type->tp_new = PyType_GenericNew; + + if (PyType_Ready(py_global_type) != 0) + return false; + + Py_INCREF(py_global_type); + ret = PyModule_AddObject(module, "_global", (PyObject *)py_global_type); + + return (ret == 0); + +} diff --git a/plugins/pychrysalide/core/global.h b/plugins/pychrysalide/core/global.h new file mode 100644 index 0000000..b136cdb --- /dev/null +++ b/plugins/pychrysalide/core/global.h @@ -0,0 +1,42 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * global.h - prototypes pour l'équivalent Python du fichier "core/global.h" + * + * 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 + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_CORE_GLOBAL_H +#define _PLUGINS_PYCHRYSALIDE_CORE_GLOBAL_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_global_type(void); + +/* Prend en charge l'objet 'pychrysalide.core._global'. */ +bool register_python_global(PyObject *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_CORE_GLOBAL_H */ diff --git a/plugins/pychrysalide/core/module.c b/plugins/pychrysalide/core/module.c index dd89ea0..22d9a48 100644 --- a/plugins/pychrysalide/core/module.c +++ b/plugins/pychrysalide/core/module.c @@ -30,6 +30,7 @@ #include "demanglers.h" #include "formats.h" +#include "global.h" #include "logs.h" #include "params.h" #include "../access.h" @@ -84,6 +85,7 @@ bool add_core_module_to_python_module(PyObject *super) result &= register_python_demanglers(module); result &= register_python_formats(module); + result &= register_python_global(module); result &= register_python_logs(module); result &= register_python_params(module); diff --git a/plugins/pychrysalide/format/executable.c b/plugins/pychrysalide/format/executable.c index 1b1bfe8..9ae45ff 100644 --- a/plugins/pychrysalide/format/executable.c +++ b/plugins/pychrysalide/format/executable.c @@ -28,7 +28,7 @@ #include <pygobject.h> -#include <format/format.h> +#include <format/executable.h> #include "format.h" diff --git a/plugins/pychrysalide/plugin.c b/plugins/pychrysalide/plugin.c index fad0084..4689d0c 100644 --- a/plugins/pychrysalide/plugin.c +++ b/plugins/pychrysalide/plugin.c @@ -76,6 +76,9 @@ static bool g_python_plugin_do_init(GPythonPlugin *); /* Procède à l'extinction du greffon. */ static bool g_python_plugin_do_exit(GPythonPlugin *, GObject *); +/* Procède à une opération liée à un contenu binaire. */ +static void g_python_plugin_handle_binary_content(const GPythonPlugin *, PluginAction, GBinContent *, gid_t, GtkStatusStack *); + /* Indique si le format peut être pris en charge ici. */ FormatMatchStatus python_plugin_is_matching(GBinContent *, GExeFormat *, GPythonPlugin *, char **); @@ -285,6 +288,27 @@ GPluginModule *g_python_plugin_new(const char *modname, const char *filename) switch (sub) { + case DPS_CONTENT: + + switch (action) + { + case PGA_CONTENT_EXPLORER: + case PGA_CONTENT_RESOLVER: + if (!register_python_binding(instance, handle_content, \ + (pg_handle_content)g_python_plugin_handle_binary_content)) + goto gppn_bad_plugin; + break; + + default: + log_variadic_message(LMT_WARNING, + _("Unknown action '0x%02x' in plugin '%s'..."), + action, filename); + break; + + } + + break; + case DPS_FORMAT: switch (action) @@ -439,7 +463,7 @@ static bool g_python_plugin_read_interface(GPythonPlugin *plugin) { action = PyList_GET_ITEM(tuple, i); - interface.actions[i] = PyLong_AsLong(action); + interface.actions[i] = PyLong_AsUnsignedLong(action); } @@ -543,6 +567,42 @@ static bool g_python_plugin_do_exit(GPythonPlugin *plugin, GObject *ref) /****************************************************************************** * * +* Paramètres : plugin = greffon à manipuler. * +* action = type d'action attendue. * +* content = contenu binaire à traiter. * +* gid = identifiant du groupe de traitement. * +* status = barre de statut à tenir informée. * +* * +* Description : Procède à une opération liée à un contenu binaire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_python_plugin_handle_binary_content(const GPythonPlugin *plugin, PluginAction action, GBinContent *content, gid_t gid, GtkStatusStack *status) +{ + PyObject *args; /* Arguments pour l'appel */ + PyObject *value; /* Valeurs obtenues */ + + args = PyTuple_New(4); + + PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action)); + PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(content))); + PyTuple_SetItem(args, 2, PyLong_FromUnsignedLong(gid)); + PyTuple_SetItem(args, 3, pygobject_new(G_OBJECT(status))); + + value = run_python_method(plugin->instance, "handle_binary_content", args); + + Py_XDECREF(value); + Py_DECREF(args); + +} + + +/****************************************************************************** +* * * Paramètres : content = contenu binaire à parcourir. * * parent = éventuel format exécutable déjà chargé. * * plugin = grefon C interne représentant le grefon Python. * @@ -678,7 +738,7 @@ static void g_python_plugin_process_disass(const GPythonPlugin *plugin, PluginAc args = PyTuple_New(2); - PyTuple_SetItem(args, 0, PyLong_FromLong(action)); + PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action)); PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(binary))); value = run_python_method(plugin->instance, "process_binary_disassembly", args); @@ -716,6 +776,8 @@ static bool py_plugin_module_define_constants(PyTypeObject *obj_type) result &= PyDict_AddIntMacro(obj_type, PGA_BASIC_NONE); result &= PyDict_AddIntMacro(obj_type, PGA_PLUGIN_INIT); result &= PyDict_AddIntMacro(obj_type, PGA_PLUGIN_EXIT); + result &= PyDict_AddIntMacro(obj_type, PGA_CONTENT_EXPLORER); + result &= PyDict_AddIntMacro(obj_type, PGA_CONTENT_RESOLVER); result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_MATCHER); result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_LOADER_LAST); result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_STARTED); |