| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2007 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | // | 
 | 18 | // Read-only access to Zip archives, with minimal heap allocation. | 
 | 19 | // | 
 | 20 | #define LOG_TAG "zipro" | 
 | 21 | //#define LOG_NDEBUG 0 | 
| Mathias Agopian | f446ba9 | 2009-06-04 13:53:57 -0700 | [diff] [blame] | 22 | #include <utils/ZipFileRO.h> | 
 | 23 | #include <utils/Log.h> | 
 | 24 | #include <utils/misc.h> | 
| Kenny Root | fa98920 | 2010-09-24 07:57:37 -0700 | [diff] [blame] | 25 | #include <utils/threads.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 |  | 
 | 27 | #include <zlib.h> | 
 | 28 |  | 
 | 29 | #include <string.h> | 
 | 30 | #include <fcntl.h> | 
 | 31 | #include <errno.h> | 
 | 32 | #include <assert.h> | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 33 | #include <unistd.h> | 
 | 34 |  | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 35 | #if HAVE_PRINTF_ZD | 
 | 36 | #  define ZD "%zd" | 
 | 37 | #  define ZD_TYPE ssize_t | 
 | 38 | #else | 
 | 39 | #  define ZD "%ld" | 
 | 40 | #  define ZD_TYPE long | 
 | 41 | #endif | 
 | 42 |  | 
 | 43 | /* | 
 | 44 |  * We must open binary files using open(path, ... | O_BINARY) under Windows. | 
 | 45 |  * Otherwise strange read errors will happen. | 
 | 46 |  */ | 
 | 47 | #ifndef O_BINARY | 
 | 48 | #  define O_BINARY  0 | 
 | 49 | #endif | 
 | 50 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 51 | /* | 
 | 52 |  * TEMP_FAILURE_RETRY is defined by some, but not all, versions of | 
 | 53 |  * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's | 
 | 54 |  * not already defined, then define it here. | 
 | 55 |  */ | 
 | 56 | #ifndef TEMP_FAILURE_RETRY | 
 | 57 | /* Used to retry syscalls that can return EINTR. */ | 
 | 58 | #define TEMP_FAILURE_RETRY(exp) ({         \ | 
 | 59 |     typeof (exp) _rc;                      \ | 
 | 60 |     do {                                   \ | 
 | 61 |         _rc = (exp);                       \ | 
 | 62 |     } while (_rc == -1 && errno == EINTR); \ | 
 | 63 |     _rc; }) | 
 | 64 | #endif | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 |  | 
 | 66 | using namespace android; | 
 | 67 |  | 
 | 68 | /* | 
 | 69 |  * Zip file constants. | 
 | 70 |  */ | 
 | 71 | #define kEOCDSignature      0x06054b50 | 
 | 72 | #define kEOCDLen            22 | 
 | 73 | #define kEOCDNumEntries     8               // offset to #of entries in file | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 74 | #define kEOCDSize           12              // size of the central directory | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | #define kEOCDFileOffset     16              // offset to central directory | 
 | 76 |  | 
 | 77 | #define kMaxCommentLen      65535           // longest possible in ushort | 
 | 78 | #define kMaxEOCDSearch      (kMaxCommentLen + kEOCDLen) | 
 | 79 |  | 
 | 80 | #define kLFHSignature       0x04034b50 | 
 | 81 | #define kLFHLen             30              // excluding variable-len fields | 
 | 82 | #define kLFHNameLen         26              // offset to filename length | 
 | 83 | #define kLFHExtraLen        28              // offset to extra length | 
 | 84 |  | 
 | 85 | #define kCDESignature       0x02014b50 | 
 | 86 | #define kCDELen             46              // excluding variable-len fields | 
 | 87 | #define kCDEMethod          10              // offset to compression method | 
 | 88 | #define kCDEModWhen         12              // offset to modification timestamp | 
 | 89 | #define kCDECRC             16              // offset to entry CRC | 
 | 90 | #define kCDECompLen         20              // offset to compressed length | 
 | 91 | #define kCDEUncompLen       24              // offset to uncompressed length | 
 | 92 | #define kCDENameLen         28              // offset to filename length | 
 | 93 | #define kCDEExtraLen        30              // offset to extra length | 
 | 94 | #define kCDECommentLen      32              // offset to comment length | 
 | 95 | #define kCDELocalOffset     42              // offset to local hdr | 
 | 96 |  | 
 | 97 | /* | 
 | 98 |  * The values we return for ZipEntryRO use 0 as an invalid value, so we | 
 | 99 |  * want to adjust the hash table index by a fixed amount.  Using a large | 
 | 100 |  * value helps insure that people don't mix & match arguments, e.g. to | 
 | 101 |  * findEntryByIndex(). | 
 | 102 |  */ | 
 | 103 | #define kZipEntryAdj        10000 | 
 | 104 |  | 
| Kenny Root | dbf6f27 | 2010-10-01 18:28:28 -0700 | [diff] [blame] | 105 | ZipFileRO::~ZipFileRO() { | 
 | 106 |     free(mHashTable); | 
 | 107 |     if (mDirectoryMap) | 
 | 108 |         mDirectoryMap->release(); | 
 | 109 |     if (mFd >= 0) | 
 | 110 |         TEMP_FAILURE_RETRY(close(mFd)); | 
 | 111 |     if (mFileName) | 
 | 112 |         free(mFileName); | 
 | 113 | } | 
 | 114 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | /* | 
 | 116 |  * Convert a ZipEntryRO to a hash table index, verifying that it's in a | 
 | 117 |  * valid range. | 
 | 118 |  */ | 
 | 119 | int ZipFileRO::entryToIndex(const ZipEntryRO entry) const | 
 | 120 | { | 
 | 121 |     long ent = ((long) entry) - kZipEntryAdj; | 
 | 122 |     if (ent < 0 || ent >= mHashTableSize || mHashTable[ent].name == NULL) { | 
 | 123 |         LOGW("Invalid ZipEntryRO %p (%ld)\n", entry, ent); | 
 | 124 |         return -1; | 
 | 125 |     } | 
 | 126 |     return ent; | 
 | 127 | } | 
 | 128 |  | 
 | 129 |  | 
 | 130 | /* | 
 | 131 |  * Open the specified file read-only.  We memory-map the entire thing and | 
 | 132 |  * close the file before returning. | 
 | 133 |  */ | 
 | 134 | status_t ZipFileRO::open(const char* zipFileName) | 
 | 135 | { | 
 | 136 |     int fd = -1; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 138 |     assert(mDirectoryMap == NULL); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 |  | 
 | 140 |     /* | 
 | 141 |      * Open and map the specified file. | 
 | 142 |      */ | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 143 |     fd = ::open(zipFileName, O_RDONLY | O_BINARY); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 |     if (fd < 0) { | 
 | 145 |         LOGW("Unable to open zip '%s': %s\n", zipFileName, strerror(errno)); | 
 | 146 |         return NAME_NOT_FOUND; | 
 | 147 |     } | 
 | 148 |  | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 149 |     mFileLength = lseek64(fd, 0, SEEK_END); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 150 |     if (mFileLength < kEOCDLen) { | 
| Kenny Root | dbf6f27 | 2010-10-01 18:28:28 -0700 | [diff] [blame] | 151 |         TEMP_FAILURE_RETRY(close(fd)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 |         return UNKNOWN_ERROR; | 
 | 153 |     } | 
 | 154 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 155 |     if (mFileName != NULL) { | 
 | 156 |         free(mFileName); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 |     } | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 158 |     mFileName = strdup(zipFileName); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 |  | 
 | 160 |     mFd = fd; | 
 | 161 |  | 
 | 162 |     /* | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 163 |      * Find the Central Directory and store its size and number of entries. | 
 | 164 |      */ | 
 | 165 |     if (!mapCentralDirectory()) { | 
 | 166 |         goto bail; | 
 | 167 |     } | 
 | 168 |  | 
 | 169 |     /* | 
 | 170 |      * Verify Central Directory and create data structures for fast access. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 |      */ | 
 | 172 |     if (!parseZipArchive()) { | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 173 |         goto bail; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 |     } | 
 | 175 |  | 
 | 176 |     return OK; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 177 |  | 
 | 178 | bail: | 
 | 179 |     free(mFileName); | 
 | 180 |     mFileName = NULL; | 
| Kenny Root | dbf6f27 | 2010-10-01 18:28:28 -0700 | [diff] [blame] | 181 |     TEMP_FAILURE_RETRY(close(fd)); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 182 |     return UNKNOWN_ERROR; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } | 
 | 184 |  | 
 | 185 | /* | 
 | 186 |  * Parse the Zip archive, verifying its contents and initializing internal | 
 | 187 |  * data structures. | 
 | 188 |  */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 189 | bool ZipFileRO::mapCentralDirectory(void) | 
 | 190 | { | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 191 |     ssize_t readAmount = kMaxEOCDSearch; | 
 | 192 |     if (readAmount > (ssize_t) mFileLength) | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 193 |         readAmount = mFileLength; | 
 | 194 |  | 
 | 195 |     unsigned char* scanBuf = (unsigned char*) malloc(readAmount); | 
 | 196 |     if (scanBuf == NULL) { | 
 | 197 |         LOGW("couldn't allocate scanBuf: %s", strerror(errno)); | 
 | 198 |         free(scanBuf); | 
 | 199 |         return false; | 
 | 200 |     } | 
 | 201 |  | 
 | 202 |     /* | 
 | 203 |      * Make sure this is a Zip archive. | 
 | 204 |      */ | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 205 |     if (lseek64(mFd, 0, SEEK_SET) != 0) { | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 206 |         LOGW("seek to start failed: %s", strerror(errno)); | 
 | 207 |         free(scanBuf); | 
 | 208 |         return false; | 
 | 209 |     } | 
 | 210 |  | 
 | 211 |     ssize_t actual = TEMP_FAILURE_RETRY(read(mFd, scanBuf, sizeof(int32_t))); | 
 | 212 |     if (actual != (ssize_t) sizeof(int32_t)) { | 
 | 213 |         LOGI("couldn't read first signature from zip archive: %s", strerror(errno)); | 
 | 214 |         free(scanBuf); | 
 | 215 |         return false; | 
 | 216 |     } | 
 | 217 |  | 
 | 218 |     { | 
 | 219 |         unsigned int header = get4LE(scanBuf); | 
 | 220 |         if (header == kEOCDSignature) { | 
 | 221 |             LOGI("Found Zip archive, but it looks empty\n"); | 
 | 222 |             free(scanBuf); | 
 | 223 |             return false; | 
 | 224 |         } else if (header != kLFHSignature) { | 
| Kenny Root | fa98920 | 2010-09-24 07:57:37 -0700 | [diff] [blame] | 225 |             LOGV("Not a Zip archive (found 0x%08x)\n", header); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 226 |             free(scanBuf); | 
 | 227 |             return false; | 
 | 228 |         } | 
 | 229 |     } | 
 | 230 |  | 
 | 231 |     /* | 
 | 232 |      * Perform the traditional EOCD snipe hunt. | 
 | 233 |      * | 
 | 234 |      * We're searching for the End of Central Directory magic number, | 
 | 235 |      * which appears at the start of the EOCD block.  It's followed by | 
 | 236 |      * 18 bytes of EOCD stuff and up to 64KB of archive comment.  We | 
 | 237 |      * need to read the last part of the file into a buffer, dig through | 
 | 238 |      * it to find the magic number, parse some values out, and use those | 
 | 239 |      * to determine the extent of the CD. | 
 | 240 |      * | 
 | 241 |      * We start by pulling in the last part of the file. | 
 | 242 |      */ | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 243 |     off64_t searchStart = mFileLength - readAmount; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 244 |  | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 245 |     if (lseek64(mFd, searchStart, SEEK_SET) != searchStart) { | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 246 |         LOGW("seek %ld failed: %s\n",  (long) searchStart, strerror(errno)); | 
 | 247 |         free(scanBuf); | 
 | 248 |         return false; | 
 | 249 |     } | 
 | 250 |     actual = TEMP_FAILURE_RETRY(read(mFd, scanBuf, readAmount)); | 
 | 251 |     if (actual != (ssize_t) readAmount) { | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 252 |         LOGW("Zip: read " ZD ", expected " ZD ". Failed: %s\n", | 
 | 253 |             (ZD_TYPE) actual, (ZD_TYPE) readAmount, strerror(errno)); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 254 |         free(scanBuf); | 
 | 255 |         return false; | 
 | 256 |     } | 
 | 257 |  | 
 | 258 |     /* | 
 | 259 |      * Scan backward for the EOCD magic.  In an archive without a trailing | 
 | 260 |      * comment, we'll find it on the first try.  (We may want to consider | 
 | 261 |      * doing an initial minimal read; if we don't find it, retry with a | 
 | 262 |      * second read as above.) | 
 | 263 |      */ | 
 | 264 |     int i; | 
 | 265 |     for (i = readAmount - kEOCDLen; i >= 0; i--) { | 
 | 266 |         if (scanBuf[i] == 0x50 && get4LE(&scanBuf[i]) == kEOCDSignature) { | 
 | 267 |             LOGV("+++ Found EOCD at buf+%d\n", i); | 
 | 268 |             break; | 
 | 269 |         } | 
 | 270 |     } | 
 | 271 |     if (i < 0) { | 
 | 272 |         LOGD("Zip: EOCD not found, %s is not zip\n", mFileName); | 
 | 273 |         free(scanBuf); | 
 | 274 |         return false; | 
 | 275 |     } | 
 | 276 |  | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 277 |     off64_t eocdOffset = searchStart + i; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 278 |     const unsigned char* eocdPtr = scanBuf + i; | 
 | 279 |  | 
 | 280 |     assert(eocdOffset < mFileLength); | 
 | 281 |  | 
 | 282 |     /* | 
 | 283 |      * Grab the CD offset and size, and the number of entries in the | 
| Kenny Root | 8f20e5e | 2010-08-04 16:30:40 -0700 | [diff] [blame] | 284 |      * archive. After that, we can release our EOCD hunt buffer. | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 285 |      */ | 
 | 286 |     unsigned int numEntries = get2LE(eocdPtr + kEOCDNumEntries); | 
 | 287 |     unsigned int dirSize = get4LE(eocdPtr + kEOCDSize); | 
 | 288 |     unsigned int dirOffset = get4LE(eocdPtr + kEOCDFileOffset); | 
| Kenny Root | 8f20e5e | 2010-08-04 16:30:40 -0700 | [diff] [blame] | 289 |     free(scanBuf); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 290 |  | 
| Kenny Root | 8f20e5e | 2010-08-04 16:30:40 -0700 | [diff] [blame] | 291 |     // Verify that they look reasonable. | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 292 |     if ((long long) dirOffset + (long long) dirSize > (long long) eocdOffset) { | 
 | 293 |         LOGW("bad offsets (dir %ld, size %u, eocd %ld)\n", | 
 | 294 |             (long) dirOffset, dirSize, (long) eocdOffset); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 295 |         return false; | 
 | 296 |     } | 
 | 297 |     if (numEntries == 0) { | 
 | 298 |         LOGW("empty archive?\n"); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 299 |         return false; | 
 | 300 |     } | 
 | 301 |  | 
 | 302 |     LOGV("+++ numEntries=%d dirSize=%d dirOffset=%d\n", | 
 | 303 |         numEntries, dirSize, dirOffset); | 
 | 304 |  | 
 | 305 |     mDirectoryMap = new FileMap(); | 
 | 306 |     if (mDirectoryMap == NULL) { | 
 | 307 |         LOGW("Unable to create directory map: %s", strerror(errno)); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 308 |         return false; | 
 | 309 |     } | 
 | 310 |  | 
 | 311 |     if (!mDirectoryMap->create(mFileName, mFd, dirOffset, dirSize, true)) { | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 312 |         LOGW("Unable to map '%s' (" ZD " to " ZD "): %s\n", mFileName, | 
 | 313 |                 (ZD_TYPE) dirOffset, (ZD_TYPE) (dirOffset + dirSize), strerror(errno)); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 314 |         return false; | 
 | 315 |     } | 
 | 316 |  | 
 | 317 |     mNumEntries = numEntries; | 
 | 318 |     mDirectoryOffset = dirOffset; | 
 | 319 |  | 
 | 320 |     return true; | 
 | 321 | } | 
 | 322 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | bool ZipFileRO::parseZipArchive(void) | 
 | 324 | { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 |     bool result = false; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 326 |     const unsigned char* cdPtr = (const unsigned char*) mDirectoryMap->getDataPtr(); | 
 | 327 |     size_t cdLength = mDirectoryMap->getDataLength(); | 
 | 328 |     int numEntries = mNumEntries; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 |  | 
 | 330 |     /* | 
 | 331 |      * Create hash table.  We have a minimum 75% load factor, possibly as | 
 | 332 |      * low as 50% after we round off to a power of 2. | 
 | 333 |      */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 334 |     mHashTableSize = roundUpPower2(1 + (numEntries * 4) / 3); | 
 | 335 |     mHashTable = (HashEntry*) calloc(mHashTableSize, sizeof(HashEntry)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 |  | 
 | 337 |     /* | 
 | 338 |      * Walk through the central directory, adding entries to the hash | 
 | 339 |      * table. | 
 | 340 |      */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 341 |     const unsigned char* ptr = cdPtr; | 
 | 342 |     for (int i = 0; i < numEntries; i++) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 |         if (get4LE(ptr) != kCDESignature) { | 
 | 344 |             LOGW("Missed a central dir sig (at %d)\n", i); | 
 | 345 |             goto bail; | 
 | 346 |         } | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 347 |         if (ptr + kCDELen > cdPtr + cdLength) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 |             LOGW("Ran off the end (at %d)\n", i); | 
 | 349 |             goto bail; | 
 | 350 |         } | 
 | 351 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 352 |         long localHdrOffset = (long) get4LE(ptr + kCDELocalOffset); | 
 | 353 |         if (localHdrOffset >= mDirectoryOffset) { | 
 | 354 |             LOGW("bad LFH offset %ld at entry %d\n", localHdrOffset, i); | 
 | 355 |             goto bail; | 
 | 356 |         } | 
 | 357 |  | 
 | 358 |         unsigned int fileNameLen, extraLen, commentLen, hash; | 
 | 359 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 |         fileNameLen = get2LE(ptr + kCDENameLen); | 
 | 361 |         extraLen = get2LE(ptr + kCDEExtraLen); | 
 | 362 |         commentLen = get2LE(ptr + kCDECommentLen); | 
 | 363 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 |         /* add the CDE filename to the hash table */ | 
 | 365 |         hash = computeHash((const char*)ptr + kCDELen, fileNameLen); | 
 | 366 |         addToHash((const char*)ptr + kCDELen, fileNameLen, hash); | 
 | 367 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 368 |         ptr += kCDELen + fileNameLen + extraLen + commentLen; | 
 | 369 |         if ((size_t)(ptr - cdPtr) > cdLength) { | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 370 |             LOGW("bad CD advance (%d vs " ZD ") at entry %d\n", | 
 | 371 |                 (int) (ptr - cdPtr), (ZD_TYPE) cdLength, i); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 |             goto bail; | 
 | 373 |         } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 |     } | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 375 |     LOGV("+++ zip good scan %d entries\n", numEntries); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 |     result = true; | 
 | 377 |  | 
 | 378 | bail: | 
 | 379 |     return result; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } | 
 | 381 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | /* | 
 | 383 |  * Simple string hash function for non-null-terminated strings. | 
 | 384 |  */ | 
 | 385 | /*static*/ unsigned int ZipFileRO::computeHash(const char* str, int len) | 
 | 386 | { | 
 | 387 |     unsigned int hash = 0; | 
 | 388 |  | 
 | 389 |     while (len--) | 
 | 390 |         hash = hash * 31 + *str++; | 
 | 391 |  | 
 | 392 |     return hash; | 
 | 393 | } | 
 | 394 |  | 
 | 395 | /* | 
 | 396 |  * Add a new entry to the hash table. | 
 | 397 |  */ | 
 | 398 | void ZipFileRO::addToHash(const char* str, int strLen, unsigned int hash) | 
 | 399 | { | 
 | 400 |     int ent = hash & (mHashTableSize-1); | 
 | 401 |  | 
 | 402 |     /* | 
 | 403 |      * We over-allocate the table, so we're guaranteed to find an empty slot. | 
 | 404 |      */ | 
 | 405 |     while (mHashTable[ent].name != NULL) | 
 | 406 |         ent = (ent + 1) & (mHashTableSize-1); | 
 | 407 |  | 
 | 408 |     mHashTable[ent].name = str; | 
 | 409 |     mHashTable[ent].nameLen = strLen; | 
 | 410 | } | 
 | 411 |  | 
 | 412 | /* | 
 | 413 |  * Find a matching entry. | 
 | 414 |  * | 
| Kenny Root | 06dbe50 | 2010-10-21 15:18:28 -0700 | [diff] [blame] | 415 |  * Returns NULL if not found. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 |  */ | 
 | 417 | ZipEntryRO ZipFileRO::findEntryByName(const char* fileName) const | 
 | 418 | { | 
| Kenny Root | 06dbe50 | 2010-10-21 15:18:28 -0700 | [diff] [blame] | 419 |     /* | 
 | 420 |      * If the ZipFileRO instance is not initialized, the entry number will | 
 | 421 |      * end up being garbage since mHashTableSize is -1. | 
 | 422 |      */ | 
 | 423 |     if (mHashTableSize <= 0) { | 
 | 424 |         return NULL; | 
 | 425 |     } | 
 | 426 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 |     int nameLen = strlen(fileName); | 
 | 428 |     unsigned int hash = computeHash(fileName, nameLen); | 
 | 429 |     int ent = hash & (mHashTableSize-1); | 
 | 430 |  | 
 | 431 |     while (mHashTable[ent].name != NULL) { | 
 | 432 |         if (mHashTable[ent].nameLen == nameLen && | 
 | 433 |             memcmp(mHashTable[ent].name, fileName, nameLen) == 0) | 
 | 434 |         { | 
 | 435 |             /* match */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 436 |             return (ZipEntryRO)(long)(ent + kZipEntryAdj); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 |         } | 
 | 438 |  | 
 | 439 |         ent = (ent + 1) & (mHashTableSize-1); | 
 | 440 |     } | 
 | 441 |  | 
 | 442 |     return NULL; | 
 | 443 | } | 
 | 444 |  | 
 | 445 | /* | 
 | 446 |  * Find the Nth entry. | 
 | 447 |  * | 
 | 448 |  * This currently involves walking through the sparse hash table, counting | 
 | 449 |  * non-empty entries.  If we need to speed this up we can either allocate | 
 | 450 |  * a parallel lookup table or (perhaps better) provide an iterator interface. | 
 | 451 |  */ | 
 | 452 | ZipEntryRO ZipFileRO::findEntryByIndex(int idx) const | 
 | 453 | { | 
 | 454 |     if (idx < 0 || idx >= mNumEntries) { | 
 | 455 |         LOGW("Invalid index %d\n", idx); | 
 | 456 |         return NULL; | 
 | 457 |     } | 
 | 458 |  | 
 | 459 |     for (int ent = 0; ent < mHashTableSize; ent++) { | 
 | 460 |         if (mHashTable[ent].name != NULL) { | 
 | 461 |             if (idx-- == 0) | 
 | 462 |                 return (ZipEntryRO) (ent + kZipEntryAdj); | 
 | 463 |         } | 
 | 464 |     } | 
 | 465 |  | 
 | 466 |     return NULL; | 
 | 467 | } | 
 | 468 |  | 
 | 469 | /* | 
 | 470 |  * Get the useful fields from the zip entry. | 
 | 471 |  * | 
 | 472 |  * Returns "false" if the offsets to the fields or the contents of the fields | 
 | 473 |  * appear to be bogus. | 
 | 474 |  */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 475 | bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen, | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 476 |     size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | { | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 478 |     bool ret = false; | 
 | 479 |  | 
 | 480 |     const int ent = entryToIndex(entry); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 |     if (ent < 0) | 
 | 482 |         return false; | 
 | 483 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 484 |     HashEntry hashEntry = mHashTable[ent]; | 
 | 485 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 |     /* | 
 | 487 |      * Recover the start of the central directory entry from the filename | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 488 |      * pointer.  The filename is the first entry past the fixed-size data, | 
 | 489 |      * so we can just subtract back from that. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 |      */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 491 |     const unsigned char* ptr = (const unsigned char*) hashEntry.name; | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 492 |     off64_t cdOffset = mDirectoryOffset; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 |  | 
 | 494 |     ptr -= kCDELen; | 
 | 495 |  | 
 | 496 |     int method = get2LE(ptr + kCDEMethod); | 
 | 497 |     if (pMethod != NULL) | 
 | 498 |         *pMethod = method; | 
 | 499 |  | 
 | 500 |     if (pModWhen != NULL) | 
 | 501 |         *pModWhen = get4LE(ptr + kCDEModWhen); | 
 | 502 |     if (pCrc32 != NULL) | 
 | 503 |         *pCrc32 = get4LE(ptr + kCDECRC); | 
 | 504 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 505 |     size_t compLen = get4LE(ptr + kCDECompLen); | 
 | 506 |     if (pCompLen != NULL) | 
 | 507 |         *pCompLen = compLen; | 
 | 508 |     size_t uncompLen = get4LE(ptr + kCDEUncompLen); | 
 | 509 |     if (pUncompLen != NULL) | 
 | 510 |         *pUncompLen = uncompLen; | 
 | 511 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 |     /* | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 513 |      * If requested, determine the offset of the start of the data.  All we | 
 | 514 |      * have is the offset to the Local File Header, which is variable size, | 
 | 515 |      * so we have to read the contents of the struct to figure out where | 
 | 516 |      * the actual data starts. | 
 | 517 |      * | 
 | 518 |      * We also need to make sure that the lengths are not so large that | 
 | 519 |      * somebody trying to map the compressed or uncompressed data runs | 
 | 520 |      * off the end of the mapped region. | 
 | 521 |      * | 
 | 522 |      * Note we don't verify compLen/uncompLen if they don't request the | 
 | 523 |      * dataOffset, because dataOffset is expensive to determine.  However, | 
 | 524 |      * if they don't have the file offset, they're not likely to be doing | 
 | 525 |      * anything with the contents. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 |      */ | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 |     if (pOffset != NULL) { | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 528 |         long localHdrOffset = get4LE(ptr + kCDELocalOffset); | 
 | 529 |         if (localHdrOffset + kLFHLen >= cdOffset) { | 
 | 530 |             LOGE("ERROR: bad local hdr offset in zip\n"); | 
 | 531 |             return false; | 
 | 532 |         } | 
 | 533 |  | 
 | 534 |         unsigned char lfhBuf[kLFHLen]; | 
| Kenny Root | fa98920 | 2010-09-24 07:57:37 -0700 | [diff] [blame] | 535 |  | 
| Kenny Root | 61ef747 | 2010-10-04 14:20:14 -0700 | [diff] [blame] | 536 | #ifdef HAVE_PREAD | 
 | 537 |         /* | 
 | 538 |          * This file descriptor might be from zygote's preloaded assets, | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 539 |          * so we need to do an pread64() instead of a lseek64() + read() to | 
| Kenny Root | 61ef747 | 2010-10-04 14:20:14 -0700 | [diff] [blame] | 540 |          * guarantee atomicity across the processes with the shared file | 
 | 541 |          * descriptors. | 
 | 542 |          */ | 
 | 543 |         ssize_t actual = | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 544 |                 TEMP_FAILURE_RETRY(pread64(mFd, lfhBuf, sizeof(lfhBuf), localHdrOffset)); | 
| Kenny Root | 61ef747 | 2010-10-04 14:20:14 -0700 | [diff] [blame] | 545 |  | 
 | 546 |         if (actual != sizeof(lfhBuf)) { | 
 | 547 |             LOGW("failed reading lfh from offset %ld\n", localHdrOffset); | 
 | 548 |             return false; | 
 | 549 |         } | 
 | 550 |  | 
 | 551 |         if (get4LE(lfhBuf) != kLFHSignature) { | 
 | 552 |             LOGW("didn't find signature at start of lfh; wanted: offset=%ld data=0x%08x; " | 
 | 553 |                     "got: data=0x%08lx\n", | 
 | 554 |                     localHdrOffset, kLFHSignature, get4LE(lfhBuf)); | 
 | 555 |             return false; | 
 | 556 |         } | 
 | 557 | #else /* HAVE_PREAD */ | 
 | 558 |         /* | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 559 |          * For hosts don't have pread64() we cannot guarantee atomic reads from | 
| Kenny Root | 61ef747 | 2010-10-04 14:20:14 -0700 | [diff] [blame] | 560 |          * an offset in a file. Android should never run on those platforms. | 
 | 561 |          * File descriptors inherited from a fork() share file offsets and | 
 | 562 |          * there would be nothing to protect from two different processes | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 563 |          * calling lseek64() concurrently. | 
| Kenny Root | 61ef747 | 2010-10-04 14:20:14 -0700 | [diff] [blame] | 564 |          */ | 
 | 565 |  | 
| Kenny Root | fa98920 | 2010-09-24 07:57:37 -0700 | [diff] [blame] | 566 |         { | 
 | 567 |             AutoMutex _l(mFdLock); | 
 | 568 |  | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 569 |             if (lseek64(mFd, localHdrOffset, SEEK_SET) != localHdrOffset) { | 
| Kenny Root | fa98920 | 2010-09-24 07:57:37 -0700 | [diff] [blame] | 570 |                 LOGW("failed seeking to lfh at offset %ld\n", localHdrOffset); | 
 | 571 |                 return false; | 
 | 572 |             } | 
 | 573 |  | 
 | 574 |             ssize_t actual = | 
| Kenny Root | 61ef747 | 2010-10-04 14:20:14 -0700 | [diff] [blame] | 575 |                     TEMP_FAILURE_RETRY(read(mFd, lfhBuf, sizeof(lfhBuf))); | 
| Kenny Root | fa98920 | 2010-09-24 07:57:37 -0700 | [diff] [blame] | 576 |             if (actual != sizeof(lfhBuf)) { | 
 | 577 |                 LOGW("failed reading lfh from offset %ld\n", localHdrOffset); | 
 | 578 |                 return false; | 
 | 579 |             } | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 580 |  | 
| Kenny Root | dbf6f27 | 2010-10-01 18:28:28 -0700 | [diff] [blame] | 581 |             if (get4LE(lfhBuf) != kLFHSignature) { | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 582 |                 off64_t actualOffset = lseek64(mFd, 0, SEEK_CUR); | 
| Kenny Root | dbf6f27 | 2010-10-01 18:28:28 -0700 | [diff] [blame] | 583 |                 LOGW("didn't find signature at start of lfh; wanted: offset=%ld data=0x%08x; " | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 584 |                         "got: offset=" ZD " data=0x%08lx\n", | 
 | 585 |                         localHdrOffset, kLFHSignature, (ZD_TYPE) actualOffset, get4LE(lfhBuf)); | 
| Kenny Root | dbf6f27 | 2010-10-01 18:28:28 -0700 | [diff] [blame] | 586 |                 return false; | 
 | 587 |             } | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 588 |         } | 
| Kenny Root | 61ef747 | 2010-10-04 14:20:14 -0700 | [diff] [blame] | 589 | #endif /* HAVE_PREAD */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 590 |  | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 591 |         off64_t dataOffset = localHdrOffset + kLFHLen | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 592 |             + get2LE(lfhBuf + kLFHNameLen) + get2LE(lfhBuf + kLFHExtraLen); | 
 | 593 |         if (dataOffset >= cdOffset) { | 
 | 594 |             LOGW("bad data offset %ld in zip\n", (long) dataOffset); | 
 | 595 |             return false; | 
 | 596 |         } | 
 | 597 |  | 
 | 598 |         /* check lengths */ | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 599 |         if ((off64_t)(dataOffset + compLen) > cdOffset) { | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 600 |             LOGW("bad compressed length in zip (%ld + " ZD " > %ld)\n", | 
 | 601 |                 (long) dataOffset, (ZD_TYPE) compLen, (long) cdOffset); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 602 |             return false; | 
 | 603 |         } | 
 | 604 |  | 
 | 605 |         if (method == kCompressStored && | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 606 |             (off64_t)(dataOffset + uncompLen) > cdOffset) | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 607 |         { | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 608 |             LOGE("ERROR: bad uncompressed length in zip (%ld + " ZD " > %ld)\n", | 
 | 609 |                 (long) dataOffset, (ZD_TYPE) uncompLen, (long) cdOffset); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 610 |             return false; | 
 | 611 |         } | 
 | 612 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 |         *pOffset = dataOffset; | 
 | 614 |     } | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 615 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 |     return true; | 
 | 617 | } | 
 | 618 |  | 
 | 619 | /* | 
 | 620 |  * Copy the entry's filename to the buffer. | 
 | 621 |  */ | 
 | 622 | int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen) | 
 | 623 |     const | 
 | 624 | { | 
 | 625 |     int ent = entryToIndex(entry); | 
 | 626 |     if (ent < 0) | 
 | 627 |         return -1; | 
 | 628 |  | 
 | 629 |     int nameLen = mHashTable[ent].nameLen; | 
 | 630 |     if (bufLen < nameLen+1) | 
 | 631 |         return nameLen+1; | 
 | 632 |  | 
 | 633 |     memcpy(buffer, mHashTable[ent].name, nameLen); | 
 | 634 |     buffer[nameLen] = '\0'; | 
 | 635 |     return 0; | 
 | 636 | } | 
 | 637 |  | 
 | 638 | /* | 
 | 639 |  * Create a new FileMap object that spans the data in "entry". | 
 | 640 |  */ | 
 | 641 | FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const | 
 | 642 | { | 
 | 643 |     /* | 
 | 644 |      * TODO: the efficient way to do this is to modify FileMap to allow | 
 | 645 |      * sub-regions of a file to be mapped.  A reference-counting scheme | 
 | 646 |      * can manage the base memory mapping.  For now, we just create a brand | 
 | 647 |      * new mapping off of the Zip archive file descriptor. | 
 | 648 |      */ | 
 | 649 |  | 
 | 650 |     FileMap* newMap; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 651 |     size_t compLen; | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 652 |     off64_t offset; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 |  | 
 | 654 |     if (!getEntryInfo(entry, NULL, NULL, &compLen, &offset, NULL, NULL)) | 
 | 655 |         return NULL; | 
 | 656 |  | 
 | 657 |     newMap = new FileMap(); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 658 |     if (!newMap->create(mFileName, mFd, offset, compLen, true)) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 |         newMap->release(); | 
 | 660 |         return NULL; | 
 | 661 |     } | 
 | 662 |  | 
 | 663 |     return newMap; | 
 | 664 | } | 
 | 665 |  | 
 | 666 | /* | 
 | 667 |  * Uncompress an entry, in its entirety, into the provided output buffer. | 
 | 668 |  * | 
 | 669 |  * This doesn't verify the data's CRC, which might be useful for | 
 | 670 |  * uncompressed data.  The caller should be able to manage it. | 
 | 671 |  */ | 
 | 672 | bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer) const | 
 | 673 | { | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 674 |     const size_t kSequentialMin = 32768; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 |     bool result = false; | 
 | 676 |     int ent = entryToIndex(entry); | 
 | 677 |     if (ent < 0) | 
 | 678 |         return -1; | 
 | 679 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 |     int method; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 681 |     size_t uncompLen, compLen; | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 682 |     off64_t offset; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 683 |     const unsigned char* ptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 |  | 
 | 685 |     getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL); | 
 | 686 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 687 |     FileMap* file = createEntryFileMap(entry); | 
 | 688 |     if (file == NULL) { | 
 | 689 |         goto bail; | 
 | 690 |     } | 
 | 691 |  | 
 | 692 |     ptr = (const unsigned char*) file->getDataPtr(); | 
 | 693 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 |     /* | 
 | 695 |      * Experiment with madvise hint.  When we want to uncompress a file, | 
 | 696 |      * we pull some stuff out of the central dir entry and then hit a | 
 | 697 |      * bunch of compressed or uncompressed data sequentially.  The CDE | 
 | 698 |      * visit will cause a limited amount of read-ahead because it's at | 
 | 699 |      * the end of the file.  We could end up doing lots of extra disk | 
 | 700 |      * access if the file we're prying open is small.  Bottom line is we | 
 | 701 |      * probably don't want to turn MADV_SEQUENTIAL on and leave it on. | 
 | 702 |      * | 
 | 703 |      * So, if the compressed size of the file is above a certain minimum | 
 | 704 |      * size, temporarily boost the read-ahead in the hope that the extra | 
 | 705 |      * pair of system calls are negated by a reduction in page faults. | 
 | 706 |      */ | 
 | 707 |     if (compLen > kSequentialMin) | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 708 |         file->advise(FileMap::SEQUENTIAL); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 |  | 
 | 710 |     if (method == kCompressStored) { | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 711 |         memcpy(buffer, ptr, uncompLen); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 |     } else { | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 713 |         if (!inflateBuffer(buffer, ptr, uncompLen, compLen)) | 
| Kenny Root | b47eafa | 2010-09-24 09:11:28 -0700 | [diff] [blame] | 714 |             goto unmap; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 |     } | 
 | 716 |  | 
 | 717 |     if (compLen > kSequentialMin) | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 718 |         file->advise(FileMap::NORMAL); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 |  | 
 | 720 |     result = true; | 
 | 721 |  | 
| Kenny Root | b47eafa | 2010-09-24 09:11:28 -0700 | [diff] [blame] | 722 | unmap: | 
 | 723 |     file->release(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | bail: | 
 | 725 |     return result; | 
 | 726 | } | 
 | 727 |  | 
 | 728 | /* | 
 | 729 |  * Uncompress an entry, in its entirety, to an open file descriptor. | 
 | 730 |  * | 
 | 731 |  * This doesn't verify the data's CRC, but probably should. | 
 | 732 |  */ | 
 | 733 | bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const | 
 | 734 | { | 
 | 735 |     bool result = false; | 
 | 736 |     int ent = entryToIndex(entry); | 
 | 737 |     if (ent < 0) | 
 | 738 |         return -1; | 
 | 739 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 |     int method; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 741 |     size_t uncompLen, compLen; | 
| Kenny Root | 18092dd | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 742 |     off64_t offset; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 743 |     const unsigned char* ptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 |  | 
 | 745 |     getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL); | 
 | 746 |  | 
| Kenny Root | b47eafa | 2010-09-24 09:11:28 -0700 | [diff] [blame] | 747 |     FileMap* file = createEntryFileMap(entry); | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 748 |     if (file == NULL) { | 
 | 749 |         goto bail; | 
 | 750 |     } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 752 |     ptr = (const unsigned char*) file->getDataPtr(); | 
 | 753 |  | 
 | 754 |     if (method == kCompressStored) { | 
 | 755 |         ssize_t actual = write(fd, ptr, uncompLen); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 |         if (actual < 0) { | 
 | 757 |             LOGE("Write failed: %s\n", strerror(errno)); | 
| Kenny Root | b47eafa | 2010-09-24 09:11:28 -0700 | [diff] [blame] | 758 |             goto unmap; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 759 |         } else if ((size_t) actual != uncompLen) { | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 760 |             LOGE("Partial write during uncompress (" ZD " of " ZD ")\n", | 
 | 761 |                 (ZD_TYPE) actual, (ZD_TYPE) uncompLen); | 
| Kenny Root | b47eafa | 2010-09-24 09:11:28 -0700 | [diff] [blame] | 762 |             goto unmap; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 |         } else { | 
 | 764 |             LOGI("+++ successful write\n"); | 
 | 765 |         } | 
 | 766 |     } else { | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 767 |         if (!inflateBuffer(fd, ptr, uncompLen, compLen)) | 
| Kenny Root | b47eafa | 2010-09-24 09:11:28 -0700 | [diff] [blame] | 768 |             goto unmap; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 |     } | 
 | 770 |  | 
 | 771 |     result = true; | 
 | 772 |  | 
| Kenny Root | b47eafa | 2010-09-24 09:11:28 -0700 | [diff] [blame] | 773 | unmap: | 
 | 774 |     file->release(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | bail: | 
 | 776 |     return result; | 
 | 777 | } | 
 | 778 |  | 
 | 779 | /* | 
 | 780 |  * Uncompress "deflate" data from one buffer to another. | 
 | 781 |  */ | 
 | 782 | /*static*/ bool ZipFileRO::inflateBuffer(void* outBuf, const void* inBuf, | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 783 |     size_t uncompLen, size_t compLen) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | { | 
 | 785 |     bool result = false; | 
 | 786 |     z_stream zstream; | 
 | 787 |     int zerr; | 
 | 788 |  | 
 | 789 |     /* | 
 | 790 |      * Initialize the zlib stream struct. | 
 | 791 |      */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 792 |     memset(&zstream, 0, sizeof(zstream)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 |     zstream.zalloc = Z_NULL; | 
 | 794 |     zstream.zfree = Z_NULL; | 
 | 795 |     zstream.opaque = Z_NULL; | 
 | 796 |     zstream.next_in = (Bytef*)inBuf; | 
 | 797 |     zstream.avail_in = compLen; | 
 | 798 |     zstream.next_out = (Bytef*) outBuf; | 
 | 799 |     zstream.avail_out = uncompLen; | 
 | 800 |     zstream.data_type = Z_UNKNOWN; | 
 | 801 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 802 |     /* | 
 | 803 |      * Use the undocumented "negative window bits" feature to tell zlib | 
 | 804 |      * that there's no zlib header waiting for it. | 
 | 805 |      */ | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 |     zerr = inflateInit2(&zstream, -MAX_WBITS); | 
 | 807 |     if (zerr != Z_OK) { | 
 | 808 |         if (zerr == Z_VERSION_ERROR) { | 
 | 809 |             LOGE("Installed zlib is not compatible with linked version (%s)\n", | 
 | 810 |                 ZLIB_VERSION); | 
 | 811 |         } else { | 
 | 812 |             LOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr); | 
 | 813 |         } | 
 | 814 |         goto bail; | 
 | 815 |     } | 
 | 816 |  | 
 | 817 |     /* | 
 | 818 |      * Expand data. | 
 | 819 |      */ | 
 | 820 |     zerr = inflate(&zstream, Z_FINISH); | 
 | 821 |     if (zerr != Z_STREAM_END) { | 
 | 822 |         LOGW("Zip inflate failed, zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)\n", | 
 | 823 |             zerr, zstream.next_in, zstream.avail_in, | 
 | 824 |             zstream.next_out, zstream.avail_out); | 
 | 825 |         goto z_bail; | 
 | 826 |     } | 
 | 827 |  | 
 | 828 |     /* paranoia */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 829 |     if (zstream.total_out != uncompLen) { | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 830 |         LOGW("Size mismatch on inflated file (%ld vs " ZD ")\n", | 
 | 831 |             zstream.total_out, (ZD_TYPE) uncompLen); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 |         goto z_bail; | 
 | 833 |     } | 
 | 834 |  | 
 | 835 |     result = true; | 
 | 836 |  | 
 | 837 | z_bail: | 
 | 838 |     inflateEnd(&zstream);        /* free up any allocated structures */ | 
 | 839 |  | 
 | 840 | bail: | 
 | 841 |     return result; | 
 | 842 | } | 
 | 843 |  | 
 | 844 | /* | 
 | 845 |  * Uncompress "deflate" data from one buffer to an open file descriptor. | 
 | 846 |  */ | 
 | 847 | /*static*/ bool ZipFileRO::inflateBuffer(int fd, const void* inBuf, | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 848 |     size_t uncompLen, size_t compLen) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | { | 
 | 850 |     bool result = false; | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 851 |     const size_t kWriteBufSize = 32768; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 |     unsigned char writeBuf[kWriteBufSize]; | 
 | 853 |     z_stream zstream; | 
 | 854 |     int zerr; | 
 | 855 |  | 
 | 856 |     /* | 
 | 857 |      * Initialize the zlib stream struct. | 
 | 858 |      */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 859 |     memset(&zstream, 0, sizeof(zstream)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 |     zstream.zalloc = Z_NULL; | 
 | 861 |     zstream.zfree = Z_NULL; | 
 | 862 |     zstream.opaque = Z_NULL; | 
 | 863 |     zstream.next_in = (Bytef*)inBuf; | 
 | 864 |     zstream.avail_in = compLen; | 
 | 865 |     zstream.next_out = (Bytef*) writeBuf; | 
 | 866 |     zstream.avail_out = sizeof(writeBuf); | 
 | 867 |     zstream.data_type = Z_UNKNOWN; | 
 | 868 |  | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 869 |     /* | 
 | 870 |      * Use the undocumented "negative window bits" feature to tell zlib | 
 | 871 |      * that there's no zlib header waiting for it. | 
 | 872 |      */ | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 |     zerr = inflateInit2(&zstream, -MAX_WBITS); | 
 | 874 |     if (zerr != Z_OK) { | 
 | 875 |         if (zerr == Z_VERSION_ERROR) { | 
 | 876 |             LOGE("Installed zlib is not compatible with linked version (%s)\n", | 
 | 877 |                 ZLIB_VERSION); | 
 | 878 |         } else { | 
 | 879 |             LOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr); | 
 | 880 |         } | 
 | 881 |         goto bail; | 
 | 882 |     } | 
 | 883 |  | 
 | 884 |     /* | 
 | 885 |      * Loop while we have more to do. | 
 | 886 |      */ | 
 | 887 |     do { | 
 | 888 |         /* | 
 | 889 |          * Expand data. | 
 | 890 |          */ | 
 | 891 |         zerr = inflate(&zstream, Z_NO_FLUSH); | 
 | 892 |         if (zerr != Z_OK && zerr != Z_STREAM_END) { | 
 | 893 |             LOGW("zlib inflate: zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)\n", | 
 | 894 |                 zerr, zstream.next_in, zstream.avail_in, | 
 | 895 |                 zstream.next_out, zstream.avail_out); | 
 | 896 |             goto z_bail; | 
 | 897 |         } | 
 | 898 |  | 
 | 899 |         /* write when we're full or when we're done */ | 
 | 900 |         if (zstream.avail_out == 0 || | 
 | 901 |             (zerr == Z_STREAM_END && zstream.avail_out != sizeof(writeBuf))) | 
 | 902 |         { | 
 | 903 |             long writeSize = zstream.next_out - writeBuf; | 
 | 904 |             int cc = write(fd, writeBuf, writeSize); | 
 | 905 |             if (cc != (int) writeSize) { | 
 | 906 |                 LOGW("write failed in inflate (%d vs %ld)\n", cc, writeSize); | 
 | 907 |                 goto z_bail; | 
 | 908 |             } | 
 | 909 |  | 
 | 910 |             zstream.next_out = writeBuf; | 
 | 911 |             zstream.avail_out = sizeof(writeBuf); | 
 | 912 |         } | 
 | 913 |     } while (zerr == Z_OK); | 
 | 914 |  | 
 | 915 |     assert(zerr == Z_STREAM_END);       /* other errors should've been caught */ | 
 | 916 |  | 
 | 917 |     /* paranoia */ | 
| Kenny Root | d4066a4 | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 918 |     if (zstream.total_out != uncompLen) { | 
| Raphael Moll | 2cf43c6 | 2010-10-13 19:13:48 -0700 | [diff] [blame] | 919 |         LOGW("Size mismatch on inflated file (%ld vs " ZD ")\n", | 
 | 920 |             zstream.total_out, (ZD_TYPE) uncompLen); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 |         goto z_bail; | 
 | 922 |     } | 
 | 923 |  | 
 | 924 |     result = true; | 
 | 925 |  | 
 | 926 | z_bail: | 
 | 927 |     inflateEnd(&zstream);        /* free up any allocated structures */ | 
 | 928 |  | 
 | 929 | bail: | 
 | 930 |     return result; | 
 | 931 | } |