Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/java/util/zip/zip_util.h
38830 views
/*1* Copyright (c) 1995, 2013, 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/*33* Header signatures34*/35#define LOCSIG 0x04034b50L /* "PK\003\004" */36#define EXTSIG 0x08074b50L /* "PK\007\008" */37#define CENSIG 0x02014b50L /* "PK\001\002" */38#define ENDSIG 0x06054b50L /* "PK\005\006" */3940#define ZIP64_ENDSIG 0x06064b50L /* "PK\006\006" */41#define ZIP64_LOCSIG 0x07064b50L /* "PK\006\007" */4243/*44* Header sizes including signatures45*/4647#define LOCHDR 3048#define EXTHDR 1649#define CENHDR 4650#define ENDHDR 225152#define ZIP64_ENDHDR 56 // ZIP64 end header size53#define ZIP64_LOCHDR 20 // ZIP64 end loc header size54#define ZIP64_EXTHDR 24 // EXT header size55#define ZIP64_EXTID 1 // Extra field Zip64 header ID5657#define ZIP64_MAGICVAL 0xffffffffLL58#define ZIP64_MAGICCOUNT 0xffff596061/*62* Header field access macros63*/64#define CH(b, n) (((unsigned char *)(b))[n])65#define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8))66#define LG(b, n) ((SH(b, n) | (SH(b, n+2) << 16)) &0xffffffffUL)67#define LL(b, n) (((jlong)LG(b, n)) | (((jlong)LG(b, n+4)) << 32))68#define GETSIG(b) LG(b, 0)6970/*71* Macros for getting local file (LOC) header fields72*/73#define LOCVER(b) SH(b, 4) /* version needed to extract */74#define LOCFLG(b) SH(b, 6) /* general purpose bit flags */75#define LOCHOW(b) SH(b, 8) /* compression method */76#define LOCTIM(b) LG(b, 10) /* modification time */77#define LOCCRC(b) LG(b, 14) /* crc of uncompressed data */78#define LOCSIZ(b) LG(b, 18) /* compressed data size */79#define LOCLEN(b) LG(b, 22) /* uncompressed data size */80#define LOCNAM(b) SH(b, 26) /* filename length */81#define LOCEXT(b) SH(b, 28) /* extra field length */8283/*84* Macros for getting extra local (EXT) header fields85*/86#define EXTCRC(b) LG(b, 4) /* crc of uncompressed data */87#define EXTSIZ(b) LG(b, 8) /* compressed size */88#define EXTLEN(b) LG(b, 12) /* uncompressed size */8990/*91* Macros for getting central directory header (CEN) fields92*/93#define CENVEM(b) SH(b, 4) /* version made by */94#define CENVER(b) SH(b, 6) /* version needed to extract */95#define CENFLG(b) SH(b, 8) /* general purpose bit flags */96#define CENHOW(b) SH(b, 10) /* compression method */97#define CENTIM(b) LG(b, 12) /* modification time */98#define CENCRC(b) LG(b, 16) /* crc of uncompressed data */99#define CENSIZ(b) LG(b, 20) /* compressed size */100#define CENLEN(b) LG(b, 24) /* uncompressed size */101#define CENNAM(b) SH(b, 28) /* length of filename */102#define CENEXT(b) SH(b, 30) /* length of extra field */103#define CENCOM(b) SH(b, 32) /* file comment length */104#define CENDSK(b) SH(b, 34) /* disk number start */105#define CENATT(b) SH(b, 36) /* internal file attributes */106#define CENATX(b) LG(b, 38) /* external file attributes */107#define CENOFF(b) LG(b, 42) /* offset of local header */108109/*110* Macros for getting end of central directory header (END) fields111*/112#define ENDSUB(b) SH(b, 8) /* number of entries on this disk */113#define ENDTOT(b) SH(b, 10) /* total number of entries */114#define ENDSIZ(b) LG(b, 12) /* central directory size */115#define ENDOFF(b) LG(b, 16) /* central directory offset */116#define ENDCOM(b) SH(b, 20) /* size of zip file comment */117118/*119* Macros for getting Zip64 end of central directory header fields120*/121#define ZIP64_ENDLEN(b) LL(b, 4) /* size of zip64 end of central dir */122#define ZIP64_ENDVEM(b) SH(b, 12) /* version made by */123#define ZIP64_ENDVER(b) SH(b, 14) /* version needed to extract */124#define ZIP64_ENDNMD(b) LG(b, 16) /* number of this disk */125#define ZIP64_ENDDSK(b) LG(b, 20) /* disk number of start */126#define ZIP64_ENDTOD(b) LL(b, 24) /* total number of entries on this disk */127#define ZIP64_ENDTOT(b) LL(b, 32) /* total number of entries */128#define ZIP64_ENDSIZ(b) LL(b, 40) /* central directory size in bytes */129#define ZIP64_ENDOFF(b) LL(b, 48) /* offset of first CEN header */130131/*132* Macros for getting Zip64 end of central directory locator fields133*/134#define ZIP64_LOCDSK(b) LG(b, 4) /* disk number start */135#define ZIP64_LOCOFF(b) LL(b, 8) /* offset of zip64 end */136#define ZIP64_LOCTOT(b) LG(b, 16) /* total number of disks */137138/*139* Supported compression methods140*/141#define STORED 0142#define DEFLATED 8143144/*145* Support for reading ZIP/JAR files. Some things worth noting:146*147* - Zip file entries larger than 2**32 bytes are not supported.148* - jzentry time and crc fields are signed even though they really149* represent unsigned quantities.150* - If csize is zero then the entry is uncompressed.151* - If extra != 0 then the first two bytes are the length of the extra152* data in intel byte order.153* - If pos <= 0 then it is the position of entry LOC header.154* If pos > 0 then it is the position of entry data.155* pos should not be accessed directly, but only by ZIP_GetEntryDataOffset.156* - entry name may include embedded null character, use nlen for length157*/158159typedef struct jzentry { /* Zip file entry */160char *name; /* entry name */161jlong time; /* modification time */162jlong size; /* size of uncompressed data */163jlong csize; /* size of compressed data (zero if uncompressed) */164jint crc; /* crc of uncompressed data */165char *comment; /* optional zip file comment */166jbyte *extra; /* optional extra data */167jlong pos; /* position of LOC header or entry data */168jint flag; /* general purpose flag */169jint nlen; /* length of the entry name */170} jzentry;171172/*173* In-memory hash table cell.174* In a typical system we have a *lot* of these, as we have one for175* every entry in every active JAR.176* Note that in order to save space we don't keep the name in memory,177* but merely remember a 32 bit hash.178*/179typedef struct jzcell {180unsigned int hash; /* 32 bit hashcode on name */181unsigned int next; /* hash chain: index into jzfile->entries */182jlong cenpos; /* Offset of central directory file header */183} jzcell;184185typedef struct cencache {186char *data; /* A cached page of CEN headers */187jlong pos; /* file offset of data */188} cencache;189190/*191* Use ZFILE to represent access to a file in a platform-indepenent192* fashion.193*/194#ifdef WIN32195#define ZFILE jlong196#else197#define ZFILE int198#endif199200/*201* Descriptor for a ZIP file.202*/203typedef struct jzfile { /* Zip file */204char *name; /* zip file name */205jint refs; /* number of active references */206jlong len; /* length (in bytes) of zip file */207#ifdef USE_MMAP208unsigned char *maddr; /* beginning address of the CEN & ENDHDR */209jlong mlen; /* length (in bytes) mmaped */210jlong offset; /* offset of the mmapped region from the211start of the file. */212jboolean usemmap; /* if mmap is used. */213#endif214jboolean locsig; /* if zip file starts with LOCSIG */215cencache cencache; /* CEN header cache */216ZFILE zfd; /* open file descriptor */217void *lock; /* read lock */218char *comment; /* zip file comment */219jint clen; /* length of the zip file comment */220char *msg; /* zip error message */221jzcell *entries; /* array of hash cells */222jint total; /* total number of entries */223jint *table; /* Hash chain heads: indexes into entries */224jint tablelen; /* number of hash heads */225struct jzfile *next; /* next zip file in search list */226jzentry *cache; /* we cache the most recently freed jzentry */227/* Information on metadata names in META-INF directory */228char **metanames; /* array of meta names (may have null names) */229jint metacurrent; /* the next empty slot in metanames array */230jint metacount; /* number of slots in metanames array */231jlong lastModified; /* last modified time */232jlong locpos; /* position of first LOC header (usually 0) */233} jzfile;234235/*236* Index representing end of hash chain237*/238#define ZIP_ENDCHAIN ((jint)-1)239240jzentry * JNICALL241ZIP_FindEntry(jzfile *zip, char *name, jint *sizeP, jint *nameLenP);242243jboolean JNICALL244ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entrynm);245246jzentry * JNICALL247ZIP_GetNextEntry(jzfile *zip, jint n);248249jzfile * JNICALL250ZIP_Open(const char *name, char **pmsg);251252jzfile *253ZIP_Open_Generic(const char *name, char **pmsg, int mode, jlong lastModified);254255jzfile *256ZIP_Get_From_Cache(const char *name, char **pmsg, jlong lastModified);257258jzfile *259ZIP_Put_In_Cache(const char *name, ZFILE zfd, char **pmsg, jlong lastModified);260261jzfile *262ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, jboolean usemmap);263264void JNICALL265ZIP_Close(jzfile *zip);266267jzentry * ZIP_GetEntry(jzfile *zip, char *name, jint ulen);268void ZIP_Lock(jzfile *zip);269void ZIP_Unlock(jzfile *zip);270jint ZIP_Read(jzfile *zip, jzentry *entry, jlong pos, void *buf, jint len);271void ZIP_FreeEntry(jzfile *zip, jzentry *ze);272jlong ZIP_GetEntryDataOffset(jzfile *zip, jzentry *entry);273jzentry * ZIP_GetEntry2(jzfile *zip, char *name, jint ulen, jboolean addSlash);274#endif /* !_ZIP_H_ */275276277