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