From 8255d60ac64a851c2e7ca37d63aa1ba37d35b704 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Fri, 3 Jan 2020 23:51:31 +0100 Subject: Used a Python enumeration for verbosity levels. --- plugins/pychrysalide/analysis/constants.c | 4 +- plugins/pychrysalide/analysis/db/constants.c | 12 ++-- plugins/pychrysalide/analysis/db/item.c | 2 +- plugins/pychrysalide/analysis/db/server.c | 2 +- plugins/pychrysalide/arch/constants.c | 4 +- plugins/pychrysalide/core/Makefile.am | 1 + plugins/pychrysalide/core/constants.c | 75 ++++++++++++++++++++++ plugins/pychrysalide/core/constants.h | 39 ++++++++++++ plugins/pychrysalide/core/logs.c | 95 +++++++++++----------------- plugins/pychrysalide/format/constants.c | 6 +- plugins/pychrysalide/format/symbol.c | 4 +- plugins/pychrysalide/helpers.c | 19 +++--- plugins/pychrysalide/helpers.h | 28 ++++++-- 13 files changed, 205 insertions(+), 86 deletions(-) create mode 100644 plugins/pychrysalide/core/constants.c create mode 100644 plugins/pychrysalide/core/constants.h diff --git a/plugins/pychrysalide/analysis/constants.c b/plugins/pychrysalide/analysis/constants.c index de7e522..efb88e2 100644 --- a/plugins/pychrysalide/analysis/constants.c +++ b/plugins/pychrysalide/analysis/constants.c @@ -62,8 +62,8 @@ bool define_analysis_content_constants(PyTypeObject *type) goto exit; } - result = attach_constants_group(type, false, "SourceEndian", values, - "Endianness of handled data."); + result = attach_constants_group_to_type(type, false, "SourceEndian", values, + "Endianness of handled data."); exit: diff --git a/plugins/pychrysalide/analysis/db/constants.c b/plugins/pychrysalide/analysis/db/constants.c index 76385fd..fb2a0ea 100644 --- a/plugins/pychrysalide/analysis/db/constants.c +++ b/plugins/pychrysalide/analysis/db/constants.c @@ -63,8 +63,8 @@ bool define_db_protocol_constants(PyTypeObject *type) goto exit; } - result = attach_constants_group(type, false, "DBFeatures", values, - "Features provided by database items."); + result = attach_constants_group_to_type(type, false, "DBFeatures", values, + "Features provided by database items."); exit: @@ -105,8 +105,8 @@ bool define_db_item_constants(PyTypeObject *type) goto exit; } - result = attach_constants_group(type, true, "DbItemFlags", values, - "Properties of a database item."); + result = attach_constants_group_to_type(type, true, "DbItemFlags", values, + "Properties of a database item."); exit: @@ -144,8 +144,8 @@ bool define_hub_server_constants(PyTypeObject *type) goto exit; } - result = attach_constants_group(type, false, "ServerStartStatus", values, - "Status of a server start."); + result = attach_constants_group_to_type(type, false, "ServerStartStatus", values, + "Status of a server start."); exit: diff --git a/plugins/pychrysalide/analysis/db/item.c b/plugins/pychrysalide/analysis/db/item.c index 1d09923..9c0ece9 100644 --- a/plugins/pychrysalide/analysis/db/item.c +++ b/plugins/pychrysalide/analysis/db/item.c @@ -255,7 +255,7 @@ static PyObject *py_db_item_get_flags(PyObject *self, void *closure) flags = g_db_item_get_flags(item); - result = cast_with_constants_group(get_python_db_item_type(), "DbItemFlags", flags); + result = cast_with_constants_group_from_type(get_python_db_item_type(), "DbItemFlags", flags); return result; diff --git a/plugins/pychrysalide/analysis/db/server.c b/plugins/pychrysalide/analysis/db/server.c index cee6bf6..92d2218 100644 --- a/plugins/pychrysalide/analysis/db/server.c +++ b/plugins/pychrysalide/analysis/db/server.c @@ -157,7 +157,7 @@ static PyObject *py_hub_server_start(PyObject *self, PyObject *args) status = g_hub_server_start(server, backlog, true); - result = cast_with_constants_group(get_python_hub_server_type(), "ServerStartStatus", status); + result = cast_with_constants_group_from_type(get_python_hub_server_type(), "ServerStartStatus", status); return result; diff --git a/plugins/pychrysalide/arch/constants.c b/plugins/pychrysalide/arch/constants.c index 341cb63..241b009 100644 --- a/plugins/pychrysalide/arch/constants.c +++ b/plugins/pychrysalide/arch/constants.c @@ -60,8 +60,8 @@ bool define_arch_vmpa_constants(PyTypeObject *type) goto exit; } - result = attach_constants_group(type, false, "VmpaSpecialValue", values, - "Special values for memory locations."); + result = attach_constants_group_to_type(type, false, "VmpaSpecialValue", values, + "Special values for memory locations."); exit: diff --git a/plugins/pychrysalide/core/Makefile.am b/plugins/pychrysalide/core/Makefile.am index 131e1b9..eadad3f 100644 --- a/plugins/pychrysalide/core/Makefile.am +++ b/plugins/pychrysalide/core/Makefile.am @@ -2,6 +2,7 @@ noinst_LTLIBRARIES = libpychrysacore.la libpychrysacore_la_SOURCES = \ + constants.h constants.c \ demanglers.h demanglers.c \ global.h global.c \ logs.h logs.c \ diff --git a/plugins/pychrysalide/core/constants.c b/plugins/pychrysalide/core/constants.c new file mode 100644 index 0000000..e61f451 --- /dev/null +++ b/plugins/pychrysalide/core/constants.c @@ -0,0 +1,75 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * constants.c - ajout des constantes liées au coeur du programme + * + * Copyright (C) 2020 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 "constants.h" + + +#include + + +#include "../helpers.h" + + + +/****************************************************************************** +* * +* Paramètres : module = module dont le dictionnaire est à compléter. * +* * +* Description : Définit les constantes pour les types de messages. * +* * +* Retour : true en cas de succès de l'opération, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool define_core_logs_constants(PyObject *module) +{ + bool result; /* Bilan à retourner */ + PyObject *values; /* Groupe de valeurs à établir */ + + values = PyDict_New(); + + result = add_const_to_group(values, "INFO", LMT_INFO); + if (result) result = add_const_to_group(values, "PROCESS", LMT_PROCESS); + if (result) result = add_const_to_group(values, "WARNING", LMT_WARNING); + if (result) result = add_const_to_group(values, "BAD_BINARY", LMT_BAD_BINARY); + if (result) result = add_const_to_group(values, "LMT_ERROR", LMT_ERROR); + if (result) result = add_const_to_group(values, "EXT_ERROR", LMT_EXT_ERROR); + if (result) result = add_const_to_group(values, "COUNT", LMT_COUNT); + + if (!result) + { + Py_DECREF(values); + goto exit; + } + + result = attach_constants_group_to_module(module, false, "LogMessageType", values, + "Available types for log messages."); + + exit: + + return result; + +} diff --git a/plugins/pychrysalide/core/constants.h b/plugins/pychrysalide/core/constants.h new file mode 100644 index 0000000..38a8ebc --- /dev/null +++ b/plugins/pychrysalide/core/constants.h @@ -0,0 +1,39 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * constants.h - prototypes pour l'ajout des constantes liées au coeur du programme + * + * Copyright (C) 2020 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_CONSTANTS_H +#define _PLUGINS_PYCHRYSALIDE_CORE_CONSTANTS_H + + +#include +#include + + + +/* Définit les constantes pour les types de messages. */ +bool define_core_logs_constants(PyObject *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_CORE_CONSTANTS_H */ diff --git a/plugins/pychrysalide/core/logs.c b/plugins/pychrysalide/core/logs.c index afab7ab..d4fd0da 100644 --- a/plugins/pychrysalide/core/logs.c +++ b/plugins/pychrysalide/core/logs.c @@ -31,6 +31,7 @@ #include +#include "constants.h" #include "../access.h" #include "../helpers.h" #include "../pychrysa.h" @@ -46,9 +47,6 @@ static PyObject *py_logs_set_verbosity(PyObject *, PyObject *); /* Affiche un message dans le journal des messages système. */ static PyObject *py_logs_log_message(PyObject *, PyObject *); -/* Définit les constantes pour les types de message. */ -static bool define_python_log_constants(PyObject *); - /****************************************************************************** @@ -69,9 +67,19 @@ static PyObject *py_logs_get_verbosity(PyObject *self, PyObject *args) PyObject *result; /* Conversion à retourner */ LogMessageType verbosity; /* Niveau de filtre de message */ +#define LOGS_GET_VERBOSITY_METHOD PYTHON_METHOD_DEF \ +( \ + get_verbosity, "", \ + METH_NOARGS, py_logs, \ + "Get the log verbosity, as a pychrysalide.core.LogMessageType level.\n" \ + "\n" \ + "A *COUNT* value means no log gets displayed, a null value means" \ + " all kinds of logs get printed." \ +) + verbosity = get_log_verbosity(); - result = PyLong_FromUnsignedLong(verbosity); + result = cast_with_constants_group_from_module("pychrysalide.core", "LogMessageType", verbosity); return result; @@ -96,6 +104,17 @@ static PyObject *py_logs_set_verbosity(PyObject *self, PyObject *args) PyObject *result; /* Bilan à retourner */ unsigned long verbosity; /* Niveau de filtre de message */ +#define LOGS_SET_VERBOSITY_METHOD PYTHON_METHOD_DEF \ +( \ + set_verbosity, "level, /", \ + METH_VARARGS, py_logs, \ + "Set the log verbosity. The provided level has to be castable into a" \ + " pychrysalide.core.LogMessageType value.\n" \ + "\n" \ + "A *COUNT* value means no log gets displayed, a null value means" \ + " all kinds of logs get printed." \ +) + if (!PyArg_ParseTuple(args, "k", &verbosity)) return NULL; @@ -128,6 +147,17 @@ static PyObject *py_logs_log_message(PyObject *self, PyObject *args) unsigned long type; /* Espèce du message */ const char *msg; /* Contenu du message */ +#define LOGS_LOG_MESSAGE_METHOD PYTHON_METHOD_DEF \ +( \ + log_message, "type, msg, /", \ + METH_VARARGS, py_logs, \ + "Display a message in the log window, in graphical mode, or in the" \ + " console output if none.\n" \ + "\n" \ + "The type of the message has to be a pychrysalide.core.LogMessageType" \ + " value." \ +) + if (!PyArg_ParseTuple(args, "ks", &type, &msg)) return NULL; @@ -159,37 +189,6 @@ static PyObject *py_logs_log_message(PyObject *self, PyObject *args) /****************************************************************************** * * -* Paramètres : dict = dictionnaire de module à compléter. * -* * -* Description : Définit les constantes pour les types de message. * -* * -* Retour : true en cas de succès de l'opération, false sinon. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static bool define_python_log_constants(PyObject *dict) -{ - bool result; /* Bilan à retourner */ - - result = true; - - result &= PyModDict_AddULongMacro(dict, LMT_INFO); - result &= PyModDict_AddULongMacro(dict, LMT_PROCESS); - result &= PyModDict_AddULongMacro(dict, LMT_WARNING); - result &= PyModDict_AddULongMacro(dict, LMT_BAD_BINARY); - result &= PyModDict_AddULongMacro(dict, LMT_ERROR); - result &= PyModDict_AddULongMacro(dict, LMT_EXT_ERROR); - result &= PyModDict_AddULongMacro(dict, LMT_COUNT); - - return result; - -} - - -/****************************************************************************** -* * * Paramètres : - * * * * Description : Définit une extension du module 'core' à compléter. * @@ -204,27 +203,12 @@ bool populate_core_module_with_logs(void) { bool result; /* Bilan à retourner */ PyObject *module; /* Module à recompléter */ - PyObject *dict; /* Dictionnaire dudit module */ static PyMethodDef py_logs_methods[] = { - - { - "get_verbosity", py_logs_get_verbosity, - METH_NOARGS, - "get_verbosity(, /)\n--\n\nGet the log verbosity." - }, - { - "set_verbosity", py_logs_set_verbosity, - METH_VARARGS, - "set_verbosity(, /)\n--\n\nSet the log verbosity." - }, - { - "log_message", py_logs_log_message, - METH_VARARGS, - "log_message(type, msg, /)\n--\n\nDisplay a message in the log window, if any." - }, + LOGS_GET_VERBOSITY_METHOD, + LOGS_SET_VERBOSITY_METHOD, + LOGS_LOG_MESSAGE_METHOD, { NULL } - }; module = get_access_to_python_module("pychrysalide.core"); @@ -232,10 +216,7 @@ bool populate_core_module_with_logs(void) result = register_python_module_methods(module, py_logs_methods); if (result) - { - dict = PyModule_GetDict(module); - result = define_python_log_constants(dict); - } + result = define_core_logs_constants(module); return result; diff --git a/plugins/pychrysalide/format/constants.c b/plugins/pychrysalide/format/constants.c index ea2aedc..1561032 100644 --- a/plugins/pychrysalide/format/constants.c +++ b/plugins/pychrysalide/format/constants.c @@ -66,7 +66,8 @@ bool define_binary_symbol_constants(PyTypeObject *type) goto exit; } - result = attach_constants_group(type, false, "SymbolType", values, "Available values for symbol types."); + result = attach_constants_group_to_type(type, false, "SymbolType", values, + "Available values for symbol types."); values = PyDict_New(); @@ -81,7 +82,8 @@ bool define_binary_symbol_constants(PyTypeObject *type) goto exit; } - result = attach_constants_group(type, false, "SymbolStatus", values, "Status of a symbol visibility."); + result = attach_constants_group_to_type(type, false, "SymbolStatus", values, + "Status of a symbol visibility."); exit: diff --git a/plugins/pychrysalide/format/symbol.c b/plugins/pychrysalide/format/symbol.c index 873510d..66e4dfc 100644 --- a/plugins/pychrysalide/format/symbol.c +++ b/plugins/pychrysalide/format/symbol.c @@ -439,7 +439,7 @@ static PyObject *py_binary_symbol_get_stype(PyObject *self, void *closure) symbol = G_BIN_SYMBOL(pygobject_get(self)); type = g_binary_symbol_get_stype(symbol); - result = cast_with_constants_group(get_python_binary_symbol_type(), "SymbolType", type); + result = cast_with_constants_group_from_type(get_python_binary_symbol_type(), "SymbolType", type); return result; @@ -508,7 +508,7 @@ static PyObject *py_binary_symbol_get_status(PyObject *self, void *closure) symbol = G_BIN_SYMBOL(pygobject_get(self)); status = g_binary_symbol_get_status(symbol); - result = cast_with_constants_group(get_python_binary_symbol_type(), "SymbolStatus", status); + result = cast_with_constants_group_from_type(get_python_binary_symbol_type(), "SymbolStatus", status); return result; diff --git a/plugins/pychrysalide/helpers.c b/plugins/pychrysalide/helpers.c index d2215d8..367714d 100644 --- a/plugins/pychrysalide/helpers.c +++ b/plugins/pychrysalide/helpers.c @@ -1024,7 +1024,8 @@ int forward_pygobjet_init(PyObject *self) /****************************************************************************** * * -* Paramètres : owner = type dont le dictionnaire est à compléter. * +* Paramètres : owner = désignation du propriétaire du dictionnaire visé. * +* dict = dictionnaire dont le contenu est à compléter. * * flags = indique le type d'énumération ciblée. * * name = désignation humaine du groupe à constituer. * * values = noms et valeurs associées. * @@ -1038,7 +1039,7 @@ int forward_pygobjet_init(PyObject *self) * * ******************************************************************************/ -bool attach_constants_group(PyTypeObject *owner, bool flags, const char *name, PyObject *values, const char *doc) +bool _attach_constants_group(const char *owner, PyObject *dict, bool flags, const char *name, PyObject *values, const char *doc) { bool result; /* Bilan à retourner */ PyObject *enum_mod; /* Module Python enum */ @@ -1093,10 +1094,10 @@ bool attach_constants_group(PyTypeObject *owner, bool flags, const char *name, P kwargs = PyDict_New(); - dot = rindex(owner->tp_name, '.'); + dot = rindex(owner, '.'); assert(dot != NULL); - module = strndup(owner->tp_name, dot - owner->tp_name); + module = strndup(owner, dot - owner); str_obj = PyUnicode_FromString(module); ret = PyDict_SetItemString(kwargs, "module", str_obj); @@ -1121,7 +1122,7 @@ bool attach_constants_group(PyTypeObject *owner, bool flags, const char *name, P new = PyObject_Call(class, args, kwargs); if (new == NULL) goto build_error; - ret = PyDict_SetItemString(owner->tp_dict, name, new); + ret = PyDict_SetItemString(dict, name, new); if (ret != 0) goto register_0_error; features = get_access_to_python_module("pychrysalide.features"); @@ -1165,7 +1166,7 @@ bool attach_constants_group(PyTypeObject *owner, bool flags, const char *name, P /****************************************************************************** * * -* Paramètres : owner = propriétaire du groupe de constantes. * +* Paramètres : owner = désignation du propriétaire du dictionnaire visé. * * name = désignation humaine du groupe à consulter. * * value = valeur à transmettre à Python. * * * @@ -1177,7 +1178,7 @@ bool attach_constants_group(PyTypeObject *owner, bool flags, const char *name, P * * ******************************************************************************/ -PyObject *cast_with_constants_group(const PyTypeObject *owner, const char *name, unsigned long value) +PyObject *_cast_with_constants_group(const char *owner, const char *name, unsigned long value) { PyObject *result; /* Objet Python à retourner */ char *dot; /* Position du dernier point */ @@ -1191,10 +1192,10 @@ PyObject *cast_with_constants_group(const PyTypeObject *owner, const char *name, /* Recherche de la classe Python */ - dot = strrchr(owner->tp_name, '.'); + dot = strrchr(owner, '.'); assert(dot != NULL); - modname = strndup(owner->tp_name, dot - owner->tp_name); + modname = strndup(owner, dot - owner); module = get_access_to_python_module(modname); diff --git a/plugins/pychrysalide/helpers.h b/plugins/pychrysalide/helpers.h index 3bb457d..8ee47e3 100644 --- a/plugins/pychrysalide/helpers.h +++ b/plugins/pychrysalide/helpers.h @@ -165,9 +165,6 @@ int forward_pygobjet_init(PyObject *); /* ----------------------- TRANSFERT DES VALEURS CONSTANTES ------------------------- */ -/* Officialise un groupe de constantes avec sémentique. */ -bool attach_constants_group(PyTypeObject *, bool, const char *, PyObject *, const char *); - /* Simplification d'un ajout de constante pour l'appelant */ #define add_const_to_group(d, n, v) \ ({ \ @@ -186,8 +183,31 @@ bool attach_constants_group(PyTypeObject *, bool, const char *, PyObject *, cons __result; \ }) +/* Officialise un groupe de constantes avec sémentique. */ +bool _attach_constants_group(const char *, PyObject *, bool, const char *, PyObject *, const char *); + +#define attach_constants_group_to_type(type, flags, name, values, doc) \ + _attach_constants_group(type->tp_name, type->tp_dict, flags, name, values, doc) + +#define attach_constants_group_to_module(mod, flags, name, values, doc) \ + ({ \ + bool __result; \ + const char *__owner; \ + PyObject *__dict; \ + __owner = PyModule_GetName(mod); \ + __dict = PyModule_GetDict(mod); \ + __result = _attach_constants_group(__owner, __dict, flags, name, values, doc); \ + __result; \ + }) + /* Traduit une valeur constante C en équivalent Python. */ -PyObject *cast_with_constants_group(const PyTypeObject *, const char *, unsigned long); +PyObject *_cast_with_constants_group(const char *, const char *, unsigned long); + +#define cast_with_constants_group_from_type(type, name, value) \ + _cast_with_constants_group(type->tp_name, name, value) + +#define cast_with_constants_group_from_module(owner, name, value) \ + _cast_with_constants_group(owner, name, value) -- cgit v0.11.2-87-g4458