File: blockop.cc

package info (click to toggle)
ht 0.5.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,388 kB
  • ctags: 9,064
  • sloc: cpp: 51,336; ansic: 11,954; sh: 2,742; yacc: 1,142; lex: 396; makefile: 178
file content (618 lines) | stat: -rw-r--r-- 14,194 bytes parent folder | download
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
 *	HT Editor
 *	blockop.cc
 *
 *	Copyright (C) 1999, 2000, 2001 Stefan Weyergraf (stefan@weyergraf.de)
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 *
 *	This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <string.h>

#include "blockop.h"
#include "htatom.h"
#include "htctrl.h"
#include "htendian.h"
#include "htexcept.h"
#include "hthist.h"
#include "htiobox.h"
#include "htkeyb.h"
#include "htstring.h"
#include "process.h"

extern "C" {
#include "evalx.h"
}

/*
 *	CLASS ht_blockop_dialog
 */

void ht_blockop_dialog::init(bounds *b, FILEOFS pstart, FILEOFS pend, ht_list *history=0)
{
	ht_dialog::init(b, "operate on block", FS_TITLE | FS_KILLER | FS_MOVE);
	bounds c;

	bool prerange=(pend>pstart);

	ht_statictext *text;

	ht_label *s;
	
	ht_list *addrhist=(ht_list*)find_atom(HISTATOM_GOTO);
/* start */
	c=*b;
	c.h=1;
	c.w=13;
	c.x=7;
	c.y=1;
	start=new ht_strinputfield();
	start->init(&c, 64, addrhist);
	insert(start);
	if (prerange) {
		char t[16];
		sprintf(t, "0x%x", pstart);
		ht_inputfield_data d;
		d.textlen=strlen(t);
		d.text=(byte*)t;
		start->databuf_set(&d);
	}

/* start_desc */
	c.x=1;
	c.w=6;
	s=new ht_label();
	s->init(&c, "~start", start);
	insert(s);
	
/* end */
	c=*b;
	c.h=1;
	c.w=13;
	c.x=27;
	c.y=1;
	end=new ht_strinputfield();
	end->init(&c, 64, addrhist);
	insert(end);
	if (prerange) {
		char t[16];
		sprintf(t, "0x%x", pend);
		ht_inputfield_data d;
		d.textlen=strlen(t);
		d.text=(byte*)t;
		end->databuf_set(&d);
	}


/* end_desc */
	c.x=23;
	c.w=3;
	s=new ht_label();
	s->init(&c, "~end", end);
	insert(s);

/* mode */
	c=*b;
	c.h=1;
	c.w=16;
	c.x=7;
	c.y=3;
	mode=new ht_listpopup();
	mode->init(&c);
	mode->insertstring("byte (8-bit)");
	mode->insertstring("word (16-bit)");
	mode->insertstring("dword (32-bit)");
	mode->insertstring("string");
	insert(mode);

/* mode_desc */
	c.x=1;
	c.w=12;
	c.y=3;
	s=new ht_label();
	s->init(&c, "~mode", mode);
	insert(s);

/* action_expl */
	c=*b;
	c.x=1;
	c.y=5;
	c.w-=2;
	c.h=1;
	text=new ht_statictext();
	text->init(&c, "set each element to", align_left);
	insert(text);
	
/* action */
	ht_list *ehist=(ht_list*)find_atom(HISTATOM_EVAL_EXPR);

	c=*b;
	c.h=1;
	c.w=30;
	c.x=7;
	c.y=6;
	action=new ht_strinputfield();
	action->init(&c, 64, ehist);
	insert(action);

/* action_desc */
	c.x=1;
	c.w=27;
	c.y=6;
	s=new ht_label();
	s->init(&c, "e~xpr", action);
	insert(s);

/* help */
	c=*b;
	c.x=1;
	c.y=8;
	c.w-=c.x+2;
	c.h-=c.y+2;
	text=new ht_statictext();
	text->init(&c,
		"special vars:          special funcs:\n"
		"o - file offset        readbyte(ofs)\n"
		"i - iteration index    readstring(ofs, n)", align_left);
	insert(text);
}

void ht_blockop_dialog::done()
{
	ht_dialog::done();
}

struct ht_blockop_dialog_data {
	ht_inputfield_data start;
	ht_inputfield_data end;
	ht_listpopup_data mode;
	ht_inputfield_data action;
};

/*
 *   blockop_dialog
 */

static dword blockop_i;
static dword blockop_o;
static bool blockop_expr_is_const;

int blockop_symbol_eval(scalar_t *r, char *symbol)
{
	if (strcmp(symbol, "i")==0) {
		r->type=SCALAR_INT;
		r->scalar.integer.value=blockop_i;
		r->scalar.integer.type=TYPE_UNKNOWN;
		blockop_expr_is_const=false;
		return 1;
	} else if (strcmp(symbol, "o")==0) {
		r->type=SCALAR_INT;
		r->scalar.integer.value=blockop_o;
		r->scalar.integer.type=TYPE_UNKNOWN;
		blockop_expr_is_const=false;
		return 1;
	}
	return 0;
}

int func_readbyte(scalar_t *result, int_t *offset)
{
	ht_streamfile *f=(ht_streamfile*)eval_get_context();
	byte b;
	if ((f->seek(offset->value)!=0) || (f->read(&b, 1)!=1)) {
		set_eval_error("i/o error (requested %d, read %d from ofs %08x)", 1, 0, offset->value);
		return 0;
	}
	scalar_create_int_c(result, b);
	return 1;
}

int func_readstring(scalar_t *result, int_t *offset, int_t *len)
{
	ht_streamfile *f=(ht_streamfile*)eval_get_context();

	UINT l=len->value;
	void *buf=malloc(l);	/* FIXME: may be too slow... */

	if (buf) {
		str_t s;
		UINT c = 0;
		if ((f->seek(offset->value)!=0) || ( (c=f->read(buf, l)) !=l)) {
			free(buf);
			set_eval_error("i/o error (requested %d, read %d from ofs %08x)", l, c, offset->value);
			return 0;
		}
		s.value=(char*)buf;
		s.len=l;
		scalar_create_str(result, &s);
		free(buf);
		return 1;
	}
	set_eval_error("out of memory");
	return 0;
}

int blockop_func_eval(scalar_t *result, char *name, scalarlist_t *params)
{
/* FIXME: non-constant funcs (e.g. rand()) should
   set blockop_expr_is_const to false */
	evalfunc_t myfuncs[] = {
		{"readbyte", (void*)&func_readbyte, {SCALAR_INT}},
		{"readstring", (void*)&func_readstring, {SCALAR_INT, SCALAR_INT}},
		{NULL}
	};
	
	blockop_expr_is_const=false;
		
	return std_eval_func_handler(result, name, params, myfuncs);
}

/*
 *	BLOCKOP STRING
 */

class ht_blockop_str_context: public ht_data {
public:
	ht_streamfile *file;

	FILEOFS ofs;
	UINT len;

	UINT size;
	bool netendian;

	char *action;

	UINT i;
	FILEOFS o;

	bool expr_const;
	str_t v;

	~ht_blockop_str_context()
	{
		free(action);
		if (expr_const) string_destroy(&v);
	}
};

ht_data *create_blockop_str_context(ht_streamfile *file, FILEOFS ofs, UINT len, UINT size, bool netendian, char *action)
{
	ht_blockop_str_context *ctx = new ht_blockop_str_context();
	ctx->file = file;
	ctx->ofs = ofs;
	ctx->len = len;
	ctx->size = size;
	ctx->netendian = netendian;
	ctx->action = strdup(action);

	ctx->i = 0;
	ctx->o = ofs;

	blockop_expr_is_const = true;

	scalar_t r;
	if (!eval(&r, action, blockop_func_eval, blockop_symbol_eval, file)) {
		char *s;
		int p;
		get_eval_error(&s, &p);
		throw new ht_io_exception("error evaluating '%s': %s at %d", action, s, p);
	}

	ctx->expr_const = blockop_expr_is_const;

	if (ctx->expr_const) {
		scalar_context_str(&r, &ctx->v);
	}
	scalar_destroy(&r);
	return ctx;
}

#define BLOCKOP_STR_MAX_ITERATIONS 1024
bool blockop_str_process(ht_data *context, ht_text *progress_indicator)
{
	char status[64];
	ht_blockop_str_context *ctx = (ht_blockop_str_context*)context;
	if (ctx->expr_const) {
		sprintf(status, "operating (constant string)... %d%% complete", (ctx->o-ctx->ofs) * 100 / ctx->len);
		progress_indicator->settext(status);
		for (UINT i=0; i < BLOCKOP_STR_MAX_ITERATIONS; i++)
		if (ctx->o < ctx->ofs + ctx->len) {
			UINT s = ctx->v.len;
			if (ctx->o + s > ctx->ofs + ctx->len) s = ctx->ofs + ctx->len - ctx->o;
			
			ctx->file->seek(ctx->o);
			if (ctx->file->write(ctx->v.value, s)!=s) {
				throw new ht_io_exception("blockop_str(): write error at pos %08x, size %08x", ctx->o, s);
			}
			ctx->o += s;
		} else {
			return false;
		}
	} else {
		sprintf(status, "operating (variable string)... %d%% complete", (ctx->o-ctx->ofs) * 100 / ctx->len);
		progress_indicator->settext(status);
		scalar_t r;
		str_t sr;
		for (UINT i=0; i < BLOCKOP_STR_MAX_ITERATIONS; i++)
		if (ctx->o < ctx->ofs + ctx->len) {
			blockop_i = ctx->i;
			blockop_o = ctx->o;
			if (!eval(&r, ctx->action, blockop_func_eval, blockop_symbol_eval, ctx->file)) {
				char *s;
				int p;
				get_eval_error(&s, &p);
				throw new ht_io_exception("error evaluating '%s': %s at %d", ctx->action, s, p);
			}
			scalar_context_str(&r, &sr);
			scalar_destroy(&r);

			UINT s = sr.len;
			if (ctx->o+s > ctx->ofs+ctx->len) s = ctx->ofs+ctx->len-ctx->o;

			ctx->file->seek(ctx->o);
			if (ctx->file->write(sr.value, s)!=s) {
				throw new ht_io_exception("blockop_str(): write error at pos %08x, size %08x", ctx->o, s);
			}
			string_destroy(&sr);
			ctx->o += s;
			ctx->i++;
		} else {
			return false;
		}
	}
	return true;
}

/*
 *	BLOCKOP INTEGER
 */

class ht_blockop_int_context: public ht_data {
public:
	ht_streamfile *file;

	FILEOFS ofs;
	UINT len;

	UINT size;
	bool netendian;

	char *action;

	UINT i;
	FILEOFS o;

	bool expr_const;
	UINT v;

	~ht_blockop_int_context()
	{
		free(action);
	}
};

ht_data *create_blockop_int_context(ht_streamfile *file, FILEOFS ofs, UINT len, UINT size, bool netendian, char *action)
{
	ht_blockop_int_context *ctx = new ht_blockop_int_context();
	ctx->file = file;
	ctx->ofs = ofs;
	ctx->len = len;
	ctx->size = size;
	ctx->netendian = netendian;
	ctx->action = strdup(action);

	ctx->i = 0;
	ctx->o = ofs;

	blockop_expr_is_const = true;
	
	scalar_t r;
	int_t ir;
	if (!eval(&r, action, blockop_func_eval, blockop_symbol_eval, file)) {
		char *s;
		int p;
		get_eval_error(&s, &p);
		throw new ht_io_exception("error evaluating '%s': %s at %d", action, s, p);
	}

	ctx->expr_const = blockop_expr_is_const;
	
	if (ctx->expr_const) {
		scalar_context_int(&r, &ir);
		ctx->v = ir.value;
	}
	scalar_destroy(&r);
	return ctx;
}

#define BLOCKOP_INT_MAX_ITERATIONS	1024
bool blockop_int_process(ht_data *context, ht_text *progress_indicator)
{
	ht_blockop_int_context *ctx = (ht_blockop_int_context*)context;
	char status[64];
	if (ctx->expr_const) {
		sprintf(status, "operating (constant integer)... %d%% complete", (ctx->o-ctx->ofs) * 100 / ctx->len);
		progress_indicator->settext(status);
		for (UINT i=0; i < BLOCKOP_INT_MAX_ITERATIONS; i++)
		if (ctx->o < ctx->ofs + ctx->len) {
			UINT s = ctx->size;
			if (ctx->o + s > ctx->ofs + ctx->len) s = ctx->ofs + ctx->len - ctx->o;
			byte ibuf[4];
			if (ctx->netendian) {
				h2n_int(ibuf, ctx->v, 4);
			} else {
				h2nn_int(ibuf, ctx->v, 4);
			}
			ctx->file->seek(ctx->o);
			if (ctx->file->write(ibuf, s)!=s) {
				throw new ht_io_exception("blockop_int(): write error at pos %08x, size %08x", ctx->o, s);
			}
			ctx->o += s;
		} else {
			return false;
		}
	} else {
		sprintf(status, "operating (variable integer)... %d%% complete", (ctx->o-ctx->ofs) * 100 / ctx->len);
		progress_indicator->settext(status);
		scalar_t r;
		int_t ir;
		UINT v;
		for (UINT i=0; i < BLOCKOP_INT_MAX_ITERATIONS; i++)
		if (ctx->o < ctx->ofs + ctx->len) {
			blockop_o = ctx->o;
			blockop_i = ctx->i;
			if (!eval(&r, ctx->action, blockop_func_eval, blockop_symbol_eval, ctx->file)) {
				char *s;
				int p;
				get_eval_error(&s, &p);
				throw new ht_io_exception("error evaluating '%s': %s at %d", ctx->action, s, p);
			}
			scalar_context_int(&r, &ir);
			scalar_destroy(&r);
			v=ir.value;

			UINT s = ctx->size;
			if (ctx->o+s > ctx->ofs+ctx->len) s = ctx->ofs+ctx->len-ctx->o;

			byte ibuf[4];
			if (ctx->netendian) {
				h2n_int(ibuf, v, 4);
			} else {
				h2nn_int(ibuf, v, 4);
			}
			ctx->file->seek(ctx->o);
			if (ctx->file->write(ibuf, s)!=s) {
				throw new ht_io_exception("blockop_int(): write error at pos %08x, size %08x", ctx->o, s);
			}
			ctx->o += s;
			ctx->i++;
		} else {
			return false;
		}
	}
	return true;
}

/*
 *
 */

bool format_string_to_offset_if_avail(ht_format_viewer *format, byte *string, int stringlen, char *string_desc, FILEOFS *ofs)
{
	if (string && *string && stringlen<64) {
		char str[64];
		memmove(str, string, stringlen);
		str[stringlen]=0;
		if (!format->string_to_offset(str, ofs)) {
			errorbox("%s: '%s' doesn't seem to be a valid offset", string_desc, &str);
			return false;
		}
		return true;
	}
	return false;
}
		
		
void blockop_dialog(ht_format_viewer *format, FILEOFS pstart, FILEOFS pend)
{
	bounds b;
	b.w=50;
	b.h=15;
	b.x=(screen->size.w-b.w)/2;
	b.y=(screen->size.h-b.h)/2;
	
	ht_blockop_dialog *d=new ht_blockop_dialog();
	d->init(&b, pstart, pend, 0);
	if (d->run(0)) {
		ht_blockop_dialog_data t;
		d->databuf_get(&t);
		
		ht_streamfile *file=format->get_file();

		baseview->sendmsg(cmd_edit_mode_i, file, NULL);
		
		if (file->get_access_mode() & FAM_WRITE) {
			FILEOFS start=pstart, end=pend;

			if (format_string_to_offset_if_avail(format, t.start.text, t.start.textlen, "start", &start) &&
			format_string_to_offset_if_avail(format, t.end.text, t.end.textlen, "end", &end)) {
				int esize=0;
				int esizes[3]={4, 2, 1};
				switch (t.mode.cursor_id) {
/* element type: byte */
					case 0: esize++;
/* element type: word */
					case 1: esize++;
/* element type: dword */
					case 2: {
						char a[1024];	/* FIXME: possible buffer overflow */
						bin2str(a, t.action.text, t.action.textlen);
						insert_history_entry((ht_list*)find_atom(HISTATOM_EVAL_EXPR), a, NULL);
						
						char addr[128];
						bin2str(addr, t.start.text, t.start.textlen);
						insert_history_entry((ht_list*)find_atom(HISTATOM_GOTO), addr, NULL);
						bin2str(addr, t.end.text, t.end.textlen);
						insert_history_entry((ht_list*)find_atom(HISTATOM_GOTO), addr, NULL);
						
						esize = esizes[esize];
						ht_data *ctx = NULL;
						try {
							ctx = create_blockop_int_context(file, start, end-start, esize, false, a);
							if (ctx) {
								/*bool b = */execute_process(blockop_int_process, ctx);
							}
						} catch (ht_exception *e) {
							errorbox("error: %s", e->what());
						}
						if (ctx) delete ctx;
						break;
					}
/* element type: string */
					case 3: {
						char a[1024];	/* FIXME: possible buffer overflow */
						bin2str(a, t.action.text, t.action.textlen);
						insert_history_entry((ht_list*)find_atom(HISTATOM_EVAL_EXPR), a, NULL);

						char addr[128];
						bin2str(addr, t.start.text, t.start.textlen);
						insert_history_entry((ht_list*)find_atom(HISTATOM_GOTO), addr, NULL);
						bin2str(addr, t.end.text, t.end.textlen);
						insert_history_entry((ht_list*)find_atom(HISTATOM_GOTO), addr, NULL);
						
						ht_data *ctx = NULL;
						try {
							ctx = create_blockop_str_context(file, start, end-start, esize, false, a);
							if (ctx) {
								/*bool b = */execute_process(blockop_str_process, ctx);
							}
						} catch (ht_exception *e) {
							errorbox("error: %s", e->what());
						}
						if (ctx) delete ctx;
						break;
					}
					default:
						errorbox("mode %d not supported", t.mode.cursor_id);
				}
			}
		}
	}

	d->done();
	delete d;
}