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 <ziparchive/zip_archive.h> 22 #include <memory> 23 #include <string> 24 25 #include "base/logging.h" 26 #include "base/unix_file/random_access_file.h" 27 #include "globals.h" 28 #include "mem_map.h" 29 #include "os.h" 30 #include "safe_map.h" 31 32 namespace art { 33 34 class ZipArchive; 35 class MemMap; 36 37 class ZipEntry { 38 public: 39 bool ExtractToFile(File& file, std::string* error_msg); 40 // Extract this entry to anonymous memory (R/W). 41 // Returns null on failure and sets error_msg. 42 MemMap* ExtractToMemMap(const char* zip_filename, const char* entry_filename, 43 std::string* error_msg); 44 // Create a file-backed private (clean, R/W) memory mapping to this entry. 45 // 'zip_filename' is used for diagnostics only, 46 // the original file that the ZipArchive was open with is used 47 // for the mapping. 48 // 49 // Will only succeed if the entry is stored uncompressed. 50 // Returns null on failure and sets error_msg. 51 MemMap* MapDirectlyFromFile(const char* zip_filename, /*out*/std::string* error_msg); 52 virtual ~ZipEntry(); 53 54 uint32_t GetUncompressedLength(); 55 uint32_t GetCrc32(); 56 57 bool IsUncompressed(); 58 bool IsAlignedTo(size_t alignment); 59 60 private: ZipEntry(ZipArchiveHandle handle,::ZipEntry * zip_entry,const std::string & entry_name)61 ZipEntry(ZipArchiveHandle handle, 62 ::ZipEntry* zip_entry, 63 const std::string& entry_name) 64 : handle_(handle), zip_entry_(zip_entry), entry_name_(entry_name) {} 65 66 ZipArchiveHandle handle_; 67 ::ZipEntry* const zip_entry_; 68 std::string const entry_name_; 69 70 friend class ZipArchive; 71 DISALLOW_COPY_AND_ASSIGN(ZipEntry); 72 }; 73 74 class ZipArchive { 75 public: 76 // return new ZipArchive instance on success, null on error. 77 static ZipArchive* Open(const char* filename, std::string* error_msg); 78 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg); 79 80 ZipEntry* Find(const char* name, std::string* error_msg) const; 81 82 ~ZipArchive(); 83 84 private: ZipArchive(ZipArchiveHandle handle)85 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {} 86 87 friend class ZipEntry; 88 89 ZipArchiveHandle handle_; 90 91 DISALLOW_COPY_AND_ASSIGN(ZipArchive); 92 }; 93 94 } // namespace art 95 96 #endif // ART_RUNTIME_ZIP_ARCHIVE_H_ 97