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
This commit is contained in:
meep-eep
2002-10-16 02:55:58 +00:00
parent 2832e02357
commit f2176c674f
2 changed files with 162 additions and 63 deletions
+157 -63
View File
@@ -15,12 +15,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/param.h> #include <sys/param.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include "unpkg.h" #include "unpkg.h"
void readIndex(const uint8 *buf); index_header *readIndex(const uint8 *buf);
char *get_file_name(const uint8 *buf, const index_header *h, int package); char *get_file_name(const uint8 *buf, const index_header *h,
uint16 file_index);
void writeFiles(const index_header *h, const uint8 *data);
int int
main(int argc, char *argv[]) { main(int argc, char *argv[]) {
@@ -28,9 +31,10 @@ main(int argc, char *argv[]) {
int in; int in;
struct stat sb; struct stat sb;
uint8 *buf; uint8 *buf;
index_header *h;
if (argc != 2) { if (argc < 2 || argc > 3) {
fprintf(stderr, "unpkg <infile>\n"); fprintf(stderr, "unpkg <infile> [outdir]\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@@ -54,100 +58,115 @@ main(int argc, char *argv[]) {
} }
close(in); 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); munmap(buf, sb.st_size);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void index_header *
readIndex(const uint8 *buf) { readIndex(const uint8 *buf) {
index_header h; index_header *h;
const uint8 *bufptr; 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", 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]); MAKE_DWORD(buf[0x02], buf[0x03], buf[0x04], buf[0x05]);
fprintf(stderr, "0x00000002 packmem_list_offs: 0x%08x\n", 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]); MAKE_DWORD(buf[0x06], buf[0x07], buf[0x08], buf[0x09]);
fprintf(stderr, "0x00000006 path_list_offs: 0x%08x\n", 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]); MAKE_DWORD(buf[0x0a], buf[0x0b], buf[0x0c], buf[0x0d]);
fprintf(stderr, "0x0000000a file_list_offs: 0x%08x\n", 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", 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", 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", fprintf(stderr, "0x00000012 Header length: 0x%08x\n",
h.header_len); h->header_len);
bufptr = buf + 0x16; 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"); fprintf(stderr, "0x00000016 package info:\n");
{ {
int i; int i;
uint32 temp; 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]); 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_types = GET_TYPE(temp);
h.package_list[i].num_instances = GET_INSTANCE(temp); h->package_list[i].num_instances = GET_INSTANCE(temp);
h.package_list[i].file_index = GET_PACKAGE(temp); h->package_list[i].file_index = GET_PACKAGE(temp);
fprintf(stderr, "0x%08x #%d, %d types, %d instances, " fprintf(stderr, "0x%08x #%d, %d types, %d instances, "
"file index 0x%04x, ", bufptr - buf, i + 1, "file index 0x%04x, ", bufptr - buf, i + 1,
h.package_list[i].num_types, h->package_list[i].num_types,
h.package_list[i].num_instances, h->package_list[i].num_instances,
h.package_list[i].file_index); h->package_list[i].file_index);
bufptr += 4; bufptr += 4;
temp = MAKE_DWORD(bufptr[0], bufptr[1], bufptr[2], bufptr[3]); temp = MAKE_DWORD(bufptr[0], bufptr[1], bufptr[2], bufptr[3]);
h.package_list[i].flags = temp >> 24; h->package_list[i].flags = temp >> 24;
h.package_list[i].data_loc = temp & 0x00ffffff; 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"); fprintf(stderr, "BAD OFFSET!\n");
} else { } else {
fprintf(stderr, "offset 0x%06x\n", fprintf(stderr, "offset 0x%06x\n",
h.package_list[i].data_loc); h->package_list[i].data_loc);
} }
bufptr += 4; bufptr += 4;
} }
} }
// Type info: // 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); fprintf(stderr, "0x%08x type info:\n", bufptr - buf);
{ {
int i; int i;
for (i = 0; i < h.num_types; i++) { for (i = 0; i < h->num_types; i++) {
h.type_list[i].instance_count = h->type_list[i].instance_count =
MAKE_WORD(bufptr[0], bufptr[1]); MAKE_WORD(bufptr[0], bufptr[1]);
fprintf(stderr, "0x%08x #%d, %d instances\n", 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; 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"); 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); fprintf(stderr, "0x%08x packmem info:\n", bufptr - buf);
{ {
@@ -155,13 +174,14 @@ readIndex(const uint8 *buf) {
int num_types, num_instances; int num_types, num_instances;
uint32 temp; uint32 temp;
for (i = 0; i < h.num_packages; i++) { for (i = 0; i < h->num_packages; i++) {
if (h.res_flags) { uint32 next_offset;
if (h->res_flags) {
// packaged // packaged
char *filename; char *filename;
filename = get_file_name(buf, &h, filename = get_file_name(buf, h,
h.package_list[i].file_index); h->package_list[i].file_index);
if (filename == NULL) if (filename == NULL)
filename = "<UNNAMED>"; filename = "<UNNAMED>";
fprintf(stderr, "0x%08x Package #%d (%s):\n", fprintf(stderr, "0x%08x Package #%d (%s):\n",
@@ -172,61 +192,82 @@ readIndex(const uint8 *buf) {
bufptr - buf, i + 1); bufptr - buf, i + 1);
} }
num_types = h.package_list[i].num_types; num_types = h->package_list[i].num_types;
h.package_list[i].type_list = h->package_list[i].type_list =
malloc(num_types * sizeof (packtype_desc)); malloc(num_types * sizeof (packtype_desc));
for (j = 0; j < num_types; j++) { for (j = 0; j < num_types; j++) {
temp = MAKE_DWORD(bufptr[0], bufptr[1], bufptr[2], bufptr[3]); 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].type = GET_TYPE(temp);
h.package_list[i].type_list[j].first_instance = h->package_list[i].type_list[j].first_instance =
GET_INSTANCE(temp); GET_INSTANCE(temp);
h.package_list[i].type_list[j].num_instances = h->package_list[i].type_list[j].num_instances =
GET_PACKAGE(temp); GET_PACKAGE(temp);
fprintf(stderr, "0x%08x Type #%d: type %d, " fprintf(stderr, "0x%08x Type #%d: type %d, "
"%d instances starting with #%d\n", "%d instances starting with #%d\n",
bufptr - buf, j, bufptr - buf, j,
h.package_list[i].type_list[j].type, h->package_list[i].type_list[j].type,
h.package_list[i].type_list[j].num_instances, 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].first_instance);
bufptr += 4; bufptr += 4;
} }
if (h->res_flags) {
// packaged
next_offset = h->package_list[i].data_loc;
}
for (j = 0; j < num_types; j++) { for (j = 0; j < num_types; j++) {
int k; int k;
num_instances = h.package_list[i].type_list[j].num_instances; num_instances = h->package_list[i].type_list[j].num_instances;
for (k = 0; k < num_instances; k++) { if (h->res_flags) {
if (h.res_flags) {
// packaged // 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) {
// 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 " fprintf(stderr, "0x%08x Instance #%d of "
"type %d: size %d bytes\n", bufptr - buf, "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, first_instance,
h.package_list[i].type_list[j].type, h->package_list[i].type_list[j].type,
4 * MAKE_WORD(bufptr[0], bufptr[1])); h->package_list[i].type_list[j].sizes[k]);
} else { } else {
char *filename; char *filename;
filename = get_file_name(buf, &h, filename = get_file_name(buf, h,
MAKE_WORD(bufptr[0], bufptr[1])); MAKE_WORD(bufptr[0], bufptr[1]));
if (filename == NULL) if (filename == NULL)
filename = "<UNNAMED>"; filename = "<UNNAMED>";
fprintf(stderr, "0x%08x Instance #%d of " fprintf(stderr, "0x%08x Instance #%d of "
"type %d: %s\n", bufptr - buf, "type %d: %s\n", bufptr - buf,
k + h.package_list[i].type_list[j]. k + h->package_list[i].type_list[j].
first_instance, first_instance,
h.package_list[i].type_list[j].type, h->package_list[i].type_list[j].type,
filename); filename);
} }
bufptr += 2; bufptr += 2;
} // for k } // for k
} // for j } // for j
} // for i
} }
} return h;
} }
char * 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]; static char result[MAXPATHLEN];
char *ptr; char *ptr;
file_info *fi; file_info *fi;
@@ -258,3 +299,56 @@ get_file_name(const uint8 *buf, const index_header *h, int file_index) {
return result; 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]);
}
}
}
}
+6 -1
View File
@@ -19,12 +19,17 @@ typedef unsigned int uint32;
#define GET_PACKAGE(res) \ #define GET_PACKAGE(res) \
((uint16)((res) >> (TYPE_BITS + INSTANCE_BITS)) & \ ((uint16)((res) >> (TYPE_BITS + INSTANCE_BITS)) & \
((1 << PACKAGE_BITS) - 1)) ((1 << PACKAGE_BITS) - 1))
#define MAKE_RESOURCE(p,t,i) \
(((uint32)(p) << (TYPE_BITS + INSTANCE_BITS)) | \
((uint32)(i) << TYPE_BITS) | \
((uint32)(t)))
typedef struct { typedef struct {
uint8 type; uint8 type;
uint16 first_instance; // only low 13 bits used uint16 first_instance; // only low 13 bits used
uint16 num_instances; // only low 11 bits used uint16 num_instances; // only low 11 bits used
uint32 *sizes; // only low 18 bits used, multiple of 4
uint32 *offsets;
} packtype_desc; } packtype_desc;
typedef struct { typedef struct {