1 /* src/vm/zip.cpp - 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 "vm/descriptor.hpp" /* needed to prevent circular dependency */
38 #include "toolbox/hashtable.h"
40 #include "mm/memory.hpp"
42 #include "vm/global.h"
43 #include "vm/suck.hpp"
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;
158 #if defined(__cplusplus)
162 /* zip_open ********************************************************************
166 *******************************************************************************/
168 hashtable *zip_open(char *path)
171 hashtable_zipfile_entry *htzfe;
173 u1 lfh_signature[SIGNATURE_LENGTH];
180 const char *filename;
181 const char *classext;
183 u4 key; /* hashkey computed from utf-text */
184 u4 slot; /* slot in hashtable */
186 /* first of all, open the file */
188 if ((fd = open(path, O_RDONLY)) == -1)
191 /* check for signature in first local file header */
193 if (read(fd, lfh_signature, SIGNATURE_LENGTH) != SIGNATURE_LENGTH)
196 if (SUCK_LE_U4(lfh_signature) != LFH_SIGNATURE)
199 /* get the file length */
201 if ((len = lseek(fd, 0, SEEK_END)) == -1)
204 /* we better mmap the file */
206 filep = (u1*) mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
208 /* some older compilers, like DEC OSF cc, don't like comparisons
211 if ((ptrint) filep == (ptrint) MAP_FAILED)
214 /* find end of central directory record */
216 for (p = filep + len; p >= filep; p--)
217 if (SUCK_LE_U4(p) == EOCDR_SIGNATURE)
220 /* get number of entries in central directory */
222 eocdr.entries = SUCK_LE_U2(p + EOCDR_ENTRIES);
223 eocdr.offset = SUCK_LE_U4(p + EOCDR_OFFSET);
225 /* create hashtable for filenames */
229 hashtable_create(ht, HASHTABLE_CLASSES_SIZE);
231 /* add all file entries into the hashtable */
233 for (i = 0, p = filep + eocdr.offset; i < eocdr.entries; i++) {
234 /* check file header signature */
236 if (SUCK_LE_U4(p) != CDSFH_SIGNATURE)
239 /* we found an entry */
241 cdsfh.compressionmethod = SUCK_LE_U2(p + CDSFH_COMPRESSION_METHOD);
242 cdsfh.compressedsize = SUCK_LE_U4(p + CDSFH_COMPRESSED_SIZE);
243 cdsfh.uncompressedsize = SUCK_LE_U4(p + CDSFH_UNCOMPRESSED_SIZE);
244 cdsfh.filenamelength = SUCK_LE_U2(p + CDSFH_FILE_NAME_LENGTH);
245 cdsfh.extrafieldlength = SUCK_LE_U2(p + CDSFH_EXTRA_FIELD_LENGTH);
246 cdsfh.filecommentlength = SUCK_LE_U2(p + CDSFH_FILE_COMMENT_LENGTH);
247 cdsfh.relativeoffset = SUCK_LE_U4(p + CDSFH_RELATIVE_OFFSET);
249 /* create utf8 string of filename, strip .class from classes */
251 filename = (const char *) (p + CDSFH_FILENAME);
252 classext = filename + cdsfh.filenamelength - strlen(".class");
254 /* skip directory entries */
256 if (filename[cdsfh.filenamelength - 1] != '/') {
257 if (strncmp(classext, ".class", strlen(".class")) == 0)
258 u = utf_new(filename, cdsfh.filenamelength - strlen(".class"));
260 u = utf_new(filename, cdsfh.filenamelength);
262 /* insert class into hashtable */
264 htzfe = NEW(hashtable_zipfile_entry);
267 htzfe->compressionmethod = cdsfh.compressionmethod;
268 htzfe->compressedsize = cdsfh.compressedsize;
269 htzfe->uncompressedsize = cdsfh.uncompressedsize;
270 htzfe->data = filep + cdsfh.relativeoffset;
272 /* get hashtable slot */
274 key = utf_hashkey(u->text, u->blength);
275 slot = key & (ht->size - 1);
277 /* insert into external chain */
279 htzfe->hashlink = (hashtable_zipfile_entry*) ht->ptr[slot];
281 /* insert hashtable zipfile entry */
283 ht->ptr[slot] = htzfe;
287 /* move to next central directory structure file header */
291 cdsfh.filenamelength +
292 cdsfh.extrafieldlength +
293 cdsfh.filecommentlength;
296 /* return pointer to hashtable */
302 /* zip_find ********************************************************************
304 Search for the given filename in the classpath entries of a zip file.
306 NOTE: The '.class' extension is stripped when reading a zip file, so if
307 you want to find a .class file, you must search for its name _without_
308 the '.class' extension.
309 XXX I dont like that, it makes foo and foo.class ambiguous. -Edwin
312 lce..........the classpath entries for the zip file
313 u............the filename to look for
316 hashtable_zipfile_entry * of the entry if found, or
319 *******************************************************************************/
321 hashtable_zipfile_entry *zip_find(list_classpath_entry *lce, utf *u)
324 u4 key; /* hashkey computed from utf-text */
325 u4 slot; /* slot in hashtable */
326 hashtable_zipfile_entry *htzfe; /* hashtable element */
328 /* get classes hashtable from the classpath entry */
332 /* get the hashtable slot of the name searched */
334 key = utf_hashkey(u->text, u->blength);
335 slot = key & (ht->size - 1);
336 htzfe = (hashtable_zipfile_entry*) ht->ptr[slot];
338 /* search external hash chain for utf-symbol */
341 if (htzfe->filename == u)
344 /* next element in external chain */
346 htzfe = htzfe->hashlink;
349 /* file not found in this archive */
355 /* zip_get ********************************************************************
359 *******************************************************************************/
361 classbuffer *zip_get(list_classpath_entry *lce, classinfo *c)
363 hashtable_zipfile_entry *htzfe;
371 /* try to find the class in the current archive */
373 htzfe = zip_find(lce, c->name);
378 /* read stuff from local file header */
380 lfh.filenamelength = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
381 lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
383 indata = htzfe->data +
386 lfh.extrafieldlength;
388 /* allocate buffer for uncompressed data */
390 outdata = MNEW(u1, htzfe->uncompressedsize);
392 /* how is the file stored? */
394 switch (htzfe->compressionmethod) {
396 /* fill z_stream structure */
399 zs.avail_in = htzfe->compressedsize;
400 zs.next_out = outdata;
401 zs.avail_out = htzfe->uncompressedsize;
407 /* initialize this inflate run */
409 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
410 vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
412 /* decompress the file into buffer */
414 err = inflate(&zs, Z_SYNC_FLUSH);
416 if ((err != Z_STREAM_END) && (err != Z_OK))
417 vm_abort("zip_get: inflate failed: %s", strerror(errno));
419 /* finish this inflate run */
421 if (inflateEnd(&zs) != Z_OK)
422 vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
426 /* uncompressed file, just copy the data */
427 MCOPY(outdata, indata, u1, htzfe->compressedsize);
431 vm_abort("zip_get: unknown compression method %d",
432 htzfe->compressionmethod);
435 /* allocate classbuffer */
437 cb = NEW(classbuffer);
440 cb->size = htzfe->uncompressedsize;
443 cb->path = lce->path;
445 /* return the filled classbuffer structure */
450 #if defined(__cplusplus)
455 * These are local overrides for various environment variables in Emacs.
456 * Please do not remove this and leave it at the end of the file, where
457 * Emacs will automagically detect them.
458 * ---------------------------------------------------------------------
461 * indent-tabs-mode: t
465 * vim:noexpandtab:sw=4:ts=4: