From 6ea1b9a8550adf84cde510c2d4446c5120c4d065 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Tue, 7 Jul 2020 23:52:13 +0200 Subject: Saved the first steps toward a new serialization system. --- configure.ac | 2 + plugins/pychrysalide/analysis/Makefile.am | 9 +- plugins/pychrysalide/analysis/module.c | 3 + plugins/pychrysalide/analysis/storage/Makefile.am | 27 ++ plugins/pychrysalide/analysis/storage/cache.c | 359 +++++++++++++++ plugins/pychrysalide/analysis/storage/cache.h | 45 ++ plugins/pychrysalide/analysis/storage/container.c | 455 +++++++++++++++++++ plugins/pychrysalide/analysis/storage/container.h | 45 ++ plugins/pychrysalide/analysis/storage/module.c | 115 +++++ plugins/pychrysalide/analysis/storage/module.h | 42 ++ plugins/pychrysalide/analysis/storage/serialize.c | 513 +++++++++++++++++++++ plugins/pychrysalide/analysis/storage/serialize.h | 45 ++ plugins/pychrysalide/analysis/storage/storage.c | 516 ++++++++++++++++++++++ plugins/pychrysalide/analysis/storage/storage.h | 48 ++ plugins/pychrysalide/analysis/storage/tpmem.c | 508 +++++++++++++++++++++ plugins/pychrysalide/analysis/storage/tpmem.h | 45 ++ src/analysis/Makefile.am | 3 +- src/analysis/storage/Makefile.am | 30 ++ src/analysis/storage/cache-int.h | 62 +++ src/analysis/storage/cache.c | 334 ++++++++++++++ src/analysis/storage/cache.h | 62 +++ src/analysis/storage/container-int.h | 62 +++ src/analysis/storage/container.c | 140 ++++++ src/analysis/storage/container.h | 66 +++ src/analysis/storage/serialize-int.h | 55 +++ src/analysis/storage/serialize.c | 119 +++++ src/analysis/storage/serialize.h | 65 +++ src/analysis/storage/storage-int.h | 66 +++ src/analysis/storage/storage.c | 436 ++++++++++++++++++ src/analysis/storage/storage.h | 70 +++ src/analysis/storage/tpmem.c | 454 +++++++++++++++++++ src/analysis/storage/tpmem.h | 70 +++ 32 files changed, 4866 insertions(+), 5 deletions(-) create mode 100644 plugins/pychrysalide/analysis/storage/Makefile.am create mode 100644 plugins/pychrysalide/analysis/storage/cache.c create mode 100644 plugins/pychrysalide/analysis/storage/cache.h create mode 100644 plugins/pychrysalide/analysis/storage/container.c create mode 100644 plugins/pychrysalide/analysis/storage/container.h create mode 100644 plugins/pychrysalide/analysis/storage/module.c create mode 100644 plugins/pychrysalide/analysis/storage/module.h create mode 100644 plugins/pychrysalide/analysis/storage/serialize.c create mode 100644 plugins/pychrysalide/analysis/storage/serialize.h create mode 100644 plugins/pychrysalide/analysis/storage/storage.c create mode 100644 plugins/pychrysalide/analysis/storage/storage.h create mode 100644 plugins/pychrysalide/analysis/storage/tpmem.c create mode 100644 plugins/pychrysalide/analysis/storage/tpmem.h create mode 100644 src/analysis/storage/Makefile.am create mode 100644 src/analysis/storage/cache-int.h create mode 100644 src/analysis/storage/cache.c create mode 100644 src/analysis/storage/cache.h create mode 100644 src/analysis/storage/container-int.h create mode 100644 src/analysis/storage/container.c create mode 100644 src/analysis/storage/container.h create mode 100644 src/analysis/storage/serialize-int.h create mode 100644 src/analysis/storage/serialize.c create mode 100644 src/analysis/storage/serialize.h create mode 100644 src/analysis/storage/storage-int.h create mode 100644 src/analysis/storage/storage.c create mode 100644 src/analysis/storage/storage.h create mode 100644 src/analysis/storage/tpmem.c create mode 100644 src/analysis/storage/tpmem.h diff --git a/configure.ac b/configure.ac index a78cf1b..09ba2c9 100644 --- a/configure.ac +++ b/configure.ac @@ -447,6 +447,7 @@ AC_CONFIG_FILES([Makefile plugins/pychrysalide/analysis/db/Makefile plugins/pychrysalide/analysis/db/items/Makefile plugins/pychrysalide/analysis/disass/Makefile + plugins/pychrysalide/analysis/storage/Makefile plugins/pychrysalide/analysis/types/Makefile plugins/pychrysalide/arch/Makefile plugins/pychrysalide/arch/instructions/Makefile @@ -481,6 +482,7 @@ AC_CONFIG_FILES([Makefile src/analysis/disass/Makefile src/analysis/human/Makefile src/analysis/human/asm/Makefile + src/analysis/storage/Makefile src/analysis/types/Makefile src/arch/Makefile src/arch/instructions/Makefile diff --git a/plugins/pychrysalide/analysis/Makefile.am b/plugins/pychrysalide/analysis/Makefile.am index bb7bec6..d8c8bb1 100644 --- a/plugins/pychrysalide/analysis/Makefile.am +++ b/plugins/pychrysalide/analysis/Makefile.am @@ -15,10 +15,11 @@ libpychrysaanalysis_la_SOURCES = \ type.h type.c \ variable.h variable.c -libpychrysaanalysis_la_LIBADD = \ +libpychrysaanalysis_la_LIBADD = \ contents/libpychrysaanalysiscontents.la \ - db/libpychrysaanalysisdb.la \ - disass/libpychrysaanalysisdisass.la \ + db/libpychrysaanalysisdb.la \ + disass/libpychrysaanalysisdisass.la \ + storage/libpychrysaanalysisstorage.la \ types/libpychrysaanalysistypes.la libpychrysaanalysis_la_LDFLAGS = @@ -34,4 +35,4 @@ AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJE AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) -SUBDIRS = contents db disass types +SUBDIRS = contents db disass storage types diff --git a/plugins/pychrysalide/analysis/module.c b/plugins/pychrysalide/analysis/module.c index e82e32b..9632956 100644 --- a/plugins/pychrysalide/analysis/module.c +++ b/plugins/pychrysalide/analysis/module.c @@ -41,6 +41,7 @@ #include "contents/module.h" #include "db/module.h" #include "disass/module.h" +#include "storage/module.h" #include "types/module.h" #include "../helpers.h" @@ -85,6 +86,7 @@ bool add_analysis_module(PyObject *super) if (result) result = add_analysis_contents_module(module); if (result) result = add_analysis_db_module(module); if (result) result = add_analysis_disass_module(module); + if (result) result = add_analysis_storage_module(module); if (result) result = add_analysis_types_module(module); if (!result) @@ -129,6 +131,7 @@ bool populate_analysis_module(void) if (result) result = populate_analysis_contents_module(); if (result) result = populate_analysis_db_module(); if (result) result = populate_analysis_disass_module(); + if (result) result = populate_analysis_storage_module(); if (result) result = populate_analysis_types_module(); assert(result); diff --git a/plugins/pychrysalide/analysis/storage/Makefile.am b/plugins/pychrysalide/analysis/storage/Makefile.am new file mode 100644 index 0000000..863166d --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/Makefile.am @@ -0,0 +1,27 @@ + +noinst_LTLIBRARIES = libpychrysaanalysisstorage.la + +libpychrysaanalysisstorage_la_SOURCES = \ + cache.h cache.c \ + container.h container.c \ + module.h module.c \ + serialize.h serialize.c \ + storage.h storage.c \ + tpmem.h tpmem.c + +libpychrysaanalysisstorage_la_LIBADD = + +libpychrysaanalysisstorage_la_LDFLAGS = + + +devdir = $(includedir)/chrysalide-$(subdir) + +dev_HEADERS = $(libpychrysaanalysisstorage_la_SOURCES:%c=) + + +AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ + -I$(top_srcdir)/src + +AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) + +SUBDIRS = diff --git a/plugins/pychrysalide/analysis/storage/cache.c b/plugins/pychrysalide/analysis/storage/cache.c new file mode 100644 index 0000000..9859623 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/cache.c @@ -0,0 +1,359 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * cache.c - équivalent Python du fichier "analysis/storage/cache.c" + * + * 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 "cache.h" + + +#include + + +#include +#include + + +#include "container.h" +#include "../loaded.h" +#include "../../access.h" +#include "../../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Accompagne la création d'une instance dérivée en Python. */ +static PyObject *py_object_cache_new(PyTypeObject *, PyObject *, PyObject *); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_object_cache_init(PyObject *, PyObject *, PyObject *); + + + +/* -------------------------- TAMPON POUR CODE DESASSEMBLE -------------------------- */ + + +/* Introduit un contenu dans un cache d'objets. */ +static PyObject *py_object_cache_add(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : type = type du nouvel objet à mettre en place. * +* args = éventuelle liste d'arguments. * +* kwds = éventuel dictionnaire de valeurs mises à disposition. * +* * +* Description : Accompagne la création d'une instance dérivée en Python. * +* * +* Retour : Nouvel objet Python mis en place ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_object_cache_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Objet à retourner */ + PyTypeObject *base; /* Type de base à dériver */ + bool first_time; /* Evite les multiples passages*/ + GType gtype; /* Nouveau type de processeur */ + bool status; /* Bilan d'un enregistrement */ + + /* Validations diverses */ + + base = get_python_object_cache_type(); + + if (type == base) + goto simple_way; + + /* Mise en place d'un type dédié */ + + first_time = (g_type_from_name(type->tp_name) == 0); + + gtype = build_dynamic_type(G_TYPE_OBJECT_CACHE, type->tp_name, NULL, NULL, NULL); + + if (first_time) + { + status = register_class_for_dynamic_pygobject(gtype, type, base); + + if (!status) + { + result = NULL; + goto exit; + } + + } + + /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */ + + simple_way: + + result = PyType_GenericNew(type, args, kwds); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* 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_object_cache_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + GLoadedContent *loaded; /* Contenu chargé et traité */ + int ret; /* Bilan de lecture des args. */ + GObjectCache *cache; /* Mécanismes natifs */ + +#define OBJECT_CACHE_DOC \ + "The ObjectCache object manages a cache built for reducing the" \ + " overall memory footprint by releasing partial content of" \ + " pychrysalide.analysis.storage.CacheContainer objects.\n" \ + "\n" \ + "Disassembled instructions are the typical objects targeted by" \ + " this feature, through serialization.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " ObjectCache(loaded)" \ + "\n" \ + "Where *loaded* is a pychrysalide.analysis.LoadedContent instance" \ + " linked to the processed objects." + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&", convert_to_loaded_content, &loaded); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + /* Eléments de base */ + + cache = G_OBJECT_CACHE(pygobject_get(self)); + + if (!g_object_cache_open_for(cache, loaded)) + return -1; + + return 0; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* TAMPON POUR CODE DESASSEMBLE */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * +* Description : Introduit un contenu dans un cache d'objets. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_object_cache_add(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GCacheContainer *container; /* Conteneur à intégrer */ + int ret; /* Bilan de lecture des args. */ + GObjectCache *cache; /* Cache en version native */ + +#define OBJECT_CACHE_ADD_METHOD PYTHON_METHOD_DEF \ +( \ + add, "$self, container, /", \ + METH_VARARGS, py_object_cache, \ + "Introduce a new content to the object cache system.\n" \ + "\n" \ + "The *container* object must implement the" \ + " pychrysalide.analysis.storage.CacheContainer interface." \ +) + + ret = PyArg_ParseTuple(args, "O&", convert_to_cache_container, &container); + if (!ret) return NULL; + + cache = G_OBJECT_CACHE(pygobject_get(self)); + + g_object_cache_add(cache, container); + + 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_object_cache_type(void) +{ + static PyMethodDef py_object_cache_methods[] = { + OBJECT_CACHE_ADD_METHOD, + { NULL } + }; + + static PyGetSetDef py_object_cache_getseters[] = { + { NULL } + }; + + static PyTypeObject py_object_cache_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.analysis.cache.ObjectCache", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = OBJECT_CACHE_DOC, + + .tp_methods = py_object_cache_methods, + .tp_getset = py_object_cache_getseters, + + .tp_init = py_object_cache_init, + .tp_new = py_object_cache_new + + }; + + return &py_object_cache_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide....ObjectCache'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_object_cache_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'ObjectCache' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_object_cache_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.analysis.storage"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_OBJECT_CACHE, type, &PyGObject_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 cache d'objets. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_object_cache(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_object_cache_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 object cache"); + break; + + case 1: + *((GObjectCache **)dst) = G_OBJECT_CACHE(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/analysis/storage/cache.h b/plugins/pychrysalide/analysis/storage/cache.h new file mode 100644 index 0000000..d9ef541 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/cache.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * cache.h - prototypes pour l'équivalent Python du fichier "analysis/storage/cache.h" + * + * 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_ANALYSIS_STORAGE_CACHE_H +#define _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_CACHE_H + + +#include +#include + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_object_cache_type(void); + +/* Prend en charge l'objet 'pychrysalide.analysis.cache.ObjectCache'. */ +bool ensure_python_object_cache_is_registered(void); + +/* Tente de convertir en cache d'objets. */ +int convert_to_object_cache(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_CACHE_H */ diff --git a/plugins/pychrysalide/analysis/storage/container.c b/plugins/pychrysalide/analysis/storage/container.c new file mode 100644 index 0000000..d32c689 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/container.c @@ -0,0 +1,455 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * container.c - équivalent Python du fichier "analysis/storage/container.h" + * + * 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 "container.h" + + +#include + + +#include + + +#include "../../access.h" +#include "../../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Procède à l'initialisation de l'interface de génération. */ +static void py_cache_container_interface_init(GCacheContainerIface *, gpointer *); + +/* Contrôle l'accès au contenu d'un conteneur. */ +static void py_cache_container_lock_unlock_wrapper(GCacheContainer *, bool); + +/* Indique si le contenu d'un conteneur peut être mis en cache. */ +static bool py_cache_container_can_store_wrapper(GCacheContainer *); + + + +/* ------------------------- CONNEXION AVEC L'API DE PYTHON ------------------------- */ + + +/* Contrôle l'accès au contenu d'un conteneur. */ +static bool py_cache_container_lock_unlock(PyObject *, PyObject *); + +/* Indique si le contenu d'un conteneur peut être mis en cache. */ +static bool py_cache_container_can_store(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : iface = interface GLib à initialiser. * +* unused = adresse non utilisée ici. * +* * +* Description : Procède à l'initialisation de l'interface de génération. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_cache_container_interface_init(GCacheContainerIface *iface, gpointer *unused) +{ + +#define CACHE_CONTAINER_DOC \ + "CacheContainer defines an interface for objects with content allowed" \ + " to get cached.\n" \ + "\n" \ + "A typical class declaration for a new implementation looks like:\n" \ + "\n" \ + " class NewImplem(GObject.Object, CacheContainer):\n" \ + " ...\n" \ + "\n" \ + "The following methods have to be defined for new implementations:\n" \ + "* pychrysalide.analysis.storage.CacheContainer._lock_unlock();\n" \ + "* pychrysalide.analysis.storage.CacheContainer._can_store();\n" + + iface->lock_unlock = py_cache_container_lock_unlock_wrapper; + iface->can_store = py_cache_container_can_store_wrapper; + +} + + +/****************************************************************************** +* * +* Paramètres : container = conteneur à manipuler. * +* lock = indique une demande de verrou. * +* * +* Description : Contrôle l'accès au contenu d'un conteneur. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_cache_container_lock_unlock_wrapper(GCacheContainer *container, bool lock) +{ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *lock_obj; /* Objet Python à emmployer */ + PyObject *args; /* Arguments pour l'appel */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Bilan de consultation */ + +#define CACHE_CONTAINER_LOCK_UNLOCK_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _lock_unlock, "$self, lock, /", \ + METH_VARARGS, \ + "Abstract method used to lock or to unlock access to cache container" \ + " internals.\n" \ + "\n" \ + "The content of such a cache can then be accessed safely, without the" \ + " fear of race condition while processing.\n" \ + "\n" \ + "The *lock* argument is a boolean value indicating the state to" \ + " achieve." \ +) + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(container)); + + if (has_python_method(pyobj, "_lock_unlock")) + { + lock_obj = lock ? Py_True : Py_False; + Py_INCREF(lock_obj); + + args = PyTuple_New(1); + PyTuple_SetItem(args, 0, lock_obj); + + pyret = run_python_method(pyobj, "_lock_unlock", args); + + Py_XDECREF(pyret); + + Py_DECREF(args); + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + +} + + +/****************************************************************************** +* * +* Paramètres : container = conteneur à consulter. * +* * +* Description : Indique si le contenu d'un conteneur peut être mis en cache. * +* * +* Retour : Bilan de la consultation. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_cache_container_can_store_wrapper(GCacheContainer *container) +{ + bool result; /* Bilan à retourner */ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Bilan de consultation */ + +#define CACHE_CONTAINER_CAN_STORE_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _can_store, "$self, /", \ + METH_NOARGS, \ + "Abstract method used to define if a container can cache its" \ + " content.\n" \ + "\n" \ + "This kind of operation is not wished if the content is currently" \ + " in use.\n" \ + "\n" \ + "The result is a boolean indicating the capacity of safely" \ + " building a cache." \ +) + + result = false; + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(container)); + + if (has_python_method(pyobj, "_can_store")) + { + pyret = run_python_method(pyobj, "_can_store", NULL); + + result = (pyret == Py_True ? true : false); + + Py_XDECREF(pyret); + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + + return result; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* CONNEXION AVEC L'API DE PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant un générateur à manipuler. * +* args = arguments fournis à l'appel. * +* * +* Description : Contrôle l'accès au contenu d'un conteneur. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_cache_container_lock_unlock(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + int lock; /* Type de demande de verrou */ + int ret; /* Bilan de lecture des args. */ + GCacheContainer *object; /* Version native */ + +#define CACHE_CONTAINER_LOCK_UNLOCK_METHOD PYTHON_METHOD_DEF \ +( \ + lock_unlock, "$self, lock, /", \ + METH_VARARGS, py_cache_container, \ + "Lock or unlock access to cache container internals.\n" \ + "\n" \ + "The content of such a cache can then be accessed safely, without the" \ + " fear of race condition while processing.\n" \ + "\n" \ + "The *lock* argument is a boolean value indicating the state to" \ + " achieve." \ +) + + ret = PyArg_ParseTuple(args, "p", &lock); + if (!ret) return NULL; + + object = G_CACHE_CONTAINER(pygobject_get(self)); + + g_cache_container_lock_unlock(object, lock); + + result = Py_None; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant un générateur à manipuler. * +* args = arguments fournis à l'appel. * +* * +* Description : Indique si le contenu d'un conteneur peut être mis en cache. * +* * +* Retour : Bilan de la consultation. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_cache_container_can_store(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GCacheContainer *object; /* Version native */ + bool status; /* Bilan de l'opération */ + +#define CACHE_CONTAINER_CAN_STORE_METHOD PYTHON_METHOD_DEF \ +( \ + can_store, "$self, /", \ + METH_NOARGS, py_cache_container, \ + "Define if a container can cache its content.\n" \ + "\n" \ + "This kind of operation is not wished if the content is currently" \ + " in use.\n" \ + "\n" \ + "The result is a boolean indicating the capacity of safely" \ + " building a cache." \ +) + + object = G_CACHE_CONTAINER(pygobject_get(self)); + + status = g_cache_container_can_store(object); + + result = status ? Py_True : Py_False; + 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_cache_container_type(void) +{ + static PyMethodDef py_cache_container_methods[] = { + CACHE_CONTAINER_LOCK_UNLOCK_WRAPPER, + CACHE_CONTAINER_CAN_STORE_WRAPPER, + CACHE_CONTAINER_LOCK_UNLOCK_METHOD, + CACHE_CONTAINER_CAN_STORE_METHOD, + { NULL } + }; + + static PyGetSetDef py_cache_container_getseters[] = { + { NULL } + }; + + static PyTypeObject py_cache_container_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.analysis.storage.CacheContainer", + .tp_basicsize = sizeof(PyObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = CACHE_CONTAINER_DOC, + + .tp_methods = py_cache_container_methods, + .tp_getset = py_cache_container_getseters, + + }; + + return &py_cache_container_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide....CacheContainer'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_cache_container_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'CacheContainer'*/ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + static GInterfaceInfo info = { /* Paramètres d'inscription */ + + .interface_init = (GInterfaceInitFunc)py_cache_container_interface_init, + .interface_finalize = NULL, + .interface_data = NULL, + + }; + + type = get_python_cache_container_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.analysis.storage"); + + dict = PyModule_GetDict(module); + + if (!register_interface_for_pygobject_2(dict, G_TYPE_CACHE_CONTAINER, type, &info)) + 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 conteneur d'objets entreposables. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_cache_container(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_cache_container_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 cache container"); + break; + + case 1: + *((GCacheContainer **)dst) = G_CACHE_CONTAINER(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/analysis/storage/container.h b/plugins/pychrysalide/analysis/storage/container.h new file mode 100644 index 0000000..3960dd8 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/container.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * container.h - prototypes pour l'équivalent Python du fichier "analysis/storage/container.h" + * + * 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_ANALYSIS_STORAGE_CONTAINER_H +#define _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_CONTAINER_H + + +#include +#include + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_cache_container_type(void); + +/* Prend en charge l'objet 'pychrysalide.analysis.storage.CacheContainer'. */ +bool ensure_python_cache_container_is_registered(void); + +/* Tente de convertir en conteneur d'objets entreposables. */ +int convert_to_cache_container(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_CONTAINER_H */ diff --git a/plugins/pychrysalide/analysis/storage/module.c b/plugins/pychrysalide/analysis/storage/module.c new file mode 100644 index 0000000..d2ac397 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/module.c @@ -0,0 +1,115 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.c - intégration du répertoire storage en tant que module + * + * 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 "module.h" + + +#include + + +#include "cache.h" +#include "container.h" +#include "serialize.h" +#include "storage.h" +#include "tpmem.h" +#include "../../helpers.h" + + + +/****************************************************************************** +* * +* Paramètres : super = module dont la définition est à compléter. * +* * +* Description : Ajoute le module 'analysis.storage' à un module Python. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_analysis_storage_module(PyObject *super) +{ + bool result; /* Bilan à retourner */ + PyObject *module; /* Sous-module mis en place */ + +#define PYCHRYSALIDE_ANALYSIS_STORAGE_MODULE_DOC \ + "This module gathers all the features relative to serialization.\n" \ + "\n" \ + "This serialization is used for object caching and disassembly" \ + " results storage." + + static PyModuleDef py_chrysalide_analysis_db_module = { + + .m_base = PyModuleDef_HEAD_INIT, + + .m_name = "pychrysalide.analysis.storage", + .m_doc = PYCHRYSALIDE_ANALYSIS_STORAGE_MODULE_DOC, + + .m_size = -1, + + }; + + module = build_python_module(super, &py_chrysalide_analysis_db_module); + + result = (module != NULL); + + if (!result) + Py_XDECREF(module); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Intègre les objets du module 'analysis.storage'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool populate_analysis_storage_module(void) +{ + bool result; /* Bilan à retourner */ + + result = true; + + if (result) result = ensure_python_cache_container_is_registered(); + if (result) result = ensure_python_serializable_object_is_registered(); + + if (result) result = ensure_python_object_cache_is_registered(); + if (result) result = ensure_python_object_storage_is_registered(); + if (result) result = ensure_python_type_memory_is_registered(); + + assert(result); + + return result; + +} diff --git a/plugins/pychrysalide/analysis/storage/module.h b/plugins/pychrysalide/analysis/storage/module.h new file mode 100644 index 0000000..c2a0181 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/module.h @@ -0,0 +1,42 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.h - prototypes pour l'intégration du répertoire storage en tant que module + * + * 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_ANALYSIS_STORAGE_MODULE_H +#define _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_MODULE_H + + +#include +#include + + + +/* Ajoute le module 'analysis.storage' à un module Python. */ +bool add_analysis_storage_module(PyObject *); + +/* Intègre les objets du module 'analysis.storage'. */ +bool populate_analysis_storage_module(void); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_MODULE_H */ diff --git a/plugins/pychrysalide/analysis/storage/serialize.c b/plugins/pychrysalide/analysis/storage/serialize.c new file mode 100644 index 0000000..67e1256 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/serialize.c @@ -0,0 +1,513 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * serialize.c - équivalent Python du fichier "analysis/storage/serialize.h" + * + * 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 "serialize.h" + + +#include + + +#include + + +#include "storage.h" +#include "tpmem.h" +#include "../../access.h" +#include "../../helpers.h" +#include "../../common/packed.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Procède à l'initialisation de l'interface de génération. */ +static void py_serializable_object_interface_init(GSerializableObjectIface *, gpointer *); + +/* Charge un objet depuis une mémoire tampon. */ +static bool py_serializable_object_load_wrapper(GSerializableObject *, GObjectStorage *, GTypeMemory *, packed_buffer *); + +/* Sauvegarde un objet dans une mémoire tampon. */ +static bool py_serializable_object_store_wrapper(const GSerializableObject *, GObjectStorage *, GTypeMemory *, packed_buffer *); + + + +/* ------------------------- CONNEXION AVEC L'API DE PYTHON ------------------------- */ + + + +/* Charge un objet depuis une mémoire tampon. */ +static bool py_serializable_object_load(PyObject *, PyObject *); + +/* Sauvegarde un objet dans une mémoire tampon. */ +static bool py_serializable_object_store(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : iface = interface GLib à initialiser. * +* unused = adresse non utilisée ici. * +* * +* Description : Procède à l'initialisation de l'interface de génération. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_serializable_object_interface_init(GSerializableObjectIface *iface, gpointer *unused) +{ + +#define SERIALIZABLE_OBJECT_DOC \ + "SerializableObject defines an interface used to store and load" \ + " objects to and from a data buffer.\n" \ + "\n" \ + "A typical class declaration for a new implementation looks like:\n" \ + "\n" \ + " class NewImplem(GObject.Object, SerializableObject):\n" \ + " ...\n" \ + "\n" \ + "The following methods have to be defined for new implementations:\n" \ + "* pychrysalide.analysis.storage.SerializableObject._load();\n" \ + "* pychrysalide.analysis.storage.SerializableObject._store();\n" \ + + iface->load = py_serializable_object_load_wrapper; + iface->store = py_serializable_object_store_wrapper; + +} + + +/****************************************************************************** +* * +* Paramètres : object = instruction d'assemblage à consulter. * +* storage = conservateur de données à manipuler ou NULL. * +* tpmem = mémoire des types d'objets à compléter. * +* pbuf = zone tampon à remplir. * +* * +* Description : Charge un objet depuis une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_serializable_object_load_wrapper(GSerializableObject *object, GObjectStorage *storage, GTypeMemory *tpmem, packed_buffer *pbuf) +{ + bool result; /* Bilan à retourner */ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *storage_obj; /* Objet Python à emmployer */ + PyObject *args; /* Arguments pour l'appel */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Bilan de consultation */ + +#define SERIALIZABLE_OBJECT_LOAD_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _load, "$self, storage, tpmem, pbuf, /", \ + METH_VARARGS, \ + "Abstract method used to load an object definition from buffered data.\n" \ + "\n" \ + "The *storage* is a pychrysalide.analysis.storage.ObjectStorage instance" \ + " provided to store inner objects, if relevant, or None. The *tpmem* object"\ + " is a pychrysalide.analysis.storage.TypeMemory remembering all involved" \ + " GLib types. The *pbuf* argument points to a" \ + " pychrysalide.common.PackedBuffer object containing the data to process.\n"\ + "\n" \ + "The result is a boolean indicating the status of the operation." \ +) + + result = false; + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(object)); + + if (has_python_method(pyobj, "_load")) + { + if (storage == NULL) + { + storage_obj = Py_None; + Py_INCREF(storage_obj); + } + else + storage_obj = pygobject_new(G_OBJECT(storage)); + + args = PyTuple_New(3); + PyTuple_SetItem(args, 0, storage_obj); + PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(tpmem))); + PyTuple_SetItem(args, 2, build_from_internal_packed_buffer(pbuf)); + + pyret = run_python_method(pyobj, "_load", args); + + result = (pyret == Py_True ? true : false); + + Py_XDECREF(pyret); + + Py_DECREF(args); + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : object = instruction d'assemblage à consulter. * +* storage = conservateur de données à manipuler ou NULL. * +* tpmem = mémoire des types d'objets à compléter. * +* pbuf = zone tampon à remplir. * +* * +* Description : Sauvegarde un objet dans une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_serializable_object_store_wrapper(const GSerializableObject *object, GObjectStorage *storage, GTypeMemory *tpmem, packed_buffer *pbuf) +{ + bool result; /* Bilan à retourner */ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *storage_obj; /* Objet Python à emmployer */ + PyObject *args; /* Arguments pour l'appel */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Bilan de consultation */ + +#define SERIALIZABLE_OBJECT_STORE_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _store, "$self, storage, tpmem, pbuf, /", \ + METH_VARARGS, \ + "Abstract method used to store an object definition into buffered data.\n" \ + "\n" \ + "The *storage* is a pychrysalide.analysis.storage.ObjectStorage instance" \ + " provided to store inner objects, if relevant, or None. The *tpmem* object"\ + " is a pychrysalide.analysis.storage.TypeMemory remembering all involved" \ + " GLib types. The *pbuf* argument points to a" \ + " pychrysalide.common.PackedBuffer object containing the data to process.\n"\ + "\n" \ + "The result is a boolean indicating the status of the operation." \ +) + + result = false; + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(object)); + + if (has_python_method(pyobj, "_store")) + { + if (storage == NULL) + { + storage_obj = Py_None; + Py_INCREF(storage_obj); + } + else + storage_obj = pygobject_new(G_OBJECT(storage)); + + args = PyTuple_New(3); + PyTuple_SetItem(args, 0, storage_obj); + PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(tpmem))); + PyTuple_SetItem(args, 2, build_from_internal_packed_buffer(pbuf)); + + pyret = run_python_method(pyobj, "_store", args); + + result = (pyret == Py_True ? true : false); + + Py_XDECREF(pyret); + + Py_DECREF(args); + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + + return result; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* CONNEXION AVEC L'API DE PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant un générateur à manipuler. * +* args = arguments fournis à l'appel. * +* * +* Description : Charge un objet depuis une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_serializable_object_load(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GObjectStorage *storage; /* Conservateur à manipuler */ + GTypeMemory *tpmem; /* Gestionnaire de types */ + packed_buffer *pbuf; /* Tampon de données à employer*/ + int ret; /* Bilan de lecture des args. */ + GSerializableObject *object; /* Version native */ + bool status; /* Bilan de l'opération */ + +#define SERIALIZABLE_OBJECT_LOAD_METHOD PYTHON_METHOD_DEF \ +( \ + load, "$self, storage, tpmem, pbuf, /", \ + METH_VARARGS, py_serializable_object, \ + "Load an object definition from buffered data.\n" \ + "\n" \ + "The *storage* is a pychrysalide.analysis.storage.ObjectStorage instance" \ + " provided to store inner objects, if relevant, or None. The *tpmem* object"\ + " is a pychrysalide.analysis.storage.TypeMemory remembering all involved" \ + " GLib types. The *pbuf* argument points to a" \ + " pychrysalide.common.PackedBuffer object containing the data to process.\n"\ + "\n" \ + "The result is a boolean indicating the status of the operation." \ +) + + ret = PyArg_ParseTuple(args, "O&O&O&", convert_to_object_storage_or_none, &storage, + convert_to_type_memory, &tpmem, convert_to_packed_buffer, &pbuf); + if (!ret) return NULL; + + object = G_SERIALIZABLE_OBJECT(pygobject_get(self)); + + status = g_serializable_object_load(object, storage, tpmem, pbuf); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant un générateur à manipuler. * +* args = arguments fournis à l'appel. * +* * +* Description : Sauvegarde un objet dans une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_serializable_object_store(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GObjectStorage *storage; /* Conservateur à manipuler */ + GTypeMemory *tpmem; /* Gestionnaire de types */ + packed_buffer *pbuf; /* Tampon de données à employer*/ + int ret; /* Bilan de lecture des args. */ + GSerializableObject *object; /* Version native */ + bool status; /* Bilan de l'opération */ + +#define SERIALIZABLE_OBJECT_STORE_METHOD PYTHON_METHOD_DEF \ +( \ + store, "$self, storage, tpmem, pbuf, /", \ + METH_VARARGS, py_serializable_object, \ + "Store an object definition into buffered data.\n" \ + "\n" \ + "The *storage* is a pychrysalide.analysis.storage.ObjectStorage instance" \ + " provided to store inner objects, if relevant, or None. The *tpmem* object"\ + " is a pychrysalide.analysis.storage.TypeMemory remembering all involved" \ + " GLib types. The *pbuf* argument points to a" \ + " pychrysalide.common.PackedBuffer object containing the data to process.\n"\ + "\n" \ + "The result is a boolean indicating the status of the operation." \ +) + + ret = PyArg_ParseTuple(args, "O&O&O&", convert_to_object_storage_or_none, &storage, + convert_to_type_memory, &tpmem, convert_to_packed_buffer, &pbuf); + if (!ret) return NULL; + + object = G_SERIALIZABLE_OBJECT(pygobject_get(self)); + + status = g_serializable_object_store(object, storage, tpmem, pbuf); + + result = status ? Py_True : Py_False; + 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_serializable_object_type(void) +{ + static PyMethodDef py_serializable_object_methods[] = { + SERIALIZABLE_OBJECT_LOAD_WRAPPER, + SERIALIZABLE_OBJECT_STORE_WRAPPER, + SERIALIZABLE_OBJECT_LOAD_METHOD, + SERIALIZABLE_OBJECT_STORE_METHOD, + { NULL } + }; + + static PyGetSetDef py_serializable_object_getseters[] = { + { NULL } + }; + + static PyTypeObject py_serializable_object_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.analysis.storage.SerializableObject", + .tp_basicsize = sizeof(PyObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = SERIALIZABLE_OBJECT_DOC, + + .tp_methods = py_serializable_object_methods, + .tp_getset = py_serializable_object_getseters, + + }; + + return &py_serializable_object_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide....SerializableObject'.* +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_serializable_object_is_registered(void) +{ + PyTypeObject *type; /* Type 'SerializableObject' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + static GInterfaceInfo info = { /* Paramètres d'inscription */ + + .interface_init = (GInterfaceInitFunc)py_serializable_object_interface_init, + .interface_finalize = NULL, + .interface_data = NULL, + + }; + + type = get_python_serializable_object_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.analysis.storage"); + + dict = PyModule_GetDict(module); + + if (!register_interface_for_pygobject_2(dict, G_TYPE_SERIALIZABLE_OBJECT, type, &info)) + 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 objet adapté à une mise en cache. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_serializable_object(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_serializable_object_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 serializable object"); + break; + + case 1: + *((GSerializableObject **)dst) = G_SERIALIZABLE_OBJECT(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/analysis/storage/serialize.h b/plugins/pychrysalide/analysis/storage/serialize.h new file mode 100644 index 0000000..7e831e5 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/serialize.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * serialize.h - prototypes pour l'équivalent Python du fichier "analysis/storage/serialize.h" + * + * 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_ANALYSIS_STORAGE_SERIALIZE_H +#define _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_SERIALIZE_H + + +#include +#include + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_serializable_object_type(void); + +/* Prend en charge l'objet 'pychrysalide.analysis.storage.SerializableObject'. */ +bool ensure_python_serializable_object_is_registered(void); + +/* Tente de convertir en objet adapté à une mise en cache. */ +int convert_to_serializable_object(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_SERIALIZE_H */ diff --git a/plugins/pychrysalide/analysis/storage/storage.c b/plugins/pychrysalide/analysis/storage/storage.c new file mode 100644 index 0000000..d8739e4 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/storage.c @@ -0,0 +1,516 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * storage.c - équivalent Python du fichier "analysis/storage/storage.c" + * + * 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 "storage.h" + + +#include + + +#include +#include + + +#include "serialize.h" +#include "../loaded.h" +#include "../../access.h" +#include "../../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Accompagne la création d'une instance dérivée en Python. */ +static PyObject *py_object_storage_new(PyTypeObject *, PyObject *, PyObject *); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_object_storage_init(PyObject *, PyObject *, PyObject *); + + + +/* -------------------------- TAMPON POUR CODE DESASSEMBLE -------------------------- */ + + +/* Ajoute le support d'un nouveau groupe d'objets construits. */ +static PyObject *py_object_storage_add_backend(PyObject *, PyObject *); + +/* Charge un objet à partir de données rassemblées. */ +static PyObject *py_object_storage_load_object(PyObject *, PyObject *); + +/* Sauvegarde un object sous forme de données rassemblées. */ +static PyObject *py_object_storage_store_object(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : type = type du nouvel objet à mettre en place. * +* args = éventuelle liste d'arguments. * +* kwds = éventuel dictionnaire de valeurs mises à disposition. * +* * +* Description : Accompagne la création d'une instance dérivée en Python. * +* * +* Retour : Nouvel objet Python mis en place ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_object_storage_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Objet à retourner */ + PyTypeObject *base; /* Type de base à dériver */ + bool first_time; /* Evite les multiples passages*/ + GType gtype; /* Nouveau type de processeur */ + bool status; /* Bilan d'un enregistrement */ + + /* Validations diverses */ + + base = get_python_object_storage_type(); + + if (type == base) + goto simple_way; + + /* Mise en place d'un type dédié */ + + first_time = (g_type_from_name(type->tp_name) == 0); + + gtype = build_dynamic_type(G_TYPE_OBJECT_STORAGE, type->tp_name, NULL, NULL, NULL); + + if (first_time) + { + status = register_class_for_dynamic_pygobject(gtype, type, base); + + if (!status) + { + result = NULL; + goto exit; + } + + } + + /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */ + + simple_way: + + result = PyType_GenericNew(type, args, kwds); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* 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_object_storage_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + GLoadedContent *loaded; /* Contenu chargé et traité */ + int ret; /* Bilan de lecture des args. */ + GObjectStorage *storage; /* Mécanismes natifs */ + +#define OBJECT_STORAGE_DOC \ + "The ObjectStorage object manages the generic storage of GLib" \ + " objects through serialization.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " ObjectStorage(loaded)" \ + "\n" \ + "Where *loaded* is a pychrysalide.analysis.LoadedContent instance" \ + " linked to the objects which can apply for a storage process." + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&", convert_to_loaded_content, &loaded); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + /* Eléments de base */ + + storage = G_OBJECT_STORAGE(pygobject_get(self)); + + storage->loaded = loaded; + g_object_ref(G_OBJECT(loaded)); + + return 0; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* TAMPON POUR CODE DESASSEMBLE */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * +* Description : Ajoute le support d'un nouveau groupe d'objets construits. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_object_storage_add_backend(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + const char *name; /* Désignation de groupe */ + const char *filename; /* Nom de fichier à associer */ + int ret; /* Bilan de lecture des args. */ + GObjectStorage *storage; /* Mécanismes natifs */ + bool status; /* Bilan de l'opération */ + +#define OBJECT_STORAGE_ADD_BACKEND_METHOD PYTHON_METHOD_DEF \ +( \ + add_backend, "$self, name, /, filename", \ + METH_VARARGS, py_object_storage, \ + "Add storage support for a new kind of GLib objects.\n" \ + "\n" \ + "The *name* is a string label for the group of target objects" \ + " and the optional *filename* points to a file used to load" \ + " objects.\n" \ + "\n" \ + "The result is a boolean value indicating the status of" \ + " the operation: True for success, False for failure." \ +) + + filename = NULL; + + ret = PyArg_ParseTuple(args, "s|s", &name, &filename); + if (!ret) return NULL; + + storage = G_OBJECT_STORAGE(pygobject_get(self)); + + status = g_object_storage_add_backend(storage, name, filename); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * +* Description : Charge un objet à partir de données rassemblées. * +* * +* Retour : Objet restauré en mémoire ou None en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_object_storage_load_object(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + const char *name; /* Désignation de groupe */ + unsigned long long pos; /* Emplacement des données */ + int ret; /* Bilan de lecture des args. */ + GObjectStorage *storage; /* Mécanismes natifs */ + GSerializableObject *object; /* Objet reconstruit ou NULL */ + +#define OBJECT_STORAGE_LOAD_OBJECT_METHOD PYTHON_METHOD_DEF \ +( \ + load_object, "$self, name, pos, /", \ + METH_VARARGS, py_object_storage, \ + "Load an object from serialized data.\n" \ + "\n" \ + "The *name* is a string label for the group of target objects and" \ + " *pos* is an offset into the data stream indicating the start of" \ + " the data to unserialize.\n" \ + "\n" \ + "The result is a pychrysalide.analysis.storage.SerializableObject" \ + " instancet in case of success, or None in case of failure." \ +) + + ret = PyArg_ParseTuple(args, "sK", &name, &pos); + if (!ret) return NULL; + + storage = G_OBJECT_STORAGE(pygobject_get(self)); + + object = g_object_storage_load_object(storage, name, pos); + + if (object != NULL) + result = pygobject_new(G_OBJECT(object)); + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * +* Description : Sauvegarde un object sous forme de données rassemblées. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_object_storage_store_object(PyObject *self, PyObject *args) +{ + PyObject *result; /* Emplacement à retourner */ + const char *name; /* Désignation de groupe */ + GSerializableObject *object; /* Objet à traiter */ + int ret; /* Bilan de lecture des args. */ + GObjectStorage *storage; /* Mécanismes natifs */ + off64_t pos; /* Emplacement d'enregistrement*/ + bool status; /* Bilan de l'opération */ + +#define OBJECT_STORAGE_STORE_OBJECT_METHOD PYTHON_METHOD_DEF \ +( \ + store_object, "$self, name, object, /", \ + METH_VARARGS, py_object_storage, \ + "Save an object as serialized data.\n" \ + "\n" \ + "The *name* is a string label for the group of target objects" \ + " and the processed *object* has to be a" \ + " pychrysalide.analysis.storage.SerializableObject instance.\n" \ + "\n" \ + "The result is the position of the data for stored object," \ + " provided as an integer offset, in case of success or None" \ + " in case of failure." \ +) + + ret = PyArg_ParseTuple(args, "sO&", &name, convert_to_serializable_object, &object); + if (!ret) return NULL; + + storage = G_OBJECT_STORAGE(pygobject_get(self)); + + status = g_object_storage_store_object(storage, name, object, &pos); + + if (status) + result = PyLong_FromUnsignedLongLong((unsigned long long)pos); + else + { + 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_object_storage_type(void) +{ + static PyMethodDef py_object_storage_methods[] = { + OBJECT_STORAGE_ADD_BACKEND_METHOD, + OBJECT_STORAGE_LOAD_OBJECT_METHOD, + OBJECT_STORAGE_STORE_OBJECT_METHOD, + { NULL } + }; + + static PyGetSetDef py_object_storage_getseters[] = { + { NULL } + }; + + static PyTypeObject py_object_storage_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.analysis.storage.ObjectStorage", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = OBJECT_STORAGE_DOC, + + .tp_methods = py_object_storage_methods, + .tp_getset = py_object_storage_getseters, + + .tp_init = py_object_storage_init, + .tp_new = py_object_storage_new + + }; + + return &py_object_storage_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide....ObjectStorage'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_object_storage_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'ObjectStorage' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_object_storage_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.analysis.storage"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_OBJECT_STORAGE, type, &PyGObject_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 conservateur d'objets. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_object_storage(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_object_storage_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 object storage"); + break; + + case 1: + *((GObjectStorage **)dst) = G_OBJECT_STORAGE(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} + + +/****************************************************************************** +* * +* 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 conservateur d'objets ou NULL. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_object_storage_or_none(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + if (arg == Py_None) + { + *((GTypeMemory **)dst) = NULL; + result = 1; + } + + else + result = convert_to_object_storage(arg, dst); + + return result; + +} diff --git a/plugins/pychrysalide/analysis/storage/storage.h b/plugins/pychrysalide/analysis/storage/storage.h new file mode 100644 index 0000000..a0a2c18 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/storage.h @@ -0,0 +1,48 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * storage.h - prototypes pour l'équivalent Python du fichier "analysis/storage/storage.h" + * + * 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_ANALYSIS_STORAGE_STORAGE_H +#define _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_STORAGE_H + + +#include +#include + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_object_storage_type(void); + +/* Prend en charge l'objet 'pychrysalide.analysis.storage.ObjectStorage'. */ +bool ensure_python_object_storage_is_registered(void); + +/* Tente de convertir en conservateur d'objets. */ +int convert_to_object_storage(PyObject *, void *); + +/* Tente de convertir en conservateur d'objets ou NULL. */ +int convert_to_object_storage_or_none(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_STORAGE_H */ diff --git a/plugins/pychrysalide/analysis/storage/tpmem.c b/plugins/pychrysalide/analysis/storage/tpmem.c new file mode 100644 index 0000000..2cf659f --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/tpmem.c @@ -0,0 +1,508 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * tpmem.c - équivalent Python du fichier "analysis/storage/tpmem.c" + * + * 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 "tpmem.h" + + +#include + + +#include +#include + + +#include "../../access.h" +#include "../../helpers.h" +#include "../../common/packed.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Accompagne la création d'une instance dérivée en Python. */ +static PyObject *py_type_memory_new(PyTypeObject *, PyObject *, PyObject *); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_type_memory_init(PyObject *, PyObject *, PyObject *); + + + +/* -------------------------- TAMPON POUR CODE DESASSEMBLE -------------------------- */ + + +/* Apprend tous les types mémorisés dans un flux. */ +static PyObject *py_type_memory_read_types(PyObject *, PyObject *); + +/* Crée une nouvelle instance d'objet à partir de son type. */ +static PyObject *py_type_memory_create_object(PyObject *, PyObject *); + +/* Sauvegarde le type d'un objet instancié. */ +static PyObject *py_type_memory_store_object_gtype(PyObject *, PyObject *); + +/* Enregistre tous les types mémorisés dans un flux. */ +static PyObject *py_type_memory_write_types(PyObject *, PyObject *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : type = type du nouvel objet à mettre en place. * +* args = éventuelle liste d'arguments. * +* kwds = éventuel dictionnaire de valeurs mises à disposition. * +* * +* Description : Accompagne la création d'une instance dérivée en Python. * +* * +* Retour : Nouvel objet Python mis en place ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_type_memory_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Objet à retourner */ + PyTypeObject *base; /* Type de base à dériver */ + bool first_time; /* Evite les multiples passages*/ + GType gtype; /* Nouveau type de processeur */ + bool status; /* Bilan d'un enregistrement */ + + /* Validations diverses */ + + base = get_python_type_memory_type(); + + if (type == base) + goto simple_way; + + /* Mise en place d'un type dédié */ + + first_time = (g_type_from_name(type->tp_name) == 0); + + gtype = build_dynamic_type(G_TYPE_TYPE_MEMORY, type->tp_name, NULL, NULL, NULL); + + if (first_time) + { + status = register_class_for_dynamic_pygobject(gtype, type, base); + + if (!status) + { + result = NULL; + goto exit; + } + + } + + /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */ + + simple_way: + + result = PyType_GenericNew(type, args, kwds); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* 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_type_memory_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + int ret; /* Bilan de lecture des args. */ + +#define TYPE_MEMORY_DOC \ + "The TypeMemory remembers all the types of objects involved in" \ + " a serialization process.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " TypeMemory()" \ + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + return 0; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* TAMPON POUR CODE DESASSEMBLE */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * +* Description : Apprend tous les types mémorisés dans un flux. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_type_memory_read_types(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + int fd; /* Flux ouvert en lecture */ + int ret; /* Bilan de lecture des args. */ + GTypeMemory *tpmem; /* Mémorisation native */ + bool status; /* Bilan de l'opération */ + +#define TYPE_MEMORY_READ_TYPES_METHOD PYTHON_METHOD_DEF \ +( \ + read_types, "$self, fd", \ + METH_VARARGS, py_type_memory, \ + "Read types from a stream.\n" \ + "\n" \ + "This operation is usually handled internally by the" \ + " Chrysalide's core.\n" \ + "\n" \ + "The *fd* parameter is an integer representing a valid" \ + " identifier for a file descriptor opened for reading." \ + "\n" \ + "The result is a boolean value indicating the status of" \ + " the operation: True for success, False for failure." \ +) + + ret = PyArg_ParseTuple(args, "i", &fd); + if (!ret) return NULL; + + tpmem = G_TYPE_MEMORY(pygobject_get(self)); + + status = g_type_memory_read_types(tpmem, fd); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * +* Description : Crée une nouvelle instance d'objet à partir de son type. * +* * +* Retour : Instance issue de l'opération ou NULL. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_type_memory_create_object(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + packed_buffer *pbuf; /* Tampon à consulter */ + int ret; /* Bilan de lecture des args. */ + GTypeMemory *tpmem; /* Mémorisation native */ + GObject *obj; /* Instance retournée */ + +#define TYPE_MEMORY_CREATE_OBJECT_METHOD PYTHON_METHOD_DEF \ +( \ + create_object, "$self, pbuf", \ + METH_VARARGS, py_type_memory, \ + "Create a new GLib object from serialized data.\n" \ + "\n" \ + "The *pbuf* parameter is a pychrysalide.common.PackedBuffer" \ + " instance providing buffered data to read." \ + "\n" \ + "The result is a Python object linked to a native GLib" \ + " object instance." \ +) + + ret = PyArg_ParseTuple(args, "O&", convert_to_packed_buffer, &pbuf); + if (!ret) return NULL; + + tpmem = G_TYPE_MEMORY(pygobject_get(self)); + + obj = g_type_memory_create_object(tpmem, pbuf); + + result = pygobject_new(obj); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * +* Description : Sauvegarde le type d'un objet instancié. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_type_memory_store_object_gtype(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + GObject *obj; /* Instance à traiter */ + packed_buffer *pbuf; /* Tampon à consulter */ + int ret; /* Bilan de lecture des args. */ + GTypeMemory *tpmem; /* Mémorisation native */ + bool status; /* Bilan de l'opération */ + +#define TYPE_MEMORY_STORE_OBJECT_GTYPE_METHOD PYTHON_METHOD_DEF \ +( \ + store_object_gtype, "$self, obj, pbuf", \ + METH_VARARGS, py_type_memory, \ + "Create a new GLib object from serialized data.\n" \ + "\n" \ + "The *obj* parameter is the Python version of the GObject" \ + " whose type is to process and the *pbuf* parameter is a" \ + " pychrysalide.common.PackedBuffer instance providing buffered" \ + " data to extend." \ + "\n" \ + "The result is a boolean value indicating the status of the" \ + " operation: True for success, False for failure." \ +) + + ret = PyArg_ParseTuple(args, "O!O&", PyGObject_Type, &obj, convert_to_packed_buffer, &pbuf); + if (!ret) return NULL; + + tpmem = G_TYPE_MEMORY(pygobject_get(self)); + + status = g_type_memory_store_object_gtype(tpmem, obj, pbuf); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une mémorisation de types. * +* args = arguments fournis à l'appel. * +* * +* Description : Enregistre tous les types mémorisés dans un flux. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_type_memory_write_types(PyObject *self, PyObject *args) +{ + PyObject *result; /* Bilan à retourner */ + int fd; /* Flux ouvert en lecture */ + int ret; /* Bilan de lecture des args. */ + GTypeMemory *tpmem; /* Mémorisation native */ + bool status; /* Bilan de l'opération */ + +#define TYPE_MEMORY_WRITE_TYPES_METHOD PYTHON_METHOD_DEF \ +( \ + write_types, "$self, fd", \ + METH_VARARGS, py_type_memory, \ + "Write types into a stream.\n" \ + "\n" \ + "This operation is usually handled internally by the" \ + " Chrysalide's core.\n" \ + "\n" \ + "The *fd* parameter is an integer representing a valid" \ + " identifier for a file descriptor opened for writing." \ + "\n" \ + "The result is a boolean value indicating the status of" \ + " the operation: True for success, False for failure." \ +) + + ret = PyArg_ParseTuple(args, "i", &fd); + if (!ret) return NULL; + + tpmem = G_TYPE_MEMORY(pygobject_get(self)); + + status = g_type_memory_write_types(tpmem, fd); + + result = status ? Py_True : Py_False; + 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_type_memory_type(void) +{ + static PyMethodDef py_type_memory_methods[] = { + TYPE_MEMORY_READ_TYPES_METHOD, + TYPE_MEMORY_CREATE_OBJECT_METHOD, + TYPE_MEMORY_STORE_OBJECT_GTYPE_METHOD, + TYPE_MEMORY_WRITE_TYPES_METHOD, + { NULL } + }; + + static PyGetSetDef py_type_memory_getseters[] = { + { NULL } + }; + + static PyTypeObject py_type_memory_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.analysis.storage.TypeMemory", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = TYPE_MEMORY_DOC, + + .tp_methods = py_type_memory_methods, + .tp_getset = py_type_memory_getseters, + + .tp_init = py_type_memory_init, + .tp_new = py_type_memory_new + + }; + + return &py_type_memory_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.analysis...TypeMemory'.* +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_type_memory_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'BufferCache' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_type_memory_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.analysis.storage"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_TYPE_MEMORY, type, &PyGObject_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 mémorisation de types. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_type_memory(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_type_memory_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 buffer cache"); + break; + + case 1: + *((GTypeMemory **)dst) = G_TYPE_MEMORY(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/analysis/storage/tpmem.h b/plugins/pychrysalide/analysis/storage/tpmem.h new file mode 100644 index 0000000..1085632 --- /dev/null +++ b/plugins/pychrysalide/analysis/storage/tpmem.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * tpmem.h - prototypes pour l'équivalent Python du fichier "analysis/storage/tpmem.h" + * + * 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_ANALYSIS_STORAGE_TPMEM_H +#define _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_TPMEM_H + + +#include +#include + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_type_memory_type(void); + +/* Prend en charge l'objet 'pychrysalide.analysis.storage.TypeMemory'. */ +bool ensure_python_type_memory_is_registered(void); + +/* Tente de convertir en mémorisation de types. */ +int convert_to_type_memory(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_STORAGE_TPMEM_H */ diff --git a/src/analysis/Makefile.am b/src/analysis/Makefile.am index c1fbc9f..00cc647 100644 --- a/src/analysis/Makefile.am +++ b/src/analysis/Makefile.am @@ -23,6 +23,7 @@ libanalysis_la_LIBADD = \ db/libanalysisdb.la \ disass/libanalysisdisass.la \ human/libanalysishuman.la \ + storage/libanalysisstorage.la \ types/libanalysistypes.la libanalysis_la_LDFLAGS = @@ -37,4 +38,4 @@ AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) -SUBDIRS = contents db disass human types +SUBDIRS = contents db disass human storage types diff --git a/src/analysis/storage/Makefile.am b/src/analysis/storage/Makefile.am new file mode 100644 index 0000000..94f5041 --- /dev/null +++ b/src/analysis/storage/Makefile.am @@ -0,0 +1,30 @@ + +noinst_LTLIBRARIES = libanalysisstorage.la + + +libanalysisstorage_la_SOURCES = \ + cache-int.h \ + cache.h cache.c \ + container-int.h \ + container.h container.c \ + serialize-int.h \ + serialize.h serialize.c \ + storage-int.h \ + storage.h storage.c \ + tpmem.h tpmem.c + +libanalysisstorage_la_LIBADD = + +libanalysisstorage_la_LDFLAGS = $(LIBSSL_LIBS) + + +devdir = $(includedir)/chrysalide/$(subdir:src/%=%) + +dev_HEADERS = $(libanalysisstorage_la_SOURCES:%c=) + + +AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBARCHIVE_CFLAGS) $(LIBSQLITE_CFLAGS) $(LIBSSL_CFLAGS) + +AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) + +SUBDIRS = diff --git a/src/analysis/storage/cache-int.h b/src/analysis/storage/cache-int.h new file mode 100644 index 0000000..17be85d --- /dev/null +++ b/src/analysis/storage/cache-int.h @@ -0,0 +1,62 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * cache.h - prototypes internes pour la conservation hors mémoire d'objets choisis + * + * 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 Foobar. If not, see . + */ + + +#ifndef _ANALYSIS_STORAGE_CACHE_INT_H +#define _ANALYSIS_STORAGE_CACHE_INT_H + + +#include "cache.h" + + + +/* Définition d'un cache d'objets entreposables (instance) */ +struct _GObjectCache +{ + GObject parent; /* A laisser en premier */ + + GLoadedContent *loaded; /* Contenu principal */ + + char *filename; /* Fichier local utilisé */ + int fd; /* Descripteur du flux associé */ + + GCacheContainer **containers; /* Objets en sursis */ + size_t count; /* Quantité de ces objets */ + size_t free_ptr; /* Point d'enregistrement */ + GMutex mutex; /* Contrôle d'accès à la liste */ + +}; + +/* Définition d'un cache d'objets entreposables (classe) */ +struct _GObjectCacheClass +{ + GObjectClass parent; /* A laisser en premier */ + +}; + + +/* Associe un contenu à un cache d'objets. */ +bool g_object_cache_open_for(GObjectCache *, GLoadedContent *); + + + +#endif /* _ANALYSIS_STORAGE_CACHE_INT_H */ diff --git a/src/analysis/storage/cache.c b/src/analysis/storage/cache.c new file mode 100644 index 0000000..3d0966b --- /dev/null +++ b/src/analysis/storage/cache.c @@ -0,0 +1,334 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * cache.c - conservation hors mémoire d'objets choisis + * + * 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 Foobar. If not, see . + */ + + +#include "cache.h" + + +#include +#include +#include +#include + + +#include "cache-int.h" +#include "../../core/logs.h" + + + +/* Initialise la classe des caches d'objets entreposables. */ +static void g_object_cache_class_init(GObjectCacheClass *); + +/* Initialise une instance de cache d'objets entreposables. */ +static void g_object_cache_init(GObjectCache *); + +/* Supprime toutes les références externes. */ +static void g_object_cache_dispose(GObjectCache *); + +/* Procède à la libération totale de la mémoire. */ +static void g_object_cache_finalize(GObjectCache *); + + + +/* Indique le type défini pour un cache d'objets entreposables. */ +G_DEFINE_TYPE(GObjectCache, g_object_cache, G_TYPE_OBJECT); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des caches d'objets entreposables. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_object_cache_class_init(GObjectCacheClass *klass) +{ + GObjectClass *object; /* Autre version de la classe */ + + object = G_OBJECT_CLASS(klass); + + object->dispose = (GObjectFinalizeFunc/* ! */)g_object_cache_dispose; + object->finalize = (GObjectFinalizeFunc)g_object_cache_finalize; + +} + + +/****************************************************************************** +* * +* Paramètres : cache = instance à initialiser. * +* * +* Description : Initialise une instance de cache d'objets entreposables. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_object_cache_init(GObjectCache *cache) +{ + cache->loaded = NULL; + + cache->filename = NULL; + cache->fd = -1; + + cache->containers = NULL; + cache->count = 0; + cache->free_ptr = 0; + g_mutex_init(&cache->mutex); + +} + + +/****************************************************************************** +* * +* Paramètres : cache = instance d'objet GLib à traiter. * +* * +* Description : Supprime toutes les références externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_object_cache_dispose(GObjectCache *cache) +{ + size_t i; /* Boucle de parcours */ + + g_clear_object(&cache->loaded); + + g_mutex_lock(&cache->mutex); + + for (i = 0; i < cache->count; i++) + g_clear_object(&cache->containers[i]); + + g_mutex_unlock(&cache->mutex); + + g_mutex_clear(&cache->mutex); + + G_OBJECT_CLASS(g_object_cache_parent_class)->dispose(G_OBJECT(cache)); + +} + + +/****************************************************************************** +* * +* Paramètres : cache = instance d'objet GLib à traiter. * +* * +* Description : Procède à la libération totale de la mémoire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_object_cache_finalize(GObjectCache *cache) +{ + int ret; /* Bilan d'un appel */ + + ret = access(cache->filename, W_OK); + if (ret == 0) + { + ret = unlink(cache->filename); + if (ret != 0) LOG_ERROR_N("unlink"); + } + + free(cache->filename); + + if (cache->containers != NULL) + free(cache->containers); + + G_OBJECT_CLASS(g_object_cache_parent_class)->finalize(G_OBJECT(cache)); + +} + + +/****************************************************************************** +* * +* Paramètres : loaded = contenu binaire à associer. * +* * +* Description : Crée le support d'un cache d'objets entreposables. * +* * +* Retour : Mécanismes mis en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GObjectCache *g_object_cache_new(GLoadedContent *loaded) +{ + GObjectCache *result; /* Structure à retourner */ + + result = g_object_new(G_TYPE_OBJECT_CACHE, NULL); + + if (!g_object_cache_open_for(result, loaded)) + { + g_object_unref(G_OBJECT(result)); + result = NULL; + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : cache = cache d'objets à manipuler. * +* loaded = contenu binaire à associer. * +* * +* Description : Associe un contenu à un cache d'objets. * +* * +* Retour : Bilan de l'opéation. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_object_cache_open_for(GObjectCache *cache, GLoadedContent *loaded) +{ + bool result; /* Bilan à retourner */ + GBinContent *content; /* Contenu binaire traité */ + const gchar *checksum; /* Empreinte de ce contenu */ + + result = (cache->loaded == NULL); + assert(result); + + if (result) goto done; + + cache->loaded = loaded; + g_object_ref(G_OBJECT(loaded)); + + /* Constitution du fichier de cache */ + + content = g_loaded_content_get_content(loaded); + + checksum = g_binary_content_get_checksum(content); + + asprintf(&cache->filename, "/dev/shm/%s.cache", checksum); + + g_object_unref(G_OBJECT(content)); + + /* Ouverture dudit fichier */ + + cache->fd = open(cache->filename, O_CREAT | O_TRUNC | O_LARGEFILE, 0600); + if (cache->fd == -1) + { + LOG_ERROR_N("open"); + result = false; + goto done; + } + + /* Préparation du cache */ + + cache->count = 1000; + + cache->containers = calloc(cache->count, sizeof(GCacheContainer *)); + + result = true; + + done: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : cache = cache d'objets à manipuler. * +* container = objet à placer dans le cache. * +* * +* Description : Introduit un contenu dans un cache d'objets. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_object_cache_add(GObjectCache *cache, GCacheContainer *container) +{ + bool loop; /* Détection de trop-plein */ + size_t i; /* Boucle de parcours */ + + /* Recherche d'un emplacement libre */ + + loop = false; + + i = cache->free_ptr; + + do + { + if (cache->containers[i] != NULL) + { + g_cache_container_lock_unlock(cache->containers[i], true); + + if (g_cache_container_can_store(cache->containers[i])) + { + if (true) // TODO + g_clear_object(&cache->containers[i]); + } + + g_cache_container_lock_unlock(cache->containers[i], false); + + } + + if (cache->containers[i] == NULL) + break; + + i++; + + if (i == cache->count) + i = 0; + + loop = (i == cache->free_ptr); + + } while (!loop); + + if (loop) + { + log_simple_message(LMT_WARNING, _("Instruction cache is full!")); + goto exit; + } + + /* Inscription à la liste des sursis */ + + cache->containers[i] = container; + + cache->free_ptr = (i + 1); + + if (cache->free_ptr == cache->count) + cache->free_ptr = 0; + + exit: + + ; + +} diff --git a/src/analysis/storage/cache.h b/src/analysis/storage/cache.h new file mode 100644 index 0000000..7f65c09 --- /dev/null +++ b/src/analysis/storage/cache.h @@ -0,0 +1,62 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * cache.h - prototypes pour la conservation hors mémoire d'objets choisis + * + * 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 Foobar. If not, see . + */ + + +#ifndef _ANALYSIS_STORAGE_CACHE_H +#define _ANALYSIS_STORAGE_CACHE_H + + +#include + + +#include "container.h" +#include "../loaded.h" + + + +#define G_TYPE_OBJECT_CACHE g_object_cache_get_type() +#define G_OBJECT_CACHE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_OBJECT_CACHE, GObjectCache)) +#define G_IS_OBJECT_CACHE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_OBJECT_CACHE)) +#define G_OBJECT_CACHE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_OBJECT_CACHE, GObjectCacheClass)) +#define G_IS_OBJECT_CACHE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_OBJECT_CACHE)) +#define G_OBJECT_CACHE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_OBJECT_CACHE, GObjectCacheClass)) + + +/* Définition d'un cache d'objets entreposables (instance) */ +typedef struct _GObjectCache GObjectCache; + +/* Définition d'un cache d'objets entreposables (classe) */ +typedef struct _GObjectCacheClass GObjectCacheClass; + + +/* Indique le type défini pour un cache d'objets entreposables. */ +GType g_object_cache_get_type(void); + +/* Crée le support d'un cache d'objets entreposables. */ +GObjectCache *g_object_cache_new(GLoadedContent *); + +/* Introduit un contenu dans un cache d'objets. */ +void g_object_cache_add(GObjectCache *, GCacheContainer *); + + + +#endif /* _ANALYSIS_STORAGE_CACHE_H */ diff --git a/src/analysis/storage/container-int.h b/src/analysis/storage/container-int.h new file mode 100644 index 0000000..19e77df --- /dev/null +++ b/src/analysis/storage/container-int.h @@ -0,0 +1,62 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * container-int.h - définitions internes propres aux conteneurs d'objets entreposables dans un cache + * + * 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 Chrysalide. If not, see . + */ + + +#ifndef _ANALYSIS_STORAGE_CONTAINER_INT_H +#define _ANALYSIS_STORAGE_CONTAINER_INT_H + + +#include "container.h" + + + +/* Contrôle l'accès au contenu d'un conteneur. */ +typedef void (* lock_unlock_container_cb) (GCacheContainer *, bool); + +/* Détermine si le conteneur a ses accès verrouillés. */ +#ifndef NDEBUG +typedef bool (* is_locked_container_cb) (GCacheContainer *); +#endif + +/* Indique si le contenu d'un conteneur peut être mis en cache. */ +typedef bool (* can_store_container_cb) (GCacheContainer *); + +/* Intermédiaire pour un conteneur d'objets entreposables (interface) */ +struct _GCacheContainerIface +{ + GTypeInterface base_iface; /* A laisser en premier */ + + lock_unlock_container_cb lock_unlock; /* Contrôle d'accès au contenu */ +#ifndef NDEBUG + is_locked_container_cb is_locked; /* Validation des verrous */ +#endif + can_store_container_cb can_store; /* Mise en cache possible ? */ + +}; + + +/* Redéfinition */ +typedef GCacheContainerIface GCacheContainerInterface; + + + +#endif /* _ANALYSIS_STORAGE_CONTAINER_INT_H */ diff --git a/src/analysis/storage/container.c b/src/analysis/storage/container.c new file mode 100644 index 0000000..60d6d74 --- /dev/null +++ b/src/analysis/storage/container.c @@ -0,0 +1,140 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * container.c - conteneurs d'objets entreposables dans un cache + * + * 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 Chrysalide. If not, see . + */ + + +#include "container.h" + + +#include + + +#include "container-int.h" + + + +/* Procède à l'initialisation de l'interface de conteneur. */ +static void g_cache_container_default_init(GCacheContainerInterface *); + + + +/* Détermine le type d'une interface pour un conteneur d'objets entreposables. */ +G_DEFINE_INTERFACE(GCacheContainer, g_cache_container, G_TYPE_OBJECT) + + +/****************************************************************************** +* * +* Paramètres : iface = interface GLib à initialiser. * +* * +* Description : Procède à l'initialisation de l'interface de conteneur. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_cache_container_default_init(GCacheContainerInterface *iface) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : container = conteneur à manipuler. * +* lock = indique une demande de verrou. * +* * +* Description : Contrôle l'accès au contenu d'un conteneur. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_cache_container_lock_unlock(GCacheContainer *container, bool lock) +{ + GCacheContainerIface *iface; /* Interface utilisée */ + + assert(g_cache_container_is_locked(container)); + + iface = G_CACHE_CONTAINER_GET_IFACE(container); + + iface->lock_unlock(container, lock); + +} + + +/****************************************************************************** +* * +* Paramètres : container = conteneur à consulter. * +* * +* Description : Détermine si le conteneur a ses accès verrouillés. * +* * +* Retour : Bilan de la consultation. * +* * +* Remarques : - * +* * +******************************************************************************/ + +#ifndef NDEBUG +bool g_cache_container_is_locked(GCacheContainer *container) +{ + bool result; /* Bilan à retourner */ + GCacheContainerIface *iface; /* Interface utilisée */ + + iface = G_CACHE_CONTAINER_GET_IFACE(container); + + result = iface->is_locked(container); + + return result; + +} +#endif + + +/****************************************************************************** +* * +* Paramètres : container = conteneur à consulter. * +* * +* Description : Indique si le contenu d'un conteneur peut être mis en cache. * +* * +* Retour : Bilan de la consultation. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_cache_container_can_store(GCacheContainer *container) +{ + bool result; /* Bilan à retourner */ + GCacheContainerIface *iface; /* Interface utilisée */ + + assert(g_cache_container_is_locked(container)); + + iface = G_CACHE_CONTAINER_GET_IFACE(container); + + result = iface->can_store(container); + + return result; + +} diff --git a/src/analysis/storage/container.h b/src/analysis/storage/container.h new file mode 100644 index 0000000..b85793d --- /dev/null +++ b/src/analysis/storage/container.h @@ -0,0 +1,66 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * container.h - prototypes pour les conteneurs d'objets entreposables dans un cache + * + * 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 Chrysalide. If not, see . + */ + + +#ifndef _ANALYSIS_STORAGE_CONTAINER_H +#define _ANALYSIS_STORAGE_CONTAINER_H + + +#include + + +#include "../../common/packed.h" + + + +#define G_TYPE_CACHE_CONTAINER g_cache_container_get_type() +#define G_CACHE_CONTAINER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_CACHE_CONTAINER, GCacheContainer)) +#define G_CACHE_CONTAINER_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST((vtable), G_TYPE_CACHE_CONTAINER, GCacheContainerIface)) +#define G_IS_CACHE_CONTAINER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_CACHE_CONTAINER)) +#define G_IS_CACHE_CONTAINER_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE((vtable), G_TYPE_CACHE_CONTAINER)) +#define G_CACHE_CONTAINER_GET_IFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE((inst), G_TYPE_CACHE_CONTAINER, GCacheContainerIface)) + + +/* Intermédiaire pour un conteneur d'objets entreposables (coquille vide) */ +typedef struct _GCacheContainer GCacheContainer; + +/* Intermédiaire pour un conteneur d'objets entreposables (interface) */ +typedef struct _GCacheContainerIface GCacheContainerIface; + + +/* Détermine le type d'une interface pour un conteneur d'objets entreposables. */ +GType g_cache_container_get_type(void) G_GNUC_CONST; + +/* Contrôle l'accès au contenu d'un conteneur. */ +void g_cache_container_lock_unlock(GCacheContainer *, bool); + +/* Détermine si le conteneur a ses accès verrouillés. */ +#ifndef NDEBUG +bool g_cache_container_is_locked(GCacheContainer *); +#endif + +/* Indique si le contenu d'un conteneur peut être mis en cache. */ +bool g_cache_container_can_store(GCacheContainer *); + + + +#endif /* _ANALYSIS_STORAGE_CONTAINER_H */ diff --git a/src/analysis/storage/serialize-int.h b/src/analysis/storage/serialize-int.h new file mode 100644 index 0000000..3a99b52 --- /dev/null +++ b/src/analysis/storage/serialize-int.h @@ -0,0 +1,55 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * serialize-int.h - définitions internes propres aux objets entreposables dans un cache + * + * 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 Chrysalide. If not, see . + */ + + +#ifndef _ANALYSIS_STORAGE_SERIALIZE_INT_H +#define _ANALYSIS_STORAGE_SERIALIZE_INT_H + + +#include "serialize.h" + + + +/* Charge un objet depuis une mémoire tampon. */ +typedef bool (* load_serializable_object_cb) (GSerializableObject *, GObjectStorage *, GTypeMemory *, packed_buffer *); + +/* Sauvegarde un objet dans une mémoire tampon. */ +typedef bool (* store_serializable_object_cb) (const GSerializableObject *, GObjectStorage *, GTypeMemory *, packed_buffer *); + + +/* Intermédiaire pour la mise en cache d'objet (interface) */ +struct _GSerializableObjectIface +{ + GTypeInterface base_iface; /* A laisser en premier */ + + load_serializable_object_cb load; /* Chargement */ + store_serializable_object_cb store; /* Enregistrement */ + +}; + + +/* Redéfinition */ +typedef GSerializableObjectIface GSerializableObjectInterface; + + + +#endif /* _ANALYSIS_STORAGE_SERIALIZE_INT_H */ diff --git a/src/analysis/storage/serialize.c b/src/analysis/storage/serialize.c new file mode 100644 index 0000000..6ed1eab --- /dev/null +++ b/src/analysis/storage/serialize.c @@ -0,0 +1,119 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * serialize.h - objets entreposables dans un cache + * + * 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 Chrysalide. If not, see . + */ + + +#include "serialize.h" + + +#include "serialize-int.h" + + + +/* Procède à l'initialisation de l'interface de mise en cache. */ +static void g_serializable_object_default_init(GSerializableObjectInterface *); + + + +/* Détermine le type d'une interface pour la mise en cache d'objet. */ +G_DEFINE_INTERFACE(GSerializableObject, g_serializable_object, G_TYPE_OBJECT) + + +/****************************************************************************** +* * +* Paramètres : iface = interface GLib à initialiser. * +* * +* Description : Procède à l'initialisation de l'interface de mise en cache. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_serializable_object_default_init(GSerializableObjectInterface *iface) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : object = instruction d'assemblage à consulter. * +* storage = conservateur de données à manipuler ou NULL. * +* tpmem = mémoire des types d'objets à compléter. * +* pbuf = zone tampon à remplir. * +* * +* Description : Charge un objet depuis une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_serializable_object_load(GSerializableObject *object, GObjectStorage *storage, GTypeMemory *tpmem, packed_buffer *pbuf) +{ + bool result; /* Bilan à retourner */ + GSerializableObjectIface *iface; /* Interface utilisée */ + + iface = G_SERIALIZABLE_OBJECT_GET_IFACE(object); + + result = iface->load(object, storage, tpmem, pbuf); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : object = instruction d'assemblage à consulter. * +* storage = conservateur de données à manipuler ou NULL. * +* tpmem = mémoire des types d'objets à compléter. * +* pbuf = zone tampon à remplir. * +* * +* Description : Sauvegarde un objet dans une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_serializable_object_store(const GSerializableObject *object, GObjectStorage *storage, GTypeMemory *tpmem, packed_buffer *pbuf) +{ + bool result; /* Bilan à retourner */ + GSerializableObjectIface *iface; /* Interface utilisée */ + + result = g_type_memory_store_object_gtype(tpmem, G_OBJECT(object), pbuf); + + if (result) + { + iface = G_SERIALIZABLE_OBJECT_GET_IFACE(object); + + result = iface->store(object, storage, tpmem, pbuf); + + } + + return result; + +} diff --git a/src/analysis/storage/serialize.h b/src/analysis/storage/serialize.h new file mode 100644 index 0000000..d248b65 --- /dev/null +++ b/src/analysis/storage/serialize.h @@ -0,0 +1,65 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * serialize.h - prototypes pour les objets entreposables dans un cache + * + * 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 Chrysalide. If not, see . + */ + + +#ifndef _ANALYSIS_STORAGE_SERIALIZE_H +#define _ANALYSIS_STORAGE_SERIALIZE_H + + +#include + + +#include "tpmem.h" +#include "../../common/packed.h" + + + +#define G_TYPE_SERIALIZABLE_OBJECT g_serializable_object_get_type() +#define G_SERIALIZABLE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_SERIALIZABLE_OBJECT, GSerializableObject)) +#define G_SERIALIZABLE_OBJECT_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST((vtable), G_TYPE_SERIALIZABLE_OBJECT, GSerializableObjectIface)) +#define G_IS_SERIALIZABLE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_SERIALIZABLE_OBJECT)) +#define G_IS_SERIALIZABLE_OBJECT_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE((vtable), G_TYPE_SERIALIZABLE_OBJECT)) +#define G_SERIALIZABLE_OBJECT_GET_IFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE((inst), G_TYPE_SERIALIZABLE_OBJECT, GSerializableObjectIface)) + + +/* Intermédiaire pour la mise en cache d'objet (coquille vide) */ +typedef struct _GSerializableObject GSerializableObject; + +/* Intermédiaire pour la mise en cache d'objet (interface) */ +typedef struct _GSerializableObjectIface GSerializableObjectIface; + + +/* Détermine le type d'une interface pour la mise en cache d'objet. */ +GType g_serializable_object_get_type(void) G_GNUC_CONST; + +/* storage.h : définition d'une conservation d'objets construits */ +typedef struct _GObjectStorage GObjectStorage; + +/* Charge un objet depuis une mémoire tampon. */ +bool g_serializable_object_load(GSerializableObject *, GObjectStorage *, GTypeMemory *, packed_buffer *); + +/* Sauvegarde un objet dans une mémoire tampon. */ +bool g_serializable_object_store(const GSerializableObject *, GObjectStorage *, GTypeMemory *, packed_buffer *); + + + +#endif /* _ANALYSIS_STORAGE_SERIALIZE_H */ diff --git a/src/analysis/storage/storage-int.h b/src/analysis/storage/storage-int.h new file mode 100644 index 0000000..7e01cbc --- /dev/null +++ b/src/analysis/storage/storage-int.h @@ -0,0 +1,66 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * storage.h - prototypes internes pour la conservation sur disque d'objets construits + * + * 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 Foobar. If not, see . + */ + + +#ifndef _ANALYSIS_STORAGE_STORAGE_INT_H +#define _ANALYSIS_STORAGE_STORAGE_INT_H + + +#include "storage.h" + + + +/* Gestion d'enregistrements spécifiques */ +typedef struct _storage_backend_t +{ + char *name; /* Désignation du groupe */ + + char *filename; /* Nom du fichier associé */ + int fd; /* Flux d'accès correspondant */ + +} storage_backend_t; + +/* Définition d'une conservation d'objets construits (instance) */ +struct _GObjectStorage +{ + GObject parent; /* A laisser en premier */ + + GTypeMemory *tpmem; /* Mémorisation de types */ + + GLoadedContent *loaded; /* Contenu principal */ + + storage_backend_t *backends; /* Gestionnaires existants */ + size_t count; /* Quantité de gestionnaires */ + GMutex mutex; /* Contrôle d'accès à la liste */ + +}; + +/* Définition d'une conservation d'objets construits (classe) */ +struct _GObjectStorageClass +{ + GObjectClass parent; /* A laisser en premier */ + +}; + + + +#endif /* _ANALYSIS_STORAGE_STORAGE_INT_H */ diff --git a/src/analysis/storage/storage.c b/src/analysis/storage/storage.c new file mode 100644 index 0000000..d64a08e --- /dev/null +++ b/src/analysis/storage/storage.c @@ -0,0 +1,436 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * storage.c - conservation hors mémoire d'objets choisis + * + * 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 Foobar. If not, see . + */ + + +#include "storage.h" + + +#include +#include +#include +#include + + +#include "storage-int.h" +#include "../../core/logs.h" + + + +/* Initialise la classe des conservations d'objets en place. */ +static void g_object_storage_class_init(GObjectStorageClass *); + +/* Initialise une instance de conservation d'objets en place. */ +static void g_object_storage_init(GObjectStorage *); + +/* Supprime toutes les références externes. */ +static void g_object_storage_dispose(GObjectStorage *); + +/* Procède à la libération totale de la mémoire. */ +static void g_object_storage_finalize(GObjectStorage *); + +/* Retrouve l'encadrement pour un nouveau groupe d'objets. */ +static storage_backend_t *g_object_storage_find_backend(GObjectStorage *, const char *); + + + +/* Indique le type défini pour une conservation d'objets construits. */ +G_DEFINE_TYPE(GObjectStorage, g_object_storage, G_TYPE_OBJECT); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des conservations d'objets en place. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_object_storage_class_init(GObjectStorageClass *klass) +{ + GObjectClass *object; /* Autre version de la classe */ + + object = G_OBJECT_CLASS(klass); + + object->dispose = (GObjectFinalizeFunc/* ! */)g_object_storage_dispose; + object->finalize = (GObjectFinalizeFunc)g_object_storage_finalize; + +} + + +/****************************************************************************** +* * +* Paramètres : storage = instance à initialiser. * +* * +* Description : Initialise une instance de conservation d'objets en place. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_object_storage_init(GObjectStorage *storage) +{ + storage->tpmem = g_type_memory_new(); + + storage->loaded = NULL; + + storage->backends = NULL; + storage->count = 0; + g_mutex_init(&storage->mutex); + +} + + +/****************************************************************************** +* * +* Paramètres : storage = instance d'objet GLib à traiter. * +* * +* Description : Supprime toutes les références externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_object_storage_dispose(GObjectStorage *storage) +{ + g_clear_object(&storage->tpmem); + + g_clear_object(&storage->loaded); + + G_OBJECT_CLASS(g_object_storage_parent_class)->dispose(G_OBJECT(storage)); + +} + + +/****************************************************************************** +* * +* Paramètres : storage = instance d'objet GLib à traiter. * +* * +* Description : Procède à la libération totale de la mémoire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_object_storage_finalize(GObjectStorage *storage) +{ + size_t i; /* Boucle de parcours */ + storage_backend_t *backend; /* Gestionnaire à manipuler */ + int ret; /* Bilan d'un appel */ + + g_mutex_lock(&storage->mutex); + + for (i = 0; i < storage->count; i++) + { + backend = &storage->backends[i]; + + if (backend->fd != -1) + close(backend->fd); + else + assert(false); + + ret = access(backend->filename, W_OK); + if (ret == 0) + { + ret = unlink(backend->filename); + if (ret != 0) LOG_ERROR_N("unlink"); + } + + free(backend->name); + + free(backend->filename); + + } + + if (storage->backends != NULL) + free(storage->backends); + + g_mutex_unlock(&storage->mutex); + + g_mutex_clear(&storage->mutex); + + G_OBJECT_CLASS(g_object_storage_parent_class)->finalize(G_OBJECT(storage)); + +} + + +/****************************************************************************** +* * +* Paramètres : loaded = contenu binaire à associer. * +* * +* Description : Crée le support d'une conservation d'objets en place. * +* * +* Retour : Mécanismes mis en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GObjectStorage *g_object_storage_new(GLoadedContent *loaded) +{ + GObjectStorage *result; /* Structure à retourner */ + + result = g_object_new(G_TYPE_OBJECT_STORAGE, NULL); + + result->loaded = loaded; + g_object_ref(G_OBJECT(loaded)); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : storage = gestionnaire de conservations à compléter. * +* name = désignation d'un nouveau groupe d'objets. * +* * +* Description : Retrouve l'encadrement pour un nouveau groupe d'objets. * +* * +* Retour : Informations liées à un groupe ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static storage_backend_t *g_object_storage_find_backend(GObjectStorage *storage, const char *name) +{ + storage_backend_t *result; /* Encadrement à retourner */ + size_t i; /* Boucle de parcours */ + + assert(!g_mutex_trylock(&storage->mutex)); + + for (i = 0; i < storage->count; i++) + if (strcmp(storage->backends[i].name, name) == 0) + break; + + if (i == storage->count) + result = NULL; + else + result = &storage->backends[i]; + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : storage = gestionnaire de conservations à compléter. * +* name = désignation d'un nouveau groupe d'objets. * +* filename = éventuel nom de fichier à utiliser ou NULL. * +* * +* Description : Ajoute le support d'un nouveau groupe d'objets construits. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_object_storage_add_backend(GObjectStorage *storage, const char *name, const char *filename) +{ + bool result; /* Bilan à retourner */ + GBinContent *content; /* Contenu binaire traité */ + const gchar *checksum; /* Empreinte de ce contenu */ + char *prefix; /* Début de nom de fichier */ + storage_backend_t backend; /* Informations à intégrer */ + + result = false; + + g_mutex_lock(&storage->mutex); + + if (g_object_storage_find_backend(storage, name) != NULL) + goto exit; + + /* Préparatifs */ + + content = g_loaded_content_get_content(storage->loaded); + + checksum = g_binary_content_get_checksum(content); + + asprintf(&prefix, "%s-%s", checksum, name); + + g_object_unref(G_OBJECT(content)); + + backend.fd = make_tmp_file(prefix, "cache", &backend.filename); + + free(prefix); + + if (backend.fd == -1) + goto exit; + + /* Inscription en bonne et due forme */ + + backend.name = strdup(name); + + storage->backends = realloc(storage->backends, ++storage->count * sizeof(storage_backend_t)); + + storage->backends[storage->count - 1] = backend; + + exit: + + g_mutex_unlock(&storage->mutex); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : storage = gestionnaire à manipuler. * +* name = désignation d'un nouveau groupe d'objets. * +* pos = tête de lecture avant écriture. * +* * +* Description : Charge un objet à partir de données rassemblées. * +* * +* Retour : Objet restauré en mémoire ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GSerializableObject *g_object_storage_load_object(GObjectStorage *storage, const char *name, off64_t pos) +{ + GSerializableObject *result; /* Instance à retourner */ + bool status; /* Bilan d'une opération */ + storage_backend_t *backend; /* Informations à consulter */ + packed_buffer pbuf; /* Tampon des données à lire */ + off64_t new; /* Nouvelle position de lecture*/ + + result = NULL; + + /* Chargement */ + + status = false; + + g_mutex_lock(&storage->mutex); + + backend = g_object_storage_find_backend(storage, name); + + if (backend != NULL) + { + new = lseek64(backend->fd, pos, SEEK_SET); + + if (new == pos) + { + reset_packed_buffer(&pbuf); + status = read_packed_buffer(&pbuf, backend->fd); + } + + } + + g_mutex_unlock(&storage->mutex); + + if (!status) + goto exit; + + /* Phase de conversion */ + + result = G_SERIALIZABLE_OBJECT(g_type_memory_create_object(storage->tpmem, &pbuf)); + + if (result) + { + status = g_serializable_object_load(result, storage, storage->tpmem, &pbuf); + + if (!status) + g_clear_object(&result); + + } + + exit_packed_buffer(&pbuf); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : storage = gestionnaire à manipuler. * +* name = désignation d'un nouveau groupe d'objets. * +* pbuf = zone tampon à lire. * +* pos = tête de lecture avant écriture, ou NULL. [OUT] * +* * +* Description : Sauvegarde un object sous forme de données rassemblées. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_object_storage_store_object(GObjectStorage *storage, const char *name, const GSerializableObject *object, off64_t *pos) +{ + bool result; /* Bilan à retourner */ + packed_buffer pbuf; /* Tampon des données à écrire */ + storage_backend_t *backend; /* Informations à consulter */ + off64_t tmp; /* Conservation éphémère */ + + /* Phase de conversion */ + + init_packed_buffer(&pbuf); + + result = g_serializable_object_store(object, storage, storage->tpmem, &pbuf); + if (!result) goto exit; + + /* Enregistrement */ + + g_mutex_lock(&storage->mutex); + + backend = g_object_storage_find_backend(storage, name); + + if (backend != NULL) + { + if (pos == NULL) + pos = &tmp; + + *pos = lseek64(backend->fd, 0, SEEK_CUR); + + if (*pos != (off64_t)-1) + result = write_packed_buffer(&pbuf, backend->fd); + + } + + g_mutex_unlock(&storage->mutex); + + /* Sortie propre */ + + exit: + + exit_packed_buffer(&pbuf); + + return result; + +} diff --git a/src/analysis/storage/storage.h b/src/analysis/storage/storage.h new file mode 100644 index 0000000..611e49e --- /dev/null +++ b/src/analysis/storage/storage.h @@ -0,0 +1,70 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * storage.h - prototypes pour la conservation sur disque d'objets construits + * + * 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 Foobar. If not, see . + */ + + +#ifndef _ANALYSIS_STORAGE_STORAGE_H +#define _ANALYSIS_STORAGE_STORAGE_H + + +#include +#include + + +#include "serialize.h" +#include "tpmem.h" +#include "../loaded.h" + + + +#define G_TYPE_OBJECT_STORAGE g_object_storage_get_type() +#define G_OBJECT_STORAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_OBJECT_STORAGE, GObjectStorage)) +#define G_IS_OBJECT_STORAGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_OBJECT_STORAGE)) +#define G_OBJECT_STORAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_OBJECT_STORAGE, GObjectStorageClass)) +#define G_IS_OBJECT_STORAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_OBJECT_STORAGE)) +#define G_OBJECT_STORAGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_OBJECT_STORAGE, GObjectStorageClass)) + + +/* Définition d'une conservation d'objets construits (instance) */ +typedef struct _GObjectStorage GObjectStorage; + +/* Définition d'une conservation d'objets construits (classe) */ +typedef struct _GObjectStorageClass GObjectStorageClass; + + +/* Indique le type défini pour une conservation d'objets construits. */ +GType g_object_storage_get_type(void); + +/* Crée le support d'une conservation d'objets en place. */ +GObjectStorage *g_object_storage_new(GLoadedContent *); + +/* Ajoute le support d'un nouveau groupe d'objets construits. */ +bool g_object_storage_add_backend(GObjectStorage *, const char *, const char *); + +/* Charge un objet à partir de données rassemblées. */ +GSerializableObject *g_object_storage_load_object(GObjectStorage *, const char *, off64_t); + +/* Sauvegarde un object sous forme de données rassemblées. */ +bool g_object_storage_store_object(GObjectStorage *, const char *, const GSerializableObject *, off64_t *); + + + +#endif /* _ANALYSIS_STORAGE_STORAGE_H */ diff --git a/src/analysis/storage/tpmem.c b/src/analysis/storage/tpmem.c new file mode 100644 index 0000000..64f54d2 --- /dev/null +++ b/src/analysis/storage/tpmem.c @@ -0,0 +1,454 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * tpmem.c - mémorisation des types d'objets mis en cache + * + * 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 Foobar. If not, see . + */ + + +#include "tpmem.h" + + +#include +#include + + +#include "../../arch/operands/target.h" +#include "../../core/logs.h" + + + +/* Conservation d'une référence sur un type */ +typedef struct _gtype_ref_info_t +{ + GType gtype; /* Type pour la GLib */ + gpointer gclass; /* Lien vers sa classe */ + + /** + * La GLib n'est pas très claire sur la taille de GType : + * + * #if GLIB_SIZEOF_SIZE_T != GLIB_SIZEOF_LONG || !defined __cplusplus + * typedef gsize GType; + * #else // for historic reasons, C++ links against gulong GTypes + * typedef gulong GType; + * #endif + * + * Et : + * + * typedef unsigned $glib_size_type_define gsize; + * + * On prend donc le parti de conserver ces types sous forme de valeurs 64 bits + * lors des enregistrements. + */ + +} gtype_ref_info_t; + +/* Définition d'une mémoire de types d'objets (instance) */ +struct _GTypeMemory +{ + GObject parent; /* A laisser en premier */ + + gtype_ref_info_t *gtypes; /* Types des objets reconnus */ + uint64_t count; /* Quantité de ces objets */ + GMutex mutex; /* Contrôle d'accès à la liste */ + +}; + +/* Définition d'une mémoire de types d'objets (classe) */ +struct _GTypeMemoryClass +{ + GObjectClass parent; /* A laisser en premier */ + +}; + + +/* Initialise la classe des mémoires de types d'objets. */ +static void g_type_memory_class_init(GTypeMemoryClass *); + +/* Initialise une instance de mémoire de types d'objets. */ +static void g_type_memory_init(GTypeMemory *); + +/* Supprime toutes les références externes. */ +static void g_type_memory_dispose(GTypeMemory *); + +/* Procède à la libération totale de la mémoire. */ +static void g_type_memory_finalize(GTypeMemory *); + + + +/* Indique le type défini pour une mémoire de types d'objets. */ +G_DEFINE_TYPE(GTypeMemory, g_type_memory, G_TYPE_OBJECT); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des mémoires de types d'objets. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_type_memory_class_init(GTypeMemoryClass *klass) +{ + GObjectClass *object; /* Autre version de la classe */ + + object = G_OBJECT_CLASS(klass); + + object->dispose = (GObjectFinalizeFunc/* ! */)g_type_memory_dispose; + object->finalize = (GObjectFinalizeFunc)g_type_memory_finalize; + +} + + +/****************************************************************************** +* * +* Paramètres : tpmem = instance à initialiser. * +* * +* Description : Initialise une instance de mémoire de types d'objets. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_type_memory_init(GTypeMemory *tpmem) +{ + tpmem->gtypes = NULL; + tpmem->count = 0; + g_mutex_init(&tpmem->mutex); + +} + + +/****************************************************************************** +* * +* Paramètres : tpmem = instance d'objet GLib à traiter. * +* * +* Description : Supprime toutes les références externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_type_memory_dispose(GTypeMemory *tpmem) +{ + uint64_t i; /* Boucle de parcours */ + + g_mutex_lock(&tpmem->mutex); + + for (i = 0; i < tpmem->count; i++) + if (tpmem->gtypes[i].gclass != NULL) + g_type_class_unref(tpmem->gtypes[i].gclass); + + g_mutex_unlock(&tpmem->mutex); + + g_mutex_clear(&tpmem->mutex); + + G_OBJECT_CLASS(g_type_memory_parent_class)->dispose(G_OBJECT(tpmem)); + +} + + +/****************************************************************************** +* * +* Paramètres : tpmem = instance d'objet GLib à traiter. * +* * +* Description : Procède à la libération totale de la mémoire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_type_memory_finalize(GTypeMemory *tpmem) +{ + if (tpmem->gtypes != NULL) + free(tpmem->gtypes); + + G_OBJECT_CLASS(g_type_memory_parent_class)->finalize(G_OBJECT(tpmem)); + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Crée une mémoire pour types d'objets. * +* * +* Retour : Instance mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GTypeMemory *g_type_memory_new(void) +{ + GTypeMemory *result; /* Structure à retourner */ + + result = g_object_new(G_TYPE_TYPE_MEMORY, NULL); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : tpmem = mémoire à compléter. * +* fd = flux ouvert en lecture. * +* * +* Description : Apprend tous les types mémorisés dans un flux. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_type_memory_read_types(GTypeMemory *tpmem, int fd) +{ + bool result; /* Bilan à enregistrer */ + packed_buffer pbuf; /* Tampon des données à écrire */ + uint64_t i; /* Boucle de parcours */ + unsigned char len; /* Taille d'un nom de type */ + char *name; /* Désignation d'un type */ + + init_packed_buffer(&pbuf); + + result = read_packed_buffer(&pbuf, fd); + + if (result) + { + g_mutex_lock(&tpmem->mutex); + + result = extract_packed_buffer(&pbuf, &tpmem->count, sizeof(uint64_t), true); + + if (result) + { + assert(tpmem->gtypes == NULL); + tpmem->gtypes = calloc(tpmem->count, sizeof(gtype_ref_info_t)); + } + + for (i = 0; i < tpmem->count && result; i++) + { + result = extract_packed_buffer(&pbuf, &len, sizeof(unsigned char), false); + + if (result) + { + name = malloc(len); + + result = extract_packed_buffer(&pbuf, name, len, false); + + if (result) + { + tpmem->gtypes[i].gtype = g_type_from_name(name); + result = (tpmem->gtypes[i].gtype != 0); + + if (!result) + log_variadic_message(LMT_ERROR, "Unknown type: '%s'", name); + + } + + if (result) + tpmem->gtypes[i].gclass = g_type_class_ref(tpmem->gtypes[i].gtype); + + free(name); + + } + + } + + g_mutex_unlock(&tpmem->mutex); + + } + + exit_packed_buffer(&pbuf); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : tpmem = mémoire à manipuler. * +* pbuf = zone tampon à venir lire. * +* * +* Description : Crée une nouvelle instance d'objet à partir de son type. * +* * +* Retour : Instance issue de l'opération ou NULL. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GObject *g_type_memory_create_object(GTypeMemory *tpmem, packed_buffer *pbuf) +{ + GObject *result; /* Nouvelle instance à renvoyer*/ + uint64_t index; /* Indice du point d'insertion */ + bool status; /* Bilan d'une récupération */ + + result = NULL; + + status = extract_packed_buffer(pbuf, &index, sizeof(uint64_t), true); + + if (status) + { + g_mutex_lock(&tpmem->mutex); + + if (index < tpmem->count) + result = g_object_new(tpmem->gtypes[index].gtype, NULL); + + g_mutex_unlock(&tpmem->mutex); + + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : tpmem = mémoire à manipuler. * +* obj = instance dont le type est à mémoriser. * +* pbuf = zone tampon à remplir. * +* * +* Description : Sauvegarde le type d'un objet instancié. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_type_memory_store_object_gtype(GTypeMemory *tpmem, GObject *obj, packed_buffer *pbuf) +{ + bool result; /* Bilan à retourner */ + GType gtype; /* Type à enregistrer */ + uint64_t index; /* Indice du point d'insertion */ + + gtype = G_TYPE_FROM_INSTANCE(obj); + + /** + * Pour quelques explications sur l'esquive suivante, se rapporter aux + * commentaires de g_target_operand_unserialize(). + * + * Dans la situation présente, on ne doit pas enregistrer le type dans le tampon, + * car l'opérande va relancer l'opération entière (avec un opérande temporaire), + * ce qui conduirait à l'enregistrement de deux types successifs dans les données. + */ + + if (gtype == G_TYPE_TARGET_OPERAND) + result = true; + + else + { + g_mutex_lock(&tpmem->mutex); + + for (index = 0; index < tpmem->count; index++) + if (tpmem->gtypes[index].gtype == gtype) + break; + + if (index == tpmem->count) + { + tpmem->gtypes = realloc(tpmem->gtypes, ++tpmem->count * sizeof(gtype_ref_info_t)); + + assert(tpmem->count > 0); + + tpmem->gtypes[index].gtype = gtype; + tpmem->gtypes[index].gclass = g_type_class_ref(gtype); + + } + + g_mutex_unlock(&tpmem->mutex); + + result = extend_packed_buffer(pbuf, &index, sizeof(uint64_t), true); + + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : tpmem = mémoire à consulter. * +* fd = flux ouvert en écriture. * +* * +* Description : Enregistre tous les types mémorisés dans un flux. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_type_memory_write_types(GTypeMemory *tpmem, int fd) +{ + bool result; /* Bilan à enregistrer */ + packed_buffer pbuf; /* Tampon des données à écrire */ + uint64_t i; /* Boucle de parcours */ + const gchar *name; /* Désignation d'un type */ + size_t len; /* Taille de ce nom */ + + init_packed_buffer(&pbuf); + + g_mutex_lock(&tpmem->mutex); + + result = extend_packed_buffer(&pbuf, &tpmem->count, sizeof(uint64_t), true); + + for (i = 0; i < tpmem->count && result; i++) + { + name = g_type_name(tpmem->gtypes[i].gtype); + len = strlen(name) + 1; + + if (len > (2 << (sizeof(unsigned char) * 8 - 1))) + { + log_variadic_message(LMT_ERROR, "Type name too long: '%s' (%zu bytes)", name, len); + result = false; + break; + } + + result = extend_packed_buffer(&pbuf, (unsigned char []) { len }, sizeof(unsigned char), false); + + if (result) + result = extend_packed_buffer(&pbuf, name, len, false); + + } + + if (result) + result = write_packed_buffer(&pbuf, fd); + + g_mutex_unlock(&tpmem->mutex); + + exit_packed_buffer(&pbuf); + + return result; + +} diff --git a/src/analysis/storage/tpmem.h b/src/analysis/storage/tpmem.h new file mode 100644 index 0000000..4c82a1f --- /dev/null +++ b/src/analysis/storage/tpmem.h @@ -0,0 +1,70 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * tpmem.h - prototypes pour la mémorisation des types d'objets mis en cache + * + * 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 Foobar. If not, see . + */ + + +#ifndef _ANALYSIS_STORAGE_TPMEM_H +#define _ANALYSIS_STORAGE_TPMEM_H + + +#include + + +#include "../../common/packed.h" + + + +#define G_TYPE_TYPE_MEMORY g_type_memory_get_type() +#define G_TYPE_MEMORY(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_TYPE_MEMORY, GTypeMemory)) +#define G_IS_TYPE_MEMORY(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_TYPE_MEMORY)) +#define G_TYPE_MEMORY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_TYPE_MEMORY, GTypeMemoryClass)) +#define G_IS_TYPE_MEMORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_TYPE_MEMORY)) +#define G_TYPE_MEMORY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_TYPE_MEMORY, GTypeMemoryClass)) + + +/* Définition d'une mémoire de types d'objets (instance) */ +typedef struct _GTypeMemory GTypeMemory; + +/* Définition d'une mémoire de types d'objets (classe) */ +typedef struct _GTypeMemoryClass GTypeMemoryClass; + + +/* Indique le type défini pour une mémoire de types d'objets. */ +GType g_type_memory_get_type(void); + +/* Crée une mémoire pour types d'objets. */ +GTypeMemory *g_type_memory_new(void); + +/* Apprend tous les types mémorisés dans un flux. */ +bool g_type_memory_read_types(GTypeMemory *, int); + +/* Crée une nouvelle instance d'objet à partir de son type. */ +GObject *g_type_memory_create_object(GTypeMemory *, packed_buffer *); + +/* Sauvegarde le type d'un objet instancié. */ +bool g_type_memory_store_object_gtype(GTypeMemory *, GObject *, packed_buffer *); + +/* Enregistre tous les types mémorisés dans un flux. */ +bool g_type_memory_write_types(GTypeMemory *, int); + + + +#endif /* _ANALYSIS_STORAGE_TPMEM_H */ -- cgit v0.11.2-87-g4458