* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#include <sys/stat.h>
-#include <dirent.h>
#include "gmqcc.h"
/*
* The PAK format uses a FOURCC concept for storing the magic ident within
* the header as a uint32_t.
*/
-#define PAK_FOURCC ((uint32_t)(('P' << 24) | ('A' << 16) | ('C' << 8) | 'K'))
+#define PAK_FOURCC ((uint32_t)(('P' | ('A' << 8) | ('C' << 16) | ('K' << 24))))
typedef struct {
uint32_t magic; /* "PACK" */
directory = (char *)mem_a(56);
memset(pathsplit, 0, 56);
- memset(directory, 0, 56);
strncpy(directory, entry, 56);
for (itr = 0; (token = pak_tree_sep(&directory, "/")) != NULL; itr++) {
*/
/* TODO backup directory clean */
+ mem_d(pak);
return NULL;
}
pak->insert = true;
pak->header.magic = PAK_FOURCC;
+ /* on BE systems we need to swap the byte order of the FOURCC */
+ util_endianswap(&pak->header.magic, 1, sizeof(uint32_t));
+
/*
* We need to write out the header since files will be wrote out to
* this even with directory entries, and that not wrote. The header
/*
* Extraction abilities. These work as you expect them to.
*/
-bool pak_extract_one(pak_file_t *pak, const char *file) {
- pak_directory_t *dir = NULL;
- unsigned char *dat = NULL;
+bool pak_extract_one(pak_file_t *pak, const char *file, const char *outdir) {
+ pak_directory_t *dir = NULL;
+ unsigned char *dat = NULL;
+ char *local = NULL;
FILE *out;
if (!pak_exists(pak, file, &dir)) {
*/
pak_tree_build(file);
+ /* TODO portable path seperators */
+ util_asprintf(&local, "%s/%s", outdir, file);
+
/*
* Now create the file, if this operation fails. Then abort
* It shouldn't fail though.
*/
- if (!(out = fs_file_open(file, "wb"))) {
+ if (!(out = fs_file_open(local, "wb"))) {
mem_d(dat);
return false;
}
+ /* free memory for directory string */
+ mem_d(local);
/* read */
fs_file_seek (pak->handle, dir->pos, SEEK_SET);
if (!fs_dir_make(dir))
return false;
- if (fs_dir_change(dir))
- return false;
-
for (itr = 0; itr < vec_size(pak->directories); itr++) {
- if (!pak_extract_one(pak, pak->directories[itr].name))
+ if (!pak_extract_one(pak, pak->directories[itr].name, dir))
return false;
}
return false;
}
- strcpy(dir.name, file);
+ strncpy(dir.name, file, strlen(file));
/*
* Allocate some memory for loading in the data that will be
return true;
}
+
+/*
+ * Fancy GCC-like LONG parsing allows things like --opt=param with
+ * assignment operator. This is used for redirecting stdout/stderr
+ * console to specific files of your choice.
+ */
+static bool parsecmd(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
+ int argc = *argc_;
+ char **argv = *argv_;
+
+ size_t len = strlen(optname);
+
+ if (strncmp(argv[0]+ds, optname, len))
+ return false;
+
+ /* it's --optname, check how the parameter is supplied */
+ if (argv[0][ds+len] == '=') {
+ *out = argv[0]+ds+len+1;
+ return true;
+ }
+
+ if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
+ return false;
+
+ /* using --opt param */
+ *out = argv[1];
+ --*argc_;
+ ++*argv_;
+ return true;
+}
+
+int main(int argc, char **argv) {
+ bool extract = true;
+ char *redirout = (char*)stdout;
+ char *redirerr = (char*)stderr;
+ char *file = NULL;
+ char **files = NULL;
+ pak_file_t *pak = NULL;
+ size_t iter = 0;
+
+ con_init();
+
+ /*
+ * Command line option parsing commences now We only need to support
+ * a few things in the test suite.
+ */
+ while (argc > 1) {
+ ++argv;
+ --argc;
+
+ if (argv[0][0] == '-') {
+ if (parsecmd("redirout", &argc, &argv, &redirout, 1, false))
+ continue;
+ if (parsecmd("redirerr", &argc, &argv, &redirerr, 1, false))
+ continue;
+ if (parsecmd("file", &argc, &argv, &file, 1, false))
+ continue;
+
+ con_change(redirout, redirerr);
+
+ switch (argv[0][1]) {
+ case 'e': extract = true; continue;
+ case 'c': extract = false; continue;
+ }
+
+ if (!strcmp(argv[0]+1, "debug")) {
+ OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
+ continue;
+ }
+ if (!strcmp(argv[0]+1, "memchk")) {
+ OPTS_OPTION_BOOL(OPTION_MEMCHK) = true;
+ continue;
+ }
+ if (!strcmp(argv[0]+1, "nocolor")) {
+ con_color(0);
+ continue;
+ }
+ }
+
+ vec_push(files, argv[0]);
+ }
+ con_change(redirout, redirerr);
+
+
+ if (!file) {
+ con_err("-file must be specified for output/input PAK file\n");
+ vec_free(files);
+ return EXIT_FAILURE;
+ }
+
+ if (extract) {
+ if (!(pak = pak_open(file, "r"))) {
+ con_err("failed to open PAK file %s\n", file);
+ vec_free(files);
+ return EXIT_FAILURE;
+ }
+
+ if (!pak_extract_all(pak, "./")) {
+ con_err("failed to extract PAK %s (files may be missing)\n", file);
+ pak_close(pak);
+ vec_free(files);
+ return EXIT_FAILURE;
+ }
+
+ /* not possible */
+ pak_close(pak);
+ vec_free(files);
+ util_meminfo();
+ return EXIT_SUCCESS;
+ }
+
+ if (!(pak = pak_open(file, "w"))) {
+ con_err("failed to open PAK %s for writing\n", file);
+ vec_free(files);
+ return EXIT_FAILURE;
+ }
+
+ for (iter = 0; iter < vec_size(files); iter++) {
+ if (!(pak_insert_one(pak, files[iter]))) {
+ con_err("failed inserting %s for PAK %s\n", files[iter], file);
+ pak_close(pak);
+ vec_free(files);
+ return EXIT_FAILURE;
+ }
+ }
+
+ /* not possible */
+ pak_close(pak);
+ vec_free(files);
+
+
+ util_meminfo();
+ return EXIT_SUCCESS;
+}