changeset 1017:6436d80ae0fc

Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Mar 2015 04:47:43 +0200
parents b1d22289af24
children 782a2a3a3707
files src/dmres.c src/dmres.h
diffstat 2 files changed, 44 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/dmres.c	Sun Mar 01 04:46:41 2015 +0200
+++ b/src/dmres.c	Sun Mar 01 04:47:43 2015 +0200
@@ -409,36 +409,28 @@
         goto out;
     }
 
-    if ((ctx.zout = ctx.zoutStart = dmMalloc(node->size)) == NULL)
     {
-        ret = dmError(DMERR_MALLOC,
-            "Failed to allocate initial decompression buffer.\n");
-        goto out;
     }
 
     // Initialize decompression structures
     ctx.zbuffer    = inBuf;
     ctx.zbufferEnd = inBuf + node->length;
-    ctx.zoutEnd    = ctx.zoutStart + node->size;
-    ctx.expandable = TRUE;
 
+    ctx.zout       = ctx.zoutStart = handle->rawData;
+    ctx.zoutEnd    = handle->rawData + node->size;
+    ctx.expandable = FALSE;
+
+    // Attempt decompression
     if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK)
         goto out;
 
     if ((ret = dmZLibDecode(&ctx)) != DMERR_OK)
         goto out;
 
-    handle->rawOffset = 0;
     handle->rawData = ctx.zoutStart;
     handle->rawSize = ctx.zout - ctx.zoutStart;
-    if (handle->rawSize != node->size)
-    {
-        ret = dmError(DMERR_COMPRESSION,
-            "Decompressed data size for '%s' does not match size stored in PACK entry (%d <> %d).\n",
-            node->filename, handle->rawSize, node->size);
-    }
 
-err:
+out:
     dmFree(inBuf);
     return ret;
 }
@@ -471,7 +463,43 @@
         goto out;
     }
 
-    res = dm_pack_decompress(handle, node);
+    // Allocate memory for the node
+    if ((handle->rawData = dmMalloc(node->size)) == NULL)
+    {
+        ret = dmError(DMERR_MALLOC,
+            "Failed to allocate node data for '%s' (%d bytes).\n",
+            handle->filename, node->size);
+        goto out;
+    }
+
+    // Check if the entry is compressed
+    if (handle->flags & DMF_COMPRESSED)
+    {
+        if ((ret = dm_pack_decompress(handle, node)) != DMERR_OK)
+            goto out;
+
+        if (handle->rawSize != node->size)
+        {
+            ret = dmError(DMERR_COMPRESSION,
+                "Decompressed data size for '%s' does not match size stored in PACK entry (%d <> %d).\n",
+                handle->filename, handle->rawSize, node->size);
+        }
+    }
+    else
+    {
+        if (node->size != node->length)
+        {
+            ret = dmError(DMERR_INVALID_DATA,
+                "Node '%s' raw size and length fields differ for uncompressed node.\n", node->filename);
+            goto out;
+        }
+        if (fread(handle->rawData, sizeof(Uint8), node->size, handle->lib->packFile->file) != node->size)
+        {
+            ret = dmError(DMERR_FREAD,
+                "Error reading node data.\n");
+            goto out;
+        }
+    }
 
 out:
     return ret;
--- a/src/dmres.h	Sun Mar 01 04:46:41 2015 +0200
+++ b/src/dmres.h	Sun Mar 01 04:47:43 2015 +0200
@@ -41,6 +41,7 @@
     DMF_UNALLOCATED = 0x00004, // The raw data is not allocated, so do not free it
     DMF_LOADED_RAW  = 0x01000, // Raw data has been loaded
     DMF_LOADED_RES  = 0x02000, // Resource has been loaded
+    DMF_COMPRESSED  = 0x10000, // Resource is compressed in PACK file, otherwise raw
 };