# HG changeset patch # User Matti Hamalainen # Date 1425095011 -7200 # Node ID 5bcd219ddde360975248e516a47da60893eca6ec # Parent 929e43afbdb1acc6468151c6c842e713d1c7dc0e Some work towards dmzlib decompression support for resources. Not working yet. diff -r 929e43afbdb1 -r 5bcd219ddde3 src/dmres.c --- 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 #ifdef DM_USE_PACKFS -# include +# ifdef DM_USE_ZLIB +# include +# 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) {