67 |
|
* byte_string = (expression | STRING) {COMMA (expression | STRING)} END |
68 |
|
*/ |
69 |
|
|
70 |
< |
static bool byte_string(uint8 *s, uintptr &len) |
70 |
> |
static bool byte_string(uint8 *&str, uintptr &len) |
71 |
|
{ |
72 |
|
uintptr value; |
73 |
|
|
74 |
+ |
static const int GRANULARITY = 16; // must be a power of 2 |
75 |
+ |
str = NULL; |
76 |
|
len = 0; |
77 |
|
goto start; |
78 |
|
|
83 |
|
start: |
84 |
|
if (mon_token == T_STRING) { |
85 |
|
uint8 *p = (uint8 *)mon_string; |
86 |
< |
while ((*s++ = *p++) != 0) ; |
87 |
< |
s--; |
88 |
< |
len += strlen(mon_string); |
86 |
> |
unsigned n = strlen(mon_string); |
87 |
> |
str = (uint8 *)realloc(str, (len + n - 1 + GRANULARITY) & ~(GRANULARITY - 1)); |
88 |
> |
memcpy(str + len, mon_string, n); |
89 |
> |
len += n; |
90 |
|
mon_get_token(); |
91 |
|
} else if (mon_expression(&value)) { |
92 |
< |
*s++ = value; |
92 |
> |
str = (uint8 *)realloc(str, (len + GRANULARITY) & ~(GRANULARITY - 1)); |
93 |
> |
str[len] = value; |
94 |
|
len++; |
95 |
< |
} else |
95 |
> |
} else { |
96 |
> |
if (str) |
97 |
> |
free(str); |
98 |
|
return false; |
99 |
+ |
} |
100 |
|
|
101 |
< |
} else if (mon_token == T_END) |
101 |
> |
} else if (mon_token == T_END) { |
102 |
|
return true; |
103 |
< |
else { |
103 |
> |
} else { |
104 |
|
mon_error("',' expected"); |
105 |
+ |
if (str) |
106 |
+ |
free(str); |
107 |
|
return false; |
108 |
|
} |
109 |
|
} |
429 |
|
void modify(void) |
430 |
|
{ |
431 |
|
uintptr adr, len, src_adr = 0; |
432 |
< |
uint8 str[256]; |
432 |
> |
uint8 *str; |
433 |
|
|
434 |
|
if (!mon_expression(&adr)) |
435 |
|
return; |
438 |
|
|
439 |
|
while (src_adr < len) |
440 |
|
mon_write_byte(adr++, str[src_adr++]); |
432 |
– |
|
441 |
|
mon_dot_address = adr; |
442 |
+ |
|
443 |
+ |
free(str); |
444 |
|
} |
445 |
|
|
446 |
|
|
452 |
|
void fill(void) |
453 |
|
{ |
454 |
|
uintptr adr, end_adr, len, src_adr = 0; |
455 |
< |
uint8 str[256]; |
455 |
> |
uint8 *str; |
456 |
|
|
457 |
|
if (!mon_expression(&adr)) |
458 |
|
return; |
463 |
|
|
464 |
|
while (adr <= end_adr) |
465 |
|
mon_write_byte(adr++, str[src_adr++ % len]); |
466 |
+ |
|
467 |
+ |
free(str); |
468 |
|
} |
469 |
|
|
470 |
|
|
547 |
|
void hunt(void) |
548 |
|
{ |
549 |
|
uintptr adr, end_adr, len; |
550 |
< |
uint8 str[256]; |
550 |
> |
uint8 *str; |
551 |
|
int num = 0; |
552 |
|
|
553 |
|
if (!mon_expression(&adr)) |
575 |
|
adr++; |
576 |
|
} |
577 |
|
|
578 |
+ |
free(str); |
579 |
+ |
|
580 |
|
if (num & 7) |
581 |
|
fputc('\n', monout); |
582 |
|
fprintf(monout, "Found %d occurrences\n", num); |