]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - pak.c
Constant folding for strings
[xonotic/gmqcc.git] / pak.c
diff --git a/pak.c b/pak.c
index 25fe795840354ca23e1b10ed425e1317e7842866..9781c53aa2f8a9bcc3a603809cc3d48812e7c840 100644 (file)
--- a/pak.c
+++ b/pak.c
@@ -1,6 +1,6 @@
 /*
- * Copyright (C) 2013
- *     Dale Weiler 
+ * Copyright (C) 2013, 2014, 2015
+ *     Dale Weiler
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  * this software and associated documentation files (the "Software"), to deal in
  * 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"  
+#include <string.h>
+#include <stdlib.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)(((uint8_t)'P'|((uint8_t)'A'<<8)|((uint8_t)'C'<<16)|((uint8_t)'K'<<24))))
 
 typedef struct {
     uint32_t magic;  /* "PACK" */
@@ -32,7 +39,7 @@ typedef struct {
      * best to store the directories at the end of the file opposed
      * to the front, since it allows easy insertion without having
      * to load the entire file into memory again.
-     */     
+     */
     uint32_t diroff;
     uint32_t dirlen;
 } pak_header_t;
@@ -43,7 +50,7 @@ typedef struct {
  * describes a file (with directories/nested ones too in it's
  * file name).  Hence it can be a file, file with directory, or
  * file with directories.
- */ 
+ */
 typedef struct {
     char     name[56];
     uint32_t pos;
@@ -54,7 +61,7 @@ typedef struct {
  * Used to get the next token from a string, where the
  * strings themselfs are seperated by chracters from
  * `sep`.  This is essentially strsep.
- */   
+ */
 static char *pak_tree_sep(char **str, const char *sep) {
     char *beg = *str;
     char *end;
@@ -71,20 +78,6 @@ static char *pak_tree_sep(char **str, const char *sep) {
     return beg;
 }
 
-/*
- * Used to spawn a directory when creating the pak directory structure/
- * tree.  Think of this as mkdir(path, 0700).  We just cargo cult our
- * own because _mkdir on windows is "illegal" for Windows8 Certification
- * do to the requirement of SECURITY_ATTRIBUTES on everything.
- */    
-static bool pak_tree_spawn(const char *path) {
-#ifdef _MSC_VER
-    return CreateDirectoryA(path, NULL); /* non-zero on success */
-#else
-    return !!(mkdir(path, 0700));        /* zero on success     */
-#endif
-}
-
 /*
  * When given a string like "a/b/c/d/e/file"
  * this function will handle the creation of
@@ -100,22 +93,28 @@ static void pak_tree_build(const char *entry) {
     size_t itr;
     size_t jtr;
 
-    pathsplit = mem_a(56);
-    directory = mem_a(56);
+    pathsplit = (char *)mem_a(56);
+    directory = (char *)mem_a(56);
 
     memset(pathsplit, 0, 56);
-    memset(directory, 0, 56);
 
-    strncpy(directory, entry, 56);
-    for (itr = 0; (token = strsep(&directory, "/")) != NULL; itr++) {
+    util_strncpy(directory, entry, 56);
+    for (itr = 0; (token = pak_tree_sep(&directory, "/")) != NULL; itr++) {
         elements[itr] = token;
     }
 
     for (jtr = 0; jtr < itr - 1; jtr++) {
-        strcat(pathsplit, elements[jtr]);
-        strcat(pathsplit, "/");
+        util_strcat(pathsplit, elements[jtr]);
+        util_strcat(pathsplit, "/");
+
+        if (fs_dir_make(pathsplit)) {
+            mem_d(pathsplit);
+            mem_d(directory);
+
+            /* TODO: undo on fail */
 
-        pak_tree_spawn(pathsplit);
+            return;
+        }
     }
 
     mem_d(pathsplit);
@@ -125,7 +124,7 @@ static void pak_tree_build(const char *entry) {
 typedef struct {
     pak_directory_t *directories;
     pak_header_t     header;
-    FILE            *handle;
+    fs_file_t       *handle;
     bool             insert;
 } pak_file_t;
 
@@ -133,10 +132,10 @@ static pak_file_t *pak_open_read(const char *file) {
     pak_file_t *pak;
     size_t      itr;
 
-    if (!(pak = mem_a(sizeof(pak_file_t))))
+    if (!(pak = (pak_file_t*)mem_a(sizeof(pak_file_t))))
         return NULL;
 
-    if (!(pak->handle = file_open(file, "rb"))) {
+    if (!(pak->handle = fs_file_open(file, "rb"))) {
         mem_d(pak);
         return NULL;
     }
@@ -145,34 +144,40 @@ static pak_file_t *pak_open_read(const char *file) {
     pak->insert      = false; /* read doesn't allow insert */
 
     memset         (&pak->header, 0, sizeof(pak_header_t));
-    file_read      (&pak->header,    sizeof(pak_header_t), 1, pak->handle);
-    util_endianswap(&pak->header, 1, sizeof(pak_header_t));
+    fs_file_read   (&pak->header,    sizeof(pak_header_t), 1, pak->handle);
+
+    util_endianswap(&pak->header.magic,  1, sizeof(pak->header.magic));
+    util_endianswap(&pak->header.diroff, 1, sizeof(pak->header.diroff));
+    util_endianswap(&pak->header.dirlen, 1, sizeof(pak->header.dirlen));
 
     /*
-     * Every PAK file has "PACK" stored as little endian data in the
+     * Every PAK file has "PACK" stored as FOURCC data in the
      * header.  If this data cannot compare (as checked here), it's
      * probably not a PAK file.
      */
-    if ((memcmp(&(pak->header.magic), (const void*)"PACK", sizeof(uint32_t)))) {
-        file_close(pak->handle);
-        mem_d     (pak);
+    if (pak->header.magic != PAK_FOURCC) {
+        fs_file_close(pak->handle);
+        mem_d        (pak);
         return NULL;
     }
 
     /*
      * Time to read in the directory handles and prepare the directories
      * vector.  We're going to be reading some the file inwards soon.
-     */      
-    file_seek(pak->handle, pak->header.diroff, SEEK_SET);
+     */
+    fs_file_seek(pak->handle, pak->header.diroff, FS_FILE_SEEK_SET);
 
     /*
      * Read in all directories from the PAK file. These are considered
      * to be the "file entries".
-     */   
+     */
     for (itr = 0; itr < pak->header.dirlen / 64; itr++) {
         pak_directory_t dir;
-        file_read      (&dir, sizeof(pak_directory_t), 1, pak->handle);
-        /*util_endianswap(&dir, 1, sizeof(pak_directory_t));*/
+        fs_file_read   (&dir,    sizeof(pak_directory_t), 1, pak->handle);
+
+        /* Don't translate name - it's just an array of bytes. */
+        util_endianswap(&dir.pos, 1, sizeof(dir.pos));
+        util_endianswap(&dir.len, 1, sizeof(dir.len));
 
         vec_push(pak->directories, dir);
     }
@@ -182,22 +187,23 @@ static pak_file_t *pak_open_read(const char *file) {
 static pak_file_t *pak_open_write(const char *file) {
     pak_file_t *pak;
 
-    if (!(pak = mem_a(sizeof(pak_file_t))))
+    if (!(pak = (pak_file_t*)mem_a(sizeof(pak_file_t))))
         return NULL;
 
     /*
      * Generate the required directory structure / tree for
      * writing this PAK file too.
-     */   
+     */
     pak_tree_build(file);
 
-    if (!(pak->handle = file_open(file, "wb"))) {
+    if (!(pak->handle = fs_file_open(file, "wb"))) {
         /*
          * The directory tree that was created, needs to be
          * removed entierly if we failed to open a file.
-         */   
+         */
         /* TODO backup directory clean */
 
+        mem_d(pak);
         return NULL;
     }
 
@@ -208,13 +214,11 @@ static pak_file_t *pak_open_write(const char *file) {
      * "patching" and writing the directories at the end of the
      * file.
      */
-    pak->insert = true;
+    pak->insert       = true;
+    pak->header.magic = PAK_FOURCC;
 
-    /*
-     * A valid PAK file contains the magic "PACK" in it's header
-     * stored in little endian format.
-     */
-    memcpy(&(pak->header.magic), (const void*)"PACK", sizeof(uint32_t));
+    /* 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
@@ -222,12 +226,12 @@ static pak_file_t *pak_open_write(const char *file) {
      * will need to be patched in later with a file_seek, and overwrite,
      * we could use offsets and other trickery.  This is just easier.
      */
-    file_write(&(pak->header), sizeof(pak_header_t), 1, pak->handle);
+    fs_file_write(&(pak->header), sizeof(pak_header_t), 1, pak->handle);
 
     return pak;
 }
 
-pak_file_t *pak_open(const char *file, const char *mode) {
+static pak_file_t *pak_open(const char *file, const char *mode) {
     if (!file || !mode)
         return NULL;
 
@@ -239,23 +243,18 @@ pak_file_t *pak_open(const char *file, const char *mode) {
     return NULL;
 }
 
-bool pak_exists(pak_file_t *pak, const char *file, pak_directory_t **dir) {
+static bool pak_exists(pak_file_t *pak, const char *file, pak_directory_t **dir) {
     size_t itr;
 
     if (!pak || !file)
         return false;
 
-    /*
-     * We could technically use a hashtable here.  But I don't think
-     * the lookup complexity is a performance concern.  This may be
-     * O(n) lookup.  But meh?
-     */    
     for (itr = 0; itr < vec_size(pak->directories); itr++) {
         if (!strcmp(pak->directories[itr].name, file)) {
             /*
              * Store back a pointer to the directory that matches
              * the request if requested (NULL is not allowed).
-             */   
+             */
             if (dir) {
                 *dir = &(pak->directories[itr]);
             }
@@ -268,63 +267,64 @@ bool pak_exists(pak_file_t *pak, const char *file, pak_directory_t **dir) {
 
 /*
  * 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;
-    FILE            *out;
+ */
+static 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;
+    fs_file_t       *out   = NULL;
 
     if (!pak_exists(pak, file, &dir)) {
         return false;
     }
 
-    if (!(dat = (unsigned char *)mem_a(dir->len))) {
-        return false;
-    }
+    if (!(dat = (unsigned char *)mem_a(dir->len)))
+        goto err;
 
     /*
      * Generate the directory structure / tree that will be required
      * to store the extracted file.
-     */   
+     */
     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 = file_open(file, "wb"))) {
-        mem_d(dat);
-        return false;
-    }
+     */
+    if (!(out = fs_file_open(local, "wb")))
+        goto err;
 
+    /* free memory for directory string */
+    mem_d(local);
 
     /* read */
-    file_seek (pak->handle, dir->pos, SEEK_SET);
-    file_read (dat, 1, dir->len, pak->handle);
+    if (fs_file_seek (pak->handle, dir->pos, FS_FILE_SEEK_SET) != 0)
+        goto err;
 
-    /* write */
-    file_write(dat, 1, dir->len, out);
+    fs_file_read (dat, 1, dir->len, pak->handle);
+    fs_file_write(dat, 1, dir->len, out);
+    fs_file_close(out);
 
-    /* close */
-    file_close(out);
-
-    /* free */
     mem_d(dat);
-
     return true;
+
+err:
+    if (dat) mem_d(dat);
+    if (out) fs_file_close(out);
+    return false;
 }
 
-bool pak_extract_all(pak_file_t *pak, const char *dir) {
+static bool pak_extract_all(pak_file_t *pak, const char *dir) {
     size_t itr;
 
-    if (!pak_tree_spawn(dir))
-        return false;
-
-    if (chdir(dir))
+    if (!fs_dir_make(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;
     }
 
@@ -335,20 +335,21 @@ bool pak_extract_all(pak_file_t *pak, const char *dir) {
  * Insertion functions (the opposite of extraction).  Yes for generating
  * PAKs.
  */
-bool pak_insert_one(pak_file_t *pak, const char *file) {
+static bool pak_insert_one(pak_file_t *pak, const char *file) {
     pak_directory_t dir;
     unsigned char  *dat;
-    FILE           *fp;
+    long            len;
+    fs_file_t      *fp;
 
     /*
      * We don't allow insertion on files that already exist within the
      * pak file.  Weird shit can happen if we allow that ;). We also
-     * don't allow insertion if the pak isn't opened in write mode.  
-     */ 
+     * don't allow insertion if the pak isn't opened in write mode.
+     */
     if (!pak || !file || !pak->insert || pak_exists(pak, file, NULL))
         return false;
 
-    if (!(fp = fopen(file, "rb")))
+    if (!(fp = fs_file_open(file, "rb")))
         return false;
 
     /*
@@ -356,35 +357,33 @@ bool pak_insert_one(pak_file_t *pak, const char *file) {
      * the directory entry, and the actual contents of the file
      * to the PAK file itself.
      */
-    file_seek(fp, 0, SEEK_END);
-    dir.len = ftell(fp);
-    file_seek(fp, 0, SEEK_SET);
+    if (fs_file_seek(fp, 0, FS_FILE_SEEK_END) != 0 || ((len = fs_file_tell(fp)) < 0))
+        goto err;
+    if (fs_file_seek(fp, 0, FS_FILE_SEEK_SET) != 0)
+        goto err;
 
-    dir.pos = ftell(pak->handle);
+    dir.len = len;
+    dir.pos = fs_file_tell(pak->handle);
 
     /*
      * We're limited to 56 bytes for a file name string, that INCLUDES
      * the directory and '/' seperators.
-     */   
-    if (strlen(file) >= 56) {
-        file_close(fp);
-        return false;
-    }
+     */
+    if (strlen(file) >= 56)
+        goto err;
 
-    strcpy(dir.name, file);
+    util_strncpy(dir.name, file, strlen(file));
 
     /*
      * Allocate some memory for loading in the data that will be
      * redirected into the PAK file.
-     */   
-    if (!(dat = (unsigned char *)mem_a(dir.len))) {
-        file_close(fp);
-        return false;
-    }
+     */
+    if (!(dat = (unsigned char *)mem_a(dir.len)))
+        goto err;
 
-    file_read (dat, dir.len, 1, fp);
-    file_close(fp);
-    file_write(dat, dir.len, 1, pak->handle);
+    fs_file_read (dat, dir.len, 1, fp);
+    fs_file_close(fp);
+    fs_file_write(dat, dir.len, 1, pak->handle);
 
     /*
      * Now add the directory to the directories vector, so pak_close
@@ -393,35 +392,42 @@ bool pak_insert_one(pak_file_t *pak, const char *file) {
     vec_push(pak->directories, dir);
 
     return true;
+
+err:
+    fs_file_close(fp);
+    return false;
 }
 
 /*
  * Like pak_insert_one, except this collects files in all directories
  * from a root directory, and inserts them all.
- */  
-bool pak_insert_all(pak_file_t *pak, const char *dir) {
+ */
+#if 0
+static bool pak_insert_all(pak_file_t *pak, const char *dir) {
     DIR           *dp;
     struct dirent *dirp;
 
     if (!(pak->insert))
         return false;
 
-    if (!(dp = opendir(dir)))
+    if (!(dp = fs_dir_open(dir)))
         return false;
 
-    while ((dirp = readdir(dp))) {
+    while ((dirp = fs_dir_read(dp))) {
         if (!(pak_insert_one(pak, dirp->d_name))) {
-            closedir(dp);
+            fs_dir_close(dp);
             return false;
         }
     }
 
-    closedir(dp);
+    fs_dir_close(dp);
     return true;
 }
+#endif /*!if 0 renable when ready to use */
 
-bool pak_close(pak_file_t *pak) {
+static bool pak_close(pak_file_t *pak) {
     size_t itr;
+    long   tell;
 
     if (!pak)
         return false;
@@ -429,38 +435,173 @@ bool pak_close(pak_file_t *pak) {
     /*
      * In insert mode we need to patch the header, and write
      * our directory entries at the end of the file.
-     */  
+     */
     if (pak->insert) {
+        if ((tell = fs_file_tell(pak->handle)) != 0)
+            goto err;
+
         pak->header.dirlen = vec_size(pak->directories) * 64;
-        pak->header.diroff = ftell(pak->handle);
+        pak->header.diroff = tell;
+
+        /* patch header */
+        if (fs_file_seek (pak->handle, 0, FS_FILE_SEEK_SET) != 0)
+            goto err;
 
-        /* patch header */ 
-        file_seek (pak->handle, 0, SEEK_SET);
-        file_write(&(pak->header), sizeof(pak_header_t), 1, pak->handle);
+        fs_file_write(&(pak->header), sizeof(pak_header_t), 1, pak->handle);
 
         /* write directories */
-        file_seek (pak->handle, pak->header.diroff, SEEK_SET);
+        if (fs_file_seek (pak->handle, pak->header.diroff, FS_FILE_SEEK_SET) != 0)
+            goto err;
 
-        for (itr = 0; itr < vec_size(pak->directories); itr++) {
-            file_write(&(pak->directories[itr]), sizeof(pak_directory_t), 1, pak->handle);
-        }
+        for (itr = 0; itr < vec_size(pak->directories); itr++)
+            fs_file_write(&(pak->directories[itr]), sizeof(pak_directory_t), 1, pak->handle);
     }
 
-    vec_free  (pak->directories);
-    file_close(pak->handle);
-    mem_d     (pak);
+    vec_free     (pak->directories);
+    fs_file_close(pak->handle);
+    mem_d        (pak);
 
     return true;
+
+err:
+    vec_free     (pak->directories);
+    fs_file_close(pak->handle);
+    mem_d        (pak);
+
+    return false;
 }
 
-/* test extraction */
-int main() {
-    pak_file_t *pak = pak_open("pak0.pak", "r");
-    if (!pak) abort();
+/*
+ * 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_;
 
-    pak_extract_all(pak, "foo/");
+    size_t len = strlen(optname);
 
-    pak_close(pak);
-    return 0;
+    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;
 }
 
+#include <stdio.h>
+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);
+        stat_info();
+
+        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);
+
+    stat_info();
+    return EXIT_SUCCESS;
+}