summaryrefslogtreecommitdiff
path: root/src/analysis/scan/patterns/tokens/nodes/any.c
blob: af2ae2925d9aa237085d203bc52f9ede4ec86f85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425

/* Chrysalide - Outil d'analyse de fichiers binaires
 * any.c - suite d'octets quelconques
 *
 * 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 "any.h"


#include <assert.h>


#include "any-int.h"



/* ------------------------ DECOMPOSITION DE MOTIF RECHERCHE ------------------------ */


/* Initialise la classe des séries d'octets quelconques. */
static void g_scan_token_node_any_class_init(GScanTokenNodeAnyClass *);

/* Initialise une instance de série d'octets quelconques. */
static void g_scan_token_node_any_init(GScanTokenNodeAny *);

/* Supprime toutes les références externes. */
static void g_scan_token_node_any_dispose(GScanTokenNodeAny *);

/* Procède à la libération totale de la mémoire. */
static void g_scan_token_node_any_finalize(GScanTokenNodeAny *);



/* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */


/* Inscrit la définition d'un motif dans un moteur de recherche. */
static bool g_scan_token_node_any_enroll(GScanTokenNodeAny *, GScanContext *, GEngineBackend *, size_t, size_t *);

/* Transforme les correspondances locales en trouvailles. */
static void g_scan_token_node_any_check_forward(const GScanTokenNodeAny *, GScanContext *, GBinContent *, pending_matches_t *, node_search_offset_t *, bool, bool *);

/* Transforme les correspondances locales en trouvailles. */
static void g_scan_token_node_any_check_backward(const GScanTokenNodeAny *, GScanContext *, GBinContent *, pending_matches_t *, node_search_offset_t *, bool, bool *);



/* ---------------------------------------------------------------------------------- */
/*                          DECOMPOSITION DE MOTIF RECHERCHE                          */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini pour une série d'octets quelconque, vide ou non. */
G_DEFINE_TYPE(GScanTokenNodeAny, g_scan_token_node_any, G_TYPE_SCAN_TOKEN_NODE);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des séries d'octets quelconques.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_scan_token_node_any_class_init(GScanTokenNodeAnyClass *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_any_dispose;
    object->finalize = (GObjectFinalizeFunc)g_scan_token_node_any_finalize;

    node = G_SCAN_TOKEN_NODE_CLASS(klass);

    node->visit = (visit_scan_token_node_fc)NULL;
    node->enroll = (enroll_scan_token_node_fc)g_scan_token_node_any_enroll;
    node->check_forward = (check_scan_token_node_fc)g_scan_token_node_any_check_forward;
    node->check_backward = (check_scan_token_node_fc)g_scan_token_node_any_check_backward;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : any = instance à initialiser.                                *
*                                                                             *
*  Description : Initialise une instance de série d'octets quelconques.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_scan_token_node_any_init(GScanTokenNodeAny *any)
{
    g_scan_token_node_set_flags(G_SCAN_TOKEN_NODE(any), STNF_PROD);

    any->min = 0;
    any->has_max = false;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : any = instance d'objet GLib à traiter.                       *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_scan_token_node_any_dispose(GScanTokenNodeAny *any)
{
    G_OBJECT_CLASS(g_scan_token_node_any_parent_class)->dispose(G_OBJECT(any));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : any = instance d'objet GLib à traiter.                       *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_scan_token_node_any_finalize(GScanTokenNodeAny *any)
{
    G_OBJECT_CLASS(g_scan_token_node_any_parent_class)->finalize(G_OBJECT(any));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : min = éventuelle quantité minimale à retrouver.              *
*                max = éventuelle quantité maximale à retrouver.              *
*                                                                             *
*  Description : Construit un noeud pointant une série d'octets quelconques.  *
*                                                                             *
*  Retour      : Mécanismes mis en place.                                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GScanTokenNode *g_scan_token_node_any_new(const phys_t *min, const phys_t *max)
{
    GScanTokenNode *result;                 /* Structure à retourner       */

    result = g_object_new(G_TYPE_SCAN_TOKEN_NODE_ANY, NULL);

    if (!g_scan_token_node_any_create(G_SCAN_TOKEN_NODE_ANY(result), min, max))
        g_clear_object(&result);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : any = séquence d'octets quelconques à initialiser pleinement.*
*                min = éventuelle quantité minimale à retrouver.              *
*                max = éventuelle quantité maximale à retrouver.              *
*                                                                             *
*  Description : Met en place un un noeud pointant une série d'octets.        *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_scan_token_node_any_create(GScanTokenNodeAny *any, const phys_t *min, const phys_t *max)
{
    bool result;                            /* Bilan à retourner           */

    result = true;

    if (min != NULL)
        any->min = *min;
    else
        any->min = 0;

    if (max != NULL)
    {
        any->max = *max;

        result = (any->min <= any->max);

    }

    any->has_max = (max != NULL);

    return result;

}



/* ---------------------------------------------------------------------------------- */
/*                       IMPLEMENTATION DES FONCTIONS DE CLASSE                       */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : node    = définition de la bribe à enregistrer.              *
*                context = contexte de l'analyse à mener.                     *
*                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_any_enroll(GScanTokenNodeAny *node, GScanContext *context, GEngineBackend *backend, size_t maxsize, size_t *slow)
{
    bool result;                            /* Statut à retourner          */
    bool forced;                            /* Inclusion dans un scan ?    */

    result = true;

    forced = (g_scan_token_node_get_flags(G_SCAN_TOKEN_NODE(node)) & STNF_MAIN);

    if (forced)
        *slow += (maxsize * 4);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : node    = définition de la bribe à manipuler.                *
*                context = contexte de l'analyse à mener.                     *
*                content = accès au contenu brut pour vérifications (optim.)  *
*                matches = suivi des correspondances à consolider.            *
*                offset  = tolérance dans les positions à appliquer.          *
*                not     = indique si les résultats doivent être inversés.    *
*                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_any_check_forward(const GScanTokenNodeAny *node, GScanContext *context, GBinContent *content, pending_matches_t *matches, node_search_offset_t *offset, bool not, bool *skip)
{
    bool initialized;                       /* Initialisation du suivi ?   */
    bool forced;                            /* Inclusion dans un scan ?    */
    phys_t size;                            /* Quantité d'octets considérés*/
    const phys_t *datasize;                 /* Taille max. à communiquer   */

    if (*skip)
        return;


    //         $a = { [1-3] 6f }
    // pas d'initialisation, construction de résultats avec une taille nulle



    initialized = are_pending_matches_initialized(matches);

    forced = (g_scan_token_node_get_flags(G_SCAN_TOKEN_NODE(node)) & STNF_MAIN);

    size = matches->content_end - matches->content_start;

    datasize = (not ? &size : NULL);

    if (forced)
    {
        assert(!initialized);

        if (node->min > size)
            /* TODO set abort in matches */;

        else
            add_range_to_node_search_offset(offset,
                                            matches->content_start,
                                            matches->content_end - matches->content_start,
                                            datasize);

    }
    else
    {
        assert(initialized);


        // TODO : compléter les intervales éventuels déjà en place


        printf("[i] create hole: %llx <-> %llx\n",
               (unsigned long long)node->min,
               (unsigned long long)node->max);



        if (node->has_max)
            add_range_to_node_search_offset(offset, node->min, node->max, datasize);
        else
            add_range_to_node_search_offset(offset, node->min, matches->content_end - node->min, datasize);

        // TODO : si dernier, virer les correspondances qui n'ont plus l'espace de fin requis
        // -> au niveau du noeud, en fonction du flag _LAST

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : node    = définition de la bribe à manipuler.                *
*                context = contexte de l'analyse à mener.                     *
*                content = accès au contenu brut pour vérifications (optim.)  *
*                matches = suivi des correspondances à consolider.            *
*                offset  = tolérance dans les positions à appliquer.          *
*                not     = indique si les résultats doivent être inversés.    *
*                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_any_check_backward(const GScanTokenNodeAny *node, GScanContext *context, GBinContent *content, pending_matches_t *matches, node_search_offset_t *offset, bool not, bool *skip)
{
#ifndef NDEBUG
    bool forced;                            /* Inclusion dans un scan ?    */
#endif
    phys_t size;                            /* Quantité d'octets considérés*/
    const phys_t *datasize;                 /* Taille max. à communiquer   */

    if (*skip)
        return;

    /**
     * En lecture à rebourd, au moins un noeud a été solicité pour analyse (lors
     * du sens de lecteur normal). Donc l'initialisation a déjà dû avoir lieu.
     */
    assert(are_pending_matches_initialized(matches));

    /**
     * Si les recherches associées au noeud ont été forcées, alors les traitements
     * liés ont déjà été effectués, et l'appel de cette fonction aurait dû être sauté.
     */
#ifndef NDEBUG
    forced = (g_scan_token_node_get_flags(G_SCAN_TOKEN_NODE(node)) & STNF_MAIN);
    assert(!forced);
#endif

    size = matches->content_end - matches->content_start;

    if (node->min > size)
        /* TODO set abort in matches */;

    else
    {
        datasize = (not ? &size : NULL);

        /**
         * Une tolérance basée sur des espaces (et non des positions) est déterminée
         * ici.
         *
         * Charge au prochain noeud de traitement de filtrer les résultats courants
         * avec, voire à la fonction _g_scan_token_node_check_backward() de
         * réaliser une synthèse finale si le noeud courant est le dernier d'une
         * lignée.
         */

        if (node->has_max)
            add_range_to_node_search_offset(offset, node->min, node->max, datasize);
        else
            add_range_to_node_search_offset(offset, node->min, matches->content_end - node->min, datasize);

    }

}