Adam Lesinski | 622f304 | 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 | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 17 | #include "ziparchive/zip_writer.h" |
| 18 | |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 19 | #include <cstdio> |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 20 | #include <sys/param.h> |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 22 | #include <zlib.h> |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 23 | #define DEF_MEM_LEVEL 8 // normally in zutil.h? |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 24 | |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "android-base/logging.h" |
Adam Lesinski | 48bd485 | 2017-03-23 11:57:05 -0700 | [diff] [blame] | 29 | #include "utils/Compat.h" |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 30 | #include "utils/Log.h" |
| 31 | |
| 32 | #include "entry_name_utils-inl.h" |
| 33 | #include "zip_archive_common.h" |
| 34 | |
Christopher Ferris | 72082ab | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 35 | #if !defined(powerof2) |
| 36 | #define powerof2(x) ((((x)-1)&(x))==0) |
| 37 | #endif |
| 38 | |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 39 | /* Zip compression methods we support */ |
| 40 | enum { |
| 41 | kCompressStored = 0, // no compression |
| 42 | kCompressDeflated = 8, // standard deflate |
| 43 | }; |
| 44 | |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 45 | // Size of the output buffer used for compression. |
| 46 | static const size_t kBufSize = 32768u; |
| 47 | |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 60 | // An error occurred in zlib. |
| 61 | static const int32_t kZlibError = -4; |
| 62 | |
Christopher Ferris | 72082ab | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 69 | static const char* sErrorCodes[] = { |
| 70 | "Invalid state", |
| 71 | "IO error", |
| 72 | "Invalid entry name", |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 73 | "Zlib error", |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 83 | static void DeleteZStream(z_stream* stream) { |
| 84 | deflateEnd(stream); |
| 85 | delete stream; |
| 86 | } |
| 87 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 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 | } |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | ZipWriter::ZipWriter(ZipWriter&& writer) : file_(writer.file_), |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 100 | seekable_(writer.seekable_), |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 101 | current_offset_(writer.current_offset_), |
| 102 | state_(writer.state_), |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 103 | files_(std::move(writer.files_)), |
| 104 | z_stream_(std::move(writer.z_stream_)), |
| 105 | buffer_(std::move(writer.buffer_)){ |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 106 | writer.file_ = nullptr; |
| 107 | writer.state_ = State::kError; |
| 108 | } |
| 109 | |
| 110 | ZipWriter& ZipWriter::operator=(ZipWriter&& writer) { |
| 111 | file_ = writer.file_; |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 112 | seekable_ = writer.seekable_; |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 113 | current_offset_ = writer.current_offset_; |
| 114 | state_ = writer.state_; |
| 115 | files_ = std::move(writer.files_); |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 116 | z_stream_ = std::move(writer.z_stream_); |
| 117 | buffer_ = std::move(writer.buffer_); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 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; |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 125 | z_stream_.reset(); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 126 | return error_code; |
| 127 | } |
| 128 | |
| 129 | int32_t ZipWriter::StartEntry(const char* path, size_t flags) { |
Christopher Ferris | 72082ab | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 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); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 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 | |
Christopher Ferris | 72082ab | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 196 | int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, |
| 197 | time_t time, uint32_t alignment) { |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 198 | if (state_ != State::kWritingZip) { |
| 199 | return kInvalidState; |
| 200 | } |
| 201 | |
Christopher Ferris | 72082ab | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 202 | if (flags & kAlign32) { |
| 203 | return kInvalidAlign32Flag; |
| 204 | } |
| 205 | |
| 206 | if (powerof2(alignment) == 0) { |
| 207 | return kInvalidAlignment; |
| 208 | } |
| 209 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 210 | FileEntry file_entry = {}; |
| 211 | file_entry.local_file_header_offset = current_offset_; |
| 212 | file_entry.path = path; |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 213 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 214 | if (!IsValidEntryName(reinterpret_cast<const uint8_t*>(file_entry.path.data()), |
| 215 | file_entry.path.size())) { |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 216 | return kInvalidEntryName; |
| 217 | } |
| 218 | |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 219 | if (flags & ZipWriter::kCompress) { |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 220 | file_entry.compression_method = kCompressDeflated; |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 221 | |
| 222 | int32_t result = PrepareDeflate(); |
| 223 | if (result != kNoError) { |
| 224 | return result; |
| 225 | } |
| 226 | } else { |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 227 | file_entry.compression_method = kCompressStored; |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 228 | } |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 229 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 230 | ExtractTimeAndDate(time, &file_entry.last_mod_time, &file_entry.last_mod_date); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 231 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 232 | off_t offset = current_offset_ + sizeof(LocalFileHeader) + file_entry.path.size(); |
Christopher Ferris | 72082ab | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 233 | std::vector<char> zero_padding; |
| 234 | if (alignment != 0 && (offset & (alignment - 1))) { |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 235 | // Pad the extra field so the data will be aligned. |
Christopher Ferris | 72082ab | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 236 | uint16_t padding = alignment - (offset % alignment); |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 237 | file_entry.padding_length = padding; |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 238 | offset += padding; |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 239 | zero_padding.resize(padding, 0); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 247 | if (fwrite(&header, sizeof(header), 1, file_) != 1) { |
| 248 | return HandleError(kIoError); |
| 249 | } |
| 250 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 251 | if (fwrite(path, sizeof(*path), file_entry.path.size(), file_) != file_entry.path.size()) { |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 252 | return HandleError(kIoError); |
| 253 | } |
| 254 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 255 | if (file_entry.padding_length != 0 && |
| 256 | fwrite(zero_padding.data(), 1, file_entry.padding_length, file_) |
| 257 | != file_entry.padding_length) { |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 258 | return HandleError(kIoError); |
| 259 | } |
| 260 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 261 | current_file_entry_ = std::move(file_entry); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 262 | current_offset_ = offset; |
| 263 | state_ = State::kWritingEntry; |
| 264 | return kNoError; |
| 265 | } |
| 266 | |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 291 | int32_t ZipWriter::PrepareDeflate() { |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 292 | CHECK(state_ == State::kWritingZip); |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 293 | |
| 294 | // Initialize the z_stream for compression. |
| 295 | z_stream_ = std::unique_ptr<z_stream, void(*)(z_stream*)>(new z_stream(), DeleteZStream); |
| 296 | |
Colin Cross | df22776 | 2016-09-16 10:15:51 -0700 | [diff] [blame] | 297 | #pragma GCC diagnostic push |
| 298 | #pragma GCC diagnostic ignored "-Wold-style-cast" |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 299 | int zerr = deflateInit2(z_stream_.get(), Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, |
| 300 | DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); |
Colin Cross | df22776 | 2016-09-16 10:15:51 -0700 | [diff] [blame] | 301 | #pragma GCC diagnostic pop |
| 302 | |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 318 | int32_t ZipWriter::WriteBytes(const void* data, size_t len) { |
| 319 | if (state_ != State::kWritingEntry) { |
| 320 | return HandleError(kInvalidState); |
| 321 | } |
| 322 | |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 323 | int32_t result = kNoError; |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 324 | if (current_file_entry_.compression_method & kCompressDeflated) { |
| 325 | result = CompressBytes(¤t_file_entry_, data, len); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 326 | } else { |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 327 | result = StoreBytes(¤t_file_entry_, data, len); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 330 | if (result != kNoError) { |
| 331 | return result; |
| 332 | } |
| 333 | |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 334 | current_file_entry_.crc32 = crc32(current_file_entry_.crc32, |
| 335 | reinterpret_cast<const Bytef*>(data), len); |
| 336 | current_file_entry_.uncompressed_size += len; |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 337 | return kNoError; |
| 338 | } |
| 339 | |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 340 | int32_t ZipWriter::StoreBytes(FileEntry* file, const void* data, size_t len) { |
| 341 | CHECK(state_ == State::kWritingEntry); |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 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); |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 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. |
Christopher Ferris | fcc081f | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 370 | size_t write_bytes = z_stream_->next_out - buffer_.data(); |
| 371 | if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) { |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 372 | return HandleError(kIoError); |
| 373 | } |
Christopher Ferris | fcc081f | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 374 | file->compressed_size += write_bytes; |
| 375 | current_offset_ += write_bytes; |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 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); |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 390 | |
Christopher Ferris | fcc081f | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 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) { |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 395 | CHECK(z_stream_->avail_out == 0); |
Christopher Ferris | fcc081f | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 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 | } |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 406 | if (zerr != Z_STREAM_END) { |
| 407 | return HandleError(kZlibError); |
| 408 | } |
| 409 | |
Christopher Ferris | fcc081f | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 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) { |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 413 | return HandleError(kIoError); |
| 414 | } |
Christopher Ferris | fcc081f | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 415 | file->compressed_size += write_bytes; |
| 416 | current_offset_ += write_bytes; |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 417 | } |
| 418 | z_stream_.reset(); |
| 419 | return kNoError; |
| 420 | } |
| 421 | |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 422 | int32_t ZipWriter::FinishEntry() { |
| 423 | if (state_ != State::kWritingEntry) { |
| 424 | return kInvalidState; |
| 425 | } |
| 426 | |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 427 | if (current_file_entry_.compression_method & kCompressDeflated) { |
| 428 | int32_t result = FlushCompressedBytes(¤t_file_entry_); |
Adam Lesinski | c322b43 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 429 | if (result != kNoError) { |
| 430 | return result; |
| 431 | } |
| 432 | } |
| 433 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 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 | } |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 441 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 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 | } |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 468 | files_.emplace_back(std::move(current_file_entry_)); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 469 | state_ = State::kWritingZip; |
| 470 | return kNoError; |
| 471 | } |
| 472 | |
| 473 | int32_t ZipWriter::Finish() { |
| 474 | if (state_ != State::kWritingZip) { |
| 475 | return kInvalidState; |
| 476 | } |
| 477 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 478 | off_t startOfCdr = current_offset_; |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 479 | for (FileEntry& file : files_) { |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 480 | CentralDirectoryRecord cdr = {}; |
| 481 | cdr.record_signature = CentralDirectoryRecord::kSignature; |
Adam Lesinski | 253cc06 | 2017-04-06 18:55:47 -0700 | [diff] [blame^] | 482 | if ((file.compression_method & kCompressDeflated) || !seekable_) { |
| 483 | cdr.gpb_flags |= kGPBDDFlagMask; |
| 484 | } |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 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(); |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 492 | cdr.local_file_header_offset = static_cast<uint32_t>(file.local_file_header_offset); |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 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; |
Adam Lesinski | a8d71d6 | 2015-10-20 12:41:49 -0700 | [diff] [blame] | 506 | er.disk_num = 0; |
| 507 | er.cd_start_disk = 0; |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | f61aaf4 | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 522 | if (ftruncate(fileno(file_), current_offset_) != 0) { |
Adam Lesinski | 2e216de | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 523 | return HandleError(kIoError); |
| 524 | } |
| 525 | |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 526 | if (fflush(file_) != 0) { |
| 527 | return HandleError(kIoError); |
| 528 | } |
| 529 | |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 530 | state_ = State::kDone; |
| 531 | return kNoError; |
| 532 | } |