The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Simple Zip file support. |
| 5 | */ |
| 6 | #include "safe_iop.h" |
| 7 | #include "zlib.h" |
| 8 | |
| 9 | #include <errno.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <limits.h> |
| 12 | #include <stdint.h> // for uintptr_t |
| 13 | #include <stdlib.h> |
| 14 | #include <sys/stat.h> // for S_ISLNK() |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #define LOG_TAG "minzip" |
| 18 | #include "Zip.h" |
| 19 | #include "Bits.h" |
| 20 | #include "Log.h" |
| 21 | #include "DirUtil.h" |
| 22 | |
| 23 | #undef NDEBUG // do this after including Log.h |
| 24 | #include <assert.h> |
| 25 | |
| 26 | #define SORT_ENTRIES 1 |
| 27 | |
| 28 | /* |
| 29 | * Offset and length constants (java.util.zip naming convention). |
| 30 | */ |
| 31 | enum { |
| 32 | CENSIG = 0x02014b50, // PK12 |
| 33 | CENHDR = 46, |
| 34 | |
| 35 | CENVEM = 4, |
| 36 | CENVER = 6, |
| 37 | CENFLG = 8, |
| 38 | CENHOW = 10, |
| 39 | CENTIM = 12, |
| 40 | CENCRC = 16, |
| 41 | CENSIZ = 20, |
| 42 | CENLEN = 24, |
| 43 | CENNAM = 28, |
Doug Zongker | 596271f | 2009-04-29 16:52:04 -0700 | [diff] [blame] | 44 | CENEXT = 30, |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | CENCOM = 32, |
| 46 | CENDSK = 34, |
| 47 | CENATT = 36, |
| 48 | CENATX = 38, |
| 49 | CENOFF = 42, |
| 50 | |
| 51 | ENDSIG = 0x06054b50, // PK56 |
| 52 | ENDHDR = 22, |
| 53 | |
| 54 | ENDSUB = 8, |
| 55 | ENDTOT = 10, |
| 56 | ENDSIZ = 12, |
| 57 | ENDOFF = 16, |
| 58 | ENDCOM = 20, |
| 59 | |
| 60 | EXTSIG = 0x08074b50, // PK78 |
| 61 | EXTHDR = 16, |
| 62 | |
| 63 | EXTCRC = 4, |
| 64 | EXTSIZ = 8, |
| 65 | EXTLEN = 12, |
| 66 | |
| 67 | LOCSIG = 0x04034b50, // PK34 |
| 68 | LOCHDR = 30, |
Doug Zongker | 596271f | 2009-04-29 16:52:04 -0700 | [diff] [blame] | 69 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 70 | LOCVER = 4, |
| 71 | LOCFLG = 6, |
| 72 | LOCHOW = 8, |
| 73 | LOCTIM = 10, |
| 74 | LOCCRC = 14, |
Doug Zongker | 596271f | 2009-04-29 16:52:04 -0700 | [diff] [blame] | 75 | LOCSIZ = 18, |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | LOCLEN = 22, |
| 77 | LOCNAM = 26, |
| 78 | LOCEXT = 28, |
| 79 | |
| 80 | STORED = 0, |
| 81 | DEFLATED = 8, |
| 82 | |
| 83 | CENVEM_UNIX = 3 << 8, // the high byte of CENVEM |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | /* |
| 88 | * For debugging, dump the contents of a ZipEntry. |
| 89 | */ |
| 90 | #if 0 |
| 91 | static void dumpEntry(const ZipEntry* pEntry) |
| 92 | { |
| 93 | LOGI(" %p '%.*s'\n", pEntry->fileName,pEntry->fileNameLen,pEntry->fileName); |
caozhiyuan | 2584f9c | 2016-03-11 15:18:29 +0800 | [diff] [blame] | 94 | LOGI(" off=%u comp=%u uncomp=%u how=%d\n", pEntry->offset, |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 95 | pEntry->compLen, pEntry->uncompLen, pEntry->compression); |
| 96 | } |
| 97 | #endif |
| 98 | |
| 99 | /* |
| 100 | * (This is a mzHashTableLookup callback.) |
| 101 | * |
| 102 | * Compare two ZipEntry structs, by name. |
| 103 | */ |
| 104 | static int hashcmpZipEntry(const void* ventry1, const void* ventry2) |
| 105 | { |
| 106 | const ZipEntry* entry1 = (const ZipEntry*) ventry1; |
| 107 | const ZipEntry* entry2 = (const ZipEntry*) ventry2; |
| 108 | |
| 109 | if (entry1->fileNameLen != entry2->fileNameLen) |
| 110 | return entry1->fileNameLen - entry2->fileNameLen; |
| 111 | return memcmp(entry1->fileName, entry2->fileName, entry1->fileNameLen); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * (This is a mzHashTableLookup callback.) |
| 116 | * |
| 117 | * find a ZipEntry struct by name. |
| 118 | */ |
| 119 | static int hashcmpZipName(const void* ventry, const void* vname) |
| 120 | { |
| 121 | const ZipEntry* entry = (const ZipEntry*) ventry; |
| 122 | const char* name = (const char*) vname; |
| 123 | unsigned int nameLen = strlen(name); |
| 124 | |
| 125 | if (entry->fileNameLen != nameLen) |
| 126 | return entry->fileNameLen - nameLen; |
| 127 | return memcmp(entry->fileName, name, nameLen); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Compute the hash code for a ZipEntry filename. |
| 132 | * |
| 133 | * Not expected to be compatible with any other hash function, so we init |
| 134 | * to 2 to ensure it doesn't happen to match. |
| 135 | */ |
| 136 | static unsigned int computeHash(const char* name, int nameLen) |
| 137 | { |
| 138 | unsigned int hash = 2; |
| 139 | |
| 140 | while (nameLen--) |
| 141 | hash = hash * 31 + *name++; |
| 142 | |
| 143 | return hash; |
| 144 | } |
| 145 | |
| 146 | static void addEntryToHashTable(HashTable* pHash, ZipEntry* pEntry) |
| 147 | { |
| 148 | unsigned int itemHash = computeHash(pEntry->fileName, pEntry->fileNameLen); |
| 149 | const ZipEntry* found; |
| 150 | |
| 151 | found = (const ZipEntry*)mzHashTableLookup(pHash, |
| 152 | itemHash, pEntry, hashcmpZipEntry, true); |
| 153 | if (found != pEntry) { |
| 154 | LOGW("WARNING: duplicate entry '%.*s' in Zip\n", |
| 155 | found->fileNameLen, found->fileName); |
| 156 | /* keep going */ |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static int validFilename(const char *fileName, unsigned int fileNameLen) |
| 161 | { |
| 162 | // Forbid super long filenames. |
| 163 | if (fileNameLen >= PATH_MAX) { |
| 164 | LOGW("Filename too long (%d chatacters)\n", fileNameLen); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | // Require all characters to be printable ASCII (no NUL, no UTF-8, etc). |
| 169 | unsigned int i; |
| 170 | for (i = 0; i < fileNameLen; ++i) { |
| 171 | if (fileName[i] < 32 || fileName[i] >= 127) { |
| 172 | LOGW("Filename contains invalid character '\%03o'\n", fileName[i]); |
| 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Parse the contents of a Zip archive. After confirming that the file |
| 182 | * is in fact a Zip, we scan out the contents of the central directory and |
| 183 | * store it in a hash table. |
| 184 | * |
| 185 | * Returns "true" on success. |
| 186 | */ |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 187 | static bool parseZipArchive(ZipArchive* pArchive) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 188 | { |
| 189 | bool result = false; |
| 190 | const unsigned char* ptr; |
| 191 | unsigned int i, numEntries, cdOffset; |
| 192 | unsigned int val; |
| 193 | |
| 194 | /* |
| 195 | * The first 4 bytes of the file will either be the local header |
| 196 | * signature for the first file (LOCSIG) or, if the archive doesn't |
| 197 | * have any files in it, the end-of-central-directory signature (ENDSIG). |
| 198 | */ |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 199 | val = get4LE(pArchive->addr); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 200 | if (val == ENDSIG) { |
Tao Bao | dd4d981 | 2015-07-15 14:13:06 -0700 | [diff] [blame] | 201 | LOGW("Found Zip archive, but it looks empty\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 202 | goto bail; |
| 203 | } else if (val != LOCSIG) { |
Tao Bao | dd4d981 | 2015-07-15 14:13:06 -0700 | [diff] [blame] | 204 | LOGW("Not a Zip archive (found 0x%08x)\n", val); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 205 | goto bail; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Find the EOCD. We'll find it immediately unless they have a file |
| 210 | * comment. |
| 211 | */ |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 212 | ptr = pArchive->addr + pArchive->length - ENDHDR; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 213 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 214 | while (ptr >= (const unsigned char*) pArchive->addr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 215 | if (*ptr == (ENDSIG & 0xff) && get4LE(ptr) == ENDSIG) |
| 216 | break; |
| 217 | ptr--; |
| 218 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 219 | if (ptr < (const unsigned char*) pArchive->addr) { |
Tao Bao | dd4d981 | 2015-07-15 14:13:06 -0700 | [diff] [blame] | 220 | LOGW("Could not find end-of-central-directory in Zip\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 221 | goto bail; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * There are two interesting items in the EOCD block: the number of |
| 226 | * entries in the file, and the file offset of the start of the |
| 227 | * central directory. |
| 228 | */ |
| 229 | numEntries = get2LE(ptr + ENDSUB); |
| 230 | cdOffset = get4LE(ptr + ENDOFF); |
| 231 | |
| 232 | LOGVV("numEntries=%d cdOffset=%d\n", numEntries, cdOffset); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 233 | if (numEntries == 0 || cdOffset >= pArchive->length) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 234 | LOGW("Invalid entries=%d offset=%d (len=%zd)\n", |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 235 | numEntries, cdOffset, pArchive->length); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 236 | goto bail; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Create data structures to hold entries. |
| 241 | */ |
| 242 | pArchive->numEntries = numEntries; |
| 243 | pArchive->pEntries = (ZipEntry*) calloc(numEntries, sizeof(ZipEntry)); |
| 244 | pArchive->pHash = mzHashTableCreate(mzHashSize(numEntries), NULL); |
| 245 | if (pArchive->pEntries == NULL || pArchive->pHash == NULL) |
| 246 | goto bail; |
| 247 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 248 | ptr = pArchive->addr + cdOffset; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 249 | for (i = 0; i < numEntries; i++) { |
| 250 | ZipEntry* pEntry; |
| 251 | unsigned int fileNameLen, extraLen, commentLen, localHdrOffset; |
| 252 | const unsigned char* localHdr; |
| 253 | const char *fileName; |
| 254 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 255 | if (ptr + CENHDR > (const unsigned char*)pArchive->addr + pArchive->length) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 256 | LOGW("Ran off the end (at %d)\n", i); |
| 257 | goto bail; |
| 258 | } |
| 259 | if (get4LE(ptr) != CENSIG) { |
| 260 | LOGW("Missed a central dir sig (at %d)\n", i); |
| 261 | goto bail; |
| 262 | } |
| 263 | |
| 264 | localHdrOffset = get4LE(ptr + CENOFF); |
| 265 | fileNameLen = get2LE(ptr + CENNAM); |
| 266 | extraLen = get2LE(ptr + CENEXT); |
| 267 | commentLen = get2LE(ptr + CENCOM); |
| 268 | fileName = (const char*)ptr + CENHDR; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 269 | if (fileName + fileNameLen > (const char*)pArchive->addr + pArchive->length) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 270 | LOGW("Filename ran off the end (at %d)\n", i); |
| 271 | goto bail; |
| 272 | } |
| 273 | if (!validFilename(fileName, fileNameLen)) { |
| 274 | LOGW("Invalid filename (at %d)\n", i); |
| 275 | goto bail; |
| 276 | } |
| 277 | |
| 278 | #if SORT_ENTRIES |
| 279 | /* Figure out where this entry should go (binary search). |
| 280 | */ |
| 281 | if (i > 0) { |
| 282 | int low, high; |
| 283 | |
| 284 | low = 0; |
| 285 | high = i - 1; |
| 286 | while (low <= high) { |
| 287 | int mid; |
| 288 | int diff; |
| 289 | int diffLen; |
| 290 | |
| 291 | mid = low + ((high - low) / 2); // avoid overflow |
| 292 | |
| 293 | if (pArchive->pEntries[mid].fileNameLen < fileNameLen) { |
| 294 | diffLen = pArchive->pEntries[mid].fileNameLen; |
| 295 | } else { |
| 296 | diffLen = fileNameLen; |
| 297 | } |
| 298 | diff = strncmp(pArchive->pEntries[mid].fileName, fileName, |
| 299 | diffLen); |
| 300 | if (diff == 0) { |
| 301 | diff = pArchive->pEntries[mid].fileNameLen - fileNameLen; |
| 302 | } |
| 303 | if (diff < 0) { |
| 304 | low = mid + 1; |
| 305 | } else if (diff > 0) { |
| 306 | high = mid - 1; |
| 307 | } else { |
| 308 | high = mid; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | unsigned int target = high + 1; |
| 314 | assert(target <= i); |
| 315 | if (target != i) { |
| 316 | /* It belongs somewhere other than at the end of |
| 317 | * the list. Make some room at [target]. |
| 318 | */ |
| 319 | memmove(pArchive->pEntries + target + 1, |
| 320 | pArchive->pEntries + target, |
| 321 | (i - target) * sizeof(ZipEntry)); |
| 322 | } |
| 323 | pEntry = &pArchive->pEntries[target]; |
| 324 | } else { |
| 325 | pEntry = &pArchive->pEntries[0]; |
| 326 | } |
| 327 | #else |
| 328 | pEntry = &pArchive->pEntries[i]; |
| 329 | #endif |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 330 | pEntry->fileNameLen = fileNameLen; |
| 331 | pEntry->fileName = fileName; |
| 332 | |
| 333 | pEntry->compLen = get4LE(ptr + CENSIZ); |
| 334 | pEntry->uncompLen = get4LE(ptr + CENLEN); |
| 335 | pEntry->compression = get2LE(ptr + CENHOW); |
| 336 | pEntry->modTime = get4LE(ptr + CENTIM); |
| 337 | pEntry->crc32 = get4LE(ptr + CENCRC); |
| 338 | |
| 339 | /* These two are necessary for finding the mode of the file. |
| 340 | */ |
| 341 | pEntry->versionMadeBy = get2LE(ptr + CENVEM); |
| 342 | if ((pEntry->versionMadeBy & 0xff00) != 0 && |
| 343 | (pEntry->versionMadeBy & 0xff00) != CENVEM_UNIX) |
| 344 | { |
| 345 | LOGW("Incompatible \"version made by\": 0x%02x (at %d)\n", |
| 346 | pEntry->versionMadeBy >> 8, i); |
| 347 | goto bail; |
| 348 | } |
| 349 | pEntry->externalFileAttributes = get4LE(ptr + CENATX); |
| 350 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 351 | // Perform pArchive->addr + localHdrOffset, ensuring that it won't |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 352 | // overflow. This is needed because localHdrOffset is untrusted. |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 353 | if (!safe_add((uintptr_t *)&localHdr, (uintptr_t)pArchive->addr, |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 354 | (uintptr_t)localHdrOffset)) { |
| 355 | LOGW("Integer overflow adding in parseZipArchive\n"); |
| 356 | goto bail; |
| 357 | } |
| 358 | if ((uintptr_t)localHdr + LOCHDR > |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 359 | (uintptr_t)pArchive->addr + pArchive->length) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 360 | LOGW("Bad offset to local header: %d (at %d)\n", localHdrOffset, i); |
| 361 | goto bail; |
| 362 | } |
| 363 | if (get4LE(localHdr) != LOCSIG) { |
| 364 | LOGW("Missed a local header sig (at %d)\n", i); |
| 365 | goto bail; |
| 366 | } |
| 367 | pEntry->offset = localHdrOffset + LOCHDR |
| 368 | + get2LE(localHdr + LOCNAM) + get2LE(localHdr + LOCEXT); |
| 369 | if (!safe_add(NULL, pEntry->offset, pEntry->compLen)) { |
| 370 | LOGW("Integer overflow adding in parseZipArchive\n"); |
| 371 | goto bail; |
| 372 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 373 | if ((size_t)pEntry->offset + pEntry->compLen > pArchive->length) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 374 | LOGW("Data ran off the end (at %d)\n", i); |
| 375 | goto bail; |
| 376 | } |
| 377 | |
| 378 | #if !SORT_ENTRIES |
| 379 | /* Add to hash table; no need to lock here. |
| 380 | * Can't do this now if we're sorting, because entries |
| 381 | * will move around. |
| 382 | */ |
| 383 | addEntryToHashTable(pArchive->pHash, pEntry); |
| 384 | #endif |
| 385 | |
| 386 | //dumpEntry(pEntry); |
| 387 | ptr += CENHDR + fileNameLen + extraLen + commentLen; |
| 388 | } |
| 389 | |
| 390 | #if SORT_ENTRIES |
| 391 | /* If we're sorting, we have to wait until all entries |
| 392 | * are in their final places, otherwise the pointers will |
| 393 | * probably point to the wrong things. |
| 394 | */ |
| 395 | for (i = 0; i < numEntries; i++) { |
| 396 | /* Add to hash table; no need to lock here. |
| 397 | */ |
| 398 | addEntryToHashTable(pArchive->pHash, &pArchive->pEntries[i]); |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | result = true; |
| 403 | |
| 404 | bail: |
| 405 | if (!result) { |
| 406 | mzHashTableFree(pArchive->pHash); |
| 407 | pArchive->pHash = NULL; |
| 408 | } |
| 409 | return result; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Open a Zip archive and scan out the contents. |
| 414 | * |
| 415 | * The easiest way to do this is to mmap() the whole thing and do the |
| 416 | * traditional backward scan for central directory. Since the EOCD is |
| 417 | * a relatively small bit at the end, we should end up only touching a |
| 418 | * small set of pages. |
| 419 | * |
| 420 | * This will be called on non-Zip files, especially during startup, so |
| 421 | * we don't want to be too noisy about failures. (Do we want a "quiet" |
| 422 | * flag?) |
| 423 | * |
| 424 | * On success, we fill out the contents of "pArchive". |
| 425 | */ |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 426 | int mzOpenZipArchive(unsigned char* addr, size_t length, ZipArchive* pArchive) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 427 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 428 | int err; |
| 429 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 430 | if (length < ENDHDR) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 431 | err = -1; |
Tao Bao | dd4d981 | 2015-07-15 14:13:06 -0700 | [diff] [blame] | 432 | LOGW("Archive %p is too small to be zip (%zd)\n", pArchive, length); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 433 | goto bail; |
| 434 | } |
| 435 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 436 | pArchive->addr = addr; |
| 437 | pArchive->length = length; |
| 438 | |
| 439 | if (!parseZipArchive(pArchive)) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 440 | err = -1; |
Tao Bao | dd4d981 | 2015-07-15 14:13:06 -0700 | [diff] [blame] | 441 | LOGW("Parsing archive %p failed\n", pArchive); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 442 | goto bail; |
| 443 | } |
| 444 | |
| 445 | err = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 446 | |
| 447 | bail: |
| 448 | if (err != 0) |
| 449 | mzCloseZipArchive(pArchive); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 450 | return err; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Close a ZipArchive, closing the file and freeing the contents. |
| 455 | * |
| 456 | * NOTE: the ZipArchive may not have been fully created. |
| 457 | */ |
| 458 | void mzCloseZipArchive(ZipArchive* pArchive) |
| 459 | { |
| 460 | LOGV("Closing archive %p\n", pArchive); |
| 461 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 462 | free(pArchive->pEntries); |
| 463 | |
| 464 | mzHashTableFree(pArchive->pHash); |
| 465 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 466 | pArchive->pHash = NULL; |
| 467 | pArchive->pEntries = NULL; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Find a matching entry. |
| 472 | * |
| 473 | * Returns NULL if no matching entry found. |
| 474 | */ |
| 475 | const ZipEntry* mzFindZipEntry(const ZipArchive* pArchive, |
| 476 | const char* entryName) |
| 477 | { |
| 478 | unsigned int itemHash = computeHash(entryName, strlen(entryName)); |
| 479 | |
| 480 | return (const ZipEntry*)mzHashTableLookup(pArchive->pHash, |
| 481 | itemHash, (char*) entryName, hashcmpZipName, false); |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Return true if the entry is a symbolic link. |
| 486 | */ |
Narayan Kamath | 3e700cf | 2015-02-23 13:29:16 +0000 | [diff] [blame] | 487 | static bool mzIsZipEntrySymlink(const ZipEntry* pEntry) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 488 | { |
| 489 | if ((pEntry->versionMadeBy & 0xff00) == CENVEM_UNIX) { |
| 490 | return S_ISLNK(pEntry->externalFileAttributes >> 16); |
| 491 | } |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | /* Call processFunction on the uncompressed data of a STORED entry. |
| 496 | */ |
| 497 | static bool processStoredEntry(const ZipArchive *pArchive, |
| 498 | const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction, |
| 499 | void *cookie) |
| 500 | { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 501 | return processFunction(pArchive->addr + pEntry->offset, pEntry->uncompLen, cookie); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static bool processDeflatedEntry(const ZipArchive *pArchive, |
| 505 | const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction, |
| 506 | void *cookie) |
| 507 | { |
caozhiyuan | 2584f9c | 2016-03-11 15:18:29 +0800 | [diff] [blame] | 508 | bool success = false; |
| 509 | unsigned long totalOut = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 510 | unsigned char procBuf[32 * 1024]; |
| 511 | z_stream zstream; |
| 512 | int zerr; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * Initialize the zlib stream. |
| 516 | */ |
| 517 | memset(&zstream, 0, sizeof(zstream)); |
| 518 | zstream.zalloc = Z_NULL; |
| 519 | zstream.zfree = Z_NULL; |
| 520 | zstream.opaque = Z_NULL; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 521 | zstream.next_in = pArchive->addr + pEntry->offset; |
| 522 | zstream.avail_in = pEntry->compLen; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 523 | zstream.next_out = (Bytef*) procBuf; |
| 524 | zstream.avail_out = sizeof(procBuf); |
| 525 | zstream.data_type = Z_UNKNOWN; |
| 526 | |
| 527 | /* |
| 528 | * Use the undocumented "negative window bits" feature to tell zlib |
| 529 | * that there's no zlib header waiting for it. |
| 530 | */ |
| 531 | zerr = inflateInit2(&zstream, -MAX_WBITS); |
| 532 | if (zerr != Z_OK) { |
| 533 | if (zerr == Z_VERSION_ERROR) { |
| 534 | LOGE("Installed zlib is not compatible with linked version (%s)\n", |
| 535 | ZLIB_VERSION); |
| 536 | } else { |
| 537 | LOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr); |
| 538 | } |
| 539 | goto bail; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Loop while we have data. |
| 544 | */ |
| 545 | do { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 546 | /* uncompress the data */ |
| 547 | zerr = inflate(&zstream, Z_NO_FLUSH); |
| 548 | if (zerr != Z_OK && zerr != Z_STREAM_END) { |
Tao Bao | dd4d981 | 2015-07-15 14:13:06 -0700 | [diff] [blame] | 549 | LOGW("zlib inflate call failed (zerr=%d)\n", zerr); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 550 | goto z_bail; |
| 551 | } |
| 552 | |
| 553 | /* write when we're full or when we're done */ |
| 554 | if (zstream.avail_out == 0 || |
| 555 | (zerr == Z_STREAM_END && zstream.avail_out != sizeof(procBuf))) |
| 556 | { |
| 557 | long procSize = zstream.next_out - procBuf; |
| 558 | LOGVV("+++ processing %d bytes\n", (int) procSize); |
| 559 | bool ret = processFunction(procBuf, procSize, cookie); |
| 560 | if (!ret) { |
| 561 | LOGW("Process function elected to fail (in inflate)\n"); |
| 562 | goto z_bail; |
| 563 | } |
| 564 | |
| 565 | zstream.next_out = procBuf; |
| 566 | zstream.avail_out = sizeof(procBuf); |
| 567 | } |
| 568 | } while (zerr == Z_OK); |
| 569 | |
| 570 | assert(zerr == Z_STREAM_END); /* other errors should've been caught */ |
| 571 | |
| 572 | // success! |
caozhiyuan | 2584f9c | 2016-03-11 15:18:29 +0800 | [diff] [blame] | 573 | totalOut = zstream.total_out; |
| 574 | success = true; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 575 | |
| 576 | z_bail: |
| 577 | inflateEnd(&zstream); /* free up any allocated structures */ |
| 578 | |
| 579 | bail: |
caozhiyuan | 2584f9c | 2016-03-11 15:18:29 +0800 | [diff] [blame] | 580 | if (totalOut != pEntry->uncompLen) { |
| 581 | if (success) { // error already shown? |
| 582 | LOGW("Size mismatch on inflated file (%lu vs %u)\n", totalOut, pEntry->uncompLen); |
| 583 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 584 | return false; |
| 585 | } |
| 586 | return true; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Stream the uncompressed data through the supplied function, |
| 591 | * passing cookie to it each time it gets called. processFunction |
| 592 | * may be called more than once. |
| 593 | * |
| 594 | * If processFunction returns false, the operation is abandoned and |
| 595 | * mzProcessZipEntryContents() immediately returns false. |
| 596 | * |
| 597 | * This is useful for calculating the hash of an entry's uncompressed contents. |
| 598 | */ |
| 599 | bool mzProcessZipEntryContents(const ZipArchive *pArchive, |
| 600 | const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction, |
| 601 | void *cookie) |
| 602 | { |
| 603 | bool ret = false; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 604 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 605 | switch (pEntry->compression) { |
| 606 | case STORED: |
| 607 | ret = processStoredEntry(pArchive, pEntry, processFunction, cookie); |
| 608 | break; |
| 609 | case DEFLATED: |
| 610 | ret = processDeflatedEntry(pArchive, pEntry, processFunction, cookie); |
| 611 | break; |
| 612 | default: |
| 613 | LOGE("Unsupported compression type %d for entry '%s'\n", |
| 614 | pEntry->compression, pEntry->fileName); |
| 615 | break; |
| 616 | } |
| 617 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 618 | return ret; |
| 619 | } |
| 620 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 621 | typedef struct { |
| 622 | char *buf; |
| 623 | int bufLen; |
| 624 | } CopyProcessArgs; |
| 625 | |
| 626 | static bool copyProcessFunction(const unsigned char *data, int dataLen, |
| 627 | void *cookie) |
| 628 | { |
| 629 | CopyProcessArgs *args = (CopyProcessArgs *)cookie; |
| 630 | if (dataLen <= args->bufLen) { |
| 631 | memcpy(args->buf, data, dataLen); |
| 632 | args->buf += dataLen; |
| 633 | args->bufLen -= dataLen; |
| 634 | return true; |
| 635 | } |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * Read an entry into a buffer allocated by the caller. |
| 641 | */ |
| 642 | bool mzReadZipEntry(const ZipArchive* pArchive, const ZipEntry* pEntry, |
| 643 | char *buf, int bufLen) |
| 644 | { |
| 645 | CopyProcessArgs args; |
| 646 | bool ret; |
Doug Zongker | 596271f | 2009-04-29 16:52:04 -0700 | [diff] [blame] | 647 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 648 | args.buf = buf; |
| 649 | args.bufLen = bufLen; |
| 650 | ret = mzProcessZipEntryContents(pArchive, pEntry, copyProcessFunction, |
| 651 | (void *)&args); |
| 652 | if (!ret) { |
| 653 | LOGE("Can't extract entry to buffer.\n"); |
| 654 | return false; |
| 655 | } |
| 656 | return true; |
| 657 | } |
| 658 | |
| 659 | static bool writeProcessFunction(const unsigned char *data, int dataLen, |
Doug Zongker | 683c462 | 2009-05-05 17:50:21 -0700 | [diff] [blame] | 660 | void *cookie) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 661 | { |
Colin Cross | 92cdf9c | 2014-02-05 17:30:31 -0800 | [diff] [blame] | 662 | int fd = (int)(intptr_t)cookie; |
Michael Runge | f5d9f89 | 2014-05-06 16:54:42 -0700 | [diff] [blame] | 663 | if (dataLen == 0) { |
| 664 | return true; |
| 665 | } |
Doug Zongker | 596271f | 2009-04-29 16:52:04 -0700 | [diff] [blame] | 666 | ssize_t soFar = 0; |
Doug Zongker | 683c462 | 2009-05-05 17:50:21 -0700 | [diff] [blame] | 667 | while (true) { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 668 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, data+soFar, dataLen-soFar)); |
Doug Zongker | 683c462 | 2009-05-05 17:50:21 -0700 | [diff] [blame] | 669 | if (n <= 0) { |
Doug Zongker | a930030 | 2014-02-10 12:35:19 -0800 | [diff] [blame] | 670 | LOGE("Error writing %zd bytes from zip file from %p: %s\n", |
Doug Zongker | 683c462 | 2009-05-05 17:50:21 -0700 | [diff] [blame] | 671 | dataLen-soFar, data+soFar, strerror(errno)); |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 672 | return false; |
Doug Zongker | 596271f | 2009-04-29 16:52:04 -0700 | [diff] [blame] | 673 | } else if (n > 0) { |
| 674 | soFar += n; |
| 675 | if (soFar == dataLen) return true; |
| 676 | if (soFar > dataLen) { |
Doug Zongker | a930030 | 2014-02-10 12:35:19 -0800 | [diff] [blame] | 677 | LOGE("write overrun? (%zd bytes instead of %d)\n", |
Doug Zongker | 596271f | 2009-04-29 16:52:04 -0700 | [diff] [blame] | 678 | soFar, dataLen); |
| 679 | return false; |
| 680 | } |
Doug Zongker | 596271f | 2009-04-29 16:52:04 -0700 | [diff] [blame] | 681 | } |
Doug Zongker | 683c462 | 2009-05-05 17:50:21 -0700 | [diff] [blame] | 682 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Uncompress "pEntry" in "pArchive" to "fd" at the current offset. |
| 687 | */ |
| 688 | bool mzExtractZipEntryToFile(const ZipArchive *pArchive, |
Doug Zongker | 1c4ceae | 2009-05-08 09:43:28 -0700 | [diff] [blame] | 689 | const ZipEntry *pEntry, int fd) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 690 | { |
| 691 | bool ret = mzProcessZipEntryContents(pArchive, pEntry, writeProcessFunction, |
Colin Cross | 92cdf9c | 2014-02-05 17:30:31 -0800 | [diff] [blame] | 692 | (void*)(intptr_t)fd); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 693 | if (!ret) { |
| 694 | LOGE("Can't extract entry to file.\n"); |
| 695 | return false; |
| 696 | } |
| 697 | return true; |
| 698 | } |
| 699 | |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 700 | typedef struct { |
| 701 | unsigned char* buffer; |
| 702 | long len; |
| 703 | } BufferExtractCookie; |
| 704 | |
| 705 | static bool bufferProcessFunction(const unsigned char *data, int dataLen, |
| 706 | void *cookie) { |
| 707 | BufferExtractCookie *bec = (BufferExtractCookie*)cookie; |
| 708 | |
| 709 | memmove(bec->buffer, data, dataLen); |
| 710 | bec->buffer += dataLen; |
| 711 | bec->len -= dataLen; |
| 712 | |
| 713 | return true; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Uncompress "pEntry" in "pArchive" to buffer, which must be large |
| 718 | * enough to hold mzGetZipEntryUncomplen(pEntry) bytes. |
| 719 | */ |
| 720 | bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive, |
| 721 | const ZipEntry *pEntry, unsigned char *buffer) |
| 722 | { |
| 723 | BufferExtractCookie bec; |
| 724 | bec.buffer = buffer; |
| 725 | bec.len = mzGetZipEntryUncompLen(pEntry); |
| 726 | |
| 727 | bool ret = mzProcessZipEntryContents(pArchive, pEntry, |
| 728 | bufferProcessFunction, (void*)&bec); |
| 729 | if (!ret || bec.len != 0) { |
| 730 | LOGE("Can't extract entry to memory buffer.\n"); |
| 731 | return false; |
| 732 | } |
| 733 | return true; |
| 734 | } |
| 735 | |
| 736 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 737 | /* Helper state to make path translation easier and less malloc-happy. |
| 738 | */ |
| 739 | typedef struct { |
| 740 | const char *targetDir; |
| 741 | const char *zipDir; |
| 742 | char *buf; |
| 743 | int targetDirLen; |
| 744 | int zipDirLen; |
| 745 | int bufLen; |
| 746 | } MzPathHelper; |
| 747 | |
| 748 | /* Given the values of targetDir and zipDir in the helper, |
| 749 | * return the target filename of the provided entry. |
| 750 | * The helper must be initialized first. |
| 751 | */ |
| 752 | static const char *targetEntryPath(MzPathHelper *helper, ZipEntry *pEntry) |
| 753 | { |
| 754 | int needLen; |
| 755 | bool firstTime = (helper->buf == NULL); |
| 756 | |
| 757 | /* target file <-- targetDir + / + entry[zipDirLen:] |
| 758 | */ |
| 759 | needLen = helper->targetDirLen + 1 + |
| 760 | pEntry->fileNameLen - helper->zipDirLen + 1; |
Tianjie Xu | 6629655 | 2016-02-22 19:49:38 -0800 | [diff] [blame] | 761 | if (firstTime || needLen > helper->bufLen) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 762 | char *newBuf; |
| 763 | |
| 764 | needLen *= 2; |
| 765 | newBuf = (char *)realloc(helper->buf, needLen); |
| 766 | if (newBuf == NULL) { |
| 767 | return NULL; |
| 768 | } |
| 769 | helper->buf = newBuf; |
| 770 | helper->bufLen = needLen; |
| 771 | } |
| 772 | |
| 773 | /* Every path will start with the target path and a slash. |
| 774 | */ |
| 775 | if (firstTime) { |
| 776 | char *p = helper->buf; |
| 777 | memcpy(p, helper->targetDir, helper->targetDirLen); |
| 778 | p += helper->targetDirLen; |
| 779 | if (p == helper->buf || p[-1] != '/') { |
| 780 | helper->targetDirLen += 1; |
| 781 | *p++ = '/'; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /* Replace the custom part of the path with the appropriate |
| 786 | * part of the entry's path. |
| 787 | */ |
| 788 | char *epath = helper->buf + helper->targetDirLen; |
| 789 | memcpy(epath, pEntry->fileName + helper->zipDirLen, |
| 790 | pEntry->fileNameLen - helper->zipDirLen); |
| 791 | epath += pEntry->fileNameLen - helper->zipDirLen; |
| 792 | *epath = '\0'; |
| 793 | |
| 794 | return helper->buf; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Inflate all entries under zipDir to the directory specified by |
| 799 | * targetDir, which must exist and be a writable directory. |
| 800 | * |
| 801 | * The immediate children of zipDir will become the immediate |
| 802 | * children of targetDir; e.g., if the archive contains the entries |
| 803 | * |
| 804 | * a/b/c/one |
| 805 | * a/b/c/two |
| 806 | * a/b/c/d/three |
| 807 | * |
| 808 | * and mzExtractRecursive(a, "a/b/c", "/tmp") is called, the resulting |
| 809 | * files will be |
| 810 | * |
| 811 | * /tmp/one |
| 812 | * /tmp/two |
| 813 | * /tmp/d/three |
| 814 | * |
| 815 | * Returns true on success, false on failure. |
| 816 | */ |
| 817 | bool mzExtractRecursive(const ZipArchive *pArchive, |
| 818 | const char *zipDir, const char *targetDir, |
Narayan Kamath | 9c0f5d6 | 2015-02-23 14:09:31 +0000 | [diff] [blame] | 819 | const struct utimbuf *timestamp, |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 820 | void (*callback)(const char *fn, void *), void *cookie, |
| 821 | struct selabel_handle *sehnd) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 822 | { |
| 823 | if (zipDir[0] == '/') { |
| 824 | LOGE("mzExtractRecursive(): zipDir must be a relative path.\n"); |
| 825 | return false; |
| 826 | } |
| 827 | if (targetDir[0] != '/') { |
| 828 | LOGE("mzExtractRecursive(): targetDir must be an absolute path.\n"); |
| 829 | return false; |
| 830 | } |
| 831 | |
| 832 | unsigned int zipDirLen; |
| 833 | char *zpath; |
| 834 | |
| 835 | zipDirLen = strlen(zipDir); |
| 836 | zpath = (char *)malloc(zipDirLen + 2); |
| 837 | if (zpath == NULL) { |
| 838 | LOGE("Can't allocate %d bytes for zip path\n", zipDirLen + 2); |
| 839 | return false; |
| 840 | } |
| 841 | /* If zipDir is empty, we'll extract the entire zip file. |
| 842 | * Otherwise, canonicalize the path. |
| 843 | */ |
| 844 | if (zipDirLen > 0) { |
| 845 | /* Make sure there's (hopefully, exactly one) slash at the |
| 846 | * end of the path. This way we don't need to worry about |
| 847 | * accidentally extracting "one/twothree" when a path like |
| 848 | * "one/two" is specified. |
| 849 | */ |
| 850 | memcpy(zpath, zipDir, zipDirLen); |
| 851 | if (zpath[zipDirLen-1] != '/') { |
| 852 | zpath[zipDirLen++] = '/'; |
| 853 | } |
| 854 | } |
| 855 | zpath[zipDirLen] = '\0'; |
| 856 | |
| 857 | /* Set up the helper structure that we'll use to assemble paths. |
| 858 | */ |
| 859 | MzPathHelper helper; |
| 860 | helper.targetDir = targetDir; |
| 861 | helper.targetDirLen = strlen(helper.targetDir); |
| 862 | helper.zipDir = zpath; |
| 863 | helper.zipDirLen = strlen(helper.zipDir); |
| 864 | helper.buf = NULL; |
| 865 | helper.bufLen = 0; |
| 866 | |
| 867 | /* Walk through the entries and extract anything whose path begins |
| 868 | * with zpath. |
Nanik Tolaram | 4e8e93b | 2015-02-08 22:31:14 +1100 | [diff] [blame] | 869 | //TODO: since the entries are sorted, binary search for the first match |
| 870 | // and stop after the first non-match. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 871 | */ |
| 872 | unsigned int i; |
| 873 | bool seenMatch = false; |
| 874 | int ok = true; |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 875 | int extractCount = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 876 | for (i = 0; i < pArchive->numEntries; i++) { |
| 877 | ZipEntry *pEntry = pArchive->pEntries + i; |
| 878 | if (pEntry->fileNameLen < zipDirLen) { |
Nanik Tolaram | 4e8e93b | 2015-02-08 22:31:14 +1100 | [diff] [blame] | 879 | //TODO: look out for a single empty directory entry that matches zpath, but |
| 880 | // missing the trailing slash. Most zip files seem to include |
| 881 | // the trailing slash, but I think it's legal to leave it off. |
| 882 | // e.g., zpath "a/b/", entry "a/b", with no children of the entry. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 883 | /* No chance of matching. |
| 884 | */ |
| 885 | #if SORT_ENTRIES |
| 886 | if (seenMatch) { |
| 887 | /* Since the entries are sorted, we can give up |
| 888 | * on the first mismatch after the first match. |
| 889 | */ |
| 890 | break; |
| 891 | } |
| 892 | #endif |
| 893 | continue; |
| 894 | } |
| 895 | /* If zpath is empty, this strncmp() will match everything, |
| 896 | * which is what we want. |
| 897 | */ |
| 898 | if (strncmp(pEntry->fileName, zpath, zipDirLen) != 0) { |
| 899 | #if SORT_ENTRIES |
| 900 | if (seenMatch) { |
| 901 | /* Since the entries are sorted, we can give up |
| 902 | * on the first mismatch after the first match. |
| 903 | */ |
| 904 | break; |
| 905 | } |
| 906 | #endif |
| 907 | continue; |
| 908 | } |
| 909 | /* This entry begins with zipDir, so we'll extract it. |
| 910 | */ |
| 911 | seenMatch = true; |
| 912 | |
| 913 | /* Find the target location of the entry. |
| 914 | */ |
| 915 | const char *targetFile = targetEntryPath(&helper, pEntry); |
| 916 | if (targetFile == NULL) { |
| 917 | LOGE("Can't assemble target path for \"%.*s\"\n", |
| 918 | pEntry->fileNameLen, pEntry->fileName); |
| 919 | ok = false; |
| 920 | break; |
| 921 | } |
| 922 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 923 | #define UNZIP_DIRMODE 0755 |
| 924 | #define UNZIP_FILEMODE 0644 |
Narayan Kamath | 9c0f5d6 | 2015-02-23 14:09:31 +0000 | [diff] [blame] | 925 | /* |
| 926 | * Create the file or directory. We ignore directory entries |
| 927 | * because we recursively create paths to each file entry we encounter |
| 928 | * in the zip archive anyway. |
| 929 | * |
| 930 | * NOTE: A "directory entry" in a zip archive is just a zero length |
| 931 | * entry that ends in a "/". They're not mandatory and many tools get |
| 932 | * rid of them. We need to process them only if we want to preserve |
| 933 | * empty directories from the archive. |
| 934 | */ |
| 935 | if (pEntry->fileName[pEntry->fileNameLen-1] != '/') { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 936 | /* This is not a directory. First, make sure that |
| 937 | * the containing directory exists. |
| 938 | */ |
| 939 | int ret = dirCreateHierarchy( |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 940 | targetFile, UNZIP_DIRMODE, timestamp, true, sehnd); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 941 | if (ret != 0) { |
| 942 | LOGE("Can't create containing directory for \"%s\": %s\n", |
| 943 | targetFile, strerror(errno)); |
| 944 | ok = false; |
| 945 | break; |
| 946 | } |
| 947 | |
Narayan Kamath | 9c0f5d6 | 2015-02-23 14:09:31 +0000 | [diff] [blame] | 948 | /* |
| 949 | * The entry is a regular file or a symlink. Open the target for writing. |
| 950 | * |
| 951 | * TODO: This behavior for symlinks seems rather bizarre. For a |
| 952 | * symlink foo/bar/baz -> foo/tar/taz, we will create a file called |
| 953 | * "foo/bar/baz" whose contents are the literal "foo/tar/taz". We |
| 954 | * warn about this for now and preserve older behavior. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 955 | */ |
Narayan Kamath | 9c0f5d6 | 2015-02-23 14:09:31 +0000 | [diff] [blame] | 956 | if (mzIsZipEntrySymlink(pEntry)) { |
| 957 | LOGE("Symlink entry \"%.*s\" will be output as a regular file.", |
| 958 | pEntry->fileNameLen, pEntry->fileName); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 959 | } |
Narayan Kamath | 9c0f5d6 | 2015-02-23 14:09:31 +0000 | [diff] [blame] | 960 | |
| 961 | char *secontext = NULL; |
| 962 | |
| 963 | if (sehnd) { |
| 964 | selabel_lookup(sehnd, &secontext, targetFile, UNZIP_FILEMODE); |
| 965 | setfscreatecon(secontext); |
| 966 | } |
| 967 | |
| 968 | int fd = open(targetFile, O_CREAT|O_WRONLY|O_TRUNC|O_SYNC, |
| 969 | UNZIP_FILEMODE); |
| 970 | |
| 971 | if (secontext) { |
| 972 | freecon(secontext); |
| 973 | setfscreatecon(NULL); |
| 974 | } |
| 975 | |
| 976 | if (fd < 0) { |
| 977 | LOGE("Can't create target file \"%s\": %s\n", |
| 978 | targetFile, strerror(errno)); |
| 979 | ok = false; |
| 980 | break; |
| 981 | } |
| 982 | |
| 983 | bool ok = mzExtractZipEntryToFile(pArchive, pEntry, fd); |
| 984 | if (ok) { |
| 985 | ok = (fsync(fd) == 0); |
| 986 | } |
| 987 | if (close(fd) != 0) { |
| 988 | ok = false; |
| 989 | } |
| 990 | if (!ok) { |
| 991 | LOGE("Error extracting \"%s\"\n", targetFile); |
| 992 | ok = false; |
| 993 | break; |
| 994 | } |
| 995 | |
| 996 | if (timestamp != NULL && utime(targetFile, timestamp)) { |
| 997 | LOGE("Error touching \"%s\"\n", targetFile); |
| 998 | ok = false; |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | LOGV("Extracted file \"%s\"\n", targetFile); |
| 1003 | ++extractCount; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | if (callback != NULL) callback(targetFile, cookie); |
| 1007 | } |
| 1008 | |
Tao Bao | dd4d981 | 2015-07-15 14:13:06 -0700 | [diff] [blame] | 1009 | LOGV("Extracted %d file(s)\n", extractCount); |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 1010 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1011 | free(helper.buf); |
| 1012 | free(zpath); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1013 | |
| 1014 | return ok; |
| 1015 | } |