disassembler: keep a per-fixup-type list for speedup
[rofl0r-agsutils.git] / Script.c
blob90691c72addb2fdcd2be2d92b680b0af0d5d0791
1 #define _GNU_SOURCE
2 #include "Script.h"
3 #include "Script_internal.h"
4 #include "endianness.h"
5 #include <string.h>
6 #include <stdlib.h>
7 #include <assert.h>
9 #define COMMENT(F, FMT, ...) fprintf(F, "; " FMT, ##__VA_ARGS__)
11 static int dump_sections(AF* a, FILE *f, size_t start, size_t count) {
12 if(count) {
13 AF_set_pos(a, start);
14 fprintf(f, ".sections\n");
15 char buf[300];
16 size_t i = 0;
17 for(; i < count; i++) {
18 if(!AF_read_string(a, buf, sizeof(buf))) return 0;
19 int off = AF_read_int(a);
20 fprintf(f, "\"%s\" = %d\n", buf, off);
22 fprintf(f, "\n");
24 return 1;
27 static int dump_strings(AF* a, FILE *f, size_t start, size_t len) {
28 if(!len) return 1;
29 AF_set_pos(a, start);
30 fprintf(f, ".%s\n\"", "strings");
31 char buf[4096];
32 while(len) {
33 size_t togo = len > sizeof(buf) ? sizeof(buf) : len;
34 if(togo != (size_t) AF_read(a, buf, togo))
35 return 0;
36 size_t i;
37 for(i = 0; i < togo; i++) {
38 len --;
39 if(!buf[i]) {
40 fprintf(f, "\"\n");
41 if(len) fprintf(f, "\"");
42 } else
43 fprintf(f, "%c", buf[i]);
46 return 1;
49 static void free_fixup_data(struct fixup_data *ret) {
50 size_t i;
51 for(i = 0; i <= FIXUP_MAX; i++) {
52 free(ret->codeindex_per[i]);
54 free(ret->codeindex);
55 free(ret->types);
58 /* fixup_data needs to be zeroed */
59 static int get_fixups(AF* a, size_t start, size_t count, struct fixup_data *ret) {
60 size_t i;
61 if(!(ret->types = malloc(count))) goto err;
62 if(!(ret->codeindex = malloc(count * sizeof(unsigned)))) goto err;
64 AF_set_pos(a, start);
66 if(count != (size_t) AF_read(a, ret->types, count)) goto err;
67 for(i = 0; i < count; i++) {
68 assert(ret->types[i]>=0 && ret->types[i]<=FIXUP_MAX);
69 ret->count[ret->types[i]]++;
70 ret->codeindex[i] = AF_read_uint(a);
72 for(i = 0; i <= FIXUP_MAX; i++) {
73 ret->codeindex_per[i] = malloc(ret->count[i] * sizeof(unsigned));
74 if(!ret->codeindex_per[i]) goto err;
75 ret->count[i] = 0; /* reset to 0 to use as index i.t. next loop */
78 for(i = 0; i < count; i++) {
79 ret->codeindex_per[ret->types[i]][ret->count[ret->types[i]]++] = ret->codeindex[i];
82 return 1;
83 err:
84 free_fixup_data(ret);
85 return 0;
88 static int dump_fixups(FILE *f, size_t count, struct fixup_data *fxd) {
89 static const char* typenames[] = {
90 [FIXUP_GLOBALDATA] = "FIXUP_GLOBALDATA",
91 [FIXUP_FUNCTION] = "FIXUP_FUNCTION",
92 [FIXUP_STRING] = "FIXUP_STRING",
93 [FIXUP_IMPORT] = "FIXUP_IMPORT",
94 [FIXUP_DATADATA] = "FIXUP_DATADATA",
95 [FIXUP_STACK] = "FIXUP_STACK",
98 fprintf(f, ".%ss\n", "fixup");
99 size_t i;
100 for(i = 0; i < count; i++) {
101 fprintf(f, "%s: %.12u\n", typenames[(int)fxd->types[i]], fxd->codeindex[i]);
103 return 1;
107 static int dump_import_export(AF* a, FILE *f, size_t start, size_t count, int import) {
108 static const char* secnames[2] = { "export", "import" };
109 const char* secname = secnames[import];
110 size_t i;
111 char buf[256]; /* arbitrarily chosen */
112 if(!count) return 1;
113 AF_set_pos(a, start);
114 fprintf(f, ".%ss\n", secname);
115 for(i = 0; i < count; i++) {
116 if(!AF_read_string(a, buf, sizeof(buf))) return 0;
117 fprintf(f, "%.12zu\"%s\"\n", i, buf);
118 if(!import) {
119 unsigned int addr = AF_read_uint(a);
120 fprintf(f, "%d:%.12u\n", addr >> 24, addr & 0x00FFFFFF);
123 return 1;
126 static struct function_export* get_exports(AF* a, size_t start, size_t count) {
127 if(!count) return 0;
128 struct function_export* fl = malloc(count * sizeof(struct function_export));
129 AF_set_pos(a, start);
130 char buf[4096]; size_t i;
131 for(i = 0; i < count; i++) {
132 if(!AF_read_string(a, buf, sizeof(buf))) return 0;
133 fl[i].fn = strdup(buf);
134 unsigned int addr = AF_read_uint(a);
135 fl[i].type = addr >> 24;
136 fl[i].instr = (addr & 0x00FFFFFF);
138 return fl;
141 static struct importlist get_imports(AF* a, size_t start, size_t count) {
142 struct importlist ret = {0};
143 if(!count) return ret;
144 ret.names = malloc(count * sizeof(char*));
145 if(!ret.names) return ret;
146 AF_set_pos(a, start);
147 char buf[4096]; size_t i;
148 for(i = 0; i < count; i++) {
149 if(!AF_read_string(a, buf, sizeof(buf))) {
150 free(ret.names);
151 return (struct importlist) {0};
153 ret.names[i] = strdup(buf);
155 return ret;
158 struct labels {
159 unsigned count;
160 unsigned *insno;
163 static int sort_comp(const void* xp, const void *yp) {
164 const unsigned *x = xp, *y = yp;
165 if(*x == *y) return 0;
166 else if(*x > *y) return 1;
167 else return -1;
170 #include "ags_cpu.h"
171 static struct labels get_labels(unsigned *code, size_t count) {
172 struct labels ret = {0, 0};
173 if(!count) return ret;
174 size_t capa = 0;
175 unsigned *p = 0;
176 size_t insno = 0;
177 unsigned insn;
178 while(insno < count) {
179 if(ret.count + 1 > capa) {
180 capa = capa ? capa * 2 : 16;
181 if(!(p = realloc(ret.insno, capa * sizeof(unsigned*)))) {
182 if(ret.insno) free(ret.insno);
183 ret.count = 0;
184 ret.insno = 0;
185 return ret;
186 } else ret.insno = p;
188 insn = code[insno] & 0x00ffffff;
189 insno++;
190 int isjmp = 0;
191 assert(insn < SCMD_MAX);
192 switch(insn) {
193 case SCMD_JZ: case SCMD_JMP: case SCMD_JNZ:
194 isjmp = 1;
195 default:
196 break;
198 size_t i = 0;
199 for(; i < opcodes[insn].argcount; i++) {
200 int val = code[insno];
201 insno++;
202 if(isjmp) {
203 if((int) insno + val < 0 || insno + val >= count || code[insno + val] > SCMD_MAX) {
204 dprintf(2, "error: label referenced from jump at %zu is out of bounds\n"
205 "or points to non-instruction start code.\n", insno);
206 assert(0);
208 ret.insno[ret.count] = insno + val;
209 ret.count++;
213 qsort(ret.insno, ret.count, sizeof(unsigned), sort_comp);
214 return ret;
217 static struct strings get_strings(AF* a, size_t start, size_t size) {
218 struct strings ret = {0,0,0};
219 if(!size) return ret;
220 if(!(ret.data = malloc(size))) return ret;
221 AF_set_pos(a, start);
222 if(size != (size_t) AF_read(a, ret.data, size)) {
223 free1:
224 free(ret.data);
225 retrn:
226 return ret;
228 size_t i, strcnt = 0;
229 for(i = 0; i < size; i++) {
230 if(!ret.data[i]) strcnt++;
232 if(!(ret.strings = malloc(strcnt * sizeof(char*)))) goto free1;
233 char* p = ret.data;
234 size_t l = 0;
235 strcnt = 0;
237 for(i = 0; i < size; i++) {
238 if(!ret.data[i]) {
239 ret.strings[strcnt] = p;
240 p += l + 1;
241 l = 0;
242 strcnt++;
243 } else
244 l++;
246 ret.count = strcnt;
247 goto retrn;
250 static char* get_varname(struct function_export* exp, size_t expcount, unsigned globaloffset) {
251 size_t i = 0;
252 for(; i < expcount; i++)
253 if(exp[i].type == EXPORT_DATA && exp[i].instr == globaloffset)
254 return exp[i].fn;
255 return 0;
258 unsigned *get_code(AF *a, size_t start, size_t count) {
259 unsigned *ret = malloc(count * sizeof(unsigned));
260 if(!ret) return 0;
261 AF_set_pos(a, start);
262 size_t i;
263 for(i=0; i<count; i++)
264 ret[i] = AF_read_uint(a);
265 return ret;
268 static enum varsize get_varsize_from_instr(unsigned* code, size_t codecount, size_t index) {
269 assert(index < codecount);
270 switch(code[index]) {
271 case SCMD_MEMREADB: case SCMD_MEMWRITEB:
272 return vs1;
273 case SCMD_MEMREADW: case SCMD_MEMWRITEW:
274 return vs2;
275 case SCMD_MEMWRITEPTR:
276 case SCMD_MEMREADPTR:
277 case SCMD_MEMZEROPTR:
278 case SCMD_MEMINITPTR:
279 case SCMD_MEMREAD: case SCMD_MEMWRITE:
280 return vs4;
281 default:
282 return vs0;
286 struct varinfo find_fixup_for_globaldata(FILE *f, size_t offset, struct fixup_data *fxd, unsigned* code, size_t codecount) {
287 static enum varsize last_size = vs4;
289 size_t i;
290 struct varinfo ret = {0,vs0};
291 for(i = 0; i < fxd->count[FIXUP_GLOBALDATA]; i++) {
292 if(1) {
293 size_t x = fxd->codeindex_per[FIXUP_GLOBALDATA][i];
294 assert(x + 1 < codecount);
295 if(code[x] == offset) {
296 ret.numrefs++;
297 enum varsize oldvarsize = ret.varsize;
298 ret.varsize = get_varsize_from_instr(code, codecount, x+1);
299 if(!ret.varsize) switch(code[x+1]) {
300 case SCMD_REGTOREG:
301 // li mar, @Obj; mr ax, mar; callobj ax
302 if(x+5 < codecount &&
303 code[x+2] == AR_MAR &&
304 code[x+4] == SCMD_CALLOBJ &&
305 code[x+3] == code[x+5]) {
306 ret.varsize = vs4;
308 break;
309 case SCMD_MUL:
310 // li mar, @var; muli dx, 4; ptrget mar; ptrassert; dynamicbounds dx
311 if(x+5 < codecount &&
312 code[x+2] != AR_MAR &&
313 code[x+4] == SCMD_MEMREADPTR &&
314 code[x+5] == AR_MAR)
315 ret.varsize = vs4;
316 // muli is used as an array index like:
317 // muli REG, 4; add MAR, REG; PUSH/POP MAR; ...
318 else if(x+11 < codecount &&
319 code[x+4] == SCMD_ADDREG &&
320 code[x+5] == AR_MAR &&
321 code[x+6] == code[x+2] &&
322 code[x+7] == SCMD_PUSHREG &&
323 code[x+8] == AR_MAR &&
324 code[x+9] == SCMD_POPREG &&
325 code[x+10] == AR_MAR) {
326 ret.varsize = get_varsize_from_instr(code, codecount, x+11);
327 if(!ret.varsize &&
328 x+14 < codecount &&
329 code[x+11] == SCMD_ADDREG &&
330 code[x+12] == AR_MAR &&
331 code[x+13] != code[x+2]
333 /* this variation adds another reg to mar before calling ptrget */
334 ret.varsize = get_varsize_from_instr(code, codecount, x+14);
335 else if(!ret.varsize &&
336 x+13 < codecount &&
337 code[x+11] == SCMD_PUSHREG &&
338 code[x+12] == AR_AX)
339 /* this variation pushes ax on the stack before doing a ptrget ax, which overwrites ax */
340 ret.varsize = get_varsize_from_instr(code, codecount, x+13);
342 else if(x+7 < codecount &&
343 code[x+4] == SCMD_ADDREG &&
344 code[x+5] == AR_MAR &&
345 code[x+6] == code[x+2]) {
346 ret.varsize = get_varsize_from_instr(code, codecount, x+7);
347 if(!ret.varsize &&
348 x + 9 < codecount &&
349 code[x+7] == SCMD_PUSHREG &&
350 code[x+8] == AR_AX)
351 ret.varsize = get_varsize_from_instr(code, codecount, x+9);
354 break;
355 case SCMD_ADDREG:
356 // addreg is typically used on an index register into an array
357 // followed by a byteread/store of the desired size
358 // the index register needs to be added to mar reg
359 assert(x+2 < codecount && code[x+2] == AR_MAR);
360 ret.varsize = get_varsize_from_instr(code, codecount, x+4);
361 // a typical method call
362 if(!ret.varsize && x+8 < codecount &&
363 code[x+4] == SCMD_REGTOREG &&
364 code[x+5] == AR_MAR &&
365 code[x+7] == SCMD_CALLOBJ &&
366 code[x+6] == code[x+8])
367 ret.varsize = vs4;
368 break;
369 case SCMD_PUSHREG:
370 // ptrget and similar ops are typically preceded by push mar, pop mar
371 if(x+4 < codecount &&
372 code[x+2] == AR_MAR &&
373 code[x+3] == SCMD_POPREG &&
374 code[x+4] == AR_MAR) {
375 ret.varsize = get_varsize_from_instr(code, codecount, x+5);
376 if(!ret.varsize && x+7 < codecount &&
377 code[x+5] == SCMD_ADDREG &&
378 code[x+6] == AR_MAR)
379 ret.varsize = get_varsize_from_instr(code, codecount, x+8);
381 break;
383 if(!ret.varsize) {
384 if(oldvarsize) {
385 /* don't bother guessing the varsize if we already determined it */
386 ret.varsize = oldvarsize;
387 } else {
388 dprintf(2, "warning: '%s' globaldata fixup on insno %zu offset %zu\n",
389 opcodes[code[x+1]].mnemonic, x+1, offset);
390 COMMENT(f, "warning: '%s' globaldata fixup on insno %zu offset %zu\n",
391 opcodes[code[x+1]].mnemonic, x+1, offset);
394 if(oldvarsize != 0 && oldvarsize != ret.varsize)
395 assert(0);
399 if(!ret.varsize) {
400 if(ret.numrefs) {
401 COMMENT(f, "warning: couldn't determine varsize, default to 4\n");
402 ret.varsize = vs4;
403 } else {
404 COMMENT(f, "unref'd, assuming array member with last known size\n");
405 ret.varsize = last_size;
408 last_size = ret.varsize;
410 return ret;
413 int has_datadata_fixup(unsigned gdoffset, struct fixup_data *fxd) {
414 size_t i;
415 for(i = 0; i < fxd->count[FIXUP_DATADATA]; i++)
416 if(fxd->codeindex_per[FIXUP_DATADATA][i] == gdoffset)
417 return 1;
418 return 0;
421 static int dump_globaldata(AF *a, FILE *f, size_t start, size_t size,
422 struct function_export* exp, size_t expcount,
423 struct fixup_data *fxd,
424 unsigned *code, size_t codesize) {
425 if(!size) return 1;
426 const char*typenames[vsmax] = {[vs0]="ERR", [vs1]="char", [vs2]="short", [vs4]="int"};
427 fprintf(f, ".%s\n", "data");
428 AF_set_pos(a, start);
429 size_t i = 0, v = 0;
430 for(; i < size; v++) {
431 struct varinfo vi = find_fixup_for_globaldata(f, i, fxd, code, codesize);
432 int x;
434 switch(vi.varsize) {
435 case vs4:
436 x = AF_read_int(a);
437 break;
438 case vs2:
439 x = AF_read_short(a);
440 break;
441 case vs1:
442 x = ByteArray_readByte(a->b);
443 break;
444 case vs0:
445 COMMENT(f, "unreferenced variable, assuming int\n");
446 vi.varsize = vs4;
447 goto sw;
448 case vsmax:
449 assert(0);
451 char* vn = get_varname(exp, expcount, i);
452 if(has_datadata_fixup(i, fxd)) {
453 if(vn) fprintf(f, "export %s %s = .data + %d\n", typenames[vi.varsize], vn, x);
454 else fprintf(f, "%s var%.6zu = .data + %d\n", typenames[vi.varsize], i, x);
455 } else {
456 if(vn) fprintf(f, "export %s %s = %d\n", typenames[vi.varsize], vn, x);
457 else fprintf(f, "%s var%.6zu = %d\n", typenames[vi.varsize], i, x);
459 i += (const unsigned[vsmax]) {[vs0]=0, [vs1]=1, [vs2]=2, [vs4]=4} [vi.varsize];
461 return 1;
464 #include "StringEscape.h"
465 #define DEBUG_OFFSETS 1
466 #define DEBUG_BYTECODE 1
467 static int disassemble_code_and_data(AF* a, ASI* s, FILE *f, int flags, struct fixup_data *fxd) {
468 int debugmode = getenv("AGSDEBUG") != 0;
469 size_t start = s->codestart;
470 size_t len = s->codesize * sizeof(unsigned);
472 unsigned *code = get_code(a, s->codestart, s->codesize);
474 struct function_export* fl = get_exports(a, s->exportstart, s->exportcount);
476 dump_globaldata(a, f, s->globaldatastart, s->globaldatasize, fl, s->exportcount, fxd, code, s->codesize);
478 if(!len) return 1; /* its valid for a scriptfile to have no code at all */
481 struct importlist il = get_imports(a, s->importstart, s->importcount);
483 struct labels lbl = get_labels(code, s->codesize);
485 struct strings str = get_strings(a, s->stringsstart, s->stringssize);
487 AF_set_pos(a, start);
488 fprintf(f, ".%s\n", "text");
490 size_t currInstr = 0, currExp = 0, currFixup = 0, currLbl = 0;
491 /* the data_data fixups appear to be glued separately onto the fixup logic,
492 * they are the only entries not sorted by instrucion number */
493 while(currFixup < s->fixupcount && fxd->types[currFixup] == FIXUP_DATADATA) currFixup++;
494 while(currInstr < s->codesize) {
495 if(flags & DISAS_DEBUG_OFFSETS) COMMENT(f, "offset: %llu\n", (long long) AF_get_pos(a));
496 unsigned regs, args, insn = AF_read_uint(a), op = insn & 0x00ffffff;
497 assert(op < SCMD_MAX);
498 while(currExp < s->exportcount && fl[currExp].type != EXPORT_FUNCTION)
499 currExp++;
500 if(currExp < s->exportcount && fl[currExp].instr == currInstr) {
501 /* new function starts here */
502 fprintf(f, "\n%s:\n", fl[currExp].fn);
503 currExp++;
505 if(currLbl < lbl.count) {
506 if(lbl.insno[currLbl] == currInstr) {
507 size_t numrefs = 0;
508 while(currLbl < lbl.count && lbl.insno[currLbl] == currInstr) {
509 currLbl++; numrefs++;
511 fprintf(f, "label%.12zu: ", currInstr);
512 COMMENT(f, "referenced by %zu spots\n", numrefs);
516 currInstr++;
518 regs = opcodes[op].regcount;
519 args = opcodes[op].argcount;
521 if(insn == SCMD_LINENUM && (flags & DISAS_SKIP_LINENO)) {
522 insn = AF_read_uint(a);
523 COMMENT(f, "line %u\n", insn);
524 currInstr++;
525 continue;
528 if(flags & DISAS_DEBUG_BYTECODE) {
529 unsigned char insbuf[16];
530 unsigned iblen = 0, val;
531 val = end_htole32(insn);
532 memcpy(insbuf+iblen, &val, 4); iblen += 4;
534 off_t currpos = AF_get_pos(a);
536 for(size_t l = 0; l < args; l++) {
537 assert(iblen+4 <= sizeof(insbuf));
538 val = AF_read_uint(a);
539 val = end_htole32(val);
540 memcpy(insbuf+iblen, &val, 4); iblen += 4;
543 char printbuf[sizeof(insbuf)*2 + 1], *pb = printbuf;
544 for(size_t l = 0; l < iblen; l++, pb+=2)
545 sprintf(pb, "%02x", (int) insbuf[l]);
546 COMMENT(f, "%s\n", printbuf);
548 AF_set_pos(a, currpos);
551 if(debugmode)
552 fprintf(f, "%.12zu""\t%s ", currInstr - 1, opcodes[op].mnemonic);
553 else
554 fprintf(f, /*"%.12zu"*/"\t%s ", /*currInstr - 1, */opcodes[op].mnemonic);
556 if(insn == SCMD_REGTOREG) {
557 /* the "mov" instruction differs from all others in that the source comes first
558 we do not want that. */
559 unsigned src, dst;
560 src = AF_read_uint(a);
561 currInstr++;
562 dst = AF_read_uint(a);
563 currInstr++;
564 fprintf(f, "%s, %s\n", regnames[dst], regnames[src]);
565 continue;
567 for (size_t l = 0; l < args; l++) {
568 char escapebuf[4096];
569 if(l) fprintf(f, ", ");
570 insn = AF_read_uint(a);
571 currInstr++;
572 if((!l && regs) || (l == 1 && regs == 2))
573 fprintf(f, "%s", regnames[insn]);
574 else {
575 while(currFixup < s->fixupcount && fxd->types[currFixup] == FIXUP_DATADATA)
576 currFixup++; /* DATADATA fixups are unrelated to the code */
577 if(currFixup < s->fixupcount && fxd->codeindex[currFixup] == currInstr - 1) {
578 switch(fxd->types[currFixup]) {
579 case FIXUP_IMPORT:
580 if(debugmode)
581 fprintf(f, "IMP:%s", il.names[insn]);
582 else
583 fprintf(f, "%s", il.names[insn]);
584 break;
585 case FIXUP_FUNCTION: {
586 size_t x = 0;
587 for(; x < s->exportcount; x++) {
588 if(fl[x].type == EXPORT_FUNCTION && fl[x].instr == insn) {
589 fprintf(f, "%s", fl[x].fn);
590 break;
593 break;
595 case FIXUP_GLOBALDATA: {
596 char *vn = get_varname(fl, s->exportcount, insn);
597 if(vn) fprintf(f, "@%s", vn);
598 else fprintf(f, "@var%.6u", insn);
599 break; }
600 case FIXUP_STACK: /* it is unclear if and where those ever get generated */
601 fprintf(f, ".stack + %d", insn);
602 break;
603 case FIXUP_STRING:
604 escape(str.data + insn, escapebuf, sizeof(escapebuf));
605 fprintf(f, "\"%s\"", escapebuf);
606 default:
607 break;
609 currFixup++;
610 } else {
611 switch(op) {
612 case SCMD_JMP: case SCMD_JZ: case SCMD_JNZ:
613 fprintf(f, "label%.12zu", currInstr + (int) insn);
614 break;
615 default:
616 fprintf(f, "%d", insn);
621 fprintf(f, "\n");
623 free (fl);
624 return 1;
627 int ASI_disassemble(AF* a, ASI* s, char *fn, int flags) {
628 FILE *f;
629 int ret = 1;
630 if((f = fopen(fn, "w")) == 0)
631 return 0;
632 AF_set_pos(a, s->start);
633 struct fixup_data fxd = {0};
634 if(!get_fixups(a, s->fixupstart, s->fixupcount, &fxd)) return 0;
636 //if(!dump_globaldata(a, fd, s->globaldatastart, s->globaldatasize)) goto err_close;
637 if(!disassemble_code_and_data(a, s, f, flags, &fxd)) goto err_close;
638 if(!dump_strings(a, f, s->stringsstart, s->stringssize)) goto err_close;
639 if(!dump_fixups(f, s->fixupcount, &fxd)) goto err_close;
640 if(!dump_import_export(a, f, s->importstart, s->importcount, 1)) goto err_close;
641 if(!dump_import_export(a, f, s->exportstart, s->exportcount, 0)) goto err_close;
642 if(!dump_sections(a, f, s->sectionstart, s->sectioncount)) goto err_close;
643 ret:
644 free_fixup_data(&fxd);
645 fclose(f);
646 return ret;
647 err_close:
648 ret = 0;
649 goto ret;
652 int ASI_read_script(AF *a, ASI* s) {
653 s->start = AF_get_pos(a);
654 char sig[4];
655 size_t l = 4;
656 if(l != (size_t) AF_read(a, sig, l)) return 0;
657 assert(memcmp("SCOM", sig, 4) == 0);
658 s->version = AF_read_int(a);
659 s->globaldatasize = AF_read_int(a);
660 s->codesize = AF_read_int(a);
661 s->stringssize = AF_read_int(a);
662 if(s->globaldatasize) {
663 s->globaldatastart = AF_get_pos(a);
664 l = s->globaldatasize;
665 if(!AF_read_junk(a, l)) return 0;
666 } else s->globaldatastart = 0;
667 if(s->codesize) {
668 s->codestart = AF_get_pos(a);
669 l = s->codesize * sizeof(int);
670 if(!AF_read_junk(a, l)) return 0;
671 } else s->codestart = 0;
672 if(s->stringssize) {
673 s->stringsstart = AF_get_pos(a);
674 l = s->stringssize;
675 if(!AF_read_junk(a, l)) return 0;
676 } else s->stringsstart = 0;
677 s->fixupcount = AF_read_int(a);
678 if(s->fixupcount) {
679 s->fixupstart = AF_get_pos(a);
680 l = s->fixupcount;
681 if(!AF_read_junk(a, l)) return 0; /* fixup types */
682 l *= sizeof(int);
683 if(!AF_read_junk(a, l)) return 0; /* fixups */
684 } else s->fixupstart = 0;
685 s->importcount = AF_read_int(a);
686 if(s->importcount) {
687 s->importstart = AF_get_pos(a);
688 char buf[300];
689 size_t i = 0;
690 for(; i < s->importcount; i++)
691 if(!AF_read_string(a, buf, sizeof(buf))) return 0;
692 } else s->importstart = 0;
693 s->exportcount = AF_read_int(a);
694 if(s->exportcount) {
695 s->exportstart = AF_get_pos(a);
696 char buf[300];
697 size_t i = 0;
698 for(; i < s->exportcount; i++) {
699 if(!AF_read_string(a, buf, sizeof(buf))) return 0;
700 AF_read_int(a); /* export_addr */
702 } else s->exportstart = 0;
703 s->sectionstart = 0;
704 s->sectioncount = 0;
705 if (s->version >= 83) {
706 s->sectioncount = AF_read_int(a);
707 if(s->sectioncount) {
708 s->sectionstart = AF_get_pos(a);
709 char buf[300];
710 size_t i = 0;
711 for(; i < s->sectioncount; i++) {
712 if(!AF_read_string(a, buf, sizeof(buf))) return 0;
713 AF_read_int(a); /* section offset */
717 if(0xbeefcafe != AF_read_uint(a)) return 0;
718 s->len = AF_get_pos(a) - s->start;
719 return 1;