summaryrefslogtreecommitdiff
path: root/src/gtkext/graph/layout.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2012-11-02 15:50:07 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2012-11-02 15:50:07 (GMT)
commitf5df6496fa50927d3d274c939a888afde652b7ad (patch)
tree281dbfdfdcb8765fea7036af274c63fb5acde8ff /src/gtkext/graph/layout.c
parentc3aba0893c29cc098c029306fd7a4c8c1fa2eee2 (diff)
Improved the computing and the rendering of the graphic view.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@277 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'src/gtkext/graph/layout.c')
-rw-r--r--src/gtkext/graph/layout.c229
1 files changed, 229 insertions, 0 deletions
diff --git a/src/gtkext/graph/layout.c b/src/gtkext/graph/layout.c
new file mode 100644
index 0000000..69e32cc
--- /dev/null
+++ b/src/gtkext/graph/layout.c
@@ -0,0 +1,229 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * layout.c - mise en place de graphique
+ *
+ * Copyright (C) 2009-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 Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "layout.h"
+
+
+#include <malloc.h>
+#include <string.h>
+#include <gtk/gtkfixed.h>
+
+
+#include "dot.h"
+#include "node.h"
+#include "../gtkbufferview.h"
+#include "../../analysis/binary.h"
+#include "../../common/extstr.h"
+
+
+
+/* Taille maximale des descriptions de liens */
+#define LINKS_DESC_LEN 128
+
+
+/* Etablit tous les liens entre les différents morceaux de code. */
+static char *complete_graph_links(const GtkGraphView *, GtkViewPanel **, size_t, char *);
+
+
+
+/******************************************************************************
+* *
+* Paramètres : view = support où placer les différents éléments. *
+* views = morceaux de code à afficher de façon organisée. *
+* count = quantité de ces morceaux de code. *
+* *
+* Description : Dispose une série de morceaux d'affichage en graphique. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool build_graph_view(GtkGraphView *view, GtkViewPanel **views, size_t count)
+{
+ GGraphNode **nodes; /* Intermédiaires en place */
+ size_t i; /* Boucle de parcours */
+ char *cmds; /* Description à envoyer à dot */
+ graph_layout *layout; /* Graphique construit */
+ GtkLinkRenderer **links; /* Liens graphiques construits */
+ size_t links_count; /* Quantité de ces liens */
+
+ /* Création de la glue */
+
+ nodes = (GGraphNode **)calloc(count, sizeof(GGraphNode *));
+
+ for (i = 0; i < count; i++)
+ nodes[i] = g_graph_node_new(GTK_WIDGET(views[i]));
+
+ /* Définition du graphique */
+
+ cmds = strdup("digraph G {\noverlap=false;\n splines=ortho;\n compound=true;\n");
+
+ for (i = 0; i < count; i++)
+ cmds = g_graph_node_register_for_dot(nodes[i], cmds);
+
+ cmds = complete_graph_links(view, views, count, cmds);
+
+ cmds = stradd(cmds, "}");
+
+ layout = create_graph_layout(cmds);
+
+ /* Affichage du graphique */
+
+ place_nodes_of_graph_layout(layout, view, nodes, count);
+
+ links = create_links_from_graph_layout(layout, &links_count, nodes, count);
+ gtk_graph_view_attach_links(view, links, links_count);
+
+ gtk_widget_queue_draw(GTK_WIDGET(view));
+
+ delete_graph_layout(layout);
+
+ /* TODO : free nodes */
+
+ return true;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : view = support contenant les différentes lignes. *
+* views = morceaux de code à afficher de façon organisée. *
+* count = quantité de ces morceaux de code. *
+* desc = description du graphique à compléter. [OUT] *
+* *
+* Description : Etablit tous les liens entre les différents morceaux de code.*
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static char *complete_graph_links(const GtkGraphView *view, GtkViewPanel **views, size_t count, char *desc)
+{
+ GLoadedBinary *binary; /* Binaire rattaché aux vues */
+ GArchInstruction *instrs; /* Instructions pour assembleur*/
+ GBufferView *buffer; /* Tampon d'une partie de code */
+ vmpa_t end; /* Adresse finale du tampon */
+ size_t i; /* Boucle de parcours #1 */
+ GArchInstruction *last; /* Dernière instruc. d'un bloc */
+ vmpa_t addr; /* Addresse d'instruction */
+ GArchInstruction **dests; /* Instr. visée par une autre */
+ InstructionLinkType *types; /* Type de lien entre lignes */
+ size_t dcount; /* Nombre de liens de dest. */
+ size_t j; /* Boucle de parcours #2 */
+ size_t k; /* Boucle de parcours #3 */
+ char cmd[LINKS_DESC_LEN]; /* Tampon pour l'ajout de liens*/
+ GArchInstruction *next; /* Instruction suivante */
+
+ if (count == 0)
+ return desc;
+
+ binary = gtk_view_panel_get_binary(views[0]);
+ instrs = g_loaded_binary_get_instructions(binary);
+
+ for (i = 0; i < count; i++)
+ {
+ buffer = gtk_buffer_view_get_buffer(GTK_BUFFER_VIEW(views[i]));
+ g_buffer_view_get_restrictions(buffer, NULL, &end);
+
+ last = g_arch_instruction_find_by_address(instrs, end, true);
+ g_arch_instruction_get_location(last, NULL, NULL, &addr);
+
+ if (g_arch_instruction_has_destinations(last))
+ {
+ dcount = g_arch_instruction_get_destinations(last, &dests, &types);
+
+ for (j = 0; j < dcount; j++)
+ {
+ g_arch_instruction_get_location(dests[j], NULL, NULL, &addr);
+
+ for (k = 0; k < count; k++)
+ if (gtk_view_panel_contain_address(views[k], addr))
+ break;
+
+ if (k < count)
+ switch (types[j])
+ {
+ case ILT_JUMP:
+ snprintf(cmd, LINKS_DESC_LEN,
+ "_%p:s -> _%p:n [ltail=cluster_%p, lhead=cluster_%p];\n",
+ views[i], views[k], views[i], views[k]);
+ desc = stradd(desc, cmd);
+ break;
+
+ case ILT_JUMP_IF_TRUE:
+ snprintf(cmd, LINKS_DESC_LEN,
+ "_%p:s -> _%p:n [ltail=cluster_%p, lhead=cluster_%p, " \
+ "color=green];\n",
+ views[i], views[k], views[i], views[k]);
+ desc = stradd(desc, cmd);
+ break;
+
+ case ILT_JUMP_IF_FALSE:
+ snprintf(cmd, LINKS_DESC_LEN,
+ "_%p:s -> _%p:n [ltail=cluster_%p, lhead=cluster_%p, " \
+ "color=red];\n",
+ views[i], views[k], views[i], views[k]);
+ desc = stradd(desc, cmd);
+ break;
+
+ default:
+ break;
+
+ }
+
+ }
+
+ }
+
+ /* Sinon on suit le flux normal */
+ else
+ {
+ next = g_arch_instruction_get_next_iter(instrs, last, VMPA_MAX);
+ if (next == NULL) continue;
+
+ g_arch_instruction_get_location(next, NULL, NULL, &addr);
+
+ for (k = 0; k < count; k++)
+ if (gtk_view_panel_contain_address(views[k], addr))
+ break;
+
+ if (k < count)
+ {
+ snprintf(cmd, LINKS_DESC_LEN,
+ "_%p:s -> _%p:n [ltail=cluster_%p, lhead=cluster_%p];\n",
+ views[i], views[k], views[i], views[k]);
+ desc = stradd(desc, cmd);
+ }
+
+ }
+
+ }
+
+ return desc;
+
+}