blob: 05cb2d01a2f0f091faea79425fa75bcf739a1257 [file] [log] [blame]
Christopher Ferris2b7daba2015-11-10 14:55:12 -08001/*
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 Hughes51cbbaa2018-10-19 16:09:39 -070017#pragma once
18
19#include <ziparchive/zip_archive.h>
Christopher Ferris2b7daba2015-11-10 14:55:12 -080020
21#include <stdint.h>
22#include <stdlib.h>
23#include <unistd.h>
24
Tianjie Xu484de542016-09-29 15:27:41 -070025#include <memory>
Tianjie Xua5e894b2020-03-05 16:31:23 -080026#include <utility>
Tianjie Xu484de542016-09-29 15:27:41 -070027#include <vector>
28
Narayan Kamath3a5a93b2017-06-15 13:58:25 +010029#include "android-base/macros.h"
Elliott Hughes51cbbaa2018-10-19 16:09:39 -070030#include "android-base/mapped_file.h"
Tianjiecc924632020-03-26 12:34:44 -070031#include "android-base/memory.h"
Tianjie Xu91caafe2020-03-13 16:16:24 -070032#include "zip_cd_entry_map.h"
33#include "zip_error.h"
Christopher Ferris2b7daba2015-11-10 14:55:12 -080034
Tianjie Xu484de542016-09-29 15:27:41 -070035class MappedZipFile {
36 public:
Jiyong Park6821cc82017-06-30 17:23:33 +090037 explicit MappedZipFile(const int fd)
Ryan Mitchell0edeea92020-03-09 09:33:46 -070038 : 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 Xu484de542016-09-29 15:27:41 -070042
Elliott Hughes62c2d532019-10-22 11:44:50 -070043 explicit MappedZipFile(const void* address, size_t length)
Ryan Mitchell0edeea92020-03-09 09:33:46 -070044 : has_fd_(false), fd_(-1), fd_offset_(0), base_ptr_(address),
45 data_length_(static_cast<off64_t>(length)) {}
Tianjie Xu484de542016-09-29 15:27:41 -070046
Jiyong Park6821cc82017-06-30 17:23:33 +090047 bool HasFd() const { return has_fd_; }
Tianjie Xu484de542016-09-29 15:27:41 -070048
49 int GetFileDescriptor() const;
50
Elliott Hughes62c2d532019-10-22 11:44:50 -070051 const void* GetBasePtr() const;
Tianjie Xu484de542016-09-29 15:27:41 -070052
Ryan Mitchell0edeea92020-03-09 09:33:46 -070053 off64_t GetFileOffset() const;
54
Tianjie Xu484de542016-09-29 15:27:41 -070055 off64_t GetFileLength() const;
56
Narayan Kamath0a5df0d2017-10-26 14:08:38 +010057 bool ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const;
Tianjie Xu484de542016-09-29 15:27:41 -070058
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 Mitchell0edeea92020-03-09 09:33:46 -070067 const off64_t fd_offset_;
Tianjie Xu484de542016-09-29 15:27:41 -070068
Elliott Hughes62c2d532019-10-22 11:44:50 -070069 const void* const base_ptr_;
Ryan Mitchell0edeea92020-03-09 09:33:46 -070070 mutable off64_t data_length_;
Tianjie Xu484de542016-09-29 15:27:41 -070071};
72
73class CentralDirectory {
74 public:
Jiyong Park6821cc82017-06-30 17:23:33 +090075 CentralDirectory(void) : base_ptr_(nullptr), length_(0) {}
Tianjie Xu484de542016-09-29 15:27:41 -070076
Jiyong Park6821cc82017-06-30 17:23:33 +090077 const uint8_t* GetBasePtr() const { return base_ptr_; }
Tianjie Xu484de542016-09-29 15:27:41 -070078
Jiyong Park6821cc82017-06-30 17:23:33 +090079 size_t GetMapLength() const { return length_; }
Tianjie Xu484de542016-09-29 15:27:41 -070080
Elliott Hughes62c2d532019-10-22 11:44:50 -070081 void Initialize(const void* map_base_ptr, off64_t cd_start_offset, size_t cd_size);
Tianjie Xu484de542016-09-29 15:27:41 -070082
83 private:
84 const uint8_t* base_ptr_;
85 size_t length_;
86};
87
Christopher Ferris2b7daba2015-11-10 14:55:12 -080088struct ZipArchive {
89 // open Zip archive
Tianjie Xu484de542016-09-29 15:27:41 -070090 mutable MappedZipFile mapped_zip;
Christopher Ferris2b7daba2015-11-10 14:55:12 -080091 const bool close_file;
92
93 // mapped central directory area
94 off64_t directory_offset;
Tianjie Xu484de542016-09-29 15:27:41 -070095 CentralDirectory central_directory;
Elliott Hughes51cbbaa2018-10-19 16:09:39 -070096 std::unique_ptr<android::base::MappedFile> directory_map;
Christopher Ferris2b7daba2015-11-10 14:55:12 -080097
98 // number of entries in the Zip archive
Tianjie Xu53a7ca02020-03-11 11:59:10 -070099 uint64_t num_entries;
Tianjie Xua5e894b2020-03-05 16:31:23 -0800100 std::unique_ptr<CdEntryMapInterface> cd_entry_map;
Christopher Ferris2b7daba2015-11-10 14:55:12 -0800101
Ryan Mitchell0edeea92020-03-09 09:33:46 -0700102 ZipArchive(MappedZipFile&& map, bool assume_ownership);
Elliott Hughes62c2d532019-10-22 11:44:50 -0700103 ZipArchive(const void* address, size_t length);
Josh Gaocdd80822018-07-17 11:08:48 -0700104 ~ZipArchive();
Tianjie Xu484de542016-09-29 15:27:41 -0700105
Elliott Hughes51cbbaa2018-10-19 16:09:39 -0700106 bool InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size);
Christopher Ferris2b7daba2015-11-10 14:55:12 -0800107};
Tianjiecc924632020-03-26 12:34:44 -0700108
Tianjiecc924632020-03-26 12:34:44 -0700109// Reads the unaligned data of type |T| and auto increment the offset.
110template <typename T>
111static 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.
118template <typename T>
119void EmitUnaligned(uint8_t** address, T data) {
120 android::base::put_unaligned<T>(*address, data);
121 *address += sizeof(T);
122}