summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/analysis/content.c
blob: f590f829d6cf20fd929e4b739007159c6ce79756 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * content.c - prototypes pour l'équivalent Python du fichier "analysis/content.c"
 *
 * Copyright (C) 2015-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "content.h"


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


#include <i18n.h>


#include <analysis/content.h>
#include <common/endianness.h>


#include "../access.h"
#include "../arch/vmpa.h"



/* Fournit une portion des données représentées. */
static PyObject *py_binary_content_read_raw(PyObject *, PyObject *);

/* Lit un nombre non signé sur un octet. */
static PyObject *py_binary_content_read_u8(PyObject *, PyObject *);

/* Lit un nombre non signé sur deux octets. */
static PyObject *py_binary_content_read_u16(PyObject *, PyObject *);

/* Lit un nombre non signé sur quatre octets. */
static PyObject *py_binary_content_read_u32(PyObject *, PyObject *);

/* Lit un nombre non signé sur huit octets. */
static PyObject *py_binary_content_read_u64(PyObject *, PyObject *);

/* Fournit une empreinte unique (SHA256) pour les données. */
static PyObject *py_binary_content_get_checksum(PyObject *, void *);

/* Détermine le nombre d'octets lisibles. */
static PyObject *py_binary_content_compute_size(PyObject *, void *);

/* Détermine la position initiale d'un contenu. */
static PyObject *py_binary_content_compute_start_pos(PyObject *, void *);

/* Détermine la position finale d'un contenu. */
static PyObject *py_binary_content_compute_end_pos(PyObject *, void *);

/* Renvoie tout le contenu binaire d'un coup. */
static PyObject *py_binary_content_get_all_bytes(PyObject *, void *);



/******************************************************************************
*                                                                             *
*  Paramètres  : self = contenu binaire à manipuler.                          *
*                args = non utilisé ici.                                      *
*                                                                             *
*  Description : Fournit une portion des données représentées.                *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_read_raw(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Instance à retourner        */
    GBinContent *content;                   /* Version GLib du format      */
    PyObject *addr_obj;                     /* Objet pour une position     */
    vmpa2t *addr;                           /* Position interne associée   */
    unsigned long long length;              /* Quantité de données à lire  */
    int ret;                                /* Bilan de lecture des args.  */
    const bin_t *val;                       /* Valeur lue à faire suivre   */

    content = G_BIN_CONTENT(pygobject_get(self));
    assert(content != NULL);

    ret = PyArg_ParseTuple(args, "OK", &addr_obj, &length);
    if (!ret) return NULL;

    addr = get_internal_vmpa(addr_obj);
    assert(addr != NULL);

    val = g_binary_content_get_raw_access(content, addr, length);
    if (val == NULL)
    {
        PyErr_SetString(PyExc_Exception, _("Invalid read access."));
        return NULL;
    }

    result = PyBytes_FromStringAndSize((char *)val, length);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = contenu binaire à manipuler.                          *
*                args = non utilisé ici.                                      *
*                                                                             *
*  Description : Lit un nombre non signé sur un octet.                        *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_read_u8(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Instance à retourner        */
    GBinContent *content;                   /* Version GLib du format      */
    int ret;                                /* Bilan de lecture des args.  */
    PyObject *addr_obj;                     /* Objet pour une position     */
    vmpa2t *addr;                           /* Position interne associée   */
    uint8_t val;                            /* Valeur lue à faire suivre   */
    bool status;                            /* Bilan de l'opération        */

    content = G_BIN_CONTENT(pygobject_get(self));
    assert(content != NULL);

    ret = PyArg_ParseTuple(args, "O", &addr_obj);
    if (!ret) return NULL;

    addr = get_internal_vmpa(addr_obj);
    assert(addr != NULL);

    status = g_binary_content_read_u8(content, addr, &val);
    if (!status)
    {
        PyErr_SetString(PyExc_Exception, _("Invalid read access."));
        return NULL;
    }

    result = PyLong_FromUnsignedLong(val);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = contenu binaire à manipuler.                          *
*                args = non utilisé ici.                                      *
*                                                                             *
*  Description : Lit un nombre non signé sur deux octets.                     *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_read_u16(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Instance à retourner        */
    GBinContent *content;                   /* Version GLib du format      */
    int ret;                                /* Bilan de lecture des args.  */
    PyObject *addr_obj;                     /* Objet pour une position     */
    unsigned long endianness;               /* Boutisme de la lecture      */
    vmpa2t *addr;                           /* Position interne associée   */
    uint16_t val;                           /* Valeur lue à faire suivre   */
    bool status;                            /* Bilan de l'opération        */

    content = G_BIN_CONTENT(pygobject_get(self));
    assert(content != NULL);

    ret = PyArg_ParseTuple(args, "Ok", &addr_obj, &endianness);
    if (!ret) return NULL;

    addr = get_internal_vmpa(addr_obj);
    assert(addr != NULL);

    status = g_binary_content_read_u16(content, addr, endianness, &val);
    if (!status)
    {
        PyErr_SetString(PyExc_Exception, _("Invalid read access."));
        return NULL;
    }

    result = PyLong_FromUnsignedLong(val);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = contenu binaire à manipuler.                          *
*                args = non utilisé ici.                                      *
*                                                                             *
*  Description : Lit un nombre non signé sur quatre octets.                   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_read_u32(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Instance à retourner        */
    GBinContent *content;                   /* Version GLib du format      */
    int ret;                                /* Bilan de lecture des args.  */
    PyObject *addr_obj;                     /* Objet pour une position     */
    unsigned long endianness;               /* Boutisme de la lecture      */
    vmpa2t *addr;                           /* Position interne associée   */
    uint32_t val;                           /* Valeur lue à faire suivre   */
    bool status;                            /* Bilan de l'opération        */

    content = G_BIN_CONTENT(pygobject_get(self));
    assert(content != NULL);

    ret = PyArg_ParseTuple(args, "Ok", &addr_obj, &endianness);
    if (!ret) return NULL;

    addr = get_internal_vmpa(addr_obj);
    assert(addr != NULL);

    status = g_binary_content_read_u32(content, addr, endianness, &val);
    if (!status)
    {
        PyErr_SetString(PyExc_Exception, _("Invalid read access."));
        return NULL;
    }

    result = PyLong_FromUnsignedLong(val);

    return result;

}

/******************************************************************************
*                                                                             *
*  Paramètres  : self = contenu binaire à manipuler.                          *
*                args = non utilisé ici.                                      *
*                                                                             *
*  Description : Lit un nombre non signé sur huit octets.                     *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_read_u64(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Instance à retourner        */
    GBinContent *content;                   /* Version GLib du format      */
    int ret;                                /* Bilan de lecture des args.  */
    PyObject *addr_obj;                     /* Objet pour une position     */
    unsigned long endianness;               /* Boutisme de la lecture      */
    vmpa2t *addr;                           /* Position interne associée   */
    uint64_t val;                           /* Valeur lue à faire suivre   */
    bool status;                            /* Bilan de l'opération        */

    content = G_BIN_CONTENT(pygobject_get(self));
    assert(content != NULL);

    ret = PyArg_ParseTuple(args, "Ok", &addr_obj, &endianness);
    if (!ret) return NULL;

    addr = get_internal_vmpa(addr_obj);
    assert(addr != NULL);

    status = g_binary_content_read_u64(content, addr, endianness, &val);
    if (!status)
    {
        PyErr_SetString(PyExc_Exception, _("Invalid read access."));
        return NULL;
    }

    result = PyLong_FromUnsignedLongLong(val);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = contenu binaire à manipuler.                       *
*                closure = adresse non utilisée ici.                          *
*                                                                             *
*  Description : Fournit une empreinte unique (SHA256) pour les données.      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_get_checksum(PyObject *self, void *closure)
{
    PyObject *result;                       /* Instance à retourner        */
    GBinContent *content;                   /* Version GLib du format      */
    const gchar *checksum;                  /* Empreinte fournie           */

    content = G_BIN_CONTENT(pygobject_get(self));

    checksum = g_binary_content_get_checksum(content);

    result = PyUnicode_FromString(checksum);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = contenu binaire à manipuler.                       *
*                closure = adresse non utilisée ici.                          *
*                                                                             *
*  Description : Détermine le nombre d'octets lisibles.                       *
*                                                                             *
*  Retour      : Quantité représentée.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_compute_size(PyObject *self, void *closure)
{
    PyObject *result;                       /* Instance à retourner        */
    GBinContent *content;                   /* Version GLib du format      */
    phys_t size;                            /* Quantité d'octets dispos.   */

    content = G_BIN_CONTENT(pygobject_get(self));

    size = g_binary_content_compute_size(content);

    result = PyLong_FromUnsignedLongLong(size);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = contenu binaire à manipuler.                       *
*                closure = adresse non utilisée ici.                          *
*                                                                             *
*  Description : Détermine la position initiale d'un contenu.                 *
*                                                                             *
*  Retour      : Position initiale.                                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_compute_start_pos(PyObject *self, void *closure)
{
    PyObject *result;                       /* Instance à retourner        */
    GBinContent *content;                   /* Version GLib du format      */
    vmpa2t pos;                             /* Position à transmettre      */

    content = G_BIN_CONTENT(pygobject_get(self));

    g_binary_content_compute_start_pos(content, &pos);

    result = build_from_internal_vmpa(&pos);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = contenu binaire à manipuler.                       *
*                closure = adresse non utilisée ici.                          *
*                                                                             *
*  Description : Détermine la position finale d'un contenu.                   *
*                                                                             *
*  Retour      : Position finale.                                             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_compute_end_pos(PyObject *self, void *closure)
{
    PyObject *result;                       /* Instance à retourner        */
    GBinContent *content;                   /* Version GLib du format      */
    vmpa2t pos;                             /* Position à transmettre      */

    content = G_BIN_CONTENT(pygobject_get(self));

    g_binary_content_compute_end_pos(content, &pos);

    result = build_from_internal_vmpa(&pos);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = contenu binaire à manipuler.                       *
*                closure = adresse non utilisée ici.                          *
*                                                                             *
*  Description : Renvoie tout le contenu binaire d'un coup.                   *
*                                                                             *
*  Retour      : Ensemble d'octets lus.                                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_content_get_all_bytes(PyObject *self, void *closure)
{
    PyObject *result;                       /* Contenu à retourner         */
    GBinContent *content;                   /* Version GLib du format      */
    vmpa2t start;                           /* Tête de lecture initiale    */
    phys_t length;                          /* Nombre d'octets disponibles */
    const bin_t *val;                       /* Valeur lue à faire suivre   */

    content = G_BIN_CONTENT(pygobject_get(self));
    assert(content != NULL);

    init_vmpa(&start, 0, VMPA_NO_VIRTUAL);

    length = g_binary_content_compute_size(content);

    val = g_binary_content_get_raw_access(content, &start, length);
    assert(val != NULL);

    result = PyBytes_FromStringAndSize((char *)val, length);

    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_binary_content_type(void)
{
    static PyMethodDef py_binary_content_methods[] = {
        {
            "read_raw", py_binary_content_read_raw,
            METH_VARARGS,
            "read_raw($self, addr, length, /)\n--\n\nRead bytes from a given position."
        },
        {
            "read_u8", py_binary_content_read_u8,
            METH_VARARGS,
            "read_u8($self, addr, /)\n--\n\nRead an unsigned byte from a given position."
        },
        {
            "read_u16", py_binary_content_read_u16,
            METH_VARARGS,
            "read_u16($self, addr, endianness, /)\n--\n\nRead two unsigned bytes from a given position."
        },
        {
            "read_u32", py_binary_content_read_u32,
            METH_VARARGS,
            "read_u32($self, addr, endianness, /)\n--\n\nRead four unsigned bytes from a given position."
        },
        {
            "read_u64", py_binary_content_read_u64,
            METH_VARARGS,
            "read_u64($self, addr, endianness, /)\n--\n\nRead eight unsigned bytes from a given position."
        },
        { NULL }
    };

    static PyGetSetDef py_binary_content_getseters[] = {
        {
            "checksum", py_binary_content_get_checksum, NULL,
            "Compute a SHA256 hash as chechsum of handled data.", NULL
        },
        {
            "size", py_binary_content_compute_size, NULL,
            "Compute the quantity of readable bytes.", NULL
        },
        {
            "start_pos", py_binary_content_compute_start_pos, NULL,
            "Provide the starting position of the binary content.", NULL
        },
        {
            "end_pos", py_binary_content_compute_end_pos, NULL,
            "Provide the ending position of the binary content.", NULL
        },
        {
            "data", py_binary_content_get_all_bytes, NULL,
            "Provide all the content bytes at once.", NULL
        },
        { NULL }
    };

    static PyTypeObject py_binary_content_type = {

        PyVarObject_HEAD_INIT(NULL, 0)

        .tp_name        = "pychrysalide.analysis.BinContent",
        .tp_basicsize   = sizeof(PyObject),

        .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,

        .tp_doc         = "PyChrysalide binary content",

        .tp_methods     = py_binary_content_methods,
        .tp_getset      = py_binary_content_getseters

    };

    return &py_binary_content_type;

}


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

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

    type = get_python_binary_content_type();

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

        dict = PyModule_GetDict(module);
        pyg_register_interface(dict, "BinContent", G_TYPE_BIN_CONTENT, type);

    }

    return true;

}