summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/arch/operands/immediate.c
blob: a335db37eab4131b112791a35073b2021b2a038e (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * immediate.c - équivalent Python du fichier "arch/operands/immediate.h"
 *
 * Copyright (C) 2020-2025 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 "immediate.h"


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


#include <i18n.h>
#include <arch/operands/immediate-int.h>


#include "constants.h"
#include "../operand.h"
#include "../../access.h"
#include "../../constants.h"
#include "../../helpers.h"
#include "../../analysis/content.h"



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


CREATE_DYN_CONSTRUCTOR(immediate_operand, G_TYPE_IMMEDIATE_OPERAND);

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



/* ---------------------------- DEFINITION D'UN IMMEDIAT ---------------------------- */


/* Renseigne la taille de la valeur indiquée à la construction. */
static PyObject *py_immediate_operand_get_size(PyObject *, void *);

/* Fournit la valeur portée par une opérande numérique. */
static PyObject *py_immediate_operand_get_value(PyObject *, void *);

/* Indique le signe d'une valeur immédiate. */
static PyObject *py_immediate_operand_is_negative(PyObject *, void *);

/* Indique le format textuel par défaut de la valeur. */
static PyObject *py_immediate_operand_get_default_display(PyObject *, void *);

/* Définit le format textuel par défaut de la valeur. */
static int py_immediate_operand_set_default_display(PyObject *, PyObject *, void *);

/* Indique la grande ligne du format textuel de la valeur. */
static PyObject *py_immediate_operand_get_display(PyObject *, void *);

/* Définit la grande ligne du format textuel de la valeur. */
static int py_immediate_operand_set_display(PyObject *, PyObject *, void *);



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


/******************************************************************************
*                                                                             *
*  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_immediate_operand_init(PyObject *self, PyObject *args, PyObject *kwds)
{
    MemoryDataSize size;                    /* Taille des données finale   */
    unsigned long long value;               /* Valeur brute à représenter  */
    int ret;                                /* Bilan de lecture des args.  */
    GImmediateOperand *operand;             /* Opérande natif à manipuler  */

#define IMMEDIATE_OPERAND_DOC                                           \
    "The ImmediateOperand deals with immediate value as operand."       \
    "\n"                                                                \
    "There are several ways to display these values in a disassembly,"  \
    " the operand handles that.\n"                                      \
    "\n"                                                                \
    "Instances can be created using the following constructor:\n"       \
    "\n"                                                                \
    "    ImmediateOperand(size, value)"                                 \
    "\n"                                                                \
    "Where *size* specifies the original size of the provided *value*," \
    " as a pychrysalide.MemoryDataSize."

    /* Récupération des paramètres */

    ret = PyArg_ParseTuple(args, "O&K", convert_to_memory_data_size, &size, &value);
    if (!ret) return -1;

    /* Initialisation d'un objet GLib */

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

    /* Eléments de base */

    operand = G_IMMEDIATE_OPERAND(pygobject_get(self));

    if (!g_immediate_operand_create_from_value(operand, size, value))
        return -1;

    return 0;

}



/* ---------------------------------------------------------------------------------- */
/*                              DEFINITION D'UN IMMEDIAT                              */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Renseigne la taille de la valeur indiquée à la construction. *
*                                                                             *
*  Retour      : Taille de la valeur représentée en mémoire.                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_immediate_operand_get_size(PyObject *self, void *closure)
{
    PyObject *result;                       /* Instance Python à retourner */
    GImmediateOperand *operand;             /* Version GLib de l'opérande  */
    MemoryDataSize size;                    /* Type de donnée représentée  */

#define IMMEDIATE_OPERAND_SIZE_ATTRIB PYTHON_GET_DEF_FULL           \
(                                                                   \
    size, py_immediate_operand,                                     \
    "Get or set the size of the value contained in the operand."    \
    "\n"                                                            \
    "The property is a value of type"                               \
    " pychrysalide.MemoryDataSize."                                 \
)

    operand = G_IMMEDIATE_OPERAND(pygobject_get(self));

    size = g_immediate_operand_get_size(operand);

    result = cast_memory_data_size_to_python(size);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Fournit la valeur portée par une opérande numérique.         *
*                                                                             *
*  Retour      : Valeur contenue dans l'opérande, ou None en cas de soucis.   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_immediate_operand_get_value(PyObject *self, void *closure)
{
    PyObject *result;                       /* Instance Python à retourner */
    GImmediateOperand *operand;             /* Version GLib de l'opérande  */
    MemoryDataSize size;                    /* Type de donnée représentée  */
    uint8_t uval8;                          /* Valeur sur 8 bits           */
    uint16_t uval16;                        /* Valeur sur 16 bits          */
    uint32_t uval32;                        /* Valeur sur 32 bits          */
    uint64_t uval64;                        /* Valeur sur 64 bits          */
    int8_t sval8;                           /* Valeur sur 8 bits           */
    int16_t sval16;                         /* Valeur sur 16 bits          */
    int32_t sval32;                         /* Valeur sur 32 bits          */
    int64_t sval64;                         /* Valeur sur 64 bits          */

#define IMMEDIATE_OPERAND_VALUE_ATTRIB PYTHON_GET_DEF_FULL  \
(                                                           \
    value, py_immediate_operand,                            \
    "Value of the immediate operand, as an integer."        \
)

    operand = G_IMMEDIATE_OPERAND(pygobject_get(self));

    size = g_immediate_operand_get_size(operand);

    switch (size)
    {
        /* Pour GCC... */
        case MDS_UNDEFINED:
            result = Py_None;
            Py_INCREF(result);
            break;
        case MDS_4_BITS_UNSIGNED:
        case MDS_8_BITS_UNSIGNED:
            g_immediate_operand_get_value(operand, size, &uval8);
            result = PyLong_FromUnsignedLong(uval8);
            break;
        case MDS_16_BITS_UNSIGNED:
            g_immediate_operand_get_value(operand, size, &uval16);
            result = PyLong_FromUnsignedLong(uval16);
            break;
        case MDS_32_BITS_UNSIGNED:
            g_immediate_operand_get_value(operand, size, &uval32);
            result = PyLong_FromUnsignedLong(uval32);
            break;
        case MDS_64_BITS_UNSIGNED:
            g_immediate_operand_get_value(operand, size, &uval64);
            result = PyLong_FromUnsignedLongLong(uval64);
            break;
        case MDS_4_BITS_SIGNED:
        case MDS_8_BITS_SIGNED:
            g_immediate_operand_get_value(operand, size, &sval8);
            result = PyLong_FromLong(sval8);
            break;
        case MDS_16_BITS_SIGNED:
            g_immediate_operand_get_value(operand, size, &sval16);
            result = PyLong_FromLong(sval16);
            break;
        case MDS_32_BITS_SIGNED:
            g_immediate_operand_get_value(operand, size, &sval32);
            result = PyLong_FromLong(sval32);
            break;
        case MDS_64_BITS_SIGNED:
            g_immediate_operand_get_value(operand, size, &sval64);
            result = PyLong_FromLongLong(sval64);
            break;

        /* Pour GCC... */
        default:
            assert(false);
            result = Py_None;
            Py_INCREF(result);
            break;

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Indique le signe d'une valeur immédiate.                     *
*                                                                             *
*  Retour      : True si la valeur est strictement négative, False sinon.     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_immediate_operand_is_negative(PyObject *self, void *closure)
{
    PyObject *result;                       /* Instance Python à retourner */
    GImmediateOperand *operand;             /* Version GLib de l'opérande  */
    bool status;                            /* Etat à faire connaître      */

#define IMMEDIATE_OPERAND_NEGATIVE_ATTRIB PYTHON_IS_DEF_FULL    \
(                                                               \
    negative, py_immediate_operand,                             \
    "Sign of the value, as a boolean status."                   \
)

    operand = G_IMMEDIATE_OPERAND(pygobject_get(self));

    status = g_immediate_operand_is_negative(operand);

    result = status ? Py_True : Py_False;
    Py_INCREF(result);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Indique le format textuel par défaut de la valeur.           *
*                                                                             *
*  Retour      : Format global d'un affichage de valeur.                      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_immediate_operand_get_default_display(PyObject *self, void *closure)
{
    PyObject *result;                       /* Instance Python à retourner */
    GImmediateOperand *operand;             /* Version GLib de l'opérande  */
    ImmOperandDisplay display;              /* Type d'affichage courant    */

#define IMMEDIATE_OPERAND_DEFAULT_DISPLAY_ATTRIB PYTHON_GETSET_DEF_FULL \
(                                                                       \
    default_display, py_immediate_operand,                              \
    "Define of the immediate operand default textual representation."   \
    "\n"                                                                \
    "The property is a value of type"                                   \
    " pychrysalide.arch.operands.ImmOperand.ImmOperandDisplay."         \
)

    operand = G_IMMEDIATE_OPERAND(pygobject_get(self));

    display = g_immediate_operand_get_default_display(operand);

    result = cast_imm_operand_display_to_python(display);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                value   = valeur fournie à intégrer ou prendre en compte.    *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Définit le format textuel par défaut de la valeur.           *
*                                                                             *
*  Retour      : Bilan de l'opération pour Python.                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static int py_immediate_operand_set_default_display(PyObject *self, PyObject *value, void *closure)
{
    ImmOperandDisplay display;              /* Type d'affichage demandé    */
    GImmediateOperand *operand;                   /* Version GLib de l'opérande  */

    if (!PyLong_Check(value))
    {
        PyErr_SetString(PyExc_TypeError, _("Invalid display type"));
        return -1;
    }

    display = PyLong_AsUnsignedLong(value);

    if (!(IOD_BIN <= display && display <= IOD_CHAR))
    {
        PyErr_SetString(PyExc_TypeError, _("Invalid display type"));
        return -1;
    }

    operand = G_IMMEDIATE_OPERAND(pygobject_get(self));

    g_immediate_operand_set_default_display(operand, display);

    return 0;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Indique la grande ligne du format textuel de la valeur.      *
*                                                                             *
*  Retour      : Format global d'un affichage de valeur.                      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_immediate_operand_get_display(PyObject *self, void *closure)
{
    PyObject *result;                       /* Instance Python à retourner */
    GImmediateOperand *operand;             /* Version GLib de l'opérande  */
    ImmOperandDisplay display;              /* Type d'affichage courant    */

#define IMMEDIATE_OPERAND_DISPLAY_ATTRIB PYTHON_GETSET_DEF_FULL     \
(                                                                   \
    display, py_immediate_operand,                                  \
    "Retrieve or define of the immediate operand current textual"   \
    " representation."                                              \
    "\n"                                                            \
    "The property is a value of type"                               \
    " pychrysalide.arch.operands.ImmOperand.ImmOperandDisplay."     \
)

    operand = G_IMMEDIATE_OPERAND(pygobject_get(self));

    display = g_immediate_operand_get_display(operand);

    result = cast_imm_operand_display_to_python(display);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                value   = valeur fournie à intégrer ou prendre en compte.    *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Définit la grande ligne du format textuel de la valeur.      *
*                                                                             *
*  Retour      : Bilan de l'opération pour Python.                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static int py_immediate_operand_set_display(PyObject *self, PyObject *value, void *closure)
{
    ImmOperandDisplay display;              /* Type d'affichage demandé    */
    GImmediateOperand *operand;                   /* Version GLib de l'opérande  */

    if (!PyLong_Check(value))
    {
        PyErr_SetString(PyExc_TypeError, _("Invalid display type"));
        return -1;
    }

    display = PyLong_AsUnsignedLong(value);

    if (!(IOD_BIN <= display && display <= IOD_CHAR))
    {
        PyErr_SetString(PyExc_TypeError, _("Invalid display type"));
        return -1;
    }

    operand = G_IMMEDIATE_OPERAND(pygobject_get(self));

    g_immediate_operand_set_display(operand, display);

    return 0;

}


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

PyTypeObject *get_python_immediate_operand_type(void)
{
    static PyMethodDef py_immediate_operand_methods[] = {
        { NULL }
    };

    static PyGetSetDef py_immediate_operand_getseters[] = {
        IMMEDIATE_OPERAND_SIZE_ATTRIB,
        IMMEDIATE_OPERAND_VALUE_ATTRIB,
        IMMEDIATE_OPERAND_NEGATIVE_ATTRIB,
        IMMEDIATE_OPERAND_DEFAULT_DISPLAY_ATTRIB,
        IMMEDIATE_OPERAND_DISPLAY_ATTRIB,
        { NULL }
    };

    static PyTypeObject py_immediate_operand_type = {

        PyVarObject_HEAD_INIT(NULL, 0)

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

        .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,

        .tp_doc         = IMMEDIATE_OPERAND_DOC,

        .tp_methods     = py_immediate_operand_methods,
        .tp_getset      = py_immediate_operand_getseters,

        .tp_init        = py_immediate_operand_init,
        .tp_new         = py_immediate_operand_new,

    };

    return &py_immediate_operand_type;

}


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

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

    type = get_python_immediate_operand_type();

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

        dict = PyModule_GetDict(module);

        if (!ensure_python_arch_operand_is_registered())
            return false;

        if (!register_class_for_pygobject(dict, G_TYPE_IMMEDIATE_OPERAND, type))
            return false;

        if (!define_immediate_operand_constants(type))
            return false;

    }

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : arg = argument quelconque à tenter de convertir.             *
*                dst = destination des valeurs récupérées en cas de succès.   *
*                                                                             *
*  Description : Tente de convertir en opérande de valeur immédiate.          *
*                                                                             *
*  Retour      : Bilan de l'opération, voire indications supplémentaires.     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

int convert_to_immediate_operand(PyObject *arg, void *dst)
{
    int result;                             /* Bilan à retourner           */

    result = PyObject_IsInstance(arg, (PyObject *)get_python_immediate_operand_type());

    switch (result)
    {
        case -1:
            /* L'exception est déjà fixée par Python */
            result = 0;
            break;

        case 0:
            PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to immediate operand");
            break;

        case 1:
            *((GImmediateOperand **)dst) = G_IMMEDIATE_OPERAND(pygobject_get(arg));
            break;

        default:
            assert(false);
            break;

    }

    return result;

}