summaryrefslogtreecommitdiff
path: root/plugins/pychrysa/arch/instruction.c
blob: 09397fc0828894c0f5be42fa9448e0f292545d1f (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

/* OpenIDA - Outil d'analyse de fichiers binaires
 * instruction.c - équivalent Python du fichier "arch/instruction.h"
 *
 * Copyright (C) 2012 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA 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 "instruction.h"


#include <pygobject.h>
#include <stdbool.h>
#include <string.h>


#include <arch/instruction.h>


#include "../quirks.h"



/* --------------------- ITERATEUR POUR BOUCLE SUR INSTRUCTIONS --------------------- */


/* Itérateur pour les instructions */
typedef struct _PyArchInstructionIter
{
    PyObject_HEAD                           /* A laisser en premier        */

    GArchInstruction *head;                 /* Ancrage supposé             */
    PyObject *current;                      /* Départ, puis parcours...    */
    bool started;                           /* Démarrage effectué ?        */

} PyArchInstructionIter;


/* Prépare l'itérateur pour un parcours d'instructions. */
static PyObject *py_arch_instruction_iterator_create(PyObject *);

/* Libère la mémoire d'un itérateur de 'ArchInstruction'. */
static void py_arch_instruction_iterator_dealloc(PyArchInstructionIter *);

/* Fournit l'élément suivant dans un parcours d'instructions. */
static PyObject *py_arch_instruction_iterator_next(PyArchInstructionIter *);



/* --------------------- INSTRUCTIONS D'ARCHITECTURES EN PYTHON --------------------- */


/* Crée un nouvel objet Python de type 'ArchInstruction'. */
static PyObject *py_arch_instruction_new(PyTypeObject *, PyObject *, PyObject *);

/* Prépare un parcours d'instructions. */
static PyObject *py_arch_instruction_get_iter(PyObject *);

/* Fournit la valeur associée à une propriété donnée. */
static PyObject *py_arch_instruction_get_location(PyObject *, char *);

/* Fournit le nom humain de l'instruction manipulée. */
static PyObject *py_arch_instruction_get_keyword(PyObject *, void *);



/* ---------------------------------------------------------------------------------- */
/*                       ITERATEUR POUR BOUCLE SUR INSTRUCTIONS                       */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : origin = élément à l'origine du parcours.                    *
*                                                                             *
*  Description : Prépare l'itérateur pour un parcours d'instructions.         *
*                                                                             *
*  Retour      : Instance d'itérateur prête à emploi.                         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_arch_instruction_iterator_create(PyObject *origin)
{
    PyArchInstructionIter *result;          /* Nouvelle instance à renvoyer*/
    PyObject *module;                       /* Module d'appartenance       */
    PyTypeObject *type;                     /* Type d'objet à créer        */

    module = PyImport_ImportModule("pychrysalide.arch");
    type = (PyTypeObject *)PyObject_GetAttrString(module, "ArchInstructionIterator");
    Py_DECREF(module);

    result = (PyArchInstructionIter *)type->tp_alloc(type, 0);
    Py_DECREF(type);

    if (result != NULL)
    {
        Py_INCREF(origin);
        result->head = G_ARCH_INSTRUCTION(pygobject_get(origin));
        g_object_ref(G_OBJECT(result->head));

        result->current = origin;
    }

    return (PyObject *)result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = instance d'objet à supprimer.                         *
*                                                                             *
*  Description : Libère la mémoire d'un itérateur de 'ArchInstruction'.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void py_arch_instruction_iterator_dealloc(PyArchInstructionIter *self)
{
    PyObject *first;                        /* Récupération de la première */

    first = pychrysalide_get_pygobject(G_OBJECT(self->head));

    Py_DECREF(first);
    g_object_unref(G_OBJECT(self->head));

#if PY_VERSION_HEX < 0x03000000
    self->ob_type->tp_free((PyObject *)self);
#else
    Py_TYPE(self)->tp_free((PyObject *)self);
#endif

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = instance manipulée à traiter.                         *
*                                                                             *
*  Description : Fournit l'élément suivant dans un parcours d'instructions.   *
*                                                                             *
*  Retour      : Point suivant du parcours ou NULL.                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_arch_instruction_iterator_next(PyArchInstructionIter *self)
{
    PyObject *result;                       /* Elément à retourner         */
    GArchInstruction *next;                 /* Elément réel suivant        */

    if (!self->started)
    {
        self->started = true;
        result = self->current;
        Py_INCREF(result);

    }
    else
    {
        next = G_ARCH_INSTRUCTION(pygobject_get(self->current));
        next = g_arch_instruction_get_next_iter(self->head, next, VMPA_MAX);

        if (next != NULL)
        {
            result = pygobject_new(G_OBJECT(next));
            self->current = result;
        }
        else result = NULL;

    }

    Py_XINCREF(result);
    return (PyObject *)result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : module = module dont la définition est à compléter.          *
*                                                                             *
*  Description : Permet une itération de 'pychrysalide.arch.ArchInstruction'. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool register_python_arch_instruction_iterator(PyObject *module)
{
    int ret;                                /* Bilan d'un appel            */

    static PyTypeObject py_arch_instruction_iterator_type = {

        PyObject_HEAD_INIT(NULL)

        .tp_name        = "pychrysalide.arch.ArchInstructionIterator",
        .tp_basicsize   = sizeof(PyArchInstructionIter),

        .tp_dealloc     = (destructor)py_arch_instruction_iterator_dealloc,

        .tp_flags       = Py_TPFLAGS_HAVE_ITER | Py_TPFLAGS_HAVE_CLASS,

        .tp_doc         = "PyChrysalide architecture instruction iterator",

        .tp_iter        = PyObject_SelfIter,
        .tp_iternext    = (iternextfunc)py_arch_instruction_iterator_next

    };

    if (PyType_Ready(&py_arch_instruction_iterator_type) < 0)
        return false;

    Py_INCREF(&py_arch_instruction_iterator_type);
    ret = PyModule_AddObject(module, "ArchInstructionIterator", (PyObject *)&py_arch_instruction_iterator_type);

    return (ret == 0);

}



/* ---------------------------------------------------------------------------------- */
/*                       INSTRUCTIONS D'ARCHITECTURES EN PYTHON                       */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : type = type de l'objet à instancier.                         *
*                args = arguments fournis à l'appel.                          *
*                kwds = arguments de type key=val fournis.                    *
*                                                                             *
*  Description : Crée un nouvel objet Python de type 'ArchInstruction'.       *
*                                                                             *
*  Retour      : Instance Python mise en place.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_arch_instruction_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    Py_RETURN_NONE;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = instance manipulée à traiter.                         *
*                                                                             *
*  Description : Prépare un parcours d'instructions.                          *
*                                                                             *
*  Retour      : Point de départ d'un parcours.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_arch_instruction_get_iter(PyObject *self)
{
    return py_arch_instruction_iterator_create(self);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = classe représentant une instruction.                  *
*                name = nom de la propriété à lire.                           *
*                                                                             *
*  Description : Fournit la valeur associée à une propriété donnée.           *
*                                                                             *
*  Retour      : Valeur associée à la propriété consultée.                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_arch_instruction_get_location(PyObject *self, char *name)
{
    PyObject *result;                       /* Trouvailles à retourner     */
    GArchInstruction *instr;                /* Version native              */
    off_t offset;                           /* Position dans le fichier    */
    off_t length;                           /* Taille de l'instruction     */
    vmpa_t address;                         /* Position en mémoire         */

    instr = G_ARCH_INSTRUCTION(pygobject_get(self));
    g_arch_instruction_get_location(instr, &offset, &length, &address);

    if (strcmp(name, "offset") == 0)
        result = PyLong_FromLong(offset);

    else if (strcmp(name, "length") == 0)
        result = PyLong_FromLong(length);

    else if (strcmp(name, "address") == 0)
        result = PyLong_FromLongLong(address);

    else
        result = NULL;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self   = classe représentant une instruction.                *
*                unused = adresse non utilisée ici.                           *
*                                                                             *
*  Description : Fournit le nom humain de l'instruction manipulée.            *
*                                                                             *
*  Retour      : Valeur associée à la propriété consultée.                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_arch_instruction_get_keyword(PyObject *self, void *unused)
{
    PyObject *result;                       /* Trouvailles à retourner     */
    GArchInstruction *instr;                /* Version native              */
    const char *kw;                         /* Valeur récupérée            */

    instr = G_ARCH_INSTRUCTION(pygobject_get(self));
    kw = g_arch_instruction_get_keyword(instr);

    result = PyString_FromString(kw);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : module = module dont la définition est à compléter.          *
*                                                                             *
*  Description : Prend en charge l'objet 'pychrysalide.arch.ArchInstruction'. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool register_python_arch_instruction(PyObject *module)
{
    PyObject *pygobj_mod;                   /* Module Python-GObject       */
    int ret;                                /* Bilan d'un appel            */

    static PyMethodDef py_arch_instruction_methods[] = {
        { NULL }
    };

    static PyGetSetDef py_arch_instruction_getseters[] = {
        {
            "offset", (getter)py_arch_instruction_get_location, (setter)NULL,
            "Provide the location of the instruction in the binary file.", "offset"
        },
        {
            "length", (getter)py_arch_instruction_get_location, (setter)NULL,
            "Provide the length of the instruction.", "length"
        },
        {
            "address", (getter)py_arch_instruction_get_location, (setter)NULL,
            "Provide the location of the instruction in memory.", "address"
        },
        {
            "keyword", (getter)py_arch_instruction_get_keyword, (setter)NULL,
            "Give le name of the assembly instruction.", NULL
        },
        { NULL }
    };

    static PyTypeObject py_arch_instruction_type = {

        PyObject_HEAD_INIT(NULL)

        .tp_name        = "pychrysalide.arch.ArchInstruction",
        .tp_basicsize   = sizeof(PyGObject),

        .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,

        .tp_doc         = "PyChrysalide architecture instruction",

        .tp_methods     = py_arch_instruction_methods,
        .tp_getset      = py_arch_instruction_getseters,
        .tp_new         = (newfunc)py_arch_instruction_new,

        .tp_iter        = (getiterfunc)py_arch_instruction_get_iter

    };

    pygobj_mod = PyImport_ImportModule("gobject");
    if (pygobj_mod == NULL) return false;

    py_arch_instruction_type.tp_base = (PyTypeObject *)PyObject_GetAttrString(pygobj_mod, "GObject");
    Py_DECREF(pygobj_mod);

    if (PyType_Ready(&py_arch_instruction_type) < 0)
        return false;

    Py_INCREF(&py_arch_instruction_type);
    ret = PyModule_AddObject(module, "ArchInstruction", (PyObject *)&py_arch_instruction_type);

    pygobject_register_class(module, "GArchInstruction", G_TYPE_ARCH_INSTRUCTION,
                             &py_arch_instruction_type,
                             Py_BuildValue("(O)", py_arch_instruction_type.tp_base));

    return (ret == 0);

}