/
main.c
500 lines (406 loc) · 12.3 KB
/
main.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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YES 1
#define NO 0
#define FAILED 0
#define SUCCEEDED 1
#define TRUE 1
#define FALSE 0
int _get_next_number(char *in, int *out) {
int i = 0, o, n;
if (in[i] >= '0' && in[i] <= '9') {
/* is it hex after all? */
n = 0;
for (o = 0; 1; o++) {
if (in[i+o] >= '0' && in[i+o] <= '9')
continue;
if (in[i+o] >= 'a' && in[i+o] <= 'f') {
n = 1;
break;
}
if (in[i+o] >= 'A' && in[i+o] <= 'F') {
n = 1;
break;
}
if (in[i+o] == 'h' || in[i+o] == 'H') {
n = 1;
break;
}
break;
}
if (n == 1) {
/* hex */
o = 0;
while (TRUE) {
if (in[i] >= '0' && in[i] <= '9')
o = (o * 16) + in[i] - '0';
else if (in[i] >= 'a' && in[i] <= 'f')
o = (o * 16) + in[i] - 'a' + 0xA;
else if (in[i] >= 'A' && in[i] <= 'F')
o = (o * 16) + in[i] - 'A' + 0xA;
else if (in[i] == 'h' || in[i] == 'H')
break;
else if (in[i] == ' ' || in[i] == 0x09 || in[i] == 0)
break;
else
return FAILED;
i++;
}
}
else {
/* decimal */
for (o = 0; in[i] >= '0' && in[i] <= '9'; i++)
o = (o * 10) + in[i] - '0';
if (!(in[i] == ' ' || in[i] == 0x09 || in[i] == 0))
return FAILED;
}
}
else if (in[i] == '$') {
i++;
o = 0;
while (TRUE) {
if (in[i] >= '0' && in[i] <= '9')
o = (o * 16) + in[i] - '0';
else if (in[i] >= 'a' && in[i] <= 'f')
o = (o * 16) + in[i] - 'a' + 0xA;
else if (in[i] >= 'A' && in[i] <= 'F')
o = (o * 16) + in[i] - 'A' + 0xA;
else if (in[i] == ' ' || in[i] == 0x09 || in[i] == 0)
break;
else
return FAILED;
i++;
}
}
else
return FAILED;
*out = o;
return SUCCEEDED;
}
unsigned char *g_binary_file = NULL;
int _read_binary_file(char *filename, int *did_we_read_data, FILE *f, int *file_size) {
FILE *fb;
fb = fopen(filename, "rb");
if (fb == NULL) {
if (g_binary_file != NULL) {
*did_we_read_data = NO;
return SUCCEEDED;
}
fprintf(stderr, "Error opening file \"%s\".\n", filename);
fclose(f);
return FAILED;
}
fseek(fb, 0, SEEK_END);
*file_size = (int)ftell(fb);
fseek(fb, 0, SEEK_SET);
if (g_binary_file != NULL)
free(g_binary_file);
g_binary_file = calloc(*file_size, 1);
if (g_binary_file == NULL) {
fprintf(stderr, "Error allocating memory for file \"%s\".\n", filename);
fclose(f);
fclose(fb);
*did_we_read_data = NO;
return FAILED;
}
if (fread(g_binary_file, 1, *file_size, fb) != (size_t) *file_size) {
fprintf(stderr, "Could not read all %d bytes of \"%s\"!", *file_size, filename);
return FAILED;
};
fclose(fb);
*did_we_read_data = YES;
return SUCCEEDED;
}
/* Get the next token from the input string and write the results on
the result argument. */
int get_token(char **input, char *result) {
int n = 0;
sscanf(*input, "%255s%n", result, &n);
if (!n) {
return FAILED;
}
*input += n;
return SUCCEEDED;
}
/* Extracts contents of byte tester comments in-place. The result is
placed on the input string itself, overwriting its contents. */
int extract_comments(char *input, size_t size) {
char *current = input, *output = input, *end = NULL;
while (*current) {
int comment_length;
char *comment = NULL, *result = NULL;
/* Search for the next comment of
each type, picking the nearest. */
/* Semicolon comment */
result = strstr(current, "; @BT");
if (result) {
comment = result + 5;
end = strchr(comment, '\n');
}
/* Double slash comment */
result = strstr(current, "// @BT");
if (result && (!comment || result < comment)) {
comment = result + 6;
end = strchr(comment, '\n');
}
/* Block comment */
result = strstr(current, "/* @BT");
if (result && (!comment || result < comment)) {
comment = result + 6;
end = strstr(comment, "*/");
}
/* Stop parsing if there is no more comments */
if (!comment) {
break;
}
/* Extend unclosed comments to the end of the input string.
This handles the cases where a semicolon or double slash
comment is on the last line, or a unclosed block comment. */
if (!end) {
end = input + size;
}
/* Overwrite input string with comment contents */
current = comment;
comment_length = (int)(end - current);
memmove(output, current, comment_length);
output += comment_length;
current += comment_length;
}
*output = '\0';
return SUCCEEDED;
}
int main(int argc, char *argv[]) {
char tmp[256], test_id[256], tag_id[256];
char *input_name = NULL, *input = NULL, *current = NULL;
unsigned char bytes[256];
int input_size, file_size, end, byte_count, i, j, length, tag_start, tag_end, wrong_bytes, failures, should_extract_comments = NO, use_address = NO, address = 0, did_we_read_data = NO, got_it = NO;
FILE *f;
if (argc < 2 || argc > 3 || argv == NULL) {
fprintf(stderr, "\n");
#ifdef AMIGACPU
fprintf(stderr, "Byte tester 2.0 (" AMIGACPU ")\n");
#else
fprintf(stderr, "Byte tester 2.0\n");
#endif
fprintf(stderr, "\n");
fprintf(stderr, "USAGE: %s [-s] <TESTS FILE / SOURCE FILE>\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "If -s is given then we read tests file data from the comments of a source code file.\n");
fprintf(stderr, "\n");
fprintf(stderr, "TESTS FILE FORMAT:\n");
fprintf(stderr, "\n");
fprintf(stderr, "...\n");
fprintf(stderr, "<BINARY FILE NAME>\n");
fprintf(stderr, "<TEST ID> <TAG ID> START <BYTE 1> <BYTE 2> ... <BYTE N> END /* Require the bytes to be between the tags */\n");
fprintf(stderr, "<TEST ID> -a <INTEGER ADDRESS> START <BYTE 1> <BYTE 2> ... <BYTE N> END /* Require the bytes to be at the address */\n");
fprintf(stderr, "<TEST ID> -y <STRING> /* Require that the string is found */\n");
fprintf(stderr, "<TEST ID> -n <STRING> /* Require that the string is not found */\n");
fprintf(stderr, "...\n");
fprintf(stderr, "\n");
fprintf(stderr, "If <TAG ID> is \"01\", then we test if the bytes between \"01>\" and \"<01\" match with bytes 1...n\n");
fprintf(stderr, "\n");
return 1;
}
/* Parse flags */
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-s") == 0) {
should_extract_comments = YES;
}
else {
input_name = calloc(strlen(argv[i]) + 1, 1);
strcpy(input_name, argv[i]);
}
}
f = fopen(input_name, "rb");
if (f == NULL) {
fprintf(stderr, "Error opening file \"%s\".\n", argv[1]);
return 1;
}
fseek(f, 0, SEEK_END);
input_size = (int)ftell(f);
fseek(f, 0, SEEK_SET);
/* make sure we allocate one extra byte for terminator 0 */
input = calloc(input_size + 1, 1);
current = input;
if (fread(input, 1, input_size, f) != (size_t) input_size) {
fprintf(stderr, "Could not read all %d bytes of \"%s\"!", input_size, input_name);
return FAILED;
};
free(input_name);
input_name = NULL;
/* Extract input from comments in file */
if (should_extract_comments) {
extract_comments(input, input_size);
}
get_token(¤t, tmp);
/* read the binary file */
if (_read_binary_file(tmp, &did_we_read_data, f, &file_size) == FAILED)
return 1;
/* execute the tests */
failures = 0;
end = 0;
while (end == 0) {
if (!get_token(¤t, test_id))
break;
/* test_id could be filename */
if (_read_binary_file(test_id, &did_we_read_data, f, &file_size) == FAILED)
return 1;
if (did_we_read_data == YES)
continue;
if (!get_token(¤t, tag_id))
break;
if (strlen(tag_id) != 2) {
fprintf(stderr, "Test \"%s\" FAILED - TAG ID must be exactly two characters long. Error in \"%s\".\n", test_id, tag_id);
failures = 1;
break;
}
if (strcmp(tag_id, "-a") == 0) {
use_address = YES;
if (!get_token(¤t, tmp))
break;
if (_get_next_number(tmp, &address) == FAILED) {
fprintf(stderr, "\"%s\" is not a number.\n", tmp);
failures = 1;
break;
}
if (!get_token(¤t, tmp))
break;
if (strcmp(tmp, "START") != 0) {
fprintf(stderr, "Test \"%s\" FAILED - START is missing in \"%s\".\n", test_id, argv[1]);
failures = 1;
break;
}
}
else if (strcmp(tag_id, "-y") == 0) {
if (!get_token(¤t, tmp))
break;
length = (int)strlen(tmp);
got_it = NO;
for (i = 0; i < file_size; i++) {
for (j = 0; j < length; j++) {
if (g_binary_file[i+j] != tmp[j])
break;
}
if (j == length) {
/* we found the string -> ok! */
got_it = YES;
break;
}
}
if (got_it == YES) {
fprintf(stderr, "Test \"%s\" SUCCEEDED!\n", test_id);
continue;
}
}
else if (strcmp(tag_id, "-n") == 0) {
if (!get_token(¤t, tmp))
break;
length = (int)strlen(tmp);
got_it = NO;
for (i = 0; i < file_size; i++) {
for (j = 0; j < length; j++) {
if (g_binary_file[i+j] != tmp[j])
break;
}
if (j == length) {
/* we found the string -> not ok! */
got_it = YES;
break;
}
}
if (got_it == YES)
return 1;
fprintf(stderr, "Test \"%s\" SUCCEEDED!\n", test_id);
continue;
}
else {
if (!get_token(¤t, tmp))
break;
if (strcmp(tmp, "START") != 0) {
fprintf(stderr, "Test \"%s\" FAILED - START/-a/-y/-n is missing in \"%s\".\n", test_id, argv[1]);
failures = 1;
break;
}
}
byte_count = 0;
while (1) {
if (byte_count == 256) {
fprintf(stderr, "Test \"%s\" FAILED - Each test can contain max 256 bytes.\n", test_id);
failures = 1;
end = 1;
break;
}
if (!get_token(¤t, tmp)) {
end = 1;
break;
}
if (strcmp(tmp, "END") == 0)
break;
if (strlen(tmp) == 1)
bytes[byte_count] = tmp[0];
else if (strlen(tmp) == 2)
bytes[byte_count] = (unsigned char)strtol(tmp, NULL, 16);
else {
fprintf(stderr, "Test \"%s\" FAILED - Unknown data \"%s\" in test \"%s\"! Must either be a character, two character hexadecimal value or END.\n", test_id, tmp, test_id);
failures = 1;
end = 1;
break;
}
byte_count++;
}
/* execute the test */
if (use_address == NO) {
for (i = 0; i < file_size - 2; i++) {
if (g_binary_file[i] == tag_id[0] && g_binary_file[i+1] == tag_id[1] && g_binary_file[i+2] == '>')
break;
}
if (i == file_size - 2) {
fprintf(stderr, "Test \"%s\" FAILED - Could not find tag \"%s>\".\n", test_id, tag_id);
failures = 1;
continue;
}
tag_start = i+3;
for ( ; i < file_size - 2; i++) {
if (g_binary_file[i] == '<' && g_binary_file[i+1] == tag_id[0] && g_binary_file[i+2] == tag_id[1])
break;
}
if (i == file_size - 2) {
fprintf(stderr, "Test \"%s\" FAILED - Could not find tag \"<%s\".\n", test_id, tag_id);
failures = 1;
continue;
}
tag_end = i;
if (tag_end - tag_start != byte_count) {
fprintf(stderr, "Test \"%s\" FAILED - There is %d bytes between the tags \"%s\", but the test \"%s\" defines %d bytes.\n", test_id, tag_end - tag_start, tag_id, test_id, byte_count);
failures = 1;
continue;
}
}
else
tag_start = address;
/* compare bytes */
wrong_bytes = 0;
for (i = 0; i < byte_count; i++) {
if (bytes[i] != g_binary_file[tag_start + i]) {
if (wrong_bytes == 0)
fprintf(stderr, "Test \"%s\" FAILED - Bytes that don't match: %d", test_id, (i+1));
else
fprintf(stderr, ", %d", (i+1));
fprintf(stderr, " (GOT: $%.2x EXPECTED: $%.2x)", g_binary_file[tag_start + i], bytes[i]);
wrong_bytes++;
}
}
if (wrong_bytes > 0) {
failures = 1;
fprintf(stderr, "\n");
}
else
fprintf(stderr, "Test \"%s\" SUCCEEDED!\n", test_id);
}
fclose(f);
free(input);
free(g_binary_file);
return failures;
}