# HG changeset patch # User Matti Hamalainen # Date 1425131979 -7200 # Node ID e8de4fbc03b6b9f40a5cac7f1194c3ab4bd465a0 # Parent 5bcd219ddde360975248e516a47da60893eca6ec Clean up packed a bit. diff -r 5bcd219ddde3 -r e8de4fbc03b6 tools/packed.c --- a/tools/packed.c Sat Feb 28 05:43:31 2015 +0200 +++ b/tools/packed.c Sat Feb 28 15:59:39 2015 +0200 @@ -262,15 +262,16 @@ z_stream zstr; off_t startOffs; unsigned int zstrSize; - FILE *inFile; - Uint8 *inBuffer, *outBuffer; + FILE *inFile = NULL; + Uint8 *inBuffer = NULL, *outBuffer = NULL; DMPackEntry entry, *node; - int result; + int zres, ret = DMERR_OK; + BOOL zinit = FALSE; *ppEntry = NULL; if (pack == NULL) - return DMERR_OK; + return DMERR_NULLPTR; if (pack->file == NULL) return DMERR_FOPEN; @@ -290,22 +291,14 @@ // Read file data if ((inFile = fopen(filename, "rb")) == NULL) - return -1; + return DMERR_FOPEN; // Allocate temporary buffer - inBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE); - if (!inBuffer) + if ((inBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL || + (outBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL) { - fclose(inFile); - return DMERR_MALLOC; - } - - outBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE); - if (!outBuffer) - { - dmFree(inBuffer); - fclose(inFile); - return DMERR_MALLOC; + ret = DMERR_MALLOC; + goto out; } // Read (and possibly compress) the data @@ -313,30 +306,33 @@ zstr.zalloc = (alloc_func) Z_NULL; zstr.zfree = (free_func) Z_NULL; zstr.opaque = (voidpf) Z_NULL; - result = deflateInit(&zstr, (doCompress) ? Z_DEFAULT_COMPRESSION : 0); - if (result != Z_OK) + zres = deflateInit(&zstr, doCompress ? Z_DEFAULT_COMPRESSION : 0); + if (zres != Z_OK) { - dmFree(inBuffer); - dmFree(outBuffer); - fclose(inFile); - return DMERR_COMPRESSION; + ret = DMERR_COMPRESSION; + goto out; } + zinit = TRUE; // Initialize compression streams - result = Z_OK; - while (!feof(inFile) && result == Z_OK) + zres = Z_OK; + while (!feof(inFile) && zres == Z_OK) { zstr.avail_in = fread(inBuffer, sizeof(Uint8), DPACK_TMPSIZE, inFile); zstr.next_in = inBuffer; zstr.next_out = outBuffer; zstr.avail_out = DPACK_TMPSIZE; zstr.total_out = 0; - result = deflate(&zstr, Z_FULL_FLUSH); + zres = deflate(&zstr, Z_FULL_FLUSH); - if (result == Z_OK && zstr.total_out > 0) + if (zres == Z_OK && zstr.total_out > 0) { zstrSize += zstr.total_out; - fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file); + if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file) != zstr.total_out) + { + ret = DMERR_FWRITE; + goto out; + } } } @@ -348,19 +344,28 @@ entry.length = zstrSize; entry.flags = flags; + + // Add directory entry + if ((*ppEntry = dmPackEntryCopy(&entry)) == NULL) + { + ret = DMERR_MALLOC; + goto out; + } + + dmPackEntryInsert(&pack->entries, *ppEntry); + +out: // Cleanup - deflateEnd(&zstr); + if (zinit) + deflateEnd(&zstr); + dmFree(inBuffer); dmFree(outBuffer); fclose(inFile); + if (inFile != NULL) + fclose(inFile); - // Add directory entry - if ((*ppEntry = dmPackEntryCopy(&entry)) == NULL) - return DMERR_MALLOC; - - dmPackEntryInsert(&pack->entries, *ppEntry); - - return DMERR_OK; + return ret; }