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