Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [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 |
| 22 | #include <androidfw/ZipFileRO.h> |
| 23 | #include <utils/Log.h> |
| 24 | #include <utils/Compat.h> |
| 25 | #include <utils/misc.h> |
| 26 | #include <utils/threads.h> |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 27 | #include <ziparchive/zip_archive.h> |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 28 | |
| 29 | #include <zlib.h> |
| 30 | |
| 31 | #include <string.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <errno.h> |
| 34 | #include <assert.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | /* |
| 38 | * We must open binary files using open(path, ... | O_BINARY) under Windows. |
| 39 | * Otherwise strange read errors will happen. |
| 40 | */ |
| 41 | #ifndef O_BINARY |
| 42 | # define O_BINARY 0 |
| 43 | #endif |
| 44 | |
| 45 | using namespace android; |
| 46 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 47 | class _ZipEntryRO { |
| 48 | public: |
| 49 | ZipEntry entry; |
| 50 | ZipEntryName name; |
| 51 | void *cookie; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 52 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 53 | _ZipEntryRO() : cookie(NULL) { |
| 54 | } |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 55 | |
Piotr Jastrzebski | 1a68b07 | 2014-08-08 12:52:39 +0100 | [diff] [blame^] | 56 | ~_ZipEntryRO() { |
| 57 | EndIteration(cookie); |
| 58 | } |
| 59 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 60 | private: |
| 61 | _ZipEntryRO(const _ZipEntryRO& other); |
| 62 | _ZipEntryRO& operator=(const _ZipEntryRO& other); |
| 63 | }; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 64 | |
| 65 | ZipFileRO::~ZipFileRO() { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 66 | CloseArchive(mHandle); |
| 67 | free(mFileName); |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 71 | * Open the specified file read-only. We memory-map the entire thing and |
| 72 | * close the file before returning. |
| 73 | */ |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 74 | /* static */ ZipFileRO* ZipFileRO::open(const char* zipFileName) |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 75 | { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 76 | ZipArchiveHandle handle; |
| 77 | const int32_t error = OpenArchive(zipFileName, &handle); |
| 78 | if (error) { |
| 79 | ALOGW("Error opening archive %s: %s", zipFileName, ErrorCodeString(error)); |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 83 | return new ZipFileRO(handle, strdup(zipFileName)); |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 86 | |
| 87 | ZipEntryRO ZipFileRO::findEntryByName(const char* entryName) const |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 88 | { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 89 | _ZipEntryRO* data = new _ZipEntryRO; |
| 90 | const int32_t error = FindEntry(mHandle, entryName, &(data->entry)); |
| 91 | if (error) { |
| 92 | delete data; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 93 | return NULL; |
| 94 | } |
| 95 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 96 | data->name.name = entryName; |
| 97 | data->name.name_length = strlen(entryName); |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 98 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 99 | return (ZipEntryRO) data; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Get the useful fields from the zip entry. |
| 104 | * |
| 105 | * Returns "false" if the offsets to the fields or the contents of the fields |
| 106 | * appear to be bogus. |
| 107 | */ |
| 108 | bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen, |
| 109 | size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) const |
| 110 | { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 111 | const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry); |
| 112 | const ZipEntry& ze = zipEntry->entry; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 113 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 114 | if (pMethod != NULL) { |
| 115 | *pMethod = ze.method; |
Kenny Root | 0d6c2d7 | 2013-08-21 10:40:16 -0700 | [diff] [blame] | 116 | } |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 117 | if (pUncompLen != NULL) { |
| 118 | *pUncompLen = ze.uncompressed_length; |
| 119 | } |
| 120 | if (pCompLen != NULL) { |
| 121 | *pCompLen = ze.compressed_length; |
| 122 | } |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 123 | if (pOffset != NULL) { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 124 | *pOffset = ze.offset; |
| 125 | } |
| 126 | if (pModWhen != NULL) { |
| 127 | *pModWhen = ze.mod_time; |
| 128 | } |
| 129 | if (pCrc32 != NULL) { |
| 130 | *pCrc32 = ze.crc32; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 136 | bool ZipFileRO::startIteration(void** cookie) |
| 137 | { |
| 138 | _ZipEntryRO* ze = new _ZipEntryRO; |
| 139 | int32_t error = StartIteration(mHandle, &(ze->cookie), NULL /* prefix */); |
| 140 | if (error) { |
| 141 | ALOGW("Could not start iteration over %s: %s", mFileName, ErrorCodeString(error)); |
| 142 | delete ze; |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | *cookie = ze; |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | ZipEntryRO ZipFileRO::nextEntry(void* cookie) |
| 151 | { |
| 152 | _ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie); |
| 153 | int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name)); |
| 154 | if (error) { |
| 155 | if (error != -1) { |
| 156 | ALOGW("Error iteration over %s: %s", mFileName, ErrorCodeString(error)); |
| 157 | } |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | return &(ze->entry); |
| 162 | } |
| 163 | |
| 164 | void ZipFileRO::endIteration(void* cookie) |
| 165 | { |
| 166 | delete reinterpret_cast<_ZipEntryRO*>(cookie); |
| 167 | } |
| 168 | |
| 169 | void ZipFileRO::releaseEntry(ZipEntryRO entry) const |
| 170 | { |
| 171 | delete reinterpret_cast<_ZipEntryRO*>(entry); |
| 172 | } |
| 173 | |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 174 | /* |
| 175 | * Copy the entry's filename to the buffer. |
| 176 | */ |
| 177 | int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen) |
| 178 | const |
| 179 | { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 180 | const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry); |
| 181 | const uint16_t requiredSize = zipEntry->name.name_length + 1; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 182 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 183 | if (bufLen < requiredSize) { |
| 184 | ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize); |
| 185 | return requiredSize; |
| 186 | } |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 187 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 188 | memcpy(buffer, zipEntry->name.name, requiredSize - 1); |
| 189 | buffer[requiredSize - 1] = '\0'; |
| 190 | |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Create a new FileMap object that spans the data in "entry". |
| 196 | */ |
| 197 | FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const |
| 198 | { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 199 | const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry); |
| 200 | const ZipEntry& ze = zipEntry->entry; |
| 201 | int fd = GetFileDescriptor(mHandle); |
| 202 | size_t actualLen = 0; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 203 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 204 | if (ze.method == kCompressStored) { |
| 205 | actualLen = ze.uncompressed_length; |
Kenny Root | 0d6c2d7 | 2013-08-21 10:40:16 -0700 | [diff] [blame] | 206 | } else { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 207 | actualLen = ze.compressed_length; |
Kenny Root | 0d6c2d7 | 2013-08-21 10:40:16 -0700 | [diff] [blame] | 208 | } |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 209 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 210 | FileMap* newMap = new FileMap(); |
| 211 | if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) { |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 212 | newMap->release(); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | return newMap; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Uncompress an entry, in its entirety, into the provided output buffer. |
| 221 | * |
| 222 | * This doesn't verify the data's CRC, which might be useful for |
| 223 | * uncompressed data. The caller should be able to manage it. |
| 224 | */ |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 225 | bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 226 | { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 227 | _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry); |
| 228 | const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry), |
| 229 | (uint8_t*) buffer, size); |
| 230 | if (error) { |
| 231 | ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error)); |
Kenny Root | 0d6c2d7 | 2013-08-21 10:40:16 -0700 | [diff] [blame] | 232 | return false; |
| 233 | } |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 234 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 235 | return true; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Uncompress an entry, in its entirety, to an open file descriptor. |
| 240 | * |
| 241 | * This doesn't verify the data's CRC, but probably should. |
| 242 | */ |
| 243 | bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const |
| 244 | { |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 245 | _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry); |
| 246 | const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd); |
| 247 | if (error) { |
| 248 | ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error)); |
Kenny Root | 0d6c2d7 | 2013-08-21 10:40:16 -0700 | [diff] [blame] | 249 | return false; |
| 250 | } |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 251 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 252 | return true; |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 253 | } |