changeset 997:cd0e0270e1ce

Cosmetics.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Mar 2015 02:58:53 +0200
parents acda8cfc9119
children 4a68f8d0adc5
files src/dmzlib.c src/dmzlib.h
diffstat 2 files changed, 44 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/src/dmzlib.c	Sun Mar 01 02:40:21 2015 +0200
+++ b/src/dmzlib.c	Sun Mar 01 02:58:53 2015 +0200
@@ -116,7 +116,7 @@
                     k += (1 << s);
                 }
             }
-            ++next_code[s];
+            next_code[s]++;
         }
     }
 
@@ -425,8 +425,9 @@
     Uint8 header[4];
     int len, nlen, k, ret;
 
+    // "Any bits of input up to the next byte boundary are ignored."
     if (ctx->numBits & 7)
-        dmZReceive(ctx, ctx->numBits & 7);     // discard
+        dmZReceive(ctx, ctx->numBits & 7);
 
     // drain the bit-packed data into header
     k = 0;
@@ -462,7 +463,7 @@
         (ret = dmZLibExpand(ctx, ctx->zout, len)) != DMERR_OK)
     {
         return dmError(DMERR_DATA_ERROR,
-            "Failed to decompress enough data: %d, %s\n",
+            "Could not expand output buffer: %d, %s\n",
             ret, dmErrorStr(ret));
     }
 
@@ -474,37 +475,6 @@
 }
 
 
-int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG)
-{
-    int cmf = dmZGet8(ctx);
-    int flags = dmZGet8(ctx);
-    int cm = cmf & 15;
-    // cinfo = cmf >> 4;
-    // window = 1 << (8 + cinfo)... but who cares, we fully buffer output
-
-    if ((cmf * 256 + flags) % 31 != 0)
-    {
-        return dmError(DMERR_INVALID_DATA,
-            "Bad zlib header.");
-    }
-
-    if (checkPNG && (flags & 32))
-    {
-        // preset dictionary not allowed in png
-        return dmError(DMERR_NOT_SUPPORTED,
-            "Preset dictionary not allowed in PNG.\n");
-    }
-
-    if (checkPNG && cm != 8)
-    {
-        return dmError(DMERR_INVALID_DATA,
-            "Bad or unsupported compression type.\n");
-    }
-
-    return DMERR_OK;
-}
-
-
 int dmZLibDecode(DMZLibContext * ctx)
 {
     int final, type, ret;
@@ -546,3 +516,40 @@
 
     return DMERR_OK;
 }
+
+
+int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG)
+{
+    // See http://tools.ietf.org/html/rfc1950
+    int cmf     = dmZGet8(ctx); // Compression method and flags
+    int flags   = dmZGet8(ctx); // Flags
+    int cmethod = (cmf & 15);
+    int cinfo   = (cmf >> 4) & 15;
+    ctx->window = 1 << (8 + cinfo); // Window size (not used at the moment)
+
+    // "The FCHECK value must be such that CMF and FLG, when viewed as
+    // a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG),
+    // is a multiple of 31."
+    if ((cmf * 256 + flags) % 31 != 0)
+    {
+        return dmError(DMERR_INVALID_DATA,
+            "Bad zlib header.");
+    }
+
+    // We only support compression method 8
+    if (cmethod != 8)
+    {
+        return dmError(DMERR_INVALID_DATA,
+            "Bad or unsupported zlib compression method %d.\n",
+            cmethod);
+    }
+
+    if (checkPNG && (flags & 32))
+    {
+        // preset dictionary not allowed in png
+        return dmError(DMERR_NOT_SUPPORTED,
+            "Preset dictionary not allowed in PNG.\n");
+    }
+    return DMERR_OK;
+}
+
--- a/src/dmzlib.h	Sun Mar 01 02:40:21 2015 +0200
+++ b/src/dmzlib.h	Sun Mar 01 02:58:53 2015 +0200
@@ -44,9 +44,9 @@
 } DMZLibContext;
 
 
-void dmZLibInit();
-int  dmZLibDecode(DMZLibContext * ctx);
-int  dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG);
+void   dmZLibInit();
+int    dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG);
+int    dmZLibDecode(DMZLibContext * ctx);
 
 
 #ifdef __cplusplus