comparison src/dmres.c @ 1050:e4f60c944a2d

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Mar 2015 18:31:29 +0200
parents 509e6ed3a24e
children f2f4994c2f17
comparison
equal deleted inserted replaced
1049:771e03bf9fcd 1050:e4f60c944a2d
332 332
333 #define DMRES_TMPBUF_SIZE (128 * 1024) 333 #define DMRES_TMPBUF_SIZE (128 * 1024)
334 334
335 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) 335 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node)
336 { 336 {
337 int res = DMERR_OK, cres, cdataLeft; 337 int ret = DMERR_OK, zret, cdataLeft;
338 Uint8 * cbuffer = NULL; 338 Uint8 * cbuffer = NULL;
339 z_stream zstr; 339 z_stream zstr;
340 BOOL zinit = FALSE; 340 BOOL zinit = FALSE;
341 341
342 // Allocate a structures and buffers 342 // Allocate a structures and buffers
343 if ((cbuffer = dmMalloc(DMRES_TMPBUF_SIZE)) == NULL) 343 if ((cbuffer = dmMalloc(DMRES_TMPBUF_SIZE)) == NULL)
344 { 344 {
345 res = DMERR_MALLOC; 345 ret = DMERR_MALLOC;
346 goto out; 346 goto out;
347 } 347 }
348 348
349 // Initialize decompression 349 // Initialize decompression
350 memset(&zstr, 0, sizeof(zstr)); 350 memset(&zstr, 0, sizeof(zstr));
351 zstr.next_out = handle->rawData; 351 zstr.next_out = handle->rawData;
352 zstr.avail_out = handle->rawSize; 352 zstr.avail_out = handle->rawSize;
353 cdataLeft = node->length; 353 cdataLeft = node->length;
354 354
355 if (inflateInit(&zstr) != Z_OK) 355 if ((zret = inflateInit(&zstr)) != Z_OK)
356 { 356 {
357 res = dmErrorDBG(DMERR_INIT_FAIL, 357 ret = dmErrorDBG(DMERR_INIT_FAIL,
358 "Could not initialize zlib stream decompression.\n"); 358 "Could not initialize zlib stream decompression.\n");
359 goto out; 359 goto out;
360 } 360 }
361 zinit = TRUE; 361 zinit = TRUE;
362 362
363 // Uncompress the data 363 // Uncompress the data
364 while (cdataLeft > 0 && zstr.avail_out > 0 && cres == Z_OK) 364 while (cdataLeft > 0 && zstr.avail_out > 0 && zret == Z_OK)
365 { 365 {
366 zstr.avail_in = fread( 366 zstr.avail_in = fread(
367 cbuffer, sizeof(Uint8), 367 cbuffer, sizeof(Uint8),
368 (cdataLeft >= DMRES_TMPBUF_SIZE) ? DMRES_TMPBUF_SIZE : cdataLeft, 368 (cdataLeft >= DMRES_TMPBUF_SIZE) ? DMRES_TMPBUF_SIZE : cdataLeft,
369 handle->lib->packFile->file); 369 handle->lib->packFile->file);
370 370
371 cdataLeft -= zstr.avail_in; 371 cdataLeft -= zstr.avail_in;
372 zstr.next_in = cbuffer; 372 zstr.next_in = cbuffer;
373 cres = inflate(&zstr, Z_FULL_FLUSH); 373 zret = inflate(&zstr, Z_FULL_FLUSH);
374 } 374 }
375 375
376 376
377 out: 377 out:
378 // Cleanup 378 // Cleanup
379 if (zinit) 379 if (zinit)
380 inflateEnd(&zstr); 380 inflateEnd(&zstr);
381 381
382 dmFree(cbuffer); 382 dmFree(cbuffer);
383 return res; 383 return ret;
384 } 384 }
385 385
386 #else 386 #else
387 387
388 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) 388 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node)
403 { 403 {
404 ret = DMERR_FREAD; 404 ret = DMERR_FREAD;
405 goto out; 405 goto out;
406 } 406 }
407 407
408 { 408 // XXX TEMP DEBUG
409 size_t n, off;
410 for (off = 0; off < 400;)
411 {
412 for (n = 0; n < 16; n++)
413 printf("%02x ", inBuf[off + n]);
414
415 printf(" | ");
416 for (n = 0; n < 16; n++)
417 printf("%c", isprint(inBuf[off + n]) ? inBuf[off + n] : '.');
418
419 printf("\n");
420 off += n;
409 } 421 }
410 422
411 // Initialize decompression structures 423 // Initialize decompression structures
412 ctx.zbuffer = inBuf; 424 ctx.zbuffer = inBuf;
413 ctx.zbufferEnd = inBuf + node->length; 425 ctx.zbufferEnd = inBuf + node->length;