summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/glibext/linegen.c
blob: 3ca7fcc876f030f4e95f6bf83b208cf1b122c7a7 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * linegen.c - équivalent Python du fichier "glibext/linegen.h"
 *
 * 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 "linegen.h"


#include <pygobject.h>


#include <common/cpp.h>
#include <glibext/linegen.h>


#include "../access.h"
#include "../helpers.h"
#include "../analysis/content.h"
#include "../arch/vmpa.h"
#include "../glibext/bufferline.h"



/* Indique le nombre de ligne prêtes à être générées. */
static PyObject *py_line_generator_count_lines(PyObject *, PyObject *);

/* Retrouve l'emplacement correspondant à une position donnée. */
//static PyObject *py_line_generator_compute_addr(PyObject *, PyObject *);

/* Détermine si le conteneur s'inscrit dans une plage donnée. */
//static PyObject *py_line_generator_contains_addr(PyObject *, PyObject *);

/* Renseigne sur les propriétés liées à un générateur. */
static PyObject *py_line_generator_get_flags(PyObject *, PyObject *);

/* Imprime dans une ligne de rendu le contenu représenté. */
static PyObject *py_line_generator_print(PyObject *, PyObject *);



/******************************************************************************
*                                                                             *
*  Paramètres  : self = classe représentant un générateur à manipuler.        *
*                args = arguments fournis à l'appel.                          *
*                                                                             *
*  Description : Indique le nombre de ligne prêtes à être générées.           *
*                                                                             *
*  Retour      : Nombre de lignes devant apparaître au final.                 *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_line_generator_count_lines(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Décompte à retourner        */
    GLineGenerator *generator;              /* Version native              */
    size_t count;                           /* Nombre de lignes présentes  */

    generator = G_LINE_GENERATOR(pygobject_get(self));

    count = g_line_generator_count_lines(generator);

    result = Py_BuildValue("k", count);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = classe représentant un générateur à manipuler.        *
*                args = arguments fournis à l'appel.                          *
*                                                                             *
*  Description : Retrouve l'emplacement correspondant à une position donnée.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
#if 0
static PyObject *py_line_generator_compute_addr(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Localisation à retourner    */
    GLineGenerator *generator;              /* Version native              */
    gint x;                                 /* Position géographique       */
    size_t index;                           /* Indice dans le tampon       */
    size_t repeat;                          /* Utilisations successives    */
    int ret;                                /* Bilan de lecture des args.  */
    vmpa2t addr;                            /* Adresse visée par l'opérat° */

    generator = G_LINE_GENERATOR(pygobject_get(self));

    ret = PyArg_ParseTuple(args, "ikk", &x, &index, &repeat);
    if (!ret) return NULL;

    g_line_generator_compute_addr(generator, x, &addr, index, repeat);

    result = build_from_internal_vmpa(&addr);

    return result;

}
#endif


/******************************************************************************
*                                                                             *
*  Paramètres  : self = classe représentant un générateur à manipuler.        *
*                args = arguments fournis à l'appel.                          *
*                                                                             *
*  Description : Détermine si le conteneur s'inscrit dans une plage donnée.   *
*                                                                             *
*  Retour      : Bilan de la détermination, utilisable en comparaisons.       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
#if 0
static PyObject *py_line_generator_contains_addr(PyObject *self, PyObject *args)
{
    GLineGenerator *generator;              /* Version native              */
    PyObject *py_vmpa;                      /* Localisation version Python */
    size_t index;                           /* Indice dans le tampon       */
    size_t repeat;                          /* Utilisations successives    */
    int ret;                                /* Bilan de lecture des args.  */
    vmpa2t *addr;                           /* Adresse visée par l'opérat° */

    generator = G_LINE_GENERATOR(pygobject_get(self));

    ret = PyArg_ParseTuple(args, "O!kk", get_python_vmpa_type(), &py_vmpa, &index, &repeat);
    if (!ret) return NULL;

    addr = get_internal_vmpa(py_vmpa);
    if (addr == NULL) return NULL;

    g_line_generator_contains_addr(generator, addr, index, repeat);

    Py_RETURN_NONE;

}
#endif


/******************************************************************************
*                                                                             *
*  Paramètres  : self = classe représentant un générateur à manipuler.        *
*                args = arguments fournis à l'appel.                          *
*                                                                             *
*  Description : Renseigne sur les propriétés liées à un générateur.          *
*                                                                             *
*  Retour      : Propriétés particulières associées.                          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_line_generator_get_flags(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Propriétés à retourner      */
    GLineGenerator *generator;              /* Version native              */
    size_t index;                           /* Indice dans le tampon       */
    size_t repeat;                          /* Utilisations successives    */
    int ret;                                /* Bilan de lecture des args.  */
    BufferLineFlags flags;                  /* Propriétés courantes        */

    generator = G_LINE_GENERATOR(pygobject_get(self));

    ret = PyArg_ParseTuple(args, "kk", &index, &repeat);
    if (!ret) return NULL;

    flags = g_line_generator_get_flags(generator, index, repeat);

    result = Py_BuildValue("I", flags);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = classe représentant un générateur à manipuler.        *
*                args = arguments fournis à l'appel.                          *
*                                                                             *
*  Description : Imprime dans une ligne de rendu le contenu représenté.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_line_generator_print(PyObject *self, PyObject *args)
{
    GLineGenerator *generator;              /* Version native              */
    GBufferLine *line;                      /* Ligne de rendu à compléter  */
    size_t index;                           /* Indice dans le tampon       */
    size_t repeat;                          /* Utilisations successives    */
    GBinContent *content;                   /* Contenu binaire associé     */
    int ret;                                /* Bilan de lecture des args.  */

    generator = G_LINE_GENERATOR(pygobject_get(self));

    ret = PyArg_ParseTuple(args, "O&kkO&", convert_to_buffer_line, &line, &index,
                           &repeat, convert_to_binary_content, &content);
    if (!ret) return NULL;

    g_line_generator_print(generator, line, index, repeat, content);

    Py_RETURN_NONE;

}













#if 0

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

static void python_line_generator_interface_init(GLineGeneratorIface *iface, PyTypeObject *type)
{
    GLineGeneratorIface *parent;            /* Défintion parente           */
    size_t i;                               /* Boucle de parcours          */
    PyObject *method;                       /* Méthode à associer          */

    static const char *meth_names[] = {
        "count_lines",
        "compute_addr",
        "contains_addr",
        "get_flags",
        "print"
    };

    parent = g_type_interface_peek_parent(iface);

    for (i = 0; i < ARRAY_SIZE(meth_names); i++)
    {
        method = NULL;

        if (type != NULL)
            method = PyObject_GetAttrString((PyObject *)type, meth_names[i]);

        if (method != NULL && PyObject_TypeCheck(method, &PyCFunction_Type) == 0)
            /*iface->iface_method = _wrap_TestInterface__proxy_do_iface_method*/;

        else
        {
            PyErr_Clear();

            if (parent != NULL)
                /*iface->iface_method = parent->iface_method*/;

        }

        Py_XDECREF(method);

    }

}


#endif




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

PyTypeObject *get_python_line_generator_type(void)
{
    static PyMethodDef py_line_generator_methods[] = {
        {
            "count_lines", py_line_generator_count_lines,
            METH_NOARGS,
            "count_lines($self, /)\n--\n\nCount the number of lines which can be displayed."
        },
#if 0
        {
            "compute_addr", py_line_generator_compute_addr,
            METH_VARARGS,
            "compute_addr($self, x, index, repeat, /)\n--\n\nReturn the position at a given location."
        },
        {
            "contains_addr", py_line_generator_contains_addr,
            METH_VARARGS,
            "contains_addr($self, addr, index, repeat, /)\n--\n\nTell if the generator contains an address."
        },
#endif
        {
            "get_flags", py_line_generator_get_flags,
            METH_VARARGS,
            "get_flags($self, index, repeat, /)\n--\n\nGet the flags of a position from the generator."
        },
        {
            "print", py_line_generator_print,
            METH_VARARGS,
            "print($self, line, index, repeat, content, /)\n--\n\nProduce output into a line from content."
        },
        { NULL }
    };

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

    static PyTypeObject py_line_generator_type = {

        PyVarObject_HEAD_INIT(NULL, 0)

        .tp_name        = "pychrysalide.glibext.LineGenerator",
        //.tp_basicsize   = sizeof(PyGObject),

        .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,

        .tp_doc         = "PyChrysalide line content generator",

        .tp_methods     = py_line_generator_methods,
        .tp_getset      = py_line_generator_getseters,

    };

    return &py_line_generator_type;

}


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

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

    type = get_python_line_generator_type();

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

        dict = PyModule_GetDict(module);

        if (!register_interface_for_pygobject(dict, G_TYPE_LINE_GENERATOR, type))
            return false;

    }

    return true;

}