forked from fuzziqersoftware/memwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_utils.c
562 lines (497 loc) · 15 KB
/
parse_utils.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
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
// parse_utils.c, by Martin Michelsen, 2012
// odds and ends used for messing with text and data
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/time.h>
#include "parse_utils.h"
extern int use_color;
inline void bswap_int8_t(void* a) { }
inline void bswap_int16_t(void* a) {
((int8_t*)a)[0] ^= ((int8_t*)a)[1];
((int8_t*)a)[1] ^= ((int8_t*)a)[0];
((int8_t*)a)[0] ^= ((int8_t*)a)[1];
}
inline void bswap_int32_t(void* a) {
((int8_t*)a)[0] ^= ((int8_t*)a)[3];
((int8_t*)a)[3] ^= ((int8_t*)a)[0];
((int8_t*)a)[0] ^= ((int8_t*)a)[3];
((int8_t*)a)[2] ^= ((int8_t*)a)[1];
((int8_t*)a)[1] ^= ((int8_t*)a)[2];
((int8_t*)a)[2] ^= ((int8_t*)a)[1];
}
inline void bswap_int64_t(void* a) {
((int8_t*)a)[0] ^= ((int8_t*)a)[7];
((int8_t*)a)[7] ^= ((int8_t*)a)[0];
((int8_t*)a)[0] ^= ((int8_t*)a)[7];
((int8_t*)a)[6] ^= ((int8_t*)a)[1];
((int8_t*)a)[1] ^= ((int8_t*)a)[6];
((int8_t*)a)[6] ^= ((int8_t*)a)[1];
((int8_t*)a)[4] ^= ((int8_t*)a)[3];
((int8_t*)a)[3] ^= ((int8_t*)a)[4];
((int8_t*)a)[4] ^= ((int8_t*)a)[3];
((int8_t*)a)[2] ^= ((int8_t*)a)[5];
((int8_t*)a)[5] ^= ((int8_t*)a)[2];
((int8_t*)a)[2] ^= ((int8_t*)a)[5];
}
void bswap(void* a, int size) {
if (size == 1)
bswap_int8_t(a);
if (size == 2)
bswap_int16_t(a);
if (size == 4)
bswap_int32_t(a);
if (size == 8)
bswap_int64_t(a);
}
// outputs control characters to the console to change color/formatting
void change_color(int color, ...) {
char fmt[0x40] = "\033";
int fmt_len = strlen(fmt);
va_list va;
va_start(va, color);
do {
fmt[fmt_len] = (fmt[fmt_len - 1] == '\033') ? '[' : ';';
fmt_len++;
fmt_len += sprintf(&fmt[fmt_len], "%d", color);
color = va_arg(va, int);
} while (color != FORMAT_END);
va_end(va);
fmt[fmt_len] = 'm';
fmt[fmt_len + 1] = 0;
printf("%s", fmt);
}
void print_data(unsigned long long address, const void* _data,
const void* _prev, unsigned long long data_size, int flags) {
if (data_size == 0)
return;
// if color is disabled or no diff source is given, disable diffing
const uint8_t* data = (const uint8_t*)_data;
const uint8_t* prev = (const uint8_t*)((_prev && use_color) ? _prev : _data);
char data_ascii[20];
char prev_ascii[20]; // actually only 16 is necessary but w/e
// start_offset is how many blank spaces to print before the first byte
int start_offset = address & 0x0F;
address &= ~0x0F;
data_size += start_offset;
// if nonzero, print the address here (the loop won't do it for the 1st line)
if (start_offset)
printf("%016llX | ", address);
// print initial spaces, if any
unsigned long long x, y;
for (x = 0; x < start_offset; x++) {
printf(" ");
data_ascii[x] = ' ';
prev_ascii[x] = ' ';
}
// print the data
for (; x < data_size; x++) {
int line_offset = x & 0x0F;
int data_offset = x - start_offset;
data_ascii[line_offset] = data[data_offset];
prev_ascii[line_offset] = prev[data_offset];
// first byte on the line? then print the address
if ((x & 0x0F) == 0)
printf("%016llX | ", address + x);
// print the byte itself
if (prev[data_offset] != data[data_offset])
change_color(FORMAT_BOLD, FORMAT_FG_RED, FORMAT_END);
printf("%02X ", data[data_offset]);
if (prev[data_offset] != data[data_offset])
change_color(FORMAT_NORMAL, FORMAT_END);
// last byte on the line? then print the ascii view and a \n
if ((x & 0x0F) == 0x0F) {
printf("| ");
for (y = 0; y < 16; y++) {
if (prev_ascii[y] != data_ascii[y])
change_color(FORMAT_FG_RED, FORMAT_END);
if (data_ascii[y] < 0x20 || data_ascii[y] == 0x7F) {
if (use_color)
change_color(FORMAT_INVERSE, FORMAT_END);
putc(' ', stdout);
if (use_color)
change_color(FORMAT_NORMAL, FORMAT_END);
} else
putc(data_ascii[y], stdout);
if (prev_ascii[y] != data_ascii[y])
change_color(FORMAT_NORMAL, FORMAT_END);
}
printf("\n");
}
}
// if the last line is a partial line, print the remaining ascii chars
if (x & 0x0F) {
for (y = x; y & 0x0F; y++)
printf(" ");
printf("| ");
for (y = 0; y < (x & 0x0F); y++) {
if (prev_ascii[y] != data_ascii[y])
change_color(FORMAT_FG_RED, FORMAT_END);
if (data_ascii[y] < 0x20 || data_ascii[y] == 0x7F) {
if (use_color)
change_color(FORMAT_INVERSE, FORMAT_END);
putc(' ', stdout);
if (use_color)
change_color(FORMAT_NORMAL, FORMAT_END);
} else
putc(data_ascii[y], stdout);
if (prev_ascii[y] != data_ascii[y])
change_color(FORMAT_NORMAL, FORMAT_END);
}
printf("\n");
}
}
// reads an unsigned long long from the input string. defaults to decimal, but
// reads hex if the number is prefixed with "0x".
// reads binary if the number is prefixed with "0b".
// returns 0 if an error occurred
int parse_ull(const char* in, unsigned long long* value, int default_hex) {
if ((in[0] == '0') && ((in[1] == 'x') || (in[1] == 'X')))
return sscanf(&in[2], "%llx", value);
if ((in[0] == '0') && ((in[1] == 'b') || (in[1] == 'B'))) {
*value = 0;
in += 2;
while (in[0] == '1' || in[0] == '2') {
*value <<= 1;
if (in[0] == 1)
*value |= 1;
}
return 1;
}
return sscanf(in, default_hex ? "%llx" : "%llu", value);
}
// macros used by read_string_data to append to a buffer
#define expand(bytes) { \
size += bytes; \
*data = (unsigned char*)realloc(*data, size); \
if (vmask) \
*mask = (unsigned char*)realloc(*mask, size); \
}
#define write_byte(x) { \
expand(1); \
(*data)[size - 1] = x; \
if (vmask) \
(*mask)[size - 1] = 0xFF; \
}
#define write_short(x) { \
expand(2); \
*(uint16_t*)(*data + size - 2) = x; \
if (vmask) \
*(uint16_t*)(*mask + size - 2) = 0xFFFF; \
}
#define write_blank() { \
expand(1); \
(*mask)[size - 1] = 0; \
}
// parses the memwatch data format from a string
unsigned long long read_string_data(const char* in, void** vdata, void** vmask) {
*vdata = NULL;
if (vmask)
*vmask = NULL;
unsigned char** data = (unsigned char**)vdata;
unsigned char** mask = (unsigned char**)vmask;
int read, chr = 0;
int string = 0, unicode_string = 0, comment = 0, multiline_comment = 0, high = 1;
int filename = 0, filename_start;
unsigned long size = 0;
int endian = 0;
while (in[0]) {
read = 0;
// if between // and a newline, don't write to output buffer
if (comment) {
if (in[0] == '\n')
comment = 0;
in++;
// if between /* and */, don't write to output buffer
} else if (multiline_comment) {
if (in[0] == '*' && in[1] == '/') {
multiline_comment = 0;
in++;
}
in++;
// if between quotes, read bytes to output buffer, unescaping
} else if (string) {
if (in[0] == '\"')
string = 0;
else if (in[0] == '\\') { // unescape char after a backslash
if (!in[1])
return size;
if (in[1] == 'n') {
write_byte('\n');
} else if (in[1] == 'r') {
write_byte('\r');
} else if (in[1] == 't') {
write_byte('\t');
} else {
write_byte(in[1]);
}
in++;
} else
write_byte(in[0]);
in++;
// if between single quotes, word-expand bytes to output buffer, unescaping
} else if (unicode_string) {
if (in[0] == '\'')
unicode_string = 0;
else if (in[0] == '\\') { // unescape char after a backslash
if (!in[1])
return size;
if (in[1] == 'n') {
write_short('\n');
} else if (in[1] == 'r') {
write_short('\r');
} else if (in[1] == 't') {
write_short('\t');
} else {
write_short(in[1]);
}
if (endian)
bswap(&(*data)[size - 2], 2);
in++;
} else {
write_short(in[0]);
if (endian)
bswap(&(*data)[size - 2], 2);
}
in++;
// if between <>, read a file name, then stick that file into the buffer
} else if (filename) {
if (in[0] == '>') {
filename = 0;
write_byte(0); // null-terminate the filename
// TODO: support <filename@offset:size> syntax
// open the file, read it into the buffer, close the file
FILE* f = fopen((char*)(*data + filename_start), "rb");
if (!f) {
if (data)
free(data);
return 0;
}
fseek(f, 0, SEEK_END);
int file_size = ftell(f);
size = filename_start + file_size;
*data = realloc(*data, size);
fseek(f, 0, SEEK_SET);
fread((*data + filename_start), 1, file_size, f);
fclose(f);
} else
write_byte(in[0]);
in++;
// ? is an unknown byte, but only if the caller wants a mask
} else if (in[0] == '?' && vmask) {
write_blank();
in++;
// $ changes the endian-ness
} else if (in[0] == '$') {
endian = !endian;
in++;
// # signifies a decimal number
} else if (in[0] == '#') { // 8-bit
unsigned long long value;
in++;
if (in[0] == '#') { // 16-bit
in++;
if (in[0] == '#') { // 32-bit
in++;
if (in[0] == '#') { // 64-bit
in++;
expand(8);
parse_ull(in, (unsigned long long*)(&((*data)[size - 8])), 0);
if (endian)
bswap(&((*data)[size - 8]), 8);
if (mask)
*(unsigned long long*)(&((*mask)[size - 8])) = 0xFFFFFFFFFFFFFFFF;
} else {
expand(4);
parse_ull(in, &value, 0);
if (endian)
bswap(&value, 4);
*(int32_t*)(&((*data)[size - 4])) = value;
if (mask)
*(uint32_t*)(&((*mask)[size - 4])) = 0xFFFFFFFF;
}
} else {
expand(2);
parse_ull(in, &value, 0);
if (endian)
bswap(&value, 2);
*(int16_t*)(&((*data)[size - 2])) = value;
if (mask)
*(uint16_t*)(&((*mask)[size - 2])) = 0xFFFF;
}
} else {
expand(1);
parse_ull(in, &value, 0);
*(int8_t*)(&((*data)[size - 1])) = value;
if (mask)
*(uint8_t*)(&((*mask)[size - 1])) = 0xFF;
}
if (in[0] == '-')
in++;
while (isdigit(in[0]))
in++;
// % is a float, %% is a double
} else if (in[0] == '%') {
in++;
if (in[0] == '%') {
in++;
expand(8);
double* value = (double*)(&((*data)[size - 8]));
sscanf(in, "%lf", value);
if (endian)
bswap(value, 8);
if (mask)
*(unsigned long long*)(&((*mask)[size - 8])) = 0xFFFFFFFFFFFFFFFF;
} else {
expand(4);
float* value = (float*)(&((*data)[size - 4]));
sscanf(in, "%f", value);
if (endian)
bswap(value, 4);
if (mask)
*(uint32_t*)(&((*mask)[size - 4])) = 0xFFFFFFFF;
}
if (in[0] == '-')
in++;
while (isdigit(in[0]) || (in[0] == '.'))
in++;
// anything else is a hex digit
} else {
if ((in[0] >= '0') && (in[0] <= '9')) {
read = 1;
chr |= (in[0] - '0');
}
if ((in[0] >= 'A') && (in[0] <= 'F')) {
read = 1;
chr |= (in[0] - 'A' + 0x0A);
}
if ((in[0] >= 'a') && (in[0] <= 'f')) {
read = 1;
chr |= (in[0] - 'a' + 0x0A);
}
if (in[0] == '\"')
string = 1;
if (in[0] == '\'')
unicode_string = 1;
if (in[0] == '/' && in[1] == '/')
comment = 1;
if (in[0] == '/' && in[1] == '*')
multiline_comment = 1;
if (in[0] == '<') {
filename = 1;
filename_start = size;
}
in++;
}
if (read) {
if (high)
chr = chr << 4;
else {
write_byte(chr);
chr = 0;
}
high = !high;
}
}
return size;
}
// reads a string from the given stream. return value must be free'd later.
char* read_string_delimited(FILE* in, char delimiter, int consume_delim) {
char* string = NULL;
int len = 0;
do {
len++;
string = realloc(string, len);
string[len - 1] = fgetc(stdin);
} while (string[len - 1] != delimiter);
string[len - 1] = 0;
if (!consume_delim)
ungetc(delimiter, in);
return string;
}
// removes spaces from the beginning of a string
void trim_spaces(char* string) {
if (!string)
return;
int x;
for (x = 0; string[x] == ' ' && string[x]; x++);
strcpy(string, &string[x]);
}
// finds the end of the word, returns a pointer to the first word after it
const char* skip_word(const char* in, char delim) {
if (!in)
return NULL;
for (; *in != delim && *in; in++);
if (!*in)
return in;
in++;
for (; *in == delim; in++);
return in;
}
// returns a copy of the first word in the string, which must be free'd later
char* get_word(const char* in, char delim) {
if (!in)
return NULL;
const char* word_end = strchr(in, delim);
return word_end ? strndup(in, word_end - in) : strdup(in);
}
// makes a copy of the quoted string given, taking care of escaped items. the
// quote character is the first character of in.
// returns the number of chars consumed in input, or -1 on error
int copy_quoted_string(const char* in, char** out) {
if (!in || !out)
return -1;
if (!in[0])
return 0;
const char* in_orig = in;
int out_size = 0;
*out = NULL;
char quote = in[0];
for (in++; in[0] && in[0] != quote; in++) {
out_size++;
*out = (char*)realloc(*out, out_size * sizeof(char*));
if (in[0] == '\\' && in[1]) {
(*out)[out_size - 1] = in[1];
in++;
} else
(*out)[out_size - 1] = in[0];
}
if (in[0] == quote)
in++;
return in - in_orig;
}
// reads addr:size (hex) or addr size (dec) from a string; returns the number of
// chars used up, or 0 if an error occurred
int read_addr_size(const char* str, unsigned long long* addr,
unsigned long long* size) {
// read address
sscanf(str, "%llX", addr);
// : means size is hex; space means it's decimal
const char* next_col = skip_word(str, ':');
const char* next_spc = skip_word(str, ' ');
if (next_spc - next_col > 0 && next_col[0]) {
sscanf(next_col, "%llX", size);
return skip_word(next_col, ' ') - str;
} else if (next_spc[0]) {
sscanf(next_spc, "%llu", size);
return skip_word(next_spc, ' ') - str;
}
return 0;
}
// returns a string representing the current local time
// example: "27 December 2013 17:45:40.236"
void get_current_time_string(char* output) {
struct timeval rawtime;
gettimeofday(&rawtime, NULL);
struct tm cooked;
localtime_r(&rawtime.tv_sec, &cooked);
const char* monthnames[] = {"January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December"};
sprintf(output, "%u %s %4u %2u:%02u:%02u.%03u", cooked.tm_mday,
monthnames[cooked.tm_mon], cooked.tm_year + 1900,
cooked.tm_hour, cooked.tm_min, cooked.tm_sec,
rawtime.tv_usec / 1000);
}