summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/plugin.c
blob: 495cecfa05e2a5bfb0e74073c972432dc0e18961 (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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727

/* Chrysalide - Outil d'analyse de fichiers binaires
 * plugin.c - interactions avec un greffon Python
 *
 * Copyright (C) 2018-2019 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 <libgen.h>
#include <malloc.h>
#include <pygobject.h>
#include <string.h>


#include <common/extstr.h>
#include <plugins/dt.h>
#include <plugins/self.h>


#include "access.h"
#include "constants.h"
#include "core.h"
#include "helpers.h"
#include "core/constants.h"



/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */


/* Accompagne la création d'une instance dérivée en Python. */
static PyObject *py_plugin_module_new(PyTypeObject *, PyObject *, PyObject *);

/* Initialise la classe des greffons d'extension. */
static void py_plugin_module_init_gclass(GPluginModuleClass *, gpointer);

/* Initialise une instance sur la base du dérivé de GObject. */
static int py_plugin_module_init(PyObject *self, PyObject *args, PyObject *kwds);

/* Accompagne la fin du chargement des modules natifs. */
static void py_plugin_module_notify_plugins_loaded_wrapper(GPluginModule *, PluginAction);

/* Complète une liste de resources pour thème. */
static void py_plugin_module_include_theme_wrapper(const GPluginModule *, PluginAction, gboolean, char ***, size_t *);

/* Rend compte de la création d'un panneau. */
static void py_plugin_module_notify_panel_creation_wrapper(const GPluginModule *, PluginAction, GPanelItem *);

/* Rend compte d'un affichage ou d'un retrait de panneau. */
static void py_plugin_module_notify_panel_docking_wrapper(const GPluginModule *, PluginAction, GPanelItem *, bool);

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

/* Procède à une opération liée à un contenu chargé. */
static void py_plugin_module_handle_loaded_content_wrapper(const GPluginModule *, PluginAction, GLoadedContent *, wgroup_id_t, GtkStatusStack *);

/* Procède à une opération liée à l'analyse d'un format. */
static bool py_plugin_module_handle_known_format_analysis_wrapper(const GPluginModule *, PluginAction, GKnownFormat *, wgroup_id_t, GtkStatusStack *);

/* Procède à un préchargement de format de fichier. */
static bool py_plugin_module_preload_binary_format_wrapper(const GPluginModule *, PluginAction, GBinFormat *, GPreloadInfo *, GtkStatusStack *);

/* Procède au rattachement d'éventuelles infos de débogage. */
static void py_plugin_module_attach_debug_format_wrapper(const GPluginModule *, PluginAction, GExeFormat *);

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

/* Effectue la détection d'effets d'outils externes. */
static void py_plugin_module_detect_external_tools_wrapper(const GPluginModule *, PluginAction, const GLoadedContent *, bool, char ***, size_t *);



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


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

};


/* 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 *);

/* Fournit le nom brut associé au greffon. */
static char *g_python_plugin_get_modname(const GPythonPlugin *);



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


/* Construit le nom d'un fichier de configuration du greffon. */
static PyObject *py_plugin_module_build_config_filename(PyObject *, PyObject *);

/* Affiche un message dans le journal des messages système. */
static PyObject *py_plugin_module_log_message(PyObject *, PyObject *);

/* Fournit le nom brut associé au greffon. */
static PyObject *py_plugin_module_get_modname(PyObject *, void *);

/* Indique le fichier contenant le greffon manipulé. */
static PyObject *py_plugin_module_get_filename(PyObject *, void *);



/* ---------------------------------------------------------------------------------- */
/*                          GLUE POUR CREATION DEPUIS PYTHON                          */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : type = type du nouvel objet à mettre en place.               *
*                args = éventuelle liste d'arguments.                         *
*                kwds = éventuel dictionnaire de valeurs mises à disposition. *
*                                                                             *
*  Description : Accompagne la création d'une instance dérivée en Python.     *
*                                                                             *
*  Retour      : Nouvel objet Python mis en place ou NULL en cas d'échec.     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_plugin_module_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyObject *result;                       /* Objet à retourner           */
    PyTypeObject *base;                     /* Type de base à dériver      */
    bool first_time;                        /* Evite les multiples passages*/
    GType gtype;                            /* Nouveau type de processeur  */
    bool status;                            /* Bilan d'un enregistrement   */

    /* Validations diverses */

    base = get_python_plugin_module_type();

    if (type == base)
    {
        result = NULL;
        PyErr_Format(PyExc_RuntimeError, _("%s is an abstract class"), type->tp_name);
        goto exit;
    }

    /* Mise en place d'un type dédié */

    first_time = (g_type_from_name(type->tp_name) == 0);

    gtype = build_dynamic_type(G_TYPE_PYTHON_PLUGIN, type->tp_name,
                               (GClassInitFunc)py_plugin_module_init_gclass, NULL, NULL);

    if (first_time)
    {
        status = register_class_for_dynamic_pygobject(gtype, type, base);

        if (!status)
        {
            result = NULL;
            goto exit;
        }

    }

    /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */

    result = PyType_GenericNew(type, args, kwds);

 exit:

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : class  = classe à initialiser.                               *
*                unused = données non utilisées ici.                          *
*                                                                             *
*  Description : Initialise la classe des greffons d'extension.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void py_plugin_module_init_gclass(GPluginModuleClass *class, gpointer unused)
{
    class->init = NULL;
    class->exit = NULL;

    class->plugins_loaded = py_plugin_module_notify_plugins_loaded_wrapper;

    class->include_theme = py_plugin_module_include_theme_wrapper;
    class->notify_panel = py_plugin_module_notify_panel_creation_wrapper;
    class->notify_docking = py_plugin_module_notify_panel_docking_wrapper;

    class->handle_content = py_plugin_module_handle_binary_content_wrapper;
    class->handle_loaded = py_plugin_module_handle_loaded_content_wrapper;

    class->handle_fmt_analysis = py_plugin_module_handle_known_format_analysis_wrapper;
    class->preload_format = py_plugin_module_preload_binary_format_wrapper;
    class->attach_debug = py_plugin_module_attach_debug_format_wrapper;

    class->process_disass = py_plugin_module_process_disassembly_event_wrapper;

    class->detect = py_plugin_module_detect_external_tools_wrapper;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = objet à initialiser (théoriquement).                  *
*                args = arguments fournis à l'appel.                          *
*                kwds = arguments de type key=val fournis.                    *
*                                                                             *
*  Description : Initialise une instance sur la base du dérivé de GObject.    *
*                                                                             *
*  Retour      : 0.                                                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static int py_plugin_module_init(PyObject *self, PyObject *args, PyObject *kwds)
{
    int ret;                                /* Bilan d'un appel            */
    GPluginModule *plugin;                  /* Greffon à manipuler         */
    plugin_interface *iface;                /* Interface à constituer      */
    PyObject *value;                        /* Valeur à présence imposée   */
    size_t i;                               /* Boucle de parcours          */
    PyObject *action;                       /* Identifiant d'une action    */

#define PLUGIN_MODULE_DOC                                                   \
    "PythonModule is the class allowing the creation of Chrysalide plugins" \
    " for Python."                                                          \
    "\n"                                                                    \
    "Calls to the *__init__* constructor of this abstract object expect"    \
    " no particular argument.\n"                                            \
    "\n"                                                                    \
    "Several items have to be defined as class attributes in the final"     \
    " class:\n"                                                             \
    "* *_name*: a string providing a small name for the plugin;\n"          \
    "* *_desc*: a string for a human readable description of the plugin;\n" \
    "* *_version*: a string providing the version of the plugin;\n"         \
    "* *_url*: a string for the homepage describing the plugin;\n"          \
    "* *_actions*: a tuple of pychrysalide.PluginModule.PluginAction"       \
    " defining the features the plugin is bringing; this list can be"       \
    " empty.\n"                                                             \
    "\n"                                                                    \
    "Depending on the implemented actions, some of the following methods"   \
    " have to be defined for new classes:\n"                                \
    "* pychrysalide.PluginModule._notify_plugins_loaded();\n"               \
    "* pychrysalide.PluginModule._include_theme();\n"                       \
    "* pychrysalide.PluginModule._on_panel_creation;\n"                     \
    "* pychrysalide.PluginModule._on_panel_docking();\n"                    \
    "* pychrysalide.PluginModule._handle_binary_content();\n"               \
    "* pychrysalide.PluginModule._handle_loaded_content();\n"               \
    "* pychrysalide.PluginModule._handle_format_analysis();\n"              \
    "* pychrysalide.PluginModule._preload_format();\n"                      \
    "* pychrysalide.PluginModule._attach_debug_format();\n"                 \
    "* pychrysalide.PluginModule._process_disassembly_event();\n"           \
    "* pychrysalide.PluginModule._detect_external_tools()."

    /* Initialisation d'un objet GLib */

    ret = forward_pygobjet_init(self);
    if (ret == -1) return -1;

    /* Eléments de base */

    plugin = G_PLUGIN_MODULE(pygobject_get(self));

    iface = calloc(1, sizeof(plugin_interface));
    plugin->interface = iface;

#define LOAD_PYTHON_IFACE(attr)                                                                         \
    do                                                                                                  \
    {                                                                                                   \
        value = PyObject_GetAttrString(self, "_" #attr);                                                \
        if (value == NULL)                                                                              \
        {                                                                                               \
            PyErr_SetString(PyExc_TypeError, _("A '_" #attr "' class attributes is missing."));         \
            return -1;                                                                                  \
        }                                                                                               \
        if (PyUnicode_Check(value))                                                                     \
        {                                                                                               \
            iface->attr = strdup(PyUnicode_AsUTF8(value));                                              \
            Py_DECREF(value);                                                                           \
        }                                                                                               \
        else                                                                                            \
        {                                                                                               \
            Py_DECREF(value);                                                                           \
            PyErr_SetString(PyExc_TypeError, _("The '_" #attr "' class attributes must be a string.")); \
            return -1;                                                                                  \
        }                                                                                               \
        assert(iface->attr != NULL);                                                                    \
    }                                                                                                   \
    while (0);

    LOAD_PYTHON_IFACE(name);
    LOAD_PYTHON_IFACE(desc);
    LOAD_PYTHON_IFACE(version);
    LOAD_PYTHON_IFACE(url);

    iface->container = false;

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

    value = PyObject_GetAttrString(self, "_actions");

    if (value == NULL)
    {
        PyErr_SetString(PyExc_TypeError, _("An '_actions' class attributes is missing."));
        return -1;
    }

    if (!PyTuple_Check(value))
    {
        Py_DECREF(value);
        PyErr_SetString(PyExc_TypeError, _("The '_actions' class attributes must be a tuple."));
        return -1;
    }

    iface->actions_count = PyTuple_Size(value);
    iface->actions = malloc(iface->actions_count * sizeof(plugin_action_t));

    for (i = 0; i < iface->actions_count; i++)
    {
        action = PyTuple_GetItem(value, i);

        if (!PyLong_Check(action))
        {
            Py_DECREF(value);
            PyErr_SetString(PyExc_TypeError, _("invalid type for plugin action."));
            return -1;
        }

        iface->actions[i] = PyLong_AsUnsignedLong(action);

    }

    Py_DECREF(value);

    return 0;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à manipuler.                                *
*                action = type d'action attendue.                             *
*                unused = variable non utilisé pour l'usage de __VA_ARGS__.   *
*                                                                             *
*  Description : Accompagne la fin du chargement des modules natifs.          *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void py_plugin_module_notify_plugins_loaded_wrapper(GPluginModule *plugin, PluginAction action)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */

#define PLUGIN_MODULE_NOTIFY_PLUGINS_LOADED_WRAPPER PYTHON_WRAPPER_DEF  \
(                                                                       \
    _notify_plugins_loaded, "$self, action, /",                         \
    METH_VARARGS,                                                       \
    "Abstract method called once all the (native?) plugins are"         \
    " loaded.\n"                                                        \
    "\n"                                                                \
    "The expected action is a pychrysalide.PluginModule.PluginAction"   \
    " value.\n"                                                         \
    "\n"                                                                \
    "This method has to be defined in order to handle actions such as"  \
    " *NATIVE_PLUGINS_LOADED* or *PLUGINS_LOADED*."                     \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_notify_plugins_loaded"))
    {
        args = PyTuple_New(1);

        PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));

        pyret = run_python_method(pyobj, "_notify_plugins_loaded", args);

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin    = greffon à manipuler.                             *
*                action    = type d'action attendue.                          *
*                dark      = indique une préférence pour la variante foncée.  *
*                resources = liste de ressources à constituer. [OUT]          *
*                count     = taille de cette liste. [OUT]                     *
*                                                                             *
*  Description : Complète une liste de resources pour thème.                  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void py_plugin_module_include_theme_wrapper(const GPluginModule *plugin, PluginAction action, gboolean dark, char ***resources, size_t *count)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *darkness;                     /* Valeur booléenne à joindre  */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */
    Py_ssize_t length;                      /* Nombre d'éléments collectés */
    Py_ssize_t i;                           /* Boucle de parcours          */
    PyObject *res;                          /* Ressource à ajouter         */

#define PLUGIN_MODULE_INCLUDE_THEME_WRAPPER PYTHON_WRAPPER_DEF          \
(                                                                       \
    _include_theme, "$self, action, dark, /",                           \
    METH_VARARGS,                                                       \
    "Abstract method called once all the native plugins are loaded.\n"  \
    "\n"                                                                \
    "The expected action is a pychrysalide.PluginModule.PluginAction"   \
    " value and the *dark* parameter indicates if a dark theme is"      \
    " being to get loaded.\n"                                           \
    "\n"                                                                \
    "The expected result is a list of CSS definition resource URIs,"    \
    " provided as strings such as 'resource:///org/xxx/extra.css'"      \
    " for instance.\n"                                                  \
    "\n"                                                                \
    "This method has to be defined in order to handle action such as"   \
    " *GUI_THEME*."                                                     \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_include_theme"))
    {
        args = PyTuple_New(2);

        darkness = (dark ? Py_True : Py_False);
        Py_INCREF(darkness);

        PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));
        PyTuple_SetItem(args, 1, darkness);

        pyret = run_python_method(pyobj, "_include_theme", args);

        if (!PySequence_Check(pyret))
            g_plugin_module_log_simple_message(plugin, LMT_ERROR, _("The returned value must be a string list"));

        else
        {
            length = PySequence_Length(pyret);

            for (i = 0; i < length; i++)
            {
                res = PySequence_GetItem(pyret, i);

                if (!PyUnicode_Check(res))
                    g_plugin_module_log_variadic_message(plugin, LMT_ERROR,
                                                         _("The returned #%zd value must be a string"));

                else
                {
                    *resources = realloc(*resources, ++(*count) * sizeof(char **));
                    *resources[*count - 1] = strdup(PyUnicode_DATA(res));
                }

                Py_DECREF(res);

            }

        }

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à manipuler.                                *
*                action = type d'action attendue.                             *
*                item   = nouveau panneau créé.                               *
*                                                                             *
*  Description : Rend compte de la création d'un panneau.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void py_plugin_module_notify_panel_creation_wrapper(const GPluginModule *plugin, PluginAction action, GPanelItem *item)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */

#define PLUGIN_MODULE_ON_PANEL_CREATION_WRAPPER PYTHON_WRAPPER_DEF      \
(                                                                       \
    _on_panel_creation, "$self, action, item, /",                       \
    METH_VARARGS,                                                       \
    "Abstract method called when a new instance of panel is created.\n" \
    "\n"                                                                \
    "The expected *action* is a pychrysalide.PluginModule.PluginAction" \
    " value and the *item* is a pychrysalide.gui.PanelItem instance.\n" \
    "\n"                                                                \
    "This method has to be defined in order to handle action such as"   \
    " *PANEL_CREATION*."                                                \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_on_panel_creation"))
    {
        args = PyTuple_New(2);

        PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));
        PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(item)));

        pyret = run_python_method(pyobj, "_on_panel_creation", args);

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à manipuler.                                *
*                action = type d'action attendue.                             *
*                item   = panneau marqué par un changement d'affichage.       *
*                dock   = indique une accroche et non un décrochage.          *
*                                                                             *
*  Description : Rend compte d'un affichage ou d'un retrait de panneau.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void py_plugin_module_notify_panel_docking_wrapper(const GPluginModule *plugin, PluginAction action, GPanelItem *item, bool dock)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *pydock;                       /* Valeur booléenne à joindre  */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */

#define PLUGIN_MODULE_ON_PANEL_DOCKING_WRAPPER PYTHON_WRAPPER_DEF       \
(                                                                       \
    _on_panel_docking, "$self, action, item, dock, /",                  \
    METH_VARARGS,                                                       \
    "Abstract method called when a panel is docked or undocked into"    \
    " the Chrysalide main graphical interface.\n"                       \
    "\n"                                                                \
    "The expected *action* is a pychrysalide.PluginModule.PluginAction" \
    " value, the *item* is a pychrysalide.gui.PanelItem instance and"   \
    " the *dock* parameter indicates if the panel request a docking"    \
    " operation or an undocking one.\n"                                 \
    "\n"                                                                \
    "This method has to be defined in order to handle action such as"   \
    " *PANEL_DOCKING*."                                                 \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_on_panel_docking"))
    {
        args = PyTuple_New(3);

        pydock = (dock ? Py_True : Py_False);
        Py_INCREF(pydock);

        PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));
        PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(item)));
        PyTuple_SetItem(args, 2, pydock);

        pyret = run_python_method(pyobj, "_on_panel_docking", args);

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

}


/******************************************************************************
*                                                                             *
*  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 py_plugin_module_handle_binary_content_wrapper(const GPluginModule *plugin, PluginAction action, GBinContent *content, wgroup_id_t wid, GtkStatusStack *status)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */

#define PLUGIN_MODULE_HANDLE_BINARY_CONTENT_WRAPPER PYTHON_WRAPPER_DEF              \
(                                                                                   \
    _handle_binary_content, "$self, action, content, wid, status, /",               \
    METH_VARARGS,                                                                   \
    "Abstract method used to explore a binary content (and possibly to add new"     \
    " contents to explore) or to load a recognized binary content into a"           \
    " pychrysalide.analysis.LoadedContent instance.\n"                              \
    "\n"                                                                            \
    "The expected action is a pychrysalide.PluginModule.PluginAction"               \
    " value and the initial binary content is a pychrysalide.analysis.BinContent"   \
    " instance. A tracking identifier is provided and is aimed to be"               \
    " used with methods from pychrysalide.analysis.ContentExplorer and"             \
    " pychrysalide.analysis.ContentResolver. A reference to the main status bar"    \
    " may also be provided, as a pychrysalide.gtkext.StatusStack instance if"       \
    " running in graphical mode or None otherwise.\n"                               \
    "\n"                                                                            \
    "This method has to be defined in order to handle actions such as"              \
    " *CONTENT_EXPLORER* or *CONTENT_RESOLVER*."                                    \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_handle_binary_content"))
    {
        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)));

        pyret = run_python_method(pyobj, "_handle_binary_content", args);

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

}


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

static void py_plugin_module_handle_loaded_content_wrapper(const GPluginModule *plugin, PluginAction action, GLoadedContent *content, wgroup_id_t gid, GtkStatusStack *status)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */

#define PLUGIN_MODULE_HANDLE_LOADED_CONTENT_WRAPPER PYTHON_WRAPPER_DEF              \
(                                                                                   \
    _handle_loaded_content, "$self, action, content, gid, status, /",               \
    METH_VARARGS,                                                                   \
    "Abstract method run once a loaded binary has been analyzed with success.\n"    \
    "\n"                                                                            \
    "The expected action is a pychrysalide.PluginModule.PluginAction"               \
    " value and the analyzed content is a pychrysalide.analysis.LoadedContent"      \
    " instance. The identifier refers to the working queue used to process the"     \
    " analysis. A reference to the main status bar may also be provided, as a"      \
    " pychrysalide.gtkext.StatusStack instance if running in graphical mode or"     \
    " None otherwise.\n"                                                            \
    "\n"                                                                            \
    "This method has to be defined in order to handle action such as"               \
    " *CONTENT_ANALYZED*."                                                          \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_handle_loaded_content"))
    {
        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(gid));
        PyTuple_SetItem(args, 3, pygobject_new(G_OBJECT(status)));

        pyret = run_python_method(pyobj, "_handle_loaded_content", args);

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à manipuler.                                *
*                action = type d'action attendue.                             *
*                format = format de binaire à manipuler pendant l'opération.  *
*                gid    = groupe de travail dédié.                            *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Procède à une opération liée à l'analyse d'un format.        *
*                                                                             *
*  Retour      : Bilan de l'exécution du traitement.                          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool py_plugin_module_handle_known_format_analysis_wrapper(const GPluginModule *plugin, PluginAction action, GKnownFormat *format, wgroup_id_t gid, GtkStatusStack *status)
{
    bool result;                            /* Bilan à retourner           */
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */

#define PLUGIN_MODULE_HANDLE_BINARY_FORMAT_ANALYSIS_WRAPPER PYTHON_WRAPPER_DEF      \
(                                                                                   \
    _handle_binary_format_analysis, "$self, action, format, gid, status, /",        \
    METH_VARARGS,                                                                   \
    "Abstract method run at several different steps of a binary format analysis:\n" \
    "* at the beginning and at the end of the main analysis pass;\n"                \
    "* at the beginning and at the end of the extra final pass.\n"                  \
    "\n"                                                                            \
    "The expected action is a pychrysalide.PluginModule.PluginAction"               \
    " value and the provided format is a pychrysalide.format.KnownFormat"           \
    " instance. The identifier refers to the working queue used to process the"     \
    " analysis. A reference to the main status bar may also be provided, as a"      \
    " pychrysalide.gtkext.StatusStack instance if running in graphical mode or"     \
    " None otherwise.\n"                                                            \
    "\n"                                                                            \
    "This method has to be defined in order to handle actions such as"              \
    " *FORMAT_ANALYSIS_STARTED*, *FORMAT_ANALYSIS_ENDED*,"                          \
    " *FORMAT_POST_ANALYSIS_STARTED* or *FORMAT_POST_ANALYSIS_ENDED*."              \
)

    result = false;

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_handle_format_analysis"))
    {
        args = PyTuple_New(4);

        PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));
        PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(format)));
        PyTuple_SetItem(args, 2, PyLong_FromUnsignedLong(gid));
        PyTuple_SetItem(args, 3, pygobject_new(G_OBJECT(status)));

        pyret = run_python_method(pyobj, "_handle_format_analysis", args);

        result = (pyret == Py_True);

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à manipuler.                                *
*                action = type d'action attendue.                             *
*                format = format de binaire à manipuler pendant l'opération.  *
*                info   = informations à constituer en avance de phase.       *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Procède à un préchargement de format de fichier.             *
*                                                                             *
*  Retour      : Bilan de l'exécution du traitement.                          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool py_plugin_module_preload_binary_format_wrapper(const GPluginModule *plugin, PluginAction action, GBinFormat *format, GPreloadInfo *info, GtkStatusStack *status)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */

#define PLUGIN_MODULE_PRELOAD_BINARY_FORMAT_WRAPPER PYTHON_WRAPPER_DEF              \
(                                                                                   \
    _preload_binary_format, "$self, action, format, info, status, /",               \
    METH_VARARGS,                                                                   \
    "Abstract method which is an opportunity to setup instructions or comments"     \
    " ahead of the disassembling process.\n"                                        \
    "\n"                                                                            \
    "Format fields do not need to get disassembled and may be annotated for"        \
    " instance.\n"                                                                  \
    "\n"                                                                            \
    "The expected action is a pychrysalide.PluginModule.PluginAction"               \
    " value and the provided format is a pychrysalide.format.BinFormat"             \
    " instance. The information holder to fill is a pychrysalide.format.PreloadInfo"\
    " instance. A reference to the main status bar may also be provided, as a"      \
    " pychrysalide.gtkext.StatusStack instance if running in graphical mode or"     \
    " None otherwise.\n"                                                            \
    "\n"                                                                            \
    "This method has to be defined in order to handle action such as"               \
    " *FORMAT_PRELOAD*."                                                            \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_preload_format"))
    {
        args = PyTuple_New(4);

        PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));
        PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(format)));
        PyTuple_SetItem(args, 2, pygobject_new(G_OBJECT(info)));
        PyTuple_SetItem(args, 3, pygobject_new(G_OBJECT(status)));

        pyret = run_python_method(pyobj, "_preload_format", args);

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à manipuler.                                *
*                action = type d'action attendue.                             *
*                format = format de binaire à manipuler pendant l'opération.  *
*                                                                             *
*  Description : Procède au rattachement d'éventuelles infos de débogage.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void py_plugin_module_attach_debug_format_wrapper(const GPluginModule *plugin, PluginAction action, GExeFormat *format)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */

#define PLUGIN_MODULE_ATTACH_DEBUG_FORMAT_WRAPPER PYTHON_WRAPPER_DEF                \
(                                                                                   \
    _attach_debug_format, "$self, action, format, /",                               \
    METH_VARARGS,                                                                   \
    "Abstract method called when a debugger is attached to a binary format.\n"      \
    "\n"                                                                            \
    "The expected action is a pychrysalide.PluginModule.PluginAction"               \
    " value and the provided format is a pychrysalide.format.ExeFormat instance.\n" \
    "\n"                                                                            \
    "This method has to be defined in order to handle action such as"               \
    " *FORMAT_ATTACH_DEBUG*."                                                       \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_attach_debug_format"))
    {
        args = PyTuple_New(2);

        PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));
        PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(format)));

        pyret = run_python_method(pyobj, "_attach_debug_format", args);

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    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 py_plugin_module_process_disassembly_event_wrapper(const GPluginModule *plugin, PluginAction action, GLoadedBinary *binary, GtkStatusStack *status, GProcContext *context)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */

#define PLUGIN_MODULE_PROCESS_DISASSEMBLY_EVENT_WRAPPER PYTHON_WRAPPER_DEF          \
(                                                                                   \
    _process_disassembly_event, "$self, action, format, /",                         \
    METH_VARARGS,                                                                   \
    "Abstract method run at several different steps of a binary analysis.\n"        \
    "\n"                                                                            \
    "The expected action is a pychrysalide.PluginModule.PluginAction"               \
    " value and the provided format is a pychrysalide.format.ExeFormat instance.\n" \
    "\n"                                                                            \
    "This method has to be defined in order to handle actions such as"              \
    " *DISASSEMBLY_STARTED*, *DISASSEMBLY_RAW*, *DISASSEMBLY_HOOKED_LINK*,"         \
    " *DISASSEMBLY_HOOKED_POST*, *DISASSEMBLY_LIMITED*, *DISASSEMBLY_LOOPS*,"       \
    " *DISASSEMBLY_LINKED*, *DISASSEMBLY_GROUPED*, *DISASSEMBLY_RANKED*,"           \
    " *DISASSEMBLY_ENDED*."                                                         \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_process_disassembly_event"))
    {
        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)));

        pyret = run_python_method(pyobj, "_process_disassembly_event", args);

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin  = greffon à manipuler.                               *
*                action  = type d'action attendue.                            *
*                content = élément chargé à consulter.                        *
*                version = précise si les versions doivent être recherchées.  *
*                names   = désignations humaines correspondantes, à libérer.  *
*                count   = nombre de types d'obscurcissement trouvés. [OUT]   *
*                                                                             *
*  Description : Effectue la détection d'effets d'outils externes.            *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void py_plugin_module_detect_external_tools_wrapper(const GPluginModule *plugin, PluginAction action, const GLoadedContent *content, bool version, char ***names, size_t *count)
{
    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
    PyObject *pyobj;                        /* Objet Python concerné       */
    PyObject *details;                      /* Valeur booléenne à joindre  */
    PyObject *args;                         /* Arguments pour l'appel      */
    PyObject *pyret;                        /* Bilan d'exécution           */
    Py_ssize_t length;                      /* Nombre d'éléments collectés */
    Py_ssize_t i;                           /* Boucle de parcours          */
    PyObject *res;                          /* Ressource à ajouter         */

#define PLUGIN_MODULE_DETECT_EXTERNAL_TOOLS_WRAPPER PYTHON_WRAPPER_DEF  \
(                                                                       \
    _detect_external_tools, "$self, action, content, version, /",       \
    METH_VARARGS,                                                       \
    "Abstract method called when a detection of tools used the build"   \
    " the analyzed content is required.\n"                              \
    "\n"                                                                \
    "The expected action is a pychrysalide.PluginModule.PluginAction"   \
    " value and the content is a pychrysalide.analysis.LoadedContent"   \
    " instance. The *version* parameter is a boolean value indicating"  \
    " if some extra details about the tools version are wished.\n"      \
    "\n"                                                                \
    "The expected result is a list of strings.\n"                       \
    "\n"                                                                \
    "This method has to be defined in order to handle action such as"   \
    " *DETECTION_OBFUSCATORS*."                                         \
)

    gstate = PyGILState_Ensure();

    pyobj = pygobject_new(G_OBJECT(plugin));

    if (has_python_method(pyobj, "_detect_external_tools"))
    {
        args = PyTuple_New(3);

        details = (version ? Py_True : Py_False);
        Py_INCREF(details);

        PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(action));
        PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(content)));
        PyTuple_SetItem(args, 2, details);

        pyret = run_python_method(pyobj, "_detect_external_tools", args);

        if (!PySequence_Check(pyret))
            g_plugin_module_log_simple_message(plugin, LMT_ERROR, _("The returned value must be a string list"));

        else
        {
            length = PySequence_Length(pyret);

            for (i = 0; i < length; i++)
            {
                res = PySequence_GetItem(pyret, i);

                if (!PyUnicode_Check(res))
                    g_plugin_module_log_variadic_message(plugin, LMT_ERROR,
                                                         _("The returned #%zd value must be a string"));

                else
                {
                    *names = realloc(*names, ++(*count) * sizeof(char **));
                    *names[*count - 1] = strdup(PyUnicode_DATA(res));
                }

                Py_DECREF(res);

            }

        }

        Py_XDECREF(pyret);
        Py_DECREF(args);

    }

    Py_DECREF(pyobj);

    PyGILState_Release(gstate);

}



/* ---------------------------------------------------------------------------------- */
/*                       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  */
    GPluginModuleClass *plugin;             /* Version parente de la classe*/

    object = G_OBJECT_CLASS(klass);

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

    plugin = G_PLUGIN_MODULE_CLASS(klass);

    plugin->get_modname = (pg_get_modname_fc)g_python_plugin_get_modname;

}


/******************************************************************************
*                                                                             *
*  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);

    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  */

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

    if (final != NULL)
    {
        if (final->name != NULL) free(final->name);
        if (final->desc != NULL) free(final->desc);
        if (final->version != NULL) free(final->version);
        if (final->url != NULL) free(final->url);

        assert(final->required_count <= 1);

        if (final->required != NULL)
            free(final->required);

        if (final->actions != NULL)
            free(final->actions);

        free(final);

    }

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

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à valider.                                  *
*                                                                             *
*  Description : Fournit le nom brut associé au greffon.                      *
*                                                                             *
*  Retour      : Désignation brute du greffon.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static char *g_python_plugin_get_modname(const GPythonPlugin *plugin)
{
    char *result;                           /* Désignation brute à renvoyer*/
    char *path;                             /* Chemin à traiter            */

    path = strdup(g_plugin_module_get_filename(G_PLUGIN_MODULE(plugin)));

    result = strdup(basename(path));

    free(path);

    return result;

}


/******************************************************************************
*                                                                             *
*  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 *dict;                         /* Dictionnaire associé        */
    PyObject *class;                        /* Classe à instancier         */
    PyObject *instance;                     /* Instance Python du greffon  */

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

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

    if (module == NULL) goto no_import;

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

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

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

    result = G_PYTHON_PLUGIN(pygobject_get(instance));

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

    /**
     * L'instance Python et l'objet GLib résultante sont un même PyGObject.
     *
     * Donc pas besoin de toucher au comptage des références ici, la libération
     * se réalisera à la fin, quand l'objet GLib sera libéré.
     */

    Py_DECREF(module);

    return G_PLUGIN_MODULE(result);

 no_instance:

    log_pychrysalide_exception(_("An error occured when building the 'AutoLoad' instance"));

 no_class:

    if (class == NULL)
        log_plugin_simple_message(LMT_ERROR,
                                  _("An error occured when looking for the 'AutoLoad': item not found!"));

 no_import:

    Py_XDECREF(module);

    log_pychrysalide_exception(_("An error occured when importing '%s'"), modname);

 bad_exit:

    return NULL;

}



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


/******************************************************************************
*                                                                             *
*  Paramètres  : self = objet Python concerné par l'appel.                    *
*                args = arguments fournis à l'appel.                          *
*                                                                             *
*  Description : Construit le nom d'un fichier de configuration du greffon.   *
*                                                                             *
*  Retour      : Chemin d'accès déterminé, ou NULL en cas d'erreur.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_plugin_module_build_config_filename(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Bilan à retourner           */
    const char *final;                      /* Suffixe de fichier imposé   */
    int create;                             /* Volonté de création         */
    char *filename;                         /* Nom de fichier déterminé    */

#define PLUGIN_MODULE_BUILD_CONFIG_FILENAME_METHOD PYTHON_METHOD_DEF        \
(                                                                           \
    build_config_filename, "final, /, create=False",                        \
    METH_VARARGS, py_plugin_module,                                         \
    "Build a filename suitable for the plugin configuration, ending with"   \
    " the *final* suffix.\n"                                                \
    "\n"                                                                    \
    "If the *create* parameter is set, the path to this filename is"        \
    " created.\n"                                                           \
    "\n"                                                                    \
    "The result is a string or None on failure."                            \
)

    create = 0;

    if (!PyArg_ParseTuple(args, "s|p", &final, &create))
        return NULL;

    filename = g_plugin_module_build_config_filename(G_PLUGIN_MODULE(pygobject_get(self)), final, create);

    if (filename != NULL)
    {
        result = PyUnicode_FromString(filename);
        free(filename);
    }
    else
    {
        result = Py_None;
        Py_INCREF(result);
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = objet Python concerné par l'appel.                    *
*                args = arguments fournis à l'appel.                          *
*                                                                             *
*  Description : Affiche un message dans le journal des messages système.     *
*                                                                             *
*  Retour      : Rien en équivalent Python.                                   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_plugin_module_log_message(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Bilan à retourner           */
    LogMessageType type;                    /* Espèce du message           */
    const char *msg;                        /* Contenu du message          */

#define PLUGIN_MODULE_LOG_MESSAGE_METHOD PYTHON_METHOD_DEF                  \
(                                                                           \
    log_message, "type, msg, /",                                            \
    METH_VARARGS, py_plugin_module,                                         \
    "Display a message in the log window, in graphical mode, or in the"     \
    " console output if none.\n"                                            \
    "\n"                                                                    \
    "The type of the message has to be a pychrysalide.core.LogMessageType"  \
    " value."                                                               \
    "\n"                                                                    \
    "The only difference with the main pychrysalide.core.log_message()"     \
    " function is that messages are automatically prefixed with the plugin" \
    " name here."                                                           \
)

    if (!PyArg_ParseTuple(args, "O&s", convert_to_log_message_type, &type, &msg))
        return NULL;

    g_plugin_module_log_simple_message(G_PLUGIN_MODULE(pygobject_get(self)), type, msg);

    result = Py_None;
    Py_INCREF(result);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Fournit le nom brut associé au greffon.                      *
*                                                                             *
*  Retour      : Désignation brute du greffon.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_plugin_module_get_modname(PyObject *self, void *closure)
{
    PyObject *result;                       /* Valeur à retourner          */
    GPluginModule *plugin;                  /* Version native du greffon   */
    char *modname;                          /* Désignation brute           */

#define PLUGIN_MODULE_MODNAME_ATTRIB PYTHON_GET_DEF_FULL    \
(                                                           \
    modname, py_plugin_module,                              \
    "Raw module name of the plugin."                        \
)

    plugin = G_PLUGIN_MODULE(pygobject_get(self));
    modname = g_plugin_module_get_modname(plugin);

    result = PyUnicode_FromString(modname);

    free(modname);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Indique le fichier contenant le greffon manipulé.            *
*                                                                             *
*  Retour      : Chemin d'accès au greffon.                                   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_plugin_module_get_filename(PyObject *self, void *closure)
{
    PyObject *result;                       /* Valeur à retourner          */
    GPluginModule *plugin;                  /* Version native du greffon   */
    const char *filename;                   /* Chemin d'accès associé      */

#define PLUGIN_MODULE_FILENAME_ATTRIB PYTHON_GET_DEF_FULL   \
(                                                           \
    filename, py_plugin_module,                             \
    "Filename of the plugin."                               \
)

    plugin = G_PLUGIN_MODULE(pygobject_get(self));
    filename = g_plugin_module_get_filename(plugin);

    result = PyUnicode_FromString(filename);

    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[] = {
        PLUGIN_MODULE_NOTIFY_PLUGINS_LOADED_WRAPPER,
        PLUGIN_MODULE_INCLUDE_THEME_WRAPPER,
        PLUGIN_MODULE_ON_PANEL_CREATION_WRAPPER,
        PLUGIN_MODULE_ON_PANEL_DOCKING_WRAPPER,
        PLUGIN_MODULE_HANDLE_BINARY_CONTENT_WRAPPER,
        PLUGIN_MODULE_HANDLE_LOADED_CONTENT_WRAPPER,
        PLUGIN_MODULE_HANDLE_BINARY_FORMAT_ANALYSIS_WRAPPER,
        PLUGIN_MODULE_PRELOAD_BINARY_FORMAT_WRAPPER,
        PLUGIN_MODULE_ATTACH_DEBUG_FORMAT_WRAPPER,
        PLUGIN_MODULE_PROCESS_DISASSEMBLY_EVENT_WRAPPER,
        PLUGIN_MODULE_DETECT_EXTERNAL_TOOLS_WRAPPER,
        PLUGIN_MODULE_BUILD_CONFIG_FILENAME_METHOD,
        PLUGIN_MODULE_LOG_MESSAGE_METHOD,
        { NULL }
    };

    static PyGetSetDef py_plugin_module_getseters[] = {
        PLUGIN_MODULE_MODNAME_ATTRIB,
        PLUGIN_MODULE_FILENAME_ATTRIB,
        { 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         = PLUGIN_MODULE_DOC,

        .tp_methods     = py_plugin_module_methods,
        .tp_getset      = py_plugin_module_getseters,

        .tp_init        = py_plugin_module_init,
        .tp_new         = py_plugin_module_new,

    };

    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_PYTHON_PLUGIN, type, &PyGObject_Type))
            return false;

        if (!define_plugin_module_constants(type))
            return false;

    }

    return true;

}