068eaaa04ffa5d253d7aa5cc638d0a7eb04c9dac
[cacao.git] / src / vm / zip.c
1 /* src/vm/zip.c - ZIP file handling for bootstrap classloader
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
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.
14
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.
19
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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: zip.c 4022 2006-01-01 16:31:40Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <zlib.h>
42 #include <sys/mman.h>
43
44 #include "vm/types.h"
45
46 #include "mm/memory.h"
47 #include "vm/global.h"
48 #include "vm/hashtable.h"
49 #include "vm/suck.h"
50 #include "vm/utf8.h"
51 #include "vm/zip.h"
52
53
54 /* start size for classes hashtable *******************************************/
55
56 #define HASHTABLE_CLASSES_SIZE    (1 << 10)
57
58
59 /* info taken from:
60    http://www.pkware.com/business_and_developers/developer/popups/appnote.txt
61 */
62
63 /* all signatures in the ZIP file have a length of 4 bytes ********************/
64
65 #define SIGNATURE_LENGTH    4
66
67
68 /* Local file header ***********************************************************
69
70    local file header signature     4 bytes  (0x04034b50)
71    version needed to extract       2 bytes
72    general purpose bit flag        2 bytes
73    compression method              2 bytes
74    last mod file time              2 bytes
75    last mod file date              2 bytes
76    crc-32                          4 bytes
77    compressed size                 4 bytes
78    uncompressed size               4 bytes
79    file name length                2 bytes
80    extra field length              2 bytes
81
82    file name (variable size)
83    extra field (variable size)
84
85 *******************************************************************************/
86
87 #define LFH_HEADER_SIZE              30
88
89 #define LFH_SIGNATURE                0x04034b50
90 #define LFH_FILE_NAME_LENGTH         26
91 #define LFH_EXTRA_FIELD_LENGTH       28
92
93 typedef struct lfh lfh;
94
95 struct lfh {
96         u2 compressionmethod;
97         u4 compressedsize;
98         u4 uncompressedsize;
99         u2 filenamelength;
100         u2 extrafieldlength;
101 };
102
103
104 /* Central directory structure *************************************************
105
106    [file header 1]
107    .
108    .
109    . 
110    [file header n]
111    [digital signature] 
112    
113    File header:
114    
115      central file header signature   4 bytes  (0x02014b50)
116      version made by                 2 bytes
117      version needed to extract       2 bytes
118      general purpose bit flag        2 bytes
119      compression method              2 bytes
120      last mod file time              2 bytes
121      last mod file date              2 bytes
122      crc-32                          4 bytes
123      compressed size                 4 bytes
124      uncompressed size               4 bytes
125      file name length                2 bytes
126      extra field length              2 bytes
127      file comment length             2 bytes
128      disk number start               2 bytes
129      internal file attributes        2 bytes
130      external file attributes        4 bytes
131      relative offset of local header 4 bytes
132    
133      file name (variable size)
134      extra field (variable size)
135      file comment (variable size)
136
137    Digital signature:
138    
139      header signature                4 bytes  (0x05054b50)
140      size of data                    2 bytes
141      signature data (variable size)
142
143 *******************************************************************************/
144
145 #define CDSFH_HEADER_SIZE            46
146
147 #define CDSFH_SIGNATURE              0x02014b50
148 #define CDSFH_COMPRESSION_METHOD     10
149 #define CDSFH_COMPRESSED_SIZE        20
150 #define CDSFH_UNCOMPRESSED_SIZE      24
151 #define CDSFH_FILE_NAME_LENGTH       28
152 #define CDSFH_EXTRA_FIELD_LENGTH     30
153 #define CDSFH_FILE_COMMENT_LENGTH    32
154 #define CDSFH_RELATIVE_OFFSET        42
155 #define CDSFH_FILENAME               46
156
157 typedef struct cdsfh cdsfh;
158
159 struct cdsfh {
160         u2 compressionmethod;
161         u4 compressedsize;
162         u4 uncompressedsize;
163         u2 filenamelength;
164         u2 extrafieldlength;
165         u2 filecommentlength;
166         u4 relativeoffset;
167 };
168
169
170 /* End of central directory record *********************************************
171
172    end of central dir signature    4 bytes  (0x06054b50)
173    number of this disk             2 bytes
174    number of the disk with the
175    start of the central directory  2 bytes
176    total number of entries in the
177    central directory on this disk  2 bytes
178    total number of entries in
179    the central directory           2 bytes
180    size of the central directory   4 bytes
181    offset of start of central
182    directory with respect to
183    the starting disk number        4 bytes
184    .ZIP file comment length        2 bytes
185    .ZIP file comment       (variable size)
186
187 *******************************************************************************/
188
189 #define EOCDR_SIGNATURE              0x06054b50
190 #define EOCDR_ENTRIES                10
191 #define EOCDR_OFFSET                 16
192
193 typedef struct eocdr eocdr;
194
195 struct eocdr {
196         u2 entries;
197         u4 offset;
198 };
199
200
201 /* zip_open ********************************************************************
202
203    XXX
204
205 *******************************************************************************/
206
207 hashtable *zip_open(char *path)
208 {
209         hashtable               *ht;
210         hashtable_zipfile_entry *htzfe;
211         int                      fd;
212         u1                       lfh_signature[SIGNATURE_LENGTH];
213         off_t                    len;
214         u1                      *filep;
215         s4                       i;
216         u1                      *p;
217         eocdr                    eocdr;
218         cdsfh                    cdsfh;
219         const char              *filename;
220         const char              *classext;
221         utf                     *u;
222         u4                       key;       /* hashkey computed from utf-text     */
223         u4                       slot;      /* slot in hashtable                  */
224
225         /* first of all, open the file */
226
227         if ((fd = open(path, O_RDONLY)) == -1)
228                 return NULL;
229
230         /* check for signature in first local file header */
231
232         if (read(fd, lfh_signature, SIGNATURE_LENGTH) != SIGNATURE_LENGTH)
233                 return NULL;
234
235         if (SUCK_LE_U4(lfh_signature) != LFH_SIGNATURE)
236                 return NULL;
237
238         /* get the file length */
239
240         if ((len = lseek(fd, 0, SEEK_END)) == -1)
241                 return NULL;
242
243         /* we better mmap the file */
244
245         filep = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
246
247         /* some older compilers, like DEC OSF cc, don't like comparisons
248        on void* types */
249
250         if ((ptrint) filep == (ptrint) MAP_FAILED)
251                 return NULL;
252
253         /* find end of central directory record */
254
255         for (p = filep + len; p >= filep; p--)
256                 if (SUCK_LE_U4(p) == EOCDR_SIGNATURE)
257                         break;
258
259         /* get number of entries in central directory */
260
261         eocdr.entries = SUCK_LE_U2(p + EOCDR_ENTRIES);
262         eocdr.offset  = SUCK_LE_U4(p + EOCDR_OFFSET);
263
264         /* create hashtable for filenames */
265
266         ht = NEW(hashtable);
267
268         hashtable_create(ht, HASHTABLE_CLASSES_SIZE);
269
270         /* add all file entries into the hashtable */
271
272         for (i = 0, p = filep + eocdr.offset; i < eocdr.entries; i++) {
273                 /* check file header signature */
274
275                 if (SUCK_LE_U4(p) != CDSFH_SIGNATURE)
276                         return NULL;
277
278                 /* we found an entry */
279
280                 cdsfh.compressionmethod = SUCK_LE_U2(p + CDSFH_COMPRESSION_METHOD);
281                 cdsfh.compressedsize    = SUCK_LE_U4(p + CDSFH_COMPRESSED_SIZE);
282                 cdsfh.uncompressedsize  = SUCK_LE_U4(p + CDSFH_UNCOMPRESSED_SIZE);
283                 cdsfh.filenamelength    = SUCK_LE_U2(p + CDSFH_FILE_NAME_LENGTH);
284                 cdsfh.extrafieldlength  = SUCK_LE_U2(p + CDSFH_EXTRA_FIELD_LENGTH);
285                 cdsfh.filecommentlength = SUCK_LE_U2(p + CDSFH_FILE_COMMENT_LENGTH);
286                 cdsfh.relativeoffset    = SUCK_LE_U4(p + CDSFH_RELATIVE_OFFSET);
287
288                 /* create utf8 string of filename, strip .class from classes */
289
290                 filename = (const char *) (p + CDSFH_FILENAME);
291                 classext = filename + cdsfh.filenamelength - strlen(".class");
292
293                 if (strncmp(classext, ".class", strlen(".class")) == 0)
294                         u = utf_new(filename, cdsfh.filenamelength - strlen(".class"));
295                 else
296                         u = utf_new(filename, cdsfh.filenamelength);
297
298                 /* insert class into hashtable */
299
300                 htzfe = NEW(hashtable_zipfile_entry);
301
302                 htzfe->filename          = u;
303                 htzfe->compressionmethod = cdsfh.compressionmethod;
304                 htzfe->compressedsize    = cdsfh.compressedsize;
305                 htzfe->uncompressedsize  = cdsfh.uncompressedsize;
306                 htzfe->data              = filep + cdsfh.relativeoffset;
307
308                 /* get hashtable slot */
309
310                 key  = utf_hashkey(u->text, u->blength);
311                 slot = key & (ht->size - 1);
312
313                 /* insert into external chain */
314
315                 htzfe->hashlink = ht->ptr[slot];
316
317                 /* insert hashtable zipfile entry */
318
319                 ht->ptr[slot] = htzfe;
320                 ht->entries++;
321
322                 /* move to next central directory structure file header */
323
324                 p = p +
325                         CDSFH_HEADER_SIZE +
326                         cdsfh.filenamelength +
327                         cdsfh.extrafieldlength +
328                         cdsfh.filecommentlength;
329         }
330
331         /* return pointer to hashtable */
332
333         return ht;
334 }
335
336
337 /* zip_find ********************************************************************
338
339    XXX
340
341 *******************************************************************************/
342
343 hashtable_zipfile_entry *zip_find(list_classpath_entry *lce, utf *u)
344 {
345         hashtable               *ht;
346         u4                       key;       /* hashkey computed from utf-text     */
347         u4                       slot;      /* slot in hashtable                  */
348         hashtable_zipfile_entry *htzfe;     /* hashtable element                  */
349
350         /* get classes hashtable from the classpath entry */
351
352         ht = lce->htclasses;
353
354         /* get the hashtable slot of the name searched */
355
356         key   = utf_hashkey(u->text, u->blength);
357         slot  = key & (ht->size - 1);
358         htzfe = ht->ptr[slot];
359
360         /* search external hash chain for utf-symbol */
361
362         while (htzfe) {
363                 if (htzfe->filename == u)
364                         return htzfe;
365
366                 /* next element in external chain */
367
368                 htzfe = htzfe->hashlink;
369         }
370
371         /* file not found in this archive */
372
373         return NULL;
374 }
375
376
377 /* zip_get ********************************************************************
378
379    XXX
380
381 *******************************************************************************/
382
383 classbuffer *zip_get(list_classpath_entry *lce, classinfo *c)
384 {
385         hashtable_zipfile_entry *htzfe;
386         lfh                      lfh;
387         u1                      *indata;
388         u1                      *outdata;
389         z_stream                 zs;
390         int                      err;
391         classbuffer             *cb;
392
393         /* try to find the class in the current archive */
394
395         if ((htzfe = zip_find(lce, c->name)) == NULL)
396                 return NULL;
397
398         /* read stuff from local file header */
399
400         lfh.filenamelength   = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
401         lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
402
403         indata = htzfe->data +
404                 LFH_HEADER_SIZE +
405                 lfh.filenamelength +
406                 lfh.extrafieldlength;
407
408         /* allocate buffer for uncompressed data */
409
410         outdata = MNEW(u1, htzfe->uncompressedsize);
411
412         /* how is the file stored? */
413
414         switch (htzfe->compressionmethod) {
415         case Z_DEFLATED:
416                 /* fill z_stream structure */
417
418                 zs.next_in   = indata;
419                 zs.avail_in  = htzfe->compressedsize;
420                 zs.next_out  = outdata;
421                 zs.avail_out = htzfe->uncompressedsize;
422
423                 zs.zalloc = Z_NULL;
424                 zs.zfree  = Z_NULL;
425                 zs.opaque = Z_NULL;
426
427                 /* initialize this inflate run */
428
429                 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
430                         assert(0);
431
432                 /* decompress the file into buffer */
433
434                 err = inflate(&zs, Z_SYNC_FLUSH);
435
436                 if ((err != Z_STREAM_END) && (err != Z_OK))
437                         assert(0);
438
439                 /* finish this inflate run */
440
441                 if (inflateEnd(&zs) != Z_OK)
442                         assert(0);
443                 break;
444
445         case 0:
446                 /* uncompressed file, just copy the data */
447                 MCOPY(outdata, indata, u1, htzfe->compressedsize);
448                 break;
449
450         default:
451                 assert(0);
452         }
453         
454         /* allocate classbuffer */
455
456         cb = NEW(classbuffer);
457
458         cb->class = c;
459         cb->size  = htzfe->uncompressedsize;
460         cb->data  = outdata;
461         cb->pos   = outdata;
462         cb->path  = lce->path;
463
464         /* return the filled classbuffer structure */
465
466         return cb;
467 }
468
469
470 /*
471  * These are local overrides for various environment variables in Emacs.
472  * Please do not remove this and leave it at the end of the file, where
473  * Emacs will automagically detect them.
474  * ---------------------------------------------------------------------
475  * Local variables:
476  * mode: c
477  * indent-tabs-mode: t
478  * c-basic-offset: 4
479  * tab-width: 4
480  * End:
481  */