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