Skip to content

Commit

Permalink
Merge pull request #237 from devnexen/deferutils_expand
Browse files Browse the repository at this point in the history
zip handling expand memutils usage
  • Loading branch information
ColinPitrat authored Jun 5, 2023
2 parents f1d3b05 + 2ec5eec commit 1074f59
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include "errors.h"
#include "log.h"
#include "memutils.h"

// TODO(cpitrat): refactoring
namespace zip
Expand All @@ -31,15 +32,15 @@ namespace zip
wCentralDirEntries = 0;
wCentralDirSize = 0;
dwCentralDirPosition = 0;
auto closure = [&]() { fclose(pfileObject); };
memutils::scope_exit<decltype(closure)> cs(closure);
do {
lFilePosition -= 256; // move backwards through ZIP file
if (fseek(pfileObject, lFilePosition, SEEK_END) != 0) {
fclose(pfileObject);
LOG_ERROR("Couldn't read zip file: " << zi->filename);
return ERR_FILE_BAD_ZIP; // exit if loading of data chunck failed
};
if (fread(pbGPBuffer, 256, 1, pfileObject) == 0) {
fclose(pfileObject);
LOG_ERROR("Couldn't read zip file: " << zi->filename);
return ERR_FILE_BAD_ZIP; // exit if loading of data chunck failed
}
Expand All @@ -55,17 +56,14 @@ namespace zip
}
} while (wCentralDirEntries == 0);
if (wCentralDirSize == 0) {
fclose(pfileObject);
LOG_ERROR("Couldn't read zip file (no central directory): " << zi->filename);
return ERR_FILE_BAD_ZIP; // exit if no central directory was found
}
if (fseek(pfileObject, dwCentralDirPosition, SEEK_SET) != 0) {
fclose(pfileObject);
LOG_ERROR("Couldn't read zip file: " << zi->filename);
return ERR_FILE_BAD_ZIP; // exit if seeking to the central directory failed
};
if (fread(pbGPBuffer, wCentralDirSize, 1, pfileObject) == 0) {
fclose(pfileObject);
LOG_ERROR("Couldn't read zip file: " << zi->filename);
return ERR_FILE_BAD_ZIP; // exit if reading the central directory failed
}
Expand All @@ -89,7 +87,6 @@ namespace zip
}
pbPtr += dwNextEntry;
}
fclose(pfileObject);

if (zi->filesOffsets.empty()) { // no files found?
LOG_ERROR("Empty zip file: " << zi->filename);
Expand Down Expand Up @@ -131,26 +128,26 @@ namespace zip
pfileIn = fopen(zi.filename.c_str(), "rb"); // open ZIP file for reading
if (pfileIn == nullptr) {
LOG_ERROR("Couldn't open zip file for reading: " << zi.filename);
fclose(*pfileOut);
return ERR_FILE_UNZIP_FAILED; // couldn't open input file
}
auto closure = [&]() { fclose(pfileIn); };
memutils::scope_exit<decltype(closure)> cs(closure);
if (fseek(pfileIn, dwOffset, SEEK_SET) != 0) { // move file pointer to beginning of data block
LOG_ERROR("Couldn't read zip file: " << zi.filename);
fclose(pfileIn);
fclose(*pfileOut);
return ERR_FILE_UNZIP_FAILED;
};
size_t rc;
if((rc = fread(pbGPBuffer, 30, 1, pfileIn)) != 1) { // read local header
LOG_ERROR("Couldn't read zip file: " << zi.filename);
fclose(pfileIn);
fclose(*pfileOut);
return ERR_FILE_UNZIP_FAILED;
}
dwSize = *reinterpret_cast<dword *>(pbGPBuffer + 18); // length of compressed data
dwOffset += 30 + *reinterpret_cast<word *>(pbGPBuffer + 26) + *reinterpret_cast<word *>(pbGPBuffer + 28);
if (fseek(pfileIn, dwOffset, SEEK_SET) != 0) { // move file pointer to start of compressed data
LOG_ERROR("Couldn't read zip file: " << zi.filename);
fclose(pfileIn);
fclose(*pfileOut);
return ERR_FILE_UNZIP_FAILED;
}
Expand All @@ -177,7 +174,6 @@ namespace zip
if (iCount) { // save data to file if some is available
if (fwrite(pbOutputBuffer, iCount, 1, *pfileOut) != 1) {
LOG_ERROR("Couldn't unzip file: Couldn't write to output file:");
fclose(pfileIn);
fclose(*pfileOut);
return ERR_FILE_UNZIP_FAILED;
}
Expand All @@ -190,7 +186,6 @@ namespace zip
return ERR_FILE_UNZIP_FAILED; // abort on error
}
iStatus = inflateEnd(&z); // clean up
fclose(pfileIn);
fseek(*pfileOut, 0, SEEK_SET);

return 0; // data was successfully decompressed
Expand Down

0 comments on commit 1074f59

Please sign in to comment.