diff options
Diffstat (limited to 'engines/sci/tools/scidisasm.cpp')
-rw-r--r-- | engines/sci/tools/scidisasm.cpp | 682 |
1 files changed, 333 insertions, 349 deletions
diff --git a/engines/sci/tools/scidisasm.cpp b/engines/sci/tools/scidisasm.cpp index dc5e9f8d07..1a3a0991e8 100644 --- a/engines/sci/tools/scidisasm.cpp +++ b/engines/sci/tools/scidisasm.cpp @@ -58,7 +58,8 @@ static struct option options[] = { {"opcode-size", no_argument, &opcode_size, 1}, {"verbose", no_argument, &verbose, 1}, {"gamedir", required_argument, 0, 'd'}, - {0, 0, 0, 0}}; + {0, 0, 0, 0} +}; #endif /* HAVE_GETOPT_LONG */ #define SCI_ASSUME_VERSION SCI_VERSION_FTU_NEW_SCRIPT_HEADER @@ -109,34 +110,33 @@ void disassemble_script(disasm_state_t *d, int res_no, int pass_no); script_state_t * -find_script_state (disasm_state_t *d, int script_no); +find_script_state(disasm_state_t *d, int script_no); void -script_free_names (script_state_t *s); +script_free_names(script_state_t *s); void -script_add_name (script_state_t *s, int aoffset, char *aname, int aclass_no); +script_add_name(script_state_t *s, int aoffset, char *aname, int aclass_no); char * -script_find_name (script_state_t *s, int offset, int *class_no); +script_find_name(script_state_t *s, int offset, int *class_no); void -script_add_area (script_state_t *s, int start_offset, int end_offset, int type, void *data); +script_add_area(script_state_t *s, int start_offset, int end_offset, int type, void *data); void -script_free_areas (script_state_t *s); +script_free_areas(script_state_t *s); int -script_get_area_type (script_state_t *s, int offset, void **pdata); +script_get_area_type(script_state_t *s, int offset, void **pdata); void -disasm_init (disasm_state_t *d); +disasm_init(disasm_state_t *d); void -disasm_free_state (disasm_state_t *d); +disasm_free_state(disasm_state_t *d); -int main(int argc, char** argv) -{ +int main(int argc, char** argv) { int i; char outfilename [256]; int optindex = 0; @@ -151,82 +151,79 @@ int main(int argc, char** argv) while ((c = getopt(argc, argv, "vhxr:d:")) > -1) { #endif /* !HAVE_GETOPT_H */ - switch (c) - { - case 256: - printf("scidisasm ("PACKAGE") "VERSION"\n"); - printf("This program is copyright (C) 1999 Christoph Reichenbach.\n" - "It comes WITHOUT WARRANTY of any kind.\n" - "This is free software, released under the GNU General Public License.\n"); - exit(0); - - case 'h': - printf("Usage: scidisasm\n" - "\nAvailable options:\n" - " --version Prints the version number\n" - " --help -h Displays this help message\n" - " --gamedir <dir> -d<dir> Read game resources from dir\n" - " --hexdump -x Hex dump all script resources\n" - " --verbose Print additional disassembly information\n" - " --opcode-size Print opcode size postfixes\n"); - exit(0); - - case 'd': - if (gamedir) sci_free (gamedir); - gamedir= sci_strdup (optarg); - break; + switch (c) { + case 256: + printf("scidisasm ("PACKAGE") "VERSION"\n"); + printf("This program is copyright (C) 1999 Christoph Reichenbach.\n" + "It comes WITHOUT WARRANTY of any kind.\n" + "This is free software, released under the GNU General Public License.\n"); + exit(0); + + case 'h': + printf("Usage: scidisasm\n" + "\nAvailable options:\n" + " --version Prints the version number\n" + " --help -h Displays this help message\n" + " --gamedir <dir> -d<dir> Read game resources from dir\n" + " --hexdump -x Hex dump all script resources\n" + " --verbose Print additional disassembly information\n" + " --opcode-size Print opcode size postfixes\n"); + exit(0); + + case 'd': + if (gamedir) sci_free(gamedir); + gamedir = sci_strdup(optarg); + break; - case 'r': - res_version = atoi(optarg); - break; - - case 0: /* getopt_long already did this for us */ - case '?': - /* getopt_long already printed an error message. */ - break; + case 'r': + res_version = atoi(optarg); + break; - default: - return -1; - } + case 0: /* getopt_long already did this for us */ + case '?': + /* getopt_long already printed an error message. */ + break; + + default: + return -1; + } } if (gamedir) - if (chdir (gamedir)) - { - printf ("Error changing to game directory '%s'\n", gamedir); - exit(1); - } + if (chdir(gamedir)) { + printf("Error changing to game directory '%s'\n", gamedir); + exit(1); + } - printf ("Loading resources...\n"); + printf("Loading resources...\n"); if (!(resmgr = scir_new_resource_manager(sci_getcwd(), res_version, - 1, 1024*128))) { - fprintf(stderr,"Could not find any resources; quitting.\n"); + 1, 1024 * 128))) { + fprintf(stderr, "Could not find any resources; quitting.\n"); exit(1); } - disasm_init (&disasm_state); + disasm_init(&disasm_state); script_adjust_opcode_formats(resmgr->sci_version); - - printf ("Performing first pass...\n"); - for (i=0; i < resmgr->resources_nr; i++) - if (resmgr->resources[i].type == sci_script) - disassemble_script(&disasm_state, - resmgr->resources[i].number, 1); - printf ("Performing second pass...\n"); - for (i=0; i < resmgr->resources_nr; i++) + printf("Performing first pass...\n"); + for (i = 0; i < resmgr->resources_nr; i++) if (resmgr->resources[i].type == sci_script) - { - sprintf (outfilename, "%03d.script", - resmgr->resources[i].number); - open_console_file (outfilename); - disassemble_script(&disasm_state, - resmgr->resources[i].number, 2); - } + disassemble_script(&disasm_state, + resmgr->resources[i].number, 1); + + printf("Performing second pass...\n"); + for (i = 0; i < resmgr->resources_nr; i++) + if (resmgr->resources[i].type == sci_script) { + sprintf(outfilename, "%03d.script", + resmgr->resources[i].number); + open_console_file(outfilename); + disassemble_script(&disasm_state, + resmgr->resources[i].number, 2); + } close_console_file(); - disasm_free_state (&disasm_state); + disasm_free_state(&disasm_state); free(resmgr->resource_path); scir_free_resource_manager(resmgr); @@ -236,108 +233,102 @@ int main(int argc, char** argv) /* -- General operations on disasm_state_t ------------------------------- */ void -disasm_init (disasm_state_t *d) -{ - d->snames = vocabulary_get_snames (resmgr, &d->selector_count, SCI_ASSUME_VERSION); +disasm_init(disasm_state_t *d) { + d->snames = vocabulary_get_snames(resmgr, &d->selector_count, SCI_ASSUME_VERSION); d->opcodes = vocabulary_get_opcodes(resmgr); - d->kernel_names = vocabulary_get_knames (resmgr, &d->kernel_names_nr); - d->words = vocab_get_words (resmgr, &d->word_count); + d->kernel_names = vocabulary_get_knames(resmgr, &d->kernel_names_nr); + d->words = vocab_get_words(resmgr, &d->word_count); d->scripts = NULL; d->old_header = 0; d->class_count = vocabulary_get_class_count(resmgr); - d->class_names = (char **) sci_malloc (d->class_count * sizeof (char *)); - memset (d->class_names, 0, d->class_count * sizeof (char *)); - d->class_selector_count = (int *) sci_malloc (d->class_count * sizeof (int)); - memset (d->class_selector_count, 0, d->class_count * sizeof (int)); - d->class_selectors = (short **) sci_malloc (d->class_count * sizeof (short *)); - memset (d->class_selectors, 0, d->class_count * sizeof (short *)); + d->class_names = (char **) sci_malloc(d->class_count * sizeof(char *)); + memset(d->class_names, 0, d->class_count * sizeof(char *)); + d->class_selector_count = (int *) sci_malloc(d->class_count * sizeof(int)); + memset(d->class_selector_count, 0, d->class_count * sizeof(int)); + d->class_selectors = (short **) sci_malloc(d->class_count * sizeof(short *)); + memset(d->class_selectors, 0, d->class_count * sizeof(short *)); } void -disasm_free_state (disasm_state_t *d) -{ +disasm_free_state(disasm_state_t *d) { script_state_t *s, *next_script; int i; - s=d->scripts; + s = d->scripts; while (s) { - next_script=s->next; - script_free_names (s); - script_free_areas (s); - s=next_script; + next_script = s->next; + script_free_names(s); + script_free_areas(s); + s = next_script; } - for (i=0; i<d->class_count; i++) { - if (d->class_names [i]) sci_free (d->class_names [i]); - if (d->class_selectors [i]) sci_free (d->class_selectors [i]); + for (i = 0; i < d->class_count; i++) { + if (d->class_names [i]) sci_free(d->class_names [i]); + if (d->class_selectors [i]) sci_free(d->class_selectors [i]); } - free (d->class_names); - free (d->class_selectors); - free (d->class_selector_count); + free(d->class_names); + free(d->class_selectors); + free(d->class_selector_count); - vocabulary_free_snames (d->snames); - vocabulary_free_opcodes (d->opcodes); - vocabulary_free_knames (d->kernel_names); - vocab_free_words (d->words, d->word_count); + vocabulary_free_snames(d->snames); + vocabulary_free_opcodes(d->opcodes); + vocabulary_free_knames(d->kernel_names); + vocab_free_words(d->words, d->word_count); } script_state_t * -find_script_state (disasm_state_t *d, int script_no) -{ +find_script_state(disasm_state_t *d, int script_no) { script_state_t *s; - for (s=d->scripts; s; s=s->next) + for (s = d->scripts; s; s = s->next) if (s->script_no == script_no) return s; - s=(script_state_t *) sci_malloc (sizeof (script_state_t)); - memset (s, 0, sizeof (script_state_t)); + s = (script_state_t *) sci_malloc(sizeof(script_state_t)); + memset(s, 0, sizeof(script_state_t)); s->script_no = script_no; s->next = d->scripts; - d->scripts=s; + d->scripts = s; return s; } /* -- Name table operations ---------------------------------------------- */ void -script_free_names (script_state_t *s) -{ - name_t *p=s->names, *next_name; +script_free_names(script_state_t *s) { + name_t *p = s->names, *next_name; while (p) { - next_name=p->next; - free (p->name); - free (p); - p=next_name; + next_name = p->next; + free(p->name); + free(p); + p = next_name; } s->names = NULL; } void -script_add_name (script_state_t *s, int aoffset, char *aname, int aclass_no) -{ +script_add_name(script_state_t *s, int aoffset, char *aname, int aclass_no) { name_t *p; - char *name=script_find_name (s, aoffset, NULL); + char *name = script_find_name(s, aoffset, NULL); if (name) return; - p=(name_t *) sci_malloc (sizeof (name_t)); - p->offset=aoffset; - p->name= sci_strdup (aname); - p->class_no=aclass_no; - p->next=s->names; - s->names=p; + p = (name_t *) sci_malloc(sizeof(name_t)); + p->offset = aoffset; + p->name = sci_strdup(aname); + p->class_no = aclass_no; + p->next = s->names; + s->names = p; } char * -script_find_name (script_state_t *s, int offset, int *aclass_no) -{ +script_find_name(script_state_t *s, int offset, int *aclass_no) { name_t *p; - for (p=s->names; p; p=p->next) + for (p = s->names; p; p = p->next) if (p->offset == offset) { if (aclass_no && p->class_no != -2) *aclass_no = p->class_no; return p->name; @@ -349,11 +340,10 @@ script_find_name (script_state_t *s, int offset, int *aclass_no) /* -- Area table operations ---------------------------------------------- */ void -script_add_area (script_state_t *s, int start_offset, int end_offset, int type, void *data) -{ +script_add_area(script_state_t *s, int start_offset, int end_offset, int type, void *data) { area_t *area; - area=(area_t *) sci_malloc (sizeof (area_t)); + area = (area_t *) sci_malloc(sizeof(area_t)); area->start_offset = start_offset; area->end_offset = end_offset; area->data = data; @@ -363,33 +353,31 @@ script_add_area (script_state_t *s, int start_offset, int end_offset, int type, } void -script_free_areas (script_state_t *s) -{ +script_free_areas(script_state_t *s) { int i; - for (i=0; i<area_last; i++) { - area_t *area=s->areas [i], *next_area; + for (i = 0; i < area_last; i++) { + area_t *area = s->areas [i], *next_area; while (area) { - next_area=area->next; - free (area); - area=next_area; + next_area = area->next; + free(area); + area = next_area; } } } int -script_get_area_type (script_state_t *s, int offset, void **pdata) -{ +script_get_area_type(script_state_t *s, int offset, void **pdata) { int i; - for (i=0; i<area_last; i++) { - area_t *area=s->areas [i]; + for (i = 0; i < area_last; i++) { + area_t *area = s->areas [i]; while (area) { if (area->start_offset <= offset && area->end_offset >= offset) { - if (pdata != NULL) *pdata=area->data; + if (pdata != NULL) *pdata = area->data; return i; } - area=area->next; + area = area->next; } } @@ -397,21 +385,19 @@ script_get_area_type (script_state_t *s, int offset, void **pdata) } char * -get_selector_name (disasm_state_t *d, int selector) -{ +get_selector_name(disasm_state_t *d, int selector) { static char selector_name [256]; if (d->snames && selector >= 0 && selector < d->selector_count) return d->snames [selector]; else { - sprintf (selector_name, "unknown_sel_%X", selector); + sprintf(selector_name, "unknown_sel_%X", selector); return selector_name; } } const char * -get_class_name (disasm_state_t *d, int class_no) -{ +get_class_name(disasm_state_t *d, int class_no) { static char class_name [256]; if (class_no == -1) @@ -419,7 +405,7 @@ get_class_name (disasm_state_t *d, int class_no) else if (class_no >= 0 && class_no < d->class_count && d->class_names [class_no]) return d->class_names [class_no]; else { - sprintf (class_name, "class_%d", class_no); + sprintf(class_name, "class_%d", class_no); return class_name; } } @@ -428,8 +414,7 @@ get_class_name (disasm_state_t *d, int class_no) static void script_dump_object(disasm_state_t *d, script_state_t *s, - unsigned char *data, int seeker, int objsize, int pass_no) -{ + unsigned char *data, int seeker, int objsize, int pass_no) { int selectors, overloads, selectorsize; int species = getInt16(data + 8 + seeker); int superclass = getInt16(data + 10 + seeker); @@ -441,16 +426,16 @@ script_dump_object(disasm_state_t *d, script_state_t *s, short *sels; selectors = (selectorsize = getInt16(data + seeker + 6)); - name=namepos? ((const char *)data + namepos) : "<unknown>"; + name = namepos ? ((const char *)data + namepos) : "<unknown>"; if (pass_no == 1) - script_add_area (s, seeker, seeker+objsize-1, area_object, strdup(name)); + script_add_area(s, seeker, seeker + objsize - 1, area_object, strdup(name)); if (pass_no == 2) { sciprintf(".object\n"); sciprintf("Name: %s\n", name); - sciprintf("Superclass: %s [%x]\n", get_class_name (d, superclass), superclass); - sciprintf("Species: %s [%x]\n", get_class_name (d, species), species); + sciprintf("Superclass: %s [%x]\n", get_class_name(d, superclass), superclass); + sciprintf("Species: %s [%x]\n", get_class_name(d, species), species); sciprintf("-info-:%x\n", getInt16(data + 12 + seeker) & 0xffff); @@ -461,18 +446,17 @@ script_dump_object(disasm_state_t *d, script_state_t *s, seeker += 8; if (species < d->class_count) - sels=d->class_selectors [species]; + sels = d->class_selectors [species]; else - sels=NULL; + sels = NULL; while (selectors--) { if (pass_no == 2) { - sel=getInt16(data + seeker) & 0xffff; + sel = getInt16(data + seeker) & 0xffff; if (sels && (sels [i] >= 0) && (sels[i] < d->selector_count)) { sciprintf(" [#%03x] %s = 0x%x\n", i, d->snames [sels [i]], sel); i++; - } - else + } else sciprintf(" [#%03x] <unknown> = 0x%x\n", i++, sel); } @@ -491,11 +475,10 @@ script_dump_object(disasm_state_t *d, script_state_t *s, if (d->old_header) selector >>= 1; if (pass_no == 1) { - sprintf (buf, "%s::%s", name, get_selector_name (d, selector)); - script_add_name (s, getInt16(data + seeker + selectors*2 + 2), buf, species); - } - else { - sciprintf(" [%03x] %s: @", selector, get_selector_name (d, selector)); + sprintf(buf, "%s::%s", name, get_selector_name(d, selector)); + script_add_name(s, getInt16(data + seeker + selectors*2 + 2), buf, species); + } else { + sciprintf(" [%03x] %s: @", selector, get_selector_name(d, selector)); sciprintf("%04x\n", getInt16(data + seeker + selectors*2 + 2)); } @@ -505,8 +488,7 @@ script_dump_object(disasm_state_t *d, script_state_t *s, static void script_dump_class(disasm_state_t *d, script_state_t *s, - unsigned char *data, int seeker, int objsize, int pass_no) -{ + unsigned char *data, int seeker, int objsize, int pass_no) { word selectors, overloads, selectorsize; int species = getInt16(data + 8 + seeker); int superclass = getInt16(data + 10 + seeker); @@ -515,27 +497,26 @@ script_dump_class(disasm_state_t *d, script_state_t *s, char buf [256]; int i; - name=namepos? ((const char *)data + namepos) : "<unknown>"; + name = namepos ? ((const char *)data + namepos) : "<unknown>"; selectors = (selectorsize = getInt16(data + seeker + 6)); if (pass_no == 1) { if (species >= 0 && species < d->class_count) { if (!namepos) { - sprintf (buf, "class_%d", species); - d->class_names [species] = sci_strdup (buf); - } - else - d->class_names [species] = sci_strdup (name); + sprintf(buf, "class_%d", species); + d->class_names [species] = sci_strdup(buf); + } else + d->class_names [species] = sci_strdup(name); d->class_selector_count [species] = selectors; - d->class_selectors [species] = (short *) sci_malloc (sizeof (short) * selectors); + d->class_selectors [species] = (short *) sci_malloc(sizeof(short) * selectors); } } if (pass_no == 2) { - sciprintf (".class\n"); + sciprintf(".class\n"); sciprintf("Name: %s\n", name); - sciprintf("Superclass: %s [%x]\n", get_class_name (d, superclass), superclass); + sciprintf("Superclass: %s [%x]\n", get_class_name(d, superclass), superclass); sciprintf("Species: %x\n", species); sciprintf("-info-:%x\n", getInt16(data + 12 + seeker) & 0xffff); @@ -546,17 +527,16 @@ script_dump_class(disasm_state_t *d, script_state_t *s, seeker += 8; selectorsize <<= 1; - for (i=0; i<selectors; i++) { + for (i = 0; i < selectors; i++) { word selector = 0xffff & getInt16(data + (seeker) + selectorsize); if (d->old_header) selector >>= 1; if (pass_no == 1) { if (species >= 0 && species < d->class_count) d->class_selectors [species][i] = selector; - } - else - sciprintf(" [%03x] %s = 0x%x\n", selector, get_selector_name (d, selector), - getInt16(data + seeker) & 0xffff); + } else + sciprintf(" [%03x] %s = 0x%x\n", selector, get_selector_name(d, selector), + getInt16(data + seeker) & 0xffff); seeker += 2; } @@ -574,11 +554,10 @@ script_dump_class(disasm_state_t *d, script_state_t *s, if (d->old_header) selector >>= 1; if (pass_no == 1) { - sprintf (buf, "%s::%s", name, get_selector_name (d, selector)); - script_add_name (s, getInt16(data + seeker + selectors*2 + 2) & 0xffff, buf, species); - } - else { - sciprintf(" [%03x] %s: @", selector & 0xffff, get_selector_name (d, selector)); + sprintf(buf, "%s::%s", name, get_selector_name(d, selector)); + script_add_name(s, getInt16(data + seeker + selectors*2 + 2) & 0xffff, buf, species); + } else { + sciprintf(" [%03x] %s: @", selector & 0xffff, get_selector_name(d, selector)); sciprintf("%04x\n", getInt16(data + seeker + selectors*2 + 2) & 0xffff); } @@ -587,113 +566,126 @@ script_dump_class(disasm_state_t *d, script_state_t *s, } static int -script_dump_said_string(disasm_state_t *d, unsigned char *data, int seeker) -{ +script_dump_said_string(disasm_state_t *d, unsigned char *data, int seeker) { while (1) { - unsigned short nextitem=(unsigned char) data [seeker++]; + unsigned short nextitem = (unsigned char) data [seeker++]; if (nextitem == 0xFF) return seeker; if (nextitem >= 0xF0) { switch (nextitem) { - case 0xf0: sciprintf(", "); break; - case 0xf1: sciprintf("& "); break; - case 0xf2: sciprintf("/ "); break; - case 0xf3: sciprintf("( "); break; - case 0xf4: sciprintf(") "); break; - case 0xf5: sciprintf("[ "); break; - case 0xf6: sciprintf("] "); break; - case 0xf7: sciprintf("# "); break; - case 0xf8: sciprintf("< "); break; - case 0xf9: sciprintf("> "); break; + case 0xf0: + sciprintf(", "); + break; + case 0xf1: + sciprintf("& "); + break; + case 0xf2: + sciprintf("/ "); + break; + case 0xf3: + sciprintf("( "); + break; + case 0xf4: + sciprintf(") "); + break; + case 0xf5: + sciprintf("[ "); + break; + case 0xf6: + sciprintf("] "); + break; + case 0xf7: + sciprintf("# "); + break; + case 0xf8: + sciprintf("< "); + break; + case 0xf9: + sciprintf("> "); + break; } - } - else { + } else { nextitem = nextitem << 8 | (unsigned char) data [seeker++]; - sciprintf ("%s ", vocab_get_any_group_word (nextitem, d->words, d->word_count)); + sciprintf("%s ", vocab_get_any_group_word(nextitem, d->words, d->word_count)); if (verbose) - sciprintf ("[%03x] ", nextitem); + sciprintf("[%03x] ", nextitem); } } } static void script_dump_said(disasm_state_t *d, script_state_t *s, - unsigned char *data, int seeker, int objsize, int pass_no) -{ - int _seeker=seeker+objsize-4; + unsigned char *data, int seeker, int objsize, int pass_no) { + int _seeker = seeker + objsize - 4; if (pass_no == 1) { - script_add_area (s, seeker, seeker+objsize-1, area_said, NULL); + script_add_area(s, seeker, seeker + objsize - 1, area_said, NULL); return; } - sciprintf (".said\n"); + sciprintf(".said\n"); - while (seeker < _seeker-1) { - sciprintf ("%04x: ", seeker); - seeker=script_dump_said_string (d, data, seeker); - sciprintf ("\n"); + while (seeker < _seeker - 1) { + sciprintf("%04x: ", seeker); + seeker = script_dump_said_string(d, data, seeker); + sciprintf("\n"); } } static void script_dump_synonyms(disasm_state_t *d, script_state_t *s, - unsigned char *data, int seeker, int objsize, int pass_no) -{ - int _seeker=seeker+objsize-4; + unsigned char *data, int seeker, int objsize, int pass_no) { + int _seeker = seeker + objsize - 4; - sciprintf ("Synonyms:\n"); + sciprintf("Synonyms:\n"); while (seeker < _seeker) { - int search=getInt16(data+seeker); - int replace=getInt16(data+seeker+2); - seeker+=4; - if (search<0) break; - sciprintf ("%s[%03x] ==> %s[%03x]\n", - vocab_get_any_group_word (search, d->words, d->word_count), search, - vocab_get_any_group_word (replace, d->words, d->word_count), replace); + int search = getInt16(data + seeker); + int replace = getInt16(data + seeker + 2); + seeker += 4; + if (search < 0) break; + sciprintf("%s[%03x] ==> %s[%03x]\n", + vocab_get_any_group_word(search, d->words, d->word_count), search, + vocab_get_any_group_word(replace, d->words, d->word_count), replace); } } static void script_dump_strings(disasm_state_t *d, script_state_t *s, - unsigned char *data, int seeker, int objsize, int pass_no) -{ - int endptr=seeker+objsize-4; + unsigned char *data, int seeker, int objsize, int pass_no) { + int endptr = seeker + objsize - 4; if (pass_no == 1) { - script_add_area (s, seeker, seeker+objsize-1, area_string, NULL); + script_add_area(s, seeker, seeker + objsize - 1, area_string, NULL); return; } sciprintf(".strings\n"); while (data [seeker] && seeker < endptr) { - sciprintf ("%04x: %s\n", seeker, data+seeker); - seeker += strlen ((char *) data+seeker)+1; + sciprintf("%04x: %s\n", seeker, data + seeker); + seeker += strlen((char *) data + seeker) + 1; } } static void script_dump_exports(disasm_state_t *d, script_state_t *s, - unsigned char *data, int seeker, int objsize, int pass_no) -{ - byte *pexport=(byte *) (data+seeker); - word export_count=getUInt16(pexport); + unsigned char *data, int seeker, int objsize, int pass_no) { + byte *pexport = (byte *)(data + seeker); + word export_count = getUInt16(pexport); int i; char buf [256]; pexport += 2; - if (pass_no == 2) sciprintf (".exports\n"); + if (pass_no == 2) sciprintf(".exports\n"); - for (i=0; i<export_count; i++) { + for (i = 0; i < export_count; i++) { if (pass_no == 1) { - guint16 offset=getUInt16(pexport); - sprintf (buf, "exp_%02X", i); - script_add_name (s, offset, buf, -1); - } - else - sciprintf ("%02X: %04X\n", i, *pexport); - pexport+=2; + guint16 offset = getUInt16(pexport); + sprintf(buf, "exp_%02X", i); + script_add_name(s, offset, buf, -1); + } else + sciprintf("%02X: %04X\n", i, *pexport); + pexport += 2; } } @@ -701,19 +693,18 @@ script_dump_exports(disasm_state_t *d, script_state_t *s, static void script_disassemble_code(disasm_state_t *d, script_state_t *s, - unsigned char *data, int seeker, int objsize, int pass_no) -{ - int endptr=seeker+objsize-4; - int i=0; - int cur_class=-1; + unsigned char *data, int seeker, int objsize, int pass_no) { + int endptr = seeker + objsize - 4; + int i = 0; + int cur_class = -1; word dest; void *area_data; char buf [256]; char *dest_name; - if (pass_no == 2) sciprintf (".code\n"); + if (pass_no == 2) sciprintf(".code\n"); - while (seeker < endptr-1) { + while (seeker < endptr - 1) { unsigned char opsize = data [seeker]; unsigned char opcode = opsize >> 1; word param_value; @@ -722,18 +713,18 @@ script_disassemble_code(disasm_state_t *d, script_state_t *s, opsize &= 1; /* byte if true, word if false */ if (pass_no == 2) { - name=script_find_name (s, seeker, &cur_class); - if (name) sciprintf (" %s:\n", name); + name = script_find_name(s, seeker, &cur_class); + if (name) sciprintf(" %s:\n", name); sciprintf("%04X: ", seeker); sciprintf("%s", d->opcodes[opcode].name); if (opcode_size && formats[opcode][0]) - sciprintf (".%c", opsize? 'b' : 'w'); - sciprintf ("\t"); + sciprintf(".%c", opsize ? 'b' : 'w'); + sciprintf("\t"); } seeker++; - for (i=0; formats[opcode][i]; i++) + for (i = 0; formats[opcode][i]; i++) switch (formats[opcode][i]) { @@ -772,125 +763,117 @@ script_disassemble_code(disasm_state_t *d, script_state_t *s, if (pass_no == 1) { if (opcode == op_jmp || opcode == op_bt || opcode == op_bnt) { - dest=seeker+(short) param_value; - sprintf (buf, "lbl_%04X", dest); - script_add_name (s, dest, buf, -2); + dest = seeker + (short) param_value; + sprintf(buf, "lbl_%04X", dest); + script_add_name(s, dest, buf, -2); } - } - else if (pass_no == 2) + } else if (pass_no == 2) switch (formats[opcode][i]) { case Script_SVariable: case Script_Variable: if (opcode == op_callk) { sciprintf(" #%s", (param_value < d->kernel_names_nr) - ? d->kernel_names[param_value] : "<invalid>"); - if (verbose) sciprintf ("[%x]", param_value); - } - else if (opcode == op_class || (opcode == op_super && i==0)) { - sciprintf (" %s", (d->class_names && param_value < d->class_count) - ? d->class_names[param_value] : "<invalid>"); - if (verbose) sciprintf ("[%x]", param_value); - } - else sciprintf(opsize? " %02x" : " %04x", param_value); + ? d->kernel_names[param_value] : "<invalid>"); + if (verbose) sciprintf("[%x]", param_value); + } else if (opcode == op_class || (opcode == op_super && i == 0)) { + sciprintf(" %s", (d->class_names && param_value < d->class_count) + ? d->class_names[param_value] : "<invalid>"); + if (verbose) sciprintf("[%x]", param_value); + } else sciprintf(opsize ? " %02x" : " %04x", param_value); if (opcode == op_pushi && param_value > 0 && param_value < d->selector_count) - sciprintf ("\t\t; selector <%s>", d->snames [param_value]); + sciprintf("\t\t; selector <%s>", d->snames [param_value]); break; case Script_Global: - sciprintf (" global_%d", param_value); + sciprintf(" global_%d", param_value); break; case Script_Local: - sciprintf (" local_%d", param_value); + sciprintf(" local_%d", param_value); break; case Script_Temp: - sciprintf (" temp_%d", param_value); + sciprintf(" temp_%d", param_value); break; case Script_Param: - sciprintf (" param_%d", param_value); + sciprintf(" param_%d", param_value); break; case Script_Offset: - dest=(short) param_value; - dest_name=script_find_name (s, dest, NULL); + dest = (short) param_value; + dest_name = script_find_name(s, dest, NULL); if (dest_name) - sciprintf (" %s", dest_name); + sciprintf(" %s", dest_name); else - sciprintf (" %04x", dest); + sciprintf(" %04x", dest); if (verbose) - sciprintf (opsize? " [%02x] " : " [%04x] ", param_value); + sciprintf(opsize ? " [%02x] " : " [%04x] ", param_value); - if (opcode == op_lofsa || opcode == op_lofss){ - int atype=script_get_area_type (s, dest, &area_data); + if (opcode == op_lofsa || opcode == op_lofss) { + int atype = script_get_area_type(s, dest, &area_data); if (atype == area_string) { - strncpy (buf, (char *) &data [dest], sizeof (buf)-1); - buf [sizeof (buf)-1] = 0; - if (strlen (buf) > 40){ + strncpy(buf, (char *) &data [dest], sizeof(buf) - 1); + buf [sizeof(buf)-1] = 0; + if (strlen(buf) > 40) { buf [40] = 0; - strcat (buf, "..."); + strcat(buf, "..."); } - sciprintf ("\t\t; \"%s\"", buf); - } - else if (atype == area_said) { - sciprintf ("\t\t; said \""); - script_dump_said_string (d, data, dest); - sciprintf ("\"\n"); - } - else if (atype == area_object) - sciprintf ("\t\t; object <%s>", area_data); + sciprintf("\t\t; \"%s\"", buf); + } else if (atype == area_said) { + sciprintf("\t\t; said \""); + script_dump_said_string(d, data, dest); + sciprintf("\"\n"); + } else if (atype == area_object) + sciprintf("\t\t; object <%s>", area_data); } break; case Script_SRelative: - dest=seeker+(short) param_value; - dest_name=script_find_name (s, dest, NULL); + dest = seeker + (short) param_value; + dest_name = script_find_name(s, dest, NULL); if (dest_name) - sciprintf (" %s", dest_name); + sciprintf(" %s", dest_name); else - sciprintf (" %04x", dest); + sciprintf(" %04x", dest); if (verbose) - sciprintf (opsize? " [%02x] " : " [%04x] ", param_value); + sciprintf(opsize ? " [%02x] " : " [%04x] ", param_value); - if (opcode == op_lofsa || opcode == op_lofss){ - int atype=script_get_area_type (s, dest, &area_data); + if (opcode == op_lofsa || opcode == op_lofss) { + int atype = script_get_area_type(s, dest, &area_data); if (atype == area_string) { - strncpy (buf, (char *) &data [dest], sizeof (buf)-1); - buf [sizeof (buf)-1] = 0; - if (strlen (buf) > 40){ + strncpy(buf, (char *) &data [dest], sizeof(buf) - 1); + buf [sizeof(buf)-1] = 0; + if (strlen(buf) > 40) { buf [40] = 0; - strcat (buf, "..."); + strcat(buf, "..."); } - sciprintf ("\t\t; \"%s\"", buf); - } - else if (atype == area_said) { - sciprintf ("\t\t; said \""); - script_dump_said_string (d, data, dest); - sciprintf ("\"\n"); - } - else if (atype == area_object) - sciprintf ("\t\t; object <%s>", area_data); + sciprintf("\t\t; \"%s\"", buf); + } else if (atype == area_said) { + sciprintf("\t\t; said \""); + script_dump_said_string(d, data, dest); + sciprintf("\"\n"); + } else if (atype == area_object) + sciprintf("\t\t; object <%s>", area_data); } break; case Script_Property: - if (cur_class != -1 && param_value/2 < d->class_selector_count [cur_class]) { - sciprintf (" %s", get_selector_name (d, d->class_selectors [cur_class][param_value/2])); - if (verbose) sciprintf ("[%x]", param_value); - } - else - sciprintf(opsize? " %02x" : " %04x", param_value); + if (cur_class != -1 && param_value / 2 < d->class_selector_count [cur_class]) { + sciprintf(" %s", get_selector_name(d, d->class_selectors [cur_class][param_value/2])); + if (verbose) sciprintf("[%x]", param_value); + } else + sciprintf(opsize ? " %02x" : " %04x", param_value); break; case Script_End: - if (pass_no == 2) sciprintf ("\n"); + if (pass_no == 2) sciprintf("\n"); break; default: @@ -900,21 +883,20 @@ script_disassemble_code(disasm_state_t *d, script_state_t *s, default: break; } - if (pass_no == 2) sciprintf ("\n"); + if (pass_no == 2) sciprintf("\n"); } } void -disassemble_script_pass (disasm_state_t *d, script_state_t *s, - resource_t *script, int pass_no) -{ +disassemble_script_pass(disasm_state_t *d, script_state_t *s, + resource_t *script, int pass_no) { int _seeker = 0; - word id=getInt16 (script->data); + word id = getInt16(script->data); if (id > 15) { - if (pass_no == 2) sciprintf ("; Old script header detected\n"); + if (pass_no == 2) sciprintf("; Old script header detected\n"); d->old_header = 1; } @@ -934,56 +916,59 @@ disassemble_script_pass (disasm_state_t *d, script_state_t *s, if (pass_no == 2) { sciprintf("; Obj type #%x, offset 0x%x, size 0x%x:\n", objtype, _seeker, objsize); - if (hexdump) sci_hexdump(script->data + seeker, objsize -4, seeker); + if (hexdump) sci_hexdump(script->data + seeker, objsize - 4, seeker); } _seeker += objsize; switch (objtype) { case sci_obj_object: - script_dump_object (d, s, script->data, seeker, objsize, pass_no); + script_dump_object(d, s, script->data, seeker, objsize, pass_no); break; case sci_obj_code: - script_disassemble_code (d, s, script->data, seeker, objsize, pass_no); + script_disassemble_code(d, s, script->data, seeker, objsize, pass_no); break; case sci_obj_synonyms: - script_dump_synonyms (d, s, script->data, seeker, objsize, pass_no); + script_dump_synonyms(d, s, script->data, seeker, objsize, pass_no); break; case sci_obj_said: - script_dump_said (d, s, script->data, seeker, objsize, pass_no); + script_dump_said(d, s, script->data, seeker, objsize, pass_no); break; case sci_obj_strings: - script_dump_strings (d, s, script->data, seeker, objsize, pass_no); + script_dump_strings(d, s, script->data, seeker, objsize, pass_no); break; case sci_obj_class: - script_dump_class (d, s, script->data, seeker, objsize, pass_no); + script_dump_class(d, s, script->data, seeker, objsize, pass_no); break; case sci_obj_exports: - script_dump_exports (d, s, script->data, seeker, objsize, pass_no); + script_dump_exports(d, s, script->data, seeker, objsize, pass_no); break; - case sci_obj_pointers: if (pass_no == 2) { - sciprintf("Pointers\n"); - sci_hexdump(script->data + seeker, objsize -4, seeker); - }; - break; + case sci_obj_pointers: + if (pass_no == 2) { + sciprintf("Pointers\n"); + sci_hexdump(script->data + seeker, objsize - 4, seeker); + }; + break; - case sci_obj_preload_text: if (pass_no == 2) { - sciprintf("The script has a preloaded text resource\n"); - }; - break; + case sci_obj_preload_text: + if (pass_no == 2) { + sciprintf("The script has a preloaded text resource\n"); + }; + break; - case sci_obj_localvars: if (pass_no == 2) { - sciprintf("Local vars\n"); - sci_hexdump(script->data + seeker, objsize -4, seeker); - }; - break; + case sci_obj_localvars: + if (pass_no == 2) { + sciprintf("Local vars\n"); + sci_hexdump(script->data + seeker, objsize - 4, seeker); + }; + break; default: sciprintf("Unsupported %d!\n", objtype); @@ -995,8 +980,7 @@ disassemble_script_pass (disasm_state_t *d, script_state_t *s, } void -disassemble_script(disasm_state_t *d, int res_no, int pass_no) -{ +disassemble_script(disasm_state_t *d, int res_no, int pass_no) { resource_t *script = scir_find_resource(resmgr, sci_script, res_no, 0); script_state_t *s = find_script_state(d, res_no); @@ -1005,5 +989,5 @@ disassemble_script(disasm_state_t *d, int res_no, int pass_no) return; } - disassemble_script_pass (d, s, script, pass_no); + disassemble_script_pass(d, s, script, pass_no); } |