Mercurial > hg > dmlib
comparison tools/packed.c @ 994:e8de4fbc03b6
Clean up packed a bit.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 28 Feb 2015 15:59:39 +0200 |
parents | 985225a93aeb |
children | acda8cfc9119 |
comparison
equal
deleted
inserted
replaced
993:5bcd219ddde3 | 994:e8de4fbc03b6 |
---|---|
260 BOOL doCompress, const Uint32 flags, DMPackEntry ** ppEntry) | 260 BOOL doCompress, const Uint32 flags, DMPackEntry ** ppEntry) |
261 { | 261 { |
262 z_stream zstr; | 262 z_stream zstr; |
263 off_t startOffs; | 263 off_t startOffs; |
264 unsigned int zstrSize; | 264 unsigned int zstrSize; |
265 FILE *inFile; | 265 FILE *inFile = NULL; |
266 Uint8 *inBuffer, *outBuffer; | 266 Uint8 *inBuffer = NULL, *outBuffer = NULL; |
267 DMPackEntry entry, *node; | 267 DMPackEntry entry, *node; |
268 int result; | 268 int zres, ret = DMERR_OK; |
269 BOOL zinit = FALSE; | |
269 | 270 |
270 *ppEntry = NULL; | 271 *ppEntry = NULL; |
271 | 272 |
272 if (pack == NULL) | 273 if (pack == NULL) |
273 return DMERR_OK; | 274 return DMERR_NULLPTR; |
274 | 275 |
275 if (pack->file == NULL) | 276 if (pack->file == NULL) |
276 return DMERR_FOPEN; | 277 return DMERR_FOPEN; |
277 | 278 |
278 // Compute starting offset | 279 // Compute starting offset |
288 if (fseek(pack->file, startOffs, SEEK_SET) != 0) | 289 if (fseek(pack->file, startOffs, SEEK_SET) != 0) |
289 return DMERR_INVALID; | 290 return DMERR_INVALID; |
290 | 291 |
291 // Read file data | 292 // Read file data |
292 if ((inFile = fopen(filename, "rb")) == NULL) | 293 if ((inFile = fopen(filename, "rb")) == NULL) |
293 return -1; | 294 return DMERR_FOPEN; |
294 | 295 |
295 // Allocate temporary buffer | 296 // Allocate temporary buffer |
296 inBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE); | 297 if ((inBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL || |
297 if (!inBuffer) | 298 (outBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE)) == NULL) |
298 { | 299 { |
299 fclose(inFile); | 300 ret = DMERR_MALLOC; |
300 return DMERR_MALLOC; | 301 goto out; |
301 } | |
302 | |
303 outBuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE); | |
304 if (!outBuffer) | |
305 { | |
306 dmFree(inBuffer); | |
307 fclose(inFile); | |
308 return DMERR_MALLOC; | |
309 } | 302 } |
310 | 303 |
311 // Read (and possibly compress) the data | 304 // Read (and possibly compress) the data |
312 zstrSize = 0; | 305 zstrSize = 0; |
313 zstr.zalloc = (alloc_func) Z_NULL; | 306 zstr.zalloc = (alloc_func) Z_NULL; |
314 zstr.zfree = (free_func) Z_NULL; | 307 zstr.zfree = (free_func) Z_NULL; |
315 zstr.opaque = (voidpf) Z_NULL; | 308 zstr.opaque = (voidpf) Z_NULL; |
316 result = deflateInit(&zstr, (doCompress) ? Z_DEFAULT_COMPRESSION : 0); | 309 zres = deflateInit(&zstr, doCompress ? Z_DEFAULT_COMPRESSION : 0); |
317 if (result != Z_OK) | 310 if (zres != Z_OK) |
318 { | 311 { |
319 dmFree(inBuffer); | 312 ret = DMERR_COMPRESSION; |
320 dmFree(outBuffer); | 313 goto out; |
321 fclose(inFile); | 314 } |
322 return DMERR_COMPRESSION; | 315 zinit = TRUE; |
323 } | |
324 | 316 |
325 // Initialize compression streams | 317 // Initialize compression streams |
326 result = Z_OK; | 318 zres = Z_OK; |
327 while (!feof(inFile) && result == Z_OK) | 319 while (!feof(inFile) && zres == Z_OK) |
328 { | 320 { |
329 zstr.avail_in = fread(inBuffer, sizeof(Uint8), DPACK_TMPSIZE, inFile); | 321 zstr.avail_in = fread(inBuffer, sizeof(Uint8), DPACK_TMPSIZE, inFile); |
330 zstr.next_in = inBuffer; | 322 zstr.next_in = inBuffer; |
331 zstr.next_out = outBuffer; | 323 zstr.next_out = outBuffer; |
332 zstr.avail_out = DPACK_TMPSIZE; | 324 zstr.avail_out = DPACK_TMPSIZE; |
333 zstr.total_out = 0; | 325 zstr.total_out = 0; |
334 result = deflate(&zstr, Z_FULL_FLUSH); | 326 zres = deflate(&zstr, Z_FULL_FLUSH); |
335 | 327 |
336 if (result == Z_OK && zstr.total_out > 0) | 328 if (zres == Z_OK && zstr.total_out > 0) |
337 { | 329 { |
338 zstrSize += zstr.total_out; | 330 zstrSize += zstr.total_out; |
339 fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file); | 331 if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file) != zstr.total_out) |
332 { | |
333 ret = DMERR_FWRITE; | |
334 goto out; | |
335 } | |
340 } | 336 } |
341 } | 337 } |
342 | 338 |
343 // Create directory entry | 339 // Create directory entry |
344 strncpy(entry.filename, filename, sizeof(entry.filename)); | 340 strncpy(entry.filename, filename, sizeof(entry.filename)); |
346 entry.size = zstr.total_in; | 342 entry.size = zstr.total_in; |
347 entry.offset = startOffs; | 343 entry.offset = startOffs; |
348 entry.length = zstrSize; | 344 entry.length = zstrSize; |
349 entry.flags = flags; | 345 entry.flags = flags; |
350 | 346 |
347 | |
348 // Add directory entry | |
349 if ((*ppEntry = dmPackEntryCopy(&entry)) == NULL) | |
350 { | |
351 ret = DMERR_MALLOC; | |
352 goto out; | |
353 } | |
354 | |
355 dmPackEntryInsert(&pack->entries, *ppEntry); | |
356 | |
357 out: | |
351 // Cleanup | 358 // Cleanup |
352 deflateEnd(&zstr); | 359 if (zinit) |
360 deflateEnd(&zstr); | |
361 | |
353 dmFree(inBuffer); | 362 dmFree(inBuffer); |
354 dmFree(outBuffer); | 363 dmFree(outBuffer); |
355 fclose(inFile); | 364 fclose(inFile); |
356 | 365 if (inFile != NULL) |
357 // Add directory entry | 366 fclose(inFile); |
358 if ((*ppEntry = dmPackEntryCopy(&entry)) == NULL) | 367 |
359 return DMERR_MALLOC; | 368 return ret; |
360 | |
361 dmPackEntryInsert(&pack->entries, *ppEntry); | |
362 | |
363 return DMERR_OK; | |
364 } | 369 } |
365 | 370 |
366 | 371 |
367 /* | 372 /* |
368 * EXTRACT a file from the PACK | 373 * EXTRACT a file from the PACK |