/* Chrysalide - Outil d'analyse de fichiers binaires
* binparts.h - boîte de dialogue permettant une sélection des sections
*
* Copyright (C) 2009-2014 Cyrille Bagard
*
* This file is part of Chrysalide.
*
* OpenIDA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* OpenIDA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see .
*/
#include "binparts.h"
#include
#include "../format/format.h"
#include "../gtkext/easygtk.h"
#define _(str) str
/* Colonnes de la liste des symboles */
typedef enum _PartsColumn
{
PTC_ACTIVE, /* Zone de code active ? */
PTC_NAME, /* Désignation humaine */
PTC_START, /* Adresse de départ */
PTC_END, /* Adresse d'arrivée (exclue) */
PTC_COUNT /* Nombre de colonnes */
} PartsColumn;
/* Mémoire d'un modèle */
typedef struct _parts_model
{
gboolean *selected; /* Sélection ou non de parties */
size_t count; /* Qté. de prises en compte */
} parts_model;
/* Sélectionne ou non tous les éléments de la liste courante. */
static void select_all_items_or_none(GtkButton *, GObject *);
/* Sauvegarde l'état courant des sélections et clôt la fenêtre. */
static void save_current_selection(GtkButton *, GObject *);
/* Ferme la fenêtre de dialogue. */
static void close_editor(GtkButton *, GtkWidget *);
/* Charge les parties courantes d'un binaire donné. */
static void load_binary_current_parts(GLoadedBinary *binary, GObject *ref);
/* Affiche les parties désassemblées par défaut. */
static void load_default_parts(GObject *);
/* Affiche les parties désassemblées selon les routines. */
static void load_routines_parts(GObject *);
/* Réagit à un changement de modèle. */
static void on_model_change(GtkComboBox *, GObject *);
/* Réagit à un changement de sélection de partie. */
static void on_part_selection_toggle(GtkCellRendererToggle *, gchar *, GObject *);
/******************************************************************************
* *
* Paramètres : binary = informations sur le binaire actuellement ouvert. *
* parent = fenêtre parente à surpasser. *
* *
* Description : Construit la fenêtre de sélection des sections. *
* *
* Retour : Adresse de la fenêtre mise en place. *
* *
* Remarques : - *
* *
******************************************************************************/
GtkWidget *create_sections_dialog(GLoadedBinary *binary, GtkWindow *parent)
{
GtkWidget *result; /* Fenêtre à renvoyer */
GObject *ref; /* Espace de référencements */
GtkWidget *vbox1;
GtkWidget *vbox2;
GtkWidget *hbox2;
GtkWidget *label; /* Etiquette à afficher */
GtkWidget *comboboxentry;
GtkWidget *hbox3;
GtkWidget *scrolledwindow1;
GtkTreeStore *store; /* Modèle de gestion */
GtkWidget *treeview; /* Affichage de la liste */
GtkCellRenderer *renderer; /* Moteur de rendu de colonne */
GtkTreeViewColumn *column; /* Colonne de la liste */
GtkWidget *vbuttonbox1;
GtkWidget *hbuttonbox1;
GtkWidget *button; /* Bouton de commande */
GtkWidget *sep; /* Barre de séparation */
result = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(result, 600, 400);
gtk_container_set_border_width(GTK_CONTAINER(result), 8);
gtk_window_set_title(GTK_WINDOW(result), _("Content to display"));
gtk_window_set_transient_for(GTK_WINDOW(result), parent);
gtk_window_set_default_size(GTK_WINDOW(result), 600, 400);
gtk_window_set_type_hint(GTK_WINDOW(result), GDK_WINDOW_TYPE_HINT_DIALOG);
ref= G_OBJECT(result);
g_object_set_data(ref, "binary", binary);
vbox1 = gtk_vbox_new(FALSE, 8);
gtk_widget_show(vbox1);
gtk_container_add(GTK_CONTAINER(result), vbox1);
vbox2 = gtk_vbox_new(FALSE, 8);
gtk_widget_show(vbox2);
gtk_container_add(GTK_CONTAINER(vbox1), vbox2);
hbox2 = gtk_hbox_new(FALSE, 8);
gtk_widget_show(hbox2);
gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
label = qck_create_label(NULL, NULL, _("Model :"));
gtk_box_pack_start(GTK_BOX(hbox2), label, FALSE, FALSE, 0);
comboboxentry = qck_create_combobox(ref, "models", G_CALLBACK(NULL), NULL);
gtk_box_pack_start(GTK_BOX(hbox2), comboboxentry, TRUE, TRUE, 0);
button = qck_create_button_with_img(NULL, NULL, "gtk-add", G_CALLBACK(NULL), NULL);
gtk_widget_set_sensitive(button, FALSE);
gtk_box_pack_start(GTK_BOX(hbox2), button, FALSE, FALSE, 0);
button = qck_create_button_with_img(NULL, NULL, "gtk-remove", G_CALLBACK(NULL), NULL);
gtk_widget_set_sensitive(button, FALSE);
gtk_box_pack_start(GTK_BOX(hbox2), button, FALSE, FALSE, 0);
hbox3 = gtk_hbox_new(FALSE, 8);
gtk_widget_show(hbox3);
gtk_box_pack_start(GTK_BOX(vbox2), hbox3, TRUE, TRUE, 0);
scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
gtk_widget_show(scrolledwindow1);
gtk_box_pack_start(GTK_BOX(hbox3), scrolledwindow1, TRUE, TRUE, 0);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow1), GTK_SHADOW_IN);
store = gtk_tree_store_new(PTC_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
g_object_set_data(ref, "store", store);
treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
gtk_widget_show(treeview);
gtk_container_add(GTK_CONTAINER(scrolledwindow1), treeview);
g_object_unref(G_OBJECT(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_toggle_new();
gtk_cell_renderer_toggle_set_activatable(GTK_CELL_RENDERER_TOGGLE(renderer), TRUE);
column = gtk_tree_view_column_new_with_attributes(_("Active"), renderer, "active", PTC_ACTIVE, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(on_part_selection_toggle), ref);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_("Name"), renderer, "text", PTC_NAME, NULL);
gtk_tree_view_column_set_expand(column, TRUE);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_("Start"), renderer, "text", PTC_START, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_("End"), renderer, "text", PTC_END, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
/* Boutons d'édition de la liste */
vbuttonbox1 = gtk_vbutton_box_new();
gtk_widget_show(vbuttonbox1);
gtk_box_pack_start(GTK_BOX(hbox3), vbuttonbox1, FALSE, FALSE, 0);
gtk_button_box_set_layout(GTK_BUTTON_BOX(vbuttonbox1), GTK_BUTTONBOX_SPREAD);
button = qck_create_button_with_img(NULL, NULL, "gtk-add", G_CALLBACK(select_all_items_or_none), ref);
gtk_container_add(GTK_CONTAINER(vbuttonbox1), button);
g_object_set_data(G_OBJECT(button), "all", button);
button = qck_create_button_with_img(NULL, NULL, "gtk-remove", G_CALLBACK(select_all_items_or_none), ref);
gtk_container_add(GTK_CONTAINER(vbuttonbox1), button);
sep = gtk_hseparator_new();
gtk_widget_show(sep);
gtk_container_add(GTK_CONTAINER(vbuttonbox1), sep);
button = qck_create_button_with_img(NULL, NULL, "gtk-add", G_CALLBACK(NULL), NULL);
gtk_widget_set_sensitive(button, FALSE);
gtk_container_add(GTK_CONTAINER(vbuttonbox1), button);
button = qck_create_button_with_img(NULL, NULL, "gtk-remove", G_CALLBACK(NULL), NULL);
gtk_widget_set_sensitive(button, FALSE);
gtk_container_add(GTK_CONTAINER(vbuttonbox1), button);
hbuttonbox1 = gtk_hbutton_box_new();
gtk_widget_show(hbuttonbox1);
gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox1, FALSE, FALSE, 0);
gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox1), GTK_BUTTONBOX_END);
/* Boutons de contrôle principaux */
button = qck_create_button_from_stock(NULL, NULL, "gtk-ok", G_CALLBACK(save_current_selection), ref);
gtk_container_add(GTK_CONTAINER(hbuttonbox1), button);
button = qck_create_button_from_stock(NULL, NULL, "gtk-cancel", G_CALLBACK(close_editor), result);
gtk_container_add(GTK_CONTAINER(hbuttonbox1), button);
/* Actualisation de l'interface */
g_signal_connect(G_OBJECT(comboboxentry), "changed", G_CALLBACK(on_model_change), ref);
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(comboboxentry), _("Default"));
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(comboboxentry), _("Routines"));
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(comboboxentry), _("User"));
load_binary_current_parts(binary, ref);
return result;
}
/******************************************************************************
* *
* Paramètres : button = bouton d'édition de la sélection. *
* ref = espace de référencement principal. *
* *
* Description : Sélectionne ou non tous les éléments de la liste courante. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void select_all_items_or_none(GtkButton *button, GObject *ref)
{
gboolean state; /* Etat de sélection à donner */
GtkTreeModel *model; /* Modèle de représentation */
GtkTreeIter iter; /* Point de modification */
state = (g_object_get_data(G_OBJECT(button), "all") != NULL);
model = GTK_TREE_MODEL(g_object_get_data(ref, "store"));
if (gtk_tree_model_get_iter_first(model, &iter))
do
gtk_tree_store_set(GTK_TREE_STORE(model), &iter,
PTC_ACTIVE, state,
-1);
while (gtk_tree_model_iter_next(model, &iter));
}
/******************************************************************************
* *
* Paramètres : button = bouton 'OK'. *
* ref = espace de référencement principal. *
* *
* Description : Sauvegarde l'état courant des sélections et clôt la fenêtre. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void save_current_selection(GtkButton *button, GObject *ref)
{
GLoadedBinary *binary; /* Binaire à traiter */
GExeFormat *format; /* Format associé au binaire */
GArchProcessor *proc; /* Architecture utilisée */
GBinPart **parts; /* Parcelles à désassembler */
size_t parts_count; /* Quantité de ces parcelles */
GBinPart *part; /* Nouvelle partie à lister */
GBinRoutine **routines; /* Liste des routines trouvées */
size_t routines_count; /* Nombre de ces routines */
parts_model *model; /* Mémoire du modèle */
size_t i; /* Boucle de parcours */
off_t offset; /* Position dans le binaire */
binary = G_LOADED_BINARY(g_object_get_data(ref, "binary"));
format = g_loaded_binary_get_format(binary);
proc = get_arch_processor_from_format(format);
/* Routines */
parts = NULL;
parts_count = 0;
routines = g_binary_format_get_routines(G_BIN_FORMAT(format), &routines_count);
qsort(routines, routines_count, sizeof(GBinRoutine *), g_binary_routine_compare);
model = (parts_model *)g_object_get_data(ref, "routines_model");
for (i = 0; i < routines_count; i++)
{
if (!model->selected[i]) continue;
part = g_binary_part_new();
g_binary_part_set_name(part, g_binary_routine_get_name(routines[i]));
g_exe_format_translate_address_into_offset(format,
g_binary_routine_get_address(routines[i]),
&offset);
g_binary_part_set_values(part, offset,
g_binary_routine_get_size(routines[i]),
g_binary_routine_get_address(routines[i]));
parts = (GBinPart **)realloc(parts, ++parts_count * sizeof(GBinPart *));
parts[parts_count - 1] = part;
}
g_loaded_binary_set_parts(binary, BPM_ROUTINES, parts, parts_count);
/* Fin */
gtk_widget_destroy(GTK_WIDGET(ref));
}
/******************************************************************************
* *
* Paramètres : button = bouton 'Annuler'. *
* widget = adresse de la fenêtre de l'éditeur à fermer. *
* *
* Description : Ferme la fenêtre de dialogue. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void close_editor(GtkButton *button, GtkWidget *widget)
{
gtk_widget_destroy(widget);
}
/******************************************************************************
* *
* Paramètres : binary = informations sur le binaire actuellement ouvert. *
* ref = espace de référencement principal. *
* *
* Description : Charge les parties courantes d'un binaire donné. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void load_binary_current_parts(GLoadedBinary *binary, GObject *ref)
{
GtkTreeModel *store; /* Modèle de représentation */
GBinPart ***list; /* Tableau de parties choisies */
BinaryPartModel model; /* Sélection courante */
size_t *count; /* Taille de chaque liste */
unsigned int i; /* Boucle de parcours #1 */
parts_model *parts; /* Liste de sélection courante */
size_t j; /* Boucle de parcours #2 */
GtkTreeIter iter; /* Lieu de lecture d'adresse */
char path[11 /* UINT_MAX */]; /* Chemin d'accès pour GTK */
gchar *value; /* Valeur d'adresse présentée */
vmpa_t addr_dlg; /* Adresse côté local */
size_t k; /* Boucle de parcours #3 */
vmpa_t addr_bin; /* Adresse côté binaire */
GtkComboBox *combo; /* Liste de tous les modèles */
/* Lecture des sélections courantes */
store = GTK_TREE_MODEL(g_object_get_data(ref, "store"));
list = g_loaded_binary_get_parts(binary, &model, &count);
for (i = 0; i < (BPM_COUNT - 1 /* TODO*/); i++)
{
gtk_tree_store_clear(GTK_TREE_STORE(store));
switch (i)
{
case BPM_DEFAULT:
load_default_parts(ref);
parts = (parts_model *)g_object_get_data(ref, "default_model");
break;
case BPM_ROUTINES:
load_routines_parts(ref);
parts = (parts_model *)g_object_get_data(ref, "routines_model");
break;
case BPM_USER:
//parts =
break;
}
for (j = 0; j < parts->count; j++)
{
snprintf(path, 11, "%u", j);
gtk_tree_model_get_iter_from_string(store, &iter, path);
gtk_tree_model_get(store, &iter, PTC_START, &value, -1);
addr_dlg = strtoll(value, NULL, 16);
g_free(value);
for (k = 0; k < count[i]; k++)
{
g_binary_part_get_values(list[i][k], NULL, NULL, &addr_bin);
if (addr_bin == addr_dlg) break;
}
parts->selected[j] = (count[i] == 0 || k < count[i]);
}
}
/* Sélection courante */
combo = GTK_COMBO_BOX(g_object_get_data(ref, "models"));
gtk_combo_box_set_active(combo, model);
}
/******************************************************************************
* *
* Paramètres : ref = espace de référencement principal. *
* *
* Description : Affiche les parties désassemblées par défaut. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void load_default_parts(GObject *ref)
{
GLoadedBinary *binary; /* Binaire à traiter */
GtkTreeStore *store; /* Modèle de gestion */
GExeFormat *format; /* Format associé au binaire */
GArchProcessor *proc; /* Architecture utilisée */
GBinPart **parts; /* Parties d'élément binaire */
size_t parts_count; /* Nombre de ces parties */
parts_model *model; /* Mémoire du modèle */
bool exist; /* Mémoire présente ? */
size_t i; /* Boucle de parcours */
off_t size; /* Taille de la partie */
vmpa_t addr; /* Adresse de départ */
char start[VMPA_MAX_SIZE]; /* Version humainement lisible */
char end[VMPA_MAX_SIZE]; /* Version humainement lisible */
GtkTreeIter iter; /* Point d'insertion */
binary = G_LOADED_BINARY(g_object_get_data(ref, "binary"));
store = GTK_TREE_STORE(g_object_get_data(ref, "store"));
format = g_loaded_binary_get_format(binary);
proc = get_arch_processor_from_format(format);
parts = g_exe_format_get_parts(format, &parts_count);
qsort(parts, parts_count, sizeof(GBinPart *), g_binary_part_compare);
model = (parts_model *)g_object_get_data(ref, "default_model");
exist = (model != NULL);
if (!exist)
{
model = (parts_model *)calloc(1, sizeof(parts_model));
g_object_set_data(ref, "default_model", model);
model->selected = (gboolean *)calloc(parts_count, sizeof(gboolean));
model->count = parts_count;
}
for (i = 0; i < parts_count; i++)
{
g_binary_part_get_values(parts[i], NULL, &size, &addr);
vmpa_to_string(addr, g_arch_processor_get_memory_size(proc), start);
vmpa_to_string(addr + size, g_arch_processor_get_memory_size(proc), end);
if (!exist)
model->selected[i] = TRUE;
gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter,
PTC_ACTIVE, model->selected[i],
PTC_NAME, g_binary_part_get_name(parts[i]),
PTC_START, start,
PTC_END, end,
-1);
}
}
/******************************************************************************
* *
* Paramètres : ref = espace de référencement principal. *
* *
* Description : Affiche les parties désassemblées selon les routines. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void load_routines_parts(GObject *ref)
{
GLoadedBinary *binary; /* Binaire à traiter */
GtkTreeStore *store; /* Modèle de gestion */
GExeFormat *format; /* Format associé au binaire */
GArchProcessor *proc; /* Architecture utilisée */
GBinRoutine **routines; /* Liste des routines trouvées */
size_t routines_count; /* Nombre de ces routines */
parts_model *model; /* Mémoire du modèle */
bool exist; /* Mémoire présente ? */
size_t i; /* Boucle de parcours */
vmpa_t addr; /* Adresse à transcrire */
char start[VMPA_MAX_SIZE]; /* Version humainement lisible */
char end[VMPA_MAX_SIZE]; /* Version humainement lisible */
GtkTreeIter iter; /* Point d'insertion */
binary = G_LOADED_BINARY(g_object_get_data(ref, "binary"));
store = GTK_TREE_STORE(g_object_get_data(ref, "store"));
format = g_loaded_binary_get_format(binary);
proc = get_arch_processor_from_format(format);
routines = g_binary_format_get_routines(G_BIN_FORMAT(format), &routines_count);
qsort(routines, routines_count, sizeof(GBinRoutine *), g_binary_routine_compare);
model = (parts_model *)g_object_get_data(ref, "routines_model");
exist = (model != NULL);
if (!exist)
{
model = (parts_model *)calloc(1, sizeof(parts_model));
g_object_set_data(ref, "routines_model", model);
model->selected = (gboolean *)calloc(routines_count, sizeof(gboolean));
model->count = routines_count;
}
for (i = 0; i < routines_count; i++)
{
addr = g_binary_routine_get_address(routines[i]);
vmpa_to_string(addr, g_arch_processor_get_memory_size(proc), start);
addr += g_binary_routine_get_size(routines[i]);
vmpa_to_string(addr, g_arch_processor_get_memory_size(proc), end);
if (!exist)
model->selected[i] = TRUE;
gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter,
PTC_ACTIVE, model->selected[i],
PTC_NAME, g_binary_routine_get_name(routines[i]),
PTC_START, start,
PTC_END, end,
-1);
}
}
/******************************************************************************
* *
* Paramètres : combo = liste des modèles proposés. *
* ref = espace de référencement principal. *
* *
* Description : Réagit à un changement de modèle. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void on_model_change(GtkComboBox *combo, GObject *ref)
{
gint index; /* Indice du nouveau modèle */
GtkTreeStore *store; /* Modèle de gestion */
store = GTK_TREE_STORE(g_object_get_data(ref, "store"));
gtk_tree_store_clear(store);
index = gtk_combo_box_get_active(combo);
switch (index)
{
case BPM_DEFAULT:
load_default_parts(ref);
break;
case BPM_ROUTINES:
load_routines_parts(ref);
break;
case BPM_USER:
break;
}
}
/******************************************************************************
* *
* Paramètres : renderer = cellule de rendu à mettre à jour. *
* path = chemin menant à la ligne concernée. *
* ref = espace de référencement principal. *
* *
* Description : Réagit à un changement de sélection de partie. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void on_part_selection_toggle(GtkCellRendererToggle *renderer, gchar *path, GObject *ref)
{
GtkTreeModel *model; /* Modèle de représentation */
GtkTreeIter iter; /* Lieu de la mise à jour */
gboolean state; /* Etat de la sélection */
GtkComboBox *combo; /* Liste de tous les modèles */
gint index; /* Indice du modèle courant */
parts_model *list; /* Mémorisation des sélections */
printf("path :: %s\n", path);
model = GTK_TREE_MODEL(g_object_get_data(ref, "store"));
if (gtk_tree_model_get_iter_from_string(model, &iter, path))
{
gtk_tree_model_get(model, &iter, PTC_ACTIVE, &state, -1);
combo = GTK_COMBO_BOX(g_object_get_data(ref, "models"));
index = gtk_combo_box_get_active(combo);
switch (index)
{
case BPM_DEFAULT:
list = (parts_model *)g_object_get_data(ref, "default_model");
break;
case BPM_ROUTINES:
list = (parts_model *)g_object_get_data(ref, "routines_model");
break;
case BPM_USER:
break;
}
list->selected[atoi(path)] = !state;
gtk_tree_store_set(GTK_TREE_STORE(model), &iter,
PTC_ACTIVE, !state,
-1);
}
}