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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * ids.c - annotation des références aux chaînes de caractères et identifiants
 *
 * 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "ids.h"


#include <assert.h>
#include <malloc.h>
#include <stdio.h>


#include <i18n.h>
#include <arch/raw.h>
#include <format/symbol.h>
#include <format/dex/dex_def.h>



/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Commente les définitions des chaînes de caractères.          *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool annotate_dex_string_ids(const GDexFormat *format, GtkStatusStack *status)
{
    GBinContent *content;                   /* Contenu binaire à lire      */
    const dex_header *header;               /* En-tête principale          */
    SourceEndian endian;                    /* Boutisme utilisé            */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    activity_id_t msg;                      /* Message de progression      */
    uint32_t i;                             /* Boucle de parcours          */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *text;                             /* Texte constant à insérer    */
    phys_t loc;                             /* Localisation physique       */
    vmpa2t item_pos;                        /* Position d'un élément       */
    vmpa2t start;                           /* Sauvagarde d'une position   */
    uleb128_t length;                       /* Taille de la chaîne en cours*/
    MemoryDataSize leb_size;                /* Taille de la taille         */

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    header = g_dex_format_get_header(format);
    endian = SRE_LITTLE;//g_dex_format_get_endianness(format);

    if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), header->string_ids_off, &pos))
        return false;

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

    for (i = 0; i < header->string_ids_size; i++)
    {
        /* Saut vers la définition */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_HEX);

        asprintf(&text, _("Offset for string item #%u/%u"), i, header->string_ids_size - 1);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        if (i == 0)
            g_binary_symbol_define_as_block_start(symbol, true);

        /* Description de la chaîne : taille */

        operand = g_arch_instruction_get_operand(instr, 0);

        if (!g_imm_operand_to_phys_t(G_IMM_OPERAND(operand), &loc))
            continue;

        if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), loc, &item_pos))
            continue;

        copy_vmpa(&start, &item_pos);

        if (!g_binary_content_read_uleb128(content, &item_pos, &length))
            continue;

        leb_size = MDS_FROM_BYTES(compute_vmpa_diff(&start, &item_pos));
        assert(leb_size != MDS_UNDEFINED);

        instr = g_raw_instruction_new_from_value(&start, leb_size, (uint64_t)length);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("String length"));

        /* Description de la chaîne : contenu */

        if (length > 0)
        {
            instr = g_raw_instruction_new_array(content, MDS_8_BITS, length, &item_pos, endian);

            g_raw_instruction_mark_as_string(G_RAW_INSTRUCTION(instr), true);

            ADD_STR_AS_SYM(format, symbol, instr);

        }

        gtk_status_stack_update_activity_value(status, msg, 1);

    }

    gtk_status_stack_remove_activity(status, msg);

    g_object_unref(G_OBJECT(content));

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Commente les définitions des identifiants de types.          *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool annotate_dex_type_ids(const GDexFormat *format, GtkStatusStack *status)
{
    GBinContent *content;                   /* Contenu binaire à lire      */
    const dex_header *header;               /* En-tête principale          */
    SourceEndian endian;                    /* Boutisme utilisé            */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    activity_id_t msg;                      /* Message de progression      */
    uint32_t i;                             /* Boucle de parcours          */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *text;                             /* Texte constant à insérer    */

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    header = g_dex_format_get_header(format);
    endian = SRE_LITTLE;//g_dex_format_get_endianness(format);

    if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), header->type_ids_off, &pos))
        return false;

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

    for (i = 0; i < header->type_ids_size; i++)
    {
        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the descriptor string of this type"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        if (i == 0)
            g_binary_symbol_define_as_block_start(symbol, true);

        gtk_status_stack_update_activity_value(status, msg, 1);

    }

    gtk_status_stack_remove_activity(status, msg);

    g_object_unref(G_OBJECT(content));

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Commente les définitions des identifiants de prototypes.     *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool annotate_dex_proto_ids(const GDexFormat *format, GtkStatusStack *status)
{
    GBinContent *content;                   /* Contenu binaire à lire      */
    const dex_header *header;               /* En-tête principale          */
    SourceEndian endian;                    /* Boutisme utilisé            */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    activity_id_t msg;                      /* Message de progression      */
    uint32_t i;                             /* Boucle de parcours          */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *text;                             /* Texte constant à insérer    */

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    header = g_dex_format_get_header(format);
    endian = SRE_LITTLE;//g_dex_format_get_endianness(format);

    if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), header->proto_ids_off, &pos))
        return false;

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

    for (i = 0; i < header->proto_ids_size; i++)
    {
        /* shorty_idx */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the short-form descriptor string of this prototype"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        if (i == 0)
            g_binary_symbol_define_as_block_start(symbol, true);

        /* return_type_idx */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the return type of this prototype"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        /* parameters_off */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Offset to the list of parameter types for this prototype"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        gtk_status_stack_update_activity_value(status, msg, 1);

    }

    gtk_status_stack_remove_activity(status, msg);

    g_object_unref(G_OBJECT(content));

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Commente les définitions des identifiants de champs.         *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool annotate_dex_field_ids(const GDexFormat *format, GtkStatusStack *status)
{
    GBinContent *content;                   /* Contenu binaire à lire      */
    const dex_header *header;               /* En-tête principale          */
    SourceEndian endian;                    /* Boutisme utilisé            */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    activity_id_t msg;                      /* Message de progression      */
    uint32_t i;                             /* Boucle de parcours          */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *text;                             /* Texte constant à insérer    */

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    header = g_dex_format_get_header(format);
    endian = SRE_LITTLE;//g_dex_format_get_endianness(format);

    if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), header->field_ids_off, &pos))
        return false;

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

    for (i = 0; i < header->field_ids_size; i++)
    {
        /* class_idx */

        instr = g_raw_instruction_new_array(content, MDS_16_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the definer of this field"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        if (i == 0)
            g_binary_symbol_define_as_block_start(symbol, true);

        /* type_idx */

        instr = g_raw_instruction_new_array(content, MDS_16_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the type of this field"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        /* name_idx */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the name of this field"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        gtk_status_stack_update_activity_value(status, msg, 1);

    }

    gtk_status_stack_remove_activity(status, msg);

    g_object_unref(G_OBJECT(content));

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Commente les définitions des identifiants de méthodes.       *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool annotate_dex_method_ids(const GDexFormat *format, GtkStatusStack *status)
{
    GBinContent *content;                   /* Contenu binaire à lire      */
    const dex_header *header;               /* En-tête principale          */
    SourceEndian endian;                    /* Boutisme utilisé            */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    uint32_t i;                             /* Boucle de parcours          */
    activity_id_t msg;                      /* Message de progression      */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *text;                             /* Texte constant à insérer    */

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    header = g_dex_format_get_header(format);
    endian = SRE_LITTLE;//g_dex_format_get_endianness(format);

    if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), header->method_ids_off, &pos))
        return false;

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

    for (i = 0; i < header->method_ids_size; i++)
    {
        /* class_idx */

        instr = g_raw_instruction_new_array(content, MDS_16_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the definer of this field"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        if (i == 0)
            g_binary_symbol_define_as_block_start(symbol, true);

        /* proto_idx */

        instr = g_raw_instruction_new_array(content, MDS_16_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the prototype of this method"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        /* name_idx */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the name of this method"));

        ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

        free(text);

        gtk_status_stack_update_activity_value(status, msg, 1);

    }

    gtk_status_stack_remove_activity(status, msg);

    g_object_unref(G_OBJECT(content));

    return true;

}