- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathzip_util.h
287 lines (251 loc) · 10.7 KB
/
zip_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Prototypes for zip file support
*/
#ifndef_ZIP_H_
#define_ZIP_H_
#include"jni.h"
/*
* Header signatures
*/
#definePKZIP_SIGNATURE_AT(p, b2, b3) \
(((p)[0] == 'P') & ((p)[1] == 'K') & ((p)[2] == b2) & ((p)[3] == b3))
#defineCENSIG_AT(p) PKZIP_SIGNATURE_AT(p, 1, 2)
#defineLOCSIG_AT(p) PKZIP_SIGNATURE_AT(p, 3, 4)
#defineENDSIG_AT(p) PKZIP_SIGNATURE_AT(p, 5, 6)
#defineEXTSIG_AT(p) PKZIP_SIGNATURE_AT(p, 7, 8)
#defineZIP64_ENDSIG_AT(p) PKZIP_SIGNATURE_AT(p, 6, 6)
#defineZIP64_LOCSIG_AT(p) PKZIP_SIGNATURE_AT(p, 6, 7)
/*
* Header sizes including signatures
*/
#defineLOCHDR 30
#defineEXTHDR 16
#defineCENHDR 46
#defineENDHDR 22
#defineZIP64_ENDHDR 56 // ZIP64 end header size
#defineZIP64_LOCHDR 20 // ZIP64 end loc header size
#defineZIP64_EXTHDR 24 // EXT header size
#defineZIP64_EXTID 1 // Extra field Zip64 header ID
#defineZIP64_MAGICVAL 0xffffffffLL
#defineZIP64_MAGICCOUNT 0xffff
/*
* Header field access macros
*/
#defineCH(b, n) (((unsigned char *)(b))[n])
#defineSH(b, n) (CH(b, n) | (CH(b, n+1) << 8))
#defineLG(b, n) ((SH(b, n) | (SH(b, n+2) << 16)) &0xffffffffUL)
#defineLL(b, n) (((jlong)LG(b, n)) | (((jlong)LG(b, n+4)) << 32))
#defineGETSIG(b) LG(b, 0)
/*
* Macros for getting local file (LOC) header fields
*/
#defineLOCVER(b) SH(b, 4) /* version needed to extract */
#defineLOCFLG(b) SH(b, 6) /* general purpose bit flags */
#defineLOCHOW(b) SH(b, 8) /* compression method */
#defineLOCTIM(b) LG(b, 10) /* modification time */
#defineLOCCRC(b) LG(b, 14) /* crc of uncompressed data */
#defineLOCSIZ(b) LG(b, 18) /* compressed data size */
#defineLOCLEN(b) LG(b, 22) /* uncompressed data size */
#defineLOCNAM(b) SH(b, 26) /* filename length */
#defineLOCEXT(b) SH(b, 28) /* extra field length */
/*
* Macros for getting extra local (EXT) header fields
*/
#defineEXTCRC(b) LG(b, 4) /* crc of uncompressed data */
#defineEXTSIZ(b) LG(b, 8) /* compressed size */
#defineEXTLEN(b) LG(b, 12) /* uncompressed size */
/*
* Macros for getting central directory header (CEN) fields
*/
#defineCENVEM(b) SH(b, 4) /* version made by */
#defineCENVER(b) SH(b, 6) /* version needed to extract */
#defineCENFLG(b) SH(b, 8) /* general purpose bit flags */
#defineCENHOW(b) SH(b, 10) /* compression method */
#defineCENTIM(b) LG(b, 12) /* modification time */
#defineCENCRC(b) LG(b, 16) /* crc of uncompressed data */
#defineCENSIZ(b) LG(b, 20) /* compressed size */
#defineCENLEN(b) LG(b, 24) /* uncompressed size */
#defineCENNAM(b) SH(b, 28) /* length of filename */
#defineCENEXT(b) SH(b, 30) /* length of extra field */
#defineCENCOM(b) SH(b, 32) /* file comment length */
#defineCENDSK(b) SH(b, 34) /* disk number start */
#defineCENATT(b) SH(b, 36) /* internal file attributes */
#defineCENATX(b) LG(b, 38) /* external file attributes */
#defineCENOFF(b) LG(b, 42) /* offset of local header */
/*
* Macros for getting end of central directory header (END) fields
*/
#defineENDSUB(b) SH(b, 8) /* number of entries on this disk */
#defineENDTOT(b) SH(b, 10) /* total number of entries */
#defineENDSIZ(b) LG(b, 12) /* central directory size */
#defineENDOFF(b) LG(b, 16) /* central directory offset */
#defineENDCOM(b) SH(b, 20) /* size of zip file comment */
/*
* Macros for getting Zip64 end of central directory header fields
*/
#defineZIP64_ENDLEN(b) LL(b, 4) /* size of zip64 end of central dir */
#defineZIP64_ENDVEM(b) SH(b, 12) /* version made by */
#defineZIP64_ENDVER(b) SH(b, 14) /* version needed to extract */
#defineZIP64_ENDNMD(b) LG(b, 16) /* number of this disk */
#defineZIP64_ENDDSK(b) LG(b, 20) /* disk number of start */
#defineZIP64_ENDTOD(b) LL(b, 24) /* total number of entries on this disk */
#defineZIP64_ENDTOT(b) LL(b, 32) /* total number of entries */
#defineZIP64_ENDSIZ(b) LL(b, 40) /* central directory size in bytes */
#defineZIP64_ENDOFF(b) LL(b, 48) /* offset of first CEN header */
/*
* Macros for getting Zip64 end of central directory locator fields
*/
#defineZIP64_LOCDSK(b) LG(b, 4) /* disk number start */
#defineZIP64_LOCOFF(b) LL(b, 8) /* offset of zip64 end */
#defineZIP64_LOCTOT(b) LG(b, 16) /* total number of disks */
/*
* Supported compression methods
*/
#defineSTORED 0
#defineDEFLATED 8
/*
* Support for reading ZIP/JAR files. Some things worth noting:
*
* - Zip file entries larger than 2**32 bytes are not supported.
* - jzentry time and crc fields are signed even though they really
* represent unsigned quantities.
* - If csize is zero then the entry is uncompressed.
* - If extra != 0 then the first two bytes are the length of the extra
* data in intel byte order.
* - If pos <= 0 then it is the position of entry LOC header.
* If pos > 0 then it is the position of entry data.
* pos should not be accessed directly, but only by ZIP_GetEntryDataOffset.
* - entry name may include embedded null character, use nlen for length
*/
typedefstructjzentry { /* Zip file entry */
char*name; /* entry name */
jlongtime; /* modification time */
jlongsize; /* size of uncompressed data */
jlongcsize; /* size of compressed data (zero if uncompressed) */
jintcrc; /* crc of uncompressed data */
char*comment; /* optional zip file comment */
jbyte*extra; /* optional extra data */
jlongpos; /* position of LOC header or entry data */
jintflag; /* general purpose flag */
jintnlen; /* length of the entry name */
} jzentry;
/*
* In-memory hash table cell.
* In a typical system we have a *lot* of these, as we have one for
* every entry in every active JAR.
* Note that in order to save space we don't keep the name in memory,
* but merely remember a 32 bit hash.
*/
typedefstructjzcell {
unsigned inthash; /* 32 bit hashcode on name */
unsigned intnext; /* hash chain: index into jzfile->entries */
jlongcenpos; /* Offset of central directory file header */
} jzcell;
typedefstructcencache {
char*data; /* A cached page of CEN headers */
jlongpos; /* file offset of data */
} cencache;
/*
* Use ZFILE to represent access to a file in a platform-indepenent
* fashion.
*/
#ifdefWIN32
#defineZFILE jlong
#else
#defineZFILE int
#endif
/*
* Descriptor for a ZIP file.
*/
typedefstructjzfile { /* Zip file */
char*name; /* zip file name */
jintrefs; /* number of active references */
jlonglen; /* length (in bytes) of zip file */
#ifdefUSE_MMAP
unsigned char*maddr; /* beginning address of the CEN & ENDHDR */
jlongmlen; /* length (in bytes) mmapped */
jlongoffset; /* offset of the mmapped region from the
start of the file. */
jbooleanusemmap; /* if mmap is used. */
#endif
jbooleanlocsig; /* if zip file starts with LOCSIG */
cencachecencache; /* CEN header cache */
ZFILEzfd; /* open file descriptor */
void*lock; /* read lock */
char*comment; /* zip file comment */
jintclen; /* length of the zip file comment */
char*msg; /* zip error message */
jzcell*entries; /* array of hash cells */
jinttotal; /* total number of entries */
jint*table; /* Hash chain heads: indexes into entries */
jinttablelen; /* number of hash heads */
structjzfile*next; /* next zip file in search list */
jzentry*cache; /* we cache the most recently freed jzentry */
/* Information on metadata names in META-INF directory */
char**metanames; /* array of meta names (may have null names) */
jintmetacurrent; /* the next empty slot in metanames array */
jintmetacount; /* number of slots in metanames array */
jlonglastModified; /* last modified time */
jlonglocpos; /* position of first LOC header (usually 0) */
} jzfile;
/*
* Index representing end of hash chain
*/
#defineZIP_ENDCHAIN ((jint)-1)
JNIEXPORTjzentry*
ZIP_FindEntry(jzfile*zip, char*name, jint*sizeP, jint*nameLenP);
JNIEXPORTjboolean
ZIP_ReadEntry(jzfile*zip, jzentry*entry, unsigned char*buf, char*entrynm);
JNIEXPORTjzentry*
ZIP_GetNextEntry(jzfile*zip, jintn);
JNIEXPORTjzfile*
ZIP_Open(constchar*name, char**pmsg);
jzfile*
ZIP_Open_Generic(constchar*name, char**pmsg, intmode, jlonglastModified);
jzfile*
ZIP_Get_From_Cache(constchar*name, char**pmsg, jlonglastModified);
jzfile*
ZIP_Put_In_Cache(constchar*name, ZFILEzfd, char**pmsg, jlonglastModified);
jzfile*
ZIP_Put_In_Cache0(constchar*name, ZFILEzfd, char**pmsg, jlonglastModified, jbooleanusemmap);
JNIEXPORTvoid
ZIP_Close(jzfile*zip);
jzentry*
ZIP_GetEntry(jzfile*zip, char*name, jintulen);
void
ZIP_Lock(jzfile*zip);
void
ZIP_Unlock(jzfile*zip);
jint
ZIP_Read(jzfile*zip, jzentry*entry, jlongpos, void*buf, jintlen);
JNIEXPORTvoid
ZIP_FreeEntry(jzfile*zip, jzentry*ze);
jlongZIP_GetEntryDataOffset(jzfile*zip, jzentry*entry);
jzentry*ZIP_GetEntry2(jzfile*zip, char*name, jintulen, jbooleanaddSlash);
JNIEXPORTjboolean
ZIP_InflateFully(void*inBuf, jlonginLen, void*outBuf, jlongoutLen, char**pmsg);
#endif/* !_ZIP_H_ */