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; |
Ryan Prichard | 35f31c6 | 2018-10-10 22:11:57 -0700 | [diff] [blame^] | 35 | typedef ZipArchive* ZipArchiveHandle; |
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: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 44 | bool ExtractToFile(File& file, std::string* error_msg); |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 45 | // Extract this entry to anonymous memory (R/W). |
| 46 | // Returns null on failure and sets error_msg. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 47 | MemMap ExtractToMemMap(const char* zip_filename, |
| 48 | const char* entry_filename, |
| 49 | std::string* error_msg); |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 50 | // Create a file-backed private (clean, R/W) memory mapping to this entry. |
| 51 | // 'zip_filename' is used for diagnostics only, |
| 52 | // the original file that the ZipArchive was open with is used |
| 53 | // for the mapping. |
| 54 | // |
| 55 | // Will only succeed if the entry is stored uncompressed. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 56 | // Returns invalid MemMap on failure and sets error_msg. |
| 57 | MemMap MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 58 | virtual ~ZipEntry(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 59 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 60 | MemMap MapDirectlyOrExtract(const char* zip_filename, |
| 61 | const char* entry_filename, |
| 62 | std::string* error_msg); |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame] | 63 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 64 | uint32_t GetUncompressedLength(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 65 | uint32_t GetCrc32(); |
| 66 | |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 67 | bool IsUncompressed(); |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 68 | bool IsAlignedTo(size_t alignment) const; |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 69 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 70 | private: |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 71 | ZipEntry(ZipArchiveHandle handle, |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 72 | ::ZipEntry* zip_entry, |
| 73 | const std::string& entry_name) |
| 74 | : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 75 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 76 | ZipArchiveHandle handle_; |
| 77 | ::ZipEntry* const zip_entry_; |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 78 | std::string const entry_name_; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 79 | |
| 80 | friend class ZipArchive; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 81 | DISALLOW_COPY_AND_ASSIGN(ZipEntry); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 84 | class ZipArchive { |
| 85 | public: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 86 | // return new ZipArchive instance on success, null on error. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 87 | static ZipArchive* Open(const char* filename, std::string* error_msg); |
| 88 | static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 89 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 90 | ZipEntry* Find(const char* name, std::string* error_msg) const; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 91 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 92 | ~ZipArchive(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 93 | |
| 94 | private: |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 95 | explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 96 | |
| 97 | friend class ZipEntry; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 98 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 99 | ZipArchiveHandle handle_; |
| 100 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 101 | DISALLOW_COPY_AND_ASSIGN(ZipArchive); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | } // namespace art |
| 105 | |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 106 | #endif // ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_ |