summaryrefslogtreecommitdiff
path: root/plugins/dexresolver/operand.c
blob: 72935995ae2b0bc5ca1807179b44bd2d26aaec4f (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

/* OpenIDA - Outil d'analyse de fichiers binaires
 * operand.c - remplacement d'opérandes par d'autres plus explicites
 *
 * Copyright (C) 2010 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "operand.h"


#include <string.h>


#include <arch/operand-int.h>
#include <format/dex/pool.h>



/* ----------------- OPERANDES POINTANT VERS LA TABLE DE CONSTANTES ----------------- */


/* Définition d'un opérande visant un élément de table de constantes Dalvik (instance) */
struct _GDalvikHPoolOperand
{
    GArchOperand parent;                    /* Instance parente            */

    const GDexFormat *format;               /* Format de binaire utilisé   */
    const GDalvikPoolOperand *child;        /* Opérande encapsulé          */

    union
    {
        GBinVariable *field;                /* Champ à représenter         */
        GBinRoutine *method;                /* Méthode à représenter       */

    } cache;

};


/* Définition d'un opérande visant un élément de table de constantes Dalvik (classe) */
struct _GDalvikHPoolOperandClass
{
    GArchOperandClass parent;               /* Classe parente              */

};


/* Initialise la classe des opérandes de constante Dalvik. */
static void g_dalvik_hpool_operand_class_init(GDalvikHPoolOperandClass *);

/* Initialise une instance d'opérande de constante Dalvik. */
static void g_dalvik_hpool_operand_init(GDalvikHPoolOperand *);

/* Ajoute du texte simple à un fichier ouvert en écriture. */
static void g_dalvik_hpool_operand_add_text(const GDalvikHPoolOperand *, GRenderingOptions *, MainRendering, FILE *);

/* Ajoute à un tampon GLib le contenu de l'instance spécifiée. */
static void g_dalvik_hpool_operand_to_buffer(/*const */GDalvikHPoolOperand *, GBufferLine *, GRenderingOptions *);











/* ---------------------------------------------------------------------------------- */
/*                   OPERANDES POINTANT VERS LA TABLE DE CONSTANTES                   */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini par la GLib pour un un élément de table de constantes Dalvik. */
G_DEFINE_TYPE(GDalvikHPoolOperand, g_dalvik_hpool_operand, G_TYPE_ARCH_OPERAND);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des opérandes de constante Dalvik.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_hpool_operand_class_init(GDalvikHPoolOperandClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = instance à initialiser.                            *
*                                                                             *
*  Description : Initialise une instance d'opérande de constante Dalvik.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_hpool_operand_init(GDalvikHPoolOperand *operand)
{
    GContentExporter *parent;               /* Instance parente            */

    parent = G_CONTENT_EXPORTER(operand);

    parent->add_text = (add_text_fc)g_dalvik_hpool_operand_add_text;
    parent->export_buffer = (export_buffer_fc)g_dalvik_hpool_operand_to_buffer;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = représentation interne du format DEX à consulter.   *
*                child  = instance existante à encapsuler.                    *
*                                                                             *
*  Description : Crée un opérande explicitant l'usage d'un élément constant.  *
*                                                                             *
*  Retour      : Opérande mis en place ou NULL si soucis.                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GArchOperand *g_dalvik_hpool_operand_new(const GDexFormat *format, const GDalvikPoolOperand *child)
{
    GDalvikHPoolOperand *result;            /* Structure à retourner       */

    if (g_dalvik_pool_operand_get_pool_type(child) == DPT_NONE)
        return NULL;

    result = g_object_new(G_TYPE_DALVIK_HPOOL_OPERAND, NULL);

    result->format = format;
    result->child = child;

    return G_ARCH_OPERAND(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand   = opérande à transcrire.                           *
*                options   = options de rendu.                                *
*                rendering = support effectif final des lignes de code.       *
*                stream    = flux ouvert en écriture.                         *
*                                                                             *
*  Description : Ajoute du texte simple à un fichier ouvert en écriture.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_hpool_operand_add_text(const GDalvikHPoolOperand *operand, GRenderingOptions *options, MainRendering rendering, FILE *stream)
{
    //g_content_exporter_add_text(G_CONTENT_EXPORTER(operand->reg), options, rendering, stream);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = opérande à transcrire.                             *
*                buffer  = espace où placer ledit contenu.                    *
*                options = options de rendu.                                  *
*                                                                             *
*  Description : Ajoute à un tampon GLib le contenu de l'instance spécifiée.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_hpool_operand_to_buffer(/*const */GDalvikHPoolOperand *operand, GBufferLine *buffer, GRenderingOptions *options)
{
    GContentExporter *exporter;             /* Autre vision de l'opérande  */
    uint32_t index;                         /* Indice de l'élément visé    */
    const char *string;                     /* Chaîne à afficher           */
    GDataType *type;                        /* Type quelconque             */
    char *tmp;                              /* Chaîne à afficher & libérer */

    exporter = G_CONTENT_EXPORTER(operand);

    index = g_dalvik_pool_operand_get_index(operand->child);

    switch (g_dalvik_pool_operand_get_pool_type(operand->child))
    {
        case DPT_STRING:

            string = get_string_from_dex_pool(operand->format, index);

            if (string == NULL)
                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      "<bad_string_index>", 18, RTT_STRING);
            else
            {
                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      "\"", 1, RTT_STRING);

                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      string, strlen(string), RTT_STRING);

                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      "\"", 1, RTT_STRING);

            }

            break;

        case DPT_FIELD:

            if (operand->cache.field == NULL)
                operand->cache.field = get_field_from_dex_pool(operand->format, index);

            if (operand->cache.field == NULL)
                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      "<bad_field_index>", 17, RTT_VAR_NAME);

            else
            {
                type = g_binary_variable_get_vtype(operand->cache.field);
                tmp = g_data_type_to_string(type);

                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      tmp, strlen(tmp), RTT_VAR_NAME);

                free(tmp);

                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      "->", 2, RTT_LTGT);

                string = g_binary_variable_get_name(operand->cache.field);

                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      string, strlen(string), RTT_VAR_NAME);

            }

            break;

        case DPT_METHOD:

            if (operand->cache.method == NULL)
                operand->cache.method = get_routine_from_dex_pool(operand->format, index);

            if (operand->cache.method == NULL)
                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      "<bad_method_index>", 18, RTT_VAR_NAME);

            else
            {
                tmp = g_binary_routine_to_string(operand->cache.method);

                g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                      tmp, strlen(tmp), RTT_VAR_NAME);

                free(tmp);

            }

            break;

        default:
            g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
                                                  "<-?->", 5, RTT_SECTION);
            break;

    }




}