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