Path: blob/master/src/java.base/share/native/libzip/zip_util.h
41149 views
/*1* Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Prototypes for zip file support27*/2829#ifndef _ZIP_H_30#define _ZIP_H_3132#include "jni.h"3334/*35* Header signatures36*/37#define PKZIP_SIGNATURE_AT(p, b2, b3) \38(((p)[0] == 'P') & ((p)[1] == 'K') & ((p)[2] == b2) & ((p)[3] == b3))39#define CENSIG_AT(p) PKZIP_SIGNATURE_AT(p, 1, 2)40#define LOCSIG_AT(p) PKZIP_SIGNATURE_AT(p, 3, 4)41#define ENDSIG_AT(p) PKZIP_SIGNATURE_AT(p, 5, 6)42#define EXTSIG_AT(p) PKZIP_SIGNATURE_AT(p, 7, 8)43#define ZIP64_ENDSIG_AT(p) PKZIP_SIGNATURE_AT(p, 6, 6)44#define ZIP64_LOCSIG_AT(p) PKZIP_SIGNATURE_AT(p, 6, 7)4546/*47* Header sizes including signatures48*/4950#define LOCHDR 3051#define EXTHDR 1652#define CENHDR 4653#define ENDHDR 225455#define ZIP64_ENDHDR 56 // ZIP64 end header size56#define ZIP64_LOCHDR 20 // ZIP64 end loc header size57#define ZIP64_EXTHDR 24 // EXT header size58#define ZIP64_EXTID 1 // Extra field Zip64 header ID5960#define ZIP64_MAGICVAL 0xffffffffLL61#define ZIP64_MAGICCOUNT 0xffff626364/*65* Header field access macros66*/67#define CH(b, n) (((unsigned char *)(b))[n])68#define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8))69#define LG(b, n) ((SH(b, n) | (SH(b, n+2) << 16)) &0xffffffffUL)70#define LL(b, n) (((jlong)LG(b, n)) | (((jlong)LG(b, n+4)) << 32))71#define GETSIG(b) LG(b, 0)7273/*74* Macros for getting local file (LOC) header fields75*/76#define LOCVER(b) SH(b, 4) /* version needed to extract */77#define LOCFLG(b) SH(b, 6) /* general purpose bit flags */78#define LOCHOW(b) SH(b, 8) /* compression method */79#define LOCTIM(b) LG(b, 10) /* modification time */80#define LOCCRC(b) LG(b, 14) /* crc of uncompressed data */81#define LOCSIZ(b) LG(b, 18) /* compressed data size */82#define LOCLEN(b) LG(b, 22) /* uncompressed data size */83#define LOCNAM(b) SH(b, 26) /* filename length */84#define LOCEXT(b) SH(b, 28) /* extra field length */8586/*87* Macros for getting extra local (EXT) header fields88*/89#define EXTCRC(b) LG(b, 4) /* crc of uncompressed data */90#define EXTSIZ(b) LG(b, 8) /* compressed size */91#define EXTLEN(b) LG(b, 12) /* uncompressed size */9293/*94* Macros for getting central directory header (CEN) fields95*/96#define CENVEM(b) SH(b, 4) /* version made by */97#define CENVER(b) SH(b, 6) /* version needed to extract */98#define CENFLG(b) SH(b, 8) /* general purpose bit flags */99#define CENHOW(b) SH(b, 10) /* compression method */100#define CENTIM(b) LG(b, 12) /* modification time */101#define CENCRC(b) LG(b, 16) /* crc of uncompressed data */102#define CENSIZ(b) LG(b, 20) /* compressed size */103#define CENLEN(b) LG(b, 24) /* uncompressed size */104#define CENNAM(b) SH(b, 28) /* length of filename */105#define CENEXT(b) SH(b, 30) /* length of extra field */106#define CENCOM(b) SH(b, 32) /* file comment length */107#define CENDSK(b) SH(b, 34) /* disk number start */108#define CENATT(b) SH(b, 36) /* internal file attributes */109#define CENATX(b) LG(b, 38) /* external file attributes */110#define CENOFF(b) LG(b, 42) /* offset of local header */111112/*113* Macros for getting end of central directory header (END) fields114*/115#define ENDSUB(b) SH(b, 8) /* number of entries on this disk */116#define ENDTOT(b) SH(b, 10) /* total number of entries */117#define ENDSIZ(b) LG(b, 12) /* central directory size */118#define ENDOFF(b) LG(b, 16) /* central directory offset */119#define ENDCOM(b) SH(b, 20) /* size of zip file comment */120121/*122* Macros for getting Zip64 end of central directory header fields123*/124#define ZIP64_ENDLEN(b) LL(b, 4) /* size of zip64 end of central dir */125#define ZIP64_ENDVEM(b) SH(b, 12) /* version made by */126#define ZIP64_ENDVER(b) SH(b, 14) /* version needed to extract */127#define ZIP64_ENDNMD(b) LG(b, 16) /* number of this disk */128#define ZIP64_ENDDSK(b) LG(b, 20) /* disk number of start */129#define ZIP64_ENDTOD(b) LL(b, 24) /* total number of entries on this disk */130#define ZIP64_ENDTOT(b) LL(b, 32) /* total number of entries */131#define ZIP64_ENDSIZ(b) LL(b, 40) /* central directory size in bytes */132#define ZIP64_ENDOFF(b) LL(b, 48) /* offset of first CEN header */133134/*135* Macros for getting Zip64 end of central directory locator fields136*/137#define ZIP64_LOCDSK(b) LG(b, 4) /* disk number start */138#define ZIP64_LOCOFF(b) LL(b, 8) /* offset of zip64 end */139#define ZIP64_LOCTOT(b) LG(b, 16) /* total number of disks */140141/*142* Supported compression methods143*/144#define STORED 0145#define DEFLATED 8146147/*148* Support for reading ZIP/JAR files. Some things worth noting:149*150* - Zip file entries larger than 2**32 bytes are not supported.151* - jzentry time and crc fields are signed even though they really152* represent unsigned quantities.153* - If csize is zero then the entry is uncompressed.154* - If extra != 0 then the first two bytes are the length of the extra155* data in intel byte order.156* - If pos <= 0 then it is the position of entry LOC header.157* If pos > 0 then it is the position of entry data.158* pos should not be accessed directly, but only by ZIP_GetEntryDataOffset.159* - entry name may include embedded null character, use nlen for length160*/161162typedef struct jzentry { /* Zip file entry */163char *name; /* entry name */164jlong time; /* modification time */165jlong size; /* size of uncompressed data */166jlong csize; /* size of compressed data (zero if uncompressed) */167jint crc; /* crc of uncompressed data */168char *comment; /* optional zip file comment */169jbyte *extra; /* optional extra data */170jlong pos; /* position of LOC header or entry data */171jint flag; /* general purpose flag */172jint nlen; /* length of the entry name */173} jzentry;174175/*176* In-memory hash table cell.177* In a typical system we have a *lot* of these, as we have one for178* every entry in every active JAR.179* Note that in order to save space we don't keep the name in memory,180* but merely remember a 32 bit hash.181*/182typedef struct jzcell {183unsigned int hash; /* 32 bit hashcode on name */184unsigned int next; /* hash chain: index into jzfile->entries */185jlong cenpos; /* Offset of central directory file header */186} jzcell;187188typedef struct cencache {189char *data; /* A cached page of CEN headers */190jlong pos; /* file offset of data */191} cencache;192193/*194* Use ZFILE to represent access to a file in a platform-indepenent195* fashion.196*/197#ifdef WIN32198#define ZFILE jlong199#else200#define ZFILE int201#endif202203/*204* Descriptor for a ZIP file.205*/206typedef struct jzfile { /* Zip file */207char *name; /* zip file name */208jint refs; /* number of active references */209jlong len; /* length (in bytes) of zip file */210#ifdef USE_MMAP211unsigned char *maddr; /* beginning address of the CEN & ENDHDR */212jlong mlen; /* length (in bytes) mmaped */213jlong offset; /* offset of the mmapped region from the214start of the file. */215jboolean usemmap; /* if mmap is used. */216#endif217jboolean locsig; /* if zip file starts with LOCSIG */218cencache cencache; /* CEN header cache */219ZFILE zfd; /* open file descriptor */220void *lock; /* read lock */221char *comment; /* zip file comment */222jint clen; /* length of the zip file comment */223char *msg; /* zip error message */224jzcell *entries; /* array of hash cells */225jint total; /* total number of entries */226jint *table; /* Hash chain heads: indexes into entries */227jint tablelen; /* number of hash heads */228struct jzfile *next; /* next zip file in search list */229jzentry *cache; /* we cache the most recently freed jzentry */230/* Information on metadata names in META-INF directory */231char **metanames; /* array of meta names (may have null names) */232jint metacurrent; /* the next empty slot in metanames array */233jint metacount; /* number of slots in metanames array */234jlong lastModified; /* last modified time */235jlong locpos; /* position of first LOC header (usually 0) */236} jzfile;237238/*239* Index representing end of hash chain240*/241#define ZIP_ENDCHAIN ((jint)-1)242243JNIEXPORT jzentry *244ZIP_FindEntry(jzfile *zip, char *name, jint *sizeP, jint *nameLenP);245246JNIEXPORT jboolean247ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entrynm);248249JNIEXPORT jzentry *250ZIP_GetNextEntry(jzfile *zip, jint n);251252JNIEXPORT jzfile *253ZIP_Open(const char *name, char **pmsg);254255jzfile *256ZIP_Open_Generic(const char *name, char **pmsg, int mode, jlong lastModified);257258jzfile *259ZIP_Get_From_Cache(const char *name, char **pmsg, jlong lastModified);260261jzfile *262ZIP_Put_In_Cache(const char *name, ZFILE zfd, char **pmsg, jlong lastModified);263264jzfile *265ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, jboolean usemmap);266267JNIEXPORT void268ZIP_Close(jzfile *zip);269270jzentry *271ZIP_GetEntry(jzfile *zip, char *name, jint ulen);272void273ZIP_Lock(jzfile *zip);274void275ZIP_Unlock(jzfile *zip);276jint277ZIP_Read(jzfile *zip, jzentry *entry, jlong pos, void *buf, jint len);278void279ZIP_FreeEntry(jzfile *zip, jzentry *ze);280jlong ZIP_GetEntryDataOffset(jzfile *zip, jzentry *entry);281jzentry * ZIP_GetEntry2(jzfile *zip, char *name, jint ulen, jboolean addSlash);282283JNIEXPORT jboolean284ZIP_InflateFully(void *inBuf, jlong inLen, void *outBuf, jlong outLen, char **pmsg);285286#endif /* !_ZIP_H_ */287288289