summaryrefslogtreecommitdiff
path: root/src/analysis/scan/patterns/tokens/nodes/choice.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/analysis/scan/patterns/tokens/nodes/choice.c')
-rw-r--r--src/analysis/scan/patterns/tokens/nodes/choice.c646
1 files changed, 646 insertions, 0 deletions
diff --git a/src/analysis/scan/patterns/tokens/nodes/choice.c b/src/analysis/scan/patterns/tokens/nodes/choice.c
new file mode 100644
index 0000000..2a5e5f5
--- /dev/null
+++ b/src/analysis/scan/patterns/tokens/nodes/choice.c
@@ -0,0 +1,646 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * choice.c - décompositions alternatives de motif de recherche
+ *
+ * Copyright (C) 2023 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 "choice.h"
+
+
+#include <assert.h>
+
+
+#include "choice-int.h"
+
+
+
+/* ------------------------ DECOMPOSITION DE MOTIF RECHERCHE ------------------------ */
+
+
+/* Initialise la classe des décompositions alternatives. */
+static void g_scan_token_node_choice_class_init(GScanTokenNodeChoiceClass *);
+
+/* Initialise une instance de décompositions alternatives. */
+static void g_scan_token_node_choice_init(GScanTokenNodeChoice *);
+
+/* Supprime toutes les références externes. */
+static void g_scan_token_node_choice_dispose(GScanTokenNodeChoice *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_scan_token_node_choice_finalize(GScanTokenNodeChoice *);
+
+
+
+/* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */
+
+
+/* Communique l'intérêt d'un noeud au sein d'une analyse. */
+static float g_scan_token_node_choice_compute_weight_for_scan(const GScanTokenNodeChoice *);
+
+/* Prend acte d'une nouvelle propriété pour le noeud. */
+static void g_scan_token_node_choice_apply_flags(GScanTokenNodeChoice *, ScanTokenNodeFlags);
+
+/* Parcourt une arborescence de noeuds et y relève des éléments. */
+static void g_scan_token_node_choice_visit(GScanTokenNodeChoice *, scan_tree_points_t *);
+
+/* Inscrit la définition d'un motif dans un moteur de recherche. */
+static bool g_scan_token_node_choice_enroll(GScanTokenNodeChoice *, GEngineBackend *, size_t, size_t *);
+
+/* Récupère un identifiant final pour un atome d'octets. */
+static bool g_scan_token_node_choice_build_id(GScanTokenNodeChoice *, GEngineBackend *);
+
+/* Transforme les correspondances locales en trouvailles. */
+static void g_scan_token_node_choice_check_forward(const GScanTokenNodeChoice *, scan_node_check_params_t *, TokenNodeCheckFlags, bool *);
+
+/* Transforme les correspondances locales en trouvailles. */
+static void g_scan_token_node_choice_check_backward(const GScanTokenNodeChoice *, scan_node_check_params_t *, TokenNodeCheckFlags, bool *);
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* DECOMPOSITION DE MOTIF RECHERCHE */
+/* ---------------------------------------------------------------------------------- */
+
+
+/* Indique le type défini pour des décompositions alternatives de motif de recherche. */
+G_DEFINE_TYPE(GScanTokenNodeChoice, g_scan_token_node_choice, G_TYPE_SCAN_TOKEN_NODE);
+
+
+/******************************************************************************
+* *
+* Paramètres : klass = classe à initialiser. *
+* *
+* Description : Initialise la classe des décompositions alternatives. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_scan_token_node_choice_class_init(GScanTokenNodeChoiceClass *klass)
+{
+ GObjectClass *object; /* Autre version de la classe */
+ GScanTokenNodeClass *node; /* Version de classe parente */
+
+ object = G_OBJECT_CLASS(klass);
+
+ object->dispose = (GObjectFinalizeFunc/* ! */)g_scan_token_node_choice_dispose;
+ object->finalize = (GObjectFinalizeFunc)g_scan_token_node_choice_finalize;
+
+ node = G_SCAN_TOKEN_NODE_CLASS(klass);
+
+ node->compute_weight = (compute_scan_token_node_weight_fc)g_scan_token_node_choice_compute_weight_for_scan;
+ node->apply = (apply_scan_token_node_flags_fc)g_scan_token_node_choice_apply_flags;
+ node->visit = (visit_scan_token_node_fc)g_scan_token_node_choice_visit;
+ node->enroll = (enroll_scan_token_node_fc)g_scan_token_node_choice_enroll;
+ node->build_id = (build_scan_token_node_id_fc)g_scan_token_node_choice_build_id;
+ node->check_forward = (check_scan_token_node_fc)g_scan_token_node_choice_check_forward;
+ node->check_backward = (check_scan_token_node_fc)g_scan_token_node_choice_check_backward;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : choice = instance à initialiser. *
+* *
+* Description : Initialise une instance de décompositions alternatives. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_scan_token_node_choice_init(GScanTokenNodeChoice *choice)
+{
+ choice->children = NULL;
+ choice->count = 0;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : choice = instance d'objet GLib à traiter. *
+* *
+* Description : Supprime toutes les références externes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_scan_token_node_choice_dispose(GScanTokenNodeChoice *choice)
+{
+ size_t i; /* Boucle de parcours */
+
+ for (i = 0; i < choice->count; i++)
+ g_clear_object(&choice->children[i]);
+
+ G_OBJECT_CLASS(g_scan_token_node_choice_parent_class)->dispose(G_OBJECT(choice));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : choice = instance d'objet GLib à traiter. *
+* *
+* Description : Procède à la libération totale de la mémoire. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_scan_token_node_choice_finalize(GScanTokenNodeChoice *choice)
+{
+ if (choice->children != NULL)
+ free(choice->children);
+
+ G_OBJECT_CLASS(g_scan_token_node_choice_parent_class)->finalize(G_OBJECT(choice));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Construit une série de décompositions alternatives de motif. *
+* *
+* Retour : Mécanismes mis en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GScanTokenNode *g_scan_token_node_choice_new(void)
+{
+ GScanTokenNode *result; /* Structure à retourner */
+
+ result = g_object_new(G_TYPE_SCAN_TOKEN_NODE_CHOICE, NULL);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : choice = ensemble de noeuds à compléter. *
+* node = nouveau noeud à intégrer. *
+* *
+* Description : Ajoute un noeud à aux décompositions alternatives de motif. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void g_scan_token_node_choice_add(GScanTokenNodeChoice *choice, GScanTokenNode *node)
+{
+ choice->children = realloc(choice->children, ++choice->count * sizeof(GScanTokenNode *));
+
+ choice->children[choice->count - 1] = node;
+ g_object_ref(G_OBJECT(node));
+
+}
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* IMPLEMENTATION DES FONCTIONS DE CLASSE */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud de motif à consulter. *
+* *
+* Description : Communique l'intérêt d'un noeud au sein d'une analyse. *
+* *
+* Retour : Poids de l'importance pour un départ de scan. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static float g_scan_token_node_choice_compute_weight_for_scan(const GScanTokenNodeChoice *node)
+{
+ float result; /* Valeur à retourner */
+ size_t weight_count; /* Nombre de comptabilisations */
+ size_t i; /* Boucle de parcours */
+ float weight; /* Nouveau poids à intégrer */
+
+ result = 0;
+
+ weight_count = 0;
+
+ for (i = 0; i < node->count; i++)
+ {
+ weight = g_scan_token_node_compute_weight_for_scan(node->children[i]);
+
+ if (weight > 0)
+ {
+ result += weight;
+ weight_count++;
+ }
+
+ }
+
+ if (weight_count != node->count)
+ result = 0;
+ else
+ result /= weight_count;
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud de motif à mettre à jour. *
+* flags = propriétés particulières à associer au noeud. *
+* *
+* Description : Prend acte d'une nouvelle propriété pour le noeud. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_scan_token_node_choice_apply_flags(GScanTokenNodeChoice *node, ScanTokenNodeFlags flags)
+{
+ size_t i; /* Boucle de parcours */
+
+ for (i = 0; i < node->count; i++)
+ g_scan_token_node_set_flags(node->children[i], flags);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = point de départ du parcours à effectuer. *
+* points = points capitaux de l'arborescence. [OUT] *
+* *
+* Description : Parcourt une arborescence de noeuds et y relève des éléments.*
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_scan_token_node_choice_visit(GScanTokenNodeChoice *node, scan_tree_points_t *points)
+{
+ size_t first_plain_count; /* Décompte de noeuds textuels */
+ size_t i; /* Boucle de parcours */
+ scan_tree_points_t tmp_points; /* Synthèse d'analyse locale */
+
+ if (points->first_plain != NULL)
+ return;
+
+ first_plain_count = 0;
+
+ for (i = 0; i < node->count; i++)
+ {
+ tmp_points.first_plain = NULL;
+ tmp_points.best_masked = NULL;
+
+ g_scan_token_node_visit(node->children[i], &tmp_points);
+
+ if (tmp_points.first_plain != NULL)
+ first_plain_count++;
+
+ }
+
+ if (first_plain_count == node->count)
+ points->first_plain = G_SCAN_TOKEN_NODE(node);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = définition de la bribe à enregistrer. *
+* backend = moteur de recherche à préchauffer. *
+* maxsize = taille max. des atomes (mise en commun optimisée). *
+* slow = niveau de ralentissement induit (0 = idéal). [OUT] *
+* *
+* Description : Inscrit la définition d'un motif dans un moteur de recherche.*
+* *
+* Retour : Bilan de l'opération à renvoyer. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static bool g_scan_token_node_choice_enroll(GScanTokenNodeChoice *node, GEngineBackend *backend, size_t maxsize, size_t *slow)
+{
+ bool result; /* Statut à retourner */
+ size_t i; /* Boucle de parcours */
+
+ result = true;
+
+ for (i = 0; i < node->count && result; i++)
+ result = _g_scan_token_node_enroll(node->children[i], backend, maxsize, slow);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = définition de la bribe à peaufiner. *
+* backend = moteur de recherche à préchauffer. *
+* *
+* Description : Récupère un identifiant final pour un atome d'octets. *
+* *
+* Retour : Bilan de l'opération à renvoyer. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static bool g_scan_token_node_choice_build_id(GScanTokenNodeChoice *node, GEngineBackend *backend)
+{
+ bool result; /* Statut à retourner */
+ size_t i; /* Boucle de parcours #1 */
+
+ result = true;
+
+ for (i = 0; i < node->count && result; i++)
+ result = g_scan_token_node_build_id(node->children[i], backend);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = définition de la bribe à manipuler. *
+* params = accès direct aux éléments utiles aux validations. *
+* cflags = altérations de traitement à respecter. *
+* skip = détermine si l'analyse est différée. [OUT] *
+* *
+* Description : Transforme les correspondances locales en trouvailles. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_scan_token_node_choice_check_forward(const GScanTokenNodeChoice *node, scan_node_check_params_t *params, TokenNodeCheckFlags cflags, bool *skip)
+{
+ bool initialized; /* Initialisation du suivi ? */
+ match_area_t *collected_areas; /* Zones mises en place ici */
+ size_t collected_count; /* Quantité de ces zones */
+ TokenNodeCheckFlags local_cflags; /* Particularités nouvelles */
+ size_t i; /* Boucle de parcours */
+ scan_node_check_params_t local_params; /* Rassemblement de paramètres */
+
+ if (0x0) printf("=================== CHECK :: %s (skip? %d)\n", __FUNCTION__, *skip);
+
+ if (*skip)
+ return;
+
+ /* Lancement des sous-traitements */
+
+ initialized = false;
+
+ collected_areas = NULL;
+ collected_count = 0;
+
+ local_cflags = (cflags & ~TNCF_UPDATE_IN_PLACE) | TNCF_CREATE_NEW;
+
+ for (i = 0; i < node->count; i++)
+ {
+ local_params = *params;
+
+ local_params.created_areas = NULL;
+ local_params.created_count = 0;
+
+ local_params.kept_areas = NULL;
+ local_params.kept_count = 0;
+
+ if ((cflags & TNCF_CREATE_NEW) == 0 && (i + 1) == node->count)
+ local_cflags = cflags;
+
+ _g_scan_token_node_check_forward(node->children[i], &local_params, local_cflags, skip);
+
+ initialized |= local_params.initialized;
+
+ if (local_cflags & TNCF_KEEP_DISCARDED)
+ {
+ merge_match_areas(&collected_areas, &local_params.kept_areas);
+ collected_count += local_params.kept_count;
+ }
+
+ else if (local_cflags & TNCF_CREATE_NEW)
+ {
+ merge_match_areas(&collected_areas, &local_params.created_areas);
+ collected_count += local_params.created_count;
+ }
+
+ else
+ {
+ assert(local_cflags & TNCF_UPDATE_IN_PLACE);
+
+ merge_match_areas(&collected_areas, &local_params.main_areas);
+ collected_count += local_params.main_count;
+
+ }
+
+ }
+
+ /* Enregistrement des résultats finaux */
+
+ if (collected_count > 1)
+ sort_match_areas_no_dup(&collected_areas, &collected_count, compare_match_area_as_dl_item, NULL);
+
+ if (0x0) printf("[%s] collected: #%zu (bis)\n", __FUNCTION__, collected_count);
+
+ params->initialized = initialized;
+
+ if (cflags & TNCF_KEEP_DISCARDED)
+ {
+ params->kept_areas = collected_areas;
+ params->kept_count = collected_count;
+ }
+
+ else if (cflags & TNCF_CREATE_NEW)
+ {
+ params->created_areas = collected_areas;
+ params->created_count = collected_count;
+ }
+
+ else
+ {
+ assert(cflags & TNCF_UPDATE_IN_PLACE);
+
+ params->main_areas = collected_areas;
+ params->main_count = collected_count;
+
+ }
+
+
+
+
+
+ /// TODO : gestion des offets en sortie : ajout (+ ajout d'un test en Python)
+
+
+
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = définition de la bribe à manipuler. *
+* params = accès direct aux éléments utiles aux validations. *
+* cflags = altérations de traitement à respecter. *
+* skip = détermine si l'analyse est différée. [OUT] *
+* *
+* Description : Transforme les correspondances locales en trouvailles. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_scan_token_node_choice_check_backward(const GScanTokenNodeChoice *node, scan_node_check_params_t *params, TokenNodeCheckFlags cflags, bool *skip)
+{
+ match_area_t *collected_areas; /* Zones mises en place ici */
+ size_t collected_count; /* Quantité de ces zones */
+ TokenNodeCheckFlags local_cflags; /* Particularités nouvelles */
+ size_t i; /* Boucle de parcours */
+ scan_node_check_params_t local_params; /* Rassemblement de paramètres */
+
+ if (0x0) printf("=================== CHECK :: %s (skip? %d)\n", __FUNCTION__, *skip);
+
+ if (*skip)
+ return;
+
+ /**
+ * En lecture à rebourd, au moins un noeud a été solicité pour analyse (lors
+ * du sens de lecture normal). Donc l'initialisation a déjà dû avoir lieu.
+ */
+
+ assert(params->initialized);
+
+ /* Lancement des sous-traitements */
+
+ collected_areas = NULL;
+ collected_count = 0;
+
+ local_cflags = (cflags & ~TNCF_UPDATE_IN_PLACE) | TNCF_CREATE_NEW;
+
+ for (i = 0; i < node->count; i++)
+ {
+ local_params = *params;
+
+ local_params.created_areas = NULL;
+ local_params.created_count = 0;
+
+ local_params.kept_areas = NULL;
+ local_params.kept_count = 0;
+
+ if ((cflags & TNCF_CREATE_NEW) == 0 && (i + 1) == node->count)
+ local_cflags = cflags;
+
+ _g_scan_token_node_check_backward(node->children[i], &local_params, local_cflags, skip);
+
+ if (local_cflags & TNCF_KEEP_DISCARDED)
+ {
+ merge_match_areas(&collected_areas, &local_params.kept_areas);
+ collected_count += local_params.kept_count;
+ }
+
+ else if (local_cflags & TNCF_CREATE_NEW)
+ {
+ merge_match_areas(&collected_areas, &local_params.created_areas);
+ collected_count += local_params.created_count;
+ }
+
+ else
+ {
+ assert(local_cflags & TNCF_UPDATE_IN_PLACE);
+
+ merge_match_areas(&collected_areas, &local_params.main_areas);
+ collected_count += local_params.main_count;
+
+ }
+
+ }
+
+ /* Enregistrement des résultats finaux */
+
+ if (collected_count > 1)
+ sort_match_areas_no_dup(&collected_areas, &collected_count, compare_match_area_as_dl_item, NULL);
+
+ if (0x0) printf("[%s] collected: #%zu (bis)\n", __FUNCTION__, collected_count);
+
+ if (cflags & TNCF_KEEP_DISCARDED)
+ {
+ params->kept_areas = collected_areas;
+ params->kept_count = collected_count;
+ }
+
+ else if (cflags & TNCF_CREATE_NEW)
+ {
+ params->created_areas = collected_areas;
+ params->created_count = collected_count;
+ }
+
+ else
+ {
+ assert(cflags & TNCF_UPDATE_IN_PLACE);
+
+ params->main_areas = collected_areas;
+ params->main_count = collected_count;
+
+ }
+
+
+
+
+
+ /// TODO : gestion des offets en sortie : ajout (+ ajout d'un test en Python)
+
+
+
+
+
+
+}