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   MemMap* ExtractToMemMap(const char* zip_filename, const char* entry_filename,
41                           std::string* error_msg);
42   virtual ~ZipEntry();
43 
44   uint32_t GetUncompressedLength();
45   uint32_t GetCrc32();
46 
47  private:
ZipEntry(ZipArchiveHandle handle,::ZipEntry * zip_entry)48   ZipEntry(ZipArchiveHandle handle,
49            ::ZipEntry* zip_entry) : handle_(handle), zip_entry_(zip_entry) {}
50 
51   ZipArchiveHandle handle_;
52   ::ZipEntry* const zip_entry_;
53 
54   friend class ZipArchive;
55   DISALLOW_COPY_AND_ASSIGN(ZipEntry);
56 };
57 
58 class ZipArchive {
59  public:
60   // return new ZipArchive instance on success, null on error.
61   static ZipArchive* Open(const char* filename, std::string* error_msg);
62   static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
63 
64   ZipEntry* Find(const char* name, std::string* error_msg) const;
65 
~ZipArchive()66   ~ZipArchive() {
67     CloseArchive(handle_);
68   }
69 
70  private:
ZipArchive(ZipArchiveHandle handle)71   explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
72 
73   friend class ZipEntry;
74 
75   ZipArchiveHandle handle_;
76 
77   DISALLOW_COPY_AND_ASSIGN(ZipArchive);
78 };
79 
80 }  // namespace art
81 
82 #endif  // ART_RUNTIME_ZIP_ARCHIVE_H_
83