blob: ddd4dd5b68918eb311f6f9f9b72e2ffc4a607a79 [file] [log] [blame]
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -07001/*
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 Lesinskiad4ad8c2015-10-05 18:16:18 -070017#include "ziparchive/zip_writer.h"
18
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070019#include <cstdio>
Adam Lesinski537713b2017-03-16 13:23:51 -070020#include <sys/param.h>
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070021#include <zlib.h>
Adam Lesinski591fd392015-10-06 15:23:46 -070022#define DEF_MEM_LEVEL 8 // normally in zutil.h?
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070023
Adam Lesinski537713b2017-03-16 13:23:51 -070024#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 Ferris5e9f3d42016-01-19 10:33:03 -080033#if !defined(powerof2)
34#define powerof2(x) ((((x)-1)&(x))==0)
35#endif
36
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070037/* Zip compression methods we support */
38enum {
39 kCompressStored = 0, // no compression
40 kCompressDeflated = 8, // standard deflate
41};
42
Adam Lesinski591fd392015-10-06 15:23:46 -070043// Size of the output buffer used for compression.
44static const size_t kBufSize = 32768u;
45
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070046// No error, operation completed successfully.
47static const int32_t kNoError = 0;
48
49// The ZipWriter is in a bad state.
50static const int32_t kInvalidState = -1;
51
52// There was an IO error while writing to disk.
53static const int32_t kIoError = -2;
54
55// The zip entry name was invalid.
56static const int32_t kInvalidEntryName = -3;
57
Adam Lesinski591fd392015-10-06 15:23:46 -070058// An error occurred in zlib.
59static const int32_t kZlibError = -4;
60
Christopher Ferris5e9f3d42016-01-19 10:33:03 -080061// The start aligned function was called with the aligned flag.
62static const int32_t kInvalidAlign32Flag = -5;
63
64// The alignment parameter is not a power of 2.
65static const int32_t kInvalidAlignment = -6;
66
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070067static const char* sErrorCodes[] = {
68 "Invalid state",
69 "IO error",
70 "Invalid entry name",
Adam Lesinski591fd392015-10-06 15:23:46 -070071 "Zlib error",
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070072};
73
74const 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 Lesinski591fd392015-10-06 15:23:46 -070081static void DeleteZStream(z_stream* stream) {
82 deflateEnd(stream);
83 delete stream;
84}
85
86ZipWriter::ZipWriter(FILE* f) : file_(f), current_offset_(0), state_(State::kWritingZip),
87 z_stream_(nullptr, DeleteZStream), buffer_(kBufSize) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070088}
89
90ZipWriter::ZipWriter(ZipWriter&& writer) : file_(writer.file_),
91 current_offset_(writer.current_offset_),
92 state_(writer.state_),
Adam Lesinski591fd392015-10-06 15:23:46 -070093 files_(std::move(writer.files_)),
94 z_stream_(std::move(writer.z_stream_)),
95 buffer_(std::move(writer.buffer_)){
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070096 writer.file_ = nullptr;
97 writer.state_ = State::kError;
98}
99
100ZipWriter& 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 Lesinski591fd392015-10-06 15:23:46 -0700105 z_stream_ = std::move(writer.z_stream_);
106 buffer_ = std::move(writer.buffer_);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700107 writer.file_ = nullptr;
108 writer.state_ = State::kError;
109 return *this;
110}
111
112int32_t ZipWriter::HandleError(int32_t error_code) {
113 state_ = State::kError;
Adam Lesinski591fd392015-10-06 15:23:46 -0700114 z_stream_.reset();
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700115 return error_code;
116}
117
118int32_t ZipWriter::StartEntry(const char* path, size_t flags) {
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800119 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
127int32_t ZipWriter::StartAlignedEntry(const char* path, size_t flags, uint32_t alignment) {
128 return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
129}
130
131int32_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 Lesinskiad4ad8c2015-10-05 18:16:18 -0700138}
139
140static 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 Ferris5e9f3d42016-01-19 10:33:03 -0800161int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags,
162 time_t time, uint32_t alignment) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700163 if (state_ != State::kWritingZip) {
164 return kInvalidState;
165 }
166
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800167 if (flags & kAlign32) {
168 return kInvalidAlign32Flag;
169 }
170
171 if (powerof2(alignment) == 0) {
172 return kInvalidAlignment;
173 }
174
Adam Lesinski537713b2017-03-16 13:23:51 -0700175 current_file_entry_ = {};
176 current_file_entry_.path = path;
177 current_file_entry_.local_file_header_offset = current_offset_;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700178
Adam Lesinski537713b2017-03-16 13:23:51 -0700179 if (!IsValidEntryName(reinterpret_cast<const uint8_t*>(current_file_entry_.path.data()),
180 current_file_entry_.path.size())) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700181 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 Lesinski591fd392015-10-06 15:23:46 -0700191 if (flags & ZipWriter::kCompress) {
Adam Lesinski537713b2017-03-16 13:23:51 -0700192 current_file_entry_.compression_method = kCompressDeflated;
Adam Lesinski591fd392015-10-06 15:23:46 -0700193
194 int32_t result = PrepareDeflate();
195 if (result != kNoError) {
196 return result;
197 }
198 } else {
Adam Lesinski537713b2017-03-16 13:23:51 -0700199 current_file_entry_.compression_method = kCompressStored;
Adam Lesinski591fd392015-10-06 15:23:46 -0700200 }
Adam Lesinski537713b2017-03-16 13:23:51 -0700201 header.compression_method = current_file_entry_.compression_method;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700202
Adam Lesinski537713b2017-03-16 13:23:51 -0700203 ExtractTimeAndDate(time, &current_file_entry_.last_mod_time, &current_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 Lesinskiad4ad8c2015-10-05 18:16:18 -0700206
Adam Lesinski537713b2017-03-16 13:23:51 -0700207 header.file_name_length = current_file_entry_.path.size();
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700208
Adam Lesinski537713b2017-03-16 13:23:51 -0700209 off64_t offset = current_offset_ + sizeof(header) + current_file_entry_.path.size();
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800210 std::vector<char> zero_padding;
211 if (alignment != 0 && (offset & (alignment - 1))) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700212 // Pad the extra field so the data will be aligned.
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800213 uint16_t padding = alignment - (offset % alignment);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700214 header.extra_field_length = padding;
215 offset += padding;
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800216 zero_padding.resize(padding);
217 memset(zero_padding.data(), 0, zero_padding.size());
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700218 }
219
220 if (fwrite(&header, sizeof(header), 1, file_) != 1) {
221 return HandleError(kIoError);
222 }
223
Adam Lesinski537713b2017-03-16 13:23:51 -0700224 if (fwrite(path, sizeof(*path), current_file_entry_.path.size(), file_)
225 != current_file_entry_.path.size()) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700226 return HandleError(kIoError);
227 }
228
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800229 if (header.extra_field_length != 0 &&
230 fwrite(zero_padding.data(), 1, header.extra_field_length, file_)
231 != header.extra_field_length) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700232 return HandleError(kIoError);
233 }
234
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700235 current_offset_ = offset;
236 state_ = State::kWritingEntry;
237 return kNoError;
238}
239
Adam Lesinski537713b2017-03-16 13:23:51 -0700240int32_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
254int32_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 Lesinski591fd392015-10-06 15:23:46 -0700264int32_t ZipWriter::PrepareDeflate() {
Adam Lesinski537713b2017-03-16 13:23:51 -0700265 CHECK(state_ == State::kWritingZip);
Adam Lesinski591fd392015-10-06 15:23:46 -0700266
267 // Initialize the z_stream for compression.
268 z_stream_ = std::unique_ptr<z_stream, void(*)(z_stream*)>(new z_stream(), DeleteZStream);
269
Colin Cross7c6c7f02016-09-16 10:15:51 -0700270#pragma GCC diagnostic push
271#pragma GCC diagnostic ignored "-Wold-style-cast"
Adam Lesinski591fd392015-10-06 15:23:46 -0700272 int zerr = deflateInit2(z_stream_.get(), Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS,
273 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
Colin Cross7c6c7f02016-09-16 10:15:51 -0700274#pragma GCC diagnostic pop
275
Adam Lesinski591fd392015-10-06 15:23:46 -0700276 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 Lesinskiad4ad8c2015-10-05 18:16:18 -0700291int32_t ZipWriter::WriteBytes(const void* data, size_t len) {
292 if (state_ != State::kWritingEntry) {
293 return HandleError(kInvalidState);
294 }
295
Adam Lesinski591fd392015-10-06 15:23:46 -0700296 int32_t result = kNoError;
Adam Lesinski537713b2017-03-16 13:23:51 -0700297 if (current_file_entry_.compression_method & kCompressDeflated) {
298 result = CompressBytes(&current_file_entry_, data, len);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700299 } else {
Adam Lesinski537713b2017-03-16 13:23:51 -0700300 result = StoreBytes(&current_file_entry_, data, len);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700301 }
302
Adam Lesinski591fd392015-10-06 15:23:46 -0700303 if (result != kNoError) {
304 return result;
305 }
306
Adam Lesinski537713b2017-03-16 13:23:51 -0700307 current_file_entry_.crc32 = crc32(current_file_entry_.crc32,
308 reinterpret_cast<const Bytef*>(data), len);
309 current_file_entry_.uncompressed_size += len;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700310 return kNoError;
311}
312
Adam Lesinski537713b2017-03-16 13:23:51 -0700313int32_t ZipWriter::StoreBytes(FileEntry* file, const void* data, size_t len) {
314 CHECK(state_ == State::kWritingEntry);
Adam Lesinski591fd392015-10-06 15:23:46 -0700315
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 Lesinski537713b2017-03-16 13:23:51 -0700324int32_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 Lesinski591fd392015-10-06 15:23:46 -0700329
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 Ferrisa2a32b02015-11-04 17:54:32 -0800343 size_t write_bytes = z_stream_->next_out - buffer_.data();
344 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) {
Adam Lesinski591fd392015-10-06 15:23:46 -0700345 return HandleError(kIoError);
346 }
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800347 file->compressed_size += write_bytes;
348 current_offset_ += write_bytes;
Adam Lesinski591fd392015-10-06 15:23:46 -0700349
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 Lesinski537713b2017-03-16 13:23:51 -0700358int32_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 Lesinski591fd392015-10-06 15:23:46 -0700363
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800364 // 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 Lesinski537713b2017-03-16 13:23:51 -0700368 CHECK(z_stream_->avail_out == 0);
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800369 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 Lesinski591fd392015-10-06 15:23:46 -0700379 if (zerr != Z_STREAM_END) {
380 return HandleError(kZlibError);
381 }
382
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800383 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 Lesinski591fd392015-10-06 15:23:46 -0700386 return HandleError(kIoError);
387 }
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800388 file->compressed_size += write_bytes;
389 current_offset_ += write_bytes;
Adam Lesinski591fd392015-10-06 15:23:46 -0700390 }
391 z_stream_.reset();
392 return kNoError;
393}
394
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700395int32_t ZipWriter::FinishEntry() {
396 if (state_ != State::kWritingEntry) {
397 return kInvalidState;
398 }
399
Adam Lesinski537713b2017-03-16 13:23:51 -0700400 if (current_file_entry_.compression_method & kCompressDeflated) {
401 int32_t result = FlushCompressedBytes(&current_file_entry_);
Adam Lesinski591fd392015-10-06 15:23:46 -0700402 if (result != kNoError) {
403 return result;
404 }
405 }
406
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700407 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 Lesinskiad4ad8c2015-10-05 18:16:18 -0700413 DataDescriptor dd = {};
Adam Lesinski537713b2017-03-16 13:23:51 -0700414 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 Lesinskiad4ad8c2015-10-05 18:16:18 -0700417 if (fwrite(&dd, sizeof(dd), 1, file_) != 1) {
418 return HandleError(kIoError);
419 }
420
Adam Lesinski537713b2017-03-16 13:23:51 -0700421 files_.emplace_back(std::move(current_file_entry_));
422
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700423 current_offset_ += sizeof(DataDescriptor::kOptSignature) + sizeof(dd);
424 state_ = State::kWritingZip;
425 return kNoError;
426}
427
428int32_t ZipWriter::Finish() {
429 if (state_ != State::kWritingZip) {
430 return kInvalidState;
431 }
432
433 off64_t startOfCdr = current_offset_;
Adam Lesinski537713b2017-03-16 13:23:51 -0700434 for (FileEntry& file : files_) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700435 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 Lesinski044c7902015-10-20 12:41:49 -0700459 er.disk_num = 0;
460 er.cd_start_disk = 0;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700461 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 Lesinski537713b2017-03-16 13:23:51 -0700470 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 Lesinskiad4ad8c2015-10-05 18:16:18 -0700479 if (fflush(file_) != 0) {
480 return HandleError(kIoError);
481 }
482
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700483 state_ = State::kDone;
484 return kNoError;
485}