From bd7816732508f5e1262b2292779db313d8969184 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Thu, 12 Oct 2023 13:05:51 +0200
Subject: Include a "wide" modifier for ROST patterns.

---
 .../pychrysalide/analysis/scan/patterns/modifier.c |   2 +-
 src/analysis/scan/core.c                           |   2 +
 src/analysis/scan/patterns/modifiers/Makefile.am   |   1 +
 src/analysis/scan/patterns/modifiers/wide.c        | 291 +++++++++++++++++++++
 src/analysis/scan/patterns/modifiers/wide.h        |  58 ++++
 tests/analysis/scan/pyapi.py                       |   9 +
 6 files changed, 362 insertions(+), 1 deletion(-)
 create mode 100644 src/analysis/scan/patterns/modifiers/wide.c
 create mode 100644 src/analysis/scan/patterns/modifiers/wide.h

diff --git a/plugins/pychrysalide/analysis/scan/patterns/modifier.c b/plugins/pychrysalide/analysis/scan/patterns/modifier.c
index 0fec60c..6547d91 100644
--- a/plugins/pychrysalide/analysis/scan/patterns/modifier.c
+++ b/plugins/pychrysalide/analysis/scan/patterns/modifier.c
@@ -318,7 +318,7 @@ PyTypeObject *get_python_scan_token_modifier_type(void)
         .tp_name        = "pychrysalide.analysis.scan.patterns.TokenModifier",
         .tp_basicsize   = sizeof(PyGObject),
 
-        .tp_flags       = Py_TPFLAGS_DEFAULT,
+        .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
 
         .tp_doc         = SCAN_TOKEN_MODIFIER_DOC,
 
diff --git a/src/analysis/scan/core.c b/src/analysis/scan/core.c
index 7b1a455..867e084 100644
--- a/src/analysis/scan/core.c
+++ b/src/analysis/scan/core.c
@@ -49,6 +49,7 @@
 #include "patterns/modifiers/hex.h"
 #include "patterns/modifiers/plain.h"
 #include "patterns/modifiers/rev.h"
+#include "patterns/modifiers/wide.h"
 #include "patterns/modifiers/xor.h"
 
 
@@ -144,6 +145,7 @@ bool load_all_known_scan_token_modifiers(void)
     if (result) result = REGISTER_SCAN_MODIFIER(g_scan_hex_modifier_new());
     if (result) result = REGISTER_SCAN_MODIFIER(g_scan_plain_modifier_new());
     if (result) result = REGISTER_SCAN_MODIFIER(g_scan_reverse_modifier_new());
+    if (result) result = REGISTER_SCAN_MODIFIER(g_scan_wide_modifier_new());
     if (result) result = REGISTER_SCAN_MODIFIER(g_scan_xor_modifier_new());
 
     return result;
diff --git a/src/analysis/scan/patterns/modifiers/Makefile.am b/src/analysis/scan/patterns/modifiers/Makefile.am
index f66393e..96d549a 100644
--- a/src/analysis/scan/patterns/modifiers/Makefile.am
+++ b/src/analysis/scan/patterns/modifiers/Makefile.am
@@ -8,6 +8,7 @@ libanalysisscanpatternsmodifiers_la_SOURCES = \
 	list.h list.c							\
 	plain.h plain.c							\
 	rev.h rev.c								\
+	wide.h wide.c							\
 	xor.h xor.c
 
 libanalysisscanpatternsmodifiers_la_CFLAGS = $(LIBGOBJ_CFLAGS)
diff --git a/src/analysis/scan/patterns/modifiers/wide.c b/src/analysis/scan/patterns/modifiers/wide.c
new file mode 100644
index 0000000..ef252d9
--- /dev/null
+++ b/src/analysis/scan/patterns/modifiers/wide.c
@@ -0,0 +1,291 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * wide.c - transcription d'une séquence d'octets en UTF-16
+ *
+ * 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 "wide.h"
+
+
+#include <malloc.h>
+#include <string.h>
+
+
+#include "../modifier-int.h"
+
+
+
+/* ----------------------- RECHERCHE D'UN MOTIF DE TEXTE BRUT ----------------------- */
+
+
+/* Initialise la classe des transcriptions en UTF-16. */
+static void g_scan_wide_modifier_class_init(GScanWideModifierClass *klass);
+
+/* Initialise une instance de transcription en UTF-16. */
+static void g_scan_wide_modifier_init(GScanWideModifier *);
+
+/* Supprime toutes les références externes. */
+static void g_scan_wide_modifier_dispose(GScanWideModifier *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_scan_wide_modifier_finalize(GScanWideModifier *);
+
+
+
+/* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */
+
+
+/* Fournit le nom d'appel d'un modificateur pour motif. */
+static char *g_scan_wide_modifier_get_name(const GScanWideModifier *);
+
+/* Transforme une séquence d'octets pour motif de recherche. */
+static bool g_scan_wide_modifier_transform(const GScanWideModifier *, const sized_binary_t *, size_t, sized_binary_t **, size_t *);
+
+/* Retrouve l'origine d'une correspondance à partir d'un indice. */
+static char *g_scan_wide_modifier_get_path(const GScanWideModifier *, size_t *);
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/*                         RECHERCHE D'UN MOTIF DE TEXTE BRUT                         */
+/* ---------------------------------------------------------------------------------- */
+
+
+/* Indique le type défini pour une transcription d'une séquence d'octets en UTF-16. */
+G_DEFINE_TYPE(GScanWideModifier, g_scan_wide_modifier, G_TYPE_SCAN_TOKEN_MODIFIER);
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : klass = classe à initialiser.                                *
+*                                                                             *
+*  Description : Initialise la classe des transcriptions en UTF-16.           *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void g_scan_wide_modifier_class_init(GScanWideModifierClass *klass)
+{
+    GObjectClass *object;                   /* Autre version de la classe  */
+    GScanTokenModifierClass *modifier;      /* Version de classe parente   */
+
+    object = G_OBJECT_CLASS(klass);
+
+    object->dispose = (GObjectFinalizeFunc/* ! */)g_scan_wide_modifier_dispose;
+    object->finalize = (GObjectFinalizeFunc)g_scan_wide_modifier_finalize;
+
+    modifier = G_SCAN_TOKEN_MODIFIER_CLASS(klass);
+
+    modifier->get_name = (get_scan_modifier_name_fc)g_scan_wide_modifier_get_name;
+
+    modifier->transform = (transform_scan_token_fc)g_scan_wide_modifier_transform;
+    modifier->get_path = (get_modifier_path)g_scan_wide_modifier_get_path;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : modifier = instance à initialiser.                           *
+*                                                                             *
+*  Description : Initialise une instance de transcription en UTF-16.          *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void g_scan_wide_modifier_init(GScanWideModifier *modifier)
+{
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : modifier = instance d'objet GLib à traiter.                  *
+*                                                                             *
+*  Description : Supprime toutes les références externes.                     *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void g_scan_wide_modifier_dispose(GScanWideModifier *modifier)
+{
+    G_OBJECT_CLASS(g_scan_wide_modifier_parent_class)->dispose(G_OBJECT(modifier));
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : modifier = instance d'objet GLib à traiter.                  *
+*                                                                             *
+*  Description : Procède à la libération totale de la mémoire.                *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void g_scan_wide_modifier_finalize(GScanWideModifier *modifier)
+{
+    G_OBJECT_CLASS(g_scan_wide_modifier_parent_class)->finalize(G_OBJECT(modifier));
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : -                                                            *
+*                                                                             *
+*  Description : Construit un modificateur livrant des octets en UTF-16.      *
+*                                                                             *
+*  Retour      : Mécanisme mis en place.                                      *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+GScanTokenModifier *g_scan_wide_modifier_new(void)
+{
+    GScanTokenModifier *result;                    /* Structure à retourner       */
+
+    result = g_object_new(G_TYPE_SCAN_WIDE_MODIFIER, NULL);
+
+    return result;
+
+}
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/*                       IMPLEMENTATION DES FONCTIONS DE CLASSE                       */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : modifier = modificateur à consulter.                         *
+*                                                                             *
+*  Description : Fournit le nom d'appel d'un modificateur pour motif.         *
+*                                                                             *
+*  Retour      : Désignation humaine.                                         *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static char *g_scan_wide_modifier_get_name(const GScanWideModifier *modifier)
+{
+    char *result;                           /* Désignation à retourner     */
+
+    result = strdup("wide");
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : modifier = modificateur à solliciter.                        *
+*                src      = séquences d'octets à traiter.                     *
+*                scount   = quantité de ces séquences.                        *
+*                dest     = nouvelle(s) séquence(s) d'octets obtenue(s) [OUT] *
+*                dcount   = quantité de ces séquences.                        *
+*                                                                             *
+*  Description : Transforme une séquence d'octets pour motif de recherche.    *
+*                                                                             *
+*  Retour      : Bilan de l'opération : succès ou échec.                      *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static bool g_scan_wide_modifier_transform(const GScanWideModifier *modifier, const sized_binary_t *src, size_t scount, sized_binary_t **dest, size_t *dcount)
+{
+    bool result;                            /* Bilan d'opération à renvoyer*/
+    sized_binary_t *binary;                 /* Raccourci vers le stockage  */
+    size_t i;                               /* Boucle de parcours #1       */
+    const sized_binary_t *_src;             /* Source courante             */
+    size_t k;                               /* Boucle de parcours #2       */
+
+    result = true;
+
+    *dcount = scount;
+    *dest = malloc(*dcount * sizeof(sized_binary_t));
+
+    binary = &(*dest)[0];
+
+    for (i = 0; i < scount; i++, binary++)
+    {
+        _src = src + i;
+
+        binary->len = _src->len * 2;
+        binary->data = calloc(binary->len * 2, sizeof(bin_t));
+
+        for (k = 0; k < _src->len; k++)
+            binary->data[k * 2] = _src->data[k];
+
+    }
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : modifier = modificateur à consulter.                         *
+*                index    = indice de la combinaison ciblée. [OUT]            *
+*                                                                             *
+*  Description : Retrouve l'origine d'une correspondance à partir d'un indice.*
+*                                                                             *
+*  Retour      : Version humainement lisible de la combinaison.               *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static char *g_scan_wide_modifier_get_path(const GScanWideModifier *modifier, size_t *index)
+{
+    char *result;                           /* Combinaison à retourner     */
+
+    if (*index > 0)
+    {
+        result = NULL;
+        (*index)--;
+    }
+
+    else
+        result = strdup("wide");
+
+    return result;
+
+}
diff --git a/src/analysis/scan/patterns/modifiers/wide.h b/src/analysis/scan/patterns/modifiers/wide.h
new file mode 100644
index 0000000..deb92a4
--- /dev/null
+++ b/src/analysis/scan/patterns/modifiers/wide.h
@@ -0,0 +1,58 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * wide.h - prototypes pour la transcription d'une séquence d'octets en UTF-16
+ *
+ * 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/>.
+ */
+
+
+#ifndef _ANALYSIS_SCAN_PATTERNS_MODIFIERS_WIDE_H
+#define _ANALYSIS_SCAN_PATTERNS_MODIFIERS_WIDE_H
+
+
+#include <glib-object.h>
+
+
+#include "../modifier.h"
+
+
+
+#define G_TYPE_SCAN_WIDE_MODIFIER            g_scan_wide_modifier_get_type()
+#define G_SCAN_WIDE_MODIFIER(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_SCAN_WIDE_MODIFIER, GScanWideModifier))
+#define G_IS_SCAN_WIDE_MODIFIER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_SCAN_WIDE_MODIFIER))
+#define G_SCAN_WIDE_MODIFIER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_SCAN_WIDE_MODIFIER, GScanWideModifierClass))
+#define G_IS_SCAN_WIDE_MODIFIER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_SCAN_WIDE_MODIFIER))
+#define G_SCAN_WIDE_MODIFIER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_SCAN_WIDE_MODIFIER, GScanWideModifierClass))
+
+
+/* Transcription d'une séquence d'octets en UTF-16 (instance) */
+typedef GScanTokenModifier GScanWideModifier;
+
+/* Transcription d'une séquence d'octets en UTF-16 (classe) */
+typedef GScanTokenModifierClass GScanWideModifierClass;
+
+
+/* Indique le type défini pour une transcription d'une séquence d'octets en UTF-16. */
+GType g_scan_wide_modifier_get_type(void);
+
+/* Construit un modificateur livrant des octets en UTF-16. */
+GScanTokenModifier *g_scan_wide_modifier_new(void);
+
+
+
+#endif  /* _ANALYSIS_SCAN_PATTERNS_MODIFIERS_WIDE_H */
diff --git a/tests/analysis/scan/pyapi.py b/tests/analysis/scan/pyapi.py
index 006c17a..29e813b 100644
--- a/tests/analysis/scan/pyapi.py
+++ b/tests/analysis/scan/pyapi.py
@@ -92,6 +92,15 @@ class TestRostPythonAPI(ChrysalideTestCase):
         self.assertEqual(source[::-1], transformed[0])
 
 
+        mod = find_token_modifiers_for_name('wide')
+        self.assertIsNotNone(mod)
+
+        source = b'ABC'
+        transformed = mod.transform(source)
+
+        self.assertEqual(source.decode('ascii'), transformed[0].decode('utf-16-le'))
+
+
         mod = find_token_modifiers_for_name('base64')
         self.assertIsNotNone(mod)
 
-- 
cgit v0.11.2-87-g4458