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