changeset 1062:d36bf7514614

Add some constants and make initialization one-time only.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 02 Mar 2015 00:03:13 +0200
parents 43905dacb365
children ada47e30d0c9
files src/dmzlib.c src/dmzlib.h
diffstat 2 files changed, 13 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/dmzlib.c	Mon Mar 02 00:02:24 2015 +0200
+++ b/src/dmzlib.c	Mon Mar 02 00:03:13 2015 +0200
@@ -26,8 +26,8 @@
     int i;
 
     // Use <= to match clearly with DEFLATE spec
-    if ((dm_zdefault_length   = dmMalloc(288)) == NULL ||
-        (dm_zdefault_distance = dmMalloc(32)) == NULL)
+    if ((dm_zdefault_length   = dmMalloc(DM_ZLIB_HUFF_CODES)) == NULL ||
+        (dm_zdefault_distance = dmMalloc(DM_ZLIB_HUFF_DIST)) == NULL)
     {
         return dmErrorDBG(DMERR_MALLOC,
             "Failed to allocate zlib decompression tables.\n");
@@ -150,6 +150,7 @@
         }
     }
 
+    ctx->initialized = TRUE;
     return DMERR_OK;
 }
 
@@ -527,13 +528,13 @@
             return DMERR_INVALID_DATA;
         else
         {
-            if (type == 1)
+            if (type == 1 && !ctx->zlength.initialized)
             {
                 // use fixed code lengths
-                if ((ret = dmZLibBuildHuffmanTables(&ctx->zlength, dm_zdefault_length, 288)) != DMERR_OK)
+                if ((ret = dmZLibBuildHuffmanTables(&ctx->zlength, dm_zdefault_length, DM_ZLIB_HUFF_CODES)) != DMERR_OK)
                     return ret;
 
-                if ((ret = dmZLibBuildHuffmanTables(&ctx->zdistance, dm_zdefault_distance, 32)) != DMERR_OK)
+                if ((ret = dmZLibBuildHuffmanTables(&ctx->zdistance, dm_zdefault_distance, DM_ZLIB_HUFF_DIST)) != DMERR_OK)
                     return ret;
             }
             else
--- a/src/dmzlib.h	Mon Mar 02 00:02:24 2015 +0200
+++ b/src/dmzlib.h	Mon Mar 02 00:03:13 2015 +0200
@@ -18,15 +18,20 @@
 #define DM_ZLIB_HFAST_SIZE  (1 << DM_ZLIB_HFAST_BITS)
 #define DM_ZLIB_HFAST_MASK  (DM_ZLIB_HFAST_SIZE - 1)
 
+#define DM_ZLIB_HUFF_CODES  (288)
+#define DM_ZLIB_HUFF_DIST   (32)
 
 typedef struct
 {
+    BOOL    initialized;
+
     Uint16  fast[DM_ZLIB_HFAST_SIZE];
     Uint16  firstCode[16];
     int     maxCode[16 + 1];
     Uint16  firstSymbol[16];
-    Uint8   size[288];
-    Uint16  value[288];
+
+    Uint8   size[DM_ZLIB_HUFF_CODES];
+    Uint16  value[DM_ZLIB_HUFF_CODES];
 } DMZHuffmanContext;