changeset 1026:16e63811d0c2

Clean up the pack file node adding/extracting.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Mar 2015 16:07:45 +0200
parents b43885dd3d1a
children d29058e93799
files tools/packed.c
diffstat 1 files changed, 96 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/tools/packed.c	Sun Mar 01 16:05:50 2015 +0200
+++ b/tools/packed.c	Sun Mar 01 16:07:45 2015 +0200
@@ -255,15 +255,14 @@
 
 
 int dmPackAddFile(DMPackFile * pack, const char *filename,
-    BOOL doCompress, const Uint32 flags, DMPackEntry ** ppEntry)
+    const Uint32 flags, const BOOL compress, int level, DMPackEntry ** ppEntry)
 {
     z_stream zstr;
-    off_t startOffs;
-    unsigned int zstrSize;
-    FILE *inFile = NULL;
+    Uint64 startOffs, outSize;
+    FILE *inFile = NULL, *tmpFile;
     Uint8 *inBuffer = NULL, *outBuffer = NULL;
     DMPackEntry entry, *node;
-    int zres, ret = DMERR_OK;
+    int ret = DMERR_OK;
     BOOL zinit = FALSE;
 
     *ppEntry = NULL;
@@ -275,6 +274,7 @@
         return DMERR_FOPEN;
 
     // Compute starting offset
+    outSize = 0;
     startOffs = sizeof(DMPackFileHeader);
     node = pack->entries;
     while (node != NULL)
@@ -300,38 +300,43 @@
     }
 
     // Read (and possibly compress) the data
-    zstrSize = 0;
-    zstr.zalloc = (alloc_func) Z_NULL;
-    zstr.zfree = (free_func) Z_NULL;
-    zstr.opaque = (voidpf) Z_NULL;
-    zres = deflateInit(&zstr, doCompress ? Z_DEFAULT_COMPRESSION : 0);
-    if (zres != Z_OK)
+    if (compress)
     {
-        ret = DMERR_COMPRESSION;
-        goto out;
-    }
-    zinit = TRUE;
+        int zret;
+        memset(&zstr, 0, sizeof(zstr));
+        if (deflateInit(&zstr, level) != Z_OK)
+        {
+            ret = DMERR_COMPRESSION;
+            goto out;
+        }
+        zinit = TRUE;
+
+        tmpFile = fopen("dump.bin", "wb");
 
-    // Initialize compression streams
-    zres = Z_OK;
-    while (!feof(inFile) && zres == Z_OK)
-    {
-        zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile);
-        zstr.next_in = inBuffer;
-        zstr.next_out = outBuffer;
-        zstr.avail_out = SET_TMPBUF_SIZE;
-        zstr.total_out = 0;
-        zres = deflate(&zstr, Z_FULL_FLUSH);
+        // Initialize compression streams
+        zret = Z_OK;
+        while (!feof(inFile) && zret == Z_OK)
+        {
+            zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile);
 
-        if (zres == Z_OK && zstr.total_out > 0)
-        {
-            zstrSize += zstr.total_out;
-            if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file) != zstr.total_out)
+            zstr.next_in = inBuffer;
+            zstr.next_out = outBuffer;
+            zstr.avail_out = SET_TMPBUF_SIZE;
+            zstr.total_out = 0;
+            zret = deflate(&zstr, Z_FULL_FLUSH);
+
+            if (zret == Z_OK && zstr.total_out > 0)
             {
-                ret = DMERR_FWRITE;
-                goto out;
+                outSize += zstr.total_out;
+                fwrite(outBuffer, sizeof(Uint8), zstr.total_out, tmpFile);
+                if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file) != zstr.total_out)
+                {
+                    ret = DMERR_FWRITE;
+                    goto out;
+                }
             }
         }
+        fclose(tmpFile);
     }
 
     // Create directory entry
@@ -339,7 +344,7 @@
     entry.filename[sizeof(entry.filename) - 1] = 0;
     entry.offset = startOffs;
     entry.size   = zstr.total_in;
-    entry.length = zstrSize;
+    entry.length = outSize;
     entry.flags  = flags;
 
     // Add directory entry
@@ -368,13 +373,14 @@
 /*
  * EXTRACT a file from the PACK
  */
-int dmPackExtractFile(DMPackFile *pack, DMPackEntry * entry)
+int dmPackExtractFile(DMPackFile *pack, DMPackEntry * entry, BOOL decompress)
 {
     z_stream zstr;
-    FILE *outFile;
-    Uint8 *inBuffer, *outBuffer;
-    size_t inDataLeft;
-    int ret;
+    FILE *outFile = NULL;
+    Uint8 *inBuffer = NULL, *outBuffer = NULL;
+    size_t remaining;
+    int zret, ret = DMERR_OK;
+    BOOL zinit = FALSE;
 
     if (pack == NULL)
         return DMERR_NULLPTR;
@@ -384,72 +390,84 @@
 
     // Seek to the position
     if (fseek(pack->file, entry->offset, SEEK_SET) != 0)
-        return DMERR_INVALID;
+        return DMERR_FSEEK;
 
     // Open destination file
     if ((outFile = fopen(entry->filename, "wb")) == NULL)
-        return -1;
+        return DMERR_FOPEN;
 
     // Allocate temporary buffer
-    if ((inBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL)
+    if ((inBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL ||
+        (outBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL)
     {
-        fclose(outFile);
-        return DMERR_MALLOC;
+        ret = DMERR_MALLOC;
+        goto out;
     }
 
-    if ((outBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL)
+    // Read and uncompress the data, if needed
+    if (decompress || (entry->flags & DMF_COMPRESSED) == 0)
     {
-        dmFree(inBuffer);
-        fclose(outFile);
-        return DMERR_MALLOC;
-    }
-
-    // Read and uncompress the data
-    zstr.zalloc = (alloc_func) Z_NULL;
-    zstr.zfree = (free_func) Z_NULL;
-    zstr.opaque = (voidpf) Z_NULL;
-    ret = inflateInit(&zstr);
-    if (ret != Z_OK)
-    {
-        dmFree(inBuffer);
-        dmFree(outBuffer);
-        fclose(outFile);
-        return DMERR_COMPRESSION;
+        memset(&zstr, 0, sizeof(zstr));
+        if (inflateInit(&zstr) != Z_OK)
+        {
+            ret = DMERR_COMPRESSION;
+            goto out;
+        }
+        zinit = TRUE;
     }
 
     // Initialize compression streams
-    inDataLeft = entry->length;
-    ret = Z_OK;
-    while (inDataLeft > 0 && ret == Z_OK)
+    remaining = entry->length;
+    zret = Z_OK;
+    while (remaining > 0 && zret == Z_OK)
     {
-        if (inDataLeft >= SET_TMPBUF_SIZE)
-            zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, pack->file);
-        else
-            zstr.avail_in = fread(inBuffer, sizeof(Uint8), inDataLeft, pack->file);
+        size_t needed = remaining > SET_TMPBUF_SIZE ? SET_TMPBUF_SIZE : remaining;
+        zstr.avail_in = fread(inBuffer, sizeof(Uint8), needed, pack->file);
+        if (zstr.avail_in < needed)
+        {
+            ret = DMERR_FREAD;
+            goto out;
+        }
 
         remaining   -= zstr.avail_in;
         zstr.next_in = inBuffer;
 
-        while (zstr.avail_in > 0 && ret == Z_OK)
+        if (!decompress)
         {
-            zstr.next_out = outBuffer;
+            if (fwrite(inBuffer, sizeof(Uint8), zstr.avail_in, outFile) != zstr.avail_in)
+            {
+                ret = DMERR_FWRITE;
+                goto out;
+            }
+        }
+        else
+        while (zstr.avail_in > 0 && zret == Z_OK)
+        {
+            zstr.next_out  = outBuffer;
             zstr.avail_out = SET_TMPBUF_SIZE;
             zstr.total_out = 0;
-            ret = inflate(&zstr, Z_FULL_FLUSH);
-            if (zstr.total_out > 0)
+            zret = inflate(&zstr, Z_FULL_FLUSH);
+            if (zstr.total_out > 0 &&
+                fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile) != zstr.total_out)
             {
-                fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile);
+                ret = DMERR_FWRITE;
+                goto out;
             }
         }
     }
 
+out:
     // Cleanup
-    inflateEnd(&zstr);
+    if (zinit)
+        inflateEnd(&zstr);
+
     dmFree(inBuffer);
     dmFree(outBuffer);
-    fclose(outFile);
 
-    return DMERR_OK;
+    if (outFile != NULL)
+        fclose(outFile);
+
+    return ret;
 }
 
 
@@ -631,7 +649,9 @@
             if (!dmCheckExcluded(srcFilenames[i]))
             {
                 DMPackEntry *node = NULL;
-                int res = dmPackAddFile(pack, srcFilenames[i], optCompress, optDefResFlags, &node);
+                int res = dmPackAddFile(pack, srcFilenames[i],
+                    optDefResFlags, optCompress,
+                    Z_DEFAULT_COMPRESSION, &node);
 
                 if (res != DMERR_OK)
                 {
@@ -739,7 +759,7 @@
                             node->filename, node->size, node->length,
                             node->offset, node->flags);
 
-                    dmPackExtractFile(pack, node);
+                    dmPackExtractFile(pack, node, !optCompress);
                 }
             }