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