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