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 17 #ifndef ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_ 18 #define ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_ 19 20 #include <stdint.h> 21 #include <memory> 22 #include <string> 23 24 #include <android-base/logging.h> 25 26 #include "globals.h" 27 #include "mem_map.h" 28 #include "os.h" 29 #include "safe_map.h" 30 #include "unix_file/random_access_file.h" 31 32 // system/core/zip_archive definitions. 33 struct ZipArchive; 34 struct ZipEntry; 35 using ZipArchiveHandle = ZipArchive*; 36 37 namespace art { 38 39 class ZipArchive; 40 class MemMap; 41 42 class ZipEntry { 43 public: 44 // Extracts this entry to file. 45 // Returns true on success, false on failure. 46 bool ExtractToFile(File& file, /*out*/std::string* error_msg); 47 // Extract this entry to anonymous memory (R/W). 48 // Returns null on failure and sets error_msg. 49 MemMap ExtractToMemMap(const char* zip_filename, 50 const char* entry_filename, 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); 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. 61 // Returns invalid MemMap on failure and sets error_msg. 62 MemMap MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg); 63 virtual ~ZipEntry(); 64 65 MemMap MapDirectlyOrExtract(const char* zip_filename, 66 const char* entry_filename, 67 std::string* error_msg, 68 size_t alignment); 69 70 uint32_t GetUncompressedLength(); 71 uint32_t GetCrc32(); 72 73 bool IsUncompressed(); 74 bool IsAlignedTo(size_t alignment) const; 75 76 private: ZipEntry(ZipArchiveHandle handle,::ZipEntry * zip_entry,const std::string & entry_name)77 ZipEntry(ZipArchiveHandle handle, 78 ::ZipEntry* zip_entry, 79 const std::string& entry_name) 80 : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {} 81 82 ZipArchiveHandle handle_; 83 ::ZipEntry* const zip_entry_; 84 std::string const entry_name_; 85 86 friend class ZipArchive; 87 DISALLOW_COPY_AND_ASSIGN(ZipEntry); 88 }; 89 90 class ZipArchive { 91 public: 92 // return new ZipArchive instance on success, null on error. 93 static ZipArchive* Open(const char* filename, std::string* error_msg); 94 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg); 95 static ZipArchive* OpenFromOwnedFd(int fd, const char* filename, std::string* error_msg); 96 static ZipArchive* OpenFromMemory(const uint8_t* data, 97 size_t size, 98 const char* filename, 99 std::string* error_msg); 100 101 ZipEntry* Find(const char* name, std::string* error_msg) const; 102 103 ~ZipArchive(); 104 105 private: 106 static ZipArchive* OpenFromFdInternal(int fd, 107 bool assume_ownership, 108 const char* filename, 109 std::string* error_msg); 110 ZipArchive(ZipArchiveHandle handle)111 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {} 112 113 friend class ZipEntry; 114 115 ZipArchiveHandle handle_; 116 117 DISALLOW_COPY_AND_ASSIGN(ZipArchive); 118 }; 119 120 } // namespace art 121 122 #endif // ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_ 123