summaryrefslogtreecommitdiff
path: root/src/gui/panels/strings.c
blob: 605a70001877d47709a2f6a0a5c022f05f106f0c (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

/* OpenIDA - Outil d'analyse de fichiers binaires
 * strings.c - panneau d'affichage des chaînes de caractères
 *
 * Copyright (C) 2008-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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "strings.h"


#include <inttypes.h>


#include "panel-int.h"
#include "../../common/extstr.h"
#include "../../gtkext/easygtk.h"



/* -------------------------- PARTIE PRINCIPALE DU PANNEAU -------------------------- */


/* Panneau d'aperçu de graphiques (instance) */
struct _GStringsPanel
{
    GPanelItem parent;                      /* A laisser en premier        */

    GtkTreeView *treeview;                  /* Composant d'affichage       */
    GtkTreeStore *store;                    /* Modèle de gestion           */

    GLoadedBinary *binary;                  /* Binaire à prendre en compte */

    GtkWidget *menubar;                     /* Support pour l'édition      */

};


/* Panneau d'aperçu de graphiques (classe) */
struct _GStringsPanelClass
{
    GPanelItemClass parent;                 /* A laisser en premier        */

};


/* Colonnes de la liste des symboles */
typedef enum _StringsColumn
{
    STC_ADDRESS,                            /* Adresse mémoire du symbole  */
    STC_STRING,                             /* Désignation humaine         */
    STC_RAW,                                /* Données non retouchées      */

    STC_COUNT                               /* Nombre de colonnes          */

} StringsColumn;


/* Réagit à un changement d'affichage principal de contenu. */
static void change_strings_panel_current_binary(GStringsPanel *, GLoadedBinary *);



/* ------------------------- GESTIONS DES MENUS CONTEXTUELS ------------------------- */


/* Affiche le menu d'édition propre aux chaînes trouvées. */
static gboolean on_strings_button_press_event(GtkTreeView *, GdkEventButton *, GStringsPanel *);

/* Réagit avec le menu "--- -> Copier". */
static void mcb_strings_copy(GtkMenuItem *, GtkTreeView *);



/* ---------------------------------------------------------------------------------- */
/*                            PARTIE PRINCIPALE DU PANNEAU                            */
/* ---------------------------------------------------------------------------------- */


/* Indique le type définit pour un panneau d'aperçu de graphiques. */
G_DEFINE_TYPE(GStringsPanel, g_strings_panel, G_TYPE_PANEL_ITEM);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des panneaux d'aperçu de graphiques.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_strings_panel_class_init(GStringsPanelClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : panel = instance à initialiser.                              *
*                                                                             *
*  Description : Initialise une instance de panneau d'aperçu de graphiques.   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_strings_panel_init(GStringsPanel *panel)
{
    GEditorItem *base;                      /* Version basique d'instance  */
    GObject *ref;                           /* Espace de référencement     */
    GtkWidget *scrollwnd;                   /* Support défilant            */
    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 *submenuitem;                 /* Sous-élément de menu        */

    base = G_EDITOR_ITEM(panel);

    base->widget = gtk_vbox_new(FALSE, 0);
    gtk_widget_show(base->widget);

    ref = G_OBJECT(base->widget);
    g_object_set_data(ref, "panel", panel);

    /* Liste des chaînes */

    scrollwnd = gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_show(scrollwnd);
    gtk_box_pack_start(GTK_BOX(base->widget), scrollwnd, TRUE, TRUE, 0);

    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwnd),
                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrollwnd), GTK_SHADOW_IN);

    store = gtk_tree_store_new(STC_COUNT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
    g_object_set_data(G_OBJECT(panel), "store", store);

    treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
    gtk_widget_show(treeview);
    gtk_container_add(GTK_CONTAINER(scrollwnd), treeview);

    g_object_unref(G_OBJECT(store));

    column = gtk_tree_view_column_new();
    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_text_new();
    column = gtk_tree_view_column_new_with_attributes(_("Address"), renderer,
                                                      "text", STC_ADDRESS, 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(_("String"), renderer,
                                                      "markup", STC_STRING, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

    /* Menu contextuel */

    panel->menubar = gtk_menu_new();

    submenuitem = qck_create_menu_item_with_stock_img(NULL, NULL, GTK_STOCK_COPY,
                                                      G_CALLBACK(mcb_strings_copy), treeview);
    gtk_container_add(GTK_CONTAINER(panel->menubar), submenuitem);

    submenuitem = qck_create_menu_item(NULL, NULL, _("Find references..."), NULL, NULL);
    gtk_container_add(GTK_CONTAINER(panel->menubar), submenuitem);

    g_signal_connect(G_OBJECT(treeview), "button_release_event",
                     G_CALLBACK(on_strings_button_press_event), panel);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ref = espace de référencement global.                        *
*                                                                             *
*  Description : Crée un panneau d'affichage des symboles.                    *
*                                                                             *
*  Retour      : Adresse de la structure mise en place.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GEditorItem *g_strings_panel_new(GObject *ref)
{
    GEditorItem *result;                    /* Structure à retourner       */

    result = g_object_new(G_TYPE_STRINGS_PANEL, NULL);

    g_panel_item_init_ext(G_PANEL_ITEM(result), ref, PANEL_STRINGS_ID,
                          _("Strings"), G_EDITOR_ITEM(result)->widget, "SE");

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ref = espace de référencement global.                        *
*                                                                             *
*  Description : Construit et intègre un panneau d'affichage des symboles.    *
*                                                                             *
*  Retour      : Adresse du panneau mis en place.                             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GPanelItem *create_strings_panel(GObject *ref)
{
    GEditorItem *result;                    /* Elément réactif à renvoyer  */

    result = g_strings_panel_new(ref);

    /* Enregistre correctement le tout */
    result->update_binary = (update_item_binary_fc)change_strings_panel_current_binary;
    register_editor_item(result);

    return G_PANEL_ITEM(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : panel  = panneau à mettre à jour.                            *
*                binary = nouvelle instance de binaire analysé.               *
*                                                                             *
*  Description : Réagit à un changement d'affichage principal de contenu.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void change_strings_panel_current_binary(GStringsPanel *panel, GLoadedBinary *binary)
{
    GtkTreeStore *store;                    /* Modèle de gestion           */
    GExeFormat *format;                     /* Format de travail           */
    size_t count;                           /* Nombre des chaînes          */
    GBinSymbol **symbols;                   /* Liste des chaînes trouvées  */
    size_t i;                               /* Boucle de parcours          */
    char address[VMPA_MAX_SIZE];            /* Conversion de l'adresse     */
    const char *raw;                        /* Texte brut trouvé           */
    char *text;                             /* Version imprimable du texte */
    GtkTreeIter iter;                       /* Point d'insertion           */

    store = g_object_get_data(G_OBJECT(panel), "store");

    format = g_loaded_binary_get_format(binary);
    symbols = g_binary_format_get_symbols(G_BIN_FORMAT(format), &count);

    for (i = 0; i < count; i++)
    {
        if (g_binary_symbol_get_target_type(symbols[i]) != STP_STRING) continue;

        /* FIXME : adresses autres que 32 bits */
        snprintf(address, VMPA_MAX_SIZE, "0x%08" PRIx64, g_binary_symbol_get_address(symbols[i]));

        raw = g_binary_symbol_to_string(symbols[i]);

        text = strdup(raw);
        text = strrpl(text, "&", "&amp;");
        text = strrpl(text, "<", "&lt;");
        text = strrpl(text, ">", "&gt;");
        text = strrpl(text, "\r", "<b>\\r</b>");
        text = strrpl(text, "\n", "<b>\\n</b>");

        gtk_tree_store_append(store, &iter, NULL);
        gtk_tree_store_set(store, &iter,
                           STC_ADDRESS, address,
                           STC_STRING, text,
                           STC_RAW, raw,
                           -1);

        free(text);

    }

}



/* ---------------------------------------------------------------------------------- */
/*                           GESTIONS DES MENUS CONTEXTUELS                           */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : treeview = composant GTK visé par l'opération.               *
*                event    = informations liées à l'événement.                 *
*                data     = référence vers le panneau contenant le menu.      *
*                                                                             *
*  Description : Affiche le menu d'édition propre aux chaînes trouvées.       *
*                                                                             *
*  Retour      : FALSE pour poursuivre la propagation de l'événement.         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static gboolean on_strings_button_press_event(GtkTreeView *treeview, GdkEventButton *event, GStringsPanel *panel)
{
    gboolean result;                        /* Bilan d'action à renvoyer   */

    result = FALSE;

    if (event->type == GDK_BUTTON_RELEASE && event->button == 3)
    {
        gtk_menu_popup(GTK_MENU(panel->menubar),
                       NULL, NULL, NULL, NULL,
                       event->button, event->time);

        result = TRUE;

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : menuitem = élément de menu sélectionné.                      *
*                treeview = arbre contenant la sélection à exporter.          *
*                                                                             *
*  Description : Réagit avec le menu "--- -> Copier".                         *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void mcb_strings_copy(GtkMenuItem *menuitem, GtkTreeView *treeview)
{
    GtkTreeSelection *selection;            /* Sélection de l'arbre        */
    GtkTreeIter iter;                       /* Point de sélection          */
    GtkTreeModel *model;                    /* Modèle de gestion           */
    gchar *string;                          /* Chaîne sélectionnée         */
    GtkClipboard *clipboard;                /* Presse-papiers d'arrivée    */

    selection = gtk_tree_view_get_selection(treeview);

    if (gtk_tree_selection_get_selected(selection, &model, &iter))
    {
        gtk_tree_model_get(model, &iter, STC_RAW, &string, -1);

        if (string != NULL)
        {
            clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(),
                                                      GDK_SELECTION_PRIMARY);

            gtk_clipboard_set_text(clipboard, string, strlen(string));
            g_free(string);

        }

    }

}