diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/pychrysalide/Makefile.am | 5 | ||||
-rw-r--r-- | plugins/pychrysalide/common/Makefile.am | 19 | ||||
-rw-r--r-- | plugins/pychrysalide/common/bits.c | 32 | ||||
-rw-r--r-- | plugins/pychrysalide/common/module.c | 20 | ||||
-rw-r--r-- | plugins/pychrysalide/common/xdg.c | 385 | ||||
-rw-r--r-- | plugins/pychrysalide/common/xdg.h | 39 | ||||
-rw-r--r-- | plugins/pychrysalide/core.c | 12 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/Makefile.am | 45 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/module.c | 9 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/work.c | 351 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/work.h | 45 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/workqueue.c | 586 | ||||
-rw-r--r-- | plugins/pychrysalide/glibext/workqueue.h | 45 |
13 files changed, 1540 insertions, 53 deletions
diff --git a/plugins/pychrysalide/Makefile.am b/plugins/pychrysalide/Makefile.am index 888de8a..8408c96 100644 --- a/plugins/pychrysalide/Makefile.am +++ b/plugins/pychrysalide/Makefile.am @@ -49,7 +49,6 @@ AM_CFLAGS = $(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) $(TOOLKIT_CFL # core/libpychrysacore.la \ # debug/libpychrysadebug.la \ # format/libpychrysaformat.la \ -# glibext/libpychrysaglibext.la \ # $(GTKEXT_LIBADD) \ # $(GUI_LIBADD) \ # mangling/libpychrysamangling.la \ @@ -58,7 +57,9 @@ AM_CFLAGS = $(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) $(TOOLKIT_CFL pychrysalide_la_LIBADD = \ analysis/libpychrysaanalysis4.la \ arch/libpychrysaarch4.la \ + common/libpychrysacommon.la \ core/libpychrysacore.la \ + glibext/libpychrysaglibext.la \ plugins/libpychrysaplugins.la # -ldl: dladdr(), dlerror() @@ -76,4 +77,4 @@ dev_HEADERS = $(pychrysalide_la_SOURCES:%c=) #SUBDIRS = analysis arch common core debug format glibext $(GTKEXT_SUBDIR) $(GUI_SUBDIR) mangling plugins -SUBDIRS = analysis arch core plugins +SUBDIRS = analysis arch common core glibext plugins diff --git a/plugins/pychrysalide/common/Makefile.am b/plugins/pychrysalide/common/Makefile.am index b5249b9..cc87a82 100644 --- a/plugins/pychrysalide/common/Makefile.am +++ b/plugins/pychrysalide/common/Makefile.am @@ -1,16 +1,21 @@ noinst_LTLIBRARIES = libpychrysacommon.la +# libpychrysacommon_la_SOURCES = \ +# bits.h bits.c \ +# fnv1a.h fnv1a.c \ +# hex.h hex.c \ +# itoa.h itoa.c \ +# leb128.h leb128.c \ +# module.h module.c \ +# packed.h packed.c \ +# pathname.h pathname.c \ +# pearson.h pearson.c + libpychrysacommon_la_SOURCES = \ bits.h bits.c \ - fnv1a.h fnv1a.c \ - hex.h hex.c \ - itoa.h itoa.c \ - leb128.h leb128.c \ module.h module.c \ - packed.h packed.c \ - pathname.h pathname.c \ - pearson.h pearson.c + xdg.h xdg.c libpychrysacommon_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ -I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT diff --git a/plugins/pychrysalide/common/bits.c b/plugins/pychrysalide/common/bits.c index a127251..35d6ea4 100644 --- a/plugins/pychrysalide/common/bits.c +++ b/plugins/pychrysalide/common/bits.c @@ -534,27 +534,33 @@ static PyObject *py_bitfield_set_all(PyObject *self, PyObject *args) static PyObject *py_bitfield_reset(PyObject *self, PyObject *args) { - unsigned long first; /* Indice du premier bit testé */ unsigned long count; /* Nombre de bits à analyser */ + unsigned long first; /* Indice du premier bit testé */ int ret; /* Bilan de lecture des args. */ py_bitfield_t *bf; /* Instance à manipuler */ #define BITFIELD_RESET_METHOD PYTHON_METHOD_DEF \ ( \ - reset, "$self, first, count, /", \ + reset, "$self, first, /, count=1", \ METH_VARARGS, py_bitfield, \ "Switch to 0 a part of bits in a bitfield.\n" \ "\n" \ "The area to process starts at bit *first* and has a" \ - " size of *count* bits." \ + " size of *count* bits (only one bit is processed if" \ + " no *size* is provided)." \ ) - ret = PyArg_ParseTuple(args, "kk", &first, &count); + count = 1; + + ret = PyArg_ParseTuple(args, "k|k", &first, &count); if (!ret) return NULL; bf = (py_bitfield_t *)self; - reset_in_bit_field(bf->native, first, count); + if (count == 1) + reset_in_bit_field(bf->native, first); + else + reset_multi_in_bit_field(bf->native, first, count); Py_RETURN_NONE; @@ -576,27 +582,33 @@ static PyObject *py_bitfield_reset(PyObject *self, PyObject *args) static PyObject *py_bitfield_set(PyObject *self, PyObject *args) { - unsigned long first; /* Indice du premier bit testé */ unsigned long count; /* Nombre de bits à analyser */ + unsigned long first; /* Indice du premier bit testé */ int ret; /* Bilan de lecture des args. */ py_bitfield_t *bf; /* Instance à manipuler */ #define BITFIELD_SET_METHOD PYTHON_METHOD_DEF \ ( \ - set, "$self, first, count, /", \ + set, "$self, first, /, count=1", \ METH_VARARGS, py_bitfield, \ "Switch to 1 a part of bits in a bitfield.\n" \ "\n" \ "The area to process starts at bit *first* and has a" \ - " size of *count* bits." \ + " size of *count* bits (only one bit is processed if" \ + " no *size* is provided)." \ ) - ret = PyArg_ParseTuple(args, "kk", &first, &count); + count = 1; + + ret = PyArg_ParseTuple(args, "k|k", &first, &count); if (!ret) return NULL; bf = (py_bitfield_t *)self; - set_in_bit_field(bf->native, first, count); + if (count == 1) + set_in_bit_field(bf->native, first); + else + set_multi_in_bit_field(bf->native, first, count); Py_RETURN_NONE; diff --git a/plugins/pychrysalide/common/module.c b/plugins/pychrysalide/common/module.c index a0042ee..7af12ba 100644 --- a/plugins/pychrysalide/common/module.c +++ b/plugins/pychrysalide/common/module.c @@ -26,13 +26,14 @@ #include "bits.h" -#include "fnv1a.h" -#include "hex.h" -#include "itoa.h" -#include "leb128.h" -#include "packed.h" -#include "pathname.h" -#include "pearson.h" +//#include "fnv1a.h" +//#include "hex.h" +//#include "itoa.h" +//#include "leb128.h" +//#include "packed.h" +//#include "pathname.h" +//#include "pearson.h" +#include "xdg.h" #include "../helpers.h" @@ -98,15 +99,18 @@ bool populate_common_module(void) result = true; + /* if (result) result = populate_common_module_with_fnv1a(); if (result) result = populate_common_module_with_hex(); if (result) result = populate_common_module_with_itoa(); if (result) result = populate_common_module_with_leb128(); if (result) result = populate_common_module_with_pathname(); if (result) result = populate_common_module_with_pearson(); + */ + if (result) result = populate_common_module_with_xdg(); if (result) result = ensure_python_bitfield_is_registered(); - if (result) result = ensure_python_packed_buffer_is_registered(); + //if (result) result = ensure_python_packed_buffer_is_registered(); assert(result); diff --git a/plugins/pychrysalide/common/xdg.c b/plugins/pychrysalide/common/xdg.c new file mode 100644 index 0000000..789a0a6 --- /dev/null +++ b/plugins/pychrysalide/common/xdg.c @@ -0,0 +1,385 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * xdg.c - équivalent Python du fichier "common/xdg.c" + * + * Copyright (C) 2024 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 "xdg.h" + + +#include <malloc.h> +#include <pygobject.h> + + +#include <common/xdg.h> + + +#include "../access.h" +#include "../helpers.h" + + + +/* Détermine le chemin d'un répertoire de données XDG. */ +static PyObject *py_xdg_get_xdg_cache_dir(PyObject *, PyObject *); + +/* Détermine le chemin d'un répertoire de données XDG. */ +static PyObject *py_xdg_get_xdg_config_dir(PyObject *, PyObject *); + +/* Détermine le chemin d'un répertoire de données XDG. */ +static PyObject *py_xdg_get_xdg_data_dir(PyObject *, PyObject *); + +/* Détermine le chemin d'un répertoire de données XDG. */ +static PyObject *py_xdg_get_xdg_state_dir(PyObject *, PyObject *); + +/* Détermine le chemin d'un répertoire éphémère XDG. */ +static PyObject *py_xdg_get_xdg_runtime_dir(PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_cache_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + int create; /* Besoin en création */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_CACHE_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_cache_dir, "suffix, create=True", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_CACHE_HOME* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory. The *create* option ensures all the directories" \ + " involved in the returned path exist.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to cache according to the XDG specifications, or" \ + " *None* in case of failure." \ +) + + create = 1; + + ret = PyArg_ParseTuple(args, "s|p", &suffix, &create); + if (!ret) return NULL; + + filename = get_xdg_cache_dir(suffix, create); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_config_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + int create; /* Besoin en création */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_CONFIG_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_config_dir, "suffix, create=True", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_CONFIG_HOME* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory. The *create* option ensures all the directories" \ + " involved in the returned path exist.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to configuration according to the XDG specifications,"\ + " or *None* in case of failure." \ +) + + create = 1; + + ret = PyArg_ParseTuple(args, "s|p", &suffix, &create); + if (!ret) return NULL; + + filename = get_xdg_config_dir(suffix, create); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_data_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + int create; /* Besoin en création */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_DATA_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_data_dir, "suffix, create=True", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_DATA_HOME* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory. The *create* option ensures all the directories" \ + " involved in the returned path exist.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to data according to the XDG specifications, or" \ + " *None* in case of failure." \ +) + + create = 1; + + ret = PyArg_ParseTuple(args, "s|p", &suffix, &create); + if (!ret) return NULL; + + filename = get_xdg_data_dir(suffix, create); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire de données XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_state_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + int create; /* Besoin en création */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_STATE_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_state_dir, "suffix, create=True", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_STATE_HOME* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory. The *create* option ensures all the directories" \ + " involved in the returned path exist.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to states according to the XDG specifications, or" \ + " *None* in case of failure." \ +) + + create = 1; + + ret = PyArg_ParseTuple(args, "s|p", &suffix, &create); + if (!ret) return NULL; + + filename = get_xdg_state_dir(suffix, create); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = arguments fournis lors de l'appel à la fonction. * +* * +* Description : Détermine le chemin d'un répertoire éphémère XDG. * +* * +* Retour : Chemin d'accès aux configurations personnelles ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_xdg_get_xdg_runtime_dir(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + const char *suffix; /* Fin de la localisation */ + int ret; /* Bilan de lecture des args. */ + char *filename; /* Chemin d'accès construit */ + +#define GET_XDG_RUNTIME_DIR_METHOD PYTHON_METHOD_DEF \ +( \ + get_xdg_runtime_dir, "suffix", \ + METH_VARARGS, py_xdg, \ + "Get the location of a file belonging to the base directory" \ + " pointed by the *XDG_RUNTIME_DIR* environment variable.\n" \ + "\n" \ + "The *suffix* argument is a string appended to the XDG base" \ + " directory.\n" \ + "\n" \ + "The function returns the full filename to use for a content" \ + " related to runtime data according to the XDG specifications," \ + " or *None* in case of failure." \ +) + + ret = PyArg_ParseTuple(args, "s", &suffix); + if (!ret) return NULL; + + filename = get_xdg_runtime_dir(suffix); + + if (filename == NULL) + { + result = Py_None; + Py_INCREF(result); + } + else + { + result = PyUnicode_FromString(filename); + free(filename); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Définit une extension du module 'common' à compléter. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool populate_common_module_with_xdg(void) +{ + bool result; /* Bilan à retourner */ + PyObject *module; /* Module à recompléter */ + + static PyMethodDef py_xdg_methods[] = { + GET_XDG_CACHE_DIR_METHOD, + GET_XDG_CONFIG_DIR_METHOD, + GET_XDG_DATA_DIR_METHOD, + GET_XDG_STATE_DIR_METHOD, + GET_XDG_RUNTIME_DIR_METHOD, + { NULL } + }; + + module = get_access_to_python_module("pychrysalide.common"); + + result = register_python_module_methods(module, py_xdg_methods); + + return result; + +} diff --git a/plugins/pychrysalide/common/xdg.h b/plugins/pychrysalide/common/xdg.h new file mode 100644 index 0000000..f1aa16a --- /dev/null +++ b/plugins/pychrysalide/common/xdg.h @@ -0,0 +1,39 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * xdg.h - prototypes pour l'équivalent Python du fichier "common/xdg.c" + * + * Copyright (C) 2024 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_COMMON_XDG_H +#define _PLUGINS_PYCHRYSALIDE_COMMON_XDG_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Définit une extension du module 'common' à compléter. */ +bool populate_common_module_with_xdg(void); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_COMMON_XDG_H */ diff --git a/plugins/pychrysalide/core.c b/plugins/pychrysalide/core.c index 35c0e35..8510d78 100644 --- a/plugins/pychrysalide/core.c +++ b/plugins/pychrysalide/core.c @@ -60,11 +60,11 @@ #include "struct.h" #include "analysis/module.h" #include "arch/module.h" -/* #include "common/module.h" */ +#include "common/module.h" +#include "glibext/module.h" /* #include "core/module.h" */ /* #include "debug/module.h" */ /* #include "format/module.h" */ -/* #include "glibext/module.h" */ /* #ifdef INCLUDE_GTK_SUPPORT */ /* # include "gtkext/module.h" */ /* # include "gui/module.h" */ @@ -645,12 +645,12 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) if (status) status = add_analysis_module(result); if (status) status = add_arch_module(result); - /* if (status) status = add_common_module(result); + if (status) status = add_glibext_module(result); + /* if (status) status = add_core_module(result); if (status) status = add_debug_module(result); if (status) status = add_format_module(result); - if (status) status = add_glibext_module(result); #ifdef INCLUDE_GTK_SUPPORT if (status) status = add_gtkext_module(result); if (status) status = add_gui_module(result); @@ -668,12 +668,12 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) if (status) status = populate_analysis_module(); if (status) status = populate_arch_module(); - /* + if (status) status = populate_glibext_module(); if (status) status = populate_common_module(); + /* if (status) status = populate_core_module(); if (status) status = populate_debug_module(); if (status) status = populate_format_module(); - if (status) status = populate_glibext_module(); #ifdef INCLUDE_GTK_SUPPORT if (status) status = populate_gtkext_module(); if (status) status = populate_gui_module(); diff --git a/plugins/pychrysalide/glibext/Makefile.am b/plugins/pychrysalide/glibext/Makefile.am index 2ed2aa5..e9c3756 100644 --- a/plugins/pychrysalide/glibext/Makefile.am +++ b/plugins/pychrysalide/glibext/Makefile.am @@ -1,29 +1,34 @@ noinst_LTLIBRARIES = libpychrysaglibext.la +# libpychrysaglibext_la_SOURCES = \ +# constants.h constants.c \ +# binarycursor.h binarycursor.c \ +# binportion.h binportion.c \ +# buffercache.h buffercache.c \ +# bufferline.h bufferline.c \ +# comparison.h comparison.c \ +# configuration.h configuration.c \ +# linecursor.h linecursor.c \ +# linegen.h linegen.c \ +# module.h module.c \ +# singleton.h singleton.c + +# if BUILD_GTK_SUPPORT + +# libpychrysaglibext_la_SOURCES += \ +# bufferview.h bufferview.c \ +# loadedpanel.h loadedpanel.c \ +# named.h named.c + +# endif + libpychrysaglibext_la_SOURCES = \ - constants.h constants.c \ - binarycursor.h binarycursor.c \ - binportion.h binportion.c \ - buffercache.h buffercache.c \ - bufferline.h bufferline.c \ - comparison.h comparison.c \ - configuration.h configuration.c \ - linecursor.h linecursor.c \ - linegen.h linegen.c \ module.h module.c \ - singleton.h singleton.c - -if BUILD_GTK_SUPPORT - -libpychrysaglibext_la_SOURCES += \ - bufferview.h bufferview.c \ - loadedpanel.h loadedpanel.c \ - named.h named.c - -endif + work.h work.c \ + workqueue.h workqueue.c -libpychrysaglibext_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ +libpychrysaglibext_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ -I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT diff --git a/plugins/pychrysalide/glibext/module.c b/plugins/pychrysalide/glibext/module.c index 3e4307a..23e7a7d 100644 --- a/plugins/pychrysalide/glibext/module.c +++ b/plugins/pychrysalide/glibext/module.c @@ -28,6 +28,7 @@ #include <assert.h> +/* #include "binarycursor.h" #include "binportion.h" #include "buffercache.h" @@ -40,6 +41,9 @@ #include "loadedpanel.h" #include "named.h" #include "singleton.h" +*/ +#include "work.h" +#include "workqueue.h" #include "../helpers.h" @@ -106,6 +110,10 @@ bool populate_glibext_module(void) result = true; + if (result) result = ensure_python_generic_work_is_registered(); + if (result) result = ensure_python_work_queue_is_registered(); + + /* if (result) result = ensure_python_singleton_candidate_is_registered(); if (result) result = ensure_python_binary_cursor_is_registered(); @@ -126,6 +134,7 @@ bool populate_glibext_module(void) if (result) result = ensure_python_named_widget_is_registered(); #endif if (result) result = ensure_python_singleton_factory_is_registered(); + */ assert(result); diff --git a/plugins/pychrysalide/glibext/work.c b/plugins/pychrysalide/glibext/work.c new file mode 100644 index 0000000..6a15984 --- /dev/null +++ b/plugins/pychrysalide/glibext/work.c @@ -0,0 +1,351 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * work.c - prototypes pour l'équivalent Python du fichier "glibext/work.c" + * + * Copyright (C) 2024 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 "work.h" + + +#include <assert.h> +#include <pygobject.h> + + +#include <glibext/work-int.h> + + +#include "../access.h" +#include "../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Initialise la classe des travaux programmés. */ +static void py_generic_work_init_gclass(GGenericWorkClass *, gpointer); + +CREATE_DYN_ABSTRACT_CONSTRUCTOR(generic_work, G_TYPE_GENERIC_WORK, py_generic_work_init_gclass); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_generic_work_init(PyObject *, PyObject *, PyObject *); + +/* Mène l'opération programmée. */ +static void py_generic_work_run_wrapper(GGenericWork *); + + + +/* ------------------------- CONNEXION AVEC L'API DE PYTHON ------------------------- */ + + +/* Mène l'opération programmée. */ +static PyObject *py_generic_work_process(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : class = classe à initialiser. * +* unused = données non utilisées ici. * +* * +* Description : Initialise la classe des travaux programmés. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_generic_work_init_gclass(GGenericWorkClass *class, gpointer unused) +{ + class->run = py_generic_work_run_wrapper; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_generic_work_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + int ret; /* Bilan de lecture des args. */ + +#define GENERIC_WORK_DOC \ + "GenericWork defines a basic work aimed to get processed in a" \ + " thread set up by a pychrysalide.glibext.WorkQueue object.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " GenericWork()" \ + "\n" \ + "The following method has to be defined for new classes:\n" \ + "* pychrysalide.glibext.GenericWork._run();\n" \ + "\n" \ + "The following signal may be emitted:\n" \ + "* *work-completed* : when the work has been processed." + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : work = travail à effectuer. * +* * +* Description : Mène l'opération programmée. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_generic_work_run_wrapper(GGenericWork *work) +{ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Bilan de consultation */ + +#define GENERIC_WORK_RUN_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _run, "$self", \ + METH_NOARGS, \ + "Abstract method used to process the work.\n" \ + "\n" \ + "No result is expected from this function." \ +) + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(work)); + + if (has_python_method(pyobj, "_run")) + { + pyret = run_python_method(pyobj, "_run", NULL); + + Py_XDECREF(pyret); + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* CONNEXION AVEC L'API DE PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Mène l'opération programmée. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_generic_work_process(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GGenericWork *work; /* Version native */ + +#define GENERIC_WORK_PROCESS_METHOD PYTHON_METHOD_DEF \ +( \ + process, "$self", \ + METH_NOARGS, py_generic_work, \ + "Process the work.\n" \ + "\n" \ + "The function does not return anything.\n" \ + "\n" \ + "The *work-completed* signal is emitted when the" \ + " function terminates." \ +) + + work = G_GENERIC_WORK(pygobject_get(self)); + + g_generic_work_process(work); + + result = Py_None; + Py_INCREF(result); + + 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_generic_work_type(void) +{ + static PyMethodDef py_generic_work_methods[] = { + GENERIC_WORK_RUN_WRAPPER, + GENERIC_WORK_PROCESS_METHOD, + { NULL } + }; + + static PyGetSetDef py_generic_work_getseters[] = { + { NULL } + }; + + static PyTypeObject py_generic_work_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.glibext.GenericWork", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = GENERIC_WORK_DOC, + + .tp_methods = py_generic_work_methods, + .tp_getset = py_generic_work_getseters, + + .tp_init = py_generic_work_init, + .tp_new = py_generic_work_new, + + }; + + return &py_generic_work_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.glibext.GenericWork'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_generic_work_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'GenericWork' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_generic_work_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.glibext"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_GENERIC_WORK, type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en travail générique. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_generic_work(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_generic_work_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to generic work"); + break; + + case 1: + *((GGenericWork **)dst) = G_GENERIC_WORK(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/glibext/work.h b/plugins/pychrysalide/glibext/work.h new file mode 100644 index 0000000..de9a936 --- /dev/null +++ b/plugins/pychrysalide/glibext/work.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * work.h - prototypes pour l'équivalent Python du fichier "glibext/work.h" + * + * Copyright (C) 2024 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_GLIBEXT_WORK_H +#define _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORK_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_generic_work_type(void); + +/* Prend en charge l'objet 'pychrysalide.glibext.GenericWork'. */ +bool ensure_python_generic_work_is_registered(void); + +/* Tente de convertir en travail générique. */ +int convert_to_generic_work(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORK_H */ diff --git a/plugins/pychrysalide/glibext/workqueue.c b/plugins/pychrysalide/glibext/workqueue.c new file mode 100644 index 0000000..d8126be --- /dev/null +++ b/plugins/pychrysalide/glibext/workqueue.c @@ -0,0 +1,586 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * workqueue.c - prototypes pour l'équivalent Python du fichier "glibext/workqueue.c" + * + * Copyright (C) 2024 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 "workqueue.h" + + +#include <assert.h> +#include <pygobject.h> + + +#include <glibext/workqueue-int.h> + + +#include "work.h" +#include "../access.h" +#include "../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +CREATE_DYN_CONSTRUCTOR(work_queue, G_TYPE_WORK_QUEUE); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_work_queue_init(PyObject *, PyObject *, PyObject *); + + + +/* ------------------------- CONNEXION AVEC L'API DE PYTHON ------------------------- */ + + +/* Constitue un nouveau groupe de travail. */ +static PyObject *py_work_queue_define_group(PyObject *, PyObject *); + +/* Dissout un groupe de travail existant. */ +static PyObject *py_work_queue_delete_group(PyObject *, PyObject *); + +/* Place une nouvelle tâche en attente. */ +static PyObject *py_work_queue_schedule(PyObject *, PyObject *); + +/* Détermine si un groupe est vide de toute programmation. */ +static PyObject *py_work_queue_is_empty(PyObject *, PyObject *); + +/* Attend que toutes les tâches d'un groupe soient traitées. */ +static PyObject *py_work_queue_wait_for_completion(PyObject *, PyObject *); + +/* Force un réveil d'une attente en cours pour la confirmer. */ +static PyObject *py_work_queue_wake_up_waiters(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_work_queue_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + int ret; /* Bilan de lecture des args. */ + +#define WORK_QUEUE_DOC \ + "WorkQueue defines a basic work aimed to get processed in a" \ + " thread setup by a pychrysalide.glibext.WorkQueue instance.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " WorkQueue()" + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + return 0; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* CONNEXION AVEC L'API DE PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Constitue un nouveau groupe de travail. * +* * +* Retour : Nouvel identifiant unique d'un nouveau groupe de travail. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_define_group(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned int count; /* Nombre de threads à activer */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + wgroup_id_t id; /* Identifiant de groupe créé */ + +#define WORK_QUEUE_DEFINE_GROUP_METHOD PYTHON_METHOD_DEF \ +( \ + define_group, "$self, /, count=0", \ + METH_VARARGS, py_work_queue, \ + "Create a new work group for a work queue.\n" \ + "\n" \ + "The *count* argument states the number of threads which will" \ + " be activated for the processing. If the provided value is" \ + " zero, then a default value based on the current hardware is" \ + " selected.\n" \ + "\n" \ + "The result is a work group identifier." \ +) + + count = 0; + + ret = PyArg_ParseTuple(args, "|I", &count); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + id = g_work_queue_define_group(queue, count); + + result = PyLong_FromUnsignedLong(id); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Dissout un groupe de travail existant. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_delete_group(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + +#define WORK_QUEUE_DELETE_GROUP_METHOD PYTHON_METHOD_DEF \ +( \ + delete_group, "$self, id", \ + METH_VARARGS, py_work_queue, \ + "Destroy a work group previously registered inside a queue.\n" \ + "\n" \ + "The *id* argument refers to a work group identifier. It" \ + " should have been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group()." \ +) + + ret = PyArg_ParseTuple(args, "K", &id); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + g_work_queue_delete_group(queue, id); + + result = Py_None; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Place une nouvelle tâche en attente. * +* * +* Retour : Bilan, qui correspond à l'existence du groupe ciblé. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_schedule(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GGenericWork *work; /* Tâche à programmer */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + bool status; /* Bilan de programmation */ + +#define WORK_QUEUE_SCHEDULE_METHOD PYTHON_METHOD_DEF \ +( \ + schedule, "$self, work, id", \ + METH_VARARGS, py_work_queue, \ + "Schedule a work into the queue processing.\n" \ + "\n" \ + "The *work* argument has to point to a" \ + " pychrysalide.glibext.GenericWork instance, and the *id*" \ + " argument refers to a work group identifier which should have" \ + " been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group().\n"\ + "\n" \ + "The result is a boolean value: *True* in case of succes (does" \ + " the provided identifier exist?), *False* otherwise." \ +) + + ret = PyArg_ParseTuple(args, "O&K", convert_to_generic_work, &work, &id); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + status = g_work_queue_schedule(queue, work, id); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Détermine si un groupe est vide de toute programmation. * +* * +* Retour : Etat du groupe de travail. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_is_empty(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + bool status; /* Bilan de consultation */ + +#define WORK_QUEUE_IS_EMPTY_METHOD PYTHON_METHOD_DEF \ +( \ + is_empty, "$self, id", \ + METH_VARARGS, py_work_queue, \ + "Signal if there is some remaining work to get processed by" \ + " the queue or not.\n" \ + "\n" \ + "The *id* argument refers to a work group identifier. It" \ + " should have been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group().\n"\ + "\n" \ + "The result is a boolean value: *True* if the group does not" \ + " contain any works or does not exist, *False* otherwise." \ +) + + ret = PyArg_ParseTuple(args, "K", &id); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + status = g_work_queue_is_empty(queue, id); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Attend que toutes les tâches d'un groupe soient traitées. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_wait_for_completion(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned long long rel; /* Durée d'attente */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + bool status; /* Bilan de l'attente menée */ + + /** + * Python appelle cette fonction, lorsqu'elle est sollicitée dans des scripts, + * via la fonction PyObject_Vectorcall(), qui obtient la structure de suivi + * du thread courant avec _PyThreadState_GET(). + * + * Un commentaire associé à cette dernière fonction précise : + * + * The caller must hold the GIL. + * + * Comme le verrou GIL est posé par Python au moment de l'exécution de + * py_work_queue_wait_for_completion(), aucune attente bloquante ne peut + * alors être engagée ici : elle bloquerait la tâche _run() d'un travail + * réalisé en Python(), comme son déférencement via unref_ojbect() qui + * implique une exécution de la fonction pyg_toggle_notify() appelant + * PyGILState_Ensure() dans python3-gi. + * + * Aussi une attente bornée dans le temps est la seule option possible + * ici. + */ + +#define WORK_QUEUE_WAIT_FOR_COMPLETION_METHOD PYTHON_METHOD_DEF \ +( \ + wait_for_completion, "$self, id, /, rel=100000", \ + METH_VARARGS, py_work_queue, \ + "Wait until all the works from a given group have been" \ + " processed.\n" \ + "\n" \ + "The *id* argument refers to a work group identifier. It" \ + " should have been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group()." \ + " The *rel* range provide a relative time to wait (100ms by" \ + " default).\n" \ + "\n" \ + "The returned value is a boolean status: *False* on a timeout," \ + " *True* otherwise." \ + "\n" \ + "_Important note:_\n" \ + "As this function is called by Python with its Global" \ + " Interpreter Lock (GIL) held, an endless wait is impossible" \ + " because it would keep the GIL unreleased whereas others" \ + " threads (including those which may end the wait) may need to" \ + " acquire it.\n" \ + "\n" \ + "So an unoptimized timed wait is performed here instead, and" \ + " the returned status has to be checked by the caller in order" \ + " to schedule another wait if needed." \ +) + + rel = 100 * G_TIME_SPAN_MILLISECOND; + + ret = PyArg_ParseTuple(args, "K|K", &id, &rel); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + status = g_work_queue_wait_timed_for_completion(queue, id, rel); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = élément d'appel à consulter. * +* args = arguments fournis pour l'opération. * +* * +* Description : Force un réveil d'une attente en cours pour la confirmer. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_work_queue_wake_up_waiters(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + unsigned long long id; /* Identifiant de groupe ciblé */ + int ret; /* Bilan de lecture des args. */ + GWorkQueue *queue; /* Version native */ + +#define WORK_QUEUE_WAKE_UP_WAITERS_METHOD PYTHON_METHOD_DEF \ +( \ + wake_up_waiters, "$self, id", \ + METH_VARARGS, py_work_queue, \ + "Force a wake up of all threads waiting for processing a given" \ + " queue work group.\n" \ + "\n" \ + "The *id* argument refers to a work group identifier. It" \ + " should have been provided by a previous call to" \ + " pychrysalide.glibext.WorkQueue.g_work_queue_define_group()." \ +) + + ret = PyArg_ParseTuple(args, "K", &id); + if (!ret) return NULL; + + queue = G_WORK_QUEUE(pygobject_get(self)); + + g_work_queue_wake_up_waiters(queue, id); + + result = Py_None; + Py_INCREF(result); + + 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_work_queue_type(void) +{ + static PyMethodDef py_work_queue_methods[] = { + WORK_QUEUE_DEFINE_GROUP_METHOD, + WORK_QUEUE_DELETE_GROUP_METHOD, + WORK_QUEUE_SCHEDULE_METHOD, + WORK_QUEUE_IS_EMPTY_METHOD, + WORK_QUEUE_WAIT_FOR_COMPLETION_METHOD, + WORK_QUEUE_WAKE_UP_WAITERS_METHOD, + { NULL } + }; + + static PyGetSetDef py_work_queue_getseters[] = { + { NULL } + }; + + static PyTypeObject py_work_queue_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.glibext.WorkQueue", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = WORK_QUEUE_DOC, + + .tp_methods = py_work_queue_methods, + .tp_getset = py_work_queue_getseters, + + .tp_init = py_work_queue_init, + .tp_new = py_work_queue_new, + + }; + + return &py_work_queue_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.glibext.WorkQueue'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_work_queue_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'WorkQueue' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_work_queue_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.glibext"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_WORK_QUEUE, type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en gestionnaire de travaux différés. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_work_queue(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_work_queue_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to work queue"); + break; + + case 1: + *((GWorkQueue **)dst) = G_WORK_QUEUE(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/glibext/workqueue.h b/plugins/pychrysalide/glibext/workqueue.h new file mode 100644 index 0000000..3fe426b --- /dev/null +++ b/plugins/pychrysalide/glibext/workqueue.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * workqueue.h - prototypes pour l'équivalent Python du fichier "glibext/workqueue.h" + * + * Copyright (C) 2024 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_GLIBEXT_WORKQUEUE_H +#define _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORKQUEUE_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_work_queue_type(void); + +/* Prend en charge l'objet 'pychrysalide.glibext.WorkQueue'. */ +bool ensure_python_work_queue_is_registered(void); + +/* Tente de convertir en gestionnaire de travaux différés. */ +int convert_to_work_queue(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_GLIBEXT_WORKQUEUE_H */ |