summaryrefslogtreecommitdiff
path: root/src/glibext/tpmem.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/glibext/tpmem.c')
-rw-r--r--src/glibext/tpmem.c363
1 files changed, 363 insertions, 0 deletions
diff --git a/src/glibext/tpmem.c b/src/glibext/tpmem.c
new file mode 100644
index 0000000..14b5e33
--- /dev/null
+++ b/src/glibext/tpmem.c
@@ -0,0 +1,363 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * tpmem.c - mémorisation des types d'objets mis en cache
+ *
+ * Copyright (C) 2020-2025 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 <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "tpmem.h"
+
+
+#include <assert.h>
+
+
+#include "tpmem-int.h"
+#include "../common/szbin.h"
+#include "../core/logs.h"
+
+
+
+/* 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(GObject *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_type_memory_finalize(GObject *);
+
+
+
+/* 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 = g_type_memory_dispose;
+ object->finalize = 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 : object = instance d'objet GLib à traiter. *
+* *
+* Description : Supprime toutes les références externes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_type_memory_dispose(GObject *object)
+{
+ GTypeMemory *tpmem; /* Version spécialisée */
+ uint64_t i; /* Boucle de parcours */
+
+ tpmem = G_TYPE_MEMORY(object);
+
+ 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(object);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : object = instance d'objet GLib à traiter. *
+* *
+* Description : Procède à la libération totale de la mémoire. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_type_memory_finalize(GObject *object)
+{
+ GTypeMemory *tpmem; /* Version spécialisée */
+
+ tpmem = G_TYPE_MEMORY(object);
+
+ if (tpmem->gtypes != NULL)
+ free(tpmem->gtypes);
+
+ G_OBJECT_CLASS(g_type_memory_parent_class)->finalize(object);
+
+}
+
+
+/******************************************************************************
+* *
+* 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 tampon. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool g_type_memory_load(GTypeMemory *tpmem, int fd)
+{
+ bool result; /* Bilan à enregistrer */
+ uleb128_t count; /* Nombre d'éléments détectés */
+ uleb128_t i; /* Boucle de parcours */
+ sized_binary_t str; /* Chaîne à charger */
+
+ result = load_uleb128(&count, fd);
+
+ if (result)
+ {
+ g_mutex_lock(&tpmem->mutex);
+
+ tpmem->count = count;
+
+ assert(tpmem->gtypes == NULL);
+ tpmem->gtypes = calloc(count, sizeof(gtype_ref_info_t));
+
+ for (i = 0; i < tpmem->count && result; i++)
+ {
+ result = load_sized_binary_as_string(&str, fd);
+ if (!result) break;
+
+ tpmem->gtypes[i].gtype = g_type_from_name(str.data);
+ result = (tpmem->gtypes[i].gtype != 0);
+
+ if (!result)
+ log_variadic_message(LMT_ERROR, "Unknown type: '%s'", str.data);
+
+ else
+ tpmem->gtypes[i].gclass = g_type_class_ref(tpmem->gtypes[i].gtype);
+
+ exit_sized_binary(&str);
+
+ }
+
+ g_mutex_unlock(&tpmem->mutex);
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : tpmem = mémoire à consulter. *
+* fd = flux ouvert en écriture. *
+* *
+* Description : Enregistre tous les types mémorisés dans un tampon. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool g_type_memory_store(GTypeMemory *tpmem, int fd)
+{
+ bool result; /* Bilan à enregistrer */
+ uint64_t i; /* Boucle de parcours */
+ const gchar *name; /* Désignation d'un type */
+ sized_binary_t str; /* Chaîne à conserver */
+
+ g_mutex_lock(&tpmem->mutex);
+
+ result = store_uleb128((uleb128_t []){ tpmem->count }, fd);
+
+ for (i = 0; i < tpmem->count && result; i++)
+ {
+ name = g_type_name(tpmem->gtypes[i].gtype);
+
+ setup_sized_binary_from_static_string(&str, name);
+
+ store_sized_binary_as_string(&str, fd);
+
+ }
+
+ g_mutex_unlock(&tpmem->mutex);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : tpmem = mémoire à manipuler. *
+* fd = flux ouvert en lecture. *
+* *
+* 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_from_gtype(GTypeMemory *tpmem, int fd)
+{
+ GObject *result; /* Nouvelle instance à renvoyer*/
+ uleb128_t index; /* Indice du point d'insertion */
+ bool status; /* Bilan d'une récupération */
+
+ result = NULL;
+
+ status = load_uleb128(&index, fd);
+
+ 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. *
+* fd = flux ouvert en écriture. *
+* *
+* 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, int fd)
+{
+ bool result; /* Bilan à retourner */
+ GType gtype; /* Type à enregistrer */
+ size_t index; /* Indice du point d'insertion */
+
+ gtype = G_TYPE_FROM_INSTANCE(obj);
+
+ 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));
+
+ tpmem->gtypes[index].gtype = gtype;
+ tpmem->gtypes[index].gclass = g_type_class_ref(gtype);
+
+ }
+
+ g_mutex_unlock(&tpmem->mutex);
+
+ result = store_uleb128((uleb128_t []){ index }, fd);
+
+ return result;
+
+}