Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | #include "zip_archive.h" |
| 18 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 27 | #include "base/stringprintf.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 28 | #include "base/unix_file/fd_file.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 29 | #include "UniquePtr.h" |
| 30 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 33 | static const size_t kBufSize = 32 * KB; |
| 34 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 35 | // Get 2 little-endian bytes. |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 36 | static uint32_t Le16ToHost(const byte* src) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 37 | return ((src[0] << 0) | |
| 38 | (src[1] << 8)); |
| 39 | } |
| 40 | |
| 41 | // Get 4 little-endian bytes. |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 42 | static uint32_t Le32ToHost(const byte* src) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 43 | return ((src[0] << 0) | |
| 44 | (src[1] << 8) | |
| 45 | (src[2] << 16) | |
| 46 | (src[3] << 24)); |
| 47 | } |
| 48 | |
| 49 | uint16_t ZipEntry::GetCompressionMethod() { |
| 50 | return Le16ToHost(ptr_ + ZipArchive::kCDEMethod); |
| 51 | } |
| 52 | |
| 53 | uint32_t ZipEntry::GetCompressedLength() { |
| 54 | return Le32ToHost(ptr_ + ZipArchive::kCDECompLen); |
| 55 | } |
| 56 | |
| 57 | uint32_t ZipEntry::GetUncompressedLength() { |
| 58 | return Le32ToHost(ptr_ + ZipArchive::kCDEUncompLen); |
| 59 | } |
| 60 | |
| 61 | uint32_t ZipEntry::GetCrc32() { |
| 62 | return Le32ToHost(ptr_ + ZipArchive::kCDECRC); |
| 63 | } |
| 64 | |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 65 | off64_t ZipEntry::GetDataOffset() { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 66 | // All we have is the offset to the Local File Header, which is |
| 67 | // variable size, so we have to read the contents of the struct to |
| 68 | // figure out where the actual data starts. |
| 69 | |
| 70 | // We also need to make sure that the lengths are not so large that |
| 71 | // somebody trying to map the compressed or uncompressed data runs |
| 72 | // off the end of the mapped region. |
| 73 | |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 74 | off64_t dir_offset = zip_archive_->dir_offset_; |
Brian Carlstrom | 0024d6c | 2011-08-09 08:26:12 -0700 | [diff] [blame] | 75 | int64_t lfh_offset = Le32ToHost(ptr_ + ZipArchive::kCDELocalOffset); |
| 76 | if (lfh_offset + ZipArchive::kLFHLen >= dir_offset) { |
| 77 | LOG(WARNING) << "Zip: bad LFH offset in zip"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 81 | if (lseek64(zip_archive_->fd_, lfh_offset, SEEK_SET) != lfh_offset) { |
Brian Carlstrom | 0024d6c | 2011-08-09 08:26:12 -0700 | [diff] [blame] | 82 | PLOG(WARNING) << "Zip: failed seeking to LFH at offset " << lfh_offset; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | uint8_t lfh_buf[ZipArchive::kLFHLen]; |
| 87 | ssize_t actual = TEMP_FAILURE_RETRY(read(zip_archive_->fd_, lfh_buf, sizeof(lfh_buf))); |
| 88 | if (actual != sizeof(lfh_buf)) { |
Brian Carlstrom | 0024d6c | 2011-08-09 08:26:12 -0700 | [diff] [blame] | 89 | LOG(WARNING) << "Zip: failed reading LFH from offset " << lfh_offset; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | if (Le32ToHost(lfh_buf) != ZipArchive::kLFHSignature) { |
Brian Carlstrom | 0024d6c | 2011-08-09 08:26:12 -0700 | [diff] [blame] | 94 | LOG(WARNING) << "Zip: didn't find signature at start of LFH, offset " << lfh_offset; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 98 | uint32_t gpbf = Le16ToHost(lfh_buf + ZipArchive::kLFHGPBFlags); |
| 99 | if ((gpbf & ZipArchive::kGPFUnsupportedMask) != 0) { |
| 100 | LOG(WARNING) << "Invalid General Purpose Bit Flag: " << gpbf; |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | off64_t data_offset = (lfh_offset + ZipArchive::kLFHLen |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 105 | + Le16ToHost(lfh_buf + ZipArchive::kLFHNameLen) |
| 106 | + Le16ToHost(lfh_buf + ZipArchive::kLFHExtraLen)); |
| 107 | if (data_offset >= dir_offset) { |
| 108 | LOG(WARNING) << "Zip: bad data offset " << data_offset << " in zip"; |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | // check lengths |
| 113 | |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 114 | if (static_cast<off64_t>(data_offset + GetCompressedLength()) > dir_offset) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 115 | LOG(WARNING) << "Zip: bad compressed length in zip " |
| 116 | << "(" << data_offset << " + " << GetCompressedLength() |
| 117 | << " > " << dir_offset << ")"; |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | if (GetCompressionMethod() == kCompressStored |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 122 | && static_cast<off64_t>(data_offset + GetUncompressedLength()) > dir_offset) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 123 | LOG(WARNING) << "Zip: bad uncompressed length in zip " |
| 124 | << "(" << data_offset << " + " << GetUncompressedLength() |
| 125 | << " > " << dir_offset << ")"; |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | return data_offset; |
| 130 | } |
| 131 | |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 132 | static bool CopyFdToMemory(uint8_t* begin, size_t size, int in, size_t count) { |
| 133 | uint8_t* dst = begin; |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 134 | std::vector<uint8_t> buf(kBufSize); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 135 | while (count != 0) { |
| 136 | size_t bytes_to_read = (count > kBufSize) ? kBufSize : count; |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 137 | ssize_t actual = TEMP_FAILURE_RETRY(read(in, &buf[0], bytes_to_read)); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 138 | if (actual != static_cast<ssize_t>(bytes_to_read)) { |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 139 | PLOG(WARNING) << "Zip: short read"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 140 | return false; |
| 141 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 142 | memcpy(dst, &buf[0], bytes_to_read); |
| 143 | dst += bytes_to_read; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 144 | count -= bytes_to_read; |
| 145 | } |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 146 | DCHECK_EQ(dst, begin + size); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
| 150 | class ZStream { |
| 151 | public: |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 152 | ZStream(byte* write_buf, size_t write_buf_size) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 153 | // Initialize the zlib stream struct. |
| 154 | memset(&zstream_, 0, sizeof(zstream_)); |
| 155 | zstream_.zalloc = Z_NULL; |
| 156 | zstream_.zfree = Z_NULL; |
| 157 | zstream_.opaque = Z_NULL; |
| 158 | zstream_.next_in = NULL; |
| 159 | zstream_.avail_in = 0; |
| 160 | zstream_.next_out = reinterpret_cast<Bytef*>(write_buf); |
| 161 | zstream_.avail_out = write_buf_size; |
| 162 | zstream_.data_type = Z_UNKNOWN; |
| 163 | } |
| 164 | |
| 165 | z_stream& Get() { |
| 166 | return zstream_; |
| 167 | } |
| 168 | |
| 169 | ~ZStream() { |
| 170 | inflateEnd(&zstream_); |
| 171 | } |
| 172 | private: |
| 173 | z_stream zstream_; |
| 174 | }; |
| 175 | |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 176 | static bool InflateToMemory(uint8_t* begin, size_t size, |
| 177 | int in, size_t uncompressed_length, size_t compressed_length) { |
| 178 | uint8_t* dst = begin; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 179 | UniquePtr<uint8_t[]> read_buf(new uint8_t[kBufSize]); |
| 180 | UniquePtr<uint8_t[]> write_buf(new uint8_t[kBufSize]); |
| 181 | if (read_buf.get() == NULL || write_buf.get() == NULL) { |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 182 | LOG(WARNING) << "Zip: failed to allocate buffer to inflate"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 186 | UniquePtr<ZStream> zstream(new ZStream(write_buf.get(), kBufSize)); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 187 | |
| 188 | // Use the undocumented "negative window bits" feature to tell zlib |
| 189 | // that there's no zlib header waiting for it. |
| 190 | int zerr = inflateInit2(&zstream->Get(), -MAX_WBITS); |
| 191 | if (zerr != Z_OK) { |
| 192 | if (zerr == Z_VERSION_ERROR) { |
| 193 | LOG(ERROR) << "Installed zlib is not compatible with linked version (" << ZLIB_VERSION << ")"; |
| 194 | } else { |
| 195 | LOG(WARNING) << "Call to inflateInit2 failed (zerr=" << zerr << ")"; |
| 196 | } |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | size_t remaining = compressed_length; |
| 201 | do { |
| 202 | // read as much as we can |
| 203 | if (zstream->Get().avail_in == 0) { |
| 204 | size_t bytes_to_read = (remaining > kBufSize) ? kBufSize : remaining; |
| 205 | |
| 206 | ssize_t actual = TEMP_FAILURE_RETRY(read(in, read_buf.get(), bytes_to_read)); |
| 207 | if (actual != static_cast<ssize_t>(bytes_to_read)) { |
| 208 | LOG(WARNING) << "Zip: inflate read failed (" << actual << " vs " << bytes_to_read << ")"; |
| 209 | return false; |
| 210 | } |
| 211 | remaining -= bytes_to_read; |
| 212 | zstream->Get().next_in = read_buf.get(); |
| 213 | zstream->Get().avail_in = bytes_to_read; |
| 214 | } |
| 215 | |
| 216 | // uncompress the data |
| 217 | zerr = inflate(&zstream->Get(), Z_NO_FLUSH); |
| 218 | if (zerr != Z_OK && zerr != Z_STREAM_END) { |
| 219 | LOG(WARNING) << "Zip: inflate zerr=" << zerr |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 220 | << " (next_in=" << zstream->Get().next_in |
| 221 | << " avail_in=" << zstream->Get().avail_in |
| 222 | << " next_out=" << zstream->Get().next_out |
| 223 | << " avail_out=" << zstream->Get().avail_out |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 224 | << ")"; |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | // write when we're full or when we're done |
| 229 | if (zstream->Get().avail_out == 0 || |
| 230 | (zerr == Z_STREAM_END && zstream->Get().avail_out != kBufSize)) { |
| 231 | size_t bytes_to_write = zstream->Get().next_out - write_buf.get(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 232 | memcpy(dst, write_buf.get(), bytes_to_write); |
| 233 | dst += bytes_to_write; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 234 | zstream->Get().next_out = write_buf.get(); |
| 235 | zstream->Get().avail_out = kBufSize; |
| 236 | } |
| 237 | } while (zerr == Z_OK); |
| 238 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 239 | DCHECK_EQ(zerr, Z_STREAM_END); // other errors should've been caught |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 240 | |
| 241 | // paranoia |
| 242 | if (zstream->Get().total_out != uncompressed_length) { |
| 243 | LOG(WARNING) << "Zip: size mismatch on inflated file (" |
| 244 | << zstream->Get().total_out << " vs " << uncompressed_length << ")"; |
| 245 | return false; |
| 246 | } |
| 247 | |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 248 | DCHECK_EQ(dst, begin + size); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 252 | bool ZipEntry::ExtractToFile(File& file, std::string* error_msg) { |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 253 | uint32_t length = GetUncompressedLength(); |
| 254 | int result = TEMP_FAILURE_RETRY(ftruncate(file.Fd(), length)); |
| 255 | if (result == -1) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 256 | *error_msg = StringPrintf("Zip: failed to ftruncate '%s' to length %ud", file.GetPath().c_str(), |
| 257 | length); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 261 | UniquePtr<MemMap> map(MemMap::MapFile(length, PROT_READ | PROT_WRITE, MAP_SHARED, file.Fd(), 0, |
| 262 | file.GetPath().c_str(), error_msg)); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 263 | if (map.get() == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 264 | *error_msg = StringPrintf("Zip: failed to mmap space for '%s': %s", file.GetPath().c_str(), |
| 265 | error_msg->c_str()); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 266 | return false; |
| 267 | } |
| 268 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 269 | return ExtractToMemory(map->Begin(), map->Size(), error_msg); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 272 | bool ZipEntry::ExtractToMemory(uint8_t* begin, size_t size, std::string* error_msg) { |
Brian Carlstrom | afa8ff6 | 2013-07-26 15:58:56 -0700 | [diff] [blame] | 273 | // If size is zero, data offset will be meaningless, so bail out early. |
| 274 | if (size == 0) { |
| 275 | return true; |
| 276 | } |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 277 | off64_t data_offset = GetDataOffset(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 278 | if (data_offset == -1) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 279 | *error_msg = StringPrintf("Zip: data_offset=%lld", data_offset); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 282 | if (lseek64(zip_archive_->fd_, data_offset, SEEK_SET) != data_offset) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 283 | *error_msg = StringPrintf("Zip: lseek to data at %lld failed", data_offset); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | |
| 287 | // TODO: this doesn't verify the data's CRC, but probably should (especially |
| 288 | // for uncompressed data). |
| 289 | switch (GetCompressionMethod()) { |
| 290 | case kCompressStored: |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 291 | return CopyFdToMemory(begin, size, zip_archive_->fd_, GetUncompressedLength()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 292 | case kCompressDeflated: |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 293 | return InflateToMemory(begin, size, zip_archive_->fd_, |
| 294 | GetUncompressedLength(), GetCompressedLength()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 295 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 296 | *error_msg = StringPrintf("Zip: unknown compression method 0x%x", GetCompressionMethod()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 301 | MemMap* ZipEntry::ExtractToMemMap(const char* entry_filename, std::string* error_msg) { |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 302 | std::string name(entry_filename); |
| 303 | name += " extracted in memory from "; |
| 304 | name += entry_filename; |
| 305 | UniquePtr<MemMap> map(MemMap::MapAnonymous(name.c_str(), |
| 306 | NULL, |
| 307 | GetUncompressedLength(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 308 | PROT_READ | PROT_WRITE, error_msg)); |
| 309 | if (map.get() == nullptr) { |
| 310 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 311 | return NULL; |
| 312 | } |
| 313 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 314 | bool success = ExtractToMemory(map->Begin(), map->Size(), error_msg); |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 315 | if (!success) { |
| 316 | LOG(ERROR) << "Zip: Failed to extract '" << entry_filename << "' to memory"; |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | return map.release(); |
| 321 | } |
| 322 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 323 | static void SetCloseOnExec(int fd) { |
| 324 | // This dance is more portable than Linux's O_CLOEXEC open(2) flag. |
| 325 | int flags = fcntl(fd, F_GETFD); |
| 326 | if (flags == -1) { |
| 327 | PLOG(WARNING) << "fcntl(" << fd << ", F_GETFD) failed"; |
| 328 | return; |
| 329 | } |
| 330 | int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 331 | if (rc == -1) { |
| 332 | PLOG(WARNING) << "fcntl(" << fd << ", F_SETFD, " << flags << ") failed"; |
| 333 | return; |
| 334 | } |
| 335 | } |
| 336 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 337 | ZipArchive* ZipArchive::Open(const char* filename, std::string* error_msg) { |
| 338 | DCHECK(filename != nullptr); |
| 339 | int fd = open(filename, O_RDONLY, 0); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 340 | if (fd == -1) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 341 | *error_msg = StringPrintf("Zip: unable to open '%s': %s", filename, strerror(errno)); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 342 | return NULL; |
| 343 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 344 | return OpenFromFd(fd, filename, error_msg); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 347 | ZipArchive* ZipArchive::OpenFromFd(int fd, const char* filename, std::string* error_msg) { |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 348 | SetCloseOnExec(fd); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 349 | UniquePtr<ZipArchive> zip_archive(new ZipArchive(fd, filename)); |
| 350 | CHECK(zip_archive.get() != nullptr); |
| 351 | if (!zip_archive->MapCentralDirectory(error_msg)) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 352 | zip_archive->Close(); |
| 353 | return NULL; |
| 354 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 355 | if (!zip_archive->Parse(error_msg)) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 356 | zip_archive->Close(); |
| 357 | return NULL; |
| 358 | } |
| 359 | return zip_archive.release(); |
| 360 | } |
| 361 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 362 | ZipEntry* ZipArchive::Find(const char* name) const { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 363 | DCHECK(name != NULL); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 364 | DirEntries::const_iterator it = dir_entries_.find(name); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 365 | if (it == dir_entries_.end()) { |
| 366 | return NULL; |
| 367 | } |
| 368 | return new ZipEntry(this, (*it).second); |
| 369 | } |
| 370 | |
| 371 | void ZipArchive::Close() { |
| 372 | if (fd_ != -1) { |
| 373 | close(fd_); |
| 374 | } |
| 375 | fd_ = -1; |
| 376 | num_entries_ = 0; |
| 377 | dir_offset_ = 0; |
| 378 | } |
| 379 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 380 | std::string ZipArchive::ErrorStringPrintf(const char* fmt, ...) { |
| 381 | va_list ap; |
| 382 | va_start(ap, fmt); |
| 383 | std::string result(StringPrintf("Zip '%s' : ", filename_.c_str())); |
| 384 | StringAppendV(&result, fmt, ap); |
| 385 | va_end(ap); |
| 386 | return result; |
| 387 | } |
| 388 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 389 | // Find the zip Central Directory and memory-map it. |
| 390 | // |
| 391 | // On success, returns true after populating fields from the EOCD area: |
| 392 | // num_entries_ |
| 393 | // dir_offset_ |
| 394 | // dir_map_ |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 395 | bool ZipArchive::MapCentralDirectory(std::string* error_msg) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 396 | /* |
| 397 | * Get and test file length. |
| 398 | */ |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 399 | off64_t file_length = lseek64(fd_, 0, SEEK_END); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 400 | if (file_length < kEOCDLen) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 401 | *error_msg = ErrorStringPrintf("length %lld is too small to be zip", file_length); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 402 | return false; |
| 403 | } |
| 404 | |
| 405 | size_t read_amount = kMaxEOCDSearch; |
| 406 | if (file_length < off64_t(read_amount)) { |
| 407 | read_amount = file_length; |
| 408 | } |
| 409 | |
| 410 | UniquePtr<uint8_t[]> scan_buf(new uint8_t[read_amount]); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 411 | CHECK(scan_buf.get() != nullptr); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | * Make sure this is a Zip archive. |
| 415 | */ |
| 416 | if (lseek64(fd_, 0, SEEK_SET) != 0) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 417 | *error_msg = ErrorStringPrintf("seek to start failed: %s", strerror(errno)); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 418 | return false; |
| 419 | } |
| 420 | |
| 421 | ssize_t actual = TEMP_FAILURE_RETRY(read(fd_, scan_buf.get(), sizeof(int32_t))); |
| 422 | if (actual != static_cast<ssize_t>(sizeof(int32_t))) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 423 | *error_msg = ErrorStringPrintf("couldn\'t read first signature from zip archive: %s", |
| 424 | strerror(errno)); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | |
| 428 | unsigned int header = Le32ToHost(scan_buf.get()); |
| 429 | if (header != kLFHSignature) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 430 | *error_msg = ErrorStringPrintf("not a zip archive (found 0x%x)", header); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 431 | return false; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // Perform the traditional EOCD snipe hunt. |
| 435 | // |
| 436 | // We're searching for the End of Central Directory magic number, |
| 437 | // which appears at the start of the EOCD block. It's followed by |
| 438 | // 18 bytes of EOCD stuff and up to 64KB of archive comment. We |
| 439 | // need to read the last part of the file into a buffer, dig through |
| 440 | // it to find the magic number, parse some values out, and use those |
| 441 | // to determine the extent of the CD. |
| 442 | // |
| 443 | // We start by pulling in the last part of the file. |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 444 | off64_t search_start = file_length - read_amount; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 445 | |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 446 | if (lseek64(fd_, search_start, SEEK_SET) != search_start) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 447 | *error_msg = ErrorStringPrintf("seek %lld failed: %s", search_start, strerror(errno)); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 448 | return false; |
| 449 | } |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 450 | actual = TEMP_FAILURE_RETRY(read(fd_, scan_buf.get(), read_amount)); |
| 451 | if (actual != static_cast<ssize_t>(read_amount)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 452 | *error_msg = ErrorStringPrintf("read %lld, expected %zd. %s", search_start, read_amount, |
| 453 | strerror(errno)); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 454 | return false; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | // Scan backward for the EOCD magic. In an archive without a trailing |
| 459 | // comment, we'll find it on the first try. (We may want to consider |
| 460 | // doing an initial minimal read; if we don't find it, retry with a |
| 461 | // second read as above.) |
| 462 | int i; |
| 463 | for (i = read_amount - kEOCDLen; i >= 0; i--) { |
| 464 | if (scan_buf.get()[i] == 0x50 && Le32ToHost(&(scan_buf.get())[i]) == kEOCDSignature) { |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | if (i < 0) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 469 | *error_msg = ErrorStringPrintf("EOCD not found, not a zip file"); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 473 | off64_t eocd_offset = search_start + i; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 474 | const byte* eocd_ptr = scan_buf.get() + i; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 475 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 476 | CHECK(eocd_offset < file_length); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 477 | |
| 478 | // Grab the CD offset and size, and the number of entries in the |
| 479 | // archive. Verify that they look reasonable. |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 480 | uint16_t disk_number = Le16ToHost(eocd_ptr + kEOCDDiskNumber); |
| 481 | uint16_t disk_with_central_dir = Le16ToHost(eocd_ptr + kEOCDDiskNumberForCD); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 482 | uint16_t num_entries = Le16ToHost(eocd_ptr + kEOCDNumEntries); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 483 | uint16_t total_num_entries = Le16ToHost(eocd_ptr + kEOCDTotalNumEntries); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 484 | uint32_t dir_size = Le32ToHost(eocd_ptr + kEOCDSize); |
| 485 | uint32_t dir_offset = Le32ToHost(eocd_ptr + kEOCDFileOffset); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 486 | uint16_t comment_size = Le16ToHost(eocd_ptr + kEOCDCommentSize); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 487 | |
| 488 | if ((uint64_t) dir_offset + (uint64_t) dir_size > (uint64_t) eocd_offset) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 489 | *error_msg = ErrorStringPrintf("bad offsets (dir=%ud, size=%ud, eocd=%lld)", |
| 490 | dir_offset, dir_size, eocd_offset); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | if (num_entries == 0) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 494 | *error_msg = ErrorStringPrintf("empty archive?"); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 495 | return false; |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 496 | } else if (num_entries != total_num_entries || disk_number != 0 || disk_with_central_dir != 0) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 497 | *error_msg = ErrorStringPrintf("spanned archives not supported"); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | |
| 501 | // Check to see if comment is a sane size |
| 502 | if ((comment_size > (file_length - kEOCDLen)) |
| 503 | || (eocd_offset > (file_length - kEOCDLen) - comment_size)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 504 | *error_msg = ErrorStringPrintf("comment size runs off end of file"); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 505 | return false; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // It all looks good. Create a mapping for the CD. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 509 | dir_map_.reset(MemMap::MapFile(dir_size, PROT_READ, MAP_SHARED, fd_, dir_offset, |
| 510 | filename_.c_str(), error_msg)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 511 | if (dir_map_.get() == NULL) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 512 | return false; |
| 513 | } |
| 514 | |
| 515 | num_entries_ = num_entries; |
| 516 | dir_offset_ = dir_offset; |
| 517 | return true; |
| 518 | } |
| 519 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 520 | bool ZipArchive::Parse(std::string* error_msg) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 521 | const byte* cd_ptr = dir_map_->Begin(); |
| 522 | size_t cd_length = dir_map_->Size(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 523 | |
| 524 | // Walk through the central directory, adding entries to the hash |
| 525 | // table and verifying values. |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 526 | const byte* ptr = cd_ptr; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 527 | for (int i = 0; i < num_entries_; i++) { |
| 528 | if (Le32ToHost(ptr) != kCDESignature) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 529 | *error_msg = ErrorStringPrintf("missed a central dir sig (at %d)", i); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | if (ptr + kCDELen > cd_ptr + cd_length) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 533 | *error_msg = ErrorStringPrintf("ran off the end (at %d)", i); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 534 | return false; |
| 535 | } |
| 536 | |
| 537 | int64_t local_hdr_offset = Le32ToHost(ptr + kCDELocalOffset); |
| 538 | if (local_hdr_offset >= dir_offset_) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 539 | *error_msg = ErrorStringPrintf("bad LFH offset %lld at entry %d", local_hdr_offset, i); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 543 | uint16_t gpbf = Le16ToHost(ptr + kCDEGPBFlags); |
| 544 | if ((gpbf & kGPFUnsupportedMask) != 0) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 545 | *error_msg = ErrorStringPrintf("invalid general purpose bit flag %x", gpbf); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 546 | return false; |
| 547 | } |
| 548 | |
| 549 | uint16_t name_len = Le16ToHost(ptr + kCDENameLen); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 550 | uint16_t extra_len = Le16ToHost(ptr + kCDEExtraLen); |
| 551 | uint16_t comment_len = Le16ToHost(ptr + kCDECommentLen); |
| 552 | |
| 553 | // add the CDE filename to the hash table |
| 554 | const char* name = reinterpret_cast<const char*>(ptr + kCDELen); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 555 | |
| 556 | // Check name for NULL characters |
| 557 | if (memchr(name, 0, name_len) != NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 558 | *error_msg = ErrorStringPrintf("filename contains NUL byte"); |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 559 | return false; |
| 560 | } |
| 561 | |
| 562 | dir_entries_.Put(StringPiece(name, name_len), ptr); |
| 563 | ptr += kCDELen + name_len + extra_len + comment_len; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 564 | if (ptr > cd_ptr + cd_length) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 565 | *error_msg = ErrorStringPrintf("bad CD advance (%p vs %p) at entry %d", |
| 566 | ptr, cd_ptr + cd_length, i); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 567 | return false; |
| 568 | } |
| 569 | } |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | } // namespace art |