1 /* src/vmcore/zip.c - ZIP file handling for bootstrap classloader
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
37 #include "toolbox/hashtable.h"
39 #include "mm/memory.h"
41 #include "vm/global.h"
44 #include "vmcore/suck.h"
45 #include "vmcore/utf8.h"
46 #include "vmcore/zip.h"
49 /* start size for classes hashtable *******************************************/
51 #define HASHTABLE_CLASSES_SIZE (1 << 10)
55 http://www.pkware.com/business_and_developers/developer/popups/appnote.txt
58 /* all signatures in the ZIP file have a length of 4 bytes ********************/
60 #define SIGNATURE_LENGTH 4
62 /* Central directory structure *************************************************
73 central file header signature 4 bytes (0x02014b50)
74 version made by 2 bytes
75 version needed to extract 2 bytes
76 general purpose bit flag 2 bytes
77 compression method 2 bytes
78 last mod file time 2 bytes
79 last mod file date 2 bytes
81 compressed size 4 bytes
82 uncompressed size 4 bytes
83 file name length 2 bytes
84 extra field length 2 bytes
85 file comment length 2 bytes
86 disk number start 2 bytes
87 internal file attributes 2 bytes
88 external file attributes 4 bytes
89 relative offset of local header 4 bytes
91 file name (variable size)
92 extra field (variable size)
93 file comment (variable size)
97 header signature 4 bytes (0x05054b50)
99 signature data (variable size)
101 *******************************************************************************/
103 #define CDSFH_HEADER_SIZE 46
105 #define CDSFH_SIGNATURE 0x02014b50
106 #define CDSFH_COMPRESSION_METHOD 10
107 #define CDSFH_COMPRESSED_SIZE 20
108 #define CDSFH_UNCOMPRESSED_SIZE 24
109 #define CDSFH_FILE_NAME_LENGTH 28
110 #define CDSFH_EXTRA_FIELD_LENGTH 30
111 #define CDSFH_FILE_COMMENT_LENGTH 32
112 #define CDSFH_RELATIVE_OFFSET 42
113 #define CDSFH_FILENAME 46
115 typedef struct cdsfh cdsfh;
118 u2 compressionmethod;
123 u2 filecommentlength;
128 /* End of central directory record *********************************************
130 end of central dir signature 4 bytes (0x06054b50)
131 number of this disk 2 bytes
132 number of the disk with the
133 start of the central directory 2 bytes
134 total number of entries in the
135 central directory on this disk 2 bytes
136 total number of entries in
137 the central directory 2 bytes
138 size of the central directory 4 bytes
139 offset of start of central
140 directory with respect to
141 the starting disk number 4 bytes
142 .ZIP file comment length 2 bytes
143 .ZIP file comment (variable size)
145 *******************************************************************************/
147 #define EOCDR_SIGNATURE 0x06054b50
148 #define EOCDR_ENTRIES 10
149 #define EOCDR_OFFSET 16
151 typedef struct eocdr eocdr;
159 /* zip_open ********************************************************************
163 *******************************************************************************/
165 hashtable *zip_open(char *path)
168 hashtable_zipfile_entry *htzfe;
170 u1 lfh_signature[SIGNATURE_LENGTH];
177 const char *filename;
178 const char *classext;
180 u4 key; /* hashkey computed from utf-text */
181 u4 slot; /* slot in hashtable */
183 /* first of all, open the file */
185 if ((fd = open(path, O_RDONLY)) == -1)
188 /* check for signature in first local file header */
190 if (read(fd, lfh_signature, SIGNATURE_LENGTH) != SIGNATURE_LENGTH)
193 if (SUCK_LE_U4(lfh_signature) != LFH_SIGNATURE)
196 /* get the file length */
198 if ((len = lseek(fd, 0, SEEK_END)) == -1)
201 /* we better mmap the file */
203 filep = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
205 /* some older compilers, like DEC OSF cc, don't like comparisons
208 if ((ptrint) filep == (ptrint) MAP_FAILED)
211 /* find end of central directory record */
213 for (p = filep + len; p >= filep; p--)
214 if (SUCK_LE_U4(p) == EOCDR_SIGNATURE)
217 /* get number of entries in central directory */
219 eocdr.entries = SUCK_LE_U2(p + EOCDR_ENTRIES);
220 eocdr.offset = SUCK_LE_U4(p + EOCDR_OFFSET);
222 /* create hashtable for filenames */
226 hashtable_create(ht, HASHTABLE_CLASSES_SIZE);
228 /* add all file entries into the hashtable */
230 for (i = 0, p = filep + eocdr.offset; i < eocdr.entries; i++) {
231 /* check file header signature */
233 if (SUCK_LE_U4(p) != CDSFH_SIGNATURE)
236 /* we found an entry */
238 cdsfh.compressionmethod = SUCK_LE_U2(p + CDSFH_COMPRESSION_METHOD);
239 cdsfh.compressedsize = SUCK_LE_U4(p + CDSFH_COMPRESSED_SIZE);
240 cdsfh.uncompressedsize = SUCK_LE_U4(p + CDSFH_UNCOMPRESSED_SIZE);
241 cdsfh.filenamelength = SUCK_LE_U2(p + CDSFH_FILE_NAME_LENGTH);
242 cdsfh.extrafieldlength = SUCK_LE_U2(p + CDSFH_EXTRA_FIELD_LENGTH);
243 cdsfh.filecommentlength = SUCK_LE_U2(p + CDSFH_FILE_COMMENT_LENGTH);
244 cdsfh.relativeoffset = SUCK_LE_U4(p + CDSFH_RELATIVE_OFFSET);
246 /* create utf8 string of filename, strip .class from classes */
248 filename = (const char *) (p + CDSFH_FILENAME);
249 classext = filename + cdsfh.filenamelength - strlen(".class");
251 /* skip directory entries */
253 if (filename[cdsfh.filenamelength - 1] != '/') {
254 if (strncmp(classext, ".class", strlen(".class")) == 0)
255 u = utf_new(filename, cdsfh.filenamelength - strlen(".class"));
257 u = utf_new(filename, cdsfh.filenamelength);
259 /* insert class into hashtable */
261 htzfe = NEW(hashtable_zipfile_entry);
264 htzfe->compressionmethod = cdsfh.compressionmethod;
265 htzfe->compressedsize = cdsfh.compressedsize;
266 htzfe->uncompressedsize = cdsfh.uncompressedsize;
267 htzfe->data = filep + cdsfh.relativeoffset;
269 /* get hashtable slot */
271 key = utf_hashkey(u->text, u->blength);
272 slot = key & (ht->size - 1);
274 /* insert into external chain */
276 htzfe->hashlink = ht->ptr[slot];
278 /* insert hashtable zipfile entry */
280 ht->ptr[slot] = htzfe;
284 /* move to next central directory structure file header */
288 cdsfh.filenamelength +
289 cdsfh.extrafieldlength +
290 cdsfh.filecommentlength;
293 /* return pointer to hashtable */
299 /* zip_find ********************************************************************
301 Search for the given filename in the classpath entries of a zip file.
303 NOTE: The '.class' extension is stripped when reading a zip file, so if
304 you want to find a .class file, you must search for its name _without_
305 the '.class' extension.
306 XXX I dont like that, it makes foo and foo.class ambiguous. -Edwin
309 lce..........the classpath entries for the zip file
310 u............the filename to look for
313 hashtable_zipfile_entry * of the entry if found, or
316 *******************************************************************************/
318 hashtable_zipfile_entry *zip_find(list_classpath_entry *lce, utf *u)
321 u4 key; /* hashkey computed from utf-text */
322 u4 slot; /* slot in hashtable */
323 hashtable_zipfile_entry *htzfe; /* hashtable element */
325 /* get classes hashtable from the classpath entry */
329 /* get the hashtable slot of the name searched */
331 key = utf_hashkey(u->text, u->blength);
332 slot = key & (ht->size - 1);
333 htzfe = ht->ptr[slot];
335 /* search external hash chain for utf-symbol */
338 if (htzfe->filename == u)
341 /* next element in external chain */
343 htzfe = htzfe->hashlink;
346 /* file not found in this archive */
352 /* zip_get ********************************************************************
356 *******************************************************************************/
358 classbuffer *zip_get(list_classpath_entry *lce, classinfo *c)
360 hashtable_zipfile_entry *htzfe;
368 /* try to find the class in the current archive */
370 htzfe = zip_find(lce, c->name);
375 /* read stuff from local file header */
377 lfh.filenamelength = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
378 lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
380 indata = htzfe->data +
383 lfh.extrafieldlength;
385 /* allocate buffer for uncompressed data */
387 outdata = MNEW(u1, htzfe->uncompressedsize);
389 /* how is the file stored? */
391 switch (htzfe->compressionmethod) {
393 /* fill z_stream structure */
396 zs.avail_in = htzfe->compressedsize;
397 zs.next_out = outdata;
398 zs.avail_out = htzfe->uncompressedsize;
404 /* initialize this inflate run */
406 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
407 vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
409 /* decompress the file into buffer */
411 err = inflate(&zs, Z_SYNC_FLUSH);
413 if ((err != Z_STREAM_END) && (err != Z_OK))
414 vm_abort("zip_get: inflate failed: %s", strerror(errno));
416 /* finish this inflate run */
418 if (inflateEnd(&zs) != Z_OK)
419 vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
423 /* uncompressed file, just copy the data */
424 MCOPY(outdata, indata, u1, htzfe->compressedsize);
428 vm_abort("zip_get: unknown compression method %d",
429 htzfe->compressionmethod);
432 /* allocate classbuffer */
434 cb = NEW(classbuffer);
437 cb->size = htzfe->uncompressedsize;
440 cb->path = lce->path;
442 /* return the filled classbuffer structure */
449 * These are local overrides for various environment variables in Emacs.
450 * Please do not remove this and leave it at the end of the file, where
451 * Emacs will automagically detect them.
452 * ---------------------------------------------------------------------
455 * indent-tabs-mode: t
459 * vim:noexpandtab:sw=4:ts=4: