Home | History | Annotate | Download | only in native
      1 /*
      2  * Copyright (c) 1995, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     23  * 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 #define LOCHDR 30
     49 #define EXTHDR 16
     50 #define CENHDR 46
     51 #define ENDHDR 22
     52 
     53 #define ZIP64_ENDHDR 56       // ZIP64 end header size
     54 #define ZIP64_LOCHDR 20       // ZIP64 end loc header size
     55 #define ZIP64_EXTHDR 24       // EXT header size
     56 #define ZIP64_EXTID   1       // Extra field Zip64 header ID
     57 
     58 #define ZIP64_MAGICVAL 0xffffffffLL
     59 #define ZIP64_MAGICCOUNT 0xffff
     60 
     61 
     62 /*
     63  * Header field access macros
     64  */
     65 #define CH(b, n) (((unsigned char *)(b))[n])
     66 #define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8))
     67 #define LG(b, n) ((SH(b, n) | (SH(b, n+2) << 16)) &0xffffffffUL)
     68 #define LL(b, n) (((jlong)LG(b, n)) | (((jlong)LG(b, n+4)) << 32))
     69 #define GETSIG(b) LG(b, 0)
     70 
     71 /*
     72  * Macros for getting local file (LOC) header fields
     73  */
     74 #define LOCVER(b) SH(b, 4)          /* version needed to extract */
     75 #define LOCFLG(b) SH(b, 6)          /* general purpose bit flags */
     76 #define LOCHOW(b) SH(b, 8)          /* compression method */
     77 #define LOCTIM(b) LG(b, 10)         /* modification time */
     78 #define LOCCRC(b) LG(b, 14)         /* crc of uncompressed data */
     79 #define LOCSIZ(b) LG(b, 18)         /* compressed data size */
     80 #define LOCLEN(b) LG(b, 22)         /* uncompressed data size */
     81 #define LOCNAM(b) SH(b, 26)         /* filename length */
     82 #define LOCEXT(b) SH(b, 28)         /* extra field length */
     83 
     84 /*
     85  * Macros for getting extra local (EXT) header fields
     86  */
     87 #define EXTCRC(b) LG(b, 4)          /* crc of uncompressed data */
     88 #define EXTSIZ(b) LG(b, 8)          /* compressed size */
     89 #define EXTLEN(b) LG(b, 12)         /* uncompressed size */
     90 
     91 /*
     92  * Macros for getting central directory header (CEN) fields
     93  */
     94 #define CENVEM(b) SH(b, 4)          /* version made by */
     95 #define CENVER(b) SH(b, 6)          /* version needed to extract */
     96 #define CENFLG(b) SH(b, 8)          /* general purpose bit flags */
     97 #define CENHOW(b) SH(b, 10)         /* compression method */
     98 #define CENTIM(b) LG(b, 12)         /* modification time */
     99 #define CENCRC(b) LG(b, 16)         /* crc of uncompressed data */
    100 #define CENSIZ(b) LG(b, 20)         /* compressed size */
    101 #define CENLEN(b) LG(b, 24)         /* uncompressed size */
    102 #define CENNAM(b) SH(b, 28)         /* length of filename */
    103 #define CENEXT(b) SH(b, 30)         /* length of extra field */
    104 #define CENCOM(b) SH(b, 32)         /* file comment length */
    105 #define CENDSK(b) SH(b, 34)         /* disk number start */
    106 #define CENATT(b) SH(b, 36)         /* internal file attributes */
    107 #define CENATX(b) LG(b, 38)         /* external file attributes */
    108 #define CENOFF(b) LG(b, 42)         /* offset of local header */
    109 
    110 /*
    111  * Macros for getting end of central directory header (END) fields
    112  */
    113 #define ENDSUB(b) SH(b, 8)          /* number of entries on this disk */
    114 #define ENDTOT(b) SH(b, 10)         /* total number of entries */
    115 #define ENDSIZ(b) LG(b, 12)         /* central directory size */
    116 #define ENDOFF(b) LG(b, 16)         /* central directory offset */
    117 #define ENDCOM(b) SH(b, 20)         /* size of zip file comment */
    118 
    119 /*
    120  * Macros for getting Zip64 end of central directory header fields
    121  */
    122 #define ZIP64_ENDLEN(b) LL(b, 4)      /* size of zip64 end of central dir */
    123 #define ZIP64_ENDVEM(b) SH(b, 12)     /* version made by */
    124 #define ZIP64_ENDVER(b) SH(b, 14)     /* version needed to extract */
    125 #define ZIP64_ENDNMD(b) LG(b, 16)     /* number of this disk */
    126 #define ZIP64_ENDDSK(b) LG(b, 20)     /* disk number of start */
    127 #define ZIP64_ENDTOD(b) LL(b, 24)     /* total number of entries on this disk */
    128 #define ZIP64_ENDTOT(b) LL(b, 32)     /* total number of entries */
    129 #define ZIP64_ENDSIZ(b) LL(b, 40)     /* central directory size in bytes */
    130 #define ZIP64_ENDOFF(b) LL(b, 48)     /* offset of first CEN header */
    131 
    132 /*
    133  * Macros for getting Zip64 end of central directory locator fields
    134  */
    135 #define ZIP64_LOCDSK(b) LG(b, 4)      /* disk number start */
    136 #define ZIP64_LOCOFF(b) LL(b, 8)      /* offset of zip64 end */
    137 #define ZIP64_LOCTOT(b) LG(b, 16)     /* total number of disks */
    138 
    139 /*
    140  * Supported compression methods
    141  */
    142 #define STORED      0
    143 #define DEFLATED    8
    144 
    145 /*
    146  * Support for reading ZIP/JAR files. Some things worth noting:
    147  *
    148  * - Zip file entries larger than 2**32 bytes are not supported.
    149  * - jzentry time and crc fields are signed even though they really
    150  *   represent unsigned quantities.
    151  * - If csize is zero then the entry is uncompressed.
    152  * - If extra != 0 then the first two bytes are the length of the extra
    153  *   data in intel byte order.
    154  * - If pos <= 0 then it is the position of entry LOC header.
    155  *   If pos > 0 then it is the position of entry data.
    156  *   pos should not be accessed directly, but only by ZIP_GetEntryDataOffset.
    157  * - entry name may include embedded null character, use nlen for length
    158  */
    159 
    160 typedef struct jzentry {  /* Zip file entry */
    161     char *name;           /* entry name */
    162     jlong time;           /* modification time */
    163     jlong size;           /* size of uncompressed data */
    164     jlong csize;          /* size of compressed data (zero if uncompressed) */
    165     jint crc;             /* crc of uncompressed data */
    166     char *comment;        /* optional zip file comment */
    167     jbyte *extra;         /* optional extra data */
    168     jlong pos;            /* position of LOC header or entry data */
    169     jint flag;            /* general purpose flag */
    170     jint nlen;            /* length of the entry name */
    171 } jzentry;
    172 
    173 /*
    174  * In-memory hash table cell.
    175  * In a typical system we have a *lot* of these, as we have one for
    176  * every entry in every active JAR.
    177  * Note that in order to save space we don't keep the name in memory,
    178  * but merely remember a 32 bit hash.
    179  */
    180 typedef struct jzcell {
    181     unsigned int hash;    /* 32 bit hashcode on name */
    182     unsigned int next;    /* hash chain: index into jzfile->entries */
    183     jlong cenpos;         /* Offset of central directory file header */
    184 } jzcell;
    185 
    186 typedef struct cencache {
    187     char *data;           /* A cached page of CEN headers */
    188     jlong pos;            /* file offset of data */
    189 } cencache;
    190 
    191 /*
    192  * Use ZFILE to represent access to a file in a platform-indepenent
    193  * fashion.
    194  */
    195 #ifdef WIN32
    196 #define ZFILE jlong
    197 #else
    198 #define ZFILE int
    199 #endif
    200 
    201 /*
    202  * Use mmap for CEN & ENDHDR sections
    203  */
    204 #define USE_MMAP 1
    205 
    206 /*
    207  * Descriptor for a ZIP file.
    208  */
    209 typedef struct jzfile {   /* Zip file */
    210     char *name;           /* zip file name */
    211     jint refs;            /* number of active references */
    212     jlong len;            /* length (in bytes) of zip file */
    213 #ifdef USE_MMAP
    214     unsigned char *maddr; /* beginning address of the CEN & ENDHDR */
    215     jlong mlen;           /* length (in bytes) mmaped */
    216     jlong offset;         /* offset of the mmapped region from the
    217                              start of the file. */
    218     jboolean usemmap;     /* if mmap is used. */
    219 #endif
    220     jboolean locsig;      /* if zip file starts with LOCSIG */
    221     cencache cencache;    /* CEN header cache */
    222     ZFILE zfd;            /* open file descriptor */
    223     void *lock;           /* read lock */
    224     char *comment;        /* zip file comment */
    225     jint clen;            /* length of the zip file comment */
    226     char *msg;            /* zip error message */
    227     jzcell *entries;      /* array of hash cells */
    228     jint total;           /* total number of entries */
    229     jint *table;          /* Hash chain heads: indexes into entries */
    230     jint tablelen;        /* number of hash heads */
    231     struct jzfile *next;  /* next zip file in search list */
    232     jzentry *cache;       /* we cache the most recently freed jzentry */
    233     /* Information on metadata names in META-INF directory */
    234     char **metanames;     /* array of meta names (may have null names) */
    235     jint metacurrent;     /* the next empty slot in metanames array */
    236     jint metacount;       /* number of slots in metanames array */
    237     jlong lastModified;   /* last modified time */
    238     jlong locpos;         /* position of first LOC header (usually 0) */
    239 } jzfile;
    240 
    241 /*
    242  * Index representing end of hash chain
    243  */
    244 #define ZIP_ENDCHAIN ((jint)-1)
    245 
    246 jzentry * JNICALL
    247 ZIP_FindEntry(jzfile *zip, char *name, jint *sizeP, jint *nameLenP);
    248 
    249 jboolean JNICALL
    250 ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entrynm);
    251 
    252 jzentry * JNICALL
    253 ZIP_GetNextEntry(jzfile *zip, jint n);
    254 
    255 jzfile * JNICALL
    256 ZIP_Open(const char *name, char **pmsg);
    257 
    258 jzfile *
    259 ZIP_Open_Generic(const char *name, char **pmsg, int mode, jlong lastModified);
    260 
    261 jzfile *
    262 ZIP_Get_From_Cache(const char *name, char **pmsg, jlong lastModified);
    263 
    264 jzfile *
    265 ZIP_Put_In_Cache(const char *name, ZFILE zfd, char **pmsg, jlong lastModified);
    266 
    267 jzfile *
    268 ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, jboolean usemmap);
    269 
    270 void JNICALL
    271 ZIP_Close(jzfile *zip);
    272 
    273 jzentry * ZIP_GetEntry(jzfile *zip, char *name, jint ulen);
    274 void ZIP_Lock(jzfile *zip);
    275 void ZIP_Unlock(jzfile *zip);
    276 jint ZIP_Read(jzfile *zip, jzentry *entry, jlong pos, void *buf, jint len);
    277 void ZIP_FreeEntry(jzfile *zip, jzentry *ze);
    278 jlong ZIP_GetEntryDataOffset(jzfile *zip, jzentry *entry);
    279 jzentry * ZIP_GetEntry2(jzfile *zip, char *name, jint ulen, jboolean addSlash);
    280 
    281 #endif /* !_ZIP_H_ */
    282