1 /* src/vmcore/zip.c - ZIP file handling for bootstrap classloader
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: zip.c 8347 2007-08-18 13:04:59Z twisti $
41 #include "toolbox/hashtable.h"
43 #include "mm/memory.h"
45 #include "vm/global.h"
48 #include "vmcore/suck.h"
49 #include "vmcore/utf8.h"
50 #include "vmcore/zip.h"
53 /* start size for classes hashtable *******************************************/
55 #define HASHTABLE_CLASSES_SIZE (1 << 10)
59 http://www.pkware.com/business_and_developers/developer/popups/appnote.txt
62 /* all signatures in the ZIP file have a length of 4 bytes ********************/
64 #define SIGNATURE_LENGTH 4
66 /* Central directory structure *************************************************
77 central file header signature 4 bytes (0x02014b50)
78 version made by 2 bytes
79 version needed to extract 2 bytes
80 general purpose bit flag 2 bytes
81 compression method 2 bytes
82 last mod file time 2 bytes
83 last mod file date 2 bytes
85 compressed size 4 bytes
86 uncompressed size 4 bytes
87 file name length 2 bytes
88 extra field length 2 bytes
89 file comment length 2 bytes
90 disk number start 2 bytes
91 internal file attributes 2 bytes
92 external file attributes 4 bytes
93 relative offset of local header 4 bytes
95 file name (variable size)
96 extra field (variable size)
97 file comment (variable size)
101 header signature 4 bytes (0x05054b50)
103 signature data (variable size)
105 *******************************************************************************/
107 #define CDSFH_HEADER_SIZE 46
109 #define CDSFH_SIGNATURE 0x02014b50
110 #define CDSFH_COMPRESSION_METHOD 10
111 #define CDSFH_COMPRESSED_SIZE 20
112 #define CDSFH_UNCOMPRESSED_SIZE 24
113 #define CDSFH_FILE_NAME_LENGTH 28
114 #define CDSFH_EXTRA_FIELD_LENGTH 30
115 #define CDSFH_FILE_COMMENT_LENGTH 32
116 #define CDSFH_RELATIVE_OFFSET 42
117 #define CDSFH_FILENAME 46
119 typedef struct cdsfh cdsfh;
122 u2 compressionmethod;
127 u2 filecommentlength;
132 /* End of central directory record *********************************************
134 end of central dir signature 4 bytes (0x06054b50)
135 number of this disk 2 bytes
136 number of the disk with the
137 start of the central directory 2 bytes
138 total number of entries in the
139 central directory on this disk 2 bytes
140 total number of entries in
141 the central directory 2 bytes
142 size of the central directory 4 bytes
143 offset of start of central
144 directory with respect to
145 the starting disk number 4 bytes
146 .ZIP file comment length 2 bytes
147 .ZIP file comment (variable size)
149 *******************************************************************************/
151 #define EOCDR_SIGNATURE 0x06054b50
152 #define EOCDR_ENTRIES 10
153 #define EOCDR_OFFSET 16
155 typedef struct eocdr eocdr;
163 /* zip_open ********************************************************************
167 *******************************************************************************/
169 hashtable *zip_open(char *path)
172 hashtable_zipfile_entry *htzfe;
174 u1 lfh_signature[SIGNATURE_LENGTH];
181 const char *filename;
182 const char *classext;
184 u4 key; /* hashkey computed from utf-text */
185 u4 slot; /* slot in hashtable */
187 /* first of all, open the file */
189 if ((fd = open(path, O_RDONLY)) == -1)
192 /* check for signature in first local file header */
194 if (read(fd, lfh_signature, SIGNATURE_LENGTH) != SIGNATURE_LENGTH)
197 if (SUCK_LE_U4(lfh_signature) != LFH_SIGNATURE)
200 /* get the file length */
202 if ((len = lseek(fd, 0, SEEK_END)) == -1)
205 /* we better mmap the file */
207 filep = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
209 /* some older compilers, like DEC OSF cc, don't like comparisons
212 if ((ptrint) filep == (ptrint) MAP_FAILED)
215 /* find end of central directory record */
217 for (p = filep + len; p >= filep; p--)
218 if (SUCK_LE_U4(p) == EOCDR_SIGNATURE)
221 /* get number of entries in central directory */
223 eocdr.entries = SUCK_LE_U2(p + EOCDR_ENTRIES);
224 eocdr.offset = SUCK_LE_U4(p + EOCDR_OFFSET);
226 /* create hashtable for filenames */
230 hashtable_create(ht, HASHTABLE_CLASSES_SIZE);
232 /* add all file entries into the hashtable */
234 for (i = 0, p = filep + eocdr.offset; i < eocdr.entries; i++) {
235 /* check file header signature */
237 if (SUCK_LE_U4(p) != CDSFH_SIGNATURE)
240 /* we found an entry */
242 cdsfh.compressionmethod = SUCK_LE_U2(p + CDSFH_COMPRESSION_METHOD);
243 cdsfh.compressedsize = SUCK_LE_U4(p + CDSFH_COMPRESSED_SIZE);
244 cdsfh.uncompressedsize = SUCK_LE_U4(p + CDSFH_UNCOMPRESSED_SIZE);
245 cdsfh.filenamelength = SUCK_LE_U2(p + CDSFH_FILE_NAME_LENGTH);
246 cdsfh.extrafieldlength = SUCK_LE_U2(p + CDSFH_EXTRA_FIELD_LENGTH);
247 cdsfh.filecommentlength = SUCK_LE_U2(p + CDSFH_FILE_COMMENT_LENGTH);
248 cdsfh.relativeoffset = SUCK_LE_U4(p + CDSFH_RELATIVE_OFFSET);
250 /* create utf8 string of filename, strip .class from classes */
252 filename = (const char *) (p + CDSFH_FILENAME);
253 classext = filename + cdsfh.filenamelength - strlen(".class");
255 /* skip directory entries */
257 if (filename[cdsfh.filenamelength - 1] != '/') {
258 if (strncmp(classext, ".class", strlen(".class")) == 0)
259 u = utf_new(filename, cdsfh.filenamelength - strlen(".class"));
261 u = utf_new(filename, cdsfh.filenamelength);
263 /* insert class into hashtable */
265 htzfe = NEW(hashtable_zipfile_entry);
268 htzfe->compressionmethod = cdsfh.compressionmethod;
269 htzfe->compressedsize = cdsfh.compressedsize;
270 htzfe->uncompressedsize = cdsfh.uncompressedsize;
271 htzfe->data = filep + cdsfh.relativeoffset;
273 /* get hashtable slot */
275 key = utf_hashkey(u->text, u->blength);
276 slot = key & (ht->size - 1);
278 /* insert into external chain */
280 htzfe->hashlink = ht->ptr[slot];
282 /* insert hashtable zipfile entry */
284 ht->ptr[slot] = htzfe;
288 /* move to next central directory structure file header */
292 cdsfh.filenamelength +
293 cdsfh.extrafieldlength +
294 cdsfh.filecommentlength;
297 /* return pointer to hashtable */
303 /* zip_find ********************************************************************
305 Search for the given filename in the classpath entries of a zip file.
307 NOTE: The '.class' extension is stripped when reading a zip file, so if
308 you want to find a .class file, you must search for its name _without_
309 the '.class' extension.
310 XXX I dont like that, it makes foo and foo.class ambiguous. -Edwin
313 lce..........the classpath entries for the zip file
314 u............the filename to look for
317 hashtable_zipfile_entry * of the entry if found, or
320 *******************************************************************************/
322 hashtable_zipfile_entry *zip_find(list_classpath_entry *lce, utf *u)
325 u4 key; /* hashkey computed from utf-text */
326 u4 slot; /* slot in hashtable */
327 hashtable_zipfile_entry *htzfe; /* hashtable element */
329 /* get classes hashtable from the classpath entry */
333 /* get the hashtable slot of the name searched */
335 key = utf_hashkey(u->text, u->blength);
336 slot = key & (ht->size - 1);
337 htzfe = ht->ptr[slot];
339 /* search external hash chain for utf-symbol */
342 if (htzfe->filename == u)
345 /* next element in external chain */
347 htzfe = htzfe->hashlink;
350 /* file not found in this archive */
356 /* zip_get ********************************************************************
360 *******************************************************************************/
362 classbuffer *zip_get(list_classpath_entry *lce, classinfo *c)
364 hashtable_zipfile_entry *htzfe;
372 /* try to find the class in the current archive */
374 htzfe = zip_find(lce, c->name);
379 /* read stuff from local file header */
381 lfh.filenamelength = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
382 lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
384 indata = htzfe->data +
387 lfh.extrafieldlength;
389 /* allocate buffer for uncompressed data */
391 outdata = MNEW(u1, htzfe->uncompressedsize);
393 /* how is the file stored? */
395 switch (htzfe->compressionmethod) {
397 /* fill z_stream structure */
400 zs.avail_in = htzfe->compressedsize;
401 zs.next_out = outdata;
402 zs.avail_out = htzfe->uncompressedsize;
408 /* initialize this inflate run */
410 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
411 vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
413 /* decompress the file into buffer */
415 err = inflate(&zs, Z_SYNC_FLUSH);
417 if ((err != Z_STREAM_END) && (err != Z_OK))
418 vm_abort("zip_get: inflate failed: %s", strerror(errno));
420 /* finish this inflate run */
422 if (inflateEnd(&zs) != Z_OK)
423 vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
427 /* uncompressed file, just copy the data */
428 MCOPY(outdata, indata, u1, htzfe->compressedsize);
432 vm_abort("zip_get: unknown compression method %d",
433 htzfe->compressionmethod);
436 /* allocate classbuffer */
438 cb = NEW(classbuffer);
441 cb->size = htzfe->uncompressedsize;
444 cb->path = lce->path;
446 /* return the filled classbuffer structure */
453 * These are local overrides for various environment variables in Emacs.
454 * Please do not remove this and leave it at the end of the file, where
455 * Emacs will automagically detect them.
456 * ---------------------------------------------------------------------
459 * indent-tabs-mode: t
463 * vim:noexpandtab:sw=4:ts=4: