/* OpenIDA - Outil d'analyse de fichiers binaires * symbols.c - panneau d'affichage des symboles * * Copyright (C) 2008-2012 Cyrille Bagard * * This file is part of OpenIDA. * * 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "symbols.h" #include #include "panel-int.h" #include "../../format/format.h" #include "../../gtkext/easygtk.h" #include "../../gtkext/support.h" /* -------------------------- PARTIE PRINCIPALE DU PANNEAU -------------------------- */ /* Panneau d'aperçu de graphiques (instance) */ struct _GSymbolsPanel { GPanelItem parent; /* A laisser en premier */ GtkTreeView *treeview; /* Composant d'affichage */ GtkTreeStore *store; /* Modèle de gestion */ GLoadedBinary *binary; /* Binaire à prendre en compte */ }; /* Panneau d'aperçu de graphiques (classe) */ struct _GSymbolsPanelClass { GPanelItemClass parent; /* A laisser en premier */ }; /* Colonnes de la liste des symboles */ typedef enum _SymbolsColumn { SBC_ADDRESS, /* Adresse mémoire du symbole */ SBC_ICON, /* Image de représentation */ SBC_NAME, /* Désignation humaine */ SBC_EXPAND, /* Affichage des classes */ SBC_COUNT /* Nombre de colonnes */ } SymbolsColumn; /* Initialise la classe des panneaux d'aperçu de graphiques. */ static void g_symbols_panel_class_init(GSymbolsPanelClass *); /* Initialise une instance de panneau d'aperçu de graphiques. */ static void g_symbols_panel_init(GSymbolsPanel *); /* Réagit au changement de sélection des symboles. */ static void on_symbols_selection_change(GtkTreeSelection *, GSymbolsPanel *); /* Réagit à un changement d'affichage principal de contenu. */ void change_symbols_panel_current_binary(GSymbolsPanel *, GLoadedBinary *); /* ------------------------- AFFICHAGE A L'AIDE D'UNE LISTE ------------------------- */ /* Réagit à un changement d'affichage principal de contenu. */ static void reload_symbols_for_new_list_view(GSymbolsPanel *); /* -------------------------- AFFICHAGE SOUS FORME D'ARBRE -------------------------- */ /* S'assure qu'un noeud donné existe bien. */ static GtkTreeIter ensure_symbol_node_exist(GtkTreeStore *, GtkTreeIter *, const char *); /* Détermine le point d'insertion parent d'une routine. */ static bool find_parent_for_routine(GtkTreeStore *, const GBinRoutine *, GtkTreeIter *); /* Réagit à un changement d'affichage principal de contenu. */ static void reload_symbols_for_new_tree_view(GSymbolsPanel *); /* Réagit à une nouvelle demande de réorganisation. */ static void reorganize_symbols_tree_view(GtkToolButton *, GObject *); /* Fait en sorte que toutes les classes soient affichées. */ static gboolean show_all_classes_in_tree_view(GtkTreeModel *, GtkTreePath *, GtkTreeIter *, GtkTreeView *); /* Réagit à une demande de nouvelle forme d'affichage. */ static void modify_types_in_symbols_tree_view(GtkToggleToolButton *, GObject *); /* ---------------------------------------------------------------------------------- */ /* PARTIE PRINCIPALE DU PANNEAU */ /* ---------------------------------------------------------------------------------- */ /* Indique le type définit pour un panneau d'aperçu de graphiques. */ G_DEFINE_TYPE(GSymbolsPanel, g_symbols_panel, G_TYPE_PANEL_ITEM); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des panneaux d'aperçu de graphiques. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_symbols_panel_class_init(GSymbolsPanelClass *klass) { } /****************************************************************************** * * * Paramètres : panel = instance à initialiser. * * * * Description : Initialise une instance de panneau d'aperçu de graphiques. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_symbols_panel_init(GSymbolsPanel *panel) { GEditorItem *base; /* Version basique d'instance */ GObject *ref; /* Espace de référencement */ GtkWidget *toolbar; /* Barre d'outils */ GtkWidget *button; /* Bouton de cette même barre */ GtkWidget *separator; /* Barre de séparation vert. */ GtkWidget *scrollwnd; /* Support défilant */ GtkWidget *treeview; /* Affichage de la liste */ GtkCellRenderer *renderer; /* Moteur de rendu de colonne */ GtkTreeViewColumn *column; /* Colonne de la liste */ GtkTreeSelection *select; /* Sélection dans la liste */ base = G_EDITOR_ITEM(panel); base->widget = gtk_vbox_new(FALSE, 0); gtk_widget_show(base->widget); ref = G_OBJECT(base->widget); g_object_set_data(ref, "panel", panel); /* Barre d'outils supérieure */ toolbar = gtk_toolbar_new(); gtk_widget_show(toolbar); gtk_box_pack_start(GTK_BOX(base->widget), toolbar, FALSE, FALSE, 0); //group = gtk_tool_item_group_new(_("View")); //gtk_widget_show(group); //gtk_container_add(GTK_CONTAINER(toolbar), group); button = qck_create_toggle_tool_button(ref, "list", "tbutton_list_view.png", G_CALLBACK(NULL), NULL); gtk_container_add(GTK_CONTAINER(toolbar), button); //gtk_tool_item_group_insert(GTK_TOOL_ITEM_GROUP(group), GTK_TOOL_ITEM(button), -1); button = qck_create_toggle_tool_button(ref, "tree", "tbutton_tree_view.png", G_CALLBACK(NULL), NULL); gtk_container_add(GTK_CONTAINER(toolbar), button); gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE); //gtk_tool_item_group_insert(GTK_TOOL_ITEM_GROUP(group), GTK_TOOL_ITEM(button), -1); separator = qck_create_tool_separator(NULL, NULL); gtk_container_add(GTK_CONTAINER(toolbar), separator); button = qck_create_tool_button(ref, "collapse", "tbutton_collapse.png", G_CALLBACK(reorganize_symbols_tree_view), ref); gtk_container_add(GTK_CONTAINER(toolbar), button); button = qck_create_tool_button(ref, "expand", "tbutton_expand.png", G_CALLBACK(reorganize_symbols_tree_view), ref); gtk_container_add(GTK_CONTAINER(toolbar), button); button = qck_create_tool_button(ref, "classes", "symbol_class_classic.png", G_CALLBACK(reorganize_symbols_tree_view), ref); gtk_container_add(GTK_CONTAINER(toolbar), button); separator = qck_create_tool_separator(NULL, NULL); gtk_container_add(GTK_CONTAINER(toolbar), separator); button = qck_create_toggle_tool_button(ref, "namespace", "tbutton_namespace.png", G_CALLBACK(modify_types_in_symbols_tree_view), ref); gtk_container_add(GTK_CONTAINER(toolbar), button); /* Liste arborescente ou linéaire */ scrollwnd = gtk_scrolled_window_new(NULL, NULL); gtk_widget_show(scrollwnd); gtk_box_pack_start(GTK_BOX(base->widget), scrollwnd, TRUE, TRUE, 0); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwnd), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrollwnd), GTK_SHADOW_IN); panel->store = gtk_tree_store_new(SBC_COUNT, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN); treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(panel->store)); gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE); gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(treeview), TRUE); gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE); panel->treeview = GTK_TREE_VIEW(treeview); gtk_widget_show(treeview); gtk_container_add(GTK_CONTAINER(scrollwnd), treeview); g_object_unref(G_OBJECT(panel->store)); /* column = gtk_tree_view_column_new(); gtk_tree_view_column_set_visible(column, FALSE); gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); gtk_tree_view_set_expander_column(GTK_TREE_VIEW(treeview), column); */ renderer = gtk_cell_renderer_text_new(); column = gtk_tree_view_column_new_with_attributes("Address", renderer, "text", SBC_ADDRESS, NULL); //gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); //gtk_tree_view_set_expander_column(GTK_TREE_VIEW(treeview), column); column = gtk_tree_view_column_new(); gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); gtk_tree_view_set_expander_column(GTK_TREE_VIEW(treeview), column); renderer = gtk_cell_renderer_pixbuf_new(); //column = gtk_tree_view_column_new_with_attributes("Icon", renderer, "pixbuf", SBC_ICON, NULL); gtk_tree_view_column_pack_start(column, renderer, FALSE); gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", SBC_ICON); renderer = gtk_cell_renderer_text_new(); //column = gtk_tree_view_column_new_with_attributes("Name", renderer, "text", SBC_NAME, NULL); gtk_tree_view_column_pack_end(column, renderer, TRUE); gtk_tree_view_column_add_attribute(column, renderer, "text", SBC_NAME); //gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); //gtk_tree_view_set_expander_column(GTK_TREE_VIEW(treeview), column); select = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)); gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE); g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(on_symbols_selection_change), panel); } /****************************************************************************** * * * Paramètres : ref = espace de référencement global. * * * * Description : Crée un panneau d'affichage des symboles. * * * * Retour : Adresse de la structure mise en place. * * * * Remarques : - * * * ******************************************************************************/ GEditorItem *g_symbols_panel_new(GObject *ref) { GEditorItem *result; /* Structure à retourner */ result = g_object_new(G_TYPE_SYMBOLS_PANEL, NULL); g_panel_item_init_ext(G_PANEL_ITEM(result), ref, PANEL_SYMBOL_ID, _("Binary symbols"), G_EDITOR_ITEM(result)->widget, "e"); return result; } /****************************************************************************** * * * Paramètres : ref = espace de référencement global. * * * * Description : Construit et intègre un panneau d'affichage des symboles. * * * * Retour : Adresse du panneau mis en place. * * * * Remarques : - * * * ******************************************************************************/ GPanelItem *create_symbols_panel(GObject *ref) { GEditorItem *result; /* Elément réactif à renvoyer */ result = g_symbols_panel_new(ref); /* Enregistre correctement le tout */ result->update_binary = (update_item_binary_fc)change_symbols_panel_current_binary; register_editor_item(result); return G_PANEL_ITEM(result); } /****************************************************************************** * * * Paramètres : selection = sélection modifiée. * * panel = structure contenant les informations maîtresses. * * * * Description : Réagit au changement de sélection des symboles. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void on_symbols_selection_change(GtkTreeSelection *selection, GSymbolsPanel *panel) { GtkTreeIter iter; /* Point de sélection */ GtkTreeModel *model; /* Modèle de gestion */ gchar *string; /* Chaîne sélectionnée */ vmpa_t address; /* Adresse à rejoindre */ if (gtk_tree_selection_get_selected(selection, &model, &iter)) { gtk_tree_model_get(model, &iter, SBC_ADDRESS, &string, -1); if (string != NULL) { address = strtoll(string, NULL, 16); g_free(string); gtk_view_panel_scroll_to_address(g_editor_item_get_current_view(G_EDITOR_ITEM(panel)), address); } } } /****************************************************************************** * * * Paramètres : panel = panneau à mettre à jour. * * binary = nouvelle instance de binaire analysé. * * * * Description : Réagit à un changement d'affichage principal de contenu. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void change_symbols_panel_current_binary(GSymbolsPanel *panel, GLoadedBinary *binary) { GtkToggleToolButton *button; /* Mode de représentation */ if (panel->binary != NULL) g_object_unref(G_OBJECT(panel->binary)); panel->binary = binary; g_object_ref(G_OBJECT(binary)); gtk_tree_store_clear(panel->store); button = g_object_get_data(G_OBJECT(G_EDITOR_ITEM(panel)->widget), "list"); if (gtk_toggle_tool_button_get_active(button)) reload_symbols_for_new_list_view(panel); else { reload_symbols_for_new_tree_view(panel); reorganize_symbols_tree_view(NULL, G_OBJECT(G_EDITOR_ITEM(panel)->widget)); } } /* ---------------------------------------------------------------------------------- */ /* AFFICHAGE A L'AIDE D'UNE LISTE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : panel = panneau à mettre à jour. * * * * Description : Réagit à un changement d'affichage principal de contenu. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void reload_symbols_for_new_list_view(GSymbolsPanel *panel) { GExeFormat *format; /* Format associé au binaire */ GBinRoutine **routines; /* Liste des routines trouvées */ size_t routines_count; /* Nombre de ces routines */ GArchProcessor *proc; /* Architecture utilisée */ size_t i; /* Boucle de parcours */ vmpa_t address; /* Adresse associée au symbole */ char tmp[VMPA_MAX_SIZE]; /* Version humainement lisible */ GtkTreeIter iter; /* Point d'insertion */ format = g_loaded_binary_get_format(panel->binary); routines = g_binary_format_get_routines(G_BIN_FORMAT(format), &routines_count); qsort(routines, routines_count, sizeof(GBinRoutine *), (__compar_fn_t)g_binary_routine_rcompare); if (routines != NULL) { proc = get_arch_processor_from_format(format); for (i = 0; i < routines_count; i++) { address = g_binary_routine_get_address(routines[i]); vmpa_to_string(address, g_arch_processor_get_memory_size(proc), tmp); gtk_tree_store_append(panel->store, &iter, NULL); gtk_tree_store_set(panel->store, &iter, SBC_ADDRESS, tmp, SBC_NAME, g_binary_routine_to_string(routines[i]), -1); } } } /* ---------------------------------------------------------------------------------- */ /* AFFICHAGE SOUS FORME D'ARBRE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : store = gestion des différents éléments insérés. * * parent = point d'insertion parent à retrouver. [OUT] * * name = nom du noeud ciblé. * * * * Description : S'assure qu'un noeud donné existe bien. * * * * Retour : Point d'insertion prochain. * * * * Remarques : - * * * ******************************************************************************/ static GtkTreeIter ensure_symbol_node_exist(GtkTreeStore *store, GtkTreeIter *parent, const char *name) { bool found; /* Bilan des recherches */ GtkTreeIter iter; /* Boucle de parcours */ gchar *string; /* Chaîne sélectionnée */ GdkPixbuf *pixbuf; /* Icone pour l'élément inséré */ found = false; if (gtk_tree_model_iter_children(GTK_TREE_MODEL(store), &iter, parent)) do { gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, SBC_NAME, &string, -1); found = (strcmp(string, name) == 0); g_free(string); if (found) break; } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)); if (!found) { pixbuf = get_pixbuf_from_file("symbol_package.png"); gtk_tree_store_append(store, &iter, parent); gtk_tree_store_set(store, &iter, SBC_ICON, pixbuf, SBC_NAME, name, -1); } return iter; } /****************************************************************************** * * * Paramètres : store = gestion des différents éléments insérés. * * routine = routine à intégrer. * * parent = point d'insertion parent à constituer. * * * * Description : Détermine le point d'insertion parent d'une routine. * * * * Retour : true si le point n'est pas la racine, false sinon. * * * * Remarques : - * * * ******************************************************************************/ static bool find_parent_for_routine(GtkTreeStore *store, const GBinRoutine *routine, GtkTreeIter *parent) { GOpenidaType *namespace; /* Espace d'appartenance */ char *string; /* Conversion en chaîne */ char *iter; /* Boucle de parcours */ char *token; /* Partie de texte isolée */ char *saveptr; /* Ctx. interne de découpage */ namespace = g_binary_routine_get_namespace(routine); if (namespace == NULL) return false; string = g_openida_type_to_string(namespace); for (iter = string; ; iter = NULL) { token = strtok_r(iter, "::", &saveptr); if (token == NULL) break; *parent = ensure_symbol_node_exist(store, (iter == string ? NULL : parent), token); } return true; } /****************************************************************************** * * * Paramètres : panel = panneau à mettre à jour. * * * * Description : Réagit à un changement d'affichage principal de contenu. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void reload_symbols_for_new_tree_view(GSymbolsPanel *panel) { GExeFormat *format; /* Format associé au binaire */ GBinRoutine **routines; /* Liste des routines trouvées */ size_t routines_count; /* Nombre de ces routines */ GArchProcessor *proc; /* Architecture utilisée */ Routine2StringOptions options; /* Options de rendu */ GtkToggleToolButton *button; /* Mode de représentation */ size_t i; /* Boucle de parcours */ vmpa_t address; /* Adresse associée au symbole */ char tmp[VMPA_MAX_SIZE]; /* Version humainement lisible */ GtkTreeIter parent; /* Point d'insertion parent */ GtkTreeIter iter; /* Point d'insertion */ GdkPixbuf *pixbuf; /* Icone pour l'élément inséré */ format = g_loaded_binary_get_format(panel->binary); routines = g_binary_format_get_routines(G_BIN_FORMAT(format), &routines_count); qsort(routines, routines_count, sizeof(GBinRoutine *), (__compar_fn_t)g_binary_routine_rcompare); if (routines != NULL) { proc = get_arch_processor_from_format(format); options = 0; button = g_object_get_data(G_OBJECT(G_EDITOR_ITEM(panel)->widget), "namespace"); if (gtk_toggle_tool_button_get_active(button)) options |= RSO_LONG_TYPE; for (i = 0; i < routines_count; i++) { address = g_binary_routine_get_address(routines[i]); vmpa_to_string(address, g_arch_processor_get_memory_size(proc), tmp); if (find_parent_for_routine(panel->store, routines[i], &parent)) { pixbuf = get_pixbuf_from_file("symbol_class_classic.png"); gtk_tree_store_set(panel->store, &parent, SBC_ICON, pixbuf, SBC_EXPAND, TRUE, -1); gtk_tree_store_append(panel->store, &iter, &parent); } else gtk_tree_store_append(panel->store, &iter, NULL); pixbuf = get_pixbuf_from_file("symbol_routine_classic.png"); gtk_tree_store_set(panel->store, &iter, SBC_ICON, pixbuf, SBC_ADDRESS, tmp, SBC_NAME, _g_binary_routine_to_string(routines[i], options), -1); if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf)); } } } /****************************************************************************** * * * Paramètres : button = bouton concerné par l'action. * * ref = espace de référencement des composants. * * * * Description : Réagit à une nouvelle demande de réorganisation. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void reorganize_symbols_tree_view(GtkToolButton *button, GObject *ref) { GSymbolsPanel *panel; /* Données du panneau */ panel = (GSymbolsPanel *)g_object_get_data(ref, "panel"); if (g_object_get_data(ref, "collapse") == button) gtk_tree_view_collapse_all(panel->treeview); else if (g_object_get_data(ref, "expand") == button) gtk_tree_view_expand_all(panel->treeview); else gtk_tree_model_foreach(GTK_TREE_MODEL(panel->store), (GtkTreeModelForeachFunc)show_all_classes_in_tree_view, panel->treeview); } /****************************************************************************** * * * Paramètres : model = modèle de gestion des éléments. * * path = chemin d'accès à l'élément courant. * * iter = itérateur courant. * * treeview = arborescence à manipuler ici. * * * * Description : Fait en sorte que toutes les classes soient affichées. * * * * Retour : FALSE pour continuer le parcours. * * * * Remarques : - * * * ******************************************************************************/ static gboolean show_all_classes_in_tree_view(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, GtkTreeView *treeview) { gboolean expand; /* Besoin en intervention */ GtkTreePath *tmp; /* Copie pour modification */ gtk_tree_model_get(model, iter, SBC_EXPAND, &expand, -1); if (expand) { tmp = gtk_tree_path_copy(path); if (gtk_tree_path_up(tmp)) gtk_tree_view_expand_to_path(treeview, tmp); gtk_tree_path_free(tmp); } return FALSE; } /****************************************************************************** * * * Paramètres : button = bouton concerné par l'action. * * ref = espace de référencement des composants. * * * * Description : Réagit à une demande de nouvelle forme d'affichage. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void modify_types_in_symbols_tree_view(GtkToggleToolButton *button, GObject *ref) { GSymbolsPanel *panel; /* Données du panneau */ panel = (GSymbolsPanel *)g_object_get_data(ref, "panel"); change_symbols_panel_current_binary(panel, panel->binary); }