diff options
Diffstat (limited to 'src/format/dex/dex.c')
-rwxr-xr-x | src/format/dex/dex.c | 364 |
1 files changed, 222 insertions, 142 deletions
diff --git a/src/format/dex/dex.c b/src/format/dex/dex.c index 0015357..35ffe47 100755 --- a/src/format/dex/dex.c +++ b/src/format/dex/dex.c @@ -45,49 +45,81 @@ 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 *); +/* Supprime toutes les références externes. */ +static void g_dex_format_dispose(GDexFormat *); -/* Procède à la décompilation complète du format. */ -static void g_dex_format_decompile(const GDexFormat *, GCodeBuffer *, const char *); +/* Procède à la libération totale de la mémoire. */ +static void g_dex_format_finalize(GDexFormat *); /* 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 *); +/* Etend la définition des portions au sein d'un binaire. */ +static void g_dex_format_refine_portions(const GDexFormat *, GBinPortion *); + +/* Fournit l'emplacement d'une section donnée. */ +static bool g_dex_format_get_section_range_by_name(const GDexFormat *, const char *, mrange_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 *); +/* 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 *); /****************************************************************************** * * * Paramètres : content = contenu binaire à parcourir. * +* parent = éventuel format exécutable déjà chargé. * +* unused = adresse non utilisée ici. * +* key = identifiant de format trouvé ou NULL. [OUT] * * * * Description : Indique si le format peut être pris en charge ici. * * * -* Retour : true si la réponse est positive, false sinon. * +* Retour : Conclusion de haut niveau sur la reconnaissance effectuée. * * * * Remarques : - * * * ******************************************************************************/ -bool dex_is_matching(GBinContent *content) +FormatMatchStatus dex_is_matching(GBinContent *content, GExeFormat *parent, void *unused, char **key) { - bool result; /* Bilan à faire connaître */ + FormatMatchStatus result; /* Bilan à renvoyer */ vmpa2t addr; /* Tête de lecture initiale */ + bool status; /* Bilan des accès mémoire */ char magic[DEX_FILE_MAGIC_LEN]; /* Idenfiant standard */ + + /* REMME */ + if (parent != NULL) return FMS_UNKNOWN; + + init_vmpa(&addr, 0, VMPA_NO_VIRTUAL); - result = g_binary_content_read_raw(content, &addr, DEX_FILE_MAGIC_LEN, (bin_t *)magic); + status = 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); + status &= (memcmp(magic, DEX_FILE_MAGIC, DEX_FILE_MAGIC_LEN) == 0); + + if (status) + { + result = FMS_MATCHED; + *key = strdup(parent == NULL ? "dex" : "dexdbg"); + } + else + result = FMS_UNKNOWN; return result; @@ -112,6 +144,23 @@ G_DEFINE_TYPE(GDexFormat, g_dex_format, G_TYPE_EXE_FORMAT); static void g_dex_format_class_init(GDexFormatClass *klass) { + GObjectClass *object; /* Autre version de la classe */ + GExeFormatClass *exe; /* Version en exécutable */ + + object = G_OBJECT_CLASS(klass); + + object->dispose = (GObjectFinalizeFunc/* ! */)g_dex_format_dispose; + object->finalize = (GObjectFinalizeFunc)g_dex_format_finalize; + + exe = G_EXE_FORMAT_CLASS(klass); + + exe->get_machine = (get_target_machine_fc)g_dex_format_get_target_machine; + exe->refine_portions = (refine_portions_fc)g_dex_format_refine_portions; + + exe->translate_phys = (translate_phys_fc)g_exe_format_without_virt_translate_offset_into_vmpa; + exe->translate_virt = (translate_virt_fc)g_exe_format_without_virt_translate_address_into_vmpa; + + exe->get_range_by_name = (get_range_by_name_fc)g_dex_format_get_section_range_by_name; } @@ -130,19 +179,49 @@ static void g_dex_format_class_init(GDexFormatClass *klass) static void g_dex_format_init(GDexFormat *format) { - GExeFormat *exe_format; /* Format parent à compléter #1*/ - GBinFormat *bin_format; /* Format parent à compléter #2*/ + GBinFormat *bin_format; /* Format parent à compléter #1*/ 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 : format = instance d'objet GLib à traiter. * +* * +* Description : Supprime toutes les références externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_dex_format_dispose(GDexFormat *format) +{ + G_OBJECT_CLASS(g_dex_format_parent_class)->dispose(G_OBJECT(format)); + +} + + +/****************************************************************************** +* * +* Paramètres : format = instance d'objet GLib à traiter. * +* * +* Description : Procède à la libération totale de la mémoire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_dex_format_finalize(GDexFormat *format) +{ + G_OBJECT_CLASS(g_dex_format_parent_class)->finalize(G_OBJECT(format)); } @@ -150,9 +229,9 @@ static void g_dex_format_init(GDexFormat *format) /****************************************************************************** * * * Paramètres : content = contenu binaire à parcourir. * -* length = taille du contenu en question. * +* parent = éventuel format exécutable déjà chargé. * * * -* Description : Prend en charge un nouveau format DEX. * +* Description : Prend en charge un nouveau format Dex. * * * * Retour : Adresse de la structure mise en place ou NULL en cas d'échec.* * * @@ -160,54 +239,127 @@ static void g_dex_format_init(GDexFormat *format) * * ******************************************************************************/ -GBinFormat *g_dex_format_new(const bin_t *content, off_t length) +GBinFormat *g_dex_format_new(GBinContent *content, GExeFormat *parent) { GDexFormat *result; /* Structure à retourner */ - off_t offset; /* Tête de lecture */ + vmpa2t pos; /* Position de tête de lecture */ result = g_object_new(G_TYPE_DEX_FORMAT, NULL); - //g_binary_format_set_content(G_BIN_FORMAT(result), content, length); + g_binary_format_set_content(G_BIN_FORMAT(result), content); + init_vmpa(&pos, 0, VMPA_NO_VIRTUAL); - offset = 0; + if (!read_dex_header(result, &pos, &result->header)) + goto gdfn_error; - if (!read_dex_header(result, &offset, &result->header)) - { - /* TODO */ - return NULL; - } + if (!load_all_dex_types(result)) + goto gdfn_error; - /* TODO : vérifier l'en-tête ! */ + if (!load_all_dex_fields(result)) + goto gdfn_error; + if (!load_all_dex_prototypes(result)) + goto gdfn_error; + if (!load_all_dex_methods(result)) + goto gdfn_error; if (!load_all_dex_classes(result)) - { - g_object_unref(G_OBJECT(result)); - return NULL; - } + goto gdfn_error; - register_all_dex_class_methods(result); + return G_BIN_FORMAT(result); - g_dex_format_find_all_sources(result); + gdfn_error: + g_object_unref(G_OBJECT(result)); + exit(0); - if (!find_all_dex_strings(result)) - { - g_object_unref(G_OBJECT(result)); - return NULL; - } + return NULL; +} - return G_BIN_FORMAT(result); + +/****************************************************************************** +* * +* 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. * +* raw = portion de binaire brut à raffiner. * +* * +* Description : Etend la définition des portions au sein d'un binaire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_dex_format_refine_portions(const GDexFormat *format, GBinPortion *raw) +{ + size_t max; /* Nombre d'itérations prévues */ + size_t i; /* Boucle de parcours */ + + max = g_dex_format_count_classes(format); + + for (i = 0; i < max; i++) + g_dex_class_include_as_portion(format->classes[i], raw); } /****************************************************************************** * * +* Paramètres : format = description de l'exécutable à consulter. * +* name = nom de la section recherchée. * +* range = emplacement en mémoire à renseigner. [OUT] * +* * +* Description : Fournit l'emplacement d'une section donnée. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_dex_format_get_section_range_by_name(const GDexFormat *format, const char *name, mrange_t *range) +{ + bool result; /* Bilan à retourner */ + + result = false; + + return result; + +} + + + + + + + + + + +/****************************************************************************** +* * * Paramètres : format = informations chargées à consulter. * * addr = adresse de la routine à retrouver. * * * @@ -221,6 +373,11 @@ GBinFormat *g_dex_format_new(const bin_t *content, off_t length) GDexMethod *g_dex_format_find_method_by_address(const GDexFormat *format, vmpa_t addr) { + + return NULL; + + +#if 0 GDexMethod *result; /* Trouvaille à retourner */ size_t i; /* Boucle de parcours */ @@ -230,6 +387,7 @@ GDexMethod *g_dex_format_find_method_by_address(const GDexFormat *format, vmpa_t result = g_dex_class_find_method_by_address(format->classes[i], addr); return result; +#endif } @@ -245,9 +403,12 @@ GDexMethod *g_dex_format_find_method_by_address(const GDexFormat *format, vmpa_t * Remarques : - * * * ******************************************************************************/ - +#if 0 static void g_dex_format_find_all_sources(GDexFormat *format) { + +#if 0 + GBinFormat *bf; /* Instance parente */ size_t i; /* Boucle de parcours #1 */ const char *source; /* Fichier source trouvé */ @@ -275,8 +436,10 @@ static void g_dex_format_find_all_sources(GDexFormat *format) } -} +#endif +} +#endif /****************************************************************************** * * @@ -294,6 +457,9 @@ static void g_dex_format_find_all_sources(GDexFormat *format) static void g_dex_format_decompile(const GDexFormat *format, GCodeBuffer *buffer, const char *filename) { + +#if 0 + GLangOutput *lang; /* Langage de sortie */ size_t i; /* Boucle de parcours */ const char *source; /* Fichier source trouvé */ @@ -328,106 +494,11 @@ char *_g_data_type_to_string(const GDataType *type, bool simple) } -} - - -/****************************************************************************** -* * -* 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; - -} - /****************************************************************************** * * @@ -443,7 +514,11 @@ static bool g_dex_format_translate_offset_into_address(const GDexFormat *format, size_t g_dex_format_count_classes(const GDexFormat *format) { + return 0; + +#if 0 return format->classes_count; +#endif } @@ -466,3 +541,8 @@ GDexClass *g_dex_format_get_class(const GDexFormat *format, size_t index) return format->classes[index]; } + + + + + |