summaryrefslogtreecommitdiff
path: root/plugins/readdex/class.c
blob: c414821e748eabbc619091594f49eab75a9b6bea (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * class.c - annotation des définitions de classes
 *
 * Copyright (C) 2016-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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "class.h"


#include <i18n.h>
#include <plugins/dex/class.h>
#include <plugins/dex/dex_def.h>
#include <plugins/fmtp/parser.h>


#include "code.h"



/* Définition des champs */

static fmt_field_def _dex_class_defs[] = {

    {
        .name = "class_idx",

        .size = MDS_32_BITS,
        .repeat = 1,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Index into the type_ids list for this class"))

    },

    {
        .name = "access_flags",

        .size = MDS_32_BITS,
        .repeat = 1,

        PLAIN_COMMENT(__("Access flags for the class"))

    },

    {
        .name = "superclass_idx",

        .size = MDS_32_BITS,
        .repeat = 1,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Index for the superclass or NO_INDEX if this class has no superclass"))

    },

    {
        .name = "interfaces_off",

        .size = MDS_32_BITS,
        .repeat = 1,

        PLAIN_COMMENT(__("Offset to the list of interfaces"))

    },

    {
        .name = "source_file_idx",

        .size = MDS_32_BITS,
        .repeat = 1,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Index for the name of the file containing the original source or NO_INDEX"))

    },

    {
        .name = "annotations_off",

        .size = MDS_32_BITS,
        .repeat = 1,

        PLAIN_COMMENT(__("Offset to the annotations structure for this class"))

    },

    {
        .name = "class_data_off",

        .size = MDS_32_BITS,
        .repeat = 1,

        PLAIN_COMMENT(__("Offset to the associated class data for this item"))

    },

    {
        .name = "static_values_off",

        .size = MDS_32_BITS,
        .repeat = 1,

        PLAIN_COMMENT(__("Offset to the list of initial values for static fields"))

    }

};

static fmt_field_def _dex_class_data[] = {

    {
        .name = "static_fields_size",

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of static fields defined in this item"))

    },

    {
        .name = "instance_fields_size",

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of instance fields defined in this item"))

    },

    {
        .name = "direct_methods_size",

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of direct methods defined in this item"))

    },

    {
        .name = "virtual_methods_size",

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of virtual methods defined in this item"))

    }

};

static fmt_field_def _dex_encoded_field[] = {

    {
        .name = "field_idx_diff",

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Index into the field_ids list for the identity of this field"))

    },

    {
        .name = "access_flags",

        .is_uleb128 = true,

        PLAIN_COMMENT(__("Access flags for the field"))

    }

};

static fmt_field_def _dex_encoded_method[] = {

    {
        .name = "method_idx_diff",

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Index into the method_ids list for the identity of this method"))

    },

    {
        .name = "access_flags",

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Access flags for the method"))

    },

    {
        .name = "code_off",

        .is_uleb128 = true,

        PLAIN_COMMENT(__("Offset to the code structure for this method"))

    }

};



/* Commente les définitions des classes pour la VM Dalvik. */
static bool annotate_dex_class_data(const GDexFormat *, GPreloadInfo *, const GDexClass *, uint32_t );

/* Commente les définitions des champs encodés. */
static bool annotate_dex_encoded_field(const GDexFormat *, GPreloadInfo *, vmpa2t *);

/* Commente les définitions des méthodes encodées. */
static bool annotate_dex_encoded_method(const GDexFormat *, GPreloadInfo *, const encoded_method *, vmpa2t *);



/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                info   = informations à constituer en avance de phase.       *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Commente les définitions des classes pour la VM Dalvik.      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool annotate_dex_class_defs(const GDexFormat *format, GPreloadInfo *info, GtkStatusStack *status)
{
    bool result;                            /* Bilan à retourner           */
    const dex_header *header;               /* En-tête principale          */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    activity_id_t msg;                      /* Message de progression      */
    GBinFormat *bformat;                    /* Autre version du format     */
    uint32_t i;                             /* Boucle de parcours          */
    GDexClass *class;                       /* Classe chargée à manipuler  */
    const class_def_item *def;              /* Définition brute à lire     */

    header = g_dex_format_get_header(format);

    result = g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), header->class_defs_off, &pos);

    if (!result)
        goto adcd_exit;

    msg = gtk_status_stack_add_activity(status, _("Writing annotations for all Dex classes..."),
                                        header->class_defs_size);

    bformat = G_BIN_FORMAT(format);

    for (i = 0; i < header->class_defs_size && result; i++)
    {
        result = parse_field_definitions(PARSING_DEFS(_dex_class_defs), bformat, info, &pos, NULL);
        if (!result) break;

        /* Annotations supplémentaires */

        class = g_dex_format_get_class(format, i);

        def = g_dex_class_get_definition(class);

        if (def->class_data_off > 0)
            result = annotate_dex_class_data(format, info, class, def->class_data_off);

        g_object_unref(G_OBJECT(class));

        gtk_status_stack_update_activity_value(status, msg, 1);

    }

    gtk_status_stack_remove_activity(status, msg);

 adcd_exit:

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                info   = informations à constituer en avance de phase.       *
*                class  = classe Dex dont les données sont à commenter.       *
*                offset = tête de lecture physique des symboles.              *
*                                                                             *
*  Description : Commente les définitions des classes pour la VM Dalvik.      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_class_data(const GDexFormat *format, GPreloadInfo *info, const GDexClass *class, uint32_t offset)
{
    bool result;                            /* Bilan à retourner           */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    GBinFormat *bformat;                    /* Autre version du format     */
    const class_data_item *data;            /* Données chargées à lire     */
    uleb128_t i;                            /* Boucle de parcours          */

    result = g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), offset, &pos);

    if (!result)
        goto adcd_exit;

    bformat = G_BIN_FORMAT(format);

    result = parse_field_definitions(PARSING_DEFS(_dex_class_data), bformat, info, &pos, NULL);

    if (!result)
        goto adcd_exit;

    /* Chargements complémentaires */

    data = g_dex_class_get_data(class);

    if (data != NULL)
    {
        for (i = 0; i < data->static_fields_size && result; i++)
            result = annotate_dex_encoded_field(format, info, &pos);

        for (i = 0; i < data->instance_fields_size && result; i++)
            result = annotate_dex_encoded_field(format, info, &pos);

        for (i = 0; i < data->direct_methods_size && result; i++)
            result = annotate_dex_encoded_method(format, info, &data->direct_methods[i], &pos);

        for (i = 0; i < data->virtual_methods_size && result; i++)
            result = annotate_dex_encoded_method(format, info, &data->virtual_methods[i], &pos);

    }

 adcd_exit:

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                info   = informations à constituer en avance de phase.       *
*                pos    = tête de lecture à faire progresser. [OUT]           *
*                                                                             *
*  Description : Commente les définitions des champs encodés.                 *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_encoded_field(const GDexFormat *format, GPreloadInfo *info, vmpa2t *pos)
{
    bool result;                            /* Bilan à retourner           */
    GBinFormat *bformat;                    /* Autre version du format     */

    bformat = G_BIN_FORMAT(format);

    result = parse_field_definitions(PARSING_DEFS(_dex_encoded_field), bformat, info, pos, NULL);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                info   = informations à constituer en avance de phase.       *
*                method = méthode à décrire.                                  *
*                pos    = tête de lecture à faire progresser. [OUT]           *
*                                                                             *
*  Description : Commente les définitions des méthodes encodées.              *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_encoded_method(const GDexFormat *format, GPreloadInfo *info, const encoded_method *method, vmpa2t *pos)
{
    bool result;                            /* Bilan à retourner           */
    GBinFormat *bformat;                    /* Autre version du format     */

    bformat = G_BIN_FORMAT(format);

    result = parse_field_definitions(PARSING_DEFS(_dex_encoded_method), bformat, info, pos, NULL);

    /* Chargements complémentaires, si non abstraite ni native */

    if (result && method->code_off > 0)
        result = annotate_dex_code_item(format, info, method->code_off);

    return result;

}