Mercurial > hg > dmlib
changeset 993:5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Not working yet.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 28 Feb 2015 05:43:31 +0200 |
parents | 929e43afbdb1 |
children | e8de4fbc03b6 |
files | src/dmres.c |
diffstat | 1 files changed, 99 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/src/dmres.c Sat Feb 28 05:06:55 2015 +0200 +++ b/src/dmres.c Sat Feb 28 05:43:31 2015 +0200 @@ -8,7 +8,11 @@ #include <time.h> #ifdef DM_USE_PACKFS -# include <zlib.h> +# ifdef DM_USE_ZLIB +# include <zlib.h> +# else +# include "dmzlib.h" +# endif #endif #ifdef DM_USE_STDIO @@ -327,32 +331,14 @@ * PACK file routines */ #ifdef DM_USE_PACKFS -static int dm_pack_preload(DMResource *handle) + +#ifdef DM_USE_ZLIB + +static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) { - DMPackEntry *node; int res = DMERR_OK, cres, cdataLeft; z_stream cstream; - Uint8 * cbuffer = NULL; - - if (handle->lib == NULL || handle->lib->packFile == NULL) - return DMERR_NULLPTR; - - // Search PACK nodelist for file - if ((node = dmPackFind(handle->lib->packFile->entries, handle->filename)) == NULL) - { - res = dmError(DMERR_NOT_FOUND, - "Entry '%s' not found in PACK file.\n", - handle->filename); - goto error; - } - - // Seek to entry - if (fseek(handle->lib->packFile->file, node->offset, SEEK_SET) == -1) - { - res = dmError(DMERR_FSEEK, - "Could not seek node position in PACK file.\n"); - goto error; - } + Uint8 * cbuffer = NULL; // Allocate a structures and buffers cbuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE); @@ -379,7 +365,7 @@ cstream.next_out = handle->rawData; cstream.avail_out = handle->rawSize; cdataLeft = node->length; - cres = inflateInit(&(cstream)); + cres = inflateInit(&cstream); if (cres != Z_OK) { res = dmError(DMERR_INIT_FAIL, @@ -402,13 +388,100 @@ } // Cleanup - inflateEnd(&(cstream)); + inflateEnd(&cstream); error: dmFree(cbuffer); return res; } +#else + +static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) +{ + DMZLibContext ctx; + Uint8 *inBuf = NULL; + int ret; + + if ((inBuf = dmMalloc(node->length)) == NULL) + { + ret = DMERR_MALLOC; + goto err; + } + + if (fread(inBuf, sizeof(Uint8), node->length, handle->lib->packFile->file) != node->length) + { + ret = DMERR_FREAD; + goto err; + } + + if ((ctx.zout = ctx.zoutStart = dmMalloc(node->size)) == NULL) + { + ret = dmError(DMERR_MALLOC, + "Failed to allocate initial decompression buffer.\n"); + goto err; + } + + ctx.zbuffer = inBuf; + ctx.zbufferEnd = inBuf + node->length; + ctx.zoutEnd = ctx.zoutStart + node->size; + ctx.expandable = TRUE; + + if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK) + goto err; + + if ((ret = dmZLibDecode(&ctx)) != DMERR_OK) + goto err; + + 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: + dmFree(inBuf); + return ret; +} + +#endif + + +static int dm_pack_preload(DMResource *handle) +{ + DMPackEntry *node; + int res = DMERR_OK; + + if (handle->lib == NULL || handle->lib->packFile == NULL) + return DMERR_NULLPTR; + + // Search PACK nodelist for file + if ((node = dmPackFind(handle->lib->packFile->entries, handle->filename)) == NULL) + { + res = dmError(DMERR_NOT_FOUND, + "Entry '%s' not found in PACK file.\n", + handle->filename); + goto error; + } + + // Seek to entry + if (fseek(handle->lib->packFile->file, node->offset, SEEK_SET) == -1) + { + res = dmError(DMERR_FSEEK, + "Could not seek node position in PACK file.\n"); + goto error; + } + + res = dm_pack_decompress(handle, node); + +error: + return res; +} + static int dm_pack_fopen(DMResource * f) {