summaryrefslogtreecommitdiff
path: root/src/format/dex/dex.c
blob: 0015357e68e346e815a338d755672ba1b09abf37 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * dex.c - support du format DEX
 *
 * Copyright (C) 2010-2012 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  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 "dex.h"


#include <string.h>


#include "dex-int.h"
#include "pool.h"










/* Initialise la classe des formats d'exécutables DEX. */
static void g_dex_format_class_init(GDexFormatClass *);

/* Initialise une instance de format d'exécutable DEX. */
static void g_dex_format_init(GDexFormat *);

/* Détermine tous les fichiers source indiqués. */
static void g_dex_format_find_all_sources(GDexFormat *);

/* Procède à la décompilation complète du format. */
static void g_dex_format_decompile(const GDexFormat *, GCodeBuffer *, const char *);

/* Indique le type d'architecture visée par le format. */
static const char *g_dex_format_get_target_machine(const GDexFormat *);

/* Fournit les références aux zones binaires à analyser. */
//static GBinPart **g_dex_format_get_parts(const GDexFormat *, size_t *);

/* Fournit la position correspondant à une adresse virtuelle. */
static bool g_dex_format_translate_address_into_offset(const GDexFormat *, vmpa_t, off_t *);

/* Fournit l'adresse virtuelle correspondant à une position. */
static bool g_dex_format_translate_offset_into_address(const GDexFormat *, off_t, vmpa_t *);



/******************************************************************************
*                                                                             *
*  Paramètres  : content = contenu binaire à parcourir.                       *
*                                                                             *
*  Description : Indique si le format peut être pris en charge ici.           *
*                                                                             *
*  Retour      : true si la réponse est positive, false sinon.                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool dex_is_matching(GBinContent *content)
{
    bool result;                            /* Bilan à faire connaître     */
    vmpa2t addr;                            /* Tête de lecture initiale    */
    char magic[DEX_FILE_MAGIC_LEN];         /* Idenfiant standard          */

    init_vmpa(&addr, 0, VMPA_NO_VIRTUAL);

    result = g_binary_content_read_raw(content, &addr, DEX_FILE_MAGIC_LEN, (bin_t *)magic);

    result &= (memcmp(magic, DEX_FILE_MAGIC, DEX_FILE_MAGIC_LEN) == 0);

    return result;

}


/* Indique le type défini pour un format d'exécutable DEX. */
G_DEFINE_TYPE(GDexFormat, g_dex_format, G_TYPE_EXE_FORMAT);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des formats d'exécutables DEX.          *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dex_format_class_init(GDexFormatClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une instance de format d'exécutable DEX.          *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dex_format_init(GDexFormat *format)
{
    GExeFormat *exe_format;                 /* Format parent à compléter #1*/
    GBinFormat *bin_format;                 /* Format parent à compléter #2*/

    bin_format = G_BIN_FORMAT(format);

    bin_format->decompile = (format_decompile_fc)g_dex_format_decompile;

    exe_format = G_EXE_FORMAT(format);

    exe_format->get_machine = (get_target_machine_fc)g_dex_format_get_target_machine;

    exe_format->translate_addr = (translate_addr_fc)g_dex_format_translate_address_into_offset;
    exe_format->translate_off = (translate_off_fc)g_dex_format_translate_offset_into_address;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : content = contenu binaire à parcourir.                       *
*                length  = taille du contenu en question.                     *
*                                                                             *
*  Description : Prend en charge un nouveau format DEX.                       *
*                                                                             *
*  Retour      : Adresse de la structure mise en place ou NULL en cas d'échec.*
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GBinFormat *g_dex_format_new(const bin_t *content, off_t length)
{
    GDexFormat *result;                     /* Structure à retourner       */
    off_t offset;                           /* Tête de lecture             */

    result = g_object_new(G_TYPE_DEX_FORMAT, NULL);

    //g_binary_format_set_content(G_BIN_FORMAT(result), content, length);


    offset = 0;

    if (!read_dex_header(result, &offset, &result->header))
    {
        /* TODO */
        return NULL;
    }

    /* TODO : vérifier l'en-tête ! */



    if (!load_all_dex_classes(result))
    {
        g_object_unref(G_OBJECT(result));
        return NULL;
    }

    register_all_dex_class_methods(result);

    g_dex_format_find_all_sources(result);



    if (!find_all_dex_strings(result))
    {
        g_object_unref(G_OBJECT(result));
        return NULL;
    }


    return G_BIN_FORMAT(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = informations chargées à consulter.                  *
*                addr   = adresse de la routine à retrouver.                  *
*                                                                             *
*  Description : Retrouve si possible la méthode associée à une adresse.      *
*                                                                             *
*  Retour      : Méthde retrouvée ou NULL en cas d'échec.                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDexMethod *g_dex_format_find_method_by_address(const GDexFormat *format, vmpa_t addr)
{
    GDexMethod *result;                     /* Trouvaille à retourner      */
    size_t i;                               /* Boucle de parcours          */

    result = NULL;

    for (i = 0; i < format->classes_count && result == NULL; i++)
        result = g_dex_class_find_method_by_address(format->classes[i], addr);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = informations chargées à mettre à jour.              *
*                                                                             *
*  Description : Détermine tous les fichiers source indiqués.                 *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dex_format_find_all_sources(GDexFormat *format)
{
    GBinFormat *bf;                         /* Instance parente            */
    size_t i;                               /* Boucle de parcours #1       */
    const char *source;                     /* Fichier source trouvé       */
    bool found;                             /* Présence dans la liste      */
    size_t k;                               /* Boucle de parcours #2       */

    bf = G_BIN_FORMAT(format);

    for (i = 0; i < format->classes_count; i++)
    {
        source = g_dex_class_get_source_file(format->classes[i], format);
        if (source == NULL) continue;

        found = false;

        for (k = 0; k < bf->src_count && !found; k++)
            found = (strcmp(source, bf->src_files[k]) == 0);

        if (!found)
        {
            bf->src_files = (const char **)realloc(bf->src_files,
                                                   ++bf->src_count * sizeof(const char **));
            bf->src_files[bf->src_count - 1] = source;
        }

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format   = informations chargées à consulter.                *
*                buffer   = tampon mis à disposition pour la sortie.          *
*                filename = nom du fichier source à cibler.                   *
*                                                                             *
*  Description : Procède à la décompilation complète du format.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dex_format_decompile(const GDexFormat *format, GCodeBuffer *buffer, const char *filename)
{
    GLangOutput *lang;                      /* Langage de sortie           */
    size_t i;                               /* Boucle de parcours          */
    const char *source;                     /* Fichier source trouvé       */

    lang = g_java_output_new();

    for (i = 0; i < format->classes_count; i++)
    {
        source = g_dex_class_get_source_file(format->classes[i], format);
        if (source == NULL || strcmp(source, filename) != 0) continue;

        g_dex_class_decompile(format->classes[i], lang, buffer, format);



#if 0
        GDataType *get_type_from_dex_pool(const GDexFormat *format, uint16_t index)


 Décrit le type fourni sous forme de caractères.              *
*                                                                             *
*  Retour      : Chaîne à libérer de la mémoire après usage.                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

char *_g_data_type_to_string(const GDataType *type, bool simple)

#endif

    }


}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = informations chargées à consulter.                  *
*                                                                             *
*  Description : Indique le type d'architecture visée par le format.          *
*                                                                             *
*  Retour      : Identifiant de l'architecture ciblée par le format.          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static const char *g_dex_format_get_target_machine(const GDexFormat *format)
{
    return "dalvik";

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = informations chargées à consulter.                  *
*                count  = quantité de zones listées. [OUT]                    *
*                                                                             *
*  Description : Fournit les références aux zones binaires à analyser.        *
*                                                                             *
*  Retour      : Zones binaires à analyser.                                   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
#if 0
static GBinPart **g_dex_format_get_parts(const GDexFormat *format, size_t *count)
{
    GBinPart **result;                      /* Tableau à retourner         */
    size_t i;                               /* Boucle de parcours          */

    result = NULL;
    *count = 0;

    for (i = 0; i < format->classes_count; i++)
        result = g_dex_class_get_parts(format->classes[i], result, count);

    return result;

}
#endif

/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                addr   = adresse virtuelle à retrouver.                      *
*                pos    = position correspondante. [OUT]                      *
*                                                                             *
*  Description : Fournit la position correspondant à une adresse virtuelle.   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_dex_format_translate_address_into_offset(const GDexFormat *format, vmpa_t addr, off_t *pos)
{
    bool result;                            /* Bilan à retourner           */

    result = false;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                pos    = position dans le flux binaire à retrouver.          *
*                addr   = adresse virtuelle correspondante. [OUT]             *
*                                                                             *
*  Description : Fournit l'adresse virtuelle correspondant à une position.    *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_dex_format_translate_offset_into_address(const GDexFormat *format, off_t pos, vmpa_t *addr)
{
    bool result;                            /* Bilan à retourner           */

    result = false;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                                                                             *
*  Description : Dénombre le nombre de classes trouvées.                      *
*                                                                             *
*  Retour      : Quantité de classes présentes.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

size_t g_dex_format_count_classes(const GDexFormat *format)
{
    return format->classes_count;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                index  = indice de la classe visée.                          *
*                                                                             *
*  Description : Fournit une classe du format chargée en mémoire.             *
*                                                                             *
*  Retour      : Instance représentant une classe chargée.                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDexClass *g_dex_format_get_class(const GDexFormat *format, size_t index)
{
    return format->classes[index];

}