changeset 1015:5101766dd95c

Error handling consistency.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Mar 2015 04:46:23 +0200
parents 501ee5f0c043
children b1d22289af24
files src/dmres.c
diffstat 1 files changed, 21 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/dmres.c	Sun Mar 01 04:45:23 2015 +0200
+++ b/src/dmres.c	Sun Mar 01 04:46:23 2015 +0200
@@ -339,60 +339,50 @@
 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node)
 {
     int res = DMERR_OK, cres, cdataLeft;
-    z_stream cstream;
     Uint8 * cbuffer = NULL;
+    z_stream zstr;
+    BOOL zinit = FALSE;
 
     // Allocate a structures and buffers
-    cbuffer = (Uint8 *) dmMalloc(DMRES_TMPBUF_SIZE);
-    if (cbuffer == NULL)
+    if ((cbuffer = dmMalloc(DMRES_TMPBUF_SIZE)) == NULL)
     {
         res = DMERR_MALLOC;
-        goto error;
-    }
-
-    // Initialize fields
-    handle->rawOffset = 0;
-    handle->rawSize = node->size;
-    handle->rawData = (Uint8 *) dmMalloc(node->size);
-    if (handle->rawData == NULL)
-    {
-        res = DMERR_MALLOC;
-        goto error;
+        goto out;
     }
 
     // Initialize decompression
-    cstream.zalloc = (alloc_func) Z_NULL;
-    cstream.zfree = (free_func) Z_NULL;
-    cstream.opaque = (voidpf) Z_NULL;
-    cstream.next_out = handle->rawData;
-    cstream.avail_out = handle->rawSize;
+    memset(&zstr, 0, sizeof(zstr);
+    zstr.next_out = handle->rawData;
+    zstr.avail_out = handle->rawSize;
     cdataLeft = node->length;
-    cres = inflateInit(&cstream);
-    if (cres != Z_OK)
+
+    if (inflateInit(&zstr) != Z_OK)
     {
         res = dmError(DMERR_INIT_FAIL,
-            "Could not initialize zlib stream inflation.\n");
-        goto error;
+            "Could not initialize zlib stream decompression.\n");
+        goto out;
     }
+    zinit = TRUE;
 
     // Uncompress the data
-    while (cdataLeft > 0 &&
-           cstream.avail_out > 0 && cres == Z_OK)
+    while (cdataLeft > 0 && zstr.avail_out > 0 && cres == Z_OK)
     {
-        cstream.avail_in = fread(
+        zstr.avail_in = fread(
             cbuffer, sizeof(Uint8),
             (cdataLeft >= DMRES_TMPBUF_SIZE) ? DMRES_TMPBUF_SIZE : cdataLeft,
             handle->lib->packFile->file);
 
-        cdataLeft -= cstream.avail_in;
-        cstream.next_in = cbuffer;
-        cres = inflate(&cstream, Z_FULL_FLUSH);
+        cdataLeft -= zstr.avail_in;
+        zstr.next_in = cbuffer;
+        cres = inflate(&zstr, Z_FULL_FLUSH);
     }
 
+
+out:
     // Cleanup
-    inflateEnd(&cstream);
+    if (zinit)
+        inflateEnd(&zstr);
 
-error:
     dmFree(cbuffer);
     return res;
 }