Christopher Ferris | 2b7daba | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include <ziparchive/zip_archive.h> |
Christopher Ferris | 2b7daba | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 25 | #include <memory> |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 26 | #include <utility> |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Narayan Kamath | 3a5a93b | 2017-06-15 13:58:25 +0100 | [diff] [blame] | 29 | #include "android-base/macros.h" |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 30 | #include "android-base/mapped_file.h" |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 31 | #include "android-base/memory.h" |
Tianjie Xu | 91caafe | 2020-03-13 16:16:24 -0700 | [diff] [blame] | 32 | #include "zip_cd_entry_map.h" |
| 33 | #include "zip_error.h" |
Christopher Ferris | 2b7daba | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 34 | |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 35 | class MappedZipFile { |
| 36 | public: |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 37 | explicit MappedZipFile(const int fd) |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 38 | : has_fd_(true), fd_(fd), fd_offset_(0), base_ptr_(nullptr), data_length_(-1) {} |
| 39 | |
| 40 | explicit MappedZipFile(const int fd, off64_t length, off64_t offset) |
| 41 | : has_fd_(true), fd_(fd), fd_offset_(offset), base_ptr_(nullptr), data_length_(length) {} |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 43 | explicit MappedZipFile(const void* address, size_t length) |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 44 | : has_fd_(false), fd_(-1), fd_offset_(0), base_ptr_(address), |
| 45 | data_length_(static_cast<off64_t>(length)) {} |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 46 | |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 47 | bool HasFd() const { return has_fd_; } |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 48 | |
| 49 | int GetFileDescriptor() const; |
| 50 | |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 51 | const void* GetBasePtr() const; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 52 | |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 53 | off64_t GetFileOffset() const; |
| 54 | |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 55 | off64_t GetFileLength() const; |
| 56 | |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 57 | bool ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | // If has_fd_ is true, fd is valid and we'll read contents of a zip archive |
| 61 | // from the file. Otherwise, we're opening the archive from a memory mapped |
| 62 | // file. In that case, base_ptr_ points to the start of the memory region and |
| 63 | // data_length_ defines the file length. |
| 64 | const bool has_fd_; |
| 65 | |
| 66 | const int fd_; |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 67 | const off64_t fd_offset_; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 68 | |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 69 | const void* const base_ptr_; |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 70 | mutable off64_t data_length_; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | class CentralDirectory { |
| 74 | public: |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 75 | CentralDirectory(void) : base_ptr_(nullptr), length_(0) {} |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 76 | |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 77 | const uint8_t* GetBasePtr() const { return base_ptr_; } |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 78 | |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 79 | size_t GetMapLength() const { return length_; } |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 80 | |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 81 | void Initialize(const void* map_base_ptr, off64_t cd_start_offset, size_t cd_size); |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | const uint8_t* base_ptr_; |
| 85 | size_t length_; |
| 86 | }; |
| 87 | |
Christopher Ferris | 2b7daba | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 88 | struct ZipArchive { |
| 89 | // open Zip archive |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 90 | mutable MappedZipFile mapped_zip; |
Christopher Ferris | 2b7daba | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 91 | const bool close_file; |
| 92 | |
| 93 | // mapped central directory area |
| 94 | off64_t directory_offset; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 95 | CentralDirectory central_directory; |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 96 | std::unique_ptr<android::base::MappedFile> directory_map; |
Christopher Ferris | 2b7daba | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 97 | |
| 98 | // number of entries in the Zip archive |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 99 | uint64_t num_entries; |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 100 | std::unique_ptr<CdEntryMapInterface> cd_entry_map; |
Christopher Ferris | 2b7daba | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 101 | |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 102 | ZipArchive(MappedZipFile&& map, bool assume_ownership); |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 103 | ZipArchive(const void* address, size_t length); |
Josh Gao | cdd8082 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 104 | ~ZipArchive(); |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 105 | |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 106 | bool InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size); |
Christopher Ferris | 2b7daba | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 107 | }; |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 108 | |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 109 | // Reads the unaligned data of type |T| and auto increment the offset. |
| 110 | template <typename T> |
| 111 | static T ConsumeUnaligned(uint8_t** address) { |
| 112 | auto ret = android::base::get_unaligned<T>(*address); |
| 113 | *address += sizeof(T); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | // Writes the unaligned data of type |T| and auto increment the offset. |
| 118 | template <typename T> |
| 119 | void EmitUnaligned(uint8_t** address, T data) { |
| 120 | android::base::put_unaligned<T>(*address, data); |
| 121 | *address += sizeof(T); |
| 122 | } |