Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [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 | |
| 17 | #include "entry_name_utils-inl.h" |
| 18 | #include "zip_archive_common.h" |
| 19 | #include "ziparchive/zip_writer.h" |
| 20 | |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 23 | #include <cassert> |
| 24 | #include <cstdio> |
| 25 | #include <memory> |
| 26 | #include <zlib.h> |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 27 | #define DEF_MEM_LEVEL 8 // normally in zutil.h? |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 28 | |
| 29 | /* Zip compression methods we support */ |
| 30 | enum { |
| 31 | kCompressStored = 0, // no compression |
| 32 | kCompressDeflated = 8, // standard deflate |
| 33 | }; |
| 34 | |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 35 | // Size of the output buffer used for compression. |
| 36 | static const size_t kBufSize = 32768u; |
| 37 | |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 38 | // No error, operation completed successfully. |
| 39 | static const int32_t kNoError = 0; |
| 40 | |
| 41 | // The ZipWriter is in a bad state. |
| 42 | static const int32_t kInvalidState = -1; |
| 43 | |
| 44 | // There was an IO error while writing to disk. |
| 45 | static const int32_t kIoError = -2; |
| 46 | |
| 47 | // The zip entry name was invalid. |
| 48 | static const int32_t kInvalidEntryName = -3; |
| 49 | |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 50 | // An error occurred in zlib. |
| 51 | static const int32_t kZlibError = -4; |
| 52 | |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 53 | static const char* sErrorCodes[] = { |
| 54 | "Invalid state", |
| 55 | "IO error", |
| 56 | "Invalid entry name", |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 57 | "Zlib error", |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | const char* ZipWriter::ErrorCodeString(int32_t error_code) { |
| 61 | if (error_code < 0 && (-error_code) < static_cast<int32_t>(arraysize(sErrorCodes))) { |
| 62 | return sErrorCodes[-error_code]; |
| 63 | } |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 67 | static void DeleteZStream(z_stream* stream) { |
| 68 | deflateEnd(stream); |
| 69 | delete stream; |
| 70 | } |
| 71 | |
| 72 | ZipWriter::ZipWriter(FILE* f) : file_(f), current_offset_(0), state_(State::kWritingZip), |
| 73 | z_stream_(nullptr, DeleteZStream), buffer_(kBufSize) { |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | ZipWriter::ZipWriter(ZipWriter&& writer) : file_(writer.file_), |
| 77 | current_offset_(writer.current_offset_), |
| 78 | state_(writer.state_), |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 79 | files_(std::move(writer.files_)), |
| 80 | z_stream_(std::move(writer.z_stream_)), |
| 81 | buffer_(std::move(writer.buffer_)){ |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 82 | writer.file_ = nullptr; |
| 83 | writer.state_ = State::kError; |
| 84 | } |
| 85 | |
| 86 | ZipWriter& ZipWriter::operator=(ZipWriter&& writer) { |
| 87 | file_ = writer.file_; |
| 88 | current_offset_ = writer.current_offset_; |
| 89 | state_ = writer.state_; |
| 90 | files_ = std::move(writer.files_); |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 91 | z_stream_ = std::move(writer.z_stream_); |
| 92 | buffer_ = std::move(writer.buffer_); |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 93 | writer.file_ = nullptr; |
| 94 | writer.state_ = State::kError; |
| 95 | return *this; |
| 96 | } |
| 97 | |
| 98 | int32_t ZipWriter::HandleError(int32_t error_code) { |
| 99 | state_ = State::kError; |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 100 | z_stream_.reset(); |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 101 | return error_code; |
| 102 | } |
| 103 | |
| 104 | int32_t ZipWriter::StartEntry(const char* path, size_t flags) { |
| 105 | return StartEntryWithTime(path, flags, time_t()); |
| 106 | } |
| 107 | |
| 108 | static void ExtractTimeAndDate(time_t when, uint16_t* out_time, uint16_t* out_date) { |
| 109 | /* round up to an even number of seconds */ |
| 110 | when = static_cast<time_t>((static_cast<unsigned long>(when) + 1) & (~1)); |
| 111 | |
| 112 | struct tm* ptm; |
| 113 | #if !defined(_WIN32) |
| 114 | struct tm tm_result; |
| 115 | ptm = localtime_r(&when, &tm_result); |
| 116 | #else |
| 117 | ptm = localtime(&when); |
| 118 | #endif |
| 119 | |
| 120 | int year = ptm->tm_year; |
| 121 | if (year < 80) { |
| 122 | year = 80; |
| 123 | } |
| 124 | |
| 125 | *out_date = (year - 80) << 9 | (ptm->tm_mon + 1) << 5 | ptm->tm_mday; |
| 126 | *out_time = ptm->tm_hour << 11 | ptm->tm_min << 5 | ptm->tm_sec >> 1; |
| 127 | } |
| 128 | |
| 129 | int32_t ZipWriter::StartEntryWithTime(const char* path, size_t flags, time_t time) { |
| 130 | if (state_ != State::kWritingZip) { |
| 131 | return kInvalidState; |
| 132 | } |
| 133 | |
| 134 | FileInfo fileInfo = {}; |
| 135 | fileInfo.path = std::string(path); |
| 136 | fileInfo.local_file_header_offset = current_offset_; |
| 137 | |
| 138 | if (!IsValidEntryName(reinterpret_cast<const uint8_t*>(fileInfo.path.data()), |
| 139 | fileInfo.path.size())) { |
| 140 | return kInvalidEntryName; |
| 141 | } |
| 142 | |
| 143 | LocalFileHeader header = {}; |
| 144 | header.lfh_signature = LocalFileHeader::kSignature; |
| 145 | |
| 146 | // Set this flag to denote that a DataDescriptor struct will appear after the data, |
| 147 | // containing the crc and size fields. |
| 148 | header.gpb_flags |= kGPBDDFlagMask; |
| 149 | |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 150 | if (flags & ZipWriter::kCompress) { |
| 151 | fileInfo.compression_method = kCompressDeflated; |
| 152 | |
| 153 | int32_t result = PrepareDeflate(); |
| 154 | if (result != kNoError) { |
| 155 | return result; |
| 156 | } |
| 157 | } else { |
| 158 | fileInfo.compression_method = kCompressStored; |
| 159 | } |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 160 | header.compression_method = fileInfo.compression_method; |
| 161 | |
| 162 | ExtractTimeAndDate(time, &fileInfo.last_mod_time, &fileInfo.last_mod_date); |
| 163 | header.last_mod_time = fileInfo.last_mod_time; |
| 164 | header.last_mod_date = fileInfo.last_mod_date; |
| 165 | |
| 166 | header.file_name_length = fileInfo.path.size(); |
| 167 | |
| 168 | off64_t offset = current_offset_ + sizeof(header) + fileInfo.path.size(); |
| 169 | if ((flags & ZipWriter::kAlign32) && (offset & 0x03)) { |
| 170 | // Pad the extra field so the data will be aligned. |
| 171 | uint16_t padding = 4 - (offset % 4); |
| 172 | header.extra_field_length = padding; |
| 173 | offset += padding; |
| 174 | } |
| 175 | |
| 176 | if (fwrite(&header, sizeof(header), 1, file_) != 1) { |
| 177 | return HandleError(kIoError); |
| 178 | } |
| 179 | |
| 180 | if (fwrite(path, sizeof(*path), fileInfo.path.size(), file_) != fileInfo.path.size()) { |
| 181 | return HandleError(kIoError); |
| 182 | } |
| 183 | |
| 184 | if (fwrite("\0\0\0", 1, header.extra_field_length, file_) != header.extra_field_length) { |
| 185 | return HandleError(kIoError); |
| 186 | } |
| 187 | |
| 188 | files_.emplace_back(std::move(fileInfo)); |
| 189 | |
| 190 | current_offset_ = offset; |
| 191 | state_ = State::kWritingEntry; |
| 192 | return kNoError; |
| 193 | } |
| 194 | |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 195 | int32_t ZipWriter::PrepareDeflate() { |
| 196 | assert(state_ == State::kWritingZip); |
| 197 | |
| 198 | // Initialize the z_stream for compression. |
| 199 | z_stream_ = std::unique_ptr<z_stream, void(*)(z_stream*)>(new z_stream(), DeleteZStream); |
| 200 | |
| 201 | int zerr = deflateInit2(z_stream_.get(), Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, |
| 202 | DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); |
| 203 | if (zerr != Z_OK) { |
| 204 | if (zerr == Z_VERSION_ERROR) { |
| 205 | ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION); |
| 206 | return HandleError(kZlibError); |
| 207 | } else { |
| 208 | ALOGE("deflateInit2 failed (zerr=%d)", zerr); |
| 209 | return HandleError(kZlibError); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | z_stream_->next_out = buffer_.data(); |
| 214 | z_stream_->avail_out = buffer_.size(); |
| 215 | return kNoError; |
| 216 | } |
| 217 | |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 218 | int32_t ZipWriter::WriteBytes(const void* data, size_t len) { |
| 219 | if (state_ != State::kWritingEntry) { |
| 220 | return HandleError(kInvalidState); |
| 221 | } |
| 222 | |
| 223 | FileInfo& currentFile = files_.back(); |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 224 | int32_t result = kNoError; |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 225 | if (currentFile.compression_method & kCompressDeflated) { |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 226 | result = CompressBytes(¤tFile, data, len); |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 227 | } else { |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 228 | result = StoreBytes(¤tFile, data, len); |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 231 | if (result != kNoError) { |
| 232 | return result; |
| 233 | } |
| 234 | |
| 235 | currentFile.crc32 = crc32(currentFile.crc32, reinterpret_cast<const Bytef*>(data), len); |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 236 | currentFile.uncompressed_size += len; |
| 237 | return kNoError; |
| 238 | } |
| 239 | |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 240 | int32_t ZipWriter::StoreBytes(FileInfo* file, const void* data, size_t len) { |
| 241 | assert(state_ == State::kWritingEntry); |
| 242 | |
| 243 | if (fwrite(data, 1, len, file_) != len) { |
| 244 | return HandleError(kIoError); |
| 245 | } |
| 246 | file->compressed_size += len; |
| 247 | current_offset_ += len; |
| 248 | return kNoError; |
| 249 | } |
| 250 | |
| 251 | int32_t ZipWriter::CompressBytes(FileInfo* file, const void* data, size_t len) { |
| 252 | assert(state_ == State::kWritingEntry); |
| 253 | assert(z_stream_); |
| 254 | assert(z_stream_->next_out != nullptr); |
| 255 | assert(z_stream_->avail_out != 0); |
| 256 | |
| 257 | // Prepare the input. |
| 258 | z_stream_->next_in = reinterpret_cast<const uint8_t*>(data); |
| 259 | z_stream_->avail_in = len; |
| 260 | |
| 261 | while (z_stream_->avail_in > 0) { |
| 262 | // We have more data to compress. |
| 263 | int zerr = deflate(z_stream_.get(), Z_NO_FLUSH); |
| 264 | if (zerr != Z_OK) { |
| 265 | return HandleError(kZlibError); |
| 266 | } |
| 267 | |
| 268 | if (z_stream_->avail_out == 0) { |
| 269 | // The output is full, let's write it to disk. |
| 270 | size_t dataToWrite = z_stream_->next_out - buffer_.data(); |
| 271 | if (fwrite(buffer_.data(), 1, dataToWrite, file_) != dataToWrite) { |
| 272 | return HandleError(kIoError); |
| 273 | } |
| 274 | file->compressed_size += dataToWrite; |
| 275 | current_offset_ += dataToWrite; |
| 276 | |
| 277 | // Reset the output buffer for the next input. |
| 278 | z_stream_->next_out = buffer_.data(); |
| 279 | z_stream_->avail_out = buffer_.size(); |
| 280 | } |
| 281 | } |
| 282 | return kNoError; |
| 283 | } |
| 284 | |
| 285 | int32_t ZipWriter::FlushCompressedBytes(FileInfo* file) { |
| 286 | assert(state_ == State::kWritingEntry); |
| 287 | assert(z_stream_); |
| 288 | assert(z_stream_->next_out != nullptr); |
| 289 | assert(z_stream_->avail_out != 0); |
| 290 | |
| 291 | int zerr = deflate(z_stream_.get(), Z_FINISH); |
| 292 | if (zerr != Z_STREAM_END) { |
| 293 | return HandleError(kZlibError); |
| 294 | } |
| 295 | |
| 296 | size_t dataToWrite = z_stream_->next_out - buffer_.data(); |
| 297 | if (dataToWrite != 0) { |
| 298 | if (fwrite(buffer_.data(), 1, dataToWrite, file_) != dataToWrite) { |
| 299 | return HandleError(kIoError); |
| 300 | } |
| 301 | file->compressed_size += dataToWrite; |
| 302 | current_offset_ += dataToWrite; |
| 303 | } |
| 304 | z_stream_.reset(); |
| 305 | return kNoError; |
| 306 | } |
| 307 | |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 308 | int32_t ZipWriter::FinishEntry() { |
| 309 | if (state_ != State::kWritingEntry) { |
| 310 | return kInvalidState; |
| 311 | } |
| 312 | |
Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 313 | FileInfo& currentFile = files_.back(); |
| 314 | if (currentFile.compression_method & kCompressDeflated) { |
| 315 | int32_t result = FlushCompressedBytes(¤tFile); |
| 316 | if (result != kNoError) { |
| 317 | return result; |
| 318 | } |
| 319 | } |
| 320 | |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 321 | const uint32_t sig = DataDescriptor::kOptSignature; |
| 322 | if (fwrite(&sig, sizeof(sig), 1, file_) != 1) { |
| 323 | state_ = State::kError; |
| 324 | return kIoError; |
| 325 | } |
| 326 | |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 327 | DataDescriptor dd = {}; |
| 328 | dd.crc32 = currentFile.crc32; |
| 329 | dd.compressed_size = currentFile.compressed_size; |
| 330 | dd.uncompressed_size = currentFile.uncompressed_size; |
| 331 | if (fwrite(&dd, sizeof(dd), 1, file_) != 1) { |
| 332 | return HandleError(kIoError); |
| 333 | } |
| 334 | |
| 335 | current_offset_ += sizeof(DataDescriptor::kOptSignature) + sizeof(dd); |
| 336 | state_ = State::kWritingZip; |
| 337 | return kNoError; |
| 338 | } |
| 339 | |
| 340 | int32_t ZipWriter::Finish() { |
| 341 | if (state_ != State::kWritingZip) { |
| 342 | return kInvalidState; |
| 343 | } |
| 344 | |
| 345 | off64_t startOfCdr = current_offset_; |
| 346 | for (FileInfo& file : files_) { |
| 347 | CentralDirectoryRecord cdr = {}; |
| 348 | cdr.record_signature = CentralDirectoryRecord::kSignature; |
| 349 | cdr.gpb_flags |= kGPBDDFlagMask; |
| 350 | cdr.compression_method = file.compression_method; |
| 351 | cdr.last_mod_time = file.last_mod_time; |
| 352 | cdr.last_mod_date = file.last_mod_date; |
| 353 | cdr.crc32 = file.crc32; |
| 354 | cdr.compressed_size = file.compressed_size; |
| 355 | cdr.uncompressed_size = file.uncompressed_size; |
| 356 | cdr.file_name_length = file.path.size(); |
| 357 | cdr.local_file_header_offset = file.local_file_header_offset; |
| 358 | if (fwrite(&cdr, sizeof(cdr), 1, file_) != 1) { |
| 359 | return HandleError(kIoError); |
| 360 | } |
| 361 | |
| 362 | if (fwrite(file.path.data(), 1, file.path.size(), file_) != file.path.size()) { |
| 363 | return HandleError(kIoError); |
| 364 | } |
| 365 | |
| 366 | current_offset_ += sizeof(cdr) + file.path.size(); |
| 367 | } |
| 368 | |
| 369 | EocdRecord er = {}; |
| 370 | er.eocd_signature = EocdRecord::kSignature; |
Adam Lesinski | 044c790 | 2015-10-20 12:41:49 -0700 | [diff] [blame^] | 371 | er.disk_num = 0; |
| 372 | er.cd_start_disk = 0; |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 373 | er.num_records_on_disk = files_.size(); |
| 374 | er.num_records = files_.size(); |
| 375 | er.cd_size = current_offset_ - startOfCdr; |
| 376 | er.cd_start_offset = startOfCdr; |
| 377 | |
| 378 | if (fwrite(&er, sizeof(er), 1, file_) != 1) { |
| 379 | return HandleError(kIoError); |
| 380 | } |
| 381 | |
| 382 | if (fflush(file_) != 0) { |
| 383 | return HandleError(kIoError); |
| 384 | } |
| 385 | |
| 386 | current_offset_ += sizeof(er); |
| 387 | state_ = State::kDone; |
| 388 | return kNoError; |
| 389 | } |