changeset 994:e8de4fbc03b6

Clean up packed a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 28 Feb 2015 15:59:39 +0200
parents 5bcd219ddde3
children aeafd6d44465
files tools/packed.c
diffstat 1 files changed, 41 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/tools/packed.c	Sat Feb 28 05:43:31 2015 +0200
+++ b/tools/packed.c	Sat Feb 28 15:59:39 2015 +0200
@@ -262,15 +262,16 @@
     z_stream zstr;
     off_t startOffs;
     unsigned int zstrSize;
-    FILE *inFile;
-    Uint8 *inBuffer, *outBuffer;
+    FILE *inFile = NULL;
+    Uint8 *inBuffer = NULL, *outBuffer = NULL;
     DMPackEntry entry, *node;
-    int result;
+    int zres, ret = DMERR_OK;
+    BOOL zinit = FALSE;
 
     *ppEntry = NULL;
 
     if (pack == NULL)
-        return DMERR_OK;
+        return DMERR_NULLPTR;
 
     if (pack->file == NULL)
         return DMERR_FOPEN;
@@ -290,22 +291,14 @@
 
     // Read file data
     if ((inFile = fopen(filename, "rb")) == NULL)
-        return -1;
+        return DMERR_FOPEN;
 
     // Allocate temporary buffer
-    inBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE);
-    if (!inBuffer)
+    if ((inBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL ||
+        (outBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL)
     {
-        fclose(inFile);
-        return DMERR_MALLOC;
-    }
-
-    outBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE);
-    if (!outBuffer)
-    {
-        dmFree(inBuffer);
-        fclose(inFile);
-        return DMERR_MALLOC;
+        ret = DMERR_MALLOC;
+        goto out;
     }
 
     // Read (and possibly compress) the data
@@ -313,30 +306,33 @@
     zstr.zalloc = (alloc_func) Z_NULL;
     zstr.zfree = (free_func) Z_NULL;
     zstr.opaque = (voidpf) Z_NULL;
-    result = deflateInit(&zstr, (doCompress) ? Z_DEFAULT_COMPRESSION : 0);
-    if (result != Z_OK)
+    zres = deflateInit(&zstr, doCompress ? Z_DEFAULT_COMPRESSION : 0);
+    if (zres != Z_OK)
     {
-        dmFree(inBuffer);
-        dmFree(outBuffer);
-        fclose(inFile);
-        return DMERR_COMPRESSION;
+        ret = DMERR_COMPRESSION;
+        goto out;
     }
+    zinit = TRUE;
 
     // Initialize compression streams
-    result = Z_OK;
-    while (!feof(inFile) && result == Z_OK)
+    zres = Z_OK;
+    while (!feof(inFile) && zres == Z_OK)
     {
         zstr.avail_in = fread(inBuffer, sizeof(Uint8), DPACK_TMPSIZE, inFile);
         zstr.next_in = inBuffer;
         zstr.next_out = outBuffer;
         zstr.avail_out = DPACK_TMPSIZE;
         zstr.total_out = 0;
-        result = deflate(&zstr, Z_FULL_FLUSH);
+        zres = deflate(&zstr, Z_FULL_FLUSH);
 
-        if (result == Z_OK && zstr.total_out > 0)
+        if (zres == Z_OK && zstr.total_out > 0)
         {
             zstrSize += zstr.total_out;
-            fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file);
+            if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file) != zstr.total_out)
+            {
+                ret = DMERR_FWRITE;
+                goto out;
+            }
         }
     }
 
@@ -348,19 +344,28 @@
     entry.length = zstrSize;
     entry.flags  = flags;
 
+
+    // Add directory entry
+    if ((*ppEntry = dmPackEntryCopy(&entry)) == NULL)
+    {
+        ret = DMERR_MALLOC;
+        goto out;
+    }
+
+    dmPackEntryInsert(&pack->entries, *ppEntry);
+
+out:
     // Cleanup
-    deflateEnd(&zstr);
+    if (zinit)
+        deflateEnd(&zstr);
+
     dmFree(inBuffer);
     dmFree(outBuffer);
     fclose(inFile);
+    if (inFile != NULL)
+        fclose(inFile);
 
-    // Add directory entry
-    if ((*ppEntry = dmPackEntryCopy(&entry)) == NULL)
-        return DMERR_MALLOC;
-
-    dmPackEntryInsert(&pack->entries, *ppEntry);
-
-    return DMERR_OK;
+    return ret;
 }