/* Chrysalide - Outil d'analyse de fichiers binaires * symbols.c - panneau d'affichage des symboles * * Copyright (C) 2012-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 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 #include #include #include #include #include #include "panel-int.h" #include "../../common/extstr.h" #include "../../format/format.h" #include "../../gtkext/easygtk.h" #include "../../gtkext/support.h" /* -------------------------- PARTIE PRINCIPALE DU PANNEAU -------------------------- */ /* Panneau d'affichage des symboles (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 */ regex_t *filter; /* Filtre appliqué ou NULL */ }; /* Panneau d'affichage des symboles (classe) */ struct _GSymbolsPanelClass { GPanelItemClass parent; /* A laisser en premier */ cairo_surface_t *routine_img; /* Image pour les routines */ cairo_surface_t *package_img; /* Image pour les paquets */ cairo_surface_t *class_img; /* Image pour les classes */ }; /* Colonnes de la liste des symboles */ typedef enum _SymbolsColumn { SBC_SYMBOL, /* Symbole représenté */ SBC_PICTURE, /* Image de représentation */ SBC_NAME, /* Désignation humaine */ SBC_ORIGINAL, /* Version brute d'origine */ SBC_ADDRESS, /* Adresse mémoire du symbole */ SBC_SECTION, /* Section d'appartenance */ SBC_EXPAND, /* Affichage des classes */ SBC_COUNT /* Nombre de colonnes */ } SymbolsColumn; /* Initialise la classe des panneaux d'affichage des symboles. */ static void g_symbols_panel_class_init(GSymbolsPanelClass *); /* Initialise une instance de panneau d'affichage des symboles. */ static void g_symbols_panel_init(GSymbolsPanel *); /* Supprime toutes les références externes. */ static void g_symbols_panel_dispose(GSymbolsPanel *); /* Procède à la libération totale de la mémoire. */ static void g_symbols_panel_finalize(GSymbolsPanel *); /* Réagit au changement d'affichage des symboles. */ static void on_symbols_display_change(GtkToggleToolButton *, 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. */ static 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(GSymbolsPanel *, GtkTreeIter *, const char *, const regmatch_t *, size_t); /* Détermine le point d'insertion parent d'une routine. */ static bool find_parent_for_symbol(GSymbolsPanel *, const GBinSymbol *, GtkTreeIter *, const regmatch_t *, size_t *); /* 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 *); /* ------------------------- FILTRAGE DES SYMBOLES PRESENTS ------------------------- */ /* Démarre l'actualisation du filtrage des symboles. */ static void on_symbols_filter_changed(GtkSearchEntry *, GSymbolsPanel *); /* Exécute un nouveau filtrage des symboles affichés. */ static void do_filtering_on_symbols(GSymbolsPanel *); /* Détermine si un nom de symbole doit être filtré ou non. */ static bool is_symbol_filtered(GSymbolsPanel *, const GBinSymbol *, regmatch_t *); /* Met en évidence le texte recherché en cas de correspondance. */ static char *build_highlighted_name(const char *, const regmatch_t *, size_t); /* ---------------------------------------------------------------------------------- */ /* PARTIE PRINCIPALE DU PANNEAU */ /* ---------------------------------------------------------------------------------- */ /* Indique le type définit pour un panneau d'affichage des symboles. */ G_DEFINE_TYPE(GSymbolsPanel, g_symbols_panel, G_TYPE_PANEL_ITEM); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des panneaux d'affichage des symboles. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_symbols_panel_class_init(GSymbolsPanelClass *klass) { GObjectClass *object; /* Autre version de la classe */ GEditorItemClass *editem; /* Encore une autre vision... */ gchar *filename; /* Chemin d'accès à utiliser */ GPanelItemClass *panel; /* Version parente de la classe*/ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_symbols_panel_dispose; object->finalize = (GObjectFinalizeFunc)g_symbols_panel_finalize; editem = G_EDITOR_ITEM_CLASS(klass); editem->update_binary = (update_item_binary_fc)change_symbols_panel_current_binary; filename = find_pixmap_file("symbol_routine_classic.png"); assert(filename != NULL); klass->routine_img = cairo_image_surface_create_from_png(filename); g_free(filename); filename = find_pixmap_file("symbol_package.png"); assert(filename != NULL); klass->package_img = cairo_image_surface_create_from_png(filename); g_free(filename); filename = find_pixmap_file("symbol_class_classic.png"); assert(filename != NULL); klass->class_img = cairo_image_surface_create_from_png(filename); g_free(filename); panel = G_PANEL_ITEM_CLASS(klass); panel->unique = true; panel->bindings = "F3"; } /****************************************************************************** * * * Paramètres : panel = instance à initialiser. * * * * Description : Initialise une instance de panneau d'affichage des symboles. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_symbols_panel_init(GSymbolsPanel *panel) { GEditorItem *base; /* Version basique d'instance */ GPanelItem *pitem; /* Version parente du panneau */ GObject *ref; /* Espace de référencement */ GtkWidget *box; /* Séparation horizontale */ GtkWidget *toolbar; /* Barre d'outils */ GtkWidget *button; /* Bouton de cette même barre */ GtkWidget *separator; /* Barre de séparation vert. */ GtkWidget *search; /* Zone de recherche */ 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 */ /* Eléments de base */ base = G_EDITOR_ITEM(panel); base->name = PANEL_SYMBOLS_ID; pitem = G_PANEL_ITEM(panel); pitem->personality = PIP_SINGLETON; pitem->lname = _("Binary symbols"); pitem->dock_at_startup = true; pitem->path = strdup("eN"); /* Représentation graphique */ base->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); gtk_widget_show(base->widget); ref = G_OBJECT(base->widget); g_object_set_data(ref, "panel", panel); /* Barre d'outils supérieure */ box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8); gtk_widget_show(box); gtk_box_pack_start(GTK_BOX(base->widget), box, FALSE, FALSE, 0); toolbar = gtk_toolbar_new(); gtk_widget_show(toolbar); gtk_box_pack_start(GTK_BOX(box), toolbar, TRUE, TRUE, 0); button = qck_create_toggle_tool_button(ref, "list", _("List"), "tbutton_list_view.png", G_CALLBACK(on_symbols_display_change), panel); gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("Show symbols using a list view")); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(button), -1); button = qck_create_toggle_tool_button(ref, "tree", _("Tree"), "tbutton_tree_view.png", G_CALLBACK(on_symbols_display_change), panel); gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("Show symbols using a tree view")); gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), 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", _("Collapse"), "tbutton_collapse.png", G_CALLBACK(reorganize_symbols_tree_view), ref); gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("Collapse all symbol nodes in the tree view")); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(button), -1); button = qck_create_tool_button(ref, "expand", _("Expand"), "tbutton_expand.png", G_CALLBACK(reorganize_symbols_tree_view), ref); gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("Expand all symbol nodes in the tree view")); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(button), -1); button = qck_create_tool_button(ref, "classes", _("Classes"), "symbol_class_classic.png", G_CALLBACK(reorganize_symbols_tree_view), ref); gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("Show all classes in the tree view")); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(button), -1); separator = qck_create_tool_separator(NULL, NULL); gtk_container_add(GTK_CONTAINER(toolbar), separator); /* Espace de recherche */ search = gtk_search_entry_new(); gtk_widget_set_tooltip_text(search, _("Filter symbols using POSIX extended regular expressions")); g_signal_connect(search, "search-changed", G_CALLBACK(on_symbols_filter_changed), panel); gtk_widget_show(search); gtk_widget_set_hexpand(search, TRUE); gtk_box_pack_start(GTK_BOX(box), search, TRUE, TRUE, 0); /* 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_OBJECT, CAIRO_GOBJECT_TYPE_SURFACE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, 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); panel->treeview = GTK_TREE_VIEW(treeview); gtk_widget_show(treeview); gtk_container_add(GTK_CONTAINER(scrollwnd), treeview); g_object_unref(G_OBJECT(panel->store)); /* Cellules d'affichage */ column = gtk_tree_view_column_new(); renderer = gtk_cell_renderer_pixbuf_new(); gtk_tree_view_column_pack_start(column, renderer, FALSE); gtk_tree_view_column_add_attribute(column, renderer, "surface", SBC_PICTURE); renderer = gtk_cell_renderer_text_new(); gtk_tree_view_column_pack_start(column, renderer, TRUE); gtk_tree_view_column_add_attribute(column, renderer, "markup", SBC_NAME); gtk_tree_view_column_set_sort_column_id(column, SBC_NAME); gtk_tree_view_column_set_title(column, _("Name")); gtk_tree_view_append_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_column_set_sort_column_id(column, SBC_ADDRESS); gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); renderer = gtk_cell_renderer_text_new(); column = gtk_tree_view_column_new_with_attributes(_("Section"), renderer, "text", SBC_SECTION, NULL); gtk_tree_view_column_set_sort_column_id(column, SBC_SECTION); gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); /* Prise en compte de la sélection */ 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 : panel = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_symbols_panel_dispose(GSymbolsPanel *panel) { if (panel->binary != NULL) g_object_unref(G_OBJECT(panel->binary)); G_OBJECT_CLASS(g_symbols_panel_parent_class)->dispose(G_OBJECT(panel)); } /****************************************************************************** * * * Paramètres : panel = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_symbols_panel_finalize(GSymbolsPanel *panel) { if (panel->filter != NULL) { regfree(panel->filter); free(panel->filter); } G_OBJECT_CLASS(g_symbols_panel_parent_class)->finalize(G_OBJECT(panel)); } /****************************************************************************** * * * Paramètres : - * * * * Description : Crée un panneau d'affichage des symboles. * * * * Retour : Adresse de la structure mise en place. * * * * Remarques : - * * * ******************************************************************************/ GPanelItem *g_symbols_panel_new(void) { GPanelItem *result; /* Structure à retourner */ result = g_object_new(G_TYPE_SYMBOLS_PANEL, NULL); return result; } /****************************************************************************** * * * Paramètres : button = bouton de la barre activé. * * panel = structure contenant les informations maîtresses. * * * * Description : Réagit au changement d'affichage des symboles. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void on_symbols_display_change(GtkToggleToolButton *button, GSymbolsPanel *panel) { GObject *ref; /* Espace de référencement */ GtkToggleToolButton *list; /* Bouton pour les listes */ GtkToggleToolButton *tree; /* Bouton pour l'arborescence */ gboolean state; /* Etat du bouton courant */ GtkToggleToolButton *other; /* Bouton à traiter */ GtkWidget *option; /* Bouton dont l'accès change */ ref = G_OBJECT(G_EDITOR_ITEM(panel)->widget); list = GTK_TOGGLE_TOOL_BUTTON(g_object_get_data(ref, "list")); tree = GTK_TOGGLE_TOOL_BUTTON(g_object_get_data(ref, "tree")); /* Désactivation de l'autre bouton */ state = gtk_toggle_tool_button_get_active(button); if (button == list) other = tree; else other = list; g_signal_handlers_disconnect_by_func(other, G_CALLBACK(on_symbols_display_change), panel); gtk_toggle_tool_button_set_active(other, !state); g_signal_connect(other, "toggled", G_CALLBACK(on_symbols_display_change), panel); /* Définition des accès sur le reste de la barre */ state = gtk_toggle_tool_button_get_active(tree); option = GTK_WIDGET(g_object_get_data(ref, "collapse")); if (option != NULL) gtk_widget_set_sensitive(option, state); option = GTK_WIDGET(g_object_get_data(ref, "expand")); if (option != NULL) gtk_widget_set_sensitive(option, state); option = GTK_WIDGET(g_object_get_data(ref, "classes")); if (option != NULL) gtk_widget_set_sensitive(option, state); /* Actualisation */ if (panel->binary != NULL) { g_object_ref(G_OBJECT(panel->binary)); change_symbols_panel_current_binary(panel, panel->binary); g_object_unref(G_OBJECT(panel->binary)); } } /****************************************************************************** * * * 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 */ GBinSymbol *symbol; /* Symbole à traiter */ const mrange_t *range; /* Couverture dudit symbole */ GtkDisplayPanel *display; /* Afficheur effectif de code */ if (gtk_tree_selection_get_selected(selection, &model, &iter)) { gtk_tree_model_get(model, &iter, SBC_SYMBOL, &symbol, -1); if (symbol != NULL) { range = g_binary_symbol_get_range(symbol); display = g_editor_item_get_current_view(G_EDITOR_ITEM(panel)); gtk_display_panel_request_move(display, get_mrange_addr(range)); g_object_unref(G_OBJECT(symbol)); } } } /****************************************************************************** * * * Paramètres : panel = panneau à mettre à jour. * * binary = nouvelle instance de binaire analysé. * * * * Description : Réagit à un changement d'affichage principal de contenu. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void change_symbols_panel_current_binary(GSymbolsPanel *panel, GLoadedBinary *binary) { GtkToggleToolButton *button; /* Mode de représentation */ GtkRequisition req; /* Nouvelle taille idéale */ /* Basculement du binaire utilisé */ if (panel->binary != NULL) g_object_unref(G_OBJECT(panel->binary)); panel->binary = binary; if (panel->binary != NULL) g_object_ref(G_OBJECT(panel->binary)); gtk_tree_store_clear(panel->store); /* Si le panneau actif ne représente pas un binaire... */ if (binary == NULL) return; /* Actualisation de l'affichage */ 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)); } return; /* FIXME */ gtk_widget_get_preferred_size(GTK_WIDGET(panel->treeview), NULL, &req); gtk_widget_set_size_request(GTK_WIDGET(panel->treeview), req.width, req.height); } /* ---------------------------------------------------------------------------------- */ /* 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 */ GBinSymbol **symbols; /* Symboles à représenter */ size_t sym_count; /* Qté de symboles présents */ GArchProcessor *proc; /* Architecture utilisée */ MemoryDataSize size; /* Taille des localisations */ size_t i; /* Boucle de parcours */ regmatch_t match; /* Récupération des trouvailles*/ cairo_surface_t *icon; /* Image associée au symbole */ const char *original; /* Etiquette brute d'origine */ char *name; /* Etiquette mise en relief */ const vmpa2t *addr; /* Localisation d'un symbole */ char virt[VMPA_MAX_LEN]; /* Version humainement lisible */ GtkTreeIter iter; /* Point d'insertion */ format = g_loaded_binary_get_format(panel->binary); symbols = g_binary_format_get_symbols(G_BIN_FORMAT(format), &sym_count); proc = g_loaded_binary_get_processor(panel->binary); size = g_arch_processor_get_memory_size(proc); g_object_unref(G_OBJECT(proc)); for (i = 0; i < sym_count; i++) { if (is_symbol_filtered(panel, symbols[i], &match)) continue; switch (g_binary_symbol_get_target_type(symbols[i])) { case STP_ROUTINE: case STP_ENTRY_POINT: icon = G_SYMBOLS_PANEL_GET_CLASS(panel)->routine_img; break; case STP_OBJECT: icon = G_SYMBOLS_PANEL_GET_CLASS(panel)->routine_img; /* FIXME */ break; default: assert(false); break; } original = g_binary_symbol_get_label(symbols[i]); name = build_highlighted_name(original, &match, 0); addr = get_mrange_addr(g_binary_symbol_get_range(symbols[i])); vmpa2_virt_to_string(addr, size, virt, NULL); gtk_tree_store_append(panel->store, &iter, NULL); gtk_tree_store_set(panel->store, &iter, SBC_SYMBOL, symbols[i], SBC_PICTURE, icon, SBC_NAME, name, SBC_ORIGINAL, original, SBC_ADDRESS, virt, -1); free(name); } g_object_unref(G_OBJECT(format)); } /* ---------------------------------------------------------------------------------- */ /* AFFICHAGE SOUS FORME D'ARBRE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : panel = panneau en cours de mise à jour. * * parent = point d'insertion parent à retrouver. [OUT] * * raw = nom du noeud ciblé. * * match = portion de texte à mettre en évidence. * * start = position du texte brute dans l'étiquette complète. * * * * Description : S'assure qu'un noeud donné existe bien. * * * * Retour : Point d'insertion prochain. * * * * Remarques : - * * * ******************************************************************************/ static GtkTreeIter ensure_symbol_node_exist(GSymbolsPanel *panel, GtkTreeIter *parent, const char *raw, const regmatch_t *match, size_t start) { GtkTreeStore *store; /* Gestionnaire de données */ bool found; /* Bilan des recherches */ GtkTreeIter iter; /* Boucle de parcours */ gchar *string; /* Chaîne sélectionnée */ char *name; /* Etiquette mise en relief */ store = panel->store; 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_ORIGINAL, &string, -1); found = (strcmp(string, raw) == 0); g_free(string); if (found) break; } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)); if (!found) { name = build_highlighted_name(raw, match, start); gtk_tree_store_append(store, &iter, parent); gtk_tree_store_set(store, &iter, SBC_PICTURE, G_SYMBOLS_PANEL_GET_CLASS(panel)->package_img, SBC_NAME, name, SBC_ORIGINAL, raw, -1); free(name); } return iter; } /****************************************************************************** * * * Paramètres : panel = panneau en cours de mise à jour. * * symbol = routine ou objet à intégrer. * * parent = point d'insertion parent à constituer. [OUT] * * match = portion de texte à mettre en évidence. * * last = position du dernier élément du nom de symbole. [OUT]* * * * 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_symbol(GSymbolsPanel *panel, const GBinSymbol *symbol, GtkTreeIter *parent, const regmatch_t *match, size_t *last) { const char *label; /* Etiquette immuable */ char *string; /* Etiquette modifiable */ const char *sep; /* Délimitateur à utiliser */ char *start; /* Début de boucle de parcours */ char *token; /* Partie de texte isolée */ char *saveptr; /* Ctx. interne de découpage */ char *next; /* Prochaine partie à traiter */ *last = 0; label = g_binary_symbol_get_label(symbol); if (label == NULL) return false; string = strdup(label); sep = "."/*"::"*/; /* FIXME */ for (start = string, token = strtok_r(start, sep, &saveptr); ; start = NULL, token = next) { next = strtok_r(NULL, sep, &saveptr); if (next == NULL) { *last = (token - string); break; } *parent = ensure_symbol_node_exist(panel, (start == string ? NULL : parent), token, match, token - string); } free(string); 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 */ GBinSymbol **symbols; /* Symboles à représenter */ size_t sym_count; /* Qté de symboles présents */ GArchProcessor *proc; /* Architecture utilisée */ MemoryDataSize size; /* Taille des localisations */ size_t i; /* Boucle de parcours */ regmatch_t match; /* Récupération des trouvailles*/ GtkTreeIter parent; /* Point d'insertion parent */ size_t last; /* Position du dernier élément */ cairo_surface_t *icon; /* Image associée au symbole */ const char *original; /* Etiquette brute d'origine */ char *name; /* Etiquette mise en relief */ const vmpa2t *addr; /* Localisation d'un symbole */ char virt[VMPA_MAX_LEN]; /* Version humainement lisible */ GtkTreeIter iter; /* Point d'insertion */ format = g_loaded_binary_get_format(panel->binary); symbols = g_binary_format_get_symbols(G_BIN_FORMAT(format), &sym_count); proc = g_loaded_binary_get_processor(panel->binary); size = g_arch_processor_get_memory_size(proc); g_object_unref(G_OBJECT(proc)); for (i = 0; i < sym_count; i++) { if (is_symbol_filtered(panel, symbols[i], &match)) continue; if (find_parent_for_symbol(panel, symbols[i], &parent, &match, &last)) { gtk_tree_store_set(panel->store, &parent, SBC_PICTURE, G_SYMBOLS_PANEL_GET_CLASS(panel)->class_img, SBC_EXPAND, TRUE, -1); gtk_tree_store_append(panel->store, &iter, &parent); } else gtk_tree_store_append(panel->store, &iter, NULL); switch (g_binary_symbol_get_target_type(symbols[i])) { case STP_ROUTINE: case STP_ENTRY_POINT: icon = G_SYMBOLS_PANEL_GET_CLASS(panel)->routine_img; break; case STP_OBJECT: icon = G_SYMBOLS_PANEL_GET_CLASS(panel)->routine_img; /* FIXME */ break; default: assert(false); break; } original = g_binary_symbol_get_label(symbols[i]); name = build_highlighted_name(original + last, &match, last); addr = get_mrange_addr(g_binary_symbol_get_range(symbols[i])); vmpa2_virt_to_string(addr, size, virt, NULL); gtk_tree_store_set(panel->store, &iter, SBC_SYMBOL, symbols[i], SBC_PICTURE, icon, SBC_NAME, name, SBC_ORIGINAL, original + last, SBC_ADDRESS, virt, -1); free(name); } g_object_unref(G_OBJECT(format)); } /****************************************************************************** * * * 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; } /* ---------------------------------------------------------------------------------- */ /* FILTRAGE DES SYMBOLES PRESENTS */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : entry = entrée de texte contenant le filtre brut. * * panel = panneau assurant l'affichage des symboles. * * * * Description : Démarre l'actualisation du filtrage des symboles. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void on_symbols_filter_changed(GtkSearchEntry *entry, GSymbolsPanel *panel) { const gchar *text; /* Texte de l'utilisateur */ int ret; /* Bilan de mise en place */ GdkRGBA error; /* Couleur d'erreur */ if (panel->filter != NULL) { regfree(panel->filter); free(panel->filter); panel->filter = NULL; } text = gtk_entry_get_text(GTK_ENTRY(entry)); if (strlen(text) > 0) { panel->filter = (regex_t *)calloc(1, sizeof(regex_t)); ret = regcomp(panel->filter, text, REG_EXTENDED | REG_ICASE); if (ret != 0) { free(panel->filter); panel->filter = NULL; error.red = 1.0; error.green = 0.0; error.blue = 0.0; error.alpha = 1.0; gtk_widget_override_color(GTK_WIDGET(entry), GTK_STATE_NORMAL, &error); return; } } gtk_widget_override_color(GTK_WIDGET(entry), GTK_STATE_NORMAL, NULL); do_filtering_on_symbols(panel); } /****************************************************************************** * * * Paramètres : panel = panneau assurant l'affichage des symboles. * * * * Description : Exécute un nouveau filtrage des symboles affichés. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void do_filtering_on_symbols(GSymbolsPanel *panel) { g_object_ref(G_OBJECT(panel->binary)); change_symbols_panel_current_binary(panel, panel->binary); g_object_unref(G_OBJECT(panel->binary)); } /****************************************************************************** * * * Paramètres : panel = panneau assurant l'affichage des symboles. * * symbol = symbole à traiter. * * match = récupération des trouvailles. [OUT] * * * * Description : Détermine si un nom de symbole doit être filtré ou non. * * * * Retour : true si le symbol ne doit pas être affiché, false sinon. * * * * Remarques : - * * * ******************************************************************************/ static bool is_symbol_filtered(GSymbolsPanel *panel, const GBinSymbol *symbol, regmatch_t *match) { SymbolType type; /* Type associé au symbole */ int ret; /* Bilan du filtrage */ type = g_binary_symbol_get_target_type(symbol); if (type != STP_ROUTINE && type != STP_ENTRY_POINT && type != STP_OBJECT) return true; memset(match, 0, sizeof(regmatch_t)); if (panel->filter == NULL) return false; ret = regexec(panel->filter, g_binary_symbol_get_label(symbol), 1, match, 0); if (ret == REG_NOMATCH) return true; return false; } /****************************************************************************** * * * Paramètres : raw = bribe de texte à traiter. * * match = portion de texte à mettre en évidence. * * start = position du texte brute dans l'étiquette complète. * * * * Description : Met en évidence le texte recherché en cas de correspondance. * * * * Retour : Texte final destiné à être inséré, à libérer après usage. * * * * Remarques : - * * * ******************************************************************************/ static char *build_highlighted_name(const char *raw, const regmatch_t *match, size_t start) { char *result; /* Construction à retourner */ size_t len; /* Taille du texte d'entrée */ regmatch_t selection; /* Sélection relative au texte */ char *valid; /* Retouche partielle */ len = strlen(raw); /* Si aucune sélection ou texte hors champ... */ if ((match->rm_eo - match->rm_so) == 0 || (start + len) <= match->rm_so || match->rm_eo < start) { result = strdup(raw); result = strrpl(result, "<", "<"); } /* Sinon, il y a forcément correspondance quelque part ! */ else { /* Adaptations */ if (match->rm_so < start) selection.rm_so = 0; else selection.rm_so = match->rm_so - start; selection.rm_eo = match->rm_eo - start; if (selection.rm_eo > len) selection.rm_eo = len; /* Impressions */ if (selection.rm_so > 0) { result = strndup(raw, selection.rm_so); result = strrpl(result, "<", "<"); } else result = NULL; result = stradd(result, ""); valid = strndup(&raw[selection.rm_so], selection.rm_eo - selection.rm_so); valid = strrpl(valid, "<", "<"); result = stradd(result, valid); free(valid); result = stradd(result, ""); valid = strdup(&raw[selection.rm_eo]); valid = strrpl(valid, "<", "<"); result = stradd(result, valid); free(valid); } return result; }