/* Chrysalide - Outil d'analyse de fichiers binaires * switcher.c - gestion des basculements d'affichage d'opérandes numériques * * Copyright (C) 2015-2019 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 . */ #include "switcher.h" #include #include #include #include #include "../collection-int.h" #include "../item-int.h" /* --------------------- ELABORATION D'UN ELEMENT DE COLLECTION --------------------- */ /* Bascule d'affichage pour un opérande numérique (instance) */ struct _GDbSwitcher { GDbItem parent; /* A laisser en premier */ vmpa2t addr; /* Adresse de l'instruction */ size_t index; /* Indice de l'opérande visé */ ImmOperandDisplay display; /* Type de bascule */ ImmOperandDisplay old; /* Type de bascule précédente */ }; /* Bascule d'affichage pour un opérande numérique (classe) */ struct _GDbSwitcherClass { GDbItemClass parent; /* A laisser en premier */ }; /* Initialise la classe des bascules d'affichage numérique. */ static void g_db_switcher_class_init(GDbSwitcherClass *); /* Initialise une bascule d'affichage pour opérande numérique. */ static void g_db_switcher_init(GDbSwitcher *); /* Supprime toutes les références externes. */ static void g_db_switcher_dispose(GDbSwitcher *); /* Procède à la libération totale de la mémoire. */ static void g_db_switcher_finalize(GDbSwitcher *); /* Effectue la comparaison entre deux signets de collection. */ static gint g_db_switcher_cmp(const GDbSwitcher *, const GDbSwitcher *); /* Importe la définition d'un signet depuis un flux réseau. */ static bool g_db_switcher_unpack(GDbSwitcher *, packed_buffer *); /* Exporte la définition d'un signet dans un flux réseau. */ static bool g_db_switcher_pack(const GDbSwitcher *, packed_buffer *); /* Construit la description humaine d'un signet sur un tampon. */ static char *g_db_switcher_build_label(GDbSwitcher *); /* Exécute une bascule d'affichage d'opérande sur un binaire. */ static bool g_db_switcher_run(GDbSwitcher *, GLoadedBinary *, ImmOperandDisplay *, ImmOperandDisplay); /* Applique une bascule d'affichage d'opérande sur un binaire. */ static bool g_db_switcher_apply(GDbSwitcher *, GLoadedBinary *); /* Annule une bascule d'affichage d'opérande sur un binaire. */ static bool g_db_switcher_cancel(GDbSwitcher *, GLoadedBinary *); /* Charge les valeurs utiles pour un basculement d'affichage. */ static bool g_db_switcher_load(GDbSwitcher *, const bound_value *, size_t); /* Constitue les champs destinés à une insertion / modification. */ static bool g_db_switcher_store(const GDbSwitcher *, bound_value **, size_t *); /* ---------------------- DEFINITION DE LA COLLECTION ASSOCIEE ---------------------- */ /* Collection dédiée aux basculements d'affichage (instance) */ struct _GSwitcherCollection { GDbCollection parent; /* A laisser en premier */ }; /* Collection dédiée aux basculements d'affichage (classe) */ struct _GSwitcherCollectionClass { GDbCollectionClass parent; /* A laisser en premier */ }; /* Initialise la classe des signets dans une zone de texte. */ static void g_switcher_collection_class_init(GSwitcherCollectionClass *); /* Initialise un signet dans une zone de texte. */ static void g_switcher_collection_init(GSwitcherCollection *); /* Supprime toutes les références externes. */ static void g_switcher_collection_dispose(GSwitcherCollection *); /* Procède à la libération totale de la mémoire. */ static void g_switcher_collection_finalize(GSwitcherCollection *); /* Crée la table des basculements dans une base de données. */ static bool g_switcher_collection_create_db_table(const GSwitcherCollection *, sqlite3 *); /* ---------------------------------------------------------------------------------- */ /* ELABORATION D'UN ELEMENT DE COLLECTION */ /* ---------------------------------------------------------------------------------- */ /* Indique le type défini pour un signet à l'intérieur d'une zone de texte. */ G_DEFINE_TYPE(GDbSwitcher, g_db_switcher, G_TYPE_DB_ITEM); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des bascules d'affichage numérique. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_switcher_class_init(GDbSwitcherClass *klass) { GObjectClass *object; /* Autre version de la classe */ GDbItemClass *item; /* Encore une autre vision... */ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_db_switcher_dispose; object->finalize = (GObjectFinalizeFunc)g_db_switcher_finalize; item = G_DB_ITEM_CLASS(klass); item->feature = DBF_DISPLAY_SWITCHERS; item->cmp = (cmp_db_item_fc)g_db_switcher_cmp; item->unpack = (unpack_db_item_fc)g_db_switcher_unpack; item->pack = (pack_db_item_fc)g_db_switcher_pack; item->build_label = (build_item_label_fc)g_db_switcher_build_label; item->apply = (run_item_fc)g_db_switcher_apply; item->cancel = (run_item_fc)g_db_switcher_cancel; item->load = (load_db_item_fc)g_db_switcher_load; item->store = (store_db_item_fc)g_db_switcher_store; } /****************************************************************************** * * * Paramètres : switcher = instance à initialiser. * * * * Description : Initialise une bascule d'affichage pour opérande numérique. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_switcher_init(GDbSwitcher *switcher) { } /****************************************************************************** * * * Paramètres : switcher = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_switcher_dispose(GDbSwitcher *switcher) { G_OBJECT_CLASS(g_db_switcher_parent_class)->dispose(G_OBJECT(switcher)); } /****************************************************************************** * * * Paramètres : switcher = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_db_switcher_finalize(GDbSwitcher *switcher) { G_OBJECT_CLASS(g_db_switcher_parent_class)->finalize(G_OBJECT(switcher)); } /****************************************************************************** * * * Paramètres : addr = adresse inamovible localisant une position donnée. * * comment = commentaire construit ou NULL. * * * * Description : Crée une définition d'un signet dans une zone de texte. * * * * Retour : Signet mis en place ou NULL en cas d'erreur. * * * * Remarques : - * * * ******************************************************************************/ GDbSwitcher *g_db_switcher_new(GArchInstruction *instr, const GImmOperand *imm, ImmOperandDisplay display) { GDbSwitcher *result; /* Instance à retourner */ size_t count; /* Nombre d'opérandes à visiter*/ size_t i; /* Boucle de parcours */ GArchOperand *op; /* Opérande manipulé */ const mrange_t *range; /* Localisation de l'instruct° */ /* Recherche de la position de l'opérande */ g_arch_instruction_lock_operands(instr); count = _g_arch_instruction_count_operands(instr); for (i = 0; i < count; i++) { op = _g_arch_instruction_get_operand(instr, i); if (G_ARCH_OPERAND(imm) == op) { g_object_unref(G_OBJECT(op)); break; } else g_object_unref(G_OBJECT(op)); } g_arch_instruction_unlock_operands(instr); if (i == count) return NULL; /* Sauvegarde des propriétés */ result = g_object_new(G_TYPE_DB_SWITCHER, NULL); range = g_arch_instruction_get_range(instr); copy_vmpa(&result->addr, get_mrange_addr(range)); result->index = i; result->display = display; /* Création d'un intitulé adapté */ //g_db_switcher_set_comment(result, comment); return result; } /****************************************************************************** * * * Paramètres : a = premier élément à analyser. * * b = second élément à analyser. * * * * Description : Effectue la comparaison entre deux signets de collection. * * * * Retour : Bilan de la comparaison : -1, 0 ou 1. * * * * Remarques : - * * * ******************************************************************************/ static gint g_db_switcher_cmp(const GDbSwitcher *a, const GDbSwitcher *b) { gint result; /* Bilan de la comparaison */ result = cmp_vmpa(&a->addr, &b->addr); if (result == 0) { if (a->index < b->index) result = -1; else if (a->index > b->index) result = 1; } if (result == 0) { if (a->display < b->display) result = -1; else if (a->display > b->display) result = 1; } return result; } /****************************************************************************** * * * Paramètres : switcher = bascule d'affichage aux infos à charger. [OUT] * * pbuf = paquet de données où venir inscrire les infos. * * * * Description : Importe la définition d'un signet depuis un flux réseau. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_switcher_unpack(GDbSwitcher *switcher, packed_buffer *pbuf) { bool result; /* Bilan à retourner */ uint32_t tmp32; /* Valeur sur 32 bits */ result = G_DB_ITEM_CLASS(g_db_switcher_parent_class)->unpack(G_DB_ITEM(switcher), pbuf); if (result) result = unpack_vmpa(&switcher->addr, pbuf); if (result) { result = extract_packed_buffer(pbuf, &tmp32, sizeof(uint32_t), true); switcher->index = tmp32; } if (result) { result = extract_packed_buffer(pbuf, &tmp32, sizeof(uint32_t), true); switcher->display = tmp32; if (switcher->display > IOD_COUNT) result = false; } return result; } /****************************************************************************** * * * Paramètres : switcher = bascule d'affichage aux infos à sauvegarder. * * pbuf = paquet de données où venir inscrire les infos. * * * * Description : Exporte la définition d'une bascule d'affichage d'opérande. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_switcher_pack(const GDbSwitcher *switcher, packed_buffer *pbuf) { bool result; /* Bilan à retourner */ result = G_DB_ITEM_CLASS(g_db_switcher_parent_class)->pack(G_DB_ITEM(switcher), pbuf); if (result) result = pack_vmpa(&switcher->addr, pbuf); if (result) result = extend_packed_buffer(pbuf, (uint32_t []) { switcher->index }, sizeof(uint32_t), true); if (result) result = extend_packed_buffer(pbuf, (uint32_t []) { switcher->display }, sizeof(uint32_t), true); return result; } /****************************************************************************** * * * Paramètres : switcher = bascule d'affichage à manipuler. * * * * Description : Construit la description humaine d'un signet sur un tampon. * * * * Retour : Description humaine mise en place à libérer après usage. * * * * Remarques : - * * * ******************************************************************************/ static char *g_db_switcher_build_label(GDbSwitcher *switcher) { #if 0 VMPA_BUFFER(loc); /* Indication de position */ vmpa2_to_string(&switcher->addr, MDS_UNDEFINED, loc, NULL); switch (switcher->display) { case IOD_BIN: asprintf(&G_DB_ITEM(switcher)->label, _("Switch to binary display at %s"), loc); break; case IOD_OCT: asprintf(&G_DB_ITEM(switcher)->label, _("Switch to octal display at %s"), loc); break; case IOD_DEC: asprintf(&G_DB_ITEM(switcher)->label, _("Switch to octal display at %s"), loc); break; case IOD_HEX: asprintf(&G_DB_ITEM(switcher)->label, _("Switch to octal display at %s"), loc); break; case IOD_COUNT: asprintf(&G_DB_ITEM(switcher)->label, _("Reset to default display at %s"), loc); break; default: assert(false); break; } #endif return NULL; } /****************************************************************************** * * * Paramètres : switcher = bascule d'affichage à manipuler. * * binary = binaire chargé en mémoire à modifier. * * old = état précédent à conserver. [OUT] * * new = nouvel état à appliquer. * * * * Description : Exécute une bascule d'affichage d'opérande sur un binaire. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_switcher_run(GDbSwitcher *switcher, GLoadedBinary *binary, ImmOperandDisplay *old, ImmOperandDisplay new) { return false; #if 0 bool result; /* Bilan à faire remonter */ GArchProcessor *proc; /* Propriétaire d'instructions */ GArchInstruction *instr; /* Instruction à traiter */ GArchOperand *op; /* Opérande à modifier */ GCodeBuffer *buffer; /* Tampon de lignes à traiter */ GBufferLine *line; /* Ligne de tampon à marquer */ GImmOperand *operand; /* Opérande de valeur immédiate*/ char value[IMM_MAX_SIZE]; /* Chaîne à imprimer */ size_t len; /* Taille de l'élément inséré */ result = true; /* Traitement au niveau des instructions */ proc = g_loaded_binary_get_processor(binary); instr = g_arch_processor_find_instr_by_address(proc, &switcher->addr); if (instr == NULL) { result = false; goto exit_instr; } op = g_arch_instruction_get_operand(instr, switcher->index); if (op == NULL) { result = false; goto exit_without_operand; } result = G_IS_IMM_OPERAND(op); if (!result) goto exit_operand; /* Traitement au niveau du rendu graphique */ buffer = g_loaded_binary_get_disassembled_buffer(binary); line = g_code_buffer_find_line_by_addr(buffer, &switcher->addr, BLF_HAS_CODE, NULL); if (line == NULL) { result = false; goto exit_gui; } operand = G_IMM_OPERAND(op); *old = g_imm_operand_get_display(operand); if (new == IOD_COUNT) new = g_imm_operand_get_default_display(operand); g_imm_operand_set_display(&operand, new, G_SHARE_CONTAINER(instr)); len = g_imm_operand_to_string(operand, value); result = g_buffer_line_replace_text(line, G_OBJECT(op), value, len); g_object_unref(G_OBJECT(line)); exit_gui: /* TODO g_object_unref(G_OBJECT(buffer));*/ exit_operand: g_object_unref(G_OBJECT(op)); exit_without_operand: g_object_unref(G_OBJECT(instr)); exit_instr: g_object_unref(G_OBJECT(proc)); return result; #endif } /****************************************************************************** * * * Paramètres : switcher = bascule d'affichage à manipuler. * * binary = binaire chargé en mémoire à modifier. * * * * Description : Applique une bascule d'affichage d'opérande sur un binaire. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_switcher_apply(GDbSwitcher *switcher, GLoadedBinary *binary) { bool result; /* Bilan à faire remonter */ result = g_db_switcher_run(switcher, binary, &switcher->old, switcher->display); return result; } /****************************************************************************** * * * Paramètres : switcher = bascule d'affichage à manipuler. * * binary = binaire chargé en mémoire à modifier. * * * * Description : Annule une bascule d'affichage d'opérande sur un binaire. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_switcher_cancel(GDbSwitcher *switcher, GLoadedBinary *binary) { bool result; /* Bilan à faire remonter */ result = g_db_switcher_run(switcher, binary, (ImmOperandDisplay []) { 0 }, switcher->old); return result; } /****************************************************************************** * * * Paramètres : switcher = bascule d'affichage à charger depuis les réponses.* * values = tableau d'éléments à consulter. * * count = nombre de descriptions renseignées. * * * * Description : Charge les valeurs utiles pour un basculement d'affichage. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_switcher_load(GDbSwitcher *switcher, const bound_value *values, size_t count) { bool result; /* Bilan à faire remonter */ const bound_value *value; /* Valeur à éditer / définir */ result = G_DB_ITEM_CLASS(g_db_switcher_parent_class)->load(G_DB_ITEM(switcher), values, count); result &= load_vmpa(&switcher->addr, NULL, values, count); if (result) { value = find_bound_value(values, count, "op_index"); result = (value != NULL && value->type == SQLITE_INTEGER); if (result) switcher->index = value->integer; } if (result) { value = find_bound_value(values, count, "type"); result = (value != NULL && value->type == SQLITE_INTEGER); if (result) switcher->display = value->integer; } return result; } /****************************************************************************** * * * Paramètres : switcher = base d'éléments sur laquelle s'appuyer. * * values = couples de champs et de valeurs à lier. [OUT] * * count = nombre de ces couples. [OUT] * * * * Description : Constitue les champs destinés à une insertion / modification.* * * * Retour : Etat du besoin en sauvegarde. * * * * Remarques : - * * * ******************************************************************************/ static bool g_db_switcher_store(const GDbSwitcher *switcher, bound_value **values, size_t *count) { bool status; /* Bilan d'opération initiale */ bound_value *value; /* Valeur à éditer / définir */ if (switcher == NULL) status = G_DB_ITEM_CLASS(g_db_switcher_parent_class)->store(NULL, values, count); else status = G_DB_ITEM_CLASS(g_db_switcher_parent_class)->store(G_DB_ITEM(switcher), values, count); if (!status) return false; if (switcher == NULL) status = store_vmpa(NULL, NULL, values, count); else status = store_vmpa(&switcher->addr, NULL, values, count); if (!status) return false; *count += 2; *values = realloc(*values, *count * sizeof(bound_value)); value = &(*values)[*count - 2]; value->cname = "op_index"; value->built_name = false; value->type = SQLITE_INTEGER; value->has_value = (switcher != NULL); if (value->has_value) { value->integer = switcher->index; value->delete = NULL; } value = &(*values)[*count - 1]; value->cname = "type"; value->built_name = false; value->type = SQLITE_INTEGER; value->has_value = (switcher != NULL); if (value->has_value) { value->integer = switcher->display; value->delete = NULL; } return true; } /* ---------------------------------------------------------------------------------- */ /* DEFINITION DE LA COLLECTION ASSOCIEE */ /* ---------------------------------------------------------------------------------- */ /* Indique le type défini pour une collection de basculements d'affichage. */ G_DEFINE_TYPE(GSwitcherCollection, g_switcher_collection, G_TYPE_DB_COLLECTION); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des signets dans une zone de texte. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_switcher_collection_class_init(GSwitcherCollectionClass *klass) { GObjectClass *object; /* Autre version de la classe */ GDbCollectionClass *collec; /* Encore une autre vision... */ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_switcher_collection_dispose; object->finalize = (GObjectFinalizeFunc)g_switcher_collection_finalize; collec = G_DB_COLLECTION_CLASS(klass); collec->create_table = (collec_create_db_table_fc)g_switcher_collection_create_db_table; } /****************************************************************************** * * * Paramètres : collec = instance à initialiser. * * * * Description : Initialise un signet dans une zone de texte. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_switcher_collection_init(GSwitcherCollection *collec) { G_DB_COLLECTION(collec)->featuring = DBF_DISPLAY_SWITCHERS; G_DB_COLLECTION(collec)->type = G_TYPE_DB_SWITCHER; G_DB_COLLECTION(collec)->name = "Switchers"; } /****************************************************************************** * * * Paramètres : collec = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_switcher_collection_dispose(GSwitcherCollection *collec) { G_OBJECT_CLASS(g_switcher_collection_parent_class)->dispose(G_OBJECT(collec)); } /****************************************************************************** * * * Paramètres : collec = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_switcher_collection_finalize(GSwitcherCollection *collec) { G_OBJECT_CLASS(g_switcher_collection_parent_class)->finalize(G_OBJECT(collec)); } /****************************************************************************** * * * Paramètres : - * * * * Description : Crée une collection dédiée aux basculements d'affichage. * * * * Retour : Collection mise en place. * * * * Remarques : - * * * ******************************************************************************/ GSwitcherCollection *g_switcher_collection_new(void) { GSwitcherCollection *result; /* Instance à retourner */ result = g_object_new(G_TYPE_SWITCHER_COLLECTION, NULL); return result; } /****************************************************************************** * * * Paramètres : collec = ensemble d'éléments spectateur des opérations. * * db = accès à la base de données. * * * * Description : Crée la table des basculements dans une base de données. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_switcher_collection_create_db_table(const GSwitcherCollection *collec, sqlite3 *db) { const char *sql; /* Patron de requête SQL */ char *addr_fields; /* Champs pour l'adresse */ char *request; /* Requête à exécuter */ char *msg; /* Message d'erreur */ int ret; /* Bilan de la création */ sql = "CREATE TABLE Switchers (" \ SQLITE_DB_ITEM_CREATE ", " \ "%s, " \ "op_index INTEGER, " \ "type INTEGER" \ ");"; addr_fields = create_vmpa_db_table(NULL); asprintf(&request, sql, addr_fields); ret = sqlite3_exec(db, request, NULL, NULL, &msg); free(addr_fields); free(request); if (ret != SQLITE_OK) { fprintf(stderr, "sqlite3_exec(): %s\n", msg); sqlite3_free(msg); } return (ret == SQLITE_OK); }