]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - pak.c
Update license headers
[xonotic/gmqcc.git] / pak.c
diff --git a/pak.c b/pak.c
index a9ef112869066920a06b46608926fc3cdf47f823..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 <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)(('P' | ('A' << 8) | ('C' << 16) | ('K' << 24))))
+ */
+#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" */
@@ -36,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;
@@ -47,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;
@@ -58,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;
@@ -95,14 +98,14 @@ static void pak_tree_build(const char *entry) {
 
     memset(pathsplit, 0, 56);
 
-    strncpy(directory, entry, 56);
+    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);
@@ -121,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;
 
@@ -142,7 +145,10 @@ static pak_file_t *pak_open_read(const char *file) {
 
     memset         (&pak->header, 0, sizeof(pak_header_t));
     fs_file_read   (&pak->header,    sizeof(pak_header_t), 1, pak->handle);
-    util_endianswap(&pak->header, 1, sizeof(pak_header_t));
+
+    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 FOURCC data in the
@@ -158,17 +164,20 @@ static pak_file_t *pak_open_read(const char *file) {
     /*
      * Time to read in the directory handles and prepare the directories
      * vector.  We're going to be reading some the file inwards soon.
-     */      
-    fs_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;
         fs_file_read   (&dir,    sizeof(pak_directory_t), 1, pak->handle);
-        util_endianswap(&dir, 1, sizeof(pak_directory_t));
+
+        /* 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);
     }
@@ -184,14 +193,14 @@ static pak_file_t *pak_open_write(const char *file) {
     /*
      * Generate the required directory structure / tree for
      * writing this PAK file too.
-     */   
+     */
     pak_tree_build(file);
 
     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);
@@ -222,7 +231,7 @@ static pak_file_t *pak_open_write(const char *file) {
     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;
 
@@ -234,18 +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;
-  
+
     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]);
             }
@@ -258,25 +267,24 @@ 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, const char *outdir) {
+ */
+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;
-    FILE            *out;
+    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 */
@@ -285,32 +293,31 @@ bool pak_extract_one(pak_file_t *pak, const char *file, const char *outdir) {
     /*
      * Now create the file, if this operation fails.  Then abort
      * It shouldn't fail though.
-     */   
-    if (!(out = fs_file_open(local, "wb"))) {
-        mem_d(dat);
-        return false;
-    }
+     */
+    if (!(out = fs_file_open(local, "wb")))
+        goto err;
 
     /* free memory for directory string */
     mem_d(local);
 
     /* read */
-    fs_file_seek (pak->handle, dir->pos, SEEK_SET);
-    fs_file_read (dat, 1, dir->len, pak->handle);
+    if (fs_file_seek (pak->handle, dir->pos, FS_FILE_SEEK_SET) != 0)
+        goto err;
 
-    /* write */
+    fs_file_read (dat, 1, dir->len, pak->handle);
     fs_file_write(dat, 1, dir->len, out);
-
-    /* close */
     fs_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 (!fs_dir_make(dir))
@@ -328,16 +335,17 @@ 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;
 
@@ -349,31 +357,29 @@ 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.
      */
-    fs_file_seek(fp, 0, SEEK_END);
-    dir.len = fs_file_tell(fp);
-    fs_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.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) {
-        fs_file_close(fp);
-        return false;
-    }
+     */
+    if (strlen(file) >= 56)
+        goto err;
 
-    strncpy(dir.name, file, strlen(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))) {
-        fs_file_close(fp);
-        return false;
-    }
+     */
+    if (!(dat = (unsigned char *)mem_a(dir.len)))
+        goto err;
 
     fs_file_read (dat, dir.len, 1, fp);
     fs_file_close(fp);
@@ -386,13 +392,18 @@ 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;
 
@@ -412,9 +423,11 @@ bool pak_insert_all(pak_file_t *pak, const char *dir) {
     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;
@@ -422,21 +435,26 @@ 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 */ 
-        fs_file_seek (pak->handle, 0, SEEK_SET);
         fs_file_write(&(pak->header), sizeof(pak_header_t), 1, pak->handle);
 
         /* write directories */
-        fs_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++) {
+        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);
@@ -444,6 +462,13 @@ bool pak_close(pak_file_t *pak) {
     mem_d        (pak);
 
     return true;
+
+err:
+    vec_free     (pak->directories);
+    fs_file_close(pak->handle);
+    mem_d        (pak);
+
+    return false;
 }
 
 /*
@@ -476,6 +501,7 @@ static bool parsecmd(const char *optname, int *argc_, char ***argv_, char **out,
     return true;
 }
 
+#include <stdio.h>
 int main(int argc, char **argv) {
     bool          extract   = true;
     char         *redirout  = (char*)stdout;
@@ -552,7 +578,8 @@ int main(int argc, char **argv) {
         /* not possible */
         pak_close(pak);
         vec_free(files);
-        util_meminfo();
+        stat_info();
+
         return EXIT_SUCCESS;
     }
 
@@ -575,7 +602,6 @@ int main(int argc, char **argv) {
     pak_close(pak);
     vec_free(files);
 
-
-    util_meminfo();
+    stat_info();
     return EXIT_SUCCESS;
 }