Mercurial > hg > dmlib
comparison src/dmres.c @ 1015:5101766dd95c
Error handling consistency.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 01 Mar 2015 04:46:23 +0200 |
parents | 501ee5f0c043 |
children | b1d22289af24 |
comparison
equal
deleted
inserted
replaced
1014:501ee5f0c043 | 1015:5101766dd95c |
---|---|
337 #define DMRES_TMPBUF_SIZE (128 * 1024) | 337 #define DMRES_TMPBUF_SIZE (128 * 1024) |
338 | 338 |
339 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) | 339 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) |
340 { | 340 { |
341 int res = DMERR_OK, cres, cdataLeft; | 341 int res = DMERR_OK, cres, cdataLeft; |
342 z_stream cstream; | |
343 Uint8 * cbuffer = NULL; | 342 Uint8 * cbuffer = NULL; |
343 z_stream zstr; | |
344 BOOL zinit = FALSE; | |
344 | 345 |
345 // Allocate a structures and buffers | 346 // Allocate a structures and buffers |
346 cbuffer = (Uint8 *) dmMalloc(DMRES_TMPBUF_SIZE); | 347 if ((cbuffer = dmMalloc(DMRES_TMPBUF_SIZE)) == NULL) |
347 if (cbuffer == NULL) | |
348 { | 348 { |
349 res = DMERR_MALLOC; | 349 res = DMERR_MALLOC; |
350 goto error; | 350 goto out; |
351 } | |
352 | |
353 // Initialize fields | |
354 handle->rawOffset = 0; | |
355 handle->rawSize = node->size; | |
356 handle->rawData = (Uint8 *) dmMalloc(node->size); | |
357 if (handle->rawData == NULL) | |
358 { | |
359 res = DMERR_MALLOC; | |
360 goto error; | |
361 } | 351 } |
362 | 352 |
363 // Initialize decompression | 353 // Initialize decompression |
364 cstream.zalloc = (alloc_func) Z_NULL; | 354 memset(&zstr, 0, sizeof(zstr); |
365 cstream.zfree = (free_func) Z_NULL; | 355 zstr.next_out = handle->rawData; |
366 cstream.opaque = (voidpf) Z_NULL; | 356 zstr.avail_out = handle->rawSize; |
367 cstream.next_out = handle->rawData; | |
368 cstream.avail_out = handle->rawSize; | |
369 cdataLeft = node->length; | 357 cdataLeft = node->length; |
370 cres = inflateInit(&cstream); | 358 |
371 if (cres != Z_OK) | 359 if (inflateInit(&zstr) != Z_OK) |
372 { | 360 { |
373 res = dmError(DMERR_INIT_FAIL, | 361 res = dmError(DMERR_INIT_FAIL, |
374 "Could not initialize zlib stream inflation.\n"); | 362 "Could not initialize zlib stream decompression.\n"); |
375 goto error; | 363 goto out; |
376 } | 364 } |
365 zinit = TRUE; | |
377 | 366 |
378 // Uncompress the data | 367 // Uncompress the data |
379 while (cdataLeft > 0 && | 368 while (cdataLeft > 0 && zstr.avail_out > 0 && cres == Z_OK) |
380 cstream.avail_out > 0 && cres == Z_OK) | 369 { |
381 { | 370 zstr.avail_in = fread( |
382 cstream.avail_in = fread( | |
383 cbuffer, sizeof(Uint8), | 371 cbuffer, sizeof(Uint8), |
384 (cdataLeft >= DMRES_TMPBUF_SIZE) ? DMRES_TMPBUF_SIZE : cdataLeft, | 372 (cdataLeft >= DMRES_TMPBUF_SIZE) ? DMRES_TMPBUF_SIZE : cdataLeft, |
385 handle->lib->packFile->file); | 373 handle->lib->packFile->file); |
386 | 374 |
387 cdataLeft -= cstream.avail_in; | 375 cdataLeft -= zstr.avail_in; |
388 cstream.next_in = cbuffer; | 376 zstr.next_in = cbuffer; |
389 cres = inflate(&cstream, Z_FULL_FLUSH); | 377 cres = inflate(&zstr, Z_FULL_FLUSH); |
390 } | 378 } |
391 | 379 |
380 | |
381 out: | |
392 // Cleanup | 382 // Cleanup |
393 inflateEnd(&cstream); | 383 if (zinit) |
394 | 384 inflateEnd(&zstr); |
395 error: | 385 |
396 dmFree(cbuffer); | 386 dmFree(cbuffer); |
397 return res; | 387 return res; |
398 } | 388 } |
399 | 389 |
400 #else | 390 #else |