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 "ziparchive/zip_writer.h" 18 19 #include <cstdio> 20 #include <sys/param.h> 21 #include <sys/stat.h> 22 #include <zlib.h> 23 #define DEF_MEM_LEVEL 8 // normally in zutil.h? 24 25 #include <memory> 26 #include <vector> 27 28 #include "android-base/logging.h" 29 #include "utils/Compat.h" 30 #include "utils/Log.h" 31 32 #include "entry_name_utils-inl.h" 33 #include "zip_archive_common.h" 34 35 #if !defined(powerof2) 36 #define powerof2(x) ((((x)-1)&(x))==0) 37 #endif 38 39 /* Zip compression methods we support */ 40 enum { 41 kCompressStored = 0, // no compression 42 kCompressDeflated = 8, // standard deflate 43 }; 44 45 // Size of the output buffer used for compression. 46 static const size_t kBufSize = 32768u; 47 48 // No error, operation completed successfully. 49 static const int32_t kNoError = 0; 50 51 // The ZipWriter is in a bad state. 52 static const int32_t kInvalidState = -1; 53 54 // There was an IO error while writing to disk. 55 static const int32_t kIoError = -2; 56 57 // The zip entry name was invalid. 58 static const int32_t kInvalidEntryName = -3; 59 60 // An error occurred in zlib. 61 static const int32_t kZlibError = -4; 62 63 // The start aligned function was called with the aligned flag. 64 static const int32_t kInvalidAlign32Flag = -5; 65 66 // The alignment parameter is not a power of 2. 67 static const int32_t kInvalidAlignment = -6; 68 69 static const char* sErrorCodes[] = { 70 "Invalid state", 71 "IO error", 72 "Invalid entry name", 73 "Zlib error", 74 }; 75 76 const char* ZipWriter::ErrorCodeString(int32_t error_code) { 77 if (error_code < 0 && (-error_code) < static_cast<int32_t>(arraysize(sErrorCodes))) { 78 return sErrorCodes[-error_code]; 79 } 80 return nullptr; 81 } 82 83 static void DeleteZStream(z_stream* stream) { 84 deflateEnd(stream); 85 delete stream; 86 } 87 88 ZipWriter::ZipWriter(FILE* f) : file_(f), seekable_(false), current_offset_(0), 89 state_(State::kWritingZip), z_stream_(nullptr, DeleteZStream), 90 buffer_(kBufSize) { 91 // Check if the file is seekable (regular file). If fstat fails, that's fine, subsequent calls 92 // will fail as well. 93 struct stat file_stats; 94 if (fstat(fileno(f), &file_stats) == 0) { 95 seekable_ = S_ISREG(file_stats.st_mode); 96 } 97 } 98 99 ZipWriter::ZipWriter(ZipWriter&& writer) : file_(writer.file_), 100 seekable_(writer.seekable_), 101 current_offset_(writer.current_offset_), 102 state_(writer.state_), 103 files_(std::move(writer.files_)), 104 z_stream_(std::move(writer.z_stream_)), 105 buffer_(std::move(writer.buffer_)){ 106 writer.file_ = nullptr; 107 writer.state_ = State::kError; 108 } 109 110 ZipWriter& ZipWriter::operator=(ZipWriter&& writer) { 111 file_ = writer.file_; 112 seekable_ = writer.seekable_; 113 current_offset_ = writer.current_offset_; 114 state_ = writer.state_; 115 files_ = std::move(writer.files_); 116 z_stream_ = std::move(writer.z_stream_); 117 buffer_ = std::move(writer.buffer_); 118 writer.file_ = nullptr; 119 writer.state_ = State::kError; 120 return *this; 121 } 122 123 int32_t ZipWriter::HandleError(int32_t error_code) { 124 state_ = State::kError; 125 z_stream_.reset(); 126 return error_code; 127 } 128 129 int32_t ZipWriter::StartEntry(const char* path, size_t flags) { 130 uint32_t alignment = 0; 131 if (flags & kAlign32) { 132 flags &= ~kAlign32; 133 alignment = 4; 134 } 135 return StartAlignedEntryWithTime(path, flags, time_t(), alignment); 136 } 137 138 int32_t ZipWriter::StartAlignedEntry(const char* path, size_t flags, uint32_t alignment) { 139 return StartAlignedEntryWithTime(path, flags, time_t(), alignment); 140 } 141 142 int32_t ZipWriter::StartEntryWithTime(const char* path, size_t flags, time_t time) { 143 uint32_t alignment = 0; 144 if (flags & kAlign32) { 145 flags &= ~kAlign32; 146 alignment = 4; 147 } 148 return StartAlignedEntryWithTime(path, flags, time, alignment); 149 } 150 151 static void ExtractTimeAndDate(time_t when, uint16_t* out_time, uint16_t* out_date) { 152 /* round up to an even number of seconds */ 153 when = static_cast<time_t>((static_cast<unsigned long>(when) + 1) & (~1)); 154 155 struct tm* ptm; 156 #if !defined(_WIN32) 157 struct tm tm_result; 158 ptm = localtime_r(&when, &tm_result); 159 #else 160 ptm = localtime(&when); 161 #endif 162 163 int year = ptm->tm_year; 164 if (year < 80) { 165 year = 80; 166 } 167 168 *out_date = (year - 80) << 9 | (ptm->tm_mon + 1) << 5 | ptm->tm_mday; 169 *out_time = ptm->tm_hour << 11 | ptm->tm_min << 5 | ptm->tm_sec >> 1; 170 } 171 172 static void CopyFromFileEntry(const ZipWriter::FileEntry& src, bool use_data_descriptor, 173 LocalFileHeader* dst) { 174 dst->lfh_signature = LocalFileHeader::kSignature; 175 if (use_data_descriptor) { 176 // Set this flag to denote that a DataDescriptor struct will appear after the data, 177 // containing the crc and size fields. 178 dst->gpb_flags |= kGPBDDFlagMask; 179 180 // The size and crc fields must be 0. 181 dst->compressed_size = 0u; 182 dst->uncompressed_size = 0u; 183 dst->crc32 = 0u; 184 } else { 185 dst->compressed_size = src.compressed_size; 186 dst->uncompressed_size = src.uncompressed_size; 187 dst->crc32 = src.crc32; 188 } 189 dst->compression_method = src.compression_method; 190 dst->last_mod_time = src.last_mod_time; 191 dst->last_mod_date = src.last_mod_date; 192 dst->file_name_length = src.path.size(); 193 dst->extra_field_length = src.padding_length; 194 } 195 196 int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, 197 time_t time, uint32_t alignment) { 198 if (state_ != State::kWritingZip) { 199 return kInvalidState; 200 } 201 202 if (flags & kAlign32) { 203 return kInvalidAlign32Flag; 204 } 205 206 if (powerof2(alignment) == 0) { 207 return kInvalidAlignment; 208 } 209 210 FileEntry file_entry = {}; 211 file_entry.local_file_header_offset = current_offset_; 212 file_entry.path = path; 213 214 if (!IsValidEntryName(reinterpret_cast<const uint8_t*>(file_entry.path.data()), 215 file_entry.path.size())) { 216 return kInvalidEntryName; 217 } 218 219 if (flags & ZipWriter::kCompress) { 220 file_entry.compression_method = kCompressDeflated; 221 222 int32_t result = PrepareDeflate(); 223 if (result != kNoError) { 224 return result; 225 } 226 } else { 227 file_entry.compression_method = kCompressStored; 228 } 229 230 ExtractTimeAndDate(time, &file_entry.last_mod_time, &file_entry.last_mod_date); 231 232 off_t offset = current_offset_ + sizeof(LocalFileHeader) + file_entry.path.size(); 233 std::vector<char> zero_padding; 234 if (alignment != 0 && (offset & (alignment - 1))) { 235 // Pad the extra field so the data will be aligned. 236 uint16_t padding = alignment - (offset % alignment); 237 file_entry.padding_length = padding; 238 offset += padding; 239 zero_padding.resize(padding, 0); 240 } 241 242 LocalFileHeader header = {}; 243 // Always start expecting a data descriptor. When the data has finished being written, 244 // if it is possible to seek back, the GPB flag will reset and the sizes written. 245 CopyFromFileEntry(file_entry, true /*use_data_descriptor*/, &header); 246 247 if (fwrite(&header, sizeof(header), 1, file_) != 1) { 248 return HandleError(kIoError); 249 } 250 251 if (fwrite(path, sizeof(*path), file_entry.path.size(), file_) != file_entry.path.size()) { 252 return HandleError(kIoError); 253 } 254 255 if (file_entry.padding_length != 0 && 256 fwrite(zero_padding.data(), 1, file_entry.padding_length, file_) 257 != file_entry.padding_length) { 258 return HandleError(kIoError); 259 } 260 261 current_file_entry_ = std::move(file_entry); 262 current_offset_ = offset; 263 state_ = State::kWritingEntry; 264 return kNoError; 265 } 266 267 int32_t ZipWriter::DiscardLastEntry() { 268 if (state_ != State::kWritingZip || files_.empty()) { 269 return kInvalidState; 270 } 271 272 FileEntry& last_entry = files_.back(); 273 current_offset_ = last_entry.local_file_header_offset; 274 if (fseeko(file_, current_offset_, SEEK_SET) != 0) { 275 return HandleError(kIoError); 276 } 277 files_.pop_back(); 278 return kNoError; 279 } 280 281 int32_t ZipWriter::GetLastEntry(FileEntry* out_entry) { 282 CHECK(out_entry != nullptr); 283 284 if (files_.empty()) { 285 return kInvalidState; 286 } 287 *out_entry = files_.back(); 288 return kNoError; 289 } 290 291 int32_t ZipWriter::PrepareDeflate() { 292 CHECK(state_ == State::kWritingZip); 293 294 // Initialize the z_stream for compression. 295 z_stream_ = std::unique_ptr<z_stream, void(*)(z_stream*)>(new z_stream(), DeleteZStream); 296 297 #pragma GCC diagnostic push 298 #pragma GCC diagnostic ignored "-Wold-style-cast" 299 int zerr = deflateInit2(z_stream_.get(), Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 300 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); 301 #pragma GCC diagnostic pop 302 303 if (zerr != Z_OK) { 304 if (zerr == Z_VERSION_ERROR) { 305 ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION); 306 return HandleError(kZlibError); 307 } else { 308 ALOGE("deflateInit2 failed (zerr=%d)", zerr); 309 return HandleError(kZlibError); 310 } 311 } 312 313 z_stream_->next_out = buffer_.data(); 314 z_stream_->avail_out = buffer_.size(); 315 return kNoError; 316 } 317 318 int32_t ZipWriter::WriteBytes(const void* data, size_t len) { 319 if (state_ != State::kWritingEntry) { 320 return HandleError(kInvalidState); 321 } 322 323 int32_t result = kNoError; 324 if (current_file_entry_.compression_method & kCompressDeflated) { 325 result = CompressBytes(¤t_file_entry_, data, len); 326 } else { 327 result = StoreBytes(¤t_file_entry_, data, len); 328 } 329 330 if (result != kNoError) { 331 return result; 332 } 333 334 current_file_entry_.crc32 = crc32(current_file_entry_.crc32, 335 reinterpret_cast<const Bytef*>(data), len); 336 current_file_entry_.uncompressed_size += len; 337 return kNoError; 338 } 339 340 int32_t ZipWriter::StoreBytes(FileEntry* file, const void* data, size_t len) { 341 CHECK(state_ == State::kWritingEntry); 342 343 if (fwrite(data, 1, len, file_) != len) { 344 return HandleError(kIoError); 345 } 346 file->compressed_size += len; 347 current_offset_ += len; 348 return kNoError; 349 } 350 351 int32_t ZipWriter::CompressBytes(FileEntry* file, const void* data, size_t len) { 352 CHECK(state_ == State::kWritingEntry); 353 CHECK(z_stream_); 354 CHECK(z_stream_->next_out != nullptr); 355 CHECK(z_stream_->avail_out != 0); 356 357 // Prepare the input. 358 z_stream_->next_in = reinterpret_cast<const uint8_t*>(data); 359 z_stream_->avail_in = len; 360 361 while (z_stream_->avail_in > 0) { 362 // We have more data to compress. 363 int zerr = deflate(z_stream_.get(), Z_NO_FLUSH); 364 if (zerr != Z_OK) { 365 return HandleError(kZlibError); 366 } 367 368 if (z_stream_->avail_out == 0) { 369 // The output is full, let's write it to disk. 370 size_t write_bytes = z_stream_->next_out - buffer_.data(); 371 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) { 372 return HandleError(kIoError); 373 } 374 file->compressed_size += write_bytes; 375 current_offset_ += write_bytes; 376 377 // Reset the output buffer for the next input. 378 z_stream_->next_out = buffer_.data(); 379 z_stream_->avail_out = buffer_.size(); 380 } 381 } 382 return kNoError; 383 } 384 385 int32_t ZipWriter::FlushCompressedBytes(FileEntry* file) { 386 CHECK(state_ == State::kWritingEntry); 387 CHECK(z_stream_); 388 CHECK(z_stream_->next_out != nullptr); 389 CHECK(z_stream_->avail_out != 0); 390 391 // Keep deflating while there isn't enough space in the buffer to 392 // to complete the compress. 393 int zerr; 394 while ((zerr = deflate(z_stream_.get(), Z_FINISH)) == Z_OK) { 395 CHECK(z_stream_->avail_out == 0); 396 size_t write_bytes = z_stream_->next_out - buffer_.data(); 397 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) { 398 return HandleError(kIoError); 399 } 400 file->compressed_size += write_bytes; 401 current_offset_ += write_bytes; 402 403 z_stream_->next_out = buffer_.data(); 404 z_stream_->avail_out = buffer_.size(); 405 } 406 if (zerr != Z_STREAM_END) { 407 return HandleError(kZlibError); 408 } 409 410 size_t write_bytes = z_stream_->next_out - buffer_.data(); 411 if (write_bytes != 0) { 412 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) { 413 return HandleError(kIoError); 414 } 415 file->compressed_size += write_bytes; 416 current_offset_ += write_bytes; 417 } 418 z_stream_.reset(); 419 return kNoError; 420 } 421 422 int32_t ZipWriter::FinishEntry() { 423 if (state_ != State::kWritingEntry) { 424 return kInvalidState; 425 } 426 427 if (current_file_entry_.compression_method & kCompressDeflated) { 428 int32_t result = FlushCompressedBytes(¤t_file_entry_); 429 if (result != kNoError) { 430 return result; 431 } 432 } 433 434 if ((current_file_entry_.compression_method & kCompressDeflated) || !seekable_) { 435 // Some versions of ZIP don't allow STORED data to have a trailing DataDescriptor. 436 // If this file is not seekable, or if the data is compressed, write a DataDescriptor. 437 const uint32_t sig = DataDescriptor::kOptSignature; 438 if (fwrite(&sig, sizeof(sig), 1, file_) != 1) { 439 return HandleError(kIoError); 440 } 441 442 DataDescriptor dd = {}; 443 dd.crc32 = current_file_entry_.crc32; 444 dd.compressed_size = current_file_entry_.compressed_size; 445 dd.uncompressed_size = current_file_entry_.uncompressed_size; 446 if (fwrite(&dd, sizeof(dd), 1, file_) != 1) { 447 return HandleError(kIoError); 448 } 449 current_offset_ += sizeof(DataDescriptor::kOptSignature) + sizeof(dd); 450 } else { 451 // Seek back to the header and rewrite to include the size. 452 if (fseeko(file_, current_file_entry_.local_file_header_offset, SEEK_SET) != 0) { 453 return HandleError(kIoError); 454 } 455 456 LocalFileHeader header = {}; 457 CopyFromFileEntry(current_file_entry_, false /*use_data_descriptor*/, &header); 458 459 if (fwrite(&header, sizeof(header), 1, file_) != 1) { 460 return HandleError(kIoError); 461 } 462 463 if (fseeko(file_, current_offset_, SEEK_SET) != 0) { 464 return HandleError(kIoError); 465 } 466 } 467 468 files_.emplace_back(std::move(current_file_entry_)); 469 state_ = State::kWritingZip; 470 return kNoError; 471 } 472 473 int32_t ZipWriter::Finish() { 474 if (state_ != State::kWritingZip) { 475 return kInvalidState; 476 } 477 478 off_t startOfCdr = current_offset_; 479 for (FileEntry& file : files_) { 480 CentralDirectoryRecord cdr = {}; 481 cdr.record_signature = CentralDirectoryRecord::kSignature; 482 if ((file.compression_method & kCompressDeflated) || !seekable_) { 483 cdr.gpb_flags |= kGPBDDFlagMask; 484 } 485 cdr.compression_method = file.compression_method; 486 cdr.last_mod_time = file.last_mod_time; 487 cdr.last_mod_date = file.last_mod_date; 488 cdr.crc32 = file.crc32; 489 cdr.compressed_size = file.compressed_size; 490 cdr.uncompressed_size = file.uncompressed_size; 491 cdr.file_name_length = file.path.size(); 492 cdr.local_file_header_offset = static_cast<uint32_t>(file.local_file_header_offset); 493 if (fwrite(&cdr, sizeof(cdr), 1, file_) != 1) { 494 return HandleError(kIoError); 495 } 496 497 if (fwrite(file.path.data(), 1, file.path.size(), file_) != file.path.size()) { 498 return HandleError(kIoError); 499 } 500 501 current_offset_ += sizeof(cdr) + file.path.size(); 502 } 503 504 EocdRecord er = {}; 505 er.eocd_signature = EocdRecord::kSignature; 506 er.disk_num = 0; 507 er.cd_start_disk = 0; 508 er.num_records_on_disk = files_.size(); 509 er.num_records = files_.size(); 510 er.cd_size = current_offset_ - startOfCdr; 511 er.cd_start_offset = startOfCdr; 512 513 if (fwrite(&er, sizeof(er), 1, file_) != 1) { 514 return HandleError(kIoError); 515 } 516 517 current_offset_ += sizeof(er); 518 519 // Since we can BackUp() and potentially finish writing at an offset less than one we had 520 // already written at, we must truncate the file. 521 522 if (ftruncate(fileno(file_), current_offset_) != 0) { 523 return HandleError(kIoError); 524 } 525 526 if (fflush(file_) != 0) { 527 return HandleError(kIoError); 528 } 529 530 state_ = State::kDone; 531 return kNoError; 532 } 533