summaryrefslogtreecommitdiff
path: root/src/analysis/loading.c
blob: 5aff20f46786655d46d4a22fd580cf16f76b232a (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669

/* Chrysalide - Outil d'analyse de fichiers binaires
 * loading.c - reconnaissance de contenus binaires
 *
 * Copyright (C) 2017 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "loading.h"


#include "../core/formats.h"
#include "../core/global.h"
#include "../core/logs.h"
#include "../glibext/delayed-int.h"
#include "../glibext/signal.h"



/* ----------------------- AMORCE POUR CHARGEMENT DE CONTENUS ----------------------- */


/* Ensembles binaires à désassembler (instance) */
struct _GDelayedStudy
{
    GDelayedWork parent;                    /* A laisser en premier        */

    GStudyProject *project;                 /* Projet de rattachement      */
    GBinContent *content;                   /* Contenu binaire à traiter   */
    ProjectContentState state;              /* Renseigne le type de contenu*/

    bool only_preload;                      /* Enregistrement seulement ?  */

};

/* Ensembles binaires à désassembler (classe) */
struct _GDelayedStudyClass
{
    GDelayedWorkClass parent;               /* A laisser en premier        */

};


/* Initialise la classe des intégrations de binaires à étudier. */
static void g_delayed_study_class_init(GDelayedStudyClass *);

/* Initialise une intégration de binaire à étudier. */
static void g_delayed_study_init(GDelayedStudy *);

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

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

/* Prépare une intégration de binaire au projet courant. */
static void g_delayed_study_process(GDelayedStudy *, GtkStatusStack *);



/* ----------------------- CHARGEMENT DE BINAIRE NON BLOQUANT ----------------------- */


#define G_TYPE_BINARY_LOADER            g_binary_loader_get_type()
#define G_BINARY_LOADER(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_BINARY_LOADER, GBinaryLoader))
#define G_IS_BINARY_LOADER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_BINARY_LOADER))
#define G_BINARY_LOADER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_BINARY_LOADER, GBinaryLoaderClass))
#define G_IS_BINARY_LOADER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_BINARY_LOADER))
#define G_BINARY_LOADER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_BINARY_LOADER, GBinaryLoaderClass))


/* Chargement non bloquant d'un binaire (instance) */
struct _GBinaryLoader
{
    GDelayedWork parent;                    /* A laisser en premier        */

    bool from_content;                      /* Sélection de champs         */

    union
    {
        GBinContent *content;               /* Contenu brut à disposition  */

        struct
        {
            char *filename;                 /* Chemin vers l'ensemble XML  */
            char *path;                     /* Chemin de la définition XML */

        };

    };

    GStudyProject *project;                 /* Accès aux contenus liés     */

    GLoadedBinary *binary;                  /* Résultat du chargement      */

};

/* Chargement non bloquant d'un binaire (classe) */
struct _GBinaryLoaderClass
{
    GDelayedWorkClass parent;               /* A laisser en premier        */

};


/* Indique le type défini pour le chargement non bloquant d'un binaire. */
GType g_binary_loader_get_type(void);

/* Initialise la classe des tâches de chargement non bloquant. */
static void g_binary_loader_class_init(GBinaryLoaderClass *);

/* Initialise une tâche de chargement non bloquant d'un binaire. */
static void g_binary_loader_init(GBinaryLoader *);

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

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

/* Réalise le chargement effectif d'un binaire. */
static void g_binary_loader_process(GBinaryLoader *, GtkStatusStack *);



/* ---------------------------------------------------------------------------------- */
/*                         AMORCE POUR CHARGEMENT DE CONTENUS                         */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini pour les tâches de préparations d'étude. */
G_DEFINE_TYPE(GDelayedStudy, g_delayed_study, G_TYPE_DELAYED_WORK);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des intégrations de binaires à étudier. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_delayed_study_class_init(GDelayedStudyClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GDelayedWorkClass *work;                /* Version en classe parente   */

    object = G_OBJECT_CLASS(klass);
    work = G_DELAYED_WORK_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_delayed_study_dispose;
    object->finalize = (GObjectFinalizeFunc)g_delayed_study_finalize;

    work->run = (run_task_fc)g_delayed_study_process;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : dstudy = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une intégration de binaire à étudier.             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_delayed_study_init(GDelayedStudy *dstudy)
{
    dstudy->only_preload = false;

}


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

static void g_delayed_study_dispose(GDelayedStudy *dstudy)
{
    g_object_unref(G_OBJECT(dstudy->project));
    g_object_unref(G_OBJECT(dstudy->content));

    G_OBJECT_CLASS(g_delayed_study_parent_class)->dispose(G_OBJECT(dstudy));

}


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

static void g_delayed_study_finalize(GDelayedStudy *dstudy)
{
    G_OBJECT_CLASS(g_delayed_study_parent_class)->finalize(G_OBJECT(dstudy));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : project = projet dont le contenu est à compléter.            *
*                content = contenu binaire chargé à analyser.                 *
*                state   = état du contenu à conserver.                       *
*                                                                             *
*  Description : Crée une tâche d'intégration de contenu binaire.             *
*                                                                             *
*  Retour      : Tâche créée.                                                 *
*                                                                             *
*  Remarques   : L'appelant perd la propriété du contenu.                     *
*                                                                             *
******************************************************************************/

GDelayedStudy *g_delayed_study_new(GStudyProject *project, GBinContent *content, ProjectContentState state)
{
    GDelayedStudy *result;            /* Tâche à retourner           */

    result = g_object_new(G_TYPE_DELAYED_STUDY, NULL);

    g_object_ref(G_OBJECT(project));
    result->project = project;

    g_object_ref(G_OBJECT(content));
    result->content = content;

    result->state = state;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : dstudy = intégration à mener.                                *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Prépare une intégration de binaire au projet courant.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_delayed_study_process(GDelayedStudy *dstudy, GtkStatusStack *status)
{
    FormatMatchStatus mstatus;              /* Statut d'une reconnaissance */
    char *target;                           /* Sous-traitance requise      */
    GBinaryLoader *loader;                  /* Dispositif de chargement    */
    GWorkQueue *queue;                      /* Gestionnaire de différés    */

    mstatus = find_matching_format(dstudy->content, NULL, &target);

    switch (mstatus)
    {
        case FMS_MATCHED:

            if (dstudy->only_preload)
                g_study_project_add_binary_content(dstudy->project, dstudy->content, dstudy->state);

            else
            {
                loader = g_binary_loader_new(dstudy->content, dstudy->project);

                queue = get_work_queue();
                g_work_queue_schedule_work(queue, G_DELAYED_WORK(loader), DEFAULT_WORK_GROUP);

            }

            break;

        case FMS_FORWARDED:
            /**
             * L'émetteur de ce type de réponse a pour charge de
             * reprogrammer lui même l'analyse de nouveaux contenus.
             */
            log_variadic_message(LMT_PROCESS, _("binary '%s' contains other binaries..."),
                                 g_binary_content_describe(dstudy->content, true));

            if (dstudy->state == PCS_ROOT)
                g_study_project_add_binary_content(dstudy->project, dstudy->content, PCS_ROOT);

            break;

        default:
            /**
             * Les jeux sont faits pour le contenu binaire courant.
             */
            log_variadic_message(LMT_PROCESS, _("Unknown binary format for '%s'..."),
                                 g_binary_content_describe(dstudy->content, true));
            break;

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : dstudy = tâche d'analyse de contenu pour projet à mener.     *
*                                                                             *
*  Description : Limite l'étude et l'intégration d'un contenu binaire.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_delayed_study_preload_only(GDelayedStudy *dstudy)
{
    dstudy->only_preload = true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : content = contenu binaire chargé à analyser.                 *
*                state   = état du contenu à conserver.                       *
*                                                                             *
*  Description : Programme l'étude et l'intégration d'un contenu binaire.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void qck_study_new_content(GBinContent *content, ProjectContentState state)
{
    GStudyProject *project;                 /* Projet courant              */
    GDelayedStudy *dstudy;                  /* Etude à conduire            */

    project = get_current_project();

    dstudy = g_delayed_study_new(project, content, state);

    g_object_unref(G_OBJECT(project));

    study_new_content(dstudy);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : content = contenu binaire chargé à analyser.                 *
*                state   = état du contenu à conserver.                       *
*                                                                             *
*  Description : Programme l'étude et l'intégration d'un contenu binaire.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void study_new_content(GDelayedStudy *dstudy)
{
    GWorkQueue *queue;                      /* Gestionnaire de différés    */

    queue = get_work_queue();
    g_work_queue_schedule_work(queue, G_DELAYED_WORK(dstudy), DEFAULT_WORK_GROUP);

}



/* ---------------------------------------------------------------------------------- */
/*                         CHARGEMENT DE BINAIRE NON BLOQUANT                         */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini pour le chargement non bloquant d'un binaire. */
G_DEFINE_TYPE(GBinaryLoader, g_binary_loader, G_TYPE_DELAYED_WORK);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des tâches de chargement non bloquant.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_binary_loader_class_init(GBinaryLoaderClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GDelayedWorkClass *work;                /* Version en classe parente   */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_binary_loader_dispose;
    object->finalize = (GObjectFinalizeFunc)g_binary_loader_finalize;

    work = G_DELAYED_WORK_CLASS(klass);

    work->run = (run_task_fc)g_binary_loader_process;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : loader = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une tâche de chargement non bloquant d'un binaire.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_binary_loader_init(GBinaryLoader *loader)
{
    loader->binary = NULL;

}


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

static void g_binary_loader_dispose(GBinaryLoader *loader)
{
    if (loader->from_content)
        g_object_unref(G_OBJECT(loader->content));

    g_object_unref(G_OBJECT(loader->project));

    if (loader->binary != NULL)
        g_object_unref(G_OBJECT(loader->binary));

    G_OBJECT_CLASS(g_binary_loader_parent_class)->dispose(G_OBJECT(loader));

}


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

static void g_binary_loader_finalize(GBinaryLoader *loader)
{
    if (!loader->from_content)
    {
        free(loader->filename);
        free(loader->path);
    }

    G_OBJECT_CLASS(g_binary_loader_parent_class)->finalize(G_OBJECT(loader));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : content = contenu binaire chargé en mémoire.                 *
*                project  = projet dans lequel venir ajouter des contenus.    *
*                                                                             *
*  Description : Prépare le chargement non bloqué d'un contenu binaire.       *
*                                                                             *
*  Retour      : Instance de binaire chargé ou NULL en cas d'échec.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GBinaryLoader *g_binary_loader_new(GBinContent *content, GStudyProject *project)
{
    GBinaryLoader *result;            /* Tâche à retourner           */

    result = g_object_new(G_TYPE_BINARY_LOADER, NULL);

    result->from_content = true;

    result->content = content;
    g_object_ref(G_OBJECT(content));

    result->project = project;
    g_object_ref(G_OBJECT(project));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : filename = chemin d'accès au fichier à charger.              *
*                path     = chemin d'accès au noeud XML à lire.               *
*                project  = projet dans lequel venir rechercher les contenus. *
*                                                                             *
*  Description : Prépare le chargement non bloqué d'un contenu XML.           *
*                                                                             *
*  Retour      : Instance de binaire chargé ou NULL en cas d'échec.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GBinaryLoader *g_binary_loader_new_from_xml(const char *filename, const char *path, GStudyProject *project)
{
    GBinaryLoader *result;            /* Tâche à retourner           */

    result = g_object_new(G_TYPE_BINARY_LOADER, NULL);

    result->from_content = false;

    result->filename = strdup(filename);
    result->path = strdup(path);

    result->project = project;
    g_object_ref(G_OBJECT(project));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : loader = encadrement du chargement à mener.                  *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Réalise le chargement effectif d'un binaire.                 *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_binary_loader_process(GBinaryLoader *loader, GtkStatusStack *status)
{
    xmlDocPtr xdoc;                         /* Structure XML chargée       */
    xmlXPathContextPtr context;             /* Contexte pour les XPath     */
    GLoadedBinary *binary;                  /* Binaire désormais en place  */

    /* Tentative de chargement de binaire */

    if (loader->from_content)
        loader->binary = g_loaded_binary_new(loader->content);

    else
    {
        if (open_xml_file(loader->filename, &xdoc, &context))
        {
            loader->binary = g_loaded_binary_new_from_xml(context, loader->path, loader->project);

            close_xml_file(xdoc, context);

        }

    }

    /* Poursuites qui peuvent être traitées dans la foulée */

    if (loader->binary != NULL)
    {
        /* Si le contenu n'a pas déjà été ajouté au projet au moment du chargement de ce dernier... */
        if (loader->from_content)
            g_study_project_add_binary_content(loader->project, loader->content, PCS_ROOT/* FIXME : dstudy->state*/);

        binary = g_binary_loader_get_result(loader);

        if (binary != NULL)
        {
            g_signal_connect_to_main_swapped(binary, "disassembly-done",
                                             G_CALLBACK(g_study_project_attach_content), loader->project,
                                             g_cclosure_marshal_VOID__VOID);

            g_loaded_binary_analyse(binary);

            g_object_unref(G_OBJECT(binary));

        }

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : loader = encadrement du chargement à consulter.              *
*                                                                             *
*  Description : Retourne l'instance du binaire chargé en cas de succès.      *
*                                                                             *
*  Retour      : Instance mise en place ou NULL.                              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GLoadedBinary *g_binary_loader_get_result(const GBinaryLoader *loader)
{
    GLoadedBinary *result;                  /* Chargement à faire suivre   */

    result = loader->binary;

    if (result != NULL)
        g_object_ref(G_OBJECT(result));

    return result;

}