summaryrefslogtreecommitdiff
path: root/plugins/pychrysa/glibext/codebuffer.c
blob: a090e27d7d9ab19c63945e45599beb9aad3e3e63 (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

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


#include <pygobject.h>


#include "../quirks.h"



/* Retrouve une ligne au sein d'un tampon avec une adresse. */
static PyObject *py_code_buffer_find_line_by_addr(PyObject *, PyObject *);



/******************************************************************************
*                                                                             *
*  Paramètres  : binary = instance existante GLib.                            *
*                                                                             *
*  Description : Crée un nouvel objet Python de type 'Codebuffer'.            *
*                                                                             *
*  Retour      : Instance Python mise en place.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

PyObject *py_code_buffer_from_c(GCodeBuffer *buffer)
{
    PyObject *module;                       /* Module d'appartenance       */
    PyTypeObject *type;                     /* Type Python correspondant   */

    module = PyImport_ImportModule("pychrysalide.glibext");
    type = (PyTypeObject *)PyObject_GetAttrString(module, "CodeBuffer");
    Py_DECREF(module);

    pychrysalide_set_instance_data(G_OBJECT(buffer), type);

    return pygobject_new(G_OBJECT(buffer));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self = classe représentant un tampon de code.                *
*                args = arguments fournis à l'appel.                          *
*                                                                             *
*  Description : Retrouve une ligne au sein d'un tampon avec une adresse.     *
*                                                                             *
*  Retour      : Instance de la ligne trouvée.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_code_buffer_find_line_by_addr(PyObject *self, PyObject *args)
{
    PyObject *result;                       /* Trouvailles à retourner     */
    GCodeBuffer *buffer;                    /* Version native              */
    vmpa_t addr;                            /* Adresse visée par l'opérat° */
    int ret;                                /* Bilan de lecture des args.  */
    GBufferLine *line;                      /* Ligne trouvée               */

    buffer = G_CODE_BUFFER(pygobject_get(self));

    ret = PyArg_ParseTuple(args, "K", &addr);
    if (!ret) return Py_None;

    line = g_code_buffer_find_line_by_addr(buffer, addr);
    if (line == NULL) return Py_None;

    result = py_buffer_line_from_c(line);

    return result;

}


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

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

    static PyMethodDef py_code_buffer_methods[] = {
        {
            "find_line_by_addr", (PyCFunction)py_code_buffer_find_line_by_addr,
            METH_VARARGS,
            "Find a buffer line with a given address."
        },
        { NULL }
    };

    static PyTypeObject py_code_buffer_type = {

        PyObject_HEAD_INIT(NULL)

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

        .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,

        .tp_doc         = "PyChrysalide code buffer",

        .tp_methods     = py_code_buffer_methods

    };

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

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

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

    Py_INCREF(&py_code_buffer_type);
    ret = PyModule_AddObject(module, "CodeBuffer", (PyObject *)&py_code_buffer_type);

    return (ret == 0);

}