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
39 #include "toolbox/hashtable.h"
41 #include "mm/memory.h"
43 #include "vm/global.h"
46 #include "vmcore/suck.h"
47 #include "vmcore/utf8.h"
48 #include "vmcore/zip.h"
51 /* start size for classes hashtable *******************************************/
53 #define HASHTABLE_CLASSES_SIZE (1 << 10)
57 http://www.pkware.com/business_and_developers/developer/popups/appnote.txt
60 /* all signatures in the ZIP file have a length of 4 bytes ********************/
62 #define SIGNATURE_LENGTH 4
64 /* Central directory structure *************************************************
75 central file header signature 4 bytes (0x02014b50)
76 version made by 2 bytes
77 version needed to extract 2 bytes
78 general purpose bit flag 2 bytes
79 compression method 2 bytes
80 last mod file time 2 bytes
81 last mod file date 2 bytes
83 compressed size 4 bytes
84 uncompressed size 4 bytes
85 file name length 2 bytes
86 extra field length 2 bytes
87 file comment length 2 bytes
88 disk number start 2 bytes
89 internal file attributes 2 bytes
90 external file attributes 4 bytes
91 relative offset of local header 4 bytes
93 file name (variable size)
94 extra field (variable size)
95 file comment (variable size)
99 header signature 4 bytes (0x05054b50)
101 signature data (variable size)
103 *******************************************************************************/
105 #define CDSFH_HEADER_SIZE 46
107 #define CDSFH_SIGNATURE 0x02014b50
108 #define CDSFH_COMPRESSION_METHOD 10
109 #define CDSFH_COMPRESSED_SIZE 20
110 #define CDSFH_UNCOMPRESSED_SIZE 24
111 #define CDSFH_FILE_NAME_LENGTH 28
112 #define CDSFH_EXTRA_FIELD_LENGTH 30
113 #define CDSFH_FILE_COMMENT_LENGTH 32
114 #define CDSFH_RELATIVE_OFFSET 42
115 #define CDSFH_FILENAME 46
117 typedef struct cdsfh cdsfh;
120 u2 compressionmethod;
125 u2 filecommentlength;
130 /* End of central directory record *********************************************
132 end of central dir signature 4 bytes (0x06054b50)
133 number of this disk 2 bytes
134 number of the disk with the
135 start of the central directory 2 bytes
136 total number of entries in the
137 central directory on this disk 2 bytes
138 total number of entries in
139 the central directory 2 bytes
140 size of the central directory 4 bytes
141 offset of start of central
142 directory with respect to
143 the starting disk number 4 bytes
144 .ZIP file comment length 2 bytes
145 .ZIP file comment (variable size)
147 *******************************************************************************/
149 #define EOCDR_SIGNATURE 0x06054b50
150 #define EOCDR_ENTRIES 10
151 #define EOCDR_OFFSET 16
153 typedef struct eocdr eocdr;
161 /* zip_open ********************************************************************
165 *******************************************************************************/
167 hashtable *zip_open(char *path)
170 hashtable_zipfile_entry *htzfe;
172 u1 lfh_signature[SIGNATURE_LENGTH];
179 const char *filename;
180 const char *classext;
182 u4 key; /* hashkey computed from utf-text */
183 u4 slot; /* slot in hashtable */
185 /* first of all, open the file */
187 if ((fd = open(path, O_RDONLY)) == -1)
190 /* check for signature in first local file header */
192 if (read(fd, lfh_signature, SIGNATURE_LENGTH) != SIGNATURE_LENGTH)
195 if (SUCK_LE_U4(lfh_signature) != LFH_SIGNATURE)
198 /* get the file length */
200 if ((len = lseek(fd, 0, SEEK_END)) == -1)
203 /* we better mmap the file */
205 filep = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
207 /* some older compilers, like DEC OSF cc, don't like comparisons
210 if ((ptrint) filep == (ptrint) MAP_FAILED)
213 /* find end of central directory record */
215 for (p = filep + len; p >= filep; p--)
216 if (SUCK_LE_U4(p) == EOCDR_SIGNATURE)
219 /* get number of entries in central directory */
221 eocdr.entries = SUCK_LE_U2(p + EOCDR_ENTRIES);
222 eocdr.offset = SUCK_LE_U4(p + EOCDR_OFFSET);
224 /* create hashtable for filenames */
228 hashtable_create(ht, HASHTABLE_CLASSES_SIZE);
230 /* add all file entries into the hashtable */
232 for (i = 0, p = filep + eocdr.offset; i < eocdr.entries; i++) {
233 /* check file header signature */
235 if (SUCK_LE_U4(p) != CDSFH_SIGNATURE)
238 /* we found an entry */
240 cdsfh.compressionmethod = SUCK_LE_U2(p + CDSFH_COMPRESSION_METHOD);
241 cdsfh.compressedsize = SUCK_LE_U4(p + CDSFH_COMPRESSED_SIZE);
242 cdsfh.uncompressedsize = SUCK_LE_U4(p + CDSFH_UNCOMPRESSED_SIZE);
243 cdsfh.filenamelength = SUCK_LE_U2(p + CDSFH_FILE_NAME_LENGTH);
244 cdsfh.extrafieldlength = SUCK_LE_U2(p + CDSFH_EXTRA_FIELD_LENGTH);
245 cdsfh.filecommentlength = SUCK_LE_U2(p + CDSFH_FILE_COMMENT_LENGTH);
246 cdsfh.relativeoffset = SUCK_LE_U4(p + CDSFH_RELATIVE_OFFSET);
248 /* create utf8 string of filename, strip .class from classes */
250 filename = (const char *) (p + CDSFH_FILENAME);
251 classext = filename + cdsfh.filenamelength - strlen(".class");
253 /* skip directory entries */
255 if (filename[cdsfh.filenamelength - 1] != '/') {
256 if (strncmp(classext, ".class", strlen(".class")) == 0)
257 u = utf_new(filename, cdsfh.filenamelength - strlen(".class"));
259 u = utf_new(filename, cdsfh.filenamelength);
261 /* insert class into hashtable */
263 htzfe = NEW(hashtable_zipfile_entry);
266 htzfe->compressionmethod = cdsfh.compressionmethod;
267 htzfe->compressedsize = cdsfh.compressedsize;
268 htzfe->uncompressedsize = cdsfh.uncompressedsize;
269 htzfe->data = filep + cdsfh.relativeoffset;
271 /* get hashtable slot */
273 key = utf_hashkey(u->text, u->blength);
274 slot = key & (ht->size - 1);
276 /* insert into external chain */
278 htzfe->hashlink = ht->ptr[slot];
280 /* insert hashtable zipfile entry */
282 ht->ptr[slot] = htzfe;
286 /* move to next central directory structure file header */
290 cdsfh.filenamelength +
291 cdsfh.extrafieldlength +
292 cdsfh.filecommentlength;
295 /* return pointer to hashtable */
301 /* zip_find ********************************************************************
303 Search for the given filename in the classpath entries of a zip file.
305 NOTE: The '.class' extension is stripped when reading a zip file, so if
306 you want to find a .class file, you must search for its name _without_
307 the '.class' extension.
308 XXX I dont like that, it makes foo and foo.class ambiguous. -Edwin
311 lce..........the classpath entries for the zip file
312 u............the filename to look for
315 hashtable_zipfile_entry * of the entry if found, or
318 *******************************************************************************/
320 hashtable_zipfile_entry *zip_find(list_classpath_entry *lce, utf *u)
323 u4 key; /* hashkey computed from utf-text */
324 u4 slot; /* slot in hashtable */
325 hashtable_zipfile_entry *htzfe; /* hashtable element */
327 /* get classes hashtable from the classpath entry */
331 /* get the hashtable slot of the name searched */
333 key = utf_hashkey(u->text, u->blength);
334 slot = key & (ht->size - 1);
335 htzfe = ht->ptr[slot];
337 /* search external hash chain for utf-symbol */
340 if (htzfe->filename == u)
343 /* next element in external chain */
345 htzfe = htzfe->hashlink;
348 /* file not found in this archive */
354 /* zip_get ********************************************************************
358 *******************************************************************************/
360 classbuffer *zip_get(list_classpath_entry *lce, classinfo *c)
362 hashtable_zipfile_entry *htzfe;
370 /* try to find the class in the current archive */
372 htzfe = zip_find(lce, c->name);
377 /* read stuff from local file header */
379 lfh.filenamelength = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
380 lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
382 indata = htzfe->data +
385 lfh.extrafieldlength;
387 /* allocate buffer for uncompressed data */
389 outdata = MNEW(u1, htzfe->uncompressedsize);
391 /* how is the file stored? */
393 switch (htzfe->compressionmethod) {
395 /* fill z_stream structure */
398 zs.avail_in = htzfe->compressedsize;
399 zs.next_out = outdata;
400 zs.avail_out = htzfe->uncompressedsize;
406 /* initialize this inflate run */
408 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
409 vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
411 /* decompress the file into buffer */
413 err = inflate(&zs, Z_SYNC_FLUSH);
415 if ((err != Z_STREAM_END) && (err != Z_OK))
416 vm_abort("zip_get: inflate failed: %s", strerror(errno));
418 /* finish this inflate run */
420 if (inflateEnd(&zs) != Z_OK)
421 vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
425 /* uncompressed file, just copy the data */
426 MCOPY(outdata, indata, u1, htzfe->compressedsize);
430 vm_abort("zip_get: unknown compression method %d",
431 htzfe->compressionmethod);
434 /* allocate classbuffer */
436 cb = NEW(classbuffer);
439 cb->size = htzfe->uncompressedsize;
442 cb->path = lce->path;
444 /* return the filled classbuffer structure */
451 * These are local overrides for various environment variables in Emacs.
452 * Please do not remove this and leave it at the end of the file, where
453 * Emacs will automagically detect them.
454 * ---------------------------------------------------------------------
457 * indent-tabs-mode: t
461 * vim:noexpandtab:sw=4:ts=4: