changeset 1064:a6c5be712b53

Add dmzlib / zlib test utility.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 02 Mar 2015 01:49:14 +0200
parents ada47e30d0c9
children 2e997dd888b9
files Makefile.gen tests/testdmzlib.c
diffstat 2 files changed, 264 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.gen	Mon Mar 02 01:46:19 2015 +0200
+++ b/Makefile.gen	Mon Mar 02 01:49:14 2015 +0200
@@ -278,7 +278,7 @@
 
 ### What tests to build?
 ifeq ($(DM_BUILD_TESTS),yes)
-TESTS_BINARIES += vecmattest fptest evaltest
+TESTS_BINARIES += vecmattest fptest evaltest testdmzlib
 endif
 
 FONTCONV_BIN=fontconv
@@ -470,6 +470,10 @@
 	@echo " LINK $+"
 	@$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(SDL_LDFLAGS) $(DM_ZLIB_LDFLAGS) -lm
 
+$(TESTS_BINPATH)testdmzlib$(EXEEXT): $(OBJPATH)testdmzlib.o $(DMLIB_A)
+	@echo " LINK $+"
+	@$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(SDL_LDFLAGS) $(DM_ZLIB_LDFLAGS)
+
 
 ###
 ### Tools
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/testdmzlib.c	Mon Mar 02 01:49:14 2015 +0200
@@ -0,0 +1,259 @@
+#include "dmlib.h"
+#include "dmzlib.h"
+#include "dmfile.h"
+#include "dmargs.h"
+#include <zlib.h>
+
+
+#define SET_TMPBUF_SIZE (16 * 1024)
+
+char *optInFilename = NULL,
+    *optOutFilename = NULL;
+
+int optCompressLevel = Z_BEST_COMPRESSION;
+
+
+static const DMOptArg optList[] =
+{
+    {  0, '?', "help",        "Show this help", OPT_NONE },
+    {  1, 'q', "quiet",       "Decrease verbosity", OPT_NONE },
+};
+
+static const int optListN = sizeof(optList) / sizeof(optList[0]);
+
+
+void argShowHelp()
+{
+    dmPrintBanner(stdout, dmProgName, "[options] [input filename] [output filename]");
+    dmArgsPrintHelp(stdout, optList, optListN, 0);
+}
+
+
+BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
+{
+    (void) currArg;
+
+    switch (optN)
+    {
+        case 0:
+            argShowHelp();
+            exit(0);
+            break;
+
+        case 1:
+            dmVerbosity = 0;
+            break;
+
+        case 10:
+            optCompressLevel = atoi(optArg);
+            if (optCompressLevel < 1 || optCompressLevel > 9)
+            {
+                dmErrorMsg("Invalid compression level argument '%s', must be 1 .. 9.\n", optArg);
+                return FALSE;
+            }
+            break;
+
+        default:
+            return FALSE;
+    }
+
+    return TRUE;
+}
+
+
+BOOL argHandleFile(char *currArg)
+{
+    if (optInFilename == NULL)
+        optInFilename = currArg;
+    else
+    if (optOutFilename == NULL)
+        optOutFilename = currArg;
+    else
+    {
+        dmErrorMsg("Excess filenames specified.\n");
+        return FALSE;
+    }
+    return TRUE;
+}
+
+
+int dmTestDMZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize)
+{
+    Uint8 *inBuffer = NULL, *outBuffer = NULL;
+    DMZLibContext ctx;
+    int ret;
+
+    if ((ret = dmReadDataFile(inFile, "-", &inBuffer, inSize)) != DMERR_OK)
+    {
+        dmErrorMsg("Failed to read file.\n");
+        goto out;
+    }
+    
+    if ((outBuffer = dmMalloc(SET_TMPBUF_SIZE)) == NULL)
+    {
+        ret = dmError(DMERR_MALLOC,
+            "Malloc failed.\n");
+        goto out;
+    }
+
+    // Initialize decompression structures
+    ctx.zbuffer    = inBuffer;
+    ctx.zbufferEnd = inBuffer + *inSize;
+
+    ctx.zout       = ctx.zoutStart = outBuffer;
+    ctx.zoutEnd    = outBuffer + SET_TMPBUF_SIZE;
+    ctx.expandable = TRUE;
+
+    // Attempt decompression
+    if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK)
+    {
+        dmErrorMsg("Error parsing ZLIB header: %d, %s\n", ret, dmErrorStr(ret));
+        goto out;
+    }
+
+    if ((ret = dmZLibDecode(&ctx)) != DMERR_OK)
+    {
+        dmErrorMsg("Error in ZLIB decompress: %d, %s\n", ret, dmErrorStr(ret));
+        goto out;
+    }
+
+    outBuffer = ctx.zoutStart;
+    *outSize  = ctx.zout - ctx.zoutStart;
+
+    if (fwrite(outBuffer, sizeof(Uint8), *outSize, outFile) != *outSize)
+    {
+        ret = dmError(DMERR_FWRITE, "File write error.\n");
+        goto out;
+    }
+
+out:
+
+    dmFree(inBuffer);
+    dmFree(outBuffer);
+    return ret;
+}
+
+
+int dmTestZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize, BOOL compress, int level)
+{
+    Uint8 *inBuffer = NULL, *outBuffer = NULL;
+    int zret, zinit = FALSE;
+    int ret = DMERR_OK;
+    z_stream zstr;
+
+    dmMsg(0, "Operating mode: %s\n",
+        compress ? "compress" : "decompress");
+
+    if ((inBuffer = malloc(SET_TMPBUF_SIZE)) == NULL ||
+        (outBuffer = malloc(SET_TMPBUF_SIZE)) == NULL)
+    {
+        ret = dmError(DMERR_MALLOC, "Malloc failed.\n");
+        goto out;
+    }
+
+    memset(&zstr, 0, sizeof(zstr));
+    if (compress)
+        zret = deflateInit(&zstr, level);
+    else
+        zret = inflateInit(&zstr);
+
+    if (zret != Z_OK)
+    {
+        ret = dmError(DMERR_INIT_FAIL, "Zlib init fail.\n");
+        goto out;
+    }
+    zinit = TRUE;
+
+    // Initialize compression streams
+    zret = Z_OK;
+    *outSize = 0;
+    while (!feof(inFile) && zret == Z_OK)
+    {
+        zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile);
+
+        zstr.next_in = inBuffer;
+        zstr.next_out = outBuffer;
+        zstr.avail_out = SET_TMPBUF_SIZE;
+        zstr.total_out = 0;
+
+        if (compress)
+            zret = deflate(&zstr, Z_FULL_FLUSH);
+        else
+            zret = inflate(&zstr, Z_FULL_FLUSH);
+
+        if (zret == Z_OK && zstr.total_out > 0)
+        {
+            outSize += zstr.total_out;
+            if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile) != zstr.total_out)
+            {
+                ret = dmError(DMERR_FWRITE,
+                    "File write error.\n");
+                goto out;
+            }
+        }
+    }
+
+
+    switch (zret)
+    {
+        case Z_OK:
+            break;
+
+        case Z_ERRNO:
+            dmErrorMsg("Error: errno\n");
+            break;
+        
+        case Z_STREAM_END:
+            dmErrorMsg("Stream end.\n");
+            break;
+        
+        default:
+            ret = dmError(DMERR_COMPRESSION,
+                "Error: %d, %s\n", zret, zError(zret));
+    }
+
+out:
+    *inSize = zstr.total_in;
+
+    if (zinit)
+    {
+        if (compress)
+            deflateEnd(&zstr);
+        else
+            inflateEnd(&zstr);
+    }
+
+    dmFree(inBuffer);
+    dmFree(outBuffer);
+
+    return ret;
+}
+
+
+int main(int argc, char *argv[])
+{
+    FILE *inFile = NULL, *outFile = NULL;
+    size_t inSize, outSize;
+
+    dmInitProg("testdmzlib", "zlib/dmzlib test", NULL, NULL, NULL);
+    dmVerbosity = 1;
+
+    if (!dmArgsProcess(argc, argv, optList, optListN,
+        argHandleOpt, argHandleFile, OPTH_BAILOUT))
+        exit(1);
+
+    // Input and output files
+    inFile = stdin;
+    outFile = stdout;
+
+    dmMsg(0, "In: %d, out %d bytes.\n", inSize, outSize);
+
+    // Cleanup
+    if (outFile != NULL)
+        fclose(outFile);
+
+    if (inFile != NULL)
+        fclose(inFile);
+
+    return 0;
+}