Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [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 | |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_ |
| 18 | #define ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_ |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 19 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <string> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 23 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 26 | #include "globals.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 27 | #include "mem_map.h" |
| 28 | #include "os.h" |
| 29 | #include "safe_map.h" |
| 30 | #include "unix_file/random_access_file.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | 0c2d3e5 | 2017-07-03 12:50:44 -0700 | [diff] [blame] | 32 | // system/core/zip_archive definitions. |
Ryan Prichard | 35f31c6 | 2018-10-10 22:11:57 -0700 | [diff] [blame] | 33 | struct ZipArchive; |
Andreas Gampe | 0c2d3e5 | 2017-07-03 12:50:44 -0700 | [diff] [blame] | 34 | struct ZipEntry; |
Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 35 | using ZipArchiveHandle = ZipArchive*; |
Andreas Gampe | 0c2d3e5 | 2017-07-03 12:50:44 -0700 | [diff] [blame] | 36 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | |
| 39 | class ZipArchive; |
| 40 | class MemMap; |
| 41 | |
| 42 | class ZipEntry { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 43 | public: |
Vladimir Marko | 9e4b42a | 2020-04-28 12:28:21 +0100 | [diff] [blame] | 44 | // Extracts this entry to file. |
| 45 | // Returns true on success, false on failure. |
| 46 | bool ExtractToFile(File& file, /*out*/std::string* error_msg); |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 47 | // Extract this entry to anonymous memory (R/W). |
| 48 | // Returns null on failure and sets error_msg. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 49 | MemMap ExtractToMemMap(const char* zip_filename, |
| 50 | const char* entry_filename, |
Vladimir Marko | 9e4b42a | 2020-04-28 12:28:21 +0100 | [diff] [blame] | 51 | /*out*/std::string* error_msg); |
| 52 | // Extracts this entry to memory. Stores `GetUncompressedSize()` bytes on success. |
| 53 | // Returns true on success, false on failure. |
| 54 | bool ExtractToMemory(/*out*/uint8_t* buffer, /*out*/std::string* error_msg); |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 55 | // Create a file-backed private (clean, R/W) memory mapping to this entry. |
| 56 | // 'zip_filename' is used for diagnostics only, |
| 57 | // the original file that the ZipArchive was open with is used |
| 58 | // for the mapping. |
| 59 | // |
| 60 | // Will only succeed if the entry is stored uncompressed. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 61 | // Returns invalid MemMap on failure and sets error_msg. |
| 62 | MemMap MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 63 | virtual ~ZipEntry(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 64 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 65 | MemMap MapDirectlyOrExtract(const char* zip_filename, |
| 66 | const char* entry_filename, |
Colin Cross | 2b41cca | 2018-11-16 22:43:41 -0800 | [diff] [blame] | 67 | std::string* error_msg, |
| 68 | size_t alignment); |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame] | 69 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 70 | uint32_t GetUncompressedLength(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 71 | uint32_t GetCrc32(); |
| 72 | |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 73 | bool IsUncompressed(); |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 74 | bool IsAlignedTo(size_t alignment) const; |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 75 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 76 | private: |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 77 | ZipEntry(ZipArchiveHandle handle, |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 78 | ::ZipEntry* zip_entry, |
| 79 | const std::string& entry_name) |
| 80 | : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 81 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 82 | ZipArchiveHandle handle_; |
| 83 | ::ZipEntry* const zip_entry_; |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 84 | std::string const entry_name_; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 85 | |
| 86 | friend class ZipArchive; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 87 | DISALLOW_COPY_AND_ASSIGN(ZipEntry); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 90 | class ZipArchive { |
| 91 | public: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 92 | // return new ZipArchive instance on success, null on error. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 93 | static ZipArchive* Open(const char* filename, std::string* error_msg); |
| 94 | static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg); |
Victor Hsieh | ecaf7d1 | 2021-06-14 11:09:21 -0700 | [diff] [blame] | 95 | static ZipArchive* OpenFromOwnedFd(int fd, const char* filename, std::string* error_msg); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 96 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 97 | ZipEntry* Find(const char* name, std::string* error_msg) const; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 98 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 99 | ~ZipArchive(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 100 | |
| 101 | private: |
Victor Hsieh | ecaf7d1 | 2021-06-14 11:09:21 -0700 | [diff] [blame] | 102 | static ZipArchive* OpenFromFdInternal(int fd, |
| 103 | bool assume_ownership, |
| 104 | const char* filename, |
| 105 | std::string* error_msg); |
| 106 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 107 | explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 108 | |
| 109 | friend class ZipEntry; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 110 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 111 | ZipArchiveHandle handle_; |
| 112 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 113 | DISALLOW_COPY_AND_ASSIGN(ZipArchive); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // namespace art |
| 117 | |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 118 | #endif // ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_ |