comparison tools/packed.c @ 996:acda8cfc9119

Also here.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Mar 2015 02:40:21 +0200
parents e8de4fbc03b6
children d190ebba813e
comparison
equal deleted inserted replaced
995:aeafd6d44465 996:acda8cfc9119
12 #include <zlib.h> 12 #include <zlib.h>
13 13
14 14
15 #define SET_MAX_FILES (4096) 15 #define SET_MAX_FILES (4096)
16 #define SET_DEFAULT_PACK "data.pak" 16 #define SET_DEFAULT_PACK "data.pak"
17 17 #define SET_TMPBUF_SIZE (128 * 1024)
18 18
19 enum 19 enum
20 { 20 {
21 CMD_NONE = 0, 21 CMD_NONE = 0,
22 CMD_CREATE, 22 CMD_CREATE,
292 // Read file data 292 // Read file data
293 if ((inFile = fopen(filename, "rb")) == NULL) 293 if ((inFile = fopen(filename, "rb")) == NULL)
294 return DMERR_FOPEN; 294 return DMERR_FOPEN;
295 295
296 // Allocate temporary buffer 296 // Allocate temporary buffer
297 if ((inBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL || 297 if ((inBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL ||
298 (outBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL) 298 (outBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL)
299 { 299 {
300 ret = DMERR_MALLOC; 300 ret = DMERR_MALLOC;
301 goto out; 301 goto out;
302 } 302 }
303 303
316 316
317 // Initialize compression streams 317 // Initialize compression streams
318 zres = Z_OK; 318 zres = Z_OK;
319 while (!feof(inFile) && zres == Z_OK) 319 while (!feof(inFile) && zres == Z_OK)
320 { 320 {
321 zstr.avail_in = fread(inBuffer, sizeof(Uint8), DPACK_TMPSIZE, inFile); 321 zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile);
322 zstr.next_in = inBuffer; 322 zstr.next_in = inBuffer;
323 zstr.next_out = outBuffer; 323 zstr.next_out = outBuffer;
324 zstr.avail_out = DPACK_TMPSIZE; 324 zstr.avail_out = SET_TMPBUF_SIZE;
325 zstr.total_out = 0; 325 zstr.total_out = 0;
326 zres = deflate(&zstr, Z_FULL_FLUSH); 326 zres = deflate(&zstr, Z_FULL_FLUSH);
327 327
328 if (zres == Z_OK && zstr.total_out > 0) 328 if (zres == Z_OK && zstr.total_out > 0)
329 { 329 {
393 // Open destination file 393 // Open destination file
394 if ((outFile = fopen(entry->filename, "wb")) == NULL) 394 if ((outFile = fopen(entry->filename, "wb")) == NULL)
395 return -1; 395 return -1;
396 396
397 // Allocate temporary buffer 397 // Allocate temporary buffer
398 if ((inBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL) 398 if ((inBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL)
399 { 399 {
400 fclose(outFile); 400 fclose(outFile);
401 return DMERR_MALLOC; 401 return DMERR_MALLOC;
402 } 402 }
403 403
404 if ((outBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL) 404 if ((outBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL)
405 { 405 {
406 dmFree(inBuffer); 406 dmFree(inBuffer);
407 fclose(outFile); 407 fclose(outFile);
408 return DMERR_MALLOC; 408 return DMERR_MALLOC;
409 } 409 }
424 // Initialize compression streams 424 // Initialize compression streams
425 inDataLeft = entry->length; 425 inDataLeft = entry->length;
426 ret = Z_OK; 426 ret = Z_OK;
427 while (inDataLeft > 0 && ret == Z_OK) 427 while (inDataLeft > 0 && ret == Z_OK)
428 { 428 {
429 if (inDataLeft >= DPACK_TMPSIZE) 429 if (inDataLeft >= SET_TMPBUF_SIZE)
430 zstr.avail_in = fread(inBuffer, sizeof(Uint8), DPACK_TMPSIZE, pack->file); 430 zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, pack->file);
431 else 431 else
432 zstr.avail_in = fread(inBuffer, sizeof(Uint8), inDataLeft, pack->file); 432 zstr.avail_in = fread(inBuffer, sizeof(Uint8), inDataLeft, pack->file);
433 433
434 inDataLeft -= zstr.avail_in; 434 inDataLeft -= zstr.avail_in;
435 zstr.next_in = inBuffer; 435 zstr.next_in = inBuffer;
436 436
437 while (zstr.avail_in > 0 && ret == Z_OK) 437 while (zstr.avail_in > 0 && ret == Z_OK)
438 { 438 {
439 zstr.next_out = outBuffer; 439 zstr.next_out = outBuffer;
440 zstr.avail_out = DPACK_TMPSIZE; 440 zstr.avail_out = SET_TMPBUF_SIZE;
441 zstr.total_out = 0; 441 zstr.total_out = 0;
442 ret = inflate(&zstr, Z_FULL_FLUSH); 442 ret = inflate(&zstr, Z_FULL_FLUSH);
443 if (zstr.total_out > 0) 443 if (zstr.total_out > 0)
444 { 444 {
445 fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile); 445 fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile);