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