1 /*
   2  * Copyright 1995-2009 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 /*
  27  * Prototypes for zip file support
  28  */
  29 
  30 #ifndef _ZIP_H_
  31 #define _ZIP_H_
  32 
  33 /*
  34  * Header signatures
  35  */
  36 #define LOCSIG 0x04034b50L          /* "PK\003\004" */
  37 #define EXTSIG 0x08074b50L          /* "PK\007\008" */
  38 #define CENSIG 0x02014b50L          /* "PK\001\002" */
  39 #define ENDSIG 0x06054b50L          /* "PK\005\006" */
  40 
  41 #define ZIP64_ENDSIG 0x06064b50L    /* "PK\006\006" */
  42 #define ZIP64_LOCSIG 0x07064b50L    /* "PK\006\007" */
  43 
  44 /*
  45  * Header sizes including signatures
  46  */
  47 
  48 #ifdef USE_MMAP
  49 #define SIGSIZ  4
  50 #endif
  51 #define LOCHDR 30
  52 #define EXTHDR 16
  53 #define CENHDR 46
  54 #define ENDHDR 22
  55 
  56 #define ZIP64_ENDHDR 56       // ZIP64 end header size
  57 #define ZIP64_LOCHDR 20       // ZIP64 end loc header size
  58 #define ZIP64_EXTHDR 24       // EXT header size
  59 #define ZIP64_EXTID   1       // Extra field Zip64 header ID
  60 
  61 #define ZIP64_MAGICVAL 0xffffffffLL
  62 #define ZIP64_MAGICCOUNT 0xffff
  63 
  64 
  65 /*
  66  * Header field access macros
  67  */
  68 #define CH(b, n) (((unsigned char *)(b))[n])
  69 #define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8))
  70 #define LG(b, n) ((SH(b, n) | (SH(b, n+2) << 16)) &0xffffffffUL)
  71 #define LL(b, n) (((jlong)LG(b, n)) | (((jlong)LG(b, n+4)) << 32))
  72 #define GETSIG(b) LG(b, 0)
  73 
  74 /*
  75  * Macros for getting local file (LOC) header fields
  76  */
  77 #define LOCVER(b) SH(b, 4)          /* version needed to extract */
  78 #define LOCFLG(b) SH(b, 6)          /* general purpose bit flags */
  79 #define LOCHOW(b) SH(b, 8)          /* compression method */
  80 #define LOCTIM(b) LG(b, 10)         /* modification time */
  81 #define LOCCRC(b) LG(b, 14)         /* crc of uncompressed data */
  82 #define LOCSIZ(b) LG(b, 18)         /* compressed data size */
  83 #define LOCLEN(b) LG(b, 22)         /* uncompressed data size */
  84 #define LOCNAM(b) SH(b, 26)         /* filename length */
  85 #define LOCEXT(b) SH(b, 28)         /* extra field length */
  86 
  87 /*
  88  * Macros for getting extra local (EXT) header fields
  89  */
  90 #define EXTCRC(b) LG(b, 4)          /* crc of uncompressed data */
  91 #define EXTSIZ(b) LG(b, 8)          /* compressed size */
  92 #define EXTLEN(b) LG(b, 12)         /* uncompressed size */
  93 
  94 /*
  95  * Macros for getting central directory header (CEN) fields
  96  */
  97 #define CENVEM(b) SH(b, 4)          /* version made by */
  98 #define CENVER(b) SH(b, 6)          /* version needed to extract */
  99 #define CENFLG(b) SH(b, 8)          /* general purpose bit flags */
 100 #define CENHOW(b) SH(b, 10)         /* compression method */
 101 #define CENTIM(b) LG(b, 12)         /* modification time */
 102 #define CENCRC(b) LG(b, 16)         /* crc of uncompressed data */
 103 #define CENSIZ(b) LG(b, 20)         /* compressed size */
 104 #define CENLEN(b) LG(b, 24)         /* uncompressed size */
 105 #define CENNAM(b) SH(b, 28)         /* length of filename */
 106 #define CENEXT(b) SH(b, 30)         /* length of extra field */
 107 #define CENCOM(b) SH(b, 32)         /* file comment length */
 108 #define CENDSK(b) SH(b, 34)         /* disk number start */
 109 #define CENATT(b) SH(b, 36)         /* internal file attributes */
 110 #define CENATX(b) LG(b, 38)         /* external file attributes */
 111 #define CENOFF(b) LG(b, 42)         /* offset of local header */
 112 
 113 /*
 114  * Macros for getting end of central directory header (END) fields
 115  */
 116 #define ENDSUB(b) SH(b, 8)          /* number of entries on this disk */
 117 #define ENDTOT(b) SH(b, 10)         /* total number of entries */
 118 #define ENDSIZ(b) LG(b, 12)         /* central directory size */
 119 #define ENDOFF(b) LG(b, 16)         /* central directory offset */
 120 #define ENDCOM(b) SH(b, 20)         /* size of zip file comment */
 121 
 122 /*
 123  * Macros for getting Zip64 end of central directory header fields
 124  */
 125 #define ZIP64_ENDLEN(b) LL(b, 4)      /* size of zip64 end of central dir */
 126 #define ZIP64_ENDVEM(b) SH(b, 12)     /* version made by */
 127 #define ZIP64_ENDVER(b) SH(b, 14)     /* version needed to extract */
 128 #define ZIP64_ENDNMD(b) LG(b, 16)     /* number of this disk */
 129 #define ZIP64_ENDDSK(b) LG(b, 20)     /* disk number of start */
 130 #define ZIP64_ENDTOD(b) LL(b, 24)     /* total number of entries on this disk */
 131 #define ZIP64_ENDTOT(b) LL(b, 32)     /* total number of entries */
 132 #define ZIP64_ENDSIZ(b) LL(b, 40)     /* central directory size in bytes */
 133 #define ZIP64_ENDOFF(b) LL(b, 48)     /* offset of first CEN header */
 134 
 135 /*
 136  * Macros for getting Zip64 end of central directory locator fields
 137  */
 138 #define ZIP64_LOCDSK(b) LG(b, 4)      /* disk number start */
 139 #define ZIP64_LOCOFF(b) LL(b, 8)      /* offset of zip64 end */
 140 #define ZIP64_LOCTOT(b) LG(b, 16)     /* total number of disks */
 141 
 142 /*
 143  * Supported compression methods
 144  */
 145 #define STORED      0
 146 #define DEFLATED    8
 147 
 148 /*
 149  * Support for reading ZIP/JAR files. Some things worth noting:
 150  *
 151  * - Zip file entries larger than 2**32 bytes are not supported.
 152  * - jzentry time and crc fields are signed even though they really
 153  *   represent unsigned quantities.
 154  * - If csize is zero then the entry is uncompressed.
 155  * - If extra != 0 then the first two bytes are the length of the extra
 156  *   data in intel byte order.
 157  * - If pos <= 0 then it is the position of entry LOC header.
 158  *   If pos > 0 then it is the position of entry data.
 159  *   pos should not be accessed directly, but only by ZIP_GetEntryDataOffset.
 160  */
 161 
 162 typedef struct jzentry {  /* Zip file entry */
 163     char *name;           /* entry name */
 164     jlong time;           /* modification time */
 165     jlong size;           /* size of uncompressed data */
 166     jlong csize;          /* size of compressed data (zero if uncompressed) */
 167     jint crc;             /* crc of uncompressed data */
 168     char *comment;        /* optional zip file comment */
 169     jbyte *extra;         /* optional extra data */
 170     jlong pos;            /* position of LOC header or entry data */
 171     jint flag;            /* general purpose flag */
 172 } jzentry;
 173 
 174 /*
 175  * In-memory hash table cell.
 176  * In a typical system we have a *lot* of these, as we have one for
 177  * every entry in every active JAR.
 178  * Note that in order to save space we don't keep the name in memory,
 179  * but merely remember a 32 bit hash.
 180  */
 181 typedef struct jzcell {
 182     unsigned int hash;    /* 32 bit hashcode on name */
 183     jlong cenpos;         /* Offset of central directory file header */
 184     unsigned int next;    /* hash chain: index into jzfile->entries */
 185 } jzcell;
 186 
 187 typedef struct cencache {
 188     char *data;           /* A cached page of CEN headers */
 189     jlong pos;            /* file offset of data */
 190 } cencache;
 191 
 192 /*
 193  * Use ZFILE to represent access to a file in a platform-indepenent
 194  * fashion.
 195  */
 196 #ifdef WIN32
 197 #define ZFILE jlong
 198 #else
 199 #define ZFILE int
 200 #endif
 201 
 202 /*
 203  * Descriptor for a ZIP file.
 204  */
 205 typedef struct jzfile {   /* Zip file */
 206     char *name;           /* zip file name */
 207     jint refs;            /* number of active references */
 208     jlong len;            /* length (in bytes) of zip file */
 209 #ifdef USE_MMAP
 210     unsigned char *maddr; /* beginning address of the CEN & ENDHDR */
 211     jlong mlen;           /* length (in bytes) mmaped */
 212     jlong offset;         /* offset of the mmapped region from the
 213                              start of the file. */
 214 #else
 215     cencache cencache;    /* CEN header cache */
 216 #endif
 217     ZFILE zfd;            /* open file descriptor */
 218     void *lock;           /* read lock */
 219     char *comment;        /* zip file comment */
 220     jint clen;            /* length of the zip file comment */
 221     char *msg;            /* zip error message */
 222     jzcell *entries;      /* array of hash cells */
 223     jint total;           /* total number of entries */
 224     jint *table;          /* Hash chain heads: indexes into entries */
 225     jint tablelen;        /* number of hash heads */
 226     struct jzfile *next;  /* next zip file in search list */
 227     jzentry *cache;       /* we cache the most recently freed jzentry */
 228     /* Information on metadata names in META-INF directory */
 229     char **metanames;     /* array of meta names (may have null names) */
 230     jint metacurrent;     /* the next empty slot in metanames array */
 231     jint metacount;       /* number of slots in metanames array */
 232     jlong lastModified;   /* last modified time */
 233     jlong locpos;         /* position of first LOC header (usually 0) */
 234 } jzfile;
 235 
 236 /*
 237  * Index representing end of hash chain
 238  */
 239 #define ZIP_ENDCHAIN ((jint)-1)
 240 
 241 jzentry * JNICALL
 242 ZIP_FindEntry(jzfile *zip, char *name, jint *sizeP, jint *nameLenP);
 243 
 244 jboolean JNICALL
 245 ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entrynm);
 246 
 247 jzentry * JNICALL
 248 ZIP_GetNextEntry(jzfile *zip, jint n);
 249 
 250 jzfile * JNICALL
 251 ZIP_Open(const char *name, char **pmsg);
 252 
 253 jzfile *
 254 ZIP_Open_Generic(const char *name, char **pmsg, int mode, jlong lastModified);
 255 
 256 jzfile *
 257 ZIP_Get_From_Cache(const char *name, char **pmsg, jlong lastModified);
 258 
 259 jzfile *
 260 ZIP_Put_In_Cache(const char *name, ZFILE zfd, char **pmsg, jlong lastModified);
 261 
 262 void JNICALL
 263 ZIP_Close(jzfile *zip);
 264 
 265 jzentry * ZIP_GetEntry(jzfile *zip, char *name, jint ulen);
 266 void ZIP_Lock(jzfile *zip);
 267 void ZIP_Unlock(jzfile *zip);
 268 jint ZIP_Read(jzfile *zip, jzentry *entry, jlong pos, void *buf, jint len);
 269 void ZIP_FreeEntry(jzfile *zip, jzentry *ze);
 270 jlong ZIP_GetEntryDataOffset(jzfile *zip, jzentry *entry);
 271 
 272 #endif /* !_ZIP_H_ */