summaryrefslogtreecommitdiff
path: root/src/gtkext/graph/nodes/flow.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gtkext/graph/nodes/flow.c')
-rw-r--r--src/gtkext/graph/nodes/flow.c732
1 files changed, 732 insertions, 0 deletions
diff --git a/src/gtkext/graph/nodes/flow.c b/src/gtkext/graph/nodes/flow.c
new file mode 100644
index 0000000..d4506ea
--- /dev/null
+++ b/src/gtkext/graph/nodes/flow.c
@@ -0,0 +1,732 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * flow.c - encapsulation graphique des blocs d'exécution
+ *
+ * Copyright (C) 2013 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 "flow.h"
+
+
+#include "../layout.h"
+#include "../node-int.h"
+
+
+
+/* Type d'un accrochage pour lien */
+struct _node_slot_t
+{
+ GArchInstruction *instr; /* Instruction liée */
+ size_t group_index; /* Rang dans le grp d'origine */
+
+ InstructionLinkType type; /* Type de lien à représenter */
+ size_t slot_index; /* Position dans l'accroche */
+ gint x; /* Abscisse graphique du rendu */
+
+};
+
+/* Encapsulation graphique d'un bloc d'exécution (instance) */
+struct _GFlowNode
+{
+ GGraphNode parent; /* A laisser en premier */
+
+ GFlowBlock *block; /* Bloc d'exécution associé */
+ GtkBufferView *view; /* Représentation graphique */
+
+ GtkAllocation alloc; /* Emplacement du bloc rattaché*/
+
+ node_slot_t *entries; /* Positions des pts d'entrées */
+ size_t entries_count; /* Nombre de ces points */
+ node_slot_t *exits; /* Positions des pts de sorties*/
+ size_t exits_count; /* Nombre de ces points */
+
+};
+
+/* Encapsulation graphique d'un bloc d'exécution (classe) */
+struct _GFlowNodeClass
+{
+ GGraphNodeClass parent; /* A laisser en premier */
+
+};
+
+
+#define SPACE_BETWEEN_SLOT 15
+
+
+/* Initialise la classe des encapsulations de bloc d'exécution. */
+static void g_flow_node_class_init(GFlowNodeClass *);
+
+/* Initialise une encapsulation de bloc d'exécution. */
+static void g_flow_node_init(GFlowNode *);
+
+/* Supprime toutes les références externes. */
+static void g_flow_node_dispose(GFlowNode *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_flow_node_finalize(GFlowNode *);
+
+/* Fournit le rang du noeud dans le graphique. */
+static unsigned int g_flow_node_get_rank(const GFlowNode *);
+
+/* Altère la position du noeud d'encapsulation. */
+static void g_flow_node_set_position(GFlowNode *, gint *, gint *);
+
+/* Fournit la position du noeud d'encapsulation. */
+static void g_flow_node_get_position(const GFlowNode *, gint *, gint *);
+
+/* Indique l'espace requis pour un noeud d'encapsulation. */
+static GtkAllocation g_flow_node_get_allocation(const GFlowNode *);
+
+/* Parcourt tous les blocs d'instructions dans un ordre donné. */
+static bool g_flow_node_visit_flow_nodes(GFlowNode *, graph_node_visitor_cb, void *);
+
+
+
+/* ------------------------ GESTION DES ACCROCHES D'UN NOEUD ------------------------ */
+
+
+/* Prépare les points d'entrées du noeud. */
+static void g_flow_node_setup_entry_slots(GFlowNode *);
+
+/* Prépare les points de sorties du noeud. */
+static void g_flow_node_setup_exit_slots(GFlowNode *);
+
+/* Etablit un lien entre deux noeuds graphiques. */
+static node_slot_t *g_flow_node_get_slot(const GFlowNode *, bool, GArchInstruction *, size_t);
+
+
+
+/* Indique le type définit par la GLib pour l'encapsulation d'un bloc d'exécution. */
+G_DEFINE_TYPE(GFlowNode, g_flow_node, G_TYPE_GRAPH_NODE);
+
+
+/******************************************************************************
+* *
+* Paramètres : klass = classe à initialiser. *
+* *
+* Description : Initialise la classe des encapsulations de bloc d'exécution. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_flow_node_class_init(GFlowNodeClass *klass)
+{
+ GObjectClass *object; /* Autre version de la classe */
+
+ object = G_OBJECT_CLASS(klass);
+
+ object->dispose = (GObjectFinalizeFunc/* ! */)g_flow_node_dispose;
+ object->finalize = (GObjectFinalizeFunc)g_flow_node_finalize;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = instance à initialiser. *
+* *
+* Description : Initialise une encapsulation de bloc d'exécution. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_flow_node_init(GFlowNode *node)
+{
+ GGraphNode *base; /* Version basique */
+
+ base = G_GRAPH_NODE(node);
+
+ base->get_rank = (get_node_rank_fc)g_flow_node_get_rank;
+ base->set_pos = (node_set_pos_fc)g_flow_node_set_position;
+ base->get_pos = (node_get_pos_fc)g_flow_node_get_position;
+ base->get_alloc = (node_get_alloc_fc)g_flow_node_get_allocation;
+ base->visit = (visit_flow_nodes_fc)g_flow_node_visit_flow_nodes;
+
+ node->alloc.x = UNINITIALIZED_NODE_POS;
+ node->alloc.y = UNINITIALIZED_NODE_POS;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = instance d'objet GLib à traiter. *
+* *
+* Description : Supprime toutes les références externes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_flow_node_dispose(GFlowNode *node)
+{
+ size_t i; /* Boucle de parcours */
+
+ g_object_unref(G_OBJECT(node->block));
+ g_object_unref(G_OBJECT(node->view));
+
+ for (i = 0; i < node->entries_count; i++)
+ g_object_unref(G_OBJECT(node->entries[i].instr));
+
+ for (i = 0; i < node->exits_count; i++)
+ g_object_unref(G_OBJECT(node->exits[i].instr));
+
+ G_OBJECT_CLASS(g_flow_node_parent_class)->dispose(G_OBJECT(node));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = instance d'objet GLib à traiter. *
+* *
+* Description : Procède à la libération totale de la mémoire. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_flow_node_finalize(GFlowNode *node)
+{
+ if (node->entries != NULL)
+ free(node->entries);
+
+ if (node->exits != NULL)
+ free(node->exits);
+
+ G_OBJECT_CLASS(g_flow_node_parent_class)->finalize(G_OBJECT(node));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : block = bloc d'exécution représenté graphiquement. *
+* view = morceau d'affichage à représenter. *
+* *
+* Description : Encapsule graphiquement un bloc d'exécution. *
+* *
+* Retour : Adresse de la structure mise en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GGraphNode *g_flow_node_new(GFlowBlock *block, GtkBufferView *view)
+{
+ GFlowNode *result; /* Structure à retourner */
+ GtkRequisition requisition; /* Taille à l'écran actuelle */
+
+ result = g_object_new(G_TYPE_FLOW_NODE, NULL);
+
+ g_object_ref(G_OBJECT(block));
+ g_object_ref(G_OBJECT(view));
+
+ result->block = block;
+ result->view = view;
+
+ gtk_widget_size_request(GTK_WIDGET(result->view), &requisition);
+
+ result->alloc.width = requisition.width;
+ result->alloc.height = requisition.height;
+
+ g_flow_node_setup_entry_slots(result);
+ g_flow_node_setup_exit_slots(result);
+
+ return G_GRAPH_NODE(result);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à consulter. *
+* *
+* Description : Fournit le rang du noeud dans le graphique. *
+* *
+* Retour : Indice supérieur ou égal à zéro. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static unsigned int g_flow_node_get_rank(const GFlowNode *node)
+{
+ return g_flow_block_get_rank(node->block);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à manipuler. *
+* x = éventuelle abscisse à intégrer ou NULL. *
+* y = éventuelle ordonnée à intégrer ou NULL. *
+* *
+* Description : Altère la position du noeud d'encapsulation. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_flow_node_set_position(GFlowNode *node, gint *x, gint *y)
+{
+ if (x != NULL) node->alloc.x = *x;
+ if (y != NULL) node->alloc.y = *y;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à consulter. *
+* x = éventuelle abscisse à recevoir ou NULL. [OUT] *
+* y = éventuelle ordonnée à recevoir ou NULL. [OUT] *
+* *
+* Description : Fournit la position du noeud d'encapsulation. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_flow_node_get_position(const GFlowNode *node, gint *x, gint *y)
+{
+ if (x != NULL) *x = node->alloc.x;
+ if (y != NULL) *y = node->alloc.y;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à consulter. *
+* *
+* Description : Indique l'espace requis pour un noeud d'encapsulation. *
+* *
+* Retour : Espace constitué, entièrement ou non. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static GtkAllocation g_flow_node_get_allocation(const GFlowNode *node)
+{
+ GtkAllocation result; /* Valeurs à retourner */
+
+ result = node->alloc;
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique démarrant la visite. *
+* callback = ensemble de blocs à parcourir. *
+* data = donnée utilisateur à associer au parcours. *
+* *
+* Description : Parcourt tous les blocs d'instructions dans un ordre donné. *
+* *
+* Retour : true si le parcours a été jusqu'à son terme, false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static bool g_flow_node_visit_flow_nodes(GFlowNode *node, graph_node_visitor_cb callback, void *data)
+{
+ return callback(G_GRAPH_NODE(node), data);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à consulter. *
+* instr = instruction à considérer. *
+* *
+* Description : Précise si le noeud a pour première instruction celle donnée.*
+* *
+* Retour : true si la première instruction du noeud est celle indiquée. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool g_flow_node_start_with(const GFlowNode *node, GArchInstruction *instr)
+{
+ GArchInstruction *first; /* Première instr. du noeud */
+
+ g_flow_block_get_boundary(node->block, &first, NULL);
+
+ /*
+ do
+ {
+ vmpa_t a1, a2;
+
+ g_arch_instruction_get_location(instr, NULL, NULL, &a1);
+ g_arch_instruction_get_location(first, NULL, NULL, &a2);
+
+ printf(" -- compare %p (0x%08lx) vs %p (0x%08lx)\n",
+ instr, a1, first, a2);
+
+ }
+ while (0);
+ */
+
+ return (first == instr);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à consulter. *
+* ranks = classement à mettre à jour. *
+* *
+* Description : Précise la hauteur minimale requise pour un noeud. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void g_flow_node_register_rank(const GFlowNode *node, GGraphRanks *ranks)
+{
+ unsigned int index; /* Indice du rang associé */
+
+ index = g_flow_block_get_next_rank(node->block);
+
+ g_graph_ranks_set_min_height(ranks, index, node->alloc.height);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à mettre à jour. *
+* ranks = classement à consulter. *
+* *
+* Description : Définit l'ordonnée de l'espace attribué à un noeud. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void g_flow_node_apply_rank(GFlowNode *node, GGraphRanks *ranks)
+{
+ unsigned int index; /* Indice du rang associé */
+
+ index = g_flow_block_get_next_rank(node->block);
+
+ node->alloc.y = g_graph_ranks_get_y_for_rank(ranks, index);
+
+}
+
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à mettre à jour. *
+* layout = disposition globale du graphique. *
+* nodes = ensemble des noeuds en place. *
+* *
+* Description : Etablit les liaison entre un noeud et ses suivants. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void g_flow_node_link(GFlowNode *node, GGraphLayout *layout, GGraphNode *nodes)
+{
+ GArchInstruction *last; /* Dernière instr. du noeud */
+ size_t i; /* Boucle de parcours */
+ GFlowNode *target; /* Bloc visé par le lien */
+ node_slot_t *dest_slot; /* Accrochage d'arrivée */
+ EdgeColor color; /* Couleur du lien à créer */
+ GGraphEdge *edge; /* Lien à mettre en place */
+
+ g_flow_block_get_boundary(node->block, NULL, &last);
+
+ for (i = 0; i < node->exits_count; i++)
+ {
+ target = G_FLOW_NODE(find_node_for_instruction(nodes, node->exits[i].instr));
+
+ dest_slot = g_flow_node_get_slot(target, true,
+ last, node->exits[i].group_index);
+
+ switch (node->exits[i].type)
+ {
+ case ILT_JUMP_IF_TRUE:
+ color = EGC_GREEN;
+ break;
+ case ILT_JUMP_IF_FALSE:
+ color = EGC_RED;
+ break;
+ case ILT_LOOP:
+ color = EGC_BLUE;
+ break;
+ case ILT_CATCH_EXCEPTION:
+ color = EGC_DASHED_GRAY;
+ break;
+ default:
+ color = EGC_DEFAULT;
+ break;
+
+ }
+
+ edge = g_graph_edge_new(node, &node->exits[i], target, dest_slot, color);
+ g_graph_layout_add_edge(layout, edge);
+
+ }
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = intermédiaire à consulter. *
+* view = support de destination. *
+* *
+* Description : Place un noeud sur son support final. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void g_flow_node_place(const GFlowNode *node, GtkGraphView *view)
+{
+ printf("alloc %p :: (%d ; %d) - (%d ; %d)\n",
+ node->view,
+ node->alloc.x, node->alloc.y,
+ node->alloc.width, node->alloc.height);
+
+ gtk_graph_view_put(view, GTK_WIDGET(node->view), &node->alloc);
+
+}
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* GESTION DES ACCROCHES D'UN NOEUD */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+* *
+* Paramètres : node = intermédiaire à compléter. *
+* *
+* Description : Prépare les points d'entrées du noeud. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_flow_node_setup_entry_slots(GFlowNode *node)
+{
+ GArchInstruction *first; /* Première instr. du noeud */
+ GArchInstruction **instrs; /* Instr. visée par une autre */
+ InstructionLinkType *types; /* Type de lien entre lignes */
+ size_t icount; /* Nombre de liens de dest. */
+ size_t i; /* Boucle de parcours */
+
+ g_flow_block_get_boundary(node->block, &first, NULL);
+ icount = g_arch_instruction_get_sources(first, &instrs, &types);
+
+ node->entries = (node_slot_t *)calloc(icount, sizeof(node_slot_t));
+ node->entries_count = icount;
+
+ for (i = 0; i < icount; i++)
+ {
+ g_object_ref(instrs[i]);
+ node->entries[i].instr = instrs[i];
+
+ node->entries[i].type = types[i];
+ node->entries[i].group_index = g_arch_instruction_compute_group_index(&instrs[i],
+ instrs, icount);
+ node->entries[i].slot_index = i;
+
+ }
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = intermédiaire à compléter. *
+* *
+* Description : Prépare les points de sorties du noeud. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_flow_node_setup_exit_slots(GFlowNode *node)
+{
+ GArchInstruction *last; /* Dernière instr. du noeud */
+ GArchInstruction **instrs; /* Instr. visée par une autre */
+ InstructionLinkType *types; /* Type de lien entre lignes */
+ size_t icount; /* Nombre de liens de dest. */
+ size_t i; /* Boucle de parcours */
+
+ g_flow_block_get_boundary(node->block, NULL, &last);
+ icount = g_arch_instruction_get_destinations(last, &instrs, &types, NULL);
+
+ node->exits = (node_slot_t *)calloc(icount, sizeof(node_slot_t));
+ node->exits_count = icount;
+
+ for (i = 0; i < icount; i++)
+ {
+ g_object_ref(instrs[i]);
+ node->exits[i].instr = instrs[i];
+
+ node->exits[i].type = types[i];
+ node->exits[i].group_index = g_arch_instruction_compute_group_index(&instrs[i],
+ instrs, icount);
+ node->exits[i].slot_index = i;
+
+ }
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à consulter. *
+* entry = indique le type d'accrochage recherché. *
+* instr = instruction visée. *
+* index = indice de groupe pour départager au besoin. *
+* *
+* Description : Etablit un lien entre deux noeuds graphiques. *
+* *
+* Retour : Lien graphique mis en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static node_slot_t *g_flow_node_get_slot(const GFlowNode *node, bool entry, GArchInstruction *instr, size_t index)
+{
+ node_slot_t *result; /* Emplacement à retourner */
+ node_slot_t *list; /* Liste à parcourir */
+ size_t count; /* Taille de cette liste */
+ size_t i; /* Boucle de parcours */
+
+ if (entry)
+ {
+ list = node->entries;
+ count = node->entries_count;
+ }
+ else
+ {
+ list = node->exits;
+ count = node->exits_count;
+ }
+
+ for (i = 0; i < count; i++)
+ {
+ result = list + i;
+
+ if (result->instr == instr/* && result->group_index == index*/)
+ break;
+
+ }
+
+ return (i == count ? NULL : result);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud graphique à consulter. *
+* entry = indique le type d'accrochage recherché. *
+* slot = accroche dont l'emplacement est à définir. *
+* *
+* Description : Localise un point d'accroche à un noeud graphique. *
+* *
+* Retour : Emplacement réservé pour l'accroche fournie. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GdkPoint g_flow_node_get_point_from_slot(const GFlowNode *node, bool entry, const node_slot_t *slot)
+{
+ GdkPoint result; /* Position à retourner */
+ node_slot_t *slots; /* Accroches à parcourir */
+ size_t count; /* Nombre de ces points */
+ gint slots_width; /* Largeur des accroches */
+ gint slots_left; /* Abscisse de la première */
+ size_t index; /* Indice de l'accroche visée */
+
+ if (entry)
+ {
+ result.y = node->alloc.y;
+
+ slots = node->entries;
+ count = node->entries_count;
+
+ }
+ else
+ {
+ result.y = node->alloc.y + node->alloc.height;
+
+ slots = node->exits;
+ count = node->exits_count;
+
+ }
+
+ slots_width = (count - 1) * SPACE_BETWEEN_SLOT;
+ slots_left = node->alloc.x + (node->alloc.width - slots_width) / 2;
+
+ index = (slot - slots)/* / sizeof(node_slot_t)*/;
+ /* BUG_ON(index >= count); */
+
+ result.x = slots_left + index * SPACE_BETWEEN_SLOT;
+
+ return result;
+
+}