# HG changeset patch # User Matti Hamalainen # Date 1425177983 -7200 # Node ID 5101766dd95c0f144395a6dce3af5de59b50e3ca # Parent 501ee5f0c043fcc9a5f279a6ff967901011dd7af Error handling consistency. diff -r 501ee5f0c043 -r 5101766dd95c src/dmres.c --- a/src/dmres.c Sun Mar 01 04:45:23 2015 +0200 +++ b/src/dmres.c Sun Mar 01 04:46:23 2015 +0200 @@ -339,60 +339,50 @@ static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) { int res = DMERR_OK, cres, cdataLeft; - z_stream cstream; Uint8 * cbuffer = NULL; + z_stream zstr; + BOOL zinit = FALSE; // Allocate a structures and buffers - cbuffer = (Uint8 *) dmMalloc(DMRES_TMPBUF_SIZE); - if (cbuffer == NULL) + if ((cbuffer = dmMalloc(DMRES_TMPBUF_SIZE)) == NULL) { res = DMERR_MALLOC; - goto error; - } - - // Initialize fields - handle->rawOffset = 0; - handle->rawSize = node->size; - handle->rawData = (Uint8 *) dmMalloc(node->size); - if (handle->rawData == NULL) - { - res = DMERR_MALLOC; - goto error; + goto out; } // Initialize decompression - cstream.zalloc = (alloc_func) Z_NULL; - cstream.zfree = (free_func) Z_NULL; - cstream.opaque = (voidpf) Z_NULL; - cstream.next_out = handle->rawData; - cstream.avail_out = handle->rawSize; + memset(&zstr, 0, sizeof(zstr); + zstr.next_out = handle->rawData; + zstr.avail_out = handle->rawSize; cdataLeft = node->length; - cres = inflateInit(&cstream); - if (cres != Z_OK) + + if (inflateInit(&zstr) != Z_OK) { res = dmError(DMERR_INIT_FAIL, - "Could not initialize zlib stream inflation.\n"); - goto error; + "Could not initialize zlib stream decompression.\n"); + goto out; } + zinit = TRUE; // Uncompress the data - while (cdataLeft > 0 && - cstream.avail_out > 0 && cres == Z_OK) + while (cdataLeft > 0 && zstr.avail_out > 0 && cres == Z_OK) { - cstream.avail_in = fread( + zstr.avail_in = fread( cbuffer, sizeof(Uint8), (cdataLeft >= DMRES_TMPBUF_SIZE) ? DMRES_TMPBUF_SIZE : cdataLeft, handle->lib->packFile->file); - cdataLeft -= cstream.avail_in; - cstream.next_in = cbuffer; - cres = inflate(&cstream, Z_FULL_FLUSH); + cdataLeft -= zstr.avail_in; + zstr.next_in = cbuffer; + cres = inflate(&zstr, Z_FULL_FLUSH); } + +out: // Cleanup - inflateEnd(&cstream); + if (zinit) + inflateEnd(&zstr); -error: dmFree(cbuffer); return res; }