summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/plugin.c
blob: fb2044d791355f70d211e4450e8c6c9d8af4bcf9 (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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858

/* Chrysalide - Outil d'analyse de fichiers binaires
 * plugin.c - interactions avec un greffon Python
 *
 * Copyright (C) 2012-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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "plugin.h"


#include <assert.h>
#include <malloc.h>
#include <pygobject.h>
#include <string.h>


#include <common/extstr.h>
#include <plugins/plugin-int.h>


#include "access.h"
#include "helpers.h"
#include "pychrysa.h"



/* --------------------- INTERFACE INTERNE POUR GREFFONS PYTHON --------------------- */


/* Ligne de représentation de code binaire (instance) */
struct _GPythonPlugin
{
    GPluginModule parent;                   /* Instance parente            */

    PyObject *module;                       /* Script Python chargé        */
    PyObject *instance;                     /* Instance Python du greffon  */

};


/* Ligne de représentation de code binaire (classe) */
struct _GPythonPluginClass
{
    GPluginModuleClass parent;              /* Classe parente              */

};


/* Initialise la classe des greffons Python. */
static void g_python_plugin_class_init(GPythonPluginClass *);

/* Initialise l'instance d'un greffon Python. */
static void g_python_plugin_init(GPythonPlugin *);

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

/* Description : Procède à la libération totale de la mémoire. */
static void g_python_plugin_finalize(GPythonPlugin *);

/* Reconstruit la déclaration d'interface à partir de lectures. */
static bool g_python_plugin_read_interface(GPythonPlugin *);

/* Procède à l'initialisation du greffon. */
static bool g_python_plugin_do_init(GPythonPlugin *);

/* Procède à l'extinction du greffon. */
static bool g_python_plugin_do_exit(GPythonPlugin *);

/* Procède à une opération liée à un contenu binaire. */
static void g_python_plugin_handle_binary_content(const GPythonPlugin *, PluginAction, GBinContent *, wgroup_id_t, GtkStatusStack *);

/* Exécute une action pendant un désassemblage de binaire. */
static void g_python_plugin_process_disass(const GPythonPlugin *, PluginAction, GLoadedBinary *, GtkStatusStack *, GProcContext *);



/* ------------------------- MODULE PYTHON POUR LES SCRIPTS ------------------------- */


/* Définit les constantes pour les greffons en Python. */
static bool py_plugin_module_define_constants(PyTypeObject *);



/* ---------------------------------------------------------------------------------- */
/*                       INTERFACE INTERNE POUR GREFFONS PYTHON                       */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini par la GLib pour le greffon Python. */
G_DEFINE_TYPE(GPythonPlugin, g_python_plugin, G_TYPE_PLUGIN_MODULE);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des greffons Python.                    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_python_plugin_class_init(GPythonPluginClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_python_plugin_dispose;
    object->finalize = (GObjectFinalizeFunc)g_python_plugin_finalize;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise l'instance d'un greffon Python.                   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_python_plugin_init(GPythonPlugin *plugin)
{

}


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

static void g_python_plugin_dispose(GPythonPlugin *plugin)
{
    PyThreadState *tstate;                  /* Contexte d'environnement    */

    /**
     * Cf. https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock
     *
     * Cependant, comme on se trouve à priori dans le thread principal de l'interpréteur,
     * PyGILState_Ensure() ne pose aucun verrou. Ce qui aboutit à la situation suivante :
     *
     *    Fatal Python error: drop_gil: GIL is not locked
     *
     * On peut forcer les choses avec PyEval_AcquireLock(), mais cette fonction est marquée
     * comme dépréciée depuis Python 3.2.
     *
     * Donc on choisit les alternatives officielles.
     *
     * Cependant, PyThreadState_Get() renvoit l'erreur suivante :
     *
     *    Fatal Python error: PyThreadState_Get: no current thread
     *
     * Donc on se rabat sur une sauvegarde, qui n'est initialisée que lorsque l'interpréteur
     * est intégré dans l'éditeur.
     */

    tstate = get_pychrysalide_main_tstate();

    if (tstate != NULL)
        PyEval_RestoreThread(tstate);

    Py_XDECREF(plugin->instance);
    plugin->instance = NULL;

    if (tstate != NULL)
        PyEval_SaveThread();

    G_OBJECT_CLASS(g_python_plugin_parent_class)->dispose(G_OBJECT(plugin));

}


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

static void g_python_plugin_finalize(GPythonPlugin *plugin)
{
    plugin_interface *final;                /* Interface finale conservée  */

    Py_DECREF(plugin->module);

    final = (plugin_interface *)G_PLUGIN_MODULE(plugin)->interface;

    if (final != NULL)
    {
        assert(final->required_count == 1);

        free(final->required);
        free(final);

    }

    G_OBJECT_CLASS(g_python_plugin_parent_class)->finalize(G_OBJECT(plugin));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : modname  = nom du module à charger.                          *
*                filename = chemin d'accès au code Python à charger.          *
*                                                                             *
*  Description : Crée un greffon à partir de code Python.                     *
*                                                                             *
*  Retour      : Adresse de la structure mise en place ou NULL si erreur.     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GPluginModule *g_python_plugin_new(const char *modname, const char *filename)
{
    GPythonPlugin *result;                  /* Structure à retourner       */
    PyObject *name;                         /* Chemin d'accès pour Python  */
    PyObject *module;                       /* Script Python chargé        */
    PyObject *err_type;                     /* Type d'erreur Python        */
    PyObject *err_value;                    /* Instance Python d'erreur    */
    PyObject *err_traceback;                /* Trace Python associée       */
    PyObject *err_string;                   /* Description Python d'erreur */
    const char *err_msg;                    /* Représentation humaine      */
    PyObject *dict;                         /* Dictionnaire associé        */
    PyObject *class;                        /* Classe à instancier         */
    PyObject *instance;                     /* Instance Python du greffon  */
    size_t i;                               /* Boucle de parcours          */
    uint32_t action;                        /* Identifiant d'une action    */
    uint32_t category;                      /* Catégorie principale        */
    uint32_t sub;                           /* Sous-catégorie visée        */

    name = PyUnicode_FromString(modname);
    if (name == NULL) goto gppn_bad_exit;

    module = PyImport_Import(name);
    Py_DECREF(name);

    if (PyErr_Occurred())
    {
        PyErr_Fetch(&err_type, &err_value, &err_traceback);

        if (err_value == NULL)
            log_variadic_message(LMT_ERROR,
                                 _("An unknown error occured when importing '%s'..."), modname);
        else
        {
            err_string = PyObject_Str(err_value);
            err_msg = PyUnicode_AsUTF8(err_string);

            log_variadic_message(LMT_ERROR,
                                 _("An error occured when importing '%s': \"%s\""), modname, err_msg);

            Py_DECREF(err_string);
            Py_DECREF(err_value);

        }

        Py_XDECREF(err_traceback);
        Py_XDECREF(err_type);

        Py_XDECREF(module);

        module = NULL;

    }

    if (module == NULL) goto gppn_bad_exit;

    dict = PyModule_GetDict(module);
    class = PyDict_GetItemString(dict, "AutoLoad");

    if (class == NULL) goto gppn_no_class;
    if (!PyType_Check(class->ob_type)) goto gppn_no_class;

    instance = PyObject_CallFunction(class, NULL);
    if (instance == NULL) goto gppn_no_instance;

    result = g_object_new(G_TYPE_PYTHON_PLUGIN, NULL);

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

    result->module = module;
    result->instance = instance;

    if (!g_python_plugin_read_interface(result))
        goto gppn_interface_error;

    /* Localisation des différents points d'entrée déclarés */

#define register_python_binding(inst, sym, binding)                                 \
    ({                                                                              \
        bool __result;                                                              \
        if (!has_python_method(inst, #sym))                                         \
        {                                                                           \
            log_variadic_message(LMT_ERROR,                                         \
                                 _("No '%s' entry in plugin candidate '%s'"),       \
                                 #sym, G_PLUGIN_MODULE(result)->filename);          \
            __result = false;                                                       \
        }                                                                           \
        else                                                                        \
        {                                                                           \
            G_PLUGIN_MODULE(result)->sym = binding;                                 \
            __result = true;                                                        \
        }                                                                           \
        __result;                                                                   \
    })

    for (i = 0; i < G_PLUGIN_MODULE(result)->interface->actions_count; i++)
    {
        action = G_PLUGIN_MODULE(result)->interface->actions[i];
        category = MASK_PLUGIN_CATEGORY(action);
        sub = MASK_PLUGIN_SUB_CATEGORY(action);

        switch (category)
        {
            case DPC_BASIC:

                switch (sub)
                {
                    case DPS_NONE:
                        break;

                    case PGA_PLUGIN_INIT:
                        if (!register_python_binding(instance, init, (pg_management_fc)g_python_plugin_do_init))
                            goto gppn_bad_plugin;
                        break;

                    case PGA_PLUGIN_EXIT:
                        if (!register_python_binding(instance, exit, (pg_management_fc)g_python_plugin_do_exit))
                            goto gppn_bad_plugin;
                        break;

                    default:
                        log_variadic_message(LMT_WARNING,
                                             _("Unknown sub-category '0x%02x' in plugin '%s'..."), sub, filename);
                        break;

                }

                break;

            case DPC_BINARY_PROCESSING:

                switch (sub)
                {
                    case DPS_CONTENT:

                        switch (action)
                        {
                            case PGA_CONTENT_EXPLORER:
                            case PGA_CONTENT_RESOLVER:
                                if (!register_python_binding(instance, handle_content, \
                                                             (pg_handle_content_fc)g_python_plugin_handle_binary_content))
                                    goto gppn_bad_plugin;
                                break;

                            default:
                                log_variadic_message(LMT_WARNING,
                                                     _("Unknown action '0x%02x' in plugin '%s'..."),
                                                     action, filename);
                                break;

                        }

                        break;

                    case DPS_FORMAT:

                        switch (action)
                        {
                            default:
                                log_variadic_message(LMT_WARNING,
                                                     _("Unknown action '0x%02x' in plugin '%s'..."),
                                                     action, filename);
                                break;

                        }

                        break;

                    case DPS_DISASSEMBLY:
                        if (!register_python_binding(instance, process_disass,
                                                     (pg_process_disassembly_fc)g_python_plugin_process_disass))
                            goto gppn_bad_plugin;
                        break;

                    default:
                        log_variadic_message(LMT_WARNING,
                                             _("Unknown sub-category '0x%02x' in plugin '%s'..."), sub, filename);
                        break;

                }

                break;

            default:
                log_variadic_message(LMT_WARNING,
                                     _("Unknown category '0x%02x' in plugin '%s'..."), category, filename);
                break;

        }

    }

    /* Conclusion */

    return G_PLUGIN_MODULE(result);

 gppn_bad_plugin:

 gppn_interface_error:

    g_object_unref(G_OBJECT(result));

    return NULL;

 gppn_no_instance:

 gppn_no_class:

    Py_DECREF(module);

 gppn_bad_exit:

    return NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à initialiser.                              *
*                                                                             *
*  Description : Reconstruit la déclaration d'interface à partir de lectures. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_python_plugin_read_interface(GPythonPlugin *plugin)
{
    bool result;                            /* Bilan à renvoyer            */
    plugin_interface interface;             /* Recueil des éléments        */
    PyObject *desc;                         /* Tableau de description      */
    PyObject *str;                          /* Chaîne de caractères        */
    PyObject *tuple;                        /* Liste d'éléments à traiter  */
    Py_ssize_t count;                       /* Nombre d'éléments présents  */
    Py_ssize_t i;                           /* Boucle de parcours          */
    PyObject *action;                       /* Identifiant d'une action    */
    plugin_interface *final;                /* Interface finale conservée  */

    result = true;

    desc = run_python_method(plugin->instance, "get_interface", NULL);
    if (!PyDict_Check(desc))
    {
        result = false;
        goto pgpri_end;
    }

    memset(&interface, 0, sizeof(interface));

    /* Chargements des premières chaînes */

#define READ_STR_FIELD(name)                            \
    str = PyDict_GetItemString(desc, #name);            \
    if ((result = PyUnicode_Check(str)))                \
        interface.name = strdup(PyUnicode_DATA(str));   \

    READ_STR_FIELD(name);
    READ_STR_FIELD(desc);
    READ_STR_FIELD(version);

    /* Chargement des actions supportées */

    tuple = PyDict_GetItemString(desc, "actions");

    if (!PyList_Check(tuple))
    {
        result = false;
        goto pgpri_failed;
    }

    count = PyList_GET_SIZE(tuple);

    interface.actions = (plugin_action_t *)calloc(count, sizeof(plugin_action_t));
    interface.actions_count = count;

    for (i = 0; i < count; i++)
    {
        action = PyList_GET_ITEM(tuple, i);

        interface.actions[i] = PyLong_AsUnsignedLong(action);

    }

 pgpri_failed:

    if (result)
    {
        final = (plugin_interface *)calloc(1, sizeof(plugin_interface));

        memcpy(final, &interface, sizeof(interface));

        final->required = (const char **)malloc(sizeof(char *));
        final->required[0] = "PyChrysalide";
        final->required_count = 1;

        G_PLUGIN_MODULE(plugin)->interface = final;

    }
    else
    {
        if (interface.name != NULL) free((char *)interface.name);
        if (interface.desc != NULL) free((char *)interface.desc);
        if (interface.version != NULL) free((char *)interface.version);

        if (interface.actions != NULL) free(interface.actions);

    }

 pgpri_end:

    Py_XDECREF(desc);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à initialiser.                              *
*                ref    = espace de référencement global.                     *
*                                                                             *
*  Description : Procède à l'initialisation du greffon.                       *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_python_plugin_do_init(GPythonPlugin *plugin)
{
    bool result;                            /* Bilan à retourner           */
    PyObject *value;                        /* Valeur obtenue              */

    if (!has_python_method(plugin->instance, "init"))
        result = true;

    else
    {
        value = run_python_method(plugin->instance, "init", NULL);

        result = (value != NULL && PyObject_IsTrue(value));

        Py_XDECREF(value);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à initialiser.                              *
*                                                                             *
*  Description : Procède à l'extinction du greffon.                           *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_python_plugin_do_exit(GPythonPlugin *plugin)
{
    bool result;                            /* Bilan à retourner           */
    PyObject *value;                        /* Valeur obtenue              */

    if (!has_python_method(plugin->instance, "exit"))
        result = true;

    else
    {
        value = run_python_method(plugin->instance, "exit", NULL);

        result = (value != NULL && PyObject_IsTrue(value));

        Py_XDECREF(value);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin  = greffon à manipuler.                               *
*                action  = type d'action attendue.                            *
*                content = contenu binaire à traiter.                         *
*                wid     = identifiant du groupe de traitement.               *
*                status  = barre de statut à tenir informée.                  *
*                                                                             *
*  Description : Procède à une opération liée à un contenu binaire.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_python_plugin_handle_binary_content(const GPythonPlugin *plugin, PluginAction action, GBinContent *content, wgroup_id_t wid, GtkStatusStack *status)
{
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *value;                        /* Valeurs obtenues            */
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */

    gstate = PyGILState_Ensure();

    args = PyTuple_New(4);

    PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));
    PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(content)));
    PyTuple_SetItem(args, 2, PyLong_FromUnsignedLong(wid));
    PyTuple_SetItem(args, 3, pygobject_new(G_OBJECT(status)));

    value = run_python_method(plugin->instance, "handle_content", args);

    Py_XDECREF(value);
    Py_DECREF(args);

    PyGILState_Release(gstate);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin  = greffon à manipuler.                               *
*                action  = type d'action attendue.                            *
*                binary  = binaire dont le contenu est en cours de traitement.*
*                status  = barre de statut à tenir informée.                  *
*                context = contexte de désassemblage.                         *
*                                                                             *
*  Description : Exécute une action pendant un désassemblage de binaire.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_python_plugin_process_disass(const GPythonPlugin *plugin, PluginAction action, GLoadedBinary *binary, GtkStatusStack *status, GProcContext *context)
{
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *value;                        /* Valeurs obtenues            */

    args = PyTuple_New(4);

    PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));
    PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(binary)));
    PyTuple_SetItem(args, 2, pygobject_new(G_OBJECT(status)));
    PyTuple_SetItem(args, 3, pygobject_new(G_OBJECT(context)));

    value = run_python_method(plugin->instance, "process_binary_disassembly", args);

    Py_XDECREF(value);
    Py_DECREF(args);

}



/* ---------------------------------------------------------------------------------- */
/*                           MODULE PYTHON POUR LES SCRIPTS                           */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
*                                                                             *
*  Description : Définit les constantes pour les greffons en Python.          *
*                                                                             *
*  Retour      : true en cas de succès de l'opération, false sinon.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool py_plugin_module_define_constants(PyTypeObject *obj_type)
{
    bool result;                            /* Bilan à retourner           */

    result = true;

    result &= PyDict_AddIntMacro(obj_type, PGA_BASIC_NONE);

    result &= PyDict_AddIntMacro(obj_type, PGA_PLUGIN_INIT);
    result &= PyDict_AddIntMacro(obj_type, PGA_PLUGIN_EXIT);

    result &= PyDict_AddIntMacro(obj_type, PGA_CONTENT_EXPLORER);
    result &= PyDict_AddIntMacro(obj_type, PGA_CONTENT_RESOLVER);
    result &= PyDict_AddIntMacro(obj_type, PGA_CONTENT_ANALYZED);

    result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_ANALYSIS_STARTED);
    result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_PRELOAD);
    result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_ATTACH_DEBUG);
    result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_ANALYSIS_ENDED);
    result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_POST_ANALYSIS_STARTED);
    result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_POST_ANALYSIS_ENDED);

    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_STARTED);
    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_RAW);
    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_HOOKED_LINK);
    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_HOOKED_POST);
    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_LIMITED);
    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_LOOPS);
    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_LINKED);
    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_GROUPED);
    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_RANKED);
    result &= PyDict_AddIntMacro(obj_type, PGA_DISASSEMBLY_ENDED);

    result &= PyDict_AddIntMacro(obj_type, PGA_DETECTION_OBFUSCATORS);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Fournit un accès à une définition de type à diffuser.        *
*                                                                             *
*  Retour      : Définition d'objet pour Python.                              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

PyTypeObject *get_python_plugin_module_type(void)
{
    static PyMethodDef py_plugin_module_methods[] = {
        { NULL }
    };

    static PyGetSetDef py_plugin_module_getseters[] = {
        { NULL }
    };

    static PyTypeObject py_plugin_module_type = {

        PyVarObject_HEAD_INIT(NULL, 0)

        .tp_name        = "pychrysalide.PluginModule",
        .tp_basicsize   = sizeof(PyGObject),

        .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,

        .tp_doc         = "Chrysalide plugin for Python.",

        .tp_methods     = py_plugin_module_methods,
        .tp_getset      = py_plugin_module_getseters

    };

    return &py_plugin_module_type;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Prend en charge l'objet 'pychrysalide.PluginModule'.         *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool ensure_python_plugin_module_is_registered(void)
{
    PyTypeObject *type;                     /* Type Python 'PluginModule'  */
    PyObject *module;                       /* Module à recompléter        */
    PyObject *dict;                         /* Dictionnaire du module      */

    type = get_python_plugin_module_type();

    if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
    {
        module = get_access_to_python_module("pychrysalide");

        dict = PyModule_GetDict(module);

        if (!register_class_for_pygobject(dict, G_TYPE_PLUGIN_MODULE, type, &PyGObject_Type))
            return false;

        if (!py_plugin_module_define_constants(type))
            return false;

    }

    return true;

}