blob: 0fa21ccb94b085864b1f1cdea2a9d6803161a7b6 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * Copyright (C) 2013 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 LIBZIPARCHIVE_ZIPARCHIVE_H_
21#define LIBZIPARCHIVE_ZIPARCHIVE_H_
22
23#include <stdint.h>
24#include <sys/types.h>
25#include <utils/Compat.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30/* Zip compression methods we support */
31enum {
32 kCompressStored = 0, // no compression
33 kCompressDeflated = 8, // standard deflate
34};
35
36struct ZipEntryName {
37 const char* name;
38 uint16_t name_length;
39};
40
41/*
42 * Represents information about a zip entry in a zip file.
43 */
44struct ZipEntry {
45 // Compression method: One of kCompressStored or
46 // kCompressDeflated.
47 uint16_t method;
48
49 // Modification time. The zipfile format specifies
50 // that the first two little endian bytes contain the time
51 // and the last two little endian bytes contain the date.
52 uint32_t mod_time;
53
54 // 1 if this entry contains a data descriptor segment, 0
55 // otherwise.
56 uint8_t has_data_descriptor;
57
58 // Crc32 value of this ZipEntry. This information might
59 // either be stored in the local file header or in a special
60 // Data descriptor footer at the end of the file entry.
61 uint32_t crc32;
62
63 // Compressed length of this ZipEntry. Might be present
64 // either in the local file header or in the data descriptor
65 // footer.
66 uint32_t compressed_length;
67
68 // Uncompressed length of this ZipEntry. Might be present
69 // either in the local file header or in the data descriptor
70 // footer.
71 uint32_t uncompressed_length;
72
73 // The offset to the start of data for this ZipEntry.
74 off64_t offset;
75};
76
77typedef void* ZipArchiveHandle;
78
79/*
80 * Open a Zip archive, and sets handle to the value of the opaque
81 * handle for the file. This handle must be released by calling
82 * CloseArchive with this handle.
83 *
84 * Returns 0 on success, and negative values on failure.
85 */
86int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle);
87
88/*
89 * Like OpenArchive, but takes a file descriptor open for reading
90 * at the start of the file. The descriptor must be mappable (this does
91 * not allow access to a stream).
92 *
93 * Sets handle to the value of the opaque handle for this file descriptor.
94 * This handle must be released by calling CloseArchive with this handle.
95 *
96 * This function maps and scans the central directory and builds a table
97 * of entries for future lookups.
98 *
99 * "debugFileName" will appear in error messages, but is not otherwise used.
100 *
101 * Returns 0 on success, and negative values on failure.
102 */
103int32_t OpenArchiveFd(const int fd, const char* debugFileName,
104 ZipArchiveHandle *handle);
105
106/*
107 * Close archive, releasing resources associated with it. This will
108 * unmap the central directory of the zipfile and free all internal
109 * data structures associated with the file. It is an error to use
110 * this handle for any further operations without an intervening
111 * call to one of the OpenArchive variants.
112 */
113void CloseArchive(ZipArchiveHandle handle);
114
115/*
116 * Find an entry in the Zip archive, by name. |entryName| must be a null
117 * terminated string, and |data| must point to a writeable memory location.
118 *
119 * Returns 0 if an entry is found, and populates |data| with information
120 * about this entry. Returns negative values otherwise.
121 *
122 * It's important to note that |data->crc32|, |data->compLen| and
123 * |data->uncompLen| might be set to values from the central directory
124 * if this file entry contains a data descriptor footer. To verify crc32s
125 * and length, a call to VerifyCrcAndLengths must be made after entry data
126 * has been processed.
127 */
128int32_t FindEntry(const ZipArchiveHandle handle, const char* entryName,
129 ZipEntry* data);
130
131/*
132 * Start iterating over all entries of a zip file. The order of iteration
133 * is not guaranteed to be the same as the order of elements
134 * in the central directory but is stable for a given zip file. |cookie|
135 * must point to a writeable memory location, and will be set to the value
136 * of an opaque cookie which can be used to make one or more calls to
137 * Next.
138 *
139 * This method also accepts an optional prefix to restrict iteration to
140 * entry names that start with |prefix|.
141 *
142 * Returns 0 on success and negative values on failure.
143 */
144int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
145 const char* prefix);
146
147/*
148 * Advance to the next element in the zipfile in iteration order.
149 *
150 * Returns 0 on success, -1 if there are no more elements in this
151 * archive and lower negative values on failure.
152 */
153int32_t Next(void* cookie, ZipEntry* data, ZipEntryName *name);
154
155/*
156 * Uncompress and write an entry to an open file identified by |fd|.
157 * |entry->uncompressed_length| bytes will be written to the file at
158 * its current offset, and the file will be truncated at the end of
159 * the uncompressed data.
160 *
161 * Returns 0 on success and negative values on failure.
162 */
163int32_t ExtractEntryToFile(ZipArchiveHandle handle, ZipEntry* entry, int fd);
164
165/**
166 * Uncompress a given zip entry to the memory region at |begin| and of
167 * size |size|. This size is expected to be the same as the *declared*
168 * uncompressed length of the zip entry. It is an error if the *actual*
169 * number of uncompressed bytes differs from this number.
170 *
171 * Returns 0 on success and negative values on failure.
172 */
173int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry,
174 uint8_t* begin, uint32_t size);
175
176int GetFileDescriptor(const ZipArchiveHandle handle);
177
178const char* ErrorCodeString(int32_t error_code);
179
180#ifdef __cplusplus
181}
182#endif
183#endif // LIBZIPARCHIVE_ZIPARCHIVE_H_