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