Mercurial > hg > dmlib
comparison src/dmres.c @ 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 | d66b0b6c8bf8 |
children | aeafd6d44465 |
comparison
equal
deleted
inserted
replaced
992:929e43afbdb1 | 993:5bcd219ddde3 |
---|---|
6 */ | 6 */ |
7 #include "dmres.h" | 7 #include "dmres.h" |
8 #include <time.h> | 8 #include <time.h> |
9 | 9 |
10 #ifdef DM_USE_PACKFS | 10 #ifdef DM_USE_PACKFS |
11 # include <zlib.h> | 11 # ifdef DM_USE_ZLIB |
12 # include <zlib.h> | |
13 # else | |
14 # include "dmzlib.h" | |
15 # endif | |
12 #endif | 16 #endif |
13 | 17 |
14 #ifdef DM_USE_STDIO | 18 #ifdef DM_USE_STDIO |
15 # include <sys/types.h> | 19 # include <sys/types.h> |
16 # include <sys/stat.h> | 20 # include <sys/stat.h> |
325 | 329 |
326 /* | 330 /* |
327 * PACK file routines | 331 * PACK file routines |
328 */ | 332 */ |
329 #ifdef DM_USE_PACKFS | 333 #ifdef DM_USE_PACKFS |
330 static int dm_pack_preload(DMResource *handle) | 334 |
331 { | 335 #ifdef DM_USE_ZLIB |
332 DMPackEntry *node; | 336 |
337 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) | |
338 { | |
333 int res = DMERR_OK, cres, cdataLeft; | 339 int res = DMERR_OK, cres, cdataLeft; |
334 z_stream cstream; | 340 z_stream cstream; |
335 Uint8 * cbuffer = NULL; | 341 Uint8 * cbuffer = NULL; |
336 | |
337 if (handle->lib == NULL || handle->lib->packFile == NULL) | |
338 return DMERR_NULLPTR; | |
339 | |
340 // Search PACK nodelist for file | |
341 if ((node = dmPackFind(handle->lib->packFile->entries, handle->filename)) == NULL) | |
342 { | |
343 res = dmError(DMERR_NOT_FOUND, | |
344 "Entry '%s' not found in PACK file.\n", | |
345 handle->filename); | |
346 goto error; | |
347 } | |
348 | |
349 // Seek to entry | |
350 if (fseek(handle->lib->packFile->file, node->offset, SEEK_SET) == -1) | |
351 { | |
352 res = dmError(DMERR_FSEEK, | |
353 "Could not seek node position in PACK file.\n"); | |
354 goto error; | |
355 } | |
356 | 342 |
357 // Allocate a structures and buffers | 343 // Allocate a structures and buffers |
358 cbuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE); | 344 cbuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE); |
359 if (cbuffer == NULL) | 345 if (cbuffer == NULL) |
360 { | 346 { |
377 cstream.zfree = (free_func) Z_NULL; | 363 cstream.zfree = (free_func) Z_NULL; |
378 cstream.opaque = (voidpf) Z_NULL; | 364 cstream.opaque = (voidpf) Z_NULL; |
379 cstream.next_out = handle->rawData; | 365 cstream.next_out = handle->rawData; |
380 cstream.avail_out = handle->rawSize; | 366 cstream.avail_out = handle->rawSize; |
381 cdataLeft = node->length; | 367 cdataLeft = node->length; |
382 cres = inflateInit(&(cstream)); | 368 cres = inflateInit(&cstream); |
383 if (cres != Z_OK) | 369 if (cres != Z_OK) |
384 { | 370 { |
385 res = dmError(DMERR_INIT_FAIL, | 371 res = dmError(DMERR_INIT_FAIL, |
386 "Could not initialize zlib stream inflation.\n"); | 372 "Could not initialize zlib stream inflation.\n"); |
387 goto error; | 373 goto error; |
400 cstream.next_in = cbuffer; | 386 cstream.next_in = cbuffer; |
401 cres = inflate(&cstream, Z_FULL_FLUSH); | 387 cres = inflate(&cstream, Z_FULL_FLUSH); |
402 } | 388 } |
403 | 389 |
404 // Cleanup | 390 // Cleanup |
405 inflateEnd(&(cstream)); | 391 inflateEnd(&cstream); |
406 | 392 |
407 error: | 393 error: |
408 dmFree(cbuffer); | 394 dmFree(cbuffer); |
395 return res; | |
396 } | |
397 | |
398 #else | |
399 | |
400 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) | |
401 { | |
402 DMZLibContext ctx; | |
403 Uint8 *inBuf = NULL; | |
404 int ret; | |
405 | |
406 if ((inBuf = dmMalloc(node->length)) == NULL) | |
407 { | |
408 ret = DMERR_MALLOC; | |
409 goto err; | |
410 } | |
411 | |
412 if (fread(inBuf, sizeof(Uint8), node->length, handle->lib->packFile->file) != node->length) | |
413 { | |
414 ret = DMERR_FREAD; | |
415 goto err; | |
416 } | |
417 | |
418 if ((ctx.zout = ctx.zoutStart = dmMalloc(node->size)) == NULL) | |
419 { | |
420 ret = dmError(DMERR_MALLOC, | |
421 "Failed to allocate initial decompression buffer.\n"); | |
422 goto err; | |
423 } | |
424 | |
425 ctx.zbuffer = inBuf; | |
426 ctx.zbufferEnd = inBuf + node->length; | |
427 ctx.zoutEnd = ctx.zoutStart + node->size; | |
428 ctx.expandable = TRUE; | |
429 | |
430 if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK) | |
431 goto err; | |
432 | |
433 if ((ret = dmZLibDecode(&ctx)) != DMERR_OK) | |
434 goto err; | |
435 | |
436 handle->rawOffset = 0; | |
437 handle->rawData = ctx.zoutStart; | |
438 handle->rawSize = ctx.zout - ctx.zoutStart; | |
439 if (handle->rawSize != node->size) | |
440 { | |
441 ret = dmError(DMERR_COMPRESSION, | |
442 "Decompressed data size for '%s' does not match size stored in PACK entry (%d <> %d).\n", | |
443 node->filename, handle->rawSize, node->size); | |
444 } | |
445 | |
446 err: | |
447 dmFree(inBuf); | |
448 return ret; | |
449 } | |
450 | |
451 #endif | |
452 | |
453 | |
454 static int dm_pack_preload(DMResource *handle) | |
455 { | |
456 DMPackEntry *node; | |
457 int res = DMERR_OK; | |
458 | |
459 if (handle->lib == NULL || handle->lib->packFile == NULL) | |
460 return DMERR_NULLPTR; | |
461 | |
462 // Search PACK nodelist for file | |
463 if ((node = dmPackFind(handle->lib->packFile->entries, handle->filename)) == NULL) | |
464 { | |
465 res = dmError(DMERR_NOT_FOUND, | |
466 "Entry '%s' not found in PACK file.\n", | |
467 handle->filename); | |
468 goto error; | |
469 } | |
470 | |
471 // Seek to entry | |
472 if (fseek(handle->lib->packFile->file, node->offset, SEEK_SET) == -1) | |
473 { | |
474 res = dmError(DMERR_FSEEK, | |
475 "Could not seek node position in PACK file.\n"); | |
476 goto error; | |
477 } | |
478 | |
479 res = dm_pack_decompress(handle, node); | |
480 | |
481 error: | |
409 return res; | 482 return res; |
410 } | 483 } |
411 | 484 |
412 | 485 |
413 static int dm_pack_fopen(DMResource * f) | 486 static int dm_pack_fopen(DMResource * f) |