The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | */ |
Andy McFadden | 80a4e24 | 2010-04-23 16:34:52 -0700 | [diff] [blame] | 16 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Read-only access to Zip archives, with minimal heap allocation. |
| 19 | */ |
Carl Shapiro | 375fb11 | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 20 | #ifndef LIBDEX_ZIPARCHIVE_H_ |
| 21 | #define LIBDEX_ZIPARCHIVE_H_ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | |
Narayan Kamath | 39e8b7e | 2013-12-17 10:03:19 +0000 | [diff] [blame] | 23 | #include <ziparchive/zip_archive.h> |
| 24 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | #include "SysUtil.h" |
| 26 | #include "DexFile.h" // need DEX_INLINE |
| 27 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | * Open a Zip archive. |
| 30 | * |
| 31 | * On success, returns 0 and populates "pArchive". Returns nonzero errno |
| 32 | * value on failure. |
| 33 | */ |
Narayan Kamath | 39e8b7e | 2013-12-17 10:03:19 +0000 | [diff] [blame] | 34 | DEX_INLINE int dexZipOpenArchive(const char* fileName, ZipArchiveHandle* pArchive) { |
| 35 | return OpenArchive(fileName, pArchive); |
| 36 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | */ |
Narayan Kamath | 39e8b7e | 2013-12-17 10:03:19 +0000 | [diff] [blame] | 45 | DEX_INLINE int dexZipOpenArchiveFd(int fd, const char* debugFileName, |
| 46 | ZipArchiveHandle* pArchive) { |
| 47 | return OpenArchiveFd(fd, debugFileName, pArchive); |
| 48 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | */ |
Narayan Kamath | 39e8b7e | 2013-12-17 10:03:19 +0000 | [diff] [blame] | 56 | DEX_INLINE void dexZipCloseArchive(ZipArchiveHandle archive) { |
| 57 | CloseArchive(archive); |
| 58 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Return the archive's file descriptor. |
| 62 | */ |
Narayan Kamath | 39e8b7e | 2013-12-17 10:03:19 +0000 | [diff] [blame] | 63 | DEX_INLINE int dexZipGetArchiveFd(const ZipArchiveHandle pArchive) { |
| 64 | return GetFileDescriptor(pArchive); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Find an entry in the Zip archive, by name. Returns NULL if the entry |
| 69 | * was not found. |
| 70 | */ |
Narayan Kamath | 39e8b7e | 2013-12-17 10:03:19 +0000 | [diff] [blame] | 71 | DEX_INLINE int dexZipFindEntry(const ZipArchiveHandle pArchive, |
| 72 | const char* entryName, ZipEntry* data) { |
Piotr Jastrzebski | 2e2f03c | 2014-08-13 10:00:59 +0100 | [diff] [blame] | 73 | return FindEntry(pArchive, ZipEntryName(entryName), data); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Uncompress and write an entry to a file descriptor. |
Andy McFadden | 80a4e24 | 2010-04-23 16:34:52 -0700 | [diff] [blame] | 78 | * |
| 79 | * Returns 0 on success. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 80 | */ |
Narayan Kamath | 39e8b7e | 2013-12-17 10:03:19 +0000 | [diff] [blame] | 81 | DEX_INLINE int dexZipExtractEntryToFile(ZipArchiveHandle handle, |
| 82 | ZipEntry* entry, int fd) { |
| 83 | return ExtractEntryToFile(handle, entry, fd); |
| 84 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 85 | |
Carl Shapiro | 375fb11 | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 86 | #endif // LIBDEX_ZIPARCHIVE_H_ |