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