diff options
Diffstat (limited to '')
-rw-r--r-- | minzip/Android.mk | 2 | ||||
-rw-r--r-- | minzip/DirUtil.cpp (renamed from minzip/DirUtil.c) | 66 | ||||
-rw-r--r-- | minzip/DirUtil.h | 3 | ||||
-rw-r--r-- | minzip/Zip.c | 26 | ||||
-rw-r--r-- | minzip/Zip.h | 13 |
5 files changed, 46 insertions, 64 deletions
diff --git a/minzip/Android.mk b/minzip/Android.mk index 22eabfbb1..3d36fd64e 100644 --- a/minzip/Android.mk +++ b/minzip/Android.mk @@ -4,7 +4,7 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ Hash.c \ SysUtil.c \ - DirUtil.c \ + DirUtil.cpp \ Inlines.c \ Zip.c diff --git a/minzip/DirUtil.c b/minzip/DirUtil.cpp index 97cb2e0ee..e08e360c0 100644 --- a/minzip/DirUtil.c +++ b/minzip/DirUtil.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "DirUtil.h" + #include <stdlib.h> #include <string.h> #include <stdio.h> @@ -24,7 +26,10 @@ #include <dirent.h> #include <limits.h> -#include "DirUtil.h" +#include <string> + +#include <selinux/label.h> +#include <selinux/selinux.h> typedef enum { DMISSING, DDIR, DILLEGAL } DirStatus; @@ -66,43 +71,25 @@ dirCreateHierarchy(const char *path, int mode, errno = ENOENT; return -1; } - - /* Allocate a path that we can modify; stick a slash on - * the end to make things easier. - */ - size_t pathLen = strlen(path); - char *cpath = (char *)malloc(pathLen + 2); - if (cpath == NULL) { - errno = ENOMEM; - return -1; - } - memcpy(cpath, path, pathLen); + // Allocate a path that we can modify; stick a slash on + // the end to make things easier. + std::string cpath = path; if (stripFileName) { - /* Strip everything after the last slash. - */ - char *c = cpath + pathLen - 1; - while (c != cpath && *c != '/') { - c--; - } - if (c == cpath) { - //xxx test this path - /* No directory component. Act like the path was empty. - */ + // Strip everything after the last slash. + size_t pos = cpath.rfind('/'); + if (pos == std::string::npos) { errno = ENOENT; - free(cpath); return -1; } - c[1] = '\0'; // Terminate after the slash we found. + cpath.resize(pos + 1); } else { - /* Make sure that the path ends in a slash. - */ - cpath[pathLen] = '/'; - cpath[pathLen + 1] = '\0'; + // Make sure that the path ends in a slash. + cpath.push_back('/'); } /* See if it already exists. */ - ds = getPathDirStatus(cpath); + ds = getPathDirStatus(cpath.c_str()); if (ds == DDIR) { return 0; } else if (ds == DILLEGAL) { @@ -112,7 +99,8 @@ dirCreateHierarchy(const char *path, int mode, /* Walk up the path from the root and make each level. * If a directory already exists, no big deal. */ - char *p = cpath; + const char *path_start = &cpath[0]; + char *p = &cpath[0]; while (*p != '\0') { /* Skip any slashes, watching out for the end of the string. */ @@ -135,12 +123,11 @@ dirCreateHierarchy(const char *path, int mode, /* Check this part of the path and make a new directory * if necessary. */ - ds = getPathDirStatus(cpath); + ds = getPathDirStatus(path_start); if (ds == DILLEGAL) { /* Could happen if some other process/thread is * messing with the filesystem. */ - free(cpath); return -1; } else if (ds == DMISSING) { int err; @@ -148,11 +135,11 @@ dirCreateHierarchy(const char *path, int mode, char *secontext = NULL; if (sehnd) { - selabel_lookup(sehnd, &secontext, cpath, mode); + selabel_lookup(sehnd, &secontext, path_start, mode); setfscreatecon(secontext); } - err = mkdir(cpath, mode); + err = mkdir(path_start, mode); if (secontext) { freecon(secontext); @@ -160,22 +147,17 @@ dirCreateHierarchy(const char *path, int mode, } if (err != 0) { - free(cpath); return -1; } - if (timestamp != NULL && utime(cpath, timestamp)) { - free(cpath); + if (timestamp != NULL && utime(path_start, timestamp)) { return -1; } } // else, this directory already exists. - - /* Repair the path and continue. - */ + + // Repair the path and continue. *p = '/'; } - free(cpath); - return 0; } diff --git a/minzip/DirUtil.h b/minzip/DirUtil.h index 85a00128b..85b83c387 100644 --- a/minzip/DirUtil.h +++ b/minzip/DirUtil.h @@ -24,8 +24,7 @@ extern "C" { #endif -#include <selinux/selinux.h> -#include <selinux/label.h> +struct selabel_handle; /* Like "mkdir -p", try to guarantee that all directories * specified in path are present, creating as many directories diff --git a/minzip/Zip.c b/minzip/Zip.c index bdb565c64..d557daa7f 100644 --- a/minzip/Zip.c +++ b/minzip/Zip.c @@ -23,6 +23,9 @@ #undef NDEBUG // do this after including Log.h #include <assert.h> +#include <selinux/label.h> +#include <selinux/selinux.h> + #define SORT_ENTRIES 1 /* @@ -91,7 +94,7 @@ enum { static void dumpEntry(const ZipEntry* pEntry) { LOGI(" %p '%.*s'\n", pEntry->fileName,pEntry->fileNameLen,pEntry->fileName); - LOGI(" off=%ld comp=%ld uncomp=%ld how=%d\n", pEntry->offset, + LOGI(" off=%u comp=%u uncomp=%u how=%d\n", pEntry->offset, pEntry->compLen, pEntry->uncompLen, pEntry->compression); } #endif @@ -505,13 +508,11 @@ static bool processDeflatedEntry(const ZipArchive *pArchive, const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction, void *cookie) { - long result = -1; + bool success = false; + unsigned long totalOut = 0; unsigned char procBuf[32 * 1024]; z_stream zstream; int zerr; - long compRemaining; - - compRemaining = pEntry->compLen; /* * Initialize the zlib stream. @@ -572,16 +573,17 @@ static bool processDeflatedEntry(const ZipArchive *pArchive, assert(zerr == Z_STREAM_END); /* other errors should've been caught */ // success! - result = zstream.total_out; + totalOut = zstream.total_out; + success = true; z_bail: inflateEnd(&zstream); /* free up any allocated structures */ bail: - if (result != pEntry->uncompLen) { - if (result != -1) // error already shown? - LOGW("Size mismatch on inflated file (%ld vs %ld)\n", - result, pEntry->uncompLen); + if (totalOut != pEntry->uncompLen) { + if (success) { // error already shown? + LOGW("Size mismatch on inflated file (%lu vs %u)\n", totalOut, pEntry->uncompLen); + } return false; } return true; @@ -759,7 +761,7 @@ static const char *targetEntryPath(MzPathHelper *helper, ZipEntry *pEntry) */ needLen = helper->targetDirLen + 1 + pEntry->fileNameLen - helper->zipDirLen + 1; - if (needLen > helper->bufLen) { + if (firstTime || needLen > helper->bufLen) { char *newBuf; needLen *= 2; @@ -966,7 +968,7 @@ bool mzExtractRecursive(const ZipArchive *pArchive, setfscreatecon(secontext); } - int fd = open(targetFile, O_CREAT|O_WRONLY|O_TRUNC|O_SYNC, + int fd = open(targetFile, O_CREAT|O_WRONLY|O_TRUNC, UNZIP_FILEMODE); if (secontext) { diff --git a/minzip/Zip.h b/minzip/Zip.h index 86d8db597..c932c1178 100644 --- a/minzip/Zip.h +++ b/minzip/Zip.h @@ -18,8 +18,7 @@ extern "C" { #endif -#include <selinux/selinux.h> -#include <selinux/label.h> +struct selabel_handle; /* * One entry in the Zip archive. Treat this as opaque -- use accessors below. @@ -32,9 +31,9 @@ extern "C" { typedef struct ZipEntry { unsigned int fileNameLen; const char* fileName; // not null-terminated - long offset; - long compLen; - long uncompLen; + uint32_t offset; + uint32_t compLen; + uint32_t uncompLen; int compression; long modTime; long crc32; @@ -85,10 +84,10 @@ void mzCloseZipArchive(ZipArchive* pArchive); const ZipEntry* mzFindZipEntry(const ZipArchive* pArchive, const char* entryName); -INLINE long mzGetZipEntryOffset(const ZipEntry* pEntry) { +INLINE uint32_t mzGetZipEntryOffset(const ZipEntry* pEntry) { return pEntry->offset; } -INLINE long mzGetZipEntryUncompLen(const ZipEntry* pEntry) { +INLINE uint32_t mzGetZipEntryUncompLen(const ZipEntry* pEntry) { return pEntry->uncompLen; } |