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 archive support. |
| 5 | */ |
| 6 | #ifndef _MINZIP_ZIP |
| 7 | #define _MINZIP_ZIP |
| 8 | |
| 9 | #include "inline_magic.h" |
| 10 | |
| 11 | #include <stdlib.h> |
| 12 | #include <utime.h> |
| 13 | |
| 14 | #include "Hash.h" |
| 15 | #include "SysUtil.h" |
| 16 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
| 20 | |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 21 | struct selabel_handle; |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 22 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | /* |
| 24 | * One entry in the Zip archive. Treat this as opaque -- use accessors below. |
| 25 | * |
| 26 | * TODO: we're now keeping the pages mapped so we don't have to copy the |
| 27 | * filename. We can change the accessors to retrieve the various pieces |
| 28 | * directly from the source file instead of copying them out, for a very |
| 29 | * slight speed hit and a modest reduction in memory usage. |
| 30 | */ |
| 31 | typedef struct ZipEntry { |
| 32 | unsigned int fileNameLen; |
| 33 | const char* fileName; // not null-terminated |
caozhiyuan | 2584f9c | 2016-03-11 15:18:29 +0800 | [diff] [blame] | 34 | uint32_t offset; |
| 35 | uint32_t compLen; |
| 36 | uint32_t uncompLen; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 37 | int compression; |
| 38 | long modTime; |
| 39 | long crc32; |
| 40 | int versionMadeBy; |
| 41 | long externalFileAttributes; |
| 42 | } ZipEntry; |
| 43 | |
| 44 | /* |
| 45 | * One Zip archive. Treat as opaque. |
| 46 | */ |
| 47 | typedef struct ZipArchive { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 48 | unsigned int numEntries; |
| 49 | ZipEntry* pEntries; |
| 50 | HashTable* pHash; // maps file name to ZipEntry |
| 51 | unsigned char* addr; |
| 52 | size_t length; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 53 | } ZipArchive; |
| 54 | |
| 55 | /* |
| 56 | * Represents a non-NUL-terminated string, |
| 57 | * which is how entry names are stored. |
| 58 | */ |
| 59 | typedef struct { |
| 60 | const char *str; |
| 61 | size_t len; |
| 62 | } UnterminatedString; |
| 63 | |
| 64 | /* |
| 65 | * Open a Zip archive. |
| 66 | * |
| 67 | * On success, returns 0 and populates "pArchive". Returns nonzero errno |
| 68 | * value on failure. |
| 69 | */ |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 70 | 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] | 71 | |
| 72 | /* |
| 73 | * Close archive, releasing resources associated with it. |
| 74 | * |
| 75 | * Depending on the implementation this could unmap pages used by classes |
| 76 | * stored in a Jar. This should only be done after unloading classes. |
| 77 | */ |
| 78 | void mzCloseZipArchive(ZipArchive* pArchive); |
| 79 | |
| 80 | |
| 81 | /* |
| 82 | * Find an entry in the Zip archive, by name. |
| 83 | */ |
| 84 | const ZipEntry* mzFindZipEntry(const ZipArchive* pArchive, |
| 85 | const char* entryName); |
| 86 | |
caozhiyuan | 2584f9c | 2016-03-11 15:18:29 +0800 | [diff] [blame] | 87 | INLINE uint32_t mzGetZipEntryOffset(const ZipEntry* pEntry) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 88 | return pEntry->offset; |
| 89 | } |
caozhiyuan | 2584f9c | 2016-03-11 15:18:29 +0800 | [diff] [blame] | 90 | INLINE uint32_t mzGetZipEntryUncompLen(const ZipEntry* pEntry) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 91 | return pEntry->uncompLen; |
| 92 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Type definition for the callback function used by |
| 96 | * mzProcessZipEntryContents(). |
| 97 | */ |
| 98 | typedef bool (*ProcessZipEntryContentsFunction)(const unsigned char *data, |
| 99 | int dataLen, void *cookie); |
| 100 | |
| 101 | /* |
| 102 | * Stream the uncompressed data through the supplied function, |
| 103 | * passing cookie to it each time it gets called. processFunction |
| 104 | * may be called more than once. |
| 105 | * |
| 106 | * If processFunction returns false, the operation is abandoned and |
| 107 | * mzProcessZipEntryContents() immediately returns false. |
| 108 | * |
| 109 | * This is useful for calculating the hash of an entry's uncompressed contents. |
| 110 | */ |
| 111 | bool mzProcessZipEntryContents(const ZipArchive *pArchive, |
| 112 | const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction, |
| 113 | void *cookie); |
| 114 | |
| 115 | /* |
| 116 | * Read an entry into a buffer allocated by the caller. |
| 117 | */ |
| 118 | bool mzReadZipEntry(const ZipArchive* pArchive, const ZipEntry* pEntry, |
| 119 | char* buf, int bufLen); |
| 120 | |
| 121 | /* |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 122 | * Inflate and write an entry to a file. |
| 123 | */ |
| 124 | bool mzExtractZipEntryToFile(const ZipArchive *pArchive, |
Doug Zongker | 1c4ceae | 2009-05-08 09:43:28 -0700 | [diff] [blame] | 125 | const ZipEntry *pEntry, int fd); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 126 | |
| 127 | /* |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 128 | * Inflate and write an entry to a memory buffer, which must be long |
| 129 | * enough to hold mzGetZipEntryUncomplen(pEntry) bytes. |
| 130 | */ |
| 131 | bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive, |
| 132 | const ZipEntry *pEntry, unsigned char* buffer); |
| 133 | |
| 134 | /* |
Narayan Kamath | 9c0f5d6 | 2015-02-23 14:09:31 +0000 | [diff] [blame] | 135 | * Inflate all files under zipDir to the directory specified by |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 136 | * targetDir, which must exist and be a writable directory. |
| 137 | * |
Narayan Kamath | 9c0f5d6 | 2015-02-23 14:09:31 +0000 | [diff] [blame] | 138 | * Directory entries and symlinks are not extracted. |
| 139 | * |
| 140 | * |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 141 | * The immediate children of zipDir will become the immediate |
| 142 | * children of targetDir; e.g., if the archive contains the entries |
| 143 | * |
| 144 | * a/b/c/one |
| 145 | * a/b/c/two |
| 146 | * a/b/c/d/three |
| 147 | * |
| 148 | * and mzExtractRecursive(a, "a/b/c", "/tmp", ...) is called, the resulting |
| 149 | * files will be |
| 150 | * |
| 151 | * /tmp/one |
| 152 | * /tmp/two |
| 153 | * /tmp/d/three |
| 154 | * |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 155 | * If timestamp is non-NULL, file timestamps will be set accordingly. |
| 156 | * |
| 157 | * If callback is non-NULL, it will be invoked with each unpacked file. |
| 158 | * |
| 159 | * Returns true on success, false on failure. |
| 160 | */ |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 161 | bool mzExtractRecursive(const ZipArchive *pArchive, |
| 162 | const char *zipDir, const char *targetDir, |
Narayan Kamath | 9c0f5d6 | 2015-02-23 14:09:31 +0000 | [diff] [blame] | 163 | const struct utimbuf *timestamp, |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 164 | void (*callback)(const char *fn, void*), void *cookie, |
| 165 | struct selabel_handle *sehnd); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 166 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 167 | #ifdef __cplusplus |
| 168 | } |
| 169 | #endif |
| 170 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 171 | #endif /*_MINZIP_ZIP*/ |