-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgtk-decoder.c
527 lines (458 loc) · 19.3 KB
/
gtk-decoder.c
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define MAXFILELENGTH (0x10000 * 1024) // 1Mb
//#define MAXADDRLINES 16 /* 64k */
#define MAXADDRLINES 20 /* 1Mb */
#define MAXDATALINES 8
#define WINDOWWIDTH 1300
#define WINDOWHEIGHT 750
#define ADDRLABELYPOS 600
#define ADDRCOMBOYPOS 620
#define DATALABELYPOS 660
#define DATACOMBOYPOS 680
#define BOTTOMHEIGHT ((WINDOWHEIGHT)-(ADDRLABELYPOS))
#define TEXTBOXHEIGHT ((WINDOWHEIGHT)-(BOTTOMHEIGHT))
#define FONTHEIGHT 12
#define FONTLINEHEIGHT 16
GtkWidget *combobox_addrline[MAXADDRLINES];
GtkWidget *combobox_dataline[MAXADDRLINES];
gulong combobox_dataline_signal_id[MAXDATALINES];
gulong combobox_addrline_signal_id[MAXADDRLINES];
gulong textscale_signal_id;
GtkWidget *scrolled;
GtkWidget *textbox;
GtkWidget *textscale;
unsigned long file_offset = 0;
unsigned char rawbuffer[MAXFILELENGTH];
unsigned char decodedbuffer[MAXFILELENGTH];
unsigned char hexdumpbuffer[MAXFILELENGTH*5];
#define PREVIEW_CSS \
"label {\n" \
" font-family: \"Monospace\";\n" \
" font-size: 12px;\n" \
" background-color: black;\n" \
" color: limegreen;\n" \
"}\n"
/** \brief Create a new CSS provider and set it to \a css
*
* Use this function if you need a CSS provider you'll apply multiple times,
* if you need to apply CSS to a widget only once, there's a wrapper function
* #gtk3_css_add().
*
* \param[in] css CSS string
*
* \return new provider or NULL on error
*/
GtkCssProvider *gtk3_css_provider_new(const char *css)
{
GtkCssProvider *provider;
GError *err = NULL;
/* instanciate CSS provider */
provider = gtk_css_provider_new();
/* attempt to load CSS from string */
gtk_css_provider_load_from_data(provider, css, -1, &err);
if (err != NULL) {
fprintf(stderr, "CSS error: %s\n", err->message);
g_error_free(err);
return NULL;
}
return provider;
}
/** \brief Add CSS \a provider to \a widget
*
* \param[in,out] widget widget to add \a provider to
* \param[in] provider CSS provider
*
* \return bool
*/
gboolean gtk3_css_provider_add(GtkWidget *widget,
GtkCssProvider *provider)
{
GtkStyleContext *context;
/* try to get style context */
context = gtk_widget_get_style_context(widget);
if (context == NULL) {
fprintf(stderr, "CSS error\n");
/* don't free the context, it's owned by the widget */
return FALSE;
}
/* add provider */
gtk_style_context_add_provider(context,
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
return TRUE;
}
/** \brief Remove \a provider from \a widget
*
* \param[in,out] widget gtk widget
* \param[in] provider gtk css provider to remove
*
* \return gboolean
*/
gboolean gtk3_css_provider_remove(GtkWidget *widget,
GtkCssProvider *provider)
{
GtkStyleContext *context;
/* try to get style context */
context = gtk_widget_get_style_context(widget);
if (context == NULL) {
fprintf(stderr, "Couldn't get style context of widget\n");
/* don't free the context, it's owned by the widget */
return FALSE;
}
gtk_style_context_remove_provider(context, GTK_STYLE_PROVIDER(provider));
return TRUE;
}
/** \brief Add CSS string \a css to \a widget
*
* Only use this when adding CSS to a single widget, if you need to add the
* same CSS to multiple widgets use #gtk3_css_provider_new() to create
* a CSS provider once and #gtk3_css_provider_add() to add it multiple
* times.
*
* \param[in,out] widget widget to add CSS to
* \param[in] css CSS string
*
* \return bool
*/
gboolean gtk3_css_add(GtkWidget *widget, const char *css)
{
GtkCssProvider *provider;
provider = gtk3_css_provider_new(css);
if (provider != NULL) {
return gtk3_css_provider_add(widget, provider);
}
return FALSE;
}
char *infilename = NULL, *outfilename = NULL;
int verbose = 0;
#include "decodefunc.c"
#include "filefunc.c"
static void destroy(GtkWidget *widget, gpointer data)
{
printf("saving '%s'\n", outfilename);
saveoutputfile(decodedbuffer, outfilename);
gtk_main_quit();
}
static const char *scrtab =
"@abcdefghijklmno" // 00
"pqrstuvwxyz[.]^_" // 10
" !\"#$%&'()*+,-./" // 20
"0123456789:;<=>?" // 30
".ABCDEFGHIJKLMNO" // 40
"PQRSTUVWXYZ....." // 50
"................"
"................"
"................"
"................"
"................"
"................"
"................"
"................"
"................"
"................";
static const char *pettab =
"................" // 00
"................" // 10
" !\"#$%&'()*+,-./" // 20
"0123456789:;<=>?" // 30
"@abcdefghijklmno" // 40
"pqrstuvwxyz[.]^_" // 50
".ABCDEFGHIJKLMNO" // 60
"PQRSTUVWXYZ....." // 70
"................"
"................"
"................"
"................"
".ABCDEFGHIJKLMNO" // C0
"PQRSTUVWXYZ....." // D0
"................"
"................";
static void update_hexdump(void)
{
int i, n = 0;
char *txt = hexdumpbuffer;
if (verbose > 2) { printf("update_hexdump start\n"); }
for (n = 0; n < (0x20*(TEXTBOXHEIGHT / FONTLINEHEIGHT)); n += 0x20) {
i = n + file_offset;
if (i >= filelength) {
txt += sprintf(txt, "%08x:\n", i);
} else {
txt += sprintf(txt, "%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
"%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
" %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
" %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n"
,i,
decodedbuffer[i], decodedbuffer[i+1], decodedbuffer[i+2], decodedbuffer[i+3],
decodedbuffer[i+4], decodedbuffer[i+5], decodedbuffer[i+6], decodedbuffer[i+7],
decodedbuffer[i+8], decodedbuffer[i+9], decodedbuffer[i+10], decodedbuffer[i+11],
decodedbuffer[i+12], decodedbuffer[i+13], decodedbuffer[i+14], decodedbuffer[i+15],
decodedbuffer[i+16], decodedbuffer[i+17], decodedbuffer[i+18], decodedbuffer[i+19],
decodedbuffer[i+20], decodedbuffer[i+21], decodedbuffer[i+22], decodedbuffer[i+23],
decodedbuffer[i+24], decodedbuffer[i+25], decodedbuffer[i+26], decodedbuffer[i+27],
decodedbuffer[i+28], decodedbuffer[i+29], decodedbuffer[i+30], decodedbuffer[i+31],
pettab[decodedbuffer[i]], pettab[decodedbuffer[i+1]], pettab[decodedbuffer[i+2]], pettab[decodedbuffer[i+3]],
pettab[decodedbuffer[i+4]], pettab[decodedbuffer[i+5]], pettab[decodedbuffer[i+6]], pettab[decodedbuffer[i+7]],
pettab[decodedbuffer[i+8]], pettab[decodedbuffer[i+9]], pettab[decodedbuffer[i+10]], pettab[decodedbuffer[i+11]],
pettab[decodedbuffer[i+12]], pettab[decodedbuffer[i+13]], pettab[decodedbuffer[i+14]], pettab[decodedbuffer[i+15]],
pettab[decodedbuffer[i+16]], pettab[decodedbuffer[i+17]], pettab[decodedbuffer[i+18]], pettab[decodedbuffer[i+19]],
pettab[decodedbuffer[i+20]], pettab[decodedbuffer[i+21]], pettab[decodedbuffer[i+22]], pettab[decodedbuffer[i+23]],
pettab[decodedbuffer[i+24]], pettab[decodedbuffer[i+25]], pettab[decodedbuffer[i+26]], pettab[decodedbuffer[i+27]],
pettab[decodedbuffer[i+28]], pettab[decodedbuffer[i+29]], pettab[decodedbuffer[i+30]], pettab[decodedbuffer[i+31]],
scrtab[decodedbuffer[i]], scrtab[decodedbuffer[i+1]], scrtab[decodedbuffer[i+2]], scrtab[decodedbuffer[i+3]],
scrtab[decodedbuffer[i+4]], scrtab[decodedbuffer[i+5]], scrtab[decodedbuffer[i+6]], scrtab[decodedbuffer[i+7]],
scrtab[decodedbuffer[i+8]], scrtab[decodedbuffer[i+9]], scrtab[decodedbuffer[i+10]], scrtab[decodedbuffer[i+11]],
scrtab[decodedbuffer[i+12]], scrtab[decodedbuffer[i+13]], scrtab[decodedbuffer[i+14]], scrtab[decodedbuffer[i+15]],
scrtab[decodedbuffer[i+16]], scrtab[decodedbuffer[i+17]], scrtab[decodedbuffer[i+18]], scrtab[decodedbuffer[i+19]],
scrtab[decodedbuffer[i+20]], scrtab[decodedbuffer[i+21]], scrtab[decodedbuffer[i+22]], scrtab[decodedbuffer[i+23]],
scrtab[decodedbuffer[i+24]], scrtab[decodedbuffer[i+25]], scrtab[decodedbuffer[i+26]], scrtab[decodedbuffer[i+27]],
scrtab[decodedbuffer[i+28]], scrtab[decodedbuffer[i+29]], scrtab[decodedbuffer[i+30]], scrtab[decodedbuffer[i+31]]
);
}
}
if (verbose > 2) { printf("update_hexdump set label\n"); }
// gtk_text_buffer_set_text (textbuffer, hexdumpbuffer, -1);
gtk_label_set_text(GTK_LABEL(textbox), hexdumpbuffer);
if (verbose > 2) { printf("gtk_widget_show_all\n"); }
gtk_widget_show_all(GTK_WIDGET(textbox));
if (verbose > 2) { printf("update_hexdump stop\n"); }
}
static void update_addr_comboboxes(void)
{
int i;
for (i = 0; i < MAXADDRLINES; i++) {
unsigned char combotext[5];
// we need to block the signal handler so we dont trigger ours
g_signal_handler_block (combobox_addrline[i], combobox_addrline_signal_id[i]);
sprintf(combotext, "%d", addrshift[i]);
gtk_combo_box_set_active_id (GTK_COMBO_BOX(combobox_addrline[i]), combotext);
// unblock signal handler
g_signal_handler_unblock (combobox_addrline[i], combobox_addrline_signal_id[i]);
}
}
static void update_data_comboboxes(void)
{
int i;
for (i = 0; i < MAXDATALINES; i++) {
unsigned char combotext[5];
// we need to block the signal handler so we dont trigger ours
g_signal_handler_block (combobox_dataline[i], combobox_dataline_signal_id[i]);
sprintf(combotext, "%d", datashift[i]);
gtk_combo_box_set_active_id (GTK_COMBO_BOX(combobox_dataline[i]), combotext);
// unblock signal handler
g_signal_handler_unblock (combobox_dataline[i], combobox_dataline_signal_id[i]);
}
}
static int lastaddrline = -1;
static unsigned int lastaddrshift[MAXADDRLINES];
static void addrline_changed(GtkWidget *widget, gpointer data)
{
ptrdiff_t line = (ptrdiff_t)data;
const gchar *valstr = gtk_combo_box_get_active_id (GTK_COMBO_BOX(widget));
unsigned long val, oldval;
val = strtoul(valstr, NULL, 0);
if (lastaddrline != line) {
printf("new addrline %ld\n", line);
memcpy(&lastaddrshift[0], &addrshift[0], MAXADDRLINES * sizeof(int));
lastaddrline = line;
} else {
printf("same addrline %ld\n", line);
memcpy(&addrshift[0], &lastaddrshift[0], MAXADDRLINES * sizeof(int));
}
updatereverselookup();
dumpaddrlines();
oldval = addrshift[line];
addrshift[line] = val;
printf("addr line %ld to %ld, line %d to %ld\n", line, val, addrshiftreverse[val], oldval);
addrshift[addrshiftreverse[val]] = oldval;
updatereverselookup();
dumpaddrlines();
update_addr_comboboxes();
decode(decodedbuffer, rawbuffer, filelength);
update_hexdump();
}
static int lastdataline = -1;
static unsigned int lastdatashift[MAXDATALINES];
static void dataline_changed(GtkWidget *widget, gpointer data)
{
ptrdiff_t line = (ptrdiff_t)data;
const gchar *valstr = gtk_combo_box_get_active_id (GTK_COMBO_BOX(widget));
unsigned long val, oldval;
val = strtoul(valstr, NULL, 0);
if (lastdataline != line) {
printf("new dataline %ld\n", line);
memcpy(&lastdatashift[0], &datashift[0], MAXDATALINES * sizeof(int));
lastdataline = line;
} else {
printf("same dataline %ld\n", line);
memcpy(&datashift[0], &lastdatashift[0], MAXDATALINES * sizeof(int));
}
updatereverselookup();
dumpdatalines();
oldval = datashift[line];
datashift[line] = val;
printf("data line %ld to %ld, line %d to %ld\n", line, val, datashiftreverse[val], oldval);
datashift[datashiftreverse[val]] = oldval;
updatereverselookup();
dumpdatalines();
update_data_comboboxes();
decode(decodedbuffer, rawbuffer, filelength);
update_hexdump();
}
static void textscale_changed(GtkWidget *widget, gpointer data)
{
int line = (int)gtk_range_get_value(GTK_RANGE(widget));
int addr = line * 0x20;
if (verbose > 2) { printf("textscale_changed: %08x\n", addr); }
file_offset = addr;
update_hexdump();
}
static void usage(void)
{
printf("unidec - swap address- and databits in a binary dump\n"
"usage: unidec <options> -i <input file> -o <output file>\n"
"-a <old> <new> swap address line (port->eprom)\n"
"-d <old> <new> swap data line (port->eprom)\n"
"-i <file> name input file\n"
"-o <file> name output file\n"
"--verbose increase verbose level\n");
exit(-1);
}
int main(int argc, char *argv[])
{
int i, oldline, newline;
decodeinit();
// process commandline
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-a")) {
newline = strtoul(argv[i+1], NULL, 0);
oldline = strtoul(argv[i+2], NULL, 0);
if (verbose > 1) { printf("addr port %2d -> eprom %2d\n", newline, oldline); }
addrshift[oldline] = newline;
i += 2;
} else if (!strcmp(argv[i], "-d")) {
newline = strtoul(argv[i+1], NULL, 0);
oldline = strtoul(argv[i+2], NULL, 0);
if (verbose > 1) { printf("data port %2d -> eprom %2d\n", newline, oldline); }
datashift[oldline] = newline;
i += 2;
} else if (!strcmp(argv[i], "-i")) {
infilename = argv[i+1];
i += 1;
} else if (!strcmp(argv[i], "-o")) {
outfilename = argv[i+1];
i += 1;
} else if (!strcmp(argv[i], "--verbose")) {
verbose++;
} else {
usage();
}
}
if (infilename == NULL) {
fprintf(stderr, "error: input filename missing.\n");
exit(-1);
}
if (outfilename == NULL) {
fprintf(stderr, "error: output filename missing.\n");
exit(-1);
}
loadinputfile(rawbuffer, infilename);
updatereverselookup();
decode(decodedbuffer, rawbuffer, filelength);
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GTK unidec");
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);
GtkWidget *k;
k= gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), k);
if (verbose > 2) { printf("create the textbox\n"); }
// create the textbox
{
textbox = gtk_label_new(NULL);
gtk_label_set_single_line_mode(GTK_LABEL(textbox), FALSE);
gtk3_css_add(textbox, PREVIEW_CSS);
gtk_widget_set_size_request(textbox, WINDOWWIDTH - 10, TEXTBOXHEIGHT);
gtk_fixed_put (GTK_FIXED (k), textbox, 0, 0);
// TODO: when we reload the file, we must update the range of the scale
textscale = gtk_scale_new_with_range (
GTK_ORIENTATION_VERTICAL,
0,
(filelength / 0x20) - (TEXTBOXHEIGHT / FONTLINEHEIGHT),
1);
gtk_scale_set_draw_value(GTK_SCALE(textscale), FALSE);
gtk_widget_set_size_request(textscale, 10, TEXTBOXHEIGHT);
gtk_fixed_put (GTK_FIXED (k), textscale, WINDOWWIDTH - 10, 0);
update_hexdump();
textscale_signal_id = g_signal_connect(textscale, "value-changed", G_CALLBACK(textscale_changed), NULL);
}
if (verbose > 2) { printf("create combo boxes for the address lines\n"); }
// create combo boxes for the address lines
{
int i = 0, n = 0;
GtkWidget *label;
label = gtk_label_new("Bus:");
gtk_fixed_put (GTK_FIXED (k), label, 10, ADDRLABELYPOS);
label = gtk_label_new("Chip:");
gtk_fixed_put (GTK_FIXED (k), label, 10, ADDRCOMBOYPOS);
for (i = 0; i < MAXADDRLINES; i++) {
unsigned char combotext[5];
sprintf(combotext, "A%d", i);
label = gtk_label_new(combotext);
gtk_fixed_put (GTK_FIXED (k), label, 15 + (MAXADDRLINES - i) * 60, ADDRLABELYPOS);
combobox_addrline[i] = gtk_combo_box_text_new();
gtk_fixed_put (GTK_FIXED (k), combobox_addrline[i], (MAXADDRLINES - i) * 60, ADDRCOMBOYPOS);
gtk_widget_set_size_request(combobox_addrline[i], 10, 10);
for (n = 0; n < MAXADDRLINES; n++) {
sprintf(combotext, "%d", n);
if ((addrlines <= i) || (n < addrlines)) {
gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT(combobox_addrline[i]), combotext, combotext);
}
}
sprintf(combotext, "%d", addrshift[i]);
gtk_combo_box_set_active_id (GTK_COMBO_BOX(combobox_addrline[i]), combotext);
combobox_addrline_signal_id[i] = g_signal_connect(combobox_addrline[i], "changed", G_CALLBACK(addrline_changed), (gpointer)(ptrdiff_t)i);
if (addrlines <= i) {
gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(combobox_addrline[i]), GTK_SENSITIVITY_OFF);
}
}
}
if (verbose > 2) { printf("create combo boxes for the data lines\n"); }
// create combo boxes for the data lines
{
int i = 0, n = 0;
GtkWidget *label;
label = gtk_label_new("Bus:");
gtk_fixed_put (GTK_FIXED (k), label, 10, DATALABELYPOS);
label = gtk_label_new("Chip:");
gtk_fixed_put (GTK_FIXED (k), label, 10, DATACOMBOYPOS);
for (i = 0; i < MAXDATALINES; i++) {
unsigned char combotext[5];
sprintf(combotext, "D%d", i);
label = gtk_label_new(combotext);
gtk_fixed_put (GTK_FIXED (k), label, 15 + (MAXDATALINES - i) * 60, DATALABELYPOS);
combobox_dataline[i] = gtk_combo_box_text_new();
gtk_fixed_put (GTK_FIXED (k), combobox_dataline[i], (MAXDATALINES - i) * 60, DATACOMBOYPOS);
gtk_widget_set_size_request(combobox_dataline[i], 10, 10);
for (n = 0; n < MAXDATALINES; n++) {
sprintf(combotext, "%d", n);
gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT(combobox_dataline[i]), combotext, combotext);
}
sprintf(combotext, "%d", datashift[i]);
gtk_combo_box_set_active_id (GTK_COMBO_BOX(combobox_dataline[i]), combotext);
combobox_dataline_signal_id[i] = g_signal_connect(combobox_dataline[i], "changed", G_CALLBACK(dataline_changed), (gpointer)(ptrdiff_t)i);
}
}
if (verbose > 2) { printf("gtk_widget_set_size_request\n"); }
gtk_widget_set_size_request(GTK_WIDGET(window),WINDOWWIDTH, WINDOWHEIGHT);
if (verbose > 2) { printf("gtk_widget_show_all\n"); }
gtk_widget_show_all(GTK_WIDGET(window));
if (verbose > 2) { printf("gtk_main\n"); }
gtk_main();
return 0;
}