1 /* src/vmcore/suck.c - functions to read LE ordered types from a buffer
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
35 #include "mm/memory.h"
37 #include "threads/lock-common.h"
39 #include "toolbox/list.h"
40 #include "toolbox/logging.h"
41 #include "toolbox/util.h"
43 #include "vm/exceptions.h"
44 #include "vm/properties.h"
47 #include "vmcore/loader.h"
48 #include "vmcore/options.h"
49 #include "vmcore/suck.h"
50 #include "vmcore/system.h"
51 #include "vmcore/zip.h"
54 /* global variables ***********************************************************/
56 list_t *list_classpath_entries;
59 /* suck_init *******************************************************************
61 Initializes the suck subsystem like initializing the classpath
64 *******************************************************************************/
68 TRACESUBSYSTEMINITIALIZATION("suck_init");
70 list_classpath_entries = list_create(OFFSET(list_classpath_entry, linkage));
78 /* scandir_filter **************************************************************
80 Filters for zip/jar files.
82 *******************************************************************************/
84 #if defined(__LINUX__)
85 static int scandir_filter(const struct dirent *a)
87 static int scandir_filter(struct dirent *a)
92 #if defined(_DIRENT_HAVE_D_NAMLEN)
95 namlen = strlen(a->d_name);
98 if ((strncasecmp(a->d_name + namlen - 4, ".zip", 4) == 0) ||
99 (strncasecmp(a->d_name + namlen - 4, ".jar", 4) == 0))
106 /* suck_add ********************************************************************
108 Adds a classpath to the global classpath entries list.
110 *******************************************************************************/
112 void suck_add(char *classpath)
114 list_classpath_entry *lce;
122 #if defined(ENABLE_ZLIB)
126 /* parse the classpath string */
128 for (start = classpath; (*start) != '\0'; ) {
130 /* search for ':' delimiter to get the end of the current entry */
131 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
135 filenamelen = end - start;
137 if (filenamelen > 4) {
138 if ((strncasecmp(end - 4, ".zip", 4) == 0) ||
139 (strncasecmp(end - 4, ".jar", 4) == 0)) {
144 /* save classpath entries as absolute pathnames */
149 if (*start != '/') { /* XXX fix me for win32 */
151 cwdlen = strlen(cwd) + strlen("/");
154 /* allocate memory for filename and fill it */
156 filename = MNEW(char, filenamelen + cwdlen + strlen("/") +
160 strcpy(filename, cwd);
161 strcat(filename, "/");
162 strncat(filename, start, filenamelen);
164 /* add cwd length to file length */
165 filenamelen += cwdlen;
168 strncpy(filename, start, filenamelen);
169 filename[filenamelen] = '\0';
175 #if defined(ENABLE_ZLIB)
176 ht = zip_open(filename);
179 lce = NEW(list_classpath_entry);
181 lce->type = CLASSPATH_ARCHIVE;
183 lce->path = filename;
184 lce->pathlen = filenamelen;
186 /* SUN compatible -verbose:class output */
188 if (opt_verboseclass)
189 printf("[Opened %s]\n", filename);
193 vm_abort("suck_add: zip/jar files not supported");
197 if (filename[filenamelen - 1] != '/') {/* XXX fixme for win32 */
198 filename[filenamelen] = '/';
199 filename[filenamelen + 1] = '\0';
203 lce = NEW(list_classpath_entry);
205 lce->type = CLASSPATH_PATH;
206 lce->path = filename;
207 lce->pathlen = filenamelen;
210 /* add current classpath entry, if no error */
213 list_add_last(list_classpath_entries, lce);
216 /* goto next classpath entry, skip ':' delimiter */
226 /* suck_add_from_property ******************************************************
228 Adds a classpath form a property entry to the global classpath
231 *******************************************************************************/
233 void suck_add_from_property(char *key)
240 struct dirent **namelist;
244 char *boot_class_path;
247 /* get the property value */
249 value = properties_get(key);
254 /* get the directory entries of the property */
256 for (start = value; (*start) != '\0'; ) {
258 /* search for ':' delimiter to get the end of the current entry */
260 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
265 /* allocate memory for the path entry */
267 pathlen = end - start;
268 path = MNEW(char, pathlen + strlen("0"));
270 /* copy and terminate the string */
272 strncpy(path, start, pathlen);
273 path[pathlen] = '\0';
275 /* Reset namelist to NULL for the freeing in an error case
280 /* scan the directory found for zip/jar files */
282 n = system_scandir(path, &namelist, scandir_filter, alphasort);
284 /* On error, just continue, this should be ok. */
287 for (i = 0; i < n; i++) {
288 #if defined(_DIRENT_HAVE_D_NAMLEN)
289 namlen = namelist[i]->d_namlen;
291 namlen = strlen(namelist[i]->d_name);
294 /* Allocate memory for bootclasspath. */
296 boot_class_path = properties_get("sun.boot.class.path");
299 pathlen + strlen("/") + namlen +
301 strlen(boot_class_path) +
304 /* Prepend the file found to the bootclasspath. */
308 strcat(p, namelist[i]->d_name);
310 strcat(p, boot_class_path);
312 properties_add("sun.boot.class.path", p);
313 properties_add("java.boot.class.path", p);
315 MFREE(boot_class_path, char, strlen(boot_class_path));
317 /* free the memory allocated by scandir */
318 /* (We use `free` as the memory came from the C library.) */
324 /* On some systems (like Linux) when n == 0, then namelist
325 returned from scnadir is NULL, thus we don't have to
327 (Use `free` as the memory came from the C library.) */
329 if (namelist != NULL)
332 MFREE(path, char, pathlen + strlen("0"));
335 /* goto next entry, skip ':' delimiter */
345 /* suck_check_classbuffer_size *************************************************
347 Assert that at least <len> bytes are left to read <len> is limited
348 to the range of non-negative s4 values.
350 *******************************************************************************/
352 bool suck_check_classbuffer_size(classbuffer *cb, s4 len)
354 #ifdef ENABLE_VERIFIER
355 if (len < 0 || ((cb->data + cb->size) - cb->pos) < len) {
356 exceptions_throw_classformaterror(cb->clazz, "Truncated class file");
359 #endif /* ENABLE_VERIFIER */
365 u1 suck_u1(classbuffer *cb)
369 a = SUCK_BE_U1(cb->pos);
376 u2 suck_u2(classbuffer *cb)
380 a = SUCK_BE_U2(cb->pos);
387 u4 suck_u4(classbuffer *cb)
391 a = SUCK_BE_U4(cb->pos);
398 u8 suck_u8(classbuffer *cb)
400 #if U8_AVAILABLE == 1
403 a = SUCK_BE_U8(cb->pos);
410 v.high = suck_u4(cb);
418 float suck_float(classbuffer *cb)
422 #if WORDS_BIGENDIAN == 0
426 for (i = 0; i < 4; i++)
427 buffer[3 - i] = suck_u1(cb);
429 MCOPY((u1 *) (&f), buffer, u1, 4);
431 suck_nbytes((u1*) (&f), cb, 4);
434 assert(sizeof(float) == 4);
440 double suck_double(classbuffer *cb)
444 #if WORDS_BIGENDIAN == 0
448 # if defined(__ARM__) && defined(__ARMEL__) && !defined(__VFP_FP__)
450 * On little endian ARM processors when using FPA, word order
451 * of doubles is still big endian. So take that into account
452 * here. When using VFP, word order of doubles follows byte
453 * order. (michi 2005/07/24)
455 for (i = 0; i < 4; i++)
456 buffer[3 - i] = suck_u1(cb);
457 for (i = 0; i < 4; i++)
458 buffer[7 - i] = suck_u1(cb);
460 for (i = 0; i < 8; i++)
461 buffer[7 - i] = suck_u1(cb);
462 # endif /* defined(__ARM__) && ... */
464 MCOPY((u1 *) (&d), buffer, u1, 8);
466 suck_nbytes((u1*) (&d), cb, 8);
469 assert(sizeof(double) == 8);
475 /* suck_nbytes *****************************************************************
477 Transfer block of classfile data into a buffer.
479 *******************************************************************************/
481 void suck_nbytes(u1 *buffer, classbuffer *cb, s4 len)
483 MCOPY(buffer, cb->pos, u1, len);
488 /* suck_skip_nbytes ************************************************************
490 Skip block of classfile data.
492 *******************************************************************************/
494 void suck_skip_nbytes(classbuffer *cb, s4 len)
500 /* suck_start ******************************************************************
502 Returns true if classbuffer is already loaded or a file for the
503 specified class has succussfully been read in. All directories of
504 the searchpath are used to find the classfile (<classname>.class).
505 Returns NULL if no classfile is found and writes an error message.
507 *******************************************************************************/
509 classbuffer *suck_start(classinfo *c)
511 list_classpath_entry *lce;
520 /* initialize return value */
524 /* get the classname as char string (do it here for the warning at
525 the end of the function) */
527 filenamelen = utf_bytes(c->name) + strlen(".class") + strlen("0");
528 filename = MNEW(char, filenamelen);
530 utf_copy(filename, c->name);
531 strcat(filename, ".class");
533 /* walk through all classpath entries */
535 for (lce = list_first(list_classpath_entries); lce != NULL && cb == NULL;
536 lce = list_next(list_classpath_entries, lce)) {
537 #if defined(ENABLE_ZLIB)
538 if (lce->type == CLASSPATH_ARCHIVE) {
540 /* enter a monitor on zip/jar archives */
542 LOCK_MONITOR_ENTER(lce);
544 /* try to get the file in current archive */
546 cb = zip_get(lce, c);
548 /* leave the monitor */
550 LOCK_MONITOR_EXIT(lce);
553 #endif /* defined(ENABLE_ZLIB) */
554 path = MNEW(char, lce->pathlen + filenamelen);
555 strcpy(path, lce->path);
556 strcat(path, filename);
558 classfile = system_fopen(path, "r");
560 if (classfile) { /* file exists */
561 if (!system_stat(path, &buffer)) { /* read classfile data */
562 cb = NEW(classbuffer);
564 cb->size = buffer.st_size;
565 cb->data = MNEW(u1, cb->size);
567 cb->path = lce->path;
569 /* read class data */
571 len = system_fread((void *) cb->data, 1, cb->size,
574 if (len != buffer.st_size) {
576 /* if (ferror(classfile)) { */
580 /* close the class file */
582 system_fclose(classfile);
586 MFREE(path, char, lce->pathlen + filenamelen);
587 #if defined(ENABLE_ZLIB)
594 dolog("Warning: Can not open class file '%s'", filename);
596 MFREE(filename, char, filenamelen);
602 /* suck_stop *******************************************************************
604 Frees memory for buffer with classfile data.
606 CAUTION: This function may only be called if buffer has been
607 allocated by suck_start with reading a file.
609 *******************************************************************************/
611 void suck_stop(classbuffer *cb)
615 MFREE(cb->data, u1, cb->size);
616 FREE(cb, classbuffer);
621 * These are local overrides for various environment variables in Emacs.
622 * Please do not remove this and leave it at the end of the file, where
623 * Emacs will automagically detect them.
624 * ---------------------------------------------------------------------
627 * indent-tabs-mode: t