diff options
Diffstat (limited to 'minzip')
-rw-r--r-- | minzip/Android.mk | 10 | ||||
-rw-r--r-- | minzip/DirUtil.cpp (renamed from minzip/DirUtil.c) | 66 | ||||
-rw-r--r-- | minzip/DirUtil.h | 3 | ||||
-rw-r--r-- | minzip/Hash.cpp (renamed from minzip/Hash.c) | 13 | ||||
-rw-r--r-- | minzip/Log.h | 207 | ||||
-rw-r--r-- | minzip/SysUtil.cpp (renamed from minzip/SysUtil.c) | 64 | ||||
-rw-r--r-- | minzip/Zip.cpp (renamed from minzip/Zip.c) | 146 | ||||
-rw-r--r-- | minzip/Zip.h | 13 |
8 files changed, 150 insertions, 372 deletions
diff --git a/minzip/Android.mk b/minzip/Android.mk index 22eabfbb1..6dbfee993 100644 --- a/minzip/Android.mk +++ b/minzip/Android.mk @@ -2,17 +2,17 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ - Hash.c \ - SysUtil.c \ - DirUtil.c \ + Hash.cpp \ + SysUtil.cpp \ + DirUtil.cpp \ Inlines.c \ - Zip.c + Zip.cpp LOCAL_C_INCLUDES := \ external/zlib \ external/safe-iop/include -LOCAL_STATIC_LIBRARIES := libselinux +LOCAL_STATIC_LIBRARIES := libselinux libbase LOCAL_MODULE := libminzip 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/Hash.c b/minzip/Hash.cpp index 49bcb3161..ac08935d4 100644 --- a/minzip/Hash.c +++ b/minzip/Hash.cpp @@ -8,8 +8,8 @@ #include <stdlib.h> #include <assert.h> -#define LOG_TAG "minzip" -#include "Log.h" +#include <android-base/logging.h> + #include "Hash.h" /* table load factor, i.e. how full can it get before we resize */ @@ -220,8 +220,7 @@ void* mzHashTableLookup(HashTable* pHashTable, unsigned int itemHash, void* item { if (!resizeHash(pHashTable, pHashTable->tableSize * 2)) { /* don't really have a way to indicate failure */ - LOGE("Dalvik hash resize failure\n"); - abort(); + LOG(FATAL) << "Hash resize failure"; } /* note "pEntry" is now invalid */ } @@ -373,7 +372,7 @@ void mzHashTableProbeCount(HashTable* pHashTable, HashCalcFunc calcFunc, totalProbe += count; } - LOGV("Probe: min=%d max=%d, total=%d in %d (%d), avg=%.3f\n", - minProbe, maxProbe, totalProbe, numEntries, pHashTable->tableSize, - (float) totalProbe / (float) numEntries); + LOG(VERBOSE) << "Probe: min=" << minProbe << ", max=" << maxProbe << ", total=" + << totalProbe <<" in " << numEntries << " (" << pHashTable->tableSize + << "), avg=" << (float) totalProbe / (float) numEntries; } diff --git a/minzip/Log.h b/minzip/Log.h deleted file mode 100644 index 36e62f594..000000000 --- a/minzip/Log.h +++ /dev/null @@ -1,207 +0,0 @@ -// -// Copyright 2005 The Android Open Source Project -// -// C/C++ logging functions. See the logging documentation for API details. -// -// We'd like these to be available from C code (in case we import some from -// somewhere), so this has a C interface. -// -// The output will be correct when the log file is shared between multiple -// threads and/or multiple processes so long as the operating system -// supports O_APPEND. These calls have mutex-protected data structures -// and so are NOT reentrant. Do not use LOG in a signal handler. -// -#ifndef _MINZIP_LOG_H -#define _MINZIP_LOG_H - -#include <stdio.h> - -// --------------------------------------------------------------------- - -/* - * Normally we strip LOGV (VERBOSE messages) from release builds. - * You can modify this (for example with "#define LOG_NDEBUG 0" - * at the top of your source file) to change that behavior. - */ -#ifndef LOG_NDEBUG -#ifdef NDEBUG -#define LOG_NDEBUG 1 -#else -#define LOG_NDEBUG 0 -#endif -#endif - -/* - * This is the local tag used for the following simplified - * logging macros. You can change this preprocessor definition - * before using the other macros to change the tag. - */ -#ifndef LOG_TAG -#define LOG_TAG NULL -#endif - -// --------------------------------------------------------------------- - -/* - * Simplified macro to send a verbose log message using the current LOG_TAG. - */ -#ifndef LOGV -#if LOG_NDEBUG -#define LOGV(...) ((void)0) -#else -#define LOGV(...) ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) -#endif -#endif - -#define CONDITION(cond) (__builtin_expect((cond)!=0, 0)) - -#ifndef LOGV_IF -#if LOG_NDEBUG -#define LOGV_IF(cond, ...) ((void)0) -#else -#define LOGV_IF(cond, ...) \ - ( (CONDITION(cond)) \ - ? ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ - : (void)0 ) -#endif -#endif - -#define LOGVV LOGV -#define LOGVV_IF LOGV_IF - -/* - * Simplified macro to send a debug log message using the current LOG_TAG. - */ -#ifndef LOGD -#define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) -#endif - -#ifndef LOGD_IF -#define LOGD_IF(cond, ...) \ - ( (CONDITION(cond)) \ - ? ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ - : (void)0 ) -#endif - -/* - * Simplified macro to send an info log message using the current LOG_TAG. - */ -#ifndef LOGI -#define LOGI(...) ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) -#endif - -#ifndef LOGI_IF -#define LOGI_IF(cond, ...) \ - ( (CONDITION(cond)) \ - ? ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \ - : (void)0 ) -#endif - -/* - * Simplified macro to send a warning log message using the current LOG_TAG. - */ -#ifndef LOGW -#define LOGW(...) ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) -#endif - -#ifndef LOGW_IF -#define LOGW_IF(cond, ...) \ - ( (CONDITION(cond)) \ - ? ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \ - : (void)0 ) -#endif - -/* - * Simplified macro to send an error log message using the current LOG_TAG. - */ -#ifndef LOGE -#define LOGE(...) ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) -#endif - -#ifndef LOGE_IF -#define LOGE_IF(cond, ...) \ - ( (CONDITION(cond)) \ - ? ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ - : (void)0 ) -#endif - - -/* - * Conditional based on whether the current LOG_TAG is enabled at - * verbose priority. - */ -#ifndef IF_LOGV -#if LOG_NDEBUG -#define IF_LOGV() if (false) -#else -#define IF_LOGV() IF_LOG(LOG_VERBOSE, LOG_TAG) -#endif -#endif - -/* - * Conditional based on whether the current LOG_TAG is enabled at - * debug priority. - */ -#ifndef IF_LOGD -#define IF_LOGD() IF_LOG(LOG_DEBUG, LOG_TAG) -#endif - -/* - * Conditional based on whether the current LOG_TAG is enabled at - * info priority. - */ -#ifndef IF_LOGI -#define IF_LOGI() IF_LOG(LOG_INFO, LOG_TAG) -#endif - -/* - * Conditional based on whether the current LOG_TAG is enabled at - * warn priority. - */ -#ifndef IF_LOGW -#define IF_LOGW() IF_LOG(LOG_WARN, LOG_TAG) -#endif - -/* - * Conditional based on whether the current LOG_TAG is enabled at - * error priority. - */ -#ifndef IF_LOGE -#define IF_LOGE() IF_LOG(LOG_ERROR, LOG_TAG) -#endif - -// --------------------------------------------------------------------- - -/* - * Basic log message macro. - * - * Example: - * LOG(LOG_WARN, NULL, "Failed with error %d", errno); - * - * The second argument may be NULL or "" to indicate the "global" tag. - * - * Non-gcc probably won't have __FUNCTION__. It's not vital. gcc also - * offers __PRETTY_FUNCTION__, which is rather more than we need. - */ -#ifndef LOG -#define LOG(priority, tag, ...) \ - LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) -#endif - -/* - * Log macro that allows you to specify a number for the priority. - */ -#ifndef LOG_PRI -#define LOG_PRI(priority, tag, ...) \ - printf(tag ": " __VA_ARGS__) -#endif - -/* - * Conditional given a desired logging priority and tag. - */ -#ifndef IF_LOG -#define IF_LOG(priority, tag) \ - if (1) -#endif - -#endif // _MINZIP_LOG_H diff --git a/minzip/SysUtil.c b/minzip/SysUtil.cpp index e7dd17b51..4cdd60d9f 100644 --- a/minzip/SysUtil.c +++ b/minzip/SysUtil.cpp @@ -16,8 +16,8 @@ #include <sys/types.h> #include <unistd.h> -#define LOG_TAG "sysutil" -#include "Log.h" +#include <android-base/logging.h> + #include "SysUtil.h" static bool sysMapFD(int fd, MemMapping* pMap) { @@ -25,22 +25,22 @@ static bool sysMapFD(int fd, MemMapping* pMap) { struct stat sb; if (fstat(fd, &sb) == -1) { - LOGE("fstat(%d) failed: %s\n", fd, strerror(errno)); + PLOG(ERROR) << "fstat(" << fd << ") failed"; return false; } void* memPtr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (memPtr == MAP_FAILED) { - LOGE("mmap(%d, R, PRIVATE, %d, 0) failed: %s\n", (int) sb.st_size, fd, strerror(errno)); + PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed"; return false; } - pMap->addr = memPtr; + pMap->addr = reinterpret_cast<unsigned char*>(memPtr); pMap->length = sb.st_size; pMap->range_count = 1; - pMap->ranges = malloc(sizeof(MappedRange)); + pMap->ranges = reinterpret_cast<MappedRange*>(malloc(sizeof(MappedRange))); if (pMap->ranges == NULL) { - LOGE("malloc failed: %s\n", strerror(errno)); + PLOG(ERROR) << "malloc failed"; munmap(memPtr, sb.st_size); return false; } @@ -60,7 +60,7 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) unsigned int i; if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) { - LOGE("failed to read block device from header\n"); + PLOG(ERROR) << "failed to read block device from header"; return -1; } for (i = 0; i < sizeof(block_dev); ++i) { @@ -71,37 +71,37 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) } if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) { - LOGE("failed to parse block map header\n"); + LOG(ERROR) << "failed to parse block map header"; return -1; } if (blksize != 0) { blocks = ((size-1) / blksize) + 1; } if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0) { - LOGE("invalid data in block map file: size %zu, blksize %u, range_count %u\n", - size, blksize, range_count); + LOG(ERROR) << "invalid data in block map file: size " << size << ", blksize " << blksize + << ", range_count " << range_count; return -1; } pMap->range_count = range_count; - pMap->ranges = calloc(range_count, sizeof(MappedRange)); + pMap->ranges = reinterpret_cast<MappedRange*>(calloc(range_count, sizeof(MappedRange))); if (pMap->ranges == NULL) { - LOGE("calloc(%u, %zu) failed: %s\n", range_count, sizeof(MappedRange), strerror(errno)); + PLOG(ERROR) << "calloc(" << range_count << ", " << sizeof(MappedRange) << ") failed"; return -1; } // Reserve enough contiguous address space for the whole file. - unsigned char* reserve; - reserve = mmap64(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); + unsigned char* reserve = reinterpret_cast<unsigned char*>(mmap64(NULL, blocks * blksize, + PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0)); if (reserve == MAP_FAILED) { - LOGE("failed to reserve address space: %s\n", strerror(errno)); + PLOG(ERROR) << "failed to reserve address space"; free(pMap->ranges); return -1; } int fd = open(block_dev, O_RDONLY); if (fd < 0) { - LOGE("failed to open block device %s: %s\n", block_dev, strerror(errno)); + PLOG(ERROR) << "failed to open block device " << block_dev; munmap(reserve, blocks * blksize); free(pMap->ranges); return -1; @@ -113,20 +113,20 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) for (i = 0; i < range_count; ++i) { size_t start, end; if (fscanf(mapf, "%zu %zu\n", &start, &end) != 2) { - LOGE("failed to parse range %d in block map\n", i); + LOG(ERROR) << "failed to parse range " << i << " in block map"; success = false; break; } size_t length = (end - start) * blksize; - if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) { - LOGE("unexpected range in block map: %zu %zu\n", start, end); - success = false; - break; + if (end <= start || ((end - start) > SIZE_MAX / blksize) || length > remaining_size) { + LOG(ERROR) << "unexpected range in block map: " << start << " " << end; + success = false; + break; } void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize); if (addr == MAP_FAILED) { - LOGE("failed to map block %d: %s\n", i, strerror(errno)); + PLOG(ERROR) << "failed to map block " << i; success = false; break; } @@ -137,8 +137,8 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) remaining_size -= length; } if (success && remaining_size != 0) { - LOGE("ranges in block map are invalid: remaining_size = %zu\n", remaining_size); - success = false; + LOG(ERROR) << "ranges in block map are invalid: remaining_size = " << remaining_size; + success = false; } if (!success) { close(fd); @@ -151,7 +151,7 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) pMap->addr = reserve; pMap->length = size; - LOGI("mmapped %d ranges\n", range_count); + LOG(INFO) << "mmapped " << range_count << " ranges"; return 0; } @@ -164,12 +164,12 @@ int sysMapFile(const char* fn, MemMapping* pMap) // A map of blocks FILE* mapf = fopen(fn+1, "r"); if (mapf == NULL) { - LOGE("Unable to open '%s': %s\n", fn+1, strerror(errno)); + PLOG(ERROR) << "Unable to open '" << (fn+1) << "'"; return -1; } if (sysMapBlockFile(mapf, pMap) != 0) { - LOGE("Map of '%s' failed\n", fn); + LOG(ERROR) << "Map of '" << fn << "' failed"; fclose(mapf); return -1; } @@ -179,12 +179,12 @@ int sysMapFile(const char* fn, MemMapping* pMap) // This is a regular file. int fd = open(fn, O_RDONLY); if (fd == -1) { - LOGE("Unable to open '%s': %s\n", fn, strerror(errno)); + PLOG(ERROR) << "Unable to open '" << fn << "'"; return -1; } if (!sysMapFD(fd, pMap)) { - LOGE("Map of '%s' failed\n", fn); + LOG(ERROR) << "Map of '" << fn << "' failed"; close(fd); return -1; } @@ -202,8 +202,8 @@ void sysReleaseMap(MemMapping* pMap) int i; for (i = 0; i < pMap->range_count; ++i) { if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) { - LOGE("munmap(%p, %d) failed: %s\n", - pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno)); + PLOG(ERROR) << "munmap(" << pMap->ranges[i].addr << ", " << pMap->ranges[i].length + << ") failed"; } } free(pMap->ranges); diff --git a/minzip/Zip.c b/minzip/Zip.cpp index bdb565c64..b887b8466 100644 --- a/minzip/Zip.c +++ b/minzip/Zip.cpp @@ -14,15 +14,18 @@ #include <sys/stat.h> // for S_ISLNK() #include <unistd.h> -#define LOG_TAG "minzip" +#include <string> + +#include <android-base/logging.h> +#include <android-base/stringprintf.h> +#include <assert.h> +#include <selinux/label.h> +#include <selinux/selinux.h> + #include "Zip.h" #include "Bits.h" -#include "Log.h" #include "DirUtil.h" -#undef NDEBUG // do this after including Log.h -#include <assert.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 @@ -151,8 +154,9 @@ static void addEntryToHashTable(HashTable* pHash, ZipEntry* pEntry) found = (const ZipEntry*)mzHashTableLookup(pHash, itemHash, pEntry, hashcmpZipEntry, true); if (found != pEntry) { - LOGW("WARNING: duplicate entry '%.*s' in Zip\n", - found->fileNameLen, found->fileName); + LOG(WARNING) << "WARNING: duplicate entry '" << std::string(found->fileName, + found->fileNameLen) << "' in Zip"; + /* keep going */ } } @@ -161,7 +165,7 @@ static int validFilename(const char *fileName, unsigned int fileNameLen) { // Forbid super long filenames. if (fileNameLen >= PATH_MAX) { - LOGW("Filename too long (%d chatacters)\n", fileNameLen); + LOG(WARNING) << "Filename too long (" << fileNameLen << " chatacters)"; return 0; } @@ -169,7 +173,8 @@ static int validFilename(const char *fileName, unsigned int fileNameLen) unsigned int i; for (i = 0; i < fileNameLen; ++i) { if (fileName[i] < 32 || fileName[i] >= 127) { - LOGW("Filename contains invalid character '\%03o'\n", fileName[i]); + LOG(WARNING) << android::base::StringPrintf( + "Filename contains invalid character '\%02x'\n", fileName[i]); return 0; } } @@ -198,10 +203,10 @@ static bool parseZipArchive(ZipArchive* pArchive) */ val = get4LE(pArchive->addr); if (val == ENDSIG) { - LOGW("Found Zip archive, but it looks empty\n"); + LOG(WARNING) << "Found Zip archive, but it looks empty"; goto bail; } else if (val != LOCSIG) { - LOGW("Not a Zip archive (found 0x%08x)\n", val); + LOG(WARNING) << android::base::StringPrintf("Not a Zip archive (found 0x%08x)\n", val); goto bail; } @@ -217,7 +222,7 @@ static bool parseZipArchive(ZipArchive* pArchive) ptr--; } if (ptr < (const unsigned char*) pArchive->addr) { - LOGW("Could not find end-of-central-directory in Zip\n"); + LOG(WARNING) << "Could not find end-of-central-directory in Zip"; goto bail; } @@ -229,10 +234,10 @@ static bool parseZipArchive(ZipArchive* pArchive) numEntries = get2LE(ptr + ENDSUB); cdOffset = get4LE(ptr + ENDOFF); - LOGVV("numEntries=%d cdOffset=%d\n", numEntries, cdOffset); + LOG(VERBOSE) << "numEntries=" << numEntries << " cdOffset=" << cdOffset; if (numEntries == 0 || cdOffset >= pArchive->length) { - LOGW("Invalid entries=%d offset=%d (len=%zd)\n", - numEntries, cdOffset, pArchive->length); + LOG(WARNING) << "Invalid entries=" << numEntries << " offset=" << cdOffset + << " (len=" << pArchive->length << ")"; goto bail; } @@ -253,11 +258,11 @@ static bool parseZipArchive(ZipArchive* pArchive) const char *fileName; if (ptr + CENHDR > (const unsigned char*)pArchive->addr + pArchive->length) { - LOGW("Ran off the end (at %d)\n", i); + LOG(WARNING) << "Ran off the end (at " << i << ")"; goto bail; } if (get4LE(ptr) != CENSIG) { - LOGW("Missed a central dir sig (at %d)\n", i); + LOG(WARNING) << "Missed a central dir sig (at " << i << ")"; goto bail; } @@ -267,11 +272,11 @@ static bool parseZipArchive(ZipArchive* pArchive) commentLen = get2LE(ptr + CENCOM); fileName = (const char*)ptr + CENHDR; if (fileName + fileNameLen > (const char*)pArchive->addr + pArchive->length) { - LOGW("Filename ran off the end (at %d)\n", i); + LOG(WARNING) << "Filename ran off the end (at " << i << ")"; goto bail; } if (!validFilename(fileName, fileNameLen)) { - LOGW("Invalid filename (at %d)\n", i); + LOG(WARNING) << "Invalid filename (at " << i << ")"; goto bail; } @@ -342,7 +347,8 @@ static bool parseZipArchive(ZipArchive* pArchive) if ((pEntry->versionMadeBy & 0xff00) != 0 && (pEntry->versionMadeBy & 0xff00) != CENVEM_UNIX) { - LOGW("Incompatible \"version made by\": 0x%02x (at %d)\n", + LOG(WARNING) << android::base::StringPrintf( + "Incompatible \"version made by\": 0x%02x (at %d)\n", pEntry->versionMadeBy >> 8, i); goto bail; } @@ -352,26 +358,27 @@ static bool parseZipArchive(ZipArchive* pArchive) // overflow. This is needed because localHdrOffset is untrusted. if (!safe_add((uintptr_t *)&localHdr, (uintptr_t)pArchive->addr, (uintptr_t)localHdrOffset)) { - LOGW("Integer overflow adding in parseZipArchive\n"); + LOG(WARNING) << "Integer overflow adding in parseZipArchive"; goto bail; } if ((uintptr_t)localHdr + LOCHDR > (uintptr_t)pArchive->addr + pArchive->length) { - LOGW("Bad offset to local header: %d (at %d)\n", localHdrOffset, i); + LOG(WARNING) << "Bad offset to local header: " << localHdrOffset + << " (at " << i << ")"; goto bail; } if (get4LE(localHdr) != LOCSIG) { - LOGW("Missed a local header sig (at %d)\n", i); + LOG(WARNING) << "Missed a local header sig (at " << i << ")"; goto bail; } pEntry->offset = localHdrOffset + LOCHDR + get2LE(localHdr + LOCNAM) + get2LE(localHdr + LOCEXT); if (!safe_add(NULL, pEntry->offset, pEntry->compLen)) { - LOGW("Integer overflow adding in parseZipArchive\n"); + LOG(WARNING) << "Integer overflow adding in parseZipArchive"; goto bail; } if ((size_t)pEntry->offset + pEntry->compLen > pArchive->length) { - LOGW("Data ran off the end (at %d)\n", i); + LOG(WARNING) << "Data ran off the end (at " << i << ")"; goto bail; } @@ -429,7 +436,8 @@ int mzOpenZipArchive(unsigned char* addr, size_t length, ZipArchive* pArchive) if (length < ENDHDR) { err = -1; - LOGW("Archive %p is too small to be zip (%zd)\n", pArchive, length); + LOG(WARNING) << "Archive " << pArchive << " is too small to be zip (" + << length << ")"; goto bail; } @@ -438,7 +446,7 @@ int mzOpenZipArchive(unsigned char* addr, size_t length, ZipArchive* pArchive) if (!parseZipArchive(pArchive)) { err = -1; - LOGW("Parsing archive %p failed\n", pArchive); + LOG(WARNING) << "Parsing archive " << pArchive << " failed"; goto bail; } @@ -457,7 +465,7 @@ bail: */ void mzCloseZipArchive(ZipArchive* pArchive) { - LOGV("Closing archive %p\n", pArchive); + LOG(VERBOSE) << "Closing archive " << pArchive; free(pArchive->pEntries); @@ -505,13 +513,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. @@ -533,10 +539,10 @@ static bool processDeflatedEntry(const ZipArchive *pArchive, zerr = inflateInit2(&zstream, -MAX_WBITS); if (zerr != Z_OK) { if (zerr == Z_VERSION_ERROR) { - LOGE("Installed zlib is not compatible with linked version (%s)\n", - ZLIB_VERSION); + LOG(ERROR) << "Installed zlib is not compatible with linked version (" + << ZLIB_VERSION << ")"; } else { - LOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr); + LOG(ERROR) << "Call to inflateInit2 failed (zerr=" << zerr << ")"; } goto bail; } @@ -548,7 +554,7 @@ static bool processDeflatedEntry(const ZipArchive *pArchive, /* uncompress the data */ zerr = inflate(&zstream, Z_NO_FLUSH); if (zerr != Z_OK && zerr != Z_STREAM_END) { - LOGW("zlib inflate call failed (zerr=%d)\n", zerr); + LOG(WARNING) << "zlib inflate call failed (zerr=" << zerr << ")"; goto z_bail; } @@ -557,10 +563,10 @@ static bool processDeflatedEntry(const ZipArchive *pArchive, (zerr == Z_STREAM_END && zstream.avail_out != sizeof(procBuf))) { long procSize = zstream.next_out - procBuf; - LOGVV("+++ processing %d bytes\n", (int) procSize); + LOG(VERBOSE) << "+++ processing " << procSize << " bytes"; bool ret = processFunction(procBuf, procSize, cookie); if (!ret) { - LOGW("Process function elected to fail (in inflate)\n"); + LOG(WARNING) << "Process function elected to fail (in inflate)"; goto z_bail; } @@ -572,16 +578,18 @@ 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? + LOG(WARNING) << "Size mismatch on inflated file (" << totalOut << " vs " + << pEntry->uncompLen << ")"; + } return false; } return true; @@ -611,8 +619,8 @@ bool mzProcessZipEntryContents(const ZipArchive *pArchive, ret = processDeflatedEntry(pArchive, pEntry, processFunction, cookie); break; default: - LOGE("Unsupported compression type %d for entry '%s'\n", - pEntry->compression, pEntry->fileName); + LOG(ERROR) << "Unsupported compression type " << pEntry->compression + << " for entry '" << pEntry->fileName << "'"; break; } @@ -651,7 +659,7 @@ bool mzReadZipEntry(const ZipArchive* pArchive, const ZipEntry* pEntry, ret = mzProcessZipEntryContents(pArchive, pEntry, copyProcessFunction, (void *)&args); if (!ret) { - LOGE("Can't extract entry to buffer.\n"); + LOG(ERROR) << "Can't extract entry to buffer"; return false; } return true; @@ -668,15 +676,15 @@ static bool writeProcessFunction(const unsigned char *data, int dataLen, while (true) { ssize_t n = TEMP_FAILURE_RETRY(write(fd, data+soFar, dataLen-soFar)); if (n <= 0) { - LOGE("Error writing %zd bytes from zip file from %p: %s\n", - dataLen-soFar, data+soFar, strerror(errno)); + PLOG(ERROR) << "Error writing " << dataLen-soFar << " bytes from zip file from " + << data+soFar; return false; } else if (n > 0) { soFar += n; if (soFar == dataLen) return true; if (soFar > dataLen) { - LOGE("write overrun? (%zd bytes instead of %d)\n", - soFar, dataLen); + LOG(ERROR) << "write overrun? (" << soFar << " bytes instead of " + << dataLen << ")"; return false; } } @@ -692,7 +700,7 @@ bool mzExtractZipEntryToFile(const ZipArchive *pArchive, bool ret = mzProcessZipEntryContents(pArchive, pEntry, writeProcessFunction, (void*)(intptr_t)fd); if (!ret) { - LOGE("Can't extract entry to file.\n"); + LOG(ERROR) << "Can't extract entry to file."; return false; } return true; @@ -728,7 +736,7 @@ bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive, bool ret = mzProcessZipEntryContents(pArchive, pEntry, bufferProcessFunction, (void*)&bec); if (!ret || bec.len != 0) { - LOGE("Can't extract entry to memory buffer.\n"); + LOG(ERROR) << "Can't extract entry to memory buffer."; return false; } return true; @@ -759,7 +767,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; @@ -822,11 +830,11 @@ bool mzExtractRecursive(const ZipArchive *pArchive, struct selabel_handle *sehnd) { if (zipDir[0] == '/') { - LOGE("mzExtractRecursive(): zipDir must be a relative path.\n"); + LOG(ERROR) << "mzExtractRecursive(): zipDir must be a relative path."; return false; } if (targetDir[0] != '/') { - LOGE("mzExtractRecursive(): targetDir must be an absolute path.\n"); + LOG(ERROR) << "mzExtractRecursive(): targetDir must be an absolute path.\n"; return false; } @@ -836,7 +844,7 @@ bool mzExtractRecursive(const ZipArchive *pArchive, zipDirLen = strlen(zipDir); zpath = (char *)malloc(zipDirLen + 2); if (zpath == NULL) { - LOGE("Can't allocate %d bytes for zip path\n", zipDirLen + 2); + LOG(ERROR) << "Can't allocate " << (zipDirLen + 2) << " bytes for zip path"; return false; } /* If zipDir is empty, we'll extract the entire zip file. @@ -915,8 +923,8 @@ bool mzExtractRecursive(const ZipArchive *pArchive, */ const char *targetFile = targetEntryPath(&helper, pEntry); if (targetFile == NULL) { - LOGE("Can't assemble target path for \"%.*s\"\n", - pEntry->fileNameLen, pEntry->fileName); + LOG(ERROR) << "Can't assemble target path for \"" << std::string(pEntry->fileName, + pEntry->fileNameLen) << "\""; ok = false; break; } @@ -940,8 +948,7 @@ bool mzExtractRecursive(const ZipArchive *pArchive, int ret = dirCreateHierarchy( targetFile, UNZIP_DIRMODE, timestamp, true, sehnd); if (ret != 0) { - LOGE("Can't create containing directory for \"%s\": %s\n", - targetFile, strerror(errno)); + PLOG(ERROR) << "Can't create containing directory for \"" << targetFile << "\""; ok = false; break; } @@ -955,8 +962,8 @@ bool mzExtractRecursive(const ZipArchive *pArchive, * warn about this for now and preserve older behavior. */ if (mzIsZipEntrySymlink(pEntry)) { - LOGE("Symlink entry \"%.*s\" will be output as a regular file.", - pEntry->fileNameLen, pEntry->fileName); + LOG(ERROR) << "Symlink entry \"" << std::string(pEntry->fileName, + pEntry->fileNameLen) << "\" will be output as a regular file."; } char *secontext = NULL; @@ -966,7 +973,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) { @@ -975,8 +982,7 @@ bool mzExtractRecursive(const ZipArchive *pArchive, } if (fd < 0) { - LOGE("Can't create target file \"%s\": %s\n", - targetFile, strerror(errno)); + PLOG(ERROR) << "Can't create target file \"" << targetFile << "\""; ok = false; break; } @@ -989,25 +995,25 @@ bool mzExtractRecursive(const ZipArchive *pArchive, ok = false; } if (!ok) { - LOGE("Error extracting \"%s\"\n", targetFile); + LOG(ERROR) << "Error extracting \"" << targetFile << "\""; ok = false; break; } if (timestamp != NULL && utime(targetFile, timestamp)) { - LOGE("Error touching \"%s\"\n", targetFile); + LOG(ERROR) << "Error touching \"" << targetFile << "\""; ok = false; break; } - LOGV("Extracted file \"%s\"\n", targetFile); + LOG(VERBOSE) <<"Extracted file \"" << targetFile << "\""; ++extractCount; } if (callback != NULL) callback(targetFile, cookie); } - LOGV("Extracted %d file(s)\n", extractCount); + LOG(VERBOSE) << "Extracted " << extractCount << " file(s)"; free(helper.buf); free(zpath); 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; } |