diff src/stb_image.c @ 960:1832ac20edb2

Clean up dmzlib and use it in stb_image.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Feb 2015 04:46:13 +0200
parents 88d9440afad0
children 70bcb294a437
line wrap: on
line diff
--- a/src/stb_image.c	Fri Feb 27 04:44:48 2015 +0200
+++ b/src/stb_image.c	Fri Feb 27 04:46:13 2015 +0200
@@ -4151,6 +4151,44 @@
    }
 }
 
+
+int stbi_zlib_decode_malloc_guesssize_headerflag(
+    Uint8 *inBuf, size_t inLen,
+    Uint8 **outBuf, size_t *outLen,
+    size_t initialSize,
+    BOOL parseHeader)
+{
+    DMZLibContext ctx;
+    int ret;
+
+    if ((*outBuf = dmMalloc(initialSize)) == NULL)
+        return 0;
+
+    ctx.zbuffer    = (Uint8 *) inBuf;
+    ctx.zbufferEnd = (Uint8 *) inBuf + inLen;
+    ctx.zout       = *outBuf;
+    ctx.zoutStart  = *outBuf;
+    ctx.zoutEnd    = *outBuf + initialSize;
+    ctx.expandable = TRUE;
+
+    if (parseHeader && (ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK)
+    {
+        return dmError(ret,
+            "Failed to parse zlib header data.\n");
+    }
+
+    if ((ret = dmZLibDecode(&ctx)) != DMERR_OK)
+    {
+        dmFree(*outBuf);
+        return ret;
+    }
+    
+    *outLen = ctx.zout - ctx.zoutStart;
+    return DMERR_OK;
+}
+
+
+
 #define STBI__PNG_TYPE(a,b,c,d)  (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
 
 static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
@@ -4256,7 +4294,7 @@
          }
 
          case STBI__PNG_TYPE('I','E','N','D'): {
-            Uint32 raw_len, bpl;
+            size_t raw_len, bpl;
             if (first) return stbi__err("first not IHDR", "Corrupt PNG");
             if (scan != STBI__SCAN_load) return 1;
             if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG");
@@ -4264,7 +4302,11 @@
             bpl = (s->img_x * depth + 7) / 8; // bytes per line, per component
             raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;
 
-            z->expanded = (Uint8 *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone);
+            if (stbi_zlib_decode_malloc_guesssize_headerflag(
+                z->idata, ioff,
+                &z->expanded, &raw_len,
+                raw_len, !is_iphone) != DMERR_OK)
+                return 0;
 
             if (z->expanded == NULL) return 0; // zlib should set error
             STBI_FREE(z->idata); z->idata = NULL;