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 /*
18  * Read-only access to Zip archives, with minimal heap allocation.
19  */
20 #ifndef LIBDEX_ZIPARCHIVE_H_
21 #define LIBDEX_ZIPARCHIVE_H_
22 
23 #include <ziparchive/zip_archive.h>
24 
25 #include "SysUtil.h"
26 #include "DexFile.h"            // need DEX_INLINE
27 
28 /*
29  * Open a Zip archive.
30  *
31  * On success, returns 0 and populates "pArchive".  Returns nonzero errno
32  * value on failure.
33  */
dexZipOpenArchive(const char * fileName,ZipArchiveHandle * pArchive)34 DEX_INLINE int dexZipOpenArchive(const char* fileName, ZipArchiveHandle* pArchive) {
35     return OpenArchive(fileName, pArchive);
36 }
37 
38 /*
39  * Like dexZipOpenArchive, but takes a file descriptor open for reading
40  * at the start of the file.  The descriptor must be mappable (this does
41  * not allow access to a stream).
42  *
43  * "debugFileName" will appear in error messages, but is not otherwise used.
44  */
dexZipOpenArchiveFd(int fd,const char * debugFileName,ZipArchiveHandle * pArchive)45 DEX_INLINE int dexZipOpenArchiveFd(int fd, const char* debugFileName,
46                                    ZipArchiveHandle* pArchive) {
47     return OpenArchiveFd(fd, debugFileName, pArchive);
48 }
49 
50 /*
51  * Close archive, releasing resources associated with it.
52  *
53  * Depending on the implementation this could unmap pages used by classes
54  * stored in a Jar.  This should only be done after unloading classes.
55  */
dexZipCloseArchive(ZipArchiveHandle archive)56 DEX_INLINE void dexZipCloseArchive(ZipArchiveHandle archive) {
57     CloseArchive(archive);
58 }
59 
60 /*
61  * Return the archive's file descriptor.
62  */
dexZipGetArchiveFd(const ZipArchiveHandle pArchive)63 DEX_INLINE int dexZipGetArchiveFd(const ZipArchiveHandle pArchive) {
64     return GetFileDescriptor(pArchive);
65 }
66 
67 /*
68  * Find an entry in the Zip archive, by name.  Returns NULL if the entry
69  * was not found.
70  */
dexZipFindEntry(const ZipArchiveHandle pArchive,const char * entryName,ZipEntry * data)71 DEX_INLINE int  dexZipFindEntry(const ZipArchiveHandle pArchive,
72     const char* entryName, ZipEntry* data) {
73     return FindEntry(pArchive, entryName, data);
74 }
75 
76 /*
77  * Uncompress and write an entry to a file descriptor.
78  *
79  * Returns 0 on success.
80  */
dexZipExtractEntryToFile(ZipArchiveHandle handle,ZipEntry * entry,int fd)81 DEX_INLINE int dexZipExtractEntryToFile(ZipArchiveHandle handle,
82     ZipEntry* entry, int fd) {
83     return ExtractEntryToFile(handle, entry, fd);
84 }
85 
86 #endif  // LIBDEX_ZIPARCHIVE_H_
87