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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ZIP_ARCHIVE_H_ |
| 18 | #define ART_RUNTIME_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 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 24 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 25 | #include "base/unix_file/random_access_file.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 26 | #include "globals.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 27 | #include "mem_map.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 28 | #include "os.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 29 | #include "safe_map.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 30 | |
Andreas Gampe | 0c2d3e5 | 2017-07-03 12:50:44 -0700 | [diff] [blame] | 31 | // system/core/zip_archive definitions. |
| 32 | struct ZipEntry; |
| 33 | typedef void* ZipArchiveHandle; |
| 34 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 35 | namespace art { |
| 36 | |
| 37 | class ZipArchive; |
| 38 | class MemMap; |
| 39 | |
| 40 | class ZipEntry { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 41 | public: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 42 | bool ExtractToFile(File& file, std::string* error_msg); |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 43 | // Extract this entry to anonymous memory (R/W). |
| 44 | // Returns null on failure and sets error_msg. |
Brian Carlstrom | 0aa504b | 2014-05-23 02:47:28 -0700 | [diff] [blame] | 45 | MemMap* ExtractToMemMap(const char* zip_filename, const char* entry_filename, |
| 46 | std::string* error_msg); |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 47 | // Create a file-backed private (clean, R/W) memory mapping to this entry. |
| 48 | // 'zip_filename' is used for diagnostics only, |
| 49 | // the original file that the ZipArchive was open with is used |
| 50 | // for the mapping. |
| 51 | // |
| 52 | // Will only succeed if the entry is stored uncompressed. |
| 53 | // Returns null on failure and sets error_msg. |
| 54 | MemMap* MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 55 | virtual ~ZipEntry(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 56 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 57 | uint32_t GetUncompressedLength(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 58 | uint32_t GetCrc32(); |
| 59 | |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 60 | bool IsUncompressed(); |
| 61 | bool IsAlignedTo(size_t alignment); |
| 62 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 63 | private: |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 64 | ZipEntry(ZipArchiveHandle handle, |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 65 | ::ZipEntry* zip_entry, |
| 66 | const std::string& entry_name) |
| 67 | : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 68 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 69 | ZipArchiveHandle handle_; |
| 70 | ::ZipEntry* const zip_entry_; |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 71 | std::string const entry_name_; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 72 | |
| 73 | friend class ZipArchive; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 74 | DISALLOW_COPY_AND_ASSIGN(ZipEntry); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 77 | class ZipArchive { |
| 78 | public: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 79 | // return new ZipArchive instance on success, null on error. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 80 | static ZipArchive* Open(const char* filename, std::string* error_msg); |
| 81 | static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 82 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 83 | ZipEntry* Find(const char* name, std::string* error_msg) const; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 84 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 85 | ~ZipArchive(); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 86 | |
| 87 | private: |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 88 | explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {} |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 89 | |
| 90 | friend class ZipEntry; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 91 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 92 | ZipArchiveHandle handle_; |
| 93 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 94 | DISALLOW_COPY_AND_ASSIGN(ZipArchive); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | } // namespace art |
| 98 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 99 | #endif // ART_RUNTIME_ZIP_ARCHIVE_H_ |