| 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 | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 19 | #include <sys/param.h> | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 21 | #include <zlib.h> | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 22 | #include <cstdio> | 
|  | 23 | #define DEF_MEM_LEVEL 8  // normally in zutil.h? | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 24 |  | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 25 | #include <memory> | 
|  | 26 | #include <vector> | 
|  | 27 |  | 
|  | 28 | #include "android-base/logging.h" | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 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) | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 34 | #define powerof2(x) ((((x)-1) & (x)) == 0) | 
| Christopher Ferris | 5e9f3d4 | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 35 | #endif | 
|  | 36 |  | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 37 | /* Zip compression methods we support */ | 
|  | 38 | enum { | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 39 | kCompressStored = 0,    // no compression | 
|  | 40 | kCompressDeflated = 8,  // standard deflate | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 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[] = { | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 68 | "Invalid state", "IO error", "Invalid entry name", "Zlib error", | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 69 | }; | 
|  | 70 |  | 
|  | 71 | const char* ZipWriter::ErrorCodeString(int32_t error_code) { | 
|  | 72 | if (error_code < 0 && (-error_code) < static_cast<int32_t>(arraysize(sErrorCodes))) { | 
|  | 73 | return sErrorCodes[-error_code]; | 
|  | 74 | } | 
|  | 75 | return nullptr; | 
|  | 76 | } | 
|  | 77 |  | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 78 | static void DeleteZStream(z_stream* stream) { | 
|  | 79 | deflateEnd(stream); | 
|  | 80 | delete stream; | 
|  | 81 | } | 
|  | 82 |  | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 83 | ZipWriter::ZipWriter(FILE* f) | 
|  | 84 | : file_(f), | 
|  | 85 | seekable_(false), | 
|  | 86 | current_offset_(0), | 
|  | 87 | state_(State::kWritingZip), | 
|  | 88 | z_stream_(nullptr, DeleteZStream), | 
|  | 89 | buffer_(kBufSize) { | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 90 | // Check if the file is seekable (regular file). If fstat fails, that's fine, subsequent calls | 
|  | 91 | // will fail as well. | 
|  | 92 | struct stat file_stats; | 
|  | 93 | if (fstat(fileno(f), &file_stats) == 0) { | 
|  | 94 | seekable_ = S_ISREG(file_stats.st_mode); | 
|  | 95 | } | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 98 | ZipWriter::ZipWriter(ZipWriter&& writer) noexcept | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 99 | : 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_)) { | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 106 | writer.file_ = nullptr; | 
|  | 107 | writer.state_ = State::kError; | 
|  | 108 | } | 
|  | 109 |  | 
| Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 110 | ZipWriter& ZipWriter::operator=(ZipWriter&& writer) noexcept { | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 111 | file_ = writer.file_; | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 112 | seekable_ = writer.seekable_; | 
| Adam Lesinski | ad4ad8c | 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 | 591fd39 | 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 | ad4ad8c | 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 | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 125 | z_stream_.reset(); | 
| Adam Lesinski | ad4ad8c | 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 | 5e9f3d4 | 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 | ad4ad8c | 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) | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 157 | struct tm tm_result; | 
|  | 158 | ptm = localtime_r(&when, &tm_result); | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 159 | #else | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 160 | ptm = localtime(&when); | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 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 | e2fa70b | 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 |  | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 196 | int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, time_t time, | 
|  | 197 | uint32_t alignment) { | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 198 | if (state_ != State::kWritingZip) { | 
|  | 199 | return kInvalidState; | 
|  | 200 | } | 
|  | 201 |  | 
| Christopher Ferris | 5e9f3d4 | 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 | e2fa70b | 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 | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 213 |  | 
| Adam Lesinski | e2fa70b | 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 | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 216 | return kInvalidEntryName; | 
|  | 217 | } | 
|  | 218 |  | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 219 | if (flags & ZipWriter::kCompress) { | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 220 | file_entry.compression_method = kCompressDeflated; | 
| Adam Lesinski | 591fd39 | 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 | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 227 | file_entry.compression_method = kCompressStored; | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 228 | } | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 229 |  | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 230 | ExtractTimeAndDate(time, &file_entry.last_mod_time, &file_entry.last_mod_date); | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 231 |  | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 232 | off_t offset = current_offset_ + sizeof(LocalFileHeader) + file_entry.path.size(); | 
| Christopher Ferris | 5e9f3d4 | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 233 | std::vector<char> zero_padding; | 
|  | 234 | if (alignment != 0 && (offset & (alignment - 1))) { | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 235 | // Pad the extra field so the data will be aligned. | 
| Christopher Ferris | 5e9f3d4 | 2016-01-19 10:33:03 -0800 | [diff] [blame] | 236 | uint16_t padding = alignment - (offset % alignment); | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 237 | file_entry.padding_length = padding; | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 238 | offset += padding; | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 239 | zero_padding.resize(padding, 0); | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 240 | } | 
|  | 241 |  | 
| Adam Lesinski | e2fa70b | 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 | ad4ad8c | 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 | e2fa70b | 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 | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 252 | return HandleError(kIoError); | 
|  | 253 | } | 
|  | 254 |  | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 255 | if (file_entry.padding_length != 0 && fwrite(zero_padding.data(), 1, file_entry.padding_length, | 
|  | 256 | file_) != file_entry.padding_length) { | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 257 | return HandleError(kIoError); | 
|  | 258 | } | 
|  | 259 |  | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 260 | current_file_entry_ = std::move(file_entry); | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 261 | current_offset_ = offset; | 
|  | 262 | state_ = State::kWritingEntry; | 
|  | 263 | return kNoError; | 
|  | 264 | } | 
|  | 265 |  | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 266 | int32_t ZipWriter::DiscardLastEntry() { | 
|  | 267 | if (state_ != State::kWritingZip || files_.empty()) { | 
|  | 268 | return kInvalidState; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | FileEntry& last_entry = files_.back(); | 
|  | 272 | current_offset_ = last_entry.local_file_header_offset; | 
|  | 273 | if (fseeko(file_, current_offset_, SEEK_SET) != 0) { | 
|  | 274 | return HandleError(kIoError); | 
|  | 275 | } | 
|  | 276 | files_.pop_back(); | 
|  | 277 | return kNoError; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | int32_t ZipWriter::GetLastEntry(FileEntry* out_entry) { | 
|  | 281 | CHECK(out_entry != nullptr); | 
|  | 282 |  | 
|  | 283 | if (files_.empty()) { | 
|  | 284 | return kInvalidState; | 
|  | 285 | } | 
|  | 286 | *out_entry = files_.back(); | 
|  | 287 | return kNoError; | 
|  | 288 | } | 
|  | 289 |  | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 290 | int32_t ZipWriter::PrepareDeflate() { | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 291 | CHECK(state_ == State::kWritingZip); | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 292 |  | 
|  | 293 | // Initialize the z_stream for compression. | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 294 | z_stream_ = std::unique_ptr<z_stream, void (*)(z_stream*)>(new z_stream(), DeleteZStream); | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 295 |  | 
| Colin Cross | 7c6c7f0 | 2016-09-16 10:15:51 -0700 | [diff] [blame] | 296 | #pragma GCC diagnostic push | 
|  | 297 | #pragma GCC diagnostic ignored "-Wold-style-cast" | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 298 | int zerr = deflateInit2(z_stream_.get(), Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, | 
|  | 299 | DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); | 
| Colin Cross | 7c6c7f0 | 2016-09-16 10:15:51 -0700 | [diff] [blame] | 300 | #pragma GCC diagnostic pop | 
|  | 301 |  | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 302 | if (zerr != Z_OK) { | 
|  | 303 | if (zerr == Z_VERSION_ERROR) { | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 304 | LOG(ERROR) << "Installed zlib is not compatible with linked version (" << ZLIB_VERSION << ")"; | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 305 | return HandleError(kZlibError); | 
|  | 306 | } else { | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 307 | LOG(ERROR) << "deflateInit2 failed (zerr=" << zerr << ")"; | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 308 | return HandleError(kZlibError); | 
|  | 309 | } | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | z_stream_->next_out = buffer_.data(); | 
|  | 313 | z_stream_->avail_out = buffer_.size(); | 
|  | 314 | return kNoError; | 
|  | 315 | } | 
|  | 316 |  | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 317 | int32_t ZipWriter::WriteBytes(const void* data, size_t len) { | 
|  | 318 | if (state_ != State::kWritingEntry) { | 
|  | 319 | return HandleError(kInvalidState); | 
|  | 320 | } | 
|  | 321 |  | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 322 | int32_t result = kNoError; | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 323 | if (current_file_entry_.compression_method & kCompressDeflated) { | 
|  | 324 | result = CompressBytes(¤t_file_entry_, data, len); | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 325 | } else { | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 326 | result = StoreBytes(¤t_file_entry_, data, len); | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 327 | } | 
|  | 328 |  | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 329 | if (result != kNoError) { | 
|  | 330 | return result; | 
|  | 331 | } | 
|  | 332 |  | 
| Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 333 | current_file_entry_.crc32 = | 
|  | 334 | crc32(current_file_entry_.crc32, reinterpret_cast<const Bytef*>(data), len); | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 335 | current_file_entry_.uncompressed_size += len; | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 336 | return kNoError; | 
|  | 337 | } | 
|  | 338 |  | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 339 | int32_t ZipWriter::StoreBytes(FileEntry* file, const void* data, size_t len) { | 
|  | 340 | CHECK(state_ == State::kWritingEntry); | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 341 |  | 
|  | 342 | if (fwrite(data, 1, len, file_) != len) { | 
|  | 343 | return HandleError(kIoError); | 
|  | 344 | } | 
|  | 345 | file->compressed_size += len; | 
|  | 346 | current_offset_ += len; | 
|  | 347 | return kNoError; | 
|  | 348 | } | 
|  | 349 |  | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 350 | int32_t ZipWriter::CompressBytes(FileEntry* file, const void* data, size_t len) { | 
|  | 351 | CHECK(state_ == State::kWritingEntry); | 
|  | 352 | CHECK(z_stream_); | 
|  | 353 | CHECK(z_stream_->next_out != nullptr); | 
|  | 354 | CHECK(z_stream_->avail_out != 0); | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 355 |  | 
|  | 356 | // Prepare the input. | 
|  | 357 | z_stream_->next_in = reinterpret_cast<const uint8_t*>(data); | 
|  | 358 | z_stream_->avail_in = len; | 
|  | 359 |  | 
|  | 360 | while (z_stream_->avail_in > 0) { | 
|  | 361 | // We have more data to compress. | 
|  | 362 | int zerr = deflate(z_stream_.get(), Z_NO_FLUSH); | 
|  | 363 | if (zerr != Z_OK) { | 
|  | 364 | return HandleError(kZlibError); | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | if (z_stream_->avail_out == 0) { | 
|  | 368 | // The output is full, let's write it to disk. | 
| 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) { | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 371 | return HandleError(kIoError); | 
|  | 372 | } | 
| Christopher Ferris | a2a32b0 | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 373 | file->compressed_size += write_bytes; | 
|  | 374 | current_offset_ += write_bytes; | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 375 |  | 
|  | 376 | // Reset the output buffer for the next input. | 
|  | 377 | z_stream_->next_out = buffer_.data(); | 
|  | 378 | z_stream_->avail_out = buffer_.size(); | 
|  | 379 | } | 
|  | 380 | } | 
|  | 381 | return kNoError; | 
|  | 382 | } | 
|  | 383 |  | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 384 | int32_t ZipWriter::FlushCompressedBytes(FileEntry* file) { | 
|  | 385 | CHECK(state_ == State::kWritingEntry); | 
|  | 386 | CHECK(z_stream_); | 
|  | 387 | CHECK(z_stream_->next_out != nullptr); | 
|  | 388 | CHECK(z_stream_->avail_out != 0); | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 389 |  | 
| Christopher Ferris | a2a32b0 | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 390 | // Keep deflating while there isn't enough space in the buffer to | 
|  | 391 | // to complete the compress. | 
|  | 392 | int zerr; | 
|  | 393 | while ((zerr = deflate(z_stream_.get(), Z_FINISH)) == Z_OK) { | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 394 | CHECK(z_stream_->avail_out == 0); | 
| Christopher Ferris | a2a32b0 | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 395 | size_t write_bytes = z_stream_->next_out - buffer_.data(); | 
|  | 396 | if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) { | 
|  | 397 | return HandleError(kIoError); | 
|  | 398 | } | 
|  | 399 | file->compressed_size += write_bytes; | 
|  | 400 | current_offset_ += write_bytes; | 
|  | 401 |  | 
|  | 402 | z_stream_->next_out = buffer_.data(); | 
|  | 403 | z_stream_->avail_out = buffer_.size(); | 
|  | 404 | } | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 405 | if (zerr != Z_STREAM_END) { | 
|  | 406 | return HandleError(kZlibError); | 
|  | 407 | } | 
|  | 408 |  | 
| Christopher Ferris | a2a32b0 | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 409 | size_t write_bytes = z_stream_->next_out - buffer_.data(); | 
|  | 410 | if (write_bytes != 0) { | 
|  | 411 | if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) { | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 412 | return HandleError(kIoError); | 
|  | 413 | } | 
| Christopher Ferris | a2a32b0 | 2015-11-04 17:54:32 -0800 | [diff] [blame] | 414 | file->compressed_size += write_bytes; | 
|  | 415 | current_offset_ += write_bytes; | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 416 | } | 
|  | 417 | z_stream_.reset(); | 
|  | 418 | return kNoError; | 
|  | 419 | } | 
|  | 420 |  | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 421 | int32_t ZipWriter::FinishEntry() { | 
|  | 422 | if (state_ != State::kWritingEntry) { | 
|  | 423 | return kInvalidState; | 
|  | 424 | } | 
|  | 425 |  | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 426 | if (current_file_entry_.compression_method & kCompressDeflated) { | 
|  | 427 | int32_t result = FlushCompressedBytes(¤t_file_entry_); | 
| Adam Lesinski | 591fd39 | 2015-10-06 15:23:46 -0700 | [diff] [blame] | 428 | if (result != kNoError) { | 
|  | 429 | return result; | 
|  | 430 | } | 
|  | 431 | } | 
|  | 432 |  | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 433 | if ((current_file_entry_.compression_method & kCompressDeflated) || !seekable_) { | 
|  | 434 | // Some versions of ZIP don't allow STORED data to have a trailing DataDescriptor. | 
|  | 435 | // If this file is not seekable, or if the data is compressed, write a DataDescriptor. | 
|  | 436 | const uint32_t sig = DataDescriptor::kOptSignature; | 
|  | 437 | if (fwrite(&sig, sizeof(sig), 1, file_) != 1) { | 
|  | 438 | return HandleError(kIoError); | 
|  | 439 | } | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 440 |  | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 441 | DataDescriptor dd = {}; | 
|  | 442 | dd.crc32 = current_file_entry_.crc32; | 
|  | 443 | dd.compressed_size = current_file_entry_.compressed_size; | 
|  | 444 | dd.uncompressed_size = current_file_entry_.uncompressed_size; | 
|  | 445 | if (fwrite(&dd, sizeof(dd), 1, file_) != 1) { | 
|  | 446 | return HandleError(kIoError); | 
|  | 447 | } | 
|  | 448 | current_offset_ += sizeof(DataDescriptor::kOptSignature) + sizeof(dd); | 
|  | 449 | } else { | 
|  | 450 | // Seek back to the header and rewrite to include the size. | 
|  | 451 | if (fseeko(file_, current_file_entry_.local_file_header_offset, SEEK_SET) != 0) { | 
|  | 452 | return HandleError(kIoError); | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | LocalFileHeader header = {}; | 
|  | 456 | CopyFromFileEntry(current_file_entry_, false /*use_data_descriptor*/, &header); | 
|  | 457 |  | 
|  | 458 | if (fwrite(&header, sizeof(header), 1, file_) != 1) { | 
|  | 459 | return HandleError(kIoError); | 
|  | 460 | } | 
|  | 461 |  | 
|  | 462 | if (fseeko(file_, current_offset_, SEEK_SET) != 0) { | 
|  | 463 | return HandleError(kIoError); | 
|  | 464 | } | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 465 | } | 
|  | 466 |  | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 467 | files_.emplace_back(std::move(current_file_entry_)); | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 468 | state_ = State::kWritingZip; | 
|  | 469 | return kNoError; | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | int32_t ZipWriter::Finish() { | 
|  | 473 | if (state_ != State::kWritingZip) { | 
|  | 474 | return kInvalidState; | 
|  | 475 | } | 
|  | 476 |  | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 477 | off_t startOfCdr = current_offset_; | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 478 | for (FileEntry& file : files_) { | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 479 | CentralDirectoryRecord cdr = {}; | 
|  | 480 | cdr.record_signature = CentralDirectoryRecord::kSignature; | 
| Adam Lesinski | d987c9d | 2017-04-06 18:55:47 -0700 | [diff] [blame] | 481 | if ((file.compression_method & kCompressDeflated) || !seekable_) { | 
|  | 482 | cdr.gpb_flags |= kGPBDDFlagMask; | 
|  | 483 | } | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 484 | cdr.compression_method = file.compression_method; | 
|  | 485 | cdr.last_mod_time = file.last_mod_time; | 
|  | 486 | cdr.last_mod_date = file.last_mod_date; | 
|  | 487 | cdr.crc32 = file.crc32; | 
|  | 488 | cdr.compressed_size = file.compressed_size; | 
|  | 489 | cdr.uncompressed_size = file.uncompressed_size; | 
|  | 490 | cdr.file_name_length = file.path.size(); | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 491 | cdr.local_file_header_offset = static_cast<uint32_t>(file.local_file_header_offset); | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 492 | if (fwrite(&cdr, sizeof(cdr), 1, file_) != 1) { | 
|  | 493 | return HandleError(kIoError); | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | if (fwrite(file.path.data(), 1, file.path.size(), file_) != file.path.size()) { | 
|  | 497 | return HandleError(kIoError); | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | current_offset_ += sizeof(cdr) + file.path.size(); | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | EocdRecord er = {}; | 
|  | 504 | er.eocd_signature = EocdRecord::kSignature; | 
| Adam Lesinski | 044c790 | 2015-10-20 12:41:49 -0700 | [diff] [blame] | 505 | er.disk_num = 0; | 
|  | 506 | er.cd_start_disk = 0; | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 507 | er.num_records_on_disk = files_.size(); | 
|  | 508 | er.num_records = files_.size(); | 
|  | 509 | er.cd_size = current_offset_ - startOfCdr; | 
|  | 510 | er.cd_start_offset = startOfCdr; | 
|  | 511 |  | 
|  | 512 | if (fwrite(&er, sizeof(er), 1, file_) != 1) { | 
|  | 513 | return HandleError(kIoError); | 
|  | 514 | } | 
|  | 515 |  | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 516 | current_offset_ += sizeof(er); | 
|  | 517 |  | 
|  | 518 | // Since we can BackUp() and potentially finish writing at an offset less than one we had | 
|  | 519 | // already written at, we must truncate the file. | 
|  | 520 |  | 
| Adam Lesinski | e2fa70b | 2017-03-29 16:10:11 -0700 | [diff] [blame] | 521 | if (ftruncate(fileno(file_), current_offset_) != 0) { | 
| Adam Lesinski | 537713b | 2017-03-16 13:23:51 -0700 | [diff] [blame] | 522 | return HandleError(kIoError); | 
|  | 523 | } | 
|  | 524 |  | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 525 | if (fflush(file_) != 0) { | 
|  | 526 | return HandleError(kIoError); | 
|  | 527 | } | 
|  | 528 |  | 
| Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 529 | state_ = State::kDone; | 
|  | 530 | return kNoError; | 
|  | 531 | } |