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 22 #include <memory> 23 #include <string> 24 25 #include "globals.h" 26 #include "mem_map.h" 27 #include "os.h" 28 #include "safe_map.h" 29 #include "unix_file/random_access_file.h" 30 31 // system/core/zip_archive definitions. 32 struct ZipArchive; 33 struct ZipEntry; 34 using ZipArchiveHandle = ZipArchive*; 35 36 namespace art { 37 38 class ZipArchive; 39 class MemMap; 40 41 class ZipEntry { 42 public: 43 // Extracts this entry to file. 44 // Returns true on success, false on failure. 45 bool ExtractToFile(File& file, /*out*/std::string* error_msg); 46 // Extract this entry to anonymous memory (R/W). 47 // Returns null on failure and sets error_msg. 48 MemMap ExtractToMemMap(const char* zip_filename, 49 const char* entry_filename, 50 /*out*/std::string* error_msg); 51 // Extracts this entry to memory. Stores `GetUncompressedSize()` bytes on success. 52 // Returns true on success, false on failure. 53 bool ExtractToMemory(/*out*/uint8_t* buffer, /*out*/std::string* error_msg); 54 // Create a file-backed private (clean, R/W) memory mapping to this entry. 55 // 'zip_filename' is used for diagnostics only, 56 // the original file that the ZipArchive was open with is used 57 // for the mapping. 58 // 59 // Will only succeed if the entry is stored uncompressed. 60 // Returns invalid MemMap on failure and sets error_msg. 61 MemMap MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg); 62 virtual ~ZipEntry(); 63 64 MemMap MapDirectlyOrExtract(const char* zip_filename, 65 const char* entry_filename, 66 std::string* error_msg, 67 size_t alignment); 68 69 uint32_t GetUncompressedLength() const; 70 uint32_t GetCrc32() const; 71 72 bool IsUncompressed() const; 73 bool IsAlignedTo(size_t alignment) const; 74 off_t GetOffset() 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 // Same as Find, but doesn't return an error message if the entry is not found. The callers 104 // should expect that the returned pointer is null while the error message is empty. 105 ZipEntry* FindOrNull(const char* name, std::string* error_msg) const; 106 107 ~ZipArchive(); 108 109 private: 110 static ZipArchive* OpenFromFdInternal(int fd, 111 bool assume_ownership, 112 const char* filename, 113 std::string* error_msg); 114 ZipArchive(ZipArchiveHandle handle)115 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {} 116 117 ZipEntry* FindImpl(const char* name, bool allow_entry_not_found, std::string* error_msg) const; 118 119 friend class ZipEntry; 120 121 ZipArchiveHandle handle_; 122 123 DISALLOW_COPY_AND_ASSIGN(ZipArchive); 124 }; 125 126 } // namespace art 127 128 #endif // ART_LIBARTBASE_BASE_ZIP_ARCHIVE_H_ 129