diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/Makefile | 4 | ||||
-rw-r--r-- | utils/cattbl.c | 80 | ||||
-rw-r--r-- | utils/extractstrtbltbl.c | 128 | ||||
-rw-r--r-- | utils/txt2strtbl.c | 153 |
4 files changed, 365 insertions, 0 deletions
diff --git a/utils/Makefile b/utils/Makefile new file mode 100644 index 0000000..3de7eba --- /dev/null +++ b/utils/Makefile @@ -0,0 +1,4 @@ +all: extractstrtbltbl txt2strtbl cattbl + +clean: + rm -f extractstrtbltbl txt2strtbl cattbl diff --git a/utils/cattbl.c b/utils/cattbl.c new file mode 100644 index 0000000..494b6b9 --- /dev/null +++ b/utils/cattbl.c @@ -0,0 +1,80 @@ +/* + * Copyright © 2014 IIMarckus <imarckus@gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <err.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void +putuint32BE(uint32_t n, FILE *f) +{ + fputc(n >> 24, f); + fputc(n >> 16, f); + fputc(n >> 8, f); + fputc(n, f); +} + +int +main(int argc, char *argv[]) +{ + int i, runsum, numptrs; + uint32_t *ptrs, *runsums; + FILE **f; + uint8_t buf[8192]; + + if (argc < 2) { + errx(1, "Usage: cattbl file ..."); + } + + f = reallocarray(NULL, argc - 1, sizeof *f); + ptrs = reallocarray(NULL, argc - 1, sizeof *ptrs); + runsums = reallocarray(NULL, argc - 1, sizeof *runsums); + numptrs = 0; + + putuint32BE(0, stdout); + putuint32BE(0, stdout); + + runsum = 16 + 16 * (argc - 1); + for (i = 1; i < argc; ++i) { + f[i - 1] = fopen(argv[i], "rb"); + if (f[i - 1] == NULL) { + err(1, "Could not open file '%s'", argv[i]); + } + fseek(f[i - 1], 0, SEEK_END); + runsums[i - 1] = runsum; + runsum += ftell(f[i - 1]); + ptrs[i - 1] = ftell(f[i - 1]); + fseek(f[i - 1], 0, SEEK_SET); + } + putuint32BE(runsum, stdout); + putuint32BE(argc - 1, stdout); + for (i = 0; i < argc - 1; ++i) { + putuint32BE(runsums[i], stdout); + putuint32BE(ptrs[i], stdout); + putuint32BE(0, stdout); + putuint32BE(0, stdout); + } + + for (i = 0; i < argc - 1; ++i) { + size_t n; + + while ((n = fread(buf, sizeof *buf, sizeof buf, f[i])) > 0) + if (fwrite(buf, sizeof *buf, n, stdout) != n) + ; + } +} diff --git a/utils/extractstrtbltbl.c b/utils/extractstrtbltbl.c new file mode 100644 index 0000000..1239e94 --- /dev/null +++ b/utils/extractstrtbltbl.c @@ -0,0 +1,128 @@ +/* + * Copyright © 2014 IIMarckus <imarckus@gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Dump a table of string tables to individual text files. + * One table per text file. + */ + +#include <err.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +#define TEXTBASE 0x783760 + +uint32_t +getint32BE(FILE *f) +{ + uint32_t n; + n = fgetc(f) << 24; + n |= fgetc(f) << 16; + n |= fgetc(f) << 8; + n |= fgetc(f); + + return n; +} + +int +main(int argc, char *argv[]) +{ + FILE *rom; + FILE *outfile; + uint32_t ntables, nstrings; + uint32_t *tableptrs, *stringptrs; + int i, x, table, string; + char outfilename[11]; + + if (argc != 2) { + errx(1, "Usage: extractstrtbltbl file"); + } + + rom = fopen(argv[1], "rb"); + if (rom == NULL) { + err(1, "Error opening file '%s'", argv[1]); + } + + fseek(rom, TEXTBASE + 0xc, SEEK_SET); + ntables = getint32BE(rom); + tableptrs = reallocarray(NULL, ntables, sizeof(uint32_t)); + if (tableptrs == NULL) { + err(1, "Could not allocate memory for table table"); + } + + for (i = 0; i < ntables; ++i) { + tableptrs[i] = getint32BE(rom); + getint32BE(rom); + getint32BE(rom); + getint32BE(rom); + } + printf("%d tables\n", ntables); + + for (table = 0; table < ntables; ++table) { + fseek(rom, TEXTBASE + tableptrs[table], SEEK_SET); + snprintf(outfilename, sizeof outfilename, "0x%x", ftell(rom)); + outfile = fopen(outfilename, "wb"); + nstrings = getint32BE(rom); + + printf("%d strings in table %d\n", nstrings, table); + + stringptrs = reallocarray(NULL, nstrings, sizeof(uint32_t)); + if (stringptrs == NULL) { + err(1, "Could not allocate memory for string table"); + } + + for (i = 0; i < nstrings; ++i) { + stringptrs[i] = getint32BE(rom); + } + for (i = 0; i < nstrings; ++i) { + while ((x = fgetc(rom)) != '\0') { + if (x == '\n') { + int y; + y = fgetc(rom); + ungetc(y, rom); + + if (y == '\n' || y == '\0') { + fputc('@', outfile); + } else { + fputc('\n', outfile); + } + } else if (x == 0xa5) { + fprintf(outfile, "\xc2\xa5"); + } else if (x == 0xa9) { + fprintf(outfile, "\xe2\x99\x82"); + } else if (x == 0xbe) { + fprintf(outfile, "\xe2\x99\x80"); + } else if (x == 0xb7) { + fprintf(outfile, "\xc2\xb7"); + } else if (x == 0xd7) { + fprintf(outfile, "\xc3\x97"); + } else if (x == 0xe9) { + fprintf(outfile, "\xc3\xa9"); + } else { + fputc(x, outfile); + } + } + + fputc('\n', outfile); + if (i < nstrings - 1) { + fputc('\n', outfile); + } + } + + fclose(outfile); + } +} diff --git a/utils/txt2strtbl.c b/utils/txt2strtbl.c new file mode 100644 index 0000000..dd450ea --- /dev/null +++ b/utils/txt2strtbl.c @@ -0,0 +1,153 @@ +/* + * Copyright © 2014 IIMarckus <imarckus@gmail.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <err.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* + * string table format: + * 32-bit pointers to each string, relative to the start of the table + * strings + * 0xff for padding to nearest 16-byte boundary + */ + +void +putuint32BE(uint32_t n, FILE *f) +{ + fputc(n >> 24, f); + fputc(n >> 16, f); + fputc(n >> 8, f); + fputc(n, f); +} + +/* transform a blank line-delimited record into a null-terminated string. */ +char * +getrecord(FILE *f) +{ + int x; + char *s; + int len; + + s = malloc(500); + len = 0; + + for (;;) { + x = fgetc(f); + if (x == -1) { + return NULL; + } else if (x == '@') { + s[len++] = '\n'; + } else if (x == 0xe2) { + x = fgetc(f); + if (x != 0x99) { + errx(1, "Bad input char 0x%x", x); + } + x = fgetc(f); + if (x == 0x82) { + s[len++] = '\xa9'; + } else if (x == 0x80) { + s[len++] = '\xbe'; + } else { + errx(1, "Bad input char 0x%x", x); + } + } else if (x == 0xc2) { + x = fgetc(f); + if (x == 0xa5) { + s[len++] = '\xa5'; + } else if (x == 0xb7) { + s[len++] = '\xb7'; + } else { + errx(1, "Bad input char 0x%x", x); + } + } else if (x == 0xc3) { + x = fgetc(f); + if (x == 0xa9) { + s[len++] = '\xe9'; + } else if (x == 0x97) { + s[len++] = '\xd7'; + } else { + errx(1, "Bad input char 0x%x", x); + } + } else if (x == '\n') { + x = fgetc(f); + if (x == '\n' || x == -1) { + /* end of string */ + s[len++] = '\0'; + return s; + } else { + ungetc(x, f); + s[len++] = '\n'; + } + } else { + s[len++] = x; + } + } +} + +int +main(int argc, char *argv[]) +{ + FILE *f; + char **s; + uint32_t *lengths; + int i, x, count, arrcount, numstrings, runsum; + uint8_t *arr; + char *t; + + if (argc != 2) { + errx(1, "Usage: txt2strtbl file"); + } + + f = fopen(argv[1], "rb"); + if (f == NULL) { + err(1, "Error opening file '%s'", argv[1]); + } + + /* + * 2500 is a decent enough default. + * There are no string tables that big. + */ + s = reallocarray(NULL, sizeof *s, 2500); + lengths = reallocarray(NULL, sizeof *lengths, 2500); + if (s == NULL || lengths == NULL) { + err(1, "could not allocate memory"); + } + + numstrings = 0; + while ((t = getrecord(f)) != NULL) { + s[numstrings] = t; + lengths[numstrings] = strlen(t); + numstrings++; + } + + putuint32BE(numstrings, stdout); + runsum = 4 + sizeof(uint32_t) * numstrings; + for (i = 0; i < numstrings; ++i) { + putuint32BE(runsum, stdout); + runsum += strlen(s[i]) + 1; + } + for (i = 0; i < numstrings; ++i) { + printf("%s", s[i]); + fputc('\0', stdout); + } + + while (runsum++ % 16) { + fputc(0xff, stdout); + } +} |