blob: abc98901cd02bb9560de852b437350e301784143 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 */
31enum {
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 Zongker596271f2009-04-29 16:52:04 -070044 CENEXT = 30,
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045 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 Zongker596271f2009-04-29 16:52:04 -070069
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070 LOCVER = 4,
71 LOCFLG = 6,
72 LOCHOW = 8,
73 LOCTIM = 10,
74 LOCCRC = 14,
Doug Zongker596271f2009-04-29 16:52:04 -070075 LOCSIZ = 18,
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076 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
91static void dumpEntry(const ZipEntry* pEntry)
92{
93 LOGI(" %p '%.*s'\n", pEntry->fileName,pEntry->fileNameLen,pEntry->fileName);
94 LOGI(" off=%ld comp=%ld uncomp=%ld how=%d\n", pEntry->offset,
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 */
104static 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 */
119static 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 */
136static 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
146static 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
160static 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 Zongker99916f02014-01-13 14:16:58 -0800187static bool parseZipArchive(ZipArchive* pArchive)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800188{
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 Zongker99916f02014-01-13 14:16:58 -0800199 val = get4LE(pArchive->addr);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 if (val == ENDSIG) {
201 LOGI("Found Zip archive, but it looks empty\n");
202 goto bail;
203 } else if (val != LOCSIG) {
204 LOGV("Not a Zip archive (found 0x%08x)\n", val);
205 goto bail;
206 }
207
208 /*
209 * Find the EOCD. We'll find it immediately unless they have a file
210 * comment.
211 */
Doug Zongker99916f02014-01-13 14:16:58 -0800212 ptr = pArchive->addr + pArchive->length - ENDHDR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213
Doug Zongker99916f02014-01-13 14:16:58 -0800214 while (ptr >= (const unsigned char*) pArchive->addr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215 if (*ptr == (ENDSIG & 0xff) && get4LE(ptr) == ENDSIG)
216 break;
217 ptr--;
218 }
Doug Zongker99916f02014-01-13 14:16:58 -0800219 if (ptr < (const unsigned char*) pArchive->addr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220 LOGI("Could not find end-of-central-directory in Zip\n");
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 Zongker99916f02014-01-13 14:16:58 -0800233 if (numEntries == 0 || cdOffset >= pArchive->length) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800234 LOGW("Invalid entries=%d offset=%d (len=%zd)\n",
Doug Zongker99916f02014-01-13 14:16:58 -0800235 numEntries, cdOffset, pArchive->length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236 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 Zongker99916f02014-01-13 14:16:58 -0800248 ptr = pArchive->addr + cdOffset;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800249 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 Zongker99916f02014-01-13 14:16:58 -0800255 if (ptr + CENHDR > (const unsigned char*)pArchive->addr + pArchive->length) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800256 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 Zongker99916f02014-01-13 14:16:58 -0800269 if (fileName + fileNameLen > (const char*)pArchive->addr + pArchive->length) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 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
330
331 //LOGI("%d: localHdr=%d fnl=%d el=%d cl=%d\n",
332 // i, localHdrOffset, fileNameLen, extraLen, commentLen);
333
334 pEntry->fileNameLen = fileNameLen;
335 pEntry->fileName = fileName;
336
337 pEntry->compLen = get4LE(ptr + CENSIZ);
338 pEntry->uncompLen = get4LE(ptr + CENLEN);
339 pEntry->compression = get2LE(ptr + CENHOW);
340 pEntry->modTime = get4LE(ptr + CENTIM);
341 pEntry->crc32 = get4LE(ptr + CENCRC);
342
343 /* These two are necessary for finding the mode of the file.
344 */
345 pEntry->versionMadeBy = get2LE(ptr + CENVEM);
346 if ((pEntry->versionMadeBy & 0xff00) != 0 &&
347 (pEntry->versionMadeBy & 0xff00) != CENVEM_UNIX)
348 {
349 LOGW("Incompatible \"version made by\": 0x%02x (at %d)\n",
350 pEntry->versionMadeBy >> 8, i);
351 goto bail;
352 }
353 pEntry->externalFileAttributes = get4LE(ptr + CENATX);
354
Doug Zongker99916f02014-01-13 14:16:58 -0800355 // Perform pArchive->addr + localHdrOffset, ensuring that it won't
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800356 // overflow. This is needed because localHdrOffset is untrusted.
Doug Zongker99916f02014-01-13 14:16:58 -0800357 if (!safe_add((uintptr_t *)&localHdr, (uintptr_t)pArchive->addr,
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800358 (uintptr_t)localHdrOffset)) {
359 LOGW("Integer overflow adding in parseZipArchive\n");
360 goto bail;
361 }
362 if ((uintptr_t)localHdr + LOCHDR >
Doug Zongker99916f02014-01-13 14:16:58 -0800363 (uintptr_t)pArchive->addr + pArchive->length) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800364 LOGW("Bad offset to local header: %d (at %d)\n", localHdrOffset, i);
365 goto bail;
366 }
367 if (get4LE(localHdr) != LOCSIG) {
368 LOGW("Missed a local header sig (at %d)\n", i);
369 goto bail;
370 }
371 pEntry->offset = localHdrOffset + LOCHDR
372 + get2LE(localHdr + LOCNAM) + get2LE(localHdr + LOCEXT);
373 if (!safe_add(NULL, pEntry->offset, pEntry->compLen)) {
374 LOGW("Integer overflow adding in parseZipArchive\n");
375 goto bail;
376 }
Doug Zongker99916f02014-01-13 14:16:58 -0800377 if ((size_t)pEntry->offset + pEntry->compLen > pArchive->length) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800378 LOGW("Data ran off the end (at %d)\n", i);
379 goto bail;
380 }
381
382#if !SORT_ENTRIES
383 /* Add to hash table; no need to lock here.
384 * Can't do this now if we're sorting, because entries
385 * will move around.
386 */
387 addEntryToHashTable(pArchive->pHash, pEntry);
388#endif
389
390 //dumpEntry(pEntry);
391 ptr += CENHDR + fileNameLen + extraLen + commentLen;
392 }
393
394#if SORT_ENTRIES
395 /* If we're sorting, we have to wait until all entries
396 * are in their final places, otherwise the pointers will
397 * probably point to the wrong things.
398 */
399 for (i = 0; i < numEntries; i++) {
400 /* Add to hash table; no need to lock here.
401 */
402 addEntryToHashTable(pArchive->pHash, &pArchive->pEntries[i]);
403 }
404#endif
405
406 result = true;
407
408bail:
409 if (!result) {
410 mzHashTableFree(pArchive->pHash);
411 pArchive->pHash = NULL;
412 }
413 return result;
414}
415
416/*
417 * Open a Zip archive and scan out the contents.
418 *
419 * The easiest way to do this is to mmap() the whole thing and do the
420 * traditional backward scan for central directory. Since the EOCD is
421 * a relatively small bit at the end, we should end up only touching a
422 * small set of pages.
423 *
424 * This will be called on non-Zip files, especially during startup, so
425 * we don't want to be too noisy about failures. (Do we want a "quiet"
426 * flag?)
427 *
428 * On success, we fill out the contents of "pArchive".
429 */
Doug Zongker99916f02014-01-13 14:16:58 -0800430int mzOpenZipArchive(unsigned char* addr, size_t length, ZipArchive* pArchive)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800431{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800432 int err;
433
Doug Zongker99916f02014-01-13 14:16:58 -0800434 if (length < ENDHDR) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800435 err = -1;
436 LOGV("File '%s' too small to be zip (%zd)\n", fileName, map.length);
437 goto bail;
438 }
439
Doug Zongker99916f02014-01-13 14:16:58 -0800440 pArchive->addr = addr;
441 pArchive->length = length;
442
443 if (!parseZipArchive(pArchive)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800444 err = -1;
445 LOGV("Parsing '%s' failed\n", fileName);
446 goto bail;
447 }
448
449 err = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800450
451bail:
452 if (err != 0)
453 mzCloseZipArchive(pArchive);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800454 return err;
455}
456
457/*
458 * Close a ZipArchive, closing the file and freeing the contents.
459 *
460 * NOTE: the ZipArchive may not have been fully created.
461 */
462void mzCloseZipArchive(ZipArchive* pArchive)
463{
464 LOGV("Closing archive %p\n", pArchive);
465
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800466 free(pArchive->pEntries);
467
468 mzHashTableFree(pArchive->pHash);
469
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800470 pArchive->pHash = NULL;
471 pArchive->pEntries = NULL;
472}
473
474/*
475 * Find a matching entry.
476 *
477 * Returns NULL if no matching entry found.
478 */
479const ZipEntry* mzFindZipEntry(const ZipArchive* pArchive,
480 const char* entryName)
481{
482 unsigned int itemHash = computeHash(entryName, strlen(entryName));
483
484 return (const ZipEntry*)mzHashTableLookup(pArchive->pHash,
485 itemHash, (char*) entryName, hashcmpZipName, false);
486}
487
488/*
489 * Return true if the entry is a symbolic link.
490 */
491bool mzIsZipEntrySymlink(const ZipEntry* pEntry)
492{
493 if ((pEntry->versionMadeBy & 0xff00) == CENVEM_UNIX) {
494 return S_ISLNK(pEntry->externalFileAttributes >> 16);
495 }
496 return false;
497}
498
499/* Call processFunction on the uncompressed data of a STORED entry.
500 */
501static bool processStoredEntry(const ZipArchive *pArchive,
502 const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction,
503 void *cookie)
504{
Doug Zongker99916f02014-01-13 14:16:58 -0800505 return processFunction(pArchive->addr + pEntry->offset, pEntry->uncompLen, cookie);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800506}
507
508static bool processDeflatedEntry(const ZipArchive *pArchive,
509 const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction,
510 void *cookie)
511{
512 long result = -1;
513 unsigned char readBuf[32 * 1024];
514 unsigned char procBuf[32 * 1024];
515 z_stream zstream;
516 int zerr;
517 long compRemaining;
518
519 compRemaining = pEntry->compLen;
520
521 /*
522 * Initialize the zlib stream.
523 */
524 memset(&zstream, 0, sizeof(zstream));
525 zstream.zalloc = Z_NULL;
526 zstream.zfree = Z_NULL;
527 zstream.opaque = Z_NULL;
Doug Zongker99916f02014-01-13 14:16:58 -0800528 zstream.next_in = pArchive->addr + pEntry->offset;
529 zstream.avail_in = pEntry->compLen;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800530 zstream.next_out = (Bytef*) procBuf;
531 zstream.avail_out = sizeof(procBuf);
532 zstream.data_type = Z_UNKNOWN;
533
534 /*
535 * Use the undocumented "negative window bits" feature to tell zlib
536 * that there's no zlib header waiting for it.
537 */
538 zerr = inflateInit2(&zstream, -MAX_WBITS);
539 if (zerr != Z_OK) {
540 if (zerr == Z_VERSION_ERROR) {
541 LOGE("Installed zlib is not compatible with linked version (%s)\n",
542 ZLIB_VERSION);
543 } else {
544 LOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
545 }
546 goto bail;
547 }
548
549 /*
550 * Loop while we have data.
551 */
552 do {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800553 /* uncompress the data */
554 zerr = inflate(&zstream, Z_NO_FLUSH);
555 if (zerr != Z_OK && zerr != Z_STREAM_END) {
556 LOGD("zlib inflate call failed (zerr=%d)\n", zerr);
557 goto z_bail;
558 }
559
560 /* write when we're full or when we're done */
561 if (zstream.avail_out == 0 ||
562 (zerr == Z_STREAM_END && zstream.avail_out != sizeof(procBuf)))
563 {
564 long procSize = zstream.next_out - procBuf;
565 LOGVV("+++ processing %d bytes\n", (int) procSize);
566 bool ret = processFunction(procBuf, procSize, cookie);
567 if (!ret) {
568 LOGW("Process function elected to fail (in inflate)\n");
569 goto z_bail;
570 }
571
572 zstream.next_out = procBuf;
573 zstream.avail_out = sizeof(procBuf);
574 }
575 } while (zerr == Z_OK);
576
577 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
578
579 // success!
580 result = zstream.total_out;
581
582z_bail:
583 inflateEnd(&zstream); /* free up any allocated structures */
584
585bail:
586 if (result != pEntry->uncompLen) {
587 if (result != -1) // error already shown?
588 LOGW("Size mismatch on inflated file (%ld vs %ld)\n",
589 result, pEntry->uncompLen);
590 return false;
591 }
592 return true;
593}
594
595/*
596 * Stream the uncompressed data through the supplied function,
597 * passing cookie to it each time it gets called. processFunction
598 * may be called more than once.
599 *
600 * If processFunction returns false, the operation is abandoned and
601 * mzProcessZipEntryContents() immediately returns false.
602 *
603 * This is useful for calculating the hash of an entry's uncompressed contents.
604 */
605bool mzProcessZipEntryContents(const ZipArchive *pArchive,
606 const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction,
607 void *cookie)
608{
609 bool ret = false;
610 off_t oldOff;
611
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800612 switch (pEntry->compression) {
613 case STORED:
614 ret = processStoredEntry(pArchive, pEntry, processFunction, cookie);
615 break;
616 case DEFLATED:
617 ret = processDeflatedEntry(pArchive, pEntry, processFunction, cookie);
618 break;
619 default:
620 LOGE("Unsupported compression type %d for entry '%s'\n",
621 pEntry->compression, pEntry->fileName);
622 break;
623 }
624
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800625 return ret;
626}
627
628static bool crcProcessFunction(const unsigned char *data, int dataLen,
629 void *crc)
630{
631 *(unsigned long *)crc = crc32(*(unsigned long *)crc, data, dataLen);
632 return true;
633}
634
635/*
636 * Check the CRC on this entry; return true if it is correct.
637 * May do other internal checks as well.
638 */
639bool mzIsZipEntryIntact(const ZipArchive *pArchive, const ZipEntry *pEntry)
640{
641 unsigned long crc;
642 bool ret;
643
644 crc = crc32(0L, Z_NULL, 0);
645 ret = mzProcessZipEntryContents(pArchive, pEntry, crcProcessFunction,
646 (void *)&crc);
647 if (!ret) {
648 LOGE("Can't calculate CRC for entry\n");
649 return false;
650 }
651 if (crc != (unsigned long)pEntry->crc32) {
652 LOGW("CRC for entry %.*s (0x%08lx) != expected (0x%08lx)\n",
653 pEntry->fileNameLen, pEntry->fileName, crc, pEntry->crc32);
654 return false;
655 }
656 return true;
657}
658
659typedef struct {
660 char *buf;
661 int bufLen;
662} CopyProcessArgs;
663
664static bool copyProcessFunction(const unsigned char *data, int dataLen,
665 void *cookie)
666{
667 CopyProcessArgs *args = (CopyProcessArgs *)cookie;
668 if (dataLen <= args->bufLen) {
669 memcpy(args->buf, data, dataLen);
670 args->buf += dataLen;
671 args->bufLen -= dataLen;
672 return true;
673 }
674 return false;
675}
676
677/*
678 * Read an entry into a buffer allocated by the caller.
679 */
680bool mzReadZipEntry(const ZipArchive* pArchive, const ZipEntry* pEntry,
681 char *buf, int bufLen)
682{
683 CopyProcessArgs args;
684 bool ret;
Doug Zongker596271f2009-04-29 16:52:04 -0700685
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800686 args.buf = buf;
687 args.bufLen = bufLen;
688 ret = mzProcessZipEntryContents(pArchive, pEntry, copyProcessFunction,
689 (void *)&args);
690 if (!ret) {
691 LOGE("Can't extract entry to buffer.\n");
692 return false;
693 }
694 return true;
695}
696
697static bool writeProcessFunction(const unsigned char *data, int dataLen,
Doug Zongker683c4622009-05-05 17:50:21 -0700698 void *cookie)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800699{
Colin Cross92cdf9c2014-02-05 17:30:31 -0800700 int fd = (int)(intptr_t)cookie;
Doug Zongker683c4622009-05-05 17:50:21 -0700701
Doug Zongker596271f2009-04-29 16:52:04 -0700702 ssize_t soFar = 0;
Doug Zongker683c4622009-05-05 17:50:21 -0700703 while (true) {
Doug Zongker1c4ceae2009-05-08 09:43:28 -0700704 ssize_t n = write(fd, data+soFar, dataLen-soFar);
Doug Zongker683c4622009-05-05 17:50:21 -0700705 if (n <= 0) {
Doug Zongkera9300302014-02-10 12:35:19 -0800706 LOGE("Error writing %zd bytes from zip file from %p: %s\n",
Doug Zongker683c4622009-05-05 17:50:21 -0700707 dataLen-soFar, data+soFar, strerror(errno));
Doug Zongker1c4ceae2009-05-08 09:43:28 -0700708 if (errno != EINTR) {
709 return false;
Doug Zongker683c4622009-05-05 17:50:21 -0700710 }
Doug Zongker596271f2009-04-29 16:52:04 -0700711 } else if (n > 0) {
712 soFar += n;
713 if (soFar == dataLen) return true;
714 if (soFar > dataLen) {
Doug Zongkera9300302014-02-10 12:35:19 -0800715 LOGE("write overrun? (%zd bytes instead of %d)\n",
Doug Zongker596271f2009-04-29 16:52:04 -0700716 soFar, dataLen);
717 return false;
718 }
Doug Zongker596271f2009-04-29 16:52:04 -0700719 }
Doug Zongker683c4622009-05-05 17:50:21 -0700720 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800721}
722
723/*
724 * Uncompress "pEntry" in "pArchive" to "fd" at the current offset.
725 */
726bool mzExtractZipEntryToFile(const ZipArchive *pArchive,
Doug Zongker1c4ceae2009-05-08 09:43:28 -0700727 const ZipEntry *pEntry, int fd)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800728{
729 bool ret = mzProcessZipEntryContents(pArchive, pEntry, writeProcessFunction,
Colin Cross92cdf9c2014-02-05 17:30:31 -0800730 (void*)(intptr_t)fd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800731 if (!ret) {
732 LOGE("Can't extract entry to file.\n");
733 return false;
734 }
735 return true;
736}
737
Doug Zongkera9300302014-02-10 12:35:19 -0800738/*
739 * Obtain a pointer to the in-memory representation of a stored entry.
740 */
741bool mzGetStoredEntry(const ZipArchive *pArchive,
742 const ZipEntry *pEntry, unsigned char **addr, size_t *length)
743{
744 if (pEntry->compression != STORED) {
745 LOGE("Can't getStoredEntry for '%s'; not stored\n",
746 pEntry->fileName);
747 return false;
748 }
749
750 *addr = pArchive->addr + pEntry->offset;
751 *length = pEntry->uncompLen;
752 return true;
753}
754
Doug Zongker6aece332010-02-01 14:40:12 -0800755typedef struct {
756 unsigned char* buffer;
757 long len;
758} BufferExtractCookie;
759
760static bool bufferProcessFunction(const unsigned char *data, int dataLen,
761 void *cookie) {
762 BufferExtractCookie *bec = (BufferExtractCookie*)cookie;
763
764 memmove(bec->buffer, data, dataLen);
765 bec->buffer += dataLen;
766 bec->len -= dataLen;
767
768 return true;
769}
770
771/*
772 * Uncompress "pEntry" in "pArchive" to buffer, which must be large
773 * enough to hold mzGetZipEntryUncomplen(pEntry) bytes.
774 */
775bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive,
776 const ZipEntry *pEntry, unsigned char *buffer)
777{
778 BufferExtractCookie bec;
779 bec.buffer = buffer;
780 bec.len = mzGetZipEntryUncompLen(pEntry);
781
782 bool ret = mzProcessZipEntryContents(pArchive, pEntry,
783 bufferProcessFunction, (void*)&bec);
784 if (!ret || bec.len != 0) {
785 LOGE("Can't extract entry to memory buffer.\n");
786 return false;
787 }
788 return true;
789}
790
791
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800792/* Helper state to make path translation easier and less malloc-happy.
793 */
794typedef struct {
795 const char *targetDir;
796 const char *zipDir;
797 char *buf;
798 int targetDirLen;
799 int zipDirLen;
800 int bufLen;
801} MzPathHelper;
802
803/* Given the values of targetDir and zipDir in the helper,
804 * return the target filename of the provided entry.
805 * The helper must be initialized first.
806 */
807static const char *targetEntryPath(MzPathHelper *helper, ZipEntry *pEntry)
808{
809 int needLen;
810 bool firstTime = (helper->buf == NULL);
811
812 /* target file <-- targetDir + / + entry[zipDirLen:]
813 */
814 needLen = helper->targetDirLen + 1 +
815 pEntry->fileNameLen - helper->zipDirLen + 1;
816 if (needLen > helper->bufLen) {
817 char *newBuf;
818
819 needLen *= 2;
820 newBuf = (char *)realloc(helper->buf, needLen);
821 if (newBuf == NULL) {
822 return NULL;
823 }
824 helper->buf = newBuf;
825 helper->bufLen = needLen;
826 }
827
828 /* Every path will start with the target path and a slash.
829 */
830 if (firstTime) {
831 char *p = helper->buf;
832 memcpy(p, helper->targetDir, helper->targetDirLen);
833 p += helper->targetDirLen;
834 if (p == helper->buf || p[-1] != '/') {
835 helper->targetDirLen += 1;
836 *p++ = '/';
837 }
838 }
839
840 /* Replace the custom part of the path with the appropriate
841 * part of the entry's path.
842 */
843 char *epath = helper->buf + helper->targetDirLen;
844 memcpy(epath, pEntry->fileName + helper->zipDirLen,
845 pEntry->fileNameLen - helper->zipDirLen);
846 epath += pEntry->fileNameLen - helper->zipDirLen;
847 *epath = '\0';
848
849 return helper->buf;
850}
851
852/*
853 * Inflate all entries under zipDir to the directory specified by
854 * targetDir, which must exist and be a writable directory.
855 *
856 * The immediate children of zipDir will become the immediate
857 * children of targetDir; e.g., if the archive contains the entries
858 *
859 * a/b/c/one
860 * a/b/c/two
861 * a/b/c/d/three
862 *
863 * and mzExtractRecursive(a, "a/b/c", "/tmp") is called, the resulting
864 * files will be
865 *
866 * /tmp/one
867 * /tmp/two
868 * /tmp/d/three
869 *
870 * Returns true on success, false on failure.
871 */
872bool mzExtractRecursive(const ZipArchive *pArchive,
873 const char *zipDir, const char *targetDir,
874 int flags, const struct utimbuf *timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500875 void (*callback)(const char *fn, void *), void *cookie,
876 struct selabel_handle *sehnd)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800877{
878 if (zipDir[0] == '/') {
879 LOGE("mzExtractRecursive(): zipDir must be a relative path.\n");
880 return false;
881 }
882 if (targetDir[0] != '/') {
883 LOGE("mzExtractRecursive(): targetDir must be an absolute path.\n");
884 return false;
885 }
886
887 unsigned int zipDirLen;
888 char *zpath;
889
890 zipDirLen = strlen(zipDir);
891 zpath = (char *)malloc(zipDirLen + 2);
892 if (zpath == NULL) {
893 LOGE("Can't allocate %d bytes for zip path\n", zipDirLen + 2);
894 return false;
895 }
896 /* If zipDir is empty, we'll extract the entire zip file.
897 * Otherwise, canonicalize the path.
898 */
899 if (zipDirLen > 0) {
900 /* Make sure there's (hopefully, exactly one) slash at the
901 * end of the path. This way we don't need to worry about
902 * accidentally extracting "one/twothree" when a path like
903 * "one/two" is specified.
904 */
905 memcpy(zpath, zipDir, zipDirLen);
906 if (zpath[zipDirLen-1] != '/') {
907 zpath[zipDirLen++] = '/';
908 }
909 }
910 zpath[zipDirLen] = '\0';
911
912 /* Set up the helper structure that we'll use to assemble paths.
913 */
914 MzPathHelper helper;
915 helper.targetDir = targetDir;
916 helper.targetDirLen = strlen(helper.targetDir);
917 helper.zipDir = zpath;
918 helper.zipDirLen = strlen(helper.zipDir);
919 helper.buf = NULL;
920 helper.bufLen = 0;
921
922 /* Walk through the entries and extract anything whose path begins
923 * with zpath.
924//TODO: since the entries are sorted, binary search for the first match
925// and stop after the first non-match.
926 */
927 unsigned int i;
928 bool seenMatch = false;
929 int ok = true;
Doug Zongkerbf80f492012-10-19 12:24:26 -0700930 int extractCount = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800931 for (i = 0; i < pArchive->numEntries; i++) {
932 ZipEntry *pEntry = pArchive->pEntries + i;
933 if (pEntry->fileNameLen < zipDirLen) {
934//TODO: look out for a single empty directory entry that matches zpath, but
935// missing the trailing slash. Most zip files seem to include
936// the trailing slash, but I think it's legal to leave it off.
937// e.g., zpath "a/b/", entry "a/b", with no children of the entry.
938 /* No chance of matching.
939 */
940#if SORT_ENTRIES
941 if (seenMatch) {
942 /* Since the entries are sorted, we can give up
943 * on the first mismatch after the first match.
944 */
945 break;
946 }
947#endif
948 continue;
949 }
950 /* If zpath is empty, this strncmp() will match everything,
951 * which is what we want.
952 */
953 if (strncmp(pEntry->fileName, zpath, zipDirLen) != 0) {
954#if SORT_ENTRIES
955 if (seenMatch) {
956 /* Since the entries are sorted, we can give up
957 * on the first mismatch after the first match.
958 */
959 break;
960 }
961#endif
962 continue;
963 }
964 /* This entry begins with zipDir, so we'll extract it.
965 */
966 seenMatch = true;
967
968 /* Find the target location of the entry.
969 */
970 const char *targetFile = targetEntryPath(&helper, pEntry);
971 if (targetFile == NULL) {
972 LOGE("Can't assemble target path for \"%.*s\"\n",
973 pEntry->fileNameLen, pEntry->fileName);
974 ok = false;
975 break;
976 }
977
978 /* With DRY_RUN set, invoke the callback but don't do anything else.
979 */
980 if (flags & MZ_EXTRACT_DRY_RUN) {
981 if (callback != NULL) callback(targetFile, cookie);
982 continue;
983 }
984
985 /* Create the file or directory.
986 */
987#define UNZIP_DIRMODE 0755
988#define UNZIP_FILEMODE 0644
989 if (pEntry->fileName[pEntry->fileNameLen-1] == '/') {
990 if (!(flags & MZ_EXTRACT_FILES_ONLY)) {
991 int ret = dirCreateHierarchy(
Stephen Smalley779701d2012-02-09 14:13:23 -0500992 targetFile, UNZIP_DIRMODE, timestamp, false, sehnd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800993 if (ret != 0) {
994 LOGE("Can't create containing directory for \"%s\": %s\n",
995 targetFile, strerror(errno));
996 ok = false;
997 break;
998 }
999 LOGD("Extracted dir \"%s\"\n", targetFile);
1000 }
1001 } else {
1002 /* This is not a directory. First, make sure that
1003 * the containing directory exists.
1004 */
1005 int ret = dirCreateHierarchy(
Stephen Smalley779701d2012-02-09 14:13:23 -05001006 targetFile, UNZIP_DIRMODE, timestamp, true, sehnd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001007 if (ret != 0) {
1008 LOGE("Can't create containing directory for \"%s\": %s\n",
1009 targetFile, strerror(errno));
1010 ok = false;
1011 break;
1012 }
1013
1014 /* With FILES_ONLY set, we need to ignore metadata entirely,
1015 * so treat symlinks as regular files.
1016 */
1017 if (!(flags & MZ_EXTRACT_FILES_ONLY) && mzIsZipEntrySymlink(pEntry)) {
1018 /* The entry is a symbolic link.
1019 * The relative target of the symlink is in the
1020 * data section of this entry.
1021 */
1022 if (pEntry->uncompLen == 0) {
1023 LOGE("Symlink entry \"%s\" has no target\n",
1024 targetFile);
1025 ok = false;
1026 break;
1027 }
1028 char *linkTarget = malloc(pEntry->uncompLen + 1);
1029 if (linkTarget == NULL) {
1030 ok = false;
1031 break;
1032 }
1033 ok = mzReadZipEntry(pArchive, pEntry, linkTarget,
1034 pEntry->uncompLen);
1035 if (!ok) {
1036 LOGE("Can't read symlink target for \"%s\"\n",
1037 targetFile);
1038 free(linkTarget);
1039 break;
1040 }
1041 linkTarget[pEntry->uncompLen] = '\0';
1042
1043 /* Make the link.
1044 */
1045 ret = symlink(linkTarget, targetFile);
1046 if (ret != 0) {
1047 LOGE("Can't symlink \"%s\" to \"%s\": %s\n",
1048 targetFile, linkTarget, strerror(errno));
1049 free(linkTarget);
1050 ok = false;
1051 break;
1052 }
1053 LOGD("Extracted symlink \"%s\" -> \"%s\"\n",
1054 targetFile, linkTarget);
1055 free(linkTarget);
1056 } else {
1057 /* The entry is a regular file.
1058 * Open the target for writing.
1059 */
Stephen Smalley779701d2012-02-09 14:13:23 -05001060
Stephen Smalley779701d2012-02-09 14:13:23 -05001061 char *secontext = NULL;
1062
1063 if (sehnd) {
1064 selabel_lookup(sehnd, &secontext, targetFile, UNZIP_FILEMODE);
1065 setfscreatecon(secontext);
1066 }
Stephen Smalley779701d2012-02-09 14:13:23 -05001067
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001068 int fd = creat(targetFile, UNZIP_FILEMODE);
Stephen Smalley779701d2012-02-09 14:13:23 -05001069
Stephen Smalley779701d2012-02-09 14:13:23 -05001070 if (secontext) {
1071 freecon(secontext);
1072 setfscreatecon(NULL);
1073 }
Stephen Smalley779701d2012-02-09 14:13:23 -05001074
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001075 if (fd < 0) {
1076 LOGE("Can't create target file \"%s\": %s\n",
1077 targetFile, strerror(errno));
1078 ok = false;
1079 break;
1080 }
1081
Doug Zongker1c4ceae2009-05-08 09:43:28 -07001082 bool ok = mzExtractZipEntryToFile(pArchive, pEntry, fd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001083 close(fd);
1084 if (!ok) {
1085 LOGE("Error extracting \"%s\"\n", targetFile);
1086 ok = false;
1087 break;
1088 }
1089
1090 if (timestamp != NULL && utime(targetFile, timestamp)) {
1091 LOGE("Error touching \"%s\"\n", targetFile);
1092 ok = false;
1093 break;
1094 }
1095
Doug Zongkerbf80f492012-10-19 12:24:26 -07001096 LOGV("Extracted file \"%s\"\n", targetFile);
1097 ++extractCount;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001098 }
1099 }
1100
1101 if (callback != NULL) callback(targetFile, cookie);
1102 }
1103
Doug Zongkerbf80f492012-10-19 12:24:26 -07001104 LOGD("Extracted %d file(s)\n", extractCount);
1105
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001106 free(helper.buf);
1107 free(zpath);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001108
1109 return ok;
1110}