summaryrefslogtreecommitdiff
path: root/src/dialogs/export.c
blob: a7af1410562f545dbce7524aa31fb325667e4562 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

/* OpenIDA - Outil d'analyse de fichiers binaires
 * export.c - assistant d'exportation de contenu binaire
 *
 * Copyright (C) 2010-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 "export.h"


#include <fcntl.h>
#include <malloc.h>
#include <stdio.h>
#include <unistd.h>


#include <i18n.h>


#include "../gtkext/easygtk.h"



/* ------------------------ PARTIE PRINCIPALE DE L'ASSISTANT ------------------------ */


/* Conservation des informations utiles */
struct _export_data
{
    int fd;                                 /* Flux ouvert en écriture     */
    BufferExportType type;                  /* Type d'exportation          */

    bool addr;                              /* Affichage des adresses      */
    bool code;                              /* Affichage du code binaire   */
    bool content;                           /* Affichage du contenu humain */

};


/* Ferme l'assistant sans dérouler la procédure. */
static void export_assistant_cancel(GtkAssistant *, gpointer);

/* Ferme l'assistant et déroule la procédure. */
static void export_assistant_close(GtkAssistant *, GObject *);

/* Assure l'exportation en différé. */
static bool export_buffer_line(GCodeBuffer *, GBufferLine *, struct _export_data *);



/* -------------------- DEFINITION DES REGLAGES DE L'EXPORTATION -------------------- */


/* Ajoute le panneau de choix du type de sortie. */
static void register_output_panel(GtkAssistant *);

/* Réagit un changement du nom de fichier pour l'exportation. */
static void on_export_filename_changed(GtkEntry *, GtkAssistant *);

/* Sélectionne ou non un nouveau fichier de sortie. */
static void on_filename_browsing_clicked(GtkButton *, GObject *);



/* ------------------------- SELECTION DU CONTENU A TRAITER ------------------------- */


/* Ajoute le panneau de sélection du contenu à exporter. */
static void register_content_panel(GtkAssistant *);


/* ---------------------------------------------------------------------------------- */
/*                          PARTIE PRINCIPALE DE L'ASSISTANT                          */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = binaire chargé en mémoire à traiter.                *
*                parent = fenêtre principale de l'éditeur.                    *
*                                                                             *
*  Description : Crée et affiche un assistant d'aide à l'exportation.         *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void run_export_assistant(GLoadedBinary *binary, GtkWindow *parent)
{
    GtkWidget *assistant;                   /* Fenêtre à afficher          */
    GObject *ref;                           /* Espace de référencement     */

    assistant = gtk_assistant_new();
    gtk_widget_set_size_request(assistant, 450, 300);
    gtk_window_set_position(GTK_WINDOW(assistant), GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(assistant), _("Export assistant"));

    gtk_window_set_modal(GTK_WINDOW(assistant), TRUE);
    gtk_window_set_transient_for(GTK_WINDOW(assistant), parent);

    ref = G_OBJECT(assistant);
    g_object_set_data(ref, "binary", binary);

    register_output_panel(GTK_ASSISTANT(assistant));
    register_content_panel(GTK_ASSISTANT(assistant));

    g_signal_connect(G_OBJECT(assistant), "cancel", G_CALLBACK(export_assistant_cancel), NULL);
    g_signal_connect(G_OBJECT(assistant), "close", G_CALLBACK(export_assistant_close), ref);

    gtk_widget_show_all(assistant);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : assistant = fenêtre à compléter et référencement global.     *
*                data      = adresse non utilisée ici.                        *
*                                                                             *
*  Description : Ferme l'assistant sans dérouler la procédure.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void export_assistant_cancel(GtkAssistant *assistant, gpointer data)
{
    gtk_widget_destroy(GTK_WIDGET(assistant));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : assistant = fenêtre à compléter et référencement global.     *
*                ref       = adresse de l'espace de référencement global.     *
*                                                                             *
*  Description : Ferme l'assistant et déroule la procédure.                   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void export_assistant_close(GtkAssistant *assistant, GObject *ref)
{
    GtkEntry *entry;                        /* Zone de saisie              */
    const gchar *filename;                  /* Chemin d'accès du fichier   */
    int fd;                                 /* Descripteur de la sortie    */
    struct _export_data *export;            /* Informations à faire suivre */
    GtkToggleButton *checkbutton;           /* Coche à retrouver           */
    GLoadedBinary *binary;                  /* Binaire chargé à parcourir  */
    GCodeBuffer *buffer;                    /* Tampon de code à traiter    */

    /* Fichier de sortie */

    entry = GTK_ENTRY(g_object_get_data(ref, "filename"));
    filename = gtk_entry_get_text(entry);

    fd = open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
    if (fd == -1)
    {
        perror("open");
        return;
    }

    export = (struct _export_data *)calloc(1, sizeof(struct _export_data));

    export->fd = fd;

    /* Type d'exportation */

    export->type = BET_TEXT;

    /* Eléments à afficher */

    checkbutton = GTK_TOGGLE_BUTTON(g_object_get_data(ref, "virtual_addr"));
    export->addr = gtk_toggle_button_get_active(checkbutton);

    checkbutton = GTK_TOGGLE_BUTTON(g_object_get_data(ref, "binary_code"));
    export->code = gtk_toggle_button_get_active(checkbutton);

    checkbutton = GTK_TOGGLE_BUTTON(g_object_get_data(ref, "assembly_code"));
    export->content = gtk_toggle_button_get_active(checkbutton);

    /* Programmation de la tâche */

    binary = G_LOADED_BINARY(g_object_get_data(ref, "binary"));

    buffer = g_loaded_binary_get_disassembled_buffer(binary);

    g_buffer_code_scan(buffer, 0, VMPA_MAX, _("Exporting binary lines..."),
                       (process_line_fc)export_buffer_line, export);

    gtk_widget_destroy(GTK_WIDGET(assistant));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : buffer = tampon de code contenant toutes les lignes.         *
*                line   = ligne dont le contenu est à exporter.               *
*                export = informations utiles à la manoeuvre.                 *
*                                                                             *
*  Description : Assure l'exportation en différé.                             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool export_buffer_line(GCodeBuffer *buffer, GBufferLine *line, struct _export_data *export)
{
    /* Si les traitements sont terminés... */
    if (line == NULL)
    {
        close(export->fd);
        free(export);
    }

    else
        g_buffer_line_export(line, export->fd, export->type,
                             export->addr, export->code, export->content);

    return true;

}



/* ---------------------------------------------------------------------------------- */
/*                      DEFINITION DES REGLAGES DE L'EXPORTATION                      */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : assistant = fenêtre à compléter et référencement global.     *
*                                                                             *
*  Description : Ajoute le panneau de choix du type de sortie.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void register_output_panel(GtkAssistant *assistant)
{
    GtkWidget *alignment;                   /* Disposition sur le support  */
    GtkWidget *vbox;                        /* Support principal #1        */
    GtkWidget *hbox;                        /* Support principal #2        */
    GtkWidget *label;                       /* Etiquette d'indication      */
    GtkWidget *combobox;                    /* Sélection du format         */
    GtkWidget *entry;                       /* Zone de saisie de texte     */
    GtkWidget *button;                      /* Sélection de fichier        */
    GLoadedBinary *binary;                  /* Binaire chargé à parcourir  */
    const char *filename;                   /* Chemin d'accès par défaut   */

    alignment = qck_create_padded_alignment(8, 8, 8, 8);

    vbox = gtk_vbox_new(TRUE, 0);
    gtk_widget_show(vbox);
    gtk_container_add(GTK_CONTAINER(alignment), vbox);

    /* Format de sortie */

    hbox = gtk_hbox_new(FALSE, 0);
    gtk_widget_show(hbox);
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

    label = qck_create_label(NULL, NULL, _("Format: "));
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);

    combobox = qck_create_combobox(NULL, NULL, G_CALLBACK(NULL), NULL);
    gtk_box_pack_start(GTK_BOX(hbox), combobox, TRUE, TRUE, 0);

    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combobox), _("Simple text"));

    gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);

    /* Fichier de sortie */

    hbox = gtk_hbox_new(FALSE, 0);
    gtk_widget_show(hbox);
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

    label = qck_create_label(NULL, NULL, _("File: "));
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);

    entry = qck_create_entry(G_OBJECT(assistant), "filename", NULL);
    gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);

    button = qck_create_button(NULL, NULL, "...", G_CALLBACK(on_filename_browsing_clicked), assistant);
    gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);

    /* Intégration */

    gtk_assistant_append_page(assistant, alignment);
    gtk_assistant_set_page_title(assistant, alignment, _("Output"));
    gtk_assistant_set_page_type(assistant, alignment, GTK_ASSISTANT_PAGE_INTRO);

    gtk_assistant_set_page_complete(assistant, alignment, TRUE);

    /* Choix par défaut */

    binary = G_LOADED_BINARY(g_object_get_data(G_OBJECT(assistant), "binary"));
    filename = g_loaded_binary_get_filename(binary, true);

    gtk_entry_set_text(GTK_ENTRY(entry), filename);
    gtk_editable_insert_text(GTK_EDITABLE(entry), ".txt", -1, (gint []) { strlen(filename) });

    g_signal_connect(G_OBJECT(entry), "changed", G_CALLBACK(on_export_filename_changed), assistant);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : entry     = zone de texte dont le contenu vient de changer.  *
*                assistant = fenêtre affichée et référencement global.        *
*                                                                             *
*  Description : Réagit un changement du nom de fichier pour l'exportation.   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void on_export_filename_changed(GtkEntry *entry, GtkAssistant *assistant)
{
    const gchar *text;                      /* Texte saisi dans la zone    */
    gint num;                               /* Etape courante              */
    GtkWidget *page;                        /* Support de cette étape      */

    text = gtk_entry_get_text(entry);

    num = gtk_assistant_get_current_page(assistant);
    page = gtk_assistant_get_nth_page(assistant, num);

    gtk_assistant_set_page_complete(assistant, page, (strlen(text) > 0));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : button = bouton d'édition de la sélection.                   *
*                ref    = espace de référencement principal.                  *
*                                                                             *
*  Description : Sélectionne ou non un nouveau fichier de sortie.             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void on_filename_browsing_clicked(GtkButton *button, GObject *ref)
{
    GtkWidget *dialog;                      /* Boîte à afficher            */
    gchar *filename;                        /* Nom du fichier à intégrer   */
    GtkEntry *entry;                        /* Zone de saisie à maj.       */

    dialog = gtk_file_chooser_dialog_new(_("Choose an output filename"), GTK_WINDOW(ref),
                                         GTK_FILE_CHOOSER_ACTION_SAVE,
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                         NULL);

    entry = GTK_ENTRY(g_object_get_data(ref, "filename"));
    gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), gtk_entry_get_text(entry));

    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
    {
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));

        gtk_entry_set_text(GTK_ENTRY(entry), filename);

        g_free(filename);

    }

    gtk_widget_destroy(dialog);

}



/* ---------------------------------------------------------------------------------- */
/*                           SELECTION DU CONTENU A TRAITER                           */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : assistant = fenêtre à compléter et référencement global.     *
*                                                                             *
*  Description : Ajoute le panneau de sélection du contenu à exporter.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void register_content_panel(GtkAssistant *assistant)
{
    GtkWidget *alignment;                   /* Disposition sur le support  */
    GtkWidget *vbox;                        /* Support principal           */
    GtkWidget *frame;                       /* Support avec encadrement    */
    GtkWidget *subalign;                    /* Disposition des options     */
    GtkWidget *sub_vbox;                    /* Division verticale          */
    GtkWidget *checkbutton;                 /* Coche pour une option       */

    alignment = qck_create_padded_alignment(8, 8, 8, 8);

    vbox = gtk_vbox_new(TRUE, 0);
    gtk_widget_show(vbox);
    gtk_container_add(GTK_CONTAINER(alignment), vbox);

    /* Eléments à afficher */

    frame = qck_create_frame(_("<b>Items to display</b>"), &subalign, 0, 0, 12, 0);
    gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);

    sub_vbox = gtk_vbox_new(FALSE, 0);
    gtk_widget_show(sub_vbox);
    gtk_container_add(GTK_CONTAINER(subalign), sub_vbox);

    checkbutton = qck_create_check_button(G_OBJECT(assistant), "virtual_addr", _("Virtual address"), NULL, NULL);
    gtk_box_pack_start(GTK_BOX(sub_vbox), checkbutton, FALSE, FALSE, 0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), TRUE);

    checkbutton = qck_create_check_button(G_OBJECT(assistant), "binary_code", _("Binary code"), NULL, NULL);
    gtk_box_pack_start(GTK_BOX(sub_vbox), checkbutton, FALSE, FALSE, 0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), TRUE);

    checkbutton = qck_create_check_button(G_OBJECT(assistant), "assembly_code", _("Assembly code"), NULL, NULL);
    gtk_box_pack_start(GTK_BOX(sub_vbox), checkbutton, FALSE, FALSE, 0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), TRUE);

    /* Intégration */

    gtk_assistant_append_page(assistant, alignment);
    gtk_assistant_set_page_title(assistant, alignment, _("Exported content"));
    gtk_assistant_set_page_type(assistant, alignment, GTK_ASSISTANT_PAGE_CONFIRM);

    gtk_assistant_set_page_complete(assistant, alignment, TRUE);

}