Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 17 | #define LOG_TAG "ZIPARCHIVE" |
| 18 | |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 19 | // Read-only stream access to Zip Archive entries. |
| 20 | #include <errno.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <memory> |
| 27 | #include <vector> |
| 28 | |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 29 | #include <android-base/file.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 30 | #include <log/log.h> |
| 31 | |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 32 | #include <ziparchive/zip_archive.h> |
| 33 | #include <ziparchive/zip_archive_stream_entry.h> |
| 34 | #include <zlib.h> |
| 35 | |
| 36 | #include "zip_archive_private.h" |
| 37 | |
| 38 | static constexpr size_t kBufSize = 65535; |
| 39 | |
| 40 | bool ZipArchiveStreamEntry::Init(const ZipEntry& entry) { |
| 41 | ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle_); |
| 42 | off64_t data_offset = entry.offset; |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 43 | if (!archive->mapped_zip.SeekToOffset(data_offset)) { |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 44 | ALOGW("lseek to data at %" PRId64 " failed: %s", data_offset, strerror(errno)); |
| 45 | return false; |
| 46 | } |
| 47 | crc32_ = entry.crc32; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | class ZipArchiveStreamEntryUncompressed : public ZipArchiveStreamEntry { |
| 52 | public: |
Chih-Hung Hsieh | 3a114e0 | 2016-04-25 14:38:37 -0700 | [diff] [blame] | 53 | explicit ZipArchiveStreamEntryUncompressed(ZipArchiveHandle handle) |
| 54 | : ZipArchiveStreamEntry(handle) {} |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 55 | virtual ~ZipArchiveStreamEntryUncompressed() {} |
| 56 | |
| 57 | const std::vector<uint8_t>* Read() override; |
| 58 | |
| 59 | bool Verify() override; |
| 60 | |
| 61 | protected: |
| 62 | bool Init(const ZipEntry& entry) override; |
| 63 | |
| 64 | uint32_t length_; |
| 65 | |
| 66 | private: |
| 67 | std::vector<uint8_t> data_; |
| 68 | uint32_t computed_crc32_; |
| 69 | }; |
| 70 | |
| 71 | bool ZipArchiveStreamEntryUncompressed::Init(const ZipEntry& entry) { |
| 72 | if (!ZipArchiveStreamEntry::Init(entry)) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | length_ = entry.uncompressed_length; |
| 77 | |
| 78 | data_.resize(kBufSize); |
| 79 | computed_crc32_ = 0; |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | const std::vector<uint8_t>* ZipArchiveStreamEntryUncompressed::Read() { |
| 85 | if (length_ == 0) { |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
| 89 | size_t bytes = (length_ > data_.size()) ? data_.size() : length_; |
| 90 | ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle_); |
| 91 | errno = 0; |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 92 | if (!archive->mapped_zip.ReadData(data_.data(), bytes)) { |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 93 | if (errno != 0) { |
| 94 | ALOGE("Error reading from archive fd: %s", strerror(errno)); |
| 95 | } else { |
| 96 | ALOGE("Short read of zip file, possibly corrupted zip?"); |
| 97 | } |
| 98 | length_ = 0; |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
| 102 | if (bytes < data_.size()) { |
| 103 | data_.resize(bytes); |
| 104 | } |
| 105 | computed_crc32_ = crc32(computed_crc32_, data_.data(), data_.size()); |
| 106 | length_ -= bytes; |
| 107 | return &data_; |
| 108 | } |
| 109 | |
| 110 | bool ZipArchiveStreamEntryUncompressed::Verify() { |
| 111 | return length_ == 0 && crc32_ == computed_crc32_; |
| 112 | } |
| 113 | |
| 114 | class ZipArchiveStreamEntryCompressed : public ZipArchiveStreamEntry { |
| 115 | public: |
Chih-Hung Hsieh | 3a114e0 | 2016-04-25 14:38:37 -0700 | [diff] [blame] | 116 | explicit ZipArchiveStreamEntryCompressed(ZipArchiveHandle handle) |
| 117 | : ZipArchiveStreamEntry(handle) {} |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 118 | virtual ~ZipArchiveStreamEntryCompressed(); |
| 119 | |
| 120 | const std::vector<uint8_t>* Read() override; |
| 121 | |
| 122 | bool Verify() override; |
| 123 | |
| 124 | protected: |
| 125 | bool Init(const ZipEntry& entry) override; |
| 126 | |
| 127 | private: |
| 128 | bool z_stream_init_ = false; |
| 129 | z_stream z_stream_; |
| 130 | std::vector<uint8_t> in_; |
| 131 | std::vector<uint8_t> out_; |
| 132 | uint32_t uncompressed_length_; |
| 133 | uint32_t compressed_length_; |
| 134 | uint32_t computed_crc32_; |
| 135 | }; |
| 136 | |
| 137 | // This method is using libz macros with old-style-casts |
| 138 | #pragma GCC diagnostic push |
| 139 | #pragma GCC diagnostic ignored "-Wold-style-cast" |
| 140 | static inline int zlib_inflateInit2(z_stream* stream, int window_bits) { |
| 141 | return inflateInit2(stream, window_bits); |
| 142 | } |
| 143 | #pragma GCC diagnostic pop |
| 144 | |
| 145 | bool ZipArchiveStreamEntryCompressed::Init(const ZipEntry& entry) { |
| 146 | if (!ZipArchiveStreamEntry::Init(entry)) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | // Initialize the zlib stream struct. |
| 151 | memset(&z_stream_, 0, sizeof(z_stream_)); |
| 152 | z_stream_.zalloc = Z_NULL; |
| 153 | z_stream_.zfree = Z_NULL; |
| 154 | z_stream_.opaque = Z_NULL; |
| 155 | z_stream_.next_in = nullptr; |
| 156 | z_stream_.avail_in = 0; |
| 157 | z_stream_.avail_out = 0; |
| 158 | z_stream_.data_type = Z_UNKNOWN; |
| 159 | |
| 160 | // Use the undocumented "negative window bits" feature to tell zlib |
| 161 | // that there's no zlib header waiting for it. |
| 162 | int zerr = zlib_inflateInit2(&z_stream_, -MAX_WBITS); |
| 163 | if (zerr != Z_OK) { |
| 164 | if (zerr == Z_VERSION_ERROR) { |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame^] | 165 | ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION); |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 166 | } else { |
| 167 | ALOGE("Call to inflateInit2 failed (zerr=%d)", zerr); |
| 168 | } |
| 169 | |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | z_stream_init_ = true; |
| 174 | |
| 175 | uncompressed_length_ = entry.uncompressed_length; |
| 176 | compressed_length_ = entry.compressed_length; |
| 177 | |
| 178 | out_.resize(kBufSize); |
| 179 | in_.resize(kBufSize); |
| 180 | |
| 181 | computed_crc32_ = 0; |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | ZipArchiveStreamEntryCompressed::~ZipArchiveStreamEntryCompressed() { |
| 187 | if (z_stream_init_) { |
| 188 | inflateEnd(&z_stream_); |
| 189 | z_stream_init_ = false; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | bool ZipArchiveStreamEntryCompressed::Verify() { |
| 194 | return z_stream_init_ && uncompressed_length_ == 0 && compressed_length_ == 0 && |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame^] | 195 | crc32_ == computed_crc32_; |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | const std::vector<uint8_t>* ZipArchiveStreamEntryCompressed::Read() { |
| 199 | if (z_stream_.avail_out == 0) { |
| 200 | z_stream_.next_out = out_.data(); |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame^] | 201 | z_stream_.avail_out = out_.size(); |
| 202 | ; |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | while (true) { |
| 206 | if (z_stream_.avail_in == 0) { |
| 207 | if (compressed_length_ == 0) { |
| 208 | return nullptr; |
| 209 | } |
| 210 | size_t bytes = (compressed_length_ > in_.size()) ? in_.size() : compressed_length_; |
| 211 | ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle_); |
| 212 | errno = 0; |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 213 | if (!archive->mapped_zip.ReadData(in_.data(), bytes)) { |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 214 | if (errno != 0) { |
| 215 | ALOGE("Error reading from archive fd: %s", strerror(errno)); |
| 216 | } else { |
| 217 | ALOGE("Short read of zip file, possibly corrupted zip?"); |
| 218 | } |
| 219 | return nullptr; |
| 220 | } |
| 221 | |
| 222 | compressed_length_ -= bytes; |
| 223 | z_stream_.next_in = in_.data(); |
| 224 | z_stream_.avail_in = bytes; |
| 225 | } |
| 226 | |
| 227 | int zerr = inflate(&z_stream_, Z_NO_FLUSH); |
| 228 | if (zerr != Z_OK && zerr != Z_STREAM_END) { |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame^] | 229 | ALOGE("inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, z_stream_.next_in, |
| 230 | z_stream_.avail_in, z_stream_.next_out, z_stream_.avail_out); |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 231 | return nullptr; |
| 232 | } |
| 233 | |
| 234 | if (z_stream_.avail_out == 0) { |
| 235 | uncompressed_length_ -= out_.size(); |
| 236 | computed_crc32_ = crc32(computed_crc32_, out_.data(), out_.size()); |
| 237 | return &out_; |
| 238 | } |
| 239 | if (zerr == Z_STREAM_END) { |
| 240 | if (z_stream_.avail_out != 0) { |
| 241 | // Resize the vector down to the actual size of the data. |
| 242 | out_.resize(out_.size() - z_stream_.avail_out); |
| 243 | computed_crc32_ = crc32(computed_crc32_, out_.data(), out_.size()); |
| 244 | uncompressed_length_ -= out_.size(); |
| 245 | return &out_; |
| 246 | } |
| 247 | return nullptr; |
| 248 | } |
| 249 | } |
| 250 | return nullptr; |
| 251 | } |
| 252 | |
| 253 | class ZipArchiveStreamEntryRawCompressed : public ZipArchiveStreamEntryUncompressed { |
| 254 | public: |
Chih-Hung Hsieh | 3a114e0 | 2016-04-25 14:38:37 -0700 | [diff] [blame] | 255 | explicit ZipArchiveStreamEntryRawCompressed(ZipArchiveHandle handle) |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 256 | : ZipArchiveStreamEntryUncompressed(handle) {} |
| 257 | virtual ~ZipArchiveStreamEntryRawCompressed() {} |
| 258 | |
| 259 | bool Verify() override; |
| 260 | |
| 261 | protected: |
| 262 | bool Init(const ZipEntry& entry) override; |
| 263 | }; |
| 264 | |
| 265 | bool ZipArchiveStreamEntryRawCompressed::Init(const ZipEntry& entry) { |
| 266 | if (!ZipArchiveStreamEntryUncompressed::Init(entry)) { |
| 267 | return false; |
| 268 | } |
| 269 | length_ = entry.compressed_length; |
| 270 | |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | bool ZipArchiveStreamEntryRawCompressed::Verify() { |
| 275 | return length_ == 0; |
| 276 | } |
| 277 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame^] | 278 | ZipArchiveStreamEntry* ZipArchiveStreamEntry::Create(ZipArchiveHandle handle, |
| 279 | const ZipEntry& entry) { |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 280 | ZipArchiveStreamEntry* stream = nullptr; |
| 281 | if (entry.method != kCompressStored) { |
| 282 | stream = new ZipArchiveStreamEntryCompressed(handle); |
| 283 | } else { |
| 284 | stream = new ZipArchiveStreamEntryUncompressed(handle); |
| 285 | } |
| 286 | if (stream && !stream->Init(entry)) { |
| 287 | delete stream; |
| 288 | stream = nullptr; |
| 289 | } |
| 290 | |
| 291 | return stream; |
| 292 | } |
| 293 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame^] | 294 | ZipArchiveStreamEntry* ZipArchiveStreamEntry::CreateRaw(ZipArchiveHandle handle, |
| 295 | const ZipEntry& entry) { |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 296 | ZipArchiveStreamEntry* stream = nullptr; |
| 297 | if (entry.method == kCompressStored) { |
| 298 | // Not compressed, don't need to do anything special. |
| 299 | stream = new ZipArchiveStreamEntryUncompressed(handle); |
| 300 | } else { |
| 301 | stream = new ZipArchiveStreamEntryRawCompressed(handle); |
| 302 | } |
| 303 | if (stream && !stream->Init(entry)) { |
| 304 | delete stream; |
| 305 | stream = nullptr; |
| 306 | } |
| 307 | return stream; |
| 308 | } |