summaryrefslogtreecommitdiff
path: root/src/graph/layout.c
blob: 93d6b28e5ae7f5bcee85afb47d8464b075a034b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

/* OpenIDA - Outil d'analyse de fichiers binaires
 * layout.c - mise en place de graphique
 *
 * Copyright (C) 2009 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 "../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 *, GtkBinView **, 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, GtkBinView **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(views[i]);
 
    /* Définition du graphique */

    cmds = strdup("digraph G {\noverlap=false;\n  splines=true;\n");
    //cmds = strdup("digraph G {\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, "}");

    printf("first step :: '%s'\n", cmds);

    layout = create_graph_layout(cmds);

    /* Affichage du graphique */

    place_nodes_of_graph_layout(layout, GTK_FIXED(view),  nodes, count);

    links = create_links_from_graph_layout(layout, &links_count);
    gtk_graph_view_attach_links(view, links, links_count);

    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.                *
*                                                                             *
*  Description : Etablit tous les liens entre les différents morceaux de code.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static char *complete_graph_links(const GtkGraphView *view, GtkBinView **views, size_t count, char *desc)
{
    size_t i;                               /* Boucle de parcours #1       */
    GRenderingLine *last;                   /* Dernière ligne d'un bloc    */
    GRenderingLine *dest;                   /* Ligne visée par une autre   */
    InstructionLinkType type;               /* Type de lien entre lignes   */
    bool link_next;                         /* Lien auto. avec la suivante */
    vmpa_t addr;                            /* Addresse de destination     */
    size_t k;                               /* Boucle de parcours #2       */
    char buffer[LINKS_DESC_LEN];            /* Tampon pour l'ajout de liens*/

    for (i = 0; i < count; i++)
    {
        last = gtk_bin_view_get_last_line(views[i]);

        if (g_rendering_line_has_destination(last))
        {
            link_next = false;

            /* Premier saut : exigé par l'instruction */

            dest = g_rendering_line_get_destination(last, &type);

            addr = get_rendering_line_address(dest);

            for (k = 0; k < count; k++)
                if (gtk_bin_view_contain_address(views[k], addr))
                    break;

            if (k < count)
                switch (type)
                {
                    case ILT_JUMP:
                        snprintf(buffer, LINKS_DESC_LEN, "_%p -> _%p;\n", views[i], views[k]);
                        desc = stradd(desc, buffer);
                        break;

                    case ILT_JUMP_IF_TRUE:
                        snprintf(buffer, LINKS_DESC_LEN, "_%p -> _%p;\n", views[i], views[k]);
                        desc = stradd(desc, buffer);
                        link_next = true;
                        break;

                    case ILT_JUMP_IF_FALSE:
                        snprintf(buffer, LINKS_DESC_LEN, "_%p -> _%p;\n", views[i], views[k]);
                        desc = stradd(desc, buffer);
                        link_next = true;
                        break;

                    default:
                        break;

                }

            /* Second saut : automatique selon l'instruction */

            if (link_next)
            {
                dest = g_rendering_line_get_next_iter(gtk_bin_view_get_lines(GTK_BIN_VIEW(view)),
                                                      last,
                                                      gtk_bin_view_get_last_line(GTK_BIN_VIEW(view)));

                if (dest == NULL) continue;

                addr = get_rendering_line_address(dest);

                for (k = 0; k < count; k++)
                    if (gtk_bin_view_contain_address(views[k], addr))
                        break;

                if (k < count)
                {
                        snprintf(buffer, LINKS_DESC_LEN, "_%p -> _%p;\n", views[i], views[k]);
                        desc = stradd(desc, buffer);
                }

            }

        }

        /* Si la ligne n'est pas la dernière, on suit le flux normal */
        else if (last != gtk_bin_view_get_last_line(view))
        {
            dest = g_rendering_line_get_next_iter(gtk_bin_view_get_lines(GTK_BIN_VIEW(view)),
                                                  last,
                                                  gtk_bin_view_get_last_line(GTK_BIN_VIEW(view)));

            if (dest == NULL) continue;

            addr = get_rendering_line_address(dest);

            for (k = 0; k < count; k++)
                if (gtk_bin_view_contain_address(views[k], addr))
                    break;

            if (k < count)
            {
                snprintf(buffer, LINKS_DESC_LEN, "_%p -> _%p;\n", views[i], views[k]);
                desc = stradd(desc, buffer);
            }

        }

    }

    return desc;

}