From f2176c674f00d5988c0a71595904fdb6fae55eed Mon Sep 17 00:00:00 2001 From: meep-eep Date: Wed, 16 Oct 2002 02:55:58 +0000 Subject: [PATCH] Will now output the files. All now left to do is interpret the various formats for the resources in them. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@131 8092fc87-c524-0410-9efc-e669fe64eaf9 --- tools/pkg/unpkg.c | 218 +++++++++++++++++++++++++++++++++------------- tools/pkg/unpkg.h | 7 +- 2 files changed, 162 insertions(+), 63 deletions(-) diff --git a/tools/pkg/unpkg.c b/tools/pkg/unpkg.c index 55177c7cd..ef1ace5be 100644 --- a/tools/pkg/unpkg.c +++ b/tools/pkg/unpkg.c @@ -15,12 +15,15 @@ #include #include #include +#include #include "unpkg.h" -void readIndex(const uint8 *buf); -char *get_file_name(const uint8 *buf, const index_header *h, int package); +index_header *readIndex(const uint8 *buf); +char *get_file_name(const uint8 *buf, const index_header *h, + uint16 file_index); +void writeFiles(const index_header *h, const uint8 *data); int main(int argc, char *argv[]) { @@ -28,9 +31,10 @@ main(int argc, char *argv[]) { int in; struct stat sb; uint8 *buf; + index_header *h; - if (argc != 2) { - fprintf(stderr, "unpkg \n"); + if (argc < 2 || argc > 3) { + fprintf(stderr, "unpkg [outdir]\n"); return EXIT_FAILURE; } @@ -54,100 +58,115 @@ main(int argc, char *argv[]) { } close(in); - readIndex(buf); + h = readIndex(buf); + if (argc > 2) { + char *dir = argv[2]; + if (h->res_flags) { + // packaged + if (chdir(dir) == -1) { + perror("Could not change directory"); + return EXIT_FAILURE; + } + writeFiles(h, buf); + } else { + fprintf(stderr, "Cannot unpack data from unpacked file.\n"); + } + } + + // freeIndex(h); munmap(buf, sb.st_size); - return EXIT_SUCCESS; } -void +index_header * readIndex(const uint8 *buf) { - index_header h; + index_header *h; const uint8 *bufptr; - h.res_flags = MAKE_WORD(buf[0x00], buf[0x01]) ? 1 : 0; + h = malloc(sizeof (index_header)); + h->res_flags = MAKE_WORD(buf[0x00], buf[0x01]) ? 1 : 0; fprintf(stderr, "0x00000000 File type: %s\n", - h.res_flags ? "packaged" : "not packaged"); + h->res_flags ? "packaged" : "not packaged"); - h.packmem_list_offs = + h->packmem_list_offs = MAKE_DWORD(buf[0x02], buf[0x03], buf[0x04], buf[0x05]); fprintf(stderr, "0x00000002 packmem_list_offs: 0x%08x\n", - h.packmem_list_offs); + h->packmem_list_offs); - h.path_list_offs = + h->path_list_offs = MAKE_DWORD(buf[0x06], buf[0x07], buf[0x08], buf[0x09]); fprintf(stderr, "0x00000006 path_list_offs: 0x%08x\n", - h.path_list_offs); + h->path_list_offs); - h.file_list_offs = + h->file_list_offs = MAKE_DWORD(buf[0x0a], buf[0x0b], buf[0x0c], buf[0x0d]); fprintf(stderr, "0x0000000a file_list_offs: 0x%08x\n", - h.file_list_offs); + h->file_list_offs); - h.num_packages = MAKE_WORD(buf[0x0e], buf[0x0f]); + h->num_packages = MAKE_WORD(buf[0x0e], buf[0x0f]); fprintf(stderr, "0x0000000e Number of packages: %d\n", - h.num_packages); + h->num_packages); - h.num_types = MAKE_WORD(buf[0x10], buf[0x11]); + h->num_types = MAKE_WORD(buf[0x10], buf[0x11]); fprintf(stderr, "0x00000010 Number of package types: %d\n", - h.num_types); + h->num_types); - h.header_len = MAKE_DWORD(buf[0x12], buf[0x13], buf[0x14], buf[0x15]); + h->header_len = MAKE_DWORD(buf[0x12], buf[0x13], buf[0x14], buf[0x15]); fprintf(stderr, "0x00000012 Header length: 0x%08x\n", - h.header_len); + h->header_len); bufptr = buf + 0x16; - h.package_list = malloc(h.num_packages * sizeof (package_desc)); + h->package_list = malloc(h->num_packages * sizeof (package_desc)); fprintf(stderr, "0x00000016 package info:\n"); { int i; uint32 temp; - for (i = 0; i < h.num_packages; i++) { + for (i = 0; i < h->num_packages; i++) { temp = MAKE_DWORD(bufptr[0], bufptr[1], bufptr[2], bufptr[3]); - h.package_list[i].num_types = GET_TYPE(temp); - h.package_list[i].num_instances = GET_INSTANCE(temp); - h.package_list[i].file_index = GET_PACKAGE(temp); + h->package_list[i].num_types = GET_TYPE(temp); + h->package_list[i].num_instances = GET_INSTANCE(temp); + h->package_list[i].file_index = GET_PACKAGE(temp); fprintf(stderr, "0x%08x #%d, %d types, %d instances, " "file index 0x%04x, ", bufptr - buf, i + 1, - h.package_list[i].num_types, - h.package_list[i].num_instances, - h.package_list[i].file_index); + h->package_list[i].num_types, + h->package_list[i].num_instances, + h->package_list[i].file_index); bufptr += 4; temp = MAKE_DWORD(bufptr[0], bufptr[1], bufptr[2], bufptr[3]); - h.package_list[i].flags = temp >> 24; - h.package_list[i].data_loc = temp & 0x00ffffff; + h->package_list[i].flags = temp >> 24; + h->package_list[i].data_loc = temp & 0x00ffffff; - if (h.package_list[i].flags != 0xff) { + if (h->package_list[i].flags != 0xff) { fprintf(stderr, "BAD OFFSET!\n"); } else { fprintf(stderr, "offset 0x%06x\n", - h.package_list[i].data_loc); + h->package_list[i].data_loc); } bufptr += 4; } } // Type info: - h.type_list = malloc(h.num_types * sizeof (type_desc)); + h->type_list = malloc(h->num_types * sizeof (type_desc)); fprintf(stderr, "0x%08x type info:\n", bufptr - buf); { int i; - for (i = 0; i < h.num_types; i++) { - h.type_list[i].instance_count = + for (i = 0; i < h->num_types; i++) { + h->type_list[i].instance_count = MAKE_WORD(bufptr[0], bufptr[1]); fprintf(stderr, "0x%08x #%d, %d instances\n", - bufptr - buf, i + 1, h.type_list[i].instance_count); + bufptr - buf, i + 1, h->type_list[i].instance_count); bufptr += 2; } } - if ((uint32) (bufptr - buf) != h.packmem_list_offs) { + if ((uint32) (bufptr - buf) != h->packmem_list_offs) { fprintf(stderr, "PACKMEM_LIST NOT IMMEDIATELY AFTER TYPE LIST!\n"); - bufptr = buf + h.packmem_list_offs; + bufptr = buf + h->packmem_list_offs; } fprintf(stderr, "0x%08x packmem info:\n", bufptr - buf); { @@ -155,13 +174,14 @@ readIndex(const uint8 *buf) { int num_types, num_instances; uint32 temp; - for (i = 0; i < h.num_packages; i++) { - if (h.res_flags) { + for (i = 0; i < h->num_packages; i++) { + uint32 next_offset; + if (h->res_flags) { // packaged char *filename; - filename = get_file_name(buf, &h, - h.package_list[i].file_index); + filename = get_file_name(buf, h, + h->package_list[i].file_index); if (filename == NULL) filename = ""; fprintf(stderr, "0x%08x Package #%d (%s):\n", @@ -172,61 +192,82 @@ readIndex(const uint8 *buf) { bufptr - buf, i + 1); } - num_types = h.package_list[i].num_types; - h.package_list[i].type_list = + num_types = h->package_list[i].num_types; + h->package_list[i].type_list = malloc(num_types * sizeof (packtype_desc)); for (j = 0; j < num_types; j++) { temp = MAKE_DWORD(bufptr[0], bufptr[1], bufptr[2], bufptr[3]); - h.package_list[i].type_list[j].type = GET_TYPE(temp); - h.package_list[i].type_list[j].first_instance = + h->package_list[i].type_list[j].type = GET_TYPE(temp); + h->package_list[i].type_list[j].first_instance = GET_INSTANCE(temp); - h.package_list[i].type_list[j].num_instances = + h->package_list[i].type_list[j].num_instances = GET_PACKAGE(temp); fprintf(stderr, "0x%08x Type #%d: type %d, " "%d instances starting with #%d\n", bufptr - buf, j, - h.package_list[i].type_list[j].type, - h.package_list[i].type_list[j].num_instances, - h.package_list[i].type_list[j].first_instance); + h->package_list[i].type_list[j].type, + h->package_list[i].type_list[j].num_instances, + h->package_list[i].type_list[j].first_instance); bufptr += 4; } + if (h->res_flags) { + // packaged + next_offset = h->package_list[i].data_loc; + } for (j = 0; j < num_types; j++) { int k; - num_instances = h.package_list[i].type_list[j].num_instances; + num_instances = h->package_list[i].type_list[j].num_instances; + if (h->res_flags) { + // packaged + h->package_list[i].type_list[j].sizes = + malloc(sizeof (uint32) * num_instances); + h->package_list[i].type_list[j].offsets = + malloc(sizeof (uint32) * num_instances); + } else { + h->package_list[i].type_list[j].sizes = NULL; + h->package_list[i].type_list[j].offsets = NULL; + } for (k = 0; k < num_instances; k++) { - if (h.res_flags) { + if (h->res_flags) { // packaged + h->package_list[i].type_list[j].sizes[k] = + 4 * MAKE_WORD(bufptr[0], bufptr[1]); + h->package_list[i].type_list[j].offsets[k] = + next_offset; + next_offset += + h->package_list[i].type_list[j].sizes[k]; fprintf(stderr, "0x%08x Instance #%d of " "type %d: size %d bytes\n", bufptr - buf, - k + h.package_list[i].type_list[j]. + k + h->package_list[i].type_list[j]. first_instance, - h.package_list[i].type_list[j].type, - 4 * MAKE_WORD(bufptr[0], bufptr[1])); + h->package_list[i].type_list[j].type, + h->package_list[i].type_list[j].sizes[k]); } else { char *filename; - filename = get_file_name(buf, &h, + filename = get_file_name(buf, h, MAKE_WORD(bufptr[0], bufptr[1])); if (filename == NULL) filename = ""; fprintf(stderr, "0x%08x Instance #%d of " "type %d: %s\n", bufptr - buf, - k + h.package_list[i].type_list[j]. + k + h->package_list[i].type_list[j]. first_instance, - h.package_list[i].type_list[j].type, + h->package_list[i].type_list[j].type, filename); } bufptr += 2; } // for k } // for j - } + } // for i } + return h; } char * -get_file_name(const uint8 *buf, const index_header *h, int file_index) { +get_file_name(const uint8 *buf, const index_header *h, uint16 file_index) { static char result[MAXPATHLEN]; char *ptr; file_info *fi; @@ -258,3 +299,56 @@ get_file_name(const uint8 *buf, const index_header *h, int file_index) { return result; } +void +writeFile(const char *file, const uint8 *data, size_t len) { + int fd; + + fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (fd == -1) { + perror("Could not open file for writing"); + exit(EXIT_FAILURE); + } + while (len > 0) { + ssize_t written; + written = write(fd, data, len); + if (written < 0) { + if (errno != EINTR) { + perror("write failed"); + exit(1); + } + } + len -= written; + data += written; + } + close(fd); +} + +void +writeFiles(const index_header *h, const uint8 *data) { + int i; + char filename[sizeof("01234567=012-01-012")]; + int num_types, num_instances; + + for (i = 0; i < h->num_packages; i++) { + int j; + num_types = h->package_list[i].num_types; + for (j = 0; j < num_types; j++) { + int k; + + num_instances = h->package_list[i].type_list[j].num_instances; + for (k = 0; k < num_instances; k++) { + sprintf(filename, "%08x=%03x-%02x-%03x", + MAKE_RESOURCE(i + 1, + h->package_list[i].type_list[j].type, + h->package_list[i].type_list[j].first_instance + k), + i + 1, + h->package_list[i].type_list[j].type, + h->package_list[i].type_list[j].first_instance + k); + writeFile(filename, + &data[h->package_list[i].type_list[j].offsets[k]], + h->package_list[i].type_list[j].sizes[k]); + } + } + } +} + diff --git a/tools/pkg/unpkg.h b/tools/pkg/unpkg.h index 8ac7e594e..9daa6791b 100644 --- a/tools/pkg/unpkg.h +++ b/tools/pkg/unpkg.h @@ -19,12 +19,17 @@ typedef unsigned int uint32; #define GET_PACKAGE(res) \ ((uint16)((res) >> (TYPE_BITS + INSTANCE_BITS)) & \ ((1 << PACKAGE_BITS) - 1)) - +#define MAKE_RESOURCE(p,t,i) \ + (((uint32)(p) << (TYPE_BITS + INSTANCE_BITS)) | \ + ((uint32)(i) << TYPE_BITS) | \ + ((uint32)(t))) typedef struct { uint8 type; uint16 first_instance; // only low 13 bits used uint16 num_instances; // only low 11 bits used + uint32 *sizes; // only low 18 bits used, multiple of 4 + uint32 *offsets; } packtype_desc; typedef struct {