summaryrefslogtreecommitdiff
path: root/src/analysis/scan/exprs/str.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/analysis/scan/exprs/str.c')
-rw-r--r--src/analysis/scan/exprs/str.c437
1 files changed, 437 insertions, 0 deletions
diff --git a/src/analysis/scan/exprs/str.c b/src/analysis/scan/exprs/str.c
new file mode 100644
index 0000000..675b2f6
--- /dev/null
+++ b/src/analysis/scan/exprs/str.c
@@ -0,0 +1,437 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * str.c - gestion des opérations booléennes
+ *
+ * Copyright (C) 2022 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 "str.h"
+
+
+#include <assert.h>
+#include <string.h>
+#include <strings.h>
+
+
+#include "str-int.h"
+#include "literal.h"
+
+
+
+/* --------------------- INTRODUCTION D'UNE NOUVELLE EXPRESSION --------------------- */
+
+
+/* Initialise la classe des opérations visant des chaînes. */
+static void g_string_operation_class_init(GStringOperationClass *);
+
+/* Initialise une instance d'opération visant une chaîne. */
+static void g_string_operation_init(GStringOperation *);
+
+/* Supprime toutes les références externes. */
+static void g_string_operation_dispose(GStringOperation *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_string_operation_finalize(GStringOperation *);
+
+
+
+/* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */
+
+
+/* Reproduit une expression en place dans une nouvelle instance. */
+static GScanExpression *g_string_operation_duplicate(const GStringOperation *);
+
+/* Réduit une expression à une forme plus simple. */
+GScanExpression *g_string_operation_reduce(GStringOperation *, GScanContext *, bool);
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* INTRODUCTION D'UNE NOUVELLE EXPRESSION */
+/* ---------------------------------------------------------------------------------- */
+
+
+/* Indique le type défini pour une opération traitant une chaîne de caractères. */
+G_DEFINE_TYPE(GStringOperation, g_string_operation, G_TYPE_SCAN_EXPRESSION);
+
+
+/******************************************************************************
+* *
+* Paramètres : klass = classe à initialiser. *
+* *
+* Description : Initialise la classe des opérations visant des chaînes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_string_operation_class_init(GStringOperationClass *klass)
+{
+ GObjectClass *object; /* Autre version de la classe */
+ GScanExpressionClass *expr; /* Version de classe parente */
+
+ object = G_OBJECT_CLASS(klass);
+
+ object->dispose = (GObjectFinalizeFunc/* ! */)g_string_operation_dispose;
+ object->finalize = (GObjectFinalizeFunc)g_string_operation_finalize;
+
+ expr = G_SCAN_EXPRESSION_CLASS(klass);
+
+ expr->cmp_rich = (compare_expr_rich_fc)NULL;
+ expr->dup = (dup_expr_fc)g_string_operation_duplicate;
+ expr->reduce = (reduce_expr_fc)g_string_operation_reduce;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : op = instance à initialiser. *
+* *
+* Description : Initialise une instance d'opération visant une chaîne. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_string_operation_init(GStringOperation *op)
+{
+ op->first = NULL;
+ op->second = NULL;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : op = instance d'objet GLib à traiter. *
+* *
+* Description : Supprime toutes les références externes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_string_operation_dispose(GStringOperation *op)
+{
+ g_clear_object(&op->first);
+ g_clear_object(&op->second);
+
+ G_OBJECT_CLASS(g_string_operation_parent_class)->dispose(G_OBJECT(op));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : op = instance d'objet GLib à traiter. *
+* *
+* Description : Procède à la libération totale de la mémoire. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_string_operation_finalize(GStringOperation *op)
+{
+ G_OBJECT_CLASS(g_string_operation_parent_class)->finalize(G_OBJECT(op));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : type = type d'opération booléenne à représenter. *
+* first = premier opérande concerné. *
+* second = éventuel second opérande impliqué ou NULL. *
+* sensitive = détermine la prise en compte de la casse. *
+* *
+* Description : Organise un appel de fonction avec ses arguments. *
+* *
+* Retour : Fonction mise en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GScanExpression *g_string_operation_new(StringOperationType type, GScanExpression *first, GScanExpression *second, bool sensitive)
+{
+ GScanExpression *result; /* Structure à retourner */
+
+ result = g_object_new(G_TYPE_STRING_OPERATION, NULL);
+
+ if (!g_string_operation_create(G_STRING_OPERATION(result), type, first, second, sensitive))
+ g_clear_object(&result);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : op = instance à initialiser pleinement. *
+* type = type d'opération booléenne à représenter. *
+* first = premier opérande concerné. *
+* second = éventuel second opérande impliqué ou NULL. *
+* sensitive = détermine la prise en compte de la casse. *
+* *
+* Description : Met en place une expression d'opération traite une chaîne. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool g_string_operation_create(GStringOperation *op, StringOperationType type, GScanExpression *first, GScanExpression *second, bool sensitive)
+{
+ bool result; /* Bilan à retourner */
+ ExprValueType vtype; /* Type de valeur portée */
+
+ result = false;
+
+ vtype = g_scan_expression_get_value_type(first);
+
+ if (vtype != EVT_STRING && vtype != EVT_PENDING)
+ goto exit;
+
+ vtype = g_scan_expression_get_value_type(second);
+
+ if (vtype != EVT_STRING && vtype != EVT_REG_EXPR && vtype != EVT_PENDING)
+ goto exit;
+
+ op->type = type;
+
+ switch (type)
+ {
+ case SOT_CONTAINS:
+ case SOT_STARTSWITH:
+ case SOT_ENDSWITH:
+ op->case_sensitive = sensitive;
+ break;
+
+ case SOT_MATCHES:
+ break;
+
+ case SOT_IEQUALS:
+ assert(!sensitive);
+ op->case_sensitive = false;
+ break;
+
+ }
+
+ op->first = first;
+ g_object_ref(G_OBJECT(op->first));
+
+ op->second = second;
+ g_object_ref(G_OBJECT(op->second));
+
+ result = true;
+
+ exit:
+
+ return result;
+
+}
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* IMPLEMENTATION DES FONCTIONS DE CLASSE */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+* *
+* Paramètres : op = expression à copier. *
+* *
+* Description : Reproduit une expression en place dans une nouvelle instance.*
+* *
+* Retour : Nouvelle instance d'expression. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static GScanExpression *g_string_operation_duplicate(const GStringOperation *op)
+{
+ GScanExpression *result; /* Instance copiée à retourner */
+
+ result = g_string_operation_new(op->type, op->first, op->second, op->case_sensitive);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : op = expression à consulter. *
+* ctx = contexte de suivi de l'analyse courante. *
+* final = impose une conversion finale de dernier tour. *
+* *
+* Description : Réduit une expression à une forme plus simple. *
+* *
+* Retour : Réduction correspondante, expression déjà réduite, ou NULL. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GScanExpression *g_string_operation_reduce(GStringOperation *op, GScanContext *ctx, bool final)
+{
+ GScanExpression *result; /* Instance à renvoyer */
+ GScanExpression *new; /* Nouvelle expression obtenue */
+ const char *strings[2]; /* Chaînes en jeu */
+ bool status; /* Bilan intermédiaire */
+ char *found; /* Eventuelle portion trouvée */
+ size_t len[2]; /* Tailles max. de comparaison */
+ int ret; /* Bilan de comparaison */
+ const regex_t *preg; /* Expression rationnelle */
+
+ result = NULL;
+
+ /* Réduction des éléments considérés */
+
+ new = g_scan_expression_reduce(op->first, ctx, final);
+
+ if (new != NULL)
+ {
+ g_object_unref(G_OBJECT(op->first));
+ op->first = new;
+ }
+
+ new = g_scan_expression_reduce(op->second, ctx, final);
+
+ if (new != NULL)
+ {
+ g_object_unref(G_OBJECT(op->second));
+ op->second = new;
+ }
+
+ /* Construction d'une réduction locale ? */
+
+ if (!G_IS_LITERAL_EXPRESSION(op->first))
+ goto exit;
+
+ if (!G_IS_LITERAL_EXPRESSION(op->second))
+ goto exit;
+
+ status = g_literal_expression_get_string_value(G_LITERAL_EXPRESSION(op->first), &strings[0]);
+ if (!status) goto exit;
+
+ switch (op->type)
+ {
+ case SOT_CONTAINS:
+
+ status = g_literal_expression_get_string_value(G_LITERAL_EXPRESSION(op->second), &strings[1]);
+ if (!status) goto exit;
+
+ if (op->case_sensitive)
+ found = strstr(strings[0], strings[1]);
+ else
+ found = strcasestr(strings[0], strings[1]);
+
+ result = g_literal_expression_new(EVT_BOOLEAN, (bool []) { found != NULL });
+ break;
+
+ case SOT_STARTSWITH:
+
+ status = g_literal_expression_get_string_value(G_LITERAL_EXPRESSION(op->second), &strings[1]);
+ if (!status) goto exit;
+
+ len[1] = strlen(strings[1]);
+
+ if (op->case_sensitive)
+ ret = strncmp(strings[0], strings[1], len[1]);
+ else
+ ret = strncasecmp(strings[0], strings[1], len[1]);
+
+ result = g_literal_expression_new(EVT_BOOLEAN, (bool []) { ret == 0 });
+ break;
+
+ case SOT_ENDSWITH:
+
+ len[0] = strlen(strings[0]);
+
+ status = g_literal_expression_get_string_value(G_LITERAL_EXPRESSION(op->second), &strings[1]);
+ if (!status) goto exit;
+
+ len[1] = strlen(strings[1]);
+
+ if (len[0] < len[1])
+ result = g_literal_expression_new(EVT_BOOLEAN, (bool []) { false });
+
+ else
+ {
+ if (op->case_sensitive)
+ ret = strncmp(strings[0] + (len[0] - len[1]), strings[1], len[1]);
+ else
+ ret = strncasecmp(strings[0] + (len[0] - len[1]), strings[1], len[1]);
+
+ result = g_literal_expression_new(EVT_BOOLEAN, (bool []) { ret == 0 });
+
+ }
+ break;
+
+ case SOT_MATCHES:
+
+ status = g_literal_expression_get_regex_value(G_LITERAL_EXPRESSION(op->second), &preg);
+ if (!status) goto exit;
+
+ ret = regexec(preg, strings[0], 0, NULL, 0);
+
+ result = g_literal_expression_new(EVT_BOOLEAN, (bool []) { ret != REG_NOMATCH });
+ break;
+
+ case SOT_IEQUALS:
+
+ len[0] = strlen(strings[0]);
+
+ status = g_literal_expression_get_string_value(G_LITERAL_EXPRESSION(op->second), &strings[1]);
+ if (!status) goto exit;
+
+ len[1] = strlen(strings[1]);
+
+ if (len[0] != len[1])
+ result = g_literal_expression_new(EVT_BOOLEAN, (bool []) { false });
+
+ else
+ {
+ ret = strcasecmp(strings[0], strings[1]);
+ result = g_literal_expression_new(EVT_BOOLEAN, (bool []) { ret == 0 });
+ }
+ break;
+
+ }
+
+ exit:
+
+ return result;
+
+}