summaryrefslogtreecommitdiff
path: root/src/gtkext/gtkblockview.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gtkext/gtkblockview.c')
-rw-r--r--src/gtkext/gtkblockview.c785
1 files changed, 785 insertions, 0 deletions
diff --git a/src/gtkext/gtkblockview.c b/src/gtkext/gtkblockview.c
new file mode 100644
index 0000000..b125c7b
--- /dev/null
+++ b/src/gtkext/gtkblockview.c
@@ -0,0 +1,785 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * gtkblockview.c - affichage d'un fragment de code d'assemblage
+ *
+ * Copyright (C) 2008 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "gtkblockview.h"
+
+
+#include <malloc.h>
+#include <string.h>
+
+
+#include "gtkbinview-int.h"
+#include "../common/dllist.h"
+
+
+
+#define CONTENT_BUFFER_LEN 64
+
+#define MARGIN_SPACE 4
+
+
+
+struct _GtkBlockView
+{
+ GtkBinView parent; /* A laisser en premier */
+
+ bool show_vaddress; /* Affichage des adresses ? */
+ bool show_code; /* Affichage du code brut ? */
+
+ PangoLayout *layout; /* Moteur de rendu du code ASM */
+ GdkGC *gc; /* Contexte graphique du rendu */
+ int line_height; /* Hauteur maximale des lignes */
+
+ const exe_format *format; /* Format du contenu bianire */
+
+ GRenderingLine *lines; /* Contenu à représenter */
+
+};
+
+struct _GtkBlockViewClass
+{
+ GtkBinViewClass parent; /* A laisser en premier */
+
+};
+
+
+
+
+
+
+
+
+/* Redessine l'affichage suite une mise à jour dans la marge. */
+void gtk_block_view_update_margin(GRenderingLine *, GtkBlockView *);
+
+
+
+
+
+/* Réclame une nouvelle taille adaptée au contenu présent. */
+void gtk_block_view_recompute_size_request(GtkBlockView *);
+
+
+
+
+
+
+
+
+static void gtk_block_view_class_init(GtkBlockViewClass *klass);
+static void gtk_block_view_init(GtkBlockView *cpu);
+static void gtk_block_view_size_request(GtkWidget *widget,
+ GtkRequisition *requisition);
+static void gtk_block_view_size_allocate(GtkWidget *widget,
+ GtkAllocation *allocation);
+static void gtk_block_view_realize(GtkWidget *widget);
+
+
+static gboolean gtk_block_view_button_press(GtkWidget *, GdkEventButton *event);
+
+static gboolean gtk_block_view_expose(GtkWidget *widget,
+ GdkEventExpose *event);
+static void gtk_block_view_paint(GtkBlockView *view);
+static void gtk_block_view_destroy(GtkObject *object);
+
+
+
+G_DEFINE_TYPE(GtkBlockView, gtk_block_view, GTK_TYPE_BIN_VIEW)
+
+
+
+
+GtkWidget * gtk_block_view_new(void)
+{
+ GtkBlockView *result;
+
+ result = gtk_type_new(gtk_block_view_get_type());
+
+ return GTK_WIDGET(result);
+
+}
+
+
+static void
+gtk_block_view_class_init(GtkBlockViewClass *klass)
+{
+ GtkWidgetClass *widget_class;
+ GtkObjectClass *object_class;
+
+
+ widget_class = (GtkWidgetClass *) klass;
+ object_class = (GtkObjectClass *) klass;
+
+
+ widget_class->button_press_event = gtk_block_view_button_press;
+ widget_class->realize = gtk_block_view_realize;
+ widget_class->size_request = gtk_block_view_size_request;
+ widget_class->size_allocate = gtk_block_view_size_allocate;
+ widget_class->expose_event = gtk_block_view_expose;
+
+ object_class->destroy = gtk_block_view_destroy;
+}
+
+
+static void
+gtk_block_view_init(GtkBlockView *view)
+{
+
+}
+
+
+static void
+gtk_block_view_size_request(GtkWidget *widget,
+ GtkRequisition *requisition)
+{
+ g_return_if_fail(widget != NULL);
+ g_return_if_fail(GTK_IS_BLOCK_VIEW(widget));
+ g_return_if_fail(requisition != NULL);
+
+ requisition->width = 80;
+ requisition->height = 100;
+}
+
+
+static void
+gtk_block_view_size_allocate(GtkWidget *widget,
+ GtkAllocation *allocation)
+{
+ g_return_if_fail(widget != NULL);
+ g_return_if_fail(GTK_IS_BLOCK_VIEW(widget));
+ g_return_if_fail(allocation != NULL);
+
+ widget->allocation = *allocation;
+
+ if (GTK_WIDGET_REALIZED(widget)) {
+ gdk_window_move_resize(
+ widget->window,
+ allocation->x, allocation->y,
+ allocation->width, allocation->height
+ );
+ }
+}
+
+
+static void
+gtk_block_view_realize(GtkWidget *widget)
+{
+ GdkWindowAttr attributes;
+ guint attributes_mask;
+ GdkColor white; /* Couleur de fond normale */
+
+ g_return_if_fail(widget != NULL);
+ g_return_if_fail(GTK_IS_BLOCK_VIEW(widget));
+
+ GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
+
+ attributes.window_type = GDK_WINDOW_CHILD;
+ attributes.x = widget->allocation.x;
+ attributes.y = widget->allocation.y;
+ attributes.width = widget->allocation.width;
+ attributes.height = widget->allocation.height;
+
+ attributes.wclass = GDK_INPUT_OUTPUT;
+ attributes.event_mask = gtk_widget_get_events(widget) | GDK_BUTTON_PRESS_MASK | GDK_EXPOSURE_MASK;
+
+ attributes_mask = GDK_WA_X | GDK_WA_Y;
+
+ widget->window = gdk_window_new(
+ gtk_widget_get_parent_window (widget),
+ & attributes, attributes_mask
+ );
+
+ gdk_window_set_user_data(widget->window, widget);
+
+ widget->style = gtk_style_attach(widget->style, widget->window);
+
+
+ gdk_color_white(gtk_widget_get_colormap(widget), &white);
+ gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, &white);
+
+
+ GTK_BLOCK_VIEW(widget)->layout = gtk_widget_create_pango_layout(widget, NULL);
+ GTK_BLOCK_VIEW(widget)->gc = gdk_gc_new(GDK_DRAWABLE(widget->window));
+
+ gtk_block_view_build_content(GTK_BLOCK_VIEW(widget));
+
+}
+
+
+static gboolean gtk_block_view_button_press(GtkWidget *widget, GdkEventButton *event)
+{
+ gboolean result; /* Décision à retourner */
+ GtkBlockView *view; /* Composant GTK réel */
+ gdouble y; /* Position à manipuler */
+ GRenderingLine *line; /* Ligne de rendu visée */
+
+ result = FALSE;
+
+ view = GTK_BLOCK_VIEW(widget);
+
+ y = event->y;
+ line = g_rendering_line_find_by_y(view->lines, &y);
+
+ if (line != NULL)
+ {
+ /* Clic dans la marge */
+ if (event->type == GDK_BUTTON_PRESS && event->x < (2 * MARGIN_SPACE + view->line_height))
+ {
+ result = TRUE;
+ g_rendering_line_toggle_flag(line, RLF_BREAK_POINT);
+ }
+
+ }
+
+ if (result)
+ {
+ /* TODO: regions */
+ gtk_block_view_paint(view);
+ }
+
+ return result;
+
+}
+
+
+
+/******************************************************************************
+* *
+* Paramètres : line = ligne dont un drapeau a évolué. *
+* view = composant GTK à mettre à jour. *
+* *
+* Description : Redessine l'affichage suite une mise à jour dans la marge. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_block_view_update_margin(GRenderingLine *line, GtkBlockView *view)
+{
+
+
+ gtk_block_view_paint(view);
+
+
+
+
+}
+
+
+
+
+
+static gboolean
+gtk_block_view_expose(GtkWidget *widget,
+ GdkEventExpose *event)
+{
+ g_return_val_if_fail(widget != NULL, FALSE);
+ g_return_val_if_fail(GTK_IS_BLOCK_VIEW(widget), FALSE);
+ g_return_val_if_fail(event != NULL, FALSE);
+
+ gtk_block_view_paint(GTK_BLOCK_VIEW(widget));
+
+
+
+ /*
+
+
+gdk_gc_set_clip_region (GdkGC *gc,
+ const GdkRegion *region);
+
+gdk_window_invalidate_region (GdkWindow *window,
+ const GdkRegion *region,
+ gboolean invalidate_children);
+gdk_window_begin_paint_region (GdkWindow *window,
+ const GdkRegion *region);
+void gdk_window_end_paint (GdkWindow *window);
+ */
+
+
+
+
+
+
+ return TRUE;
+
+}
+
+
+static void
+gtk_block_view_paint(GtkBlockView *view)
+{
+ GtkWidget *widget; /* Version GTK du composant */
+ GdkGCValues values; /* Propriétés du contexte */
+ GdkColor white; /* Couleur du fond */
+ int width; /* Largeur de l'élément */
+ int height; /* Hauteur de l'élément */
+ GdkColor red; /* Couleur des arrêts */
+ PangoLayoutIter *iter; /* Boucle de parcours */
+ unsigned int index; /* Indice de la ligne visée */
+ int y0; /* Ordonnée du haut d'une ligne*/
+ int y1; /* Ordonnée du bas d'une ligne */
+
+ GRenderingLine *liter;
+
+
+ widget = GTK_WIDGET(view);
+ gdk_gc_get_values(view->gc, &values);
+
+ gdk_color_white(gtk_widget_get_colormap(widget), &white);
+ gdk_gc_set_foreground(view->gc, &white);
+
+ gtk_widget_get_size_request(widget, &width, &height);
+
+ gdk_draw_rectangle(GDK_DRAWABLE(widget->window), GTK_BLOCK_VIEW(widget)->gc,
+ TRUE, 0, 0, width, height);
+
+ gdk_color_parse("#ff0000", &red);
+ gdk_color_alloc(gtk_widget_get_colormap(widget), &red);
+ gdk_gc_set_foreground(view->gc, &red);
+
+
+ index = 0;
+ iter = pango_layout_get_iter(view->layout);
+
+#if 0
+ for (; index < view->info_count; index++, pango_layout_iter_next_line(iter))
+ {
+ if (!view->info[index].bp_set) continue;
+
+ pango_layout_iter_get_line_yrange(iter, &y0, &y1);
+
+
+
+ gdk_draw_arc(GDK_DRAWABLE(widget->window), GTK_BLOCK_VIEW(widget)->gc,
+ FALSE, MARGIN_SPACE, y0 / PANGO_SCALE,
+ view->line_height - 2, view->line_height - 2,
+ 0, 360 * 64);
+
+ }
+#endif
+
+ pango_layout_iter_free(iter);
+
+ gdk_gc_set_foreground(view->gc, &values.foreground);
+
+ gdk_draw_layout(GDK_DRAWABLE(widget->window), view->gc,
+ 2 * MARGIN_SPACE + view->line_height, 0,
+ view->layout);
+
+
+ y0 = 0;
+
+ for (/* l! */liter = view->lines; liter != NULL; liter = g_rendering_line_get_next_iter(view->lines, liter))
+ {
+ g_rendering_line_draw(liter, GDK_DRAWABLE(widget->window), view->gc,
+ MARGIN_SPACE, 2 * MARGIN_SPACE + view->line_height,
+ y0, view->line_height);
+
+ y0 += view->line_height;
+
+ }
+
+
+}
+
+
+static void
+gtk_block_view_destroy(GtkObject *object)
+{
+ GtkBlockView *cpu;
+ GtkBlockViewClass *klass;
+
+ g_return_if_fail(object != NULL);
+ g_return_if_fail(GTK_IS_BLOCK_VIEW(object));
+
+ cpu = GTK_BLOCK_VIEW(object);
+
+ klass = gtk_type_class(gtk_widget_get_type());
+
+ if (GTK_OBJECT_CLASS(klass)->destroy) {
+ (* GTK_OBJECT_CLASS(klass)->destroy) (object);
+ }
+}
+
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : view = composant GTK à mettre à jour. *
+* show = état de l'affichage auquel parvenir. *
+* *
+* Description : Choisit d'afficher les adresses virtuelles ou non. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_block_view_show_vaddress(GtkBlockView *view, gboolean show)
+{
+ view->show_vaddress = show;
+
+ gtk_block_view_build_content(view);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : view = composant GTK à mettre à jour. *
+* show = état de l'affichage auquel parvenir. *
+* *
+* Description : Choisit d'afficher le code brut ou non. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_block_view_show_code(GtkBlockView *view, gboolean show)
+{
+ view->show_code = show;
+
+ gtk_block_view_build_content(view);
+
+}
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : view = composant GTK à mettre à jour. *
+* format = format du binaire affiché. *
+* *
+* Description : Définit le format auquel le contenu est lié. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_block_view_set_format(GtkBlockView *view, const exe_format *format)
+{
+ view->format = format;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : view = composant GTK à mettre à jour. *
+* lines = informations à intégrer. *
+* *
+* Description : Définit les lignes du bloc de représentation. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_block_view_set_rendering_lines(GtkBlockView *view, GRenderingLine *lines)
+{
+ GRenderingLine *iter; /* Boucle de parcours */
+
+ view->lines = lines;
+
+ for (iter = lines; iter != NULL; iter = g_rendering_line_get_next_iter(lines, iter))
+ g_signal_connect(iter, "rendering-line-flags-changed",
+ G_CALLBACK(gtk_block_view_update_margin), view);
+
+ g_rendering_line_update_bin_len(lines);
+
+ gtk_block_view_recompute_size_request(view);
+
+}
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : view = composant GTK à mettre à jour. *
+* *
+* Description : Réclame une nouvelle taille adaptée au contenu présent. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_block_view_recompute_size_request(GtkBlockView *view)
+{
+ int width; /* Largeur de l'objet actuelle */
+ int height; /* Hauteur de l'objet actuelle */
+
+ g_rendering_line_get_size(view->lines, &width, &height, &view->line_height);
+
+ gtk_widget_set_size_request(GTK_WIDGET(view),
+ width + 2 * MARGIN_SPACE + view->line_height,
+ height);
+
+}
+
+
+
+
+
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : view = composant GTK à mettre à jour. *
+* *
+* Description : Définit le contenu visuel à partir des infos enregistrées. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_block_view_build_content(GtkBlockView *view)
+{
+#if 0
+ const uint8_t *exe_content; /* Contenu binaire global */
+ off_t max_bin_len; /* Taille max du code brut */
+ unsigned int i; /* Boucle de traitement */
+ off_t bin_len; /* Taille d'instruction */
+ char *bin_code; /* Tampon du code binaire */
+ char *content; /* Contenu à définir */
+ size_t content_len; /* Taille du contenu */
+ AdressMode mode; /* Affichage des adresses */
+ char buffer[CONTENT_BUFFER_LEN]; /* Zone tampon à utiliser */
+ off_t bin_offset; /* Début de l'instruction */
+ off_t k; /* Boucle de parcours #2 */
+ off_t j; /* Boucle de parcours #1 */
+ int width; /* Largeur de l'objet actuelle */
+ int height; /* Hauteur de l'objet actuelle */
+ PangoLayoutIter *iter; /* Boucle de parcours */
+ int y0; /* Ordonnée du haut d'une ligne*/
+ int y1; /* Ordonnée du bas d'une ligne */
+
+ /* Calcul de la largeur maximale brute si besoin est */
+ if (view->show_code)
+ {
+ exe_content = get_exe_content(view->format, NULL);
+
+ max_bin_len = 1;
+
+ for (i = 0; i < view->info_count; i++)
+ {
+ /* Commentaire uniquement */
+ if (view->info[i].instr == NULL) continue;
+
+ get_asm_instr_offset_and_length(view->info[i].instr, NULL, &bin_len);
+ if (bin_len > max_bin_len) max_bin_len = bin_len;
+
+ }
+
+ max_bin_len = max_bin_len * 2 + (max_bin_len - 1);
+ bin_code = (char *)calloc(max_bin_len + 1, sizeof(char));
+
+ }
+
+ content_len = strlen("<tt>") + 1;
+ content = (char *)calloc(content_len, sizeof(char));
+ strcpy(content, "<tt>");
+
+ mode = ADM_32BITS; /* FIXME */
+
+ for (i = 0; i < view->info_count; i++)
+ {
+ if (i > 0)
+ {
+ content = (char *)realloc(content, ++content_len * sizeof(char));
+ strcat(content, "\n");
+ }
+
+ /* Eventuelle adresse virtuelle */
+
+ if (view->show_vaddress)
+ {
+ switch (mode)
+ {
+ case ADM_32BITS:
+ snprintf(buffer, CONTENT_BUFFER_LEN,
+ "<span foreground='#333333'>0x%08llx</span>",
+ view->info[i].offset);
+ break;
+
+ case ADM_64BITS:
+ snprintf(buffer, CONTENT_BUFFER_LEN,
+ "<span foreground='#333333'>0x%16llx</span>",
+ view->info[i].offset);
+ break;
+
+ }
+
+ content_len += strlen(buffer);
+ content = (char *)realloc(content, content_len * sizeof(char));
+ strcat(content, buffer);
+
+ }
+
+ /* Eventuel code brut */
+
+ if (view->show_code)
+ {
+ k = 0;
+
+ if (view->info[i].instr != NULL)
+ {
+ get_asm_instr_offset_and_length(view->info[i].instr, &bin_offset, &bin_len);
+
+ for (j = 0; j < bin_len; j++)
+ {
+ if ((j + 1) < bin_len)
+ k += snprintf(&bin_code[j * (2 + 1)], 4, "%02hhx ", exe_content[bin_offset + j]);
+ else
+ k += snprintf(&bin_code[j * (2 + 1)], 3, "%02hhx", exe_content[bin_offset + j]);
+ }
+
+ }
+
+ for (; k < max_bin_len; k++)
+ snprintf(&bin_code[k], 2, " ");
+
+ if (view->show_vaddress) content_len += strlen("\t");
+ content_len += strlen(bin_code);
+ content = (char *)realloc(content, content_len * sizeof(char));
+ if (view->show_vaddress) strcat(content, "\t");
+ strcat(content, bin_code);
+
+ }
+
+ /* Eventuelle instruction */
+
+ if (view->info[i].instr != NULL)
+ {
+ print_hinstruction(view->proc, view->format, view->info[i].instr, buffer, CONTENT_BUFFER_LEN, ASX_INTEL);
+
+ if (view->show_vaddress || view->show_code) content_len += strlen("\t");
+ content_len += strlen(buffer);
+
+ content = (char *)realloc(content, content_len * sizeof(char));
+ if (view->show_vaddress || view->show_code) strcat(content, "\t");
+ strcat(content, buffer);
+
+ }
+
+ /* Eventuel commantaire */
+
+ if (view->info[i].comment != NULL)
+ {
+ if (view->show_vaddress || view->show_code) content_len += strlen("\t");
+ content_len += strlen("<b><span foreground='#003300'>; ") + strlen(view->info[i].comment) + strlen("</span></b>");
+
+ content = (char *)realloc(content, content_len * sizeof(char));
+ if (view->show_vaddress || view->show_code) strcat(content, "\t");
+ strcat(content, "<b><span foreground='#003300'>; ");
+ strcat(content, view->info[i].comment);
+ strcat(content, "</span></b>");
+
+ }
+
+ }
+
+ content_len += strlen("</tt>");
+ content = (char *)realloc(content, content_len * sizeof(char));
+ strcat(content, "</tt>");
+
+ if (view->show_code) free(bin_code);
+
+ pango_layout_set_markup(view->layout, content, content_len - 1);
+
+ pango_layout_get_pixel_size(view->layout, &width, &height);
+
+ view->line_height = 0;
+ iter = pango_layout_get_iter(view->layout);
+
+ do
+ {
+ pango_layout_iter_get_line_yrange(iter, &y0, &y1);
+ view->line_height = MAX(view->line_height, (y1 - y0) / PANGO_SCALE);
+ }
+ while (pango_layout_iter_next_line(iter));
+
+ pango_layout_iter_free(iter);
+
+ //gtk_widget_set_size_request(GTK_WIDGET(block_view), width + 2 * MARGIN_SPACE + view->line_height, height);
+#endif
+}
+
+
+
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : view = composant GTK à consulter. *
+* address = adresse à présenter à l'écran. *
+* position = position verticale au sein du composant. [OUT] *
+* *
+* Description : Indique la position verticale d'une adresse donnée. *
+* *
+* Retour : TRUE si l'adresse fait partie du composant, FALSE sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+gboolean gtk_block_view_get_address_vposition(GtkBlockView *view, uint64_t address, gint *position)
+{
+ unsigned int i; /* Boucle de parcours */
+
+ *position = 0;
+#if 0
+ for (i = 0; i < view->info_count; i++)
+ {
+ if (view->info[i].offset == address) break;
+ else *position += view->line_height;
+ }
+#endif
+ return false;//(i < view->info_count);
+
+}
+