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_RUNTIME_ZIP_ARCHIVE_H_ 18 #define ART_RUNTIME_ZIP_ARCHIVE_H_ 19 20 #include <stdint.h> 21 #include <memory> 22 #include <string> 23 24 #include "base/logging.h" 25 #include "base/unix_file/random_access_file.h" 26 #include "globals.h" 27 #include "mem_map.h" 28 #include "os.h" 29 #include "safe_map.h" 30 31 // system/core/zip_archive definitions. 32 struct ZipEntry; 33 typedef void* ZipArchiveHandle; 34 35 namespace art { 36 37 class ZipArchive; 38 class MemMap; 39 40 class ZipEntry { 41 public: 42 bool ExtractToFile(File& file, std::string* error_msg); 43 // Extract this entry to anonymous memory (R/W). 44 // Returns null on failure and sets error_msg. 45 MemMap* ExtractToMemMap(const char* zip_filename, const char* entry_filename, 46 std::string* error_msg); 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); 55 virtual ~ZipEntry(); 56 57 uint32_t GetUncompressedLength(); 58 uint32_t GetCrc32(); 59 60 bool IsUncompressed(); 61 bool IsAlignedTo(size_t alignment); 62 63 private: ZipEntry(ZipArchiveHandle handle,::ZipEntry * zip_entry,const std::string & entry_name)64 ZipEntry(ZipArchiveHandle handle, 65 ::ZipEntry* zip_entry, 66 const std::string& entry_name) 67 : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {} 68 69 ZipArchiveHandle handle_; 70 ::ZipEntry* const zip_entry_; 71 std::string const entry_name_; 72 73 friend class ZipArchive; 74 DISALLOW_COPY_AND_ASSIGN(ZipEntry); 75 }; 76 77 class ZipArchive { 78 public: 79 // return new ZipArchive instance on success, null on error. 80 static ZipArchive* Open(const char* filename, std::string* error_msg); 81 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg); 82 83 ZipEntry* Find(const char* name, std::string* error_msg) const; 84 85 ~ZipArchive(); 86 87 private: ZipArchive(ZipArchiveHandle handle)88 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {} 89 90 friend class ZipEntry; 91 92 ZipArchiveHandle handle_; 93 94 DISALLOW_COPY_AND_ASSIGN(ZipArchive); 95 }; 96 97 } // namespace art 98 99 #endif // ART_RUNTIME_ZIP_ARCHIVE_H_ 100