1 /* src/vm/suck.cpp - 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/mutex.hpp"
39 #include "toolbox/list.h"
40 #include "toolbox/logging.h"
41 #include "toolbox/util.h"
43 #include "vm/exceptions.hpp"
44 #include "vm/loader.hpp"
45 #include "vm/options.h"
47 #include "vm/properties.hpp"
48 #include "vm/suck.hpp"
53 /* global variables ***********************************************************/
55 list_t *list_classpath_entries;
58 /* suck_init *******************************************************************
60 Initializes the suck subsystem like initializing the classpath
63 *******************************************************************************/
67 TRACESUBSYSTEMINITIALIZATION("suck_init");
69 list_classpath_entries = list_create(OFFSET(list_classpath_entry, linkage));
77 /* scandir_filter **************************************************************
79 Filters for zip/jar files.
81 *******************************************************************************/
83 static int scandir_filter(const struct dirent *a)
87 #if defined(_DIRENT_HAVE_D_NAMLEN)
90 namlen = strlen(a->d_name);
93 if ((strncasecmp(a->d_name + namlen - 4, ".zip", 4) == 0) ||
94 (strncasecmp(a->d_name + namlen - 4, ".jar", 4) == 0))
101 /* suck_add ********************************************************************
103 Adds a classpath to the global classpath entries list.
105 *******************************************************************************/
107 void suck_add(char *classpath)
109 list_classpath_entry *lce;
117 #if defined(ENABLE_ZLIB)
121 /* parse the classpath string */
123 for (start = classpath; (*start) != '\0'; ) {
125 /* search for ':' delimiter to get the end of the current entry */
126 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
130 filenamelen = end - start;
132 if (filenamelen > 4) {
133 if ((strncasecmp(end - 4, ".zip", 4) == 0) ||
134 (strncasecmp(end - 4, ".jar", 4) == 0)) {
139 /* save classpath entries as absolute pathnames */
144 if (*start != '/') { /* XXX fix me for win32 */
146 cwdlen = strlen(cwd) + strlen("/");
149 /* allocate memory for filename and fill it */
151 filename = MNEW(char, filenamelen + cwdlen + strlen("/") +
155 strcpy(filename, cwd);
156 strcat(filename, "/");
157 strncat(filename, start, filenamelen);
159 /* add cwd length to file length */
160 filenamelen += cwdlen;
163 strncpy(filename, start, filenamelen);
164 filename[filenamelen] = '\0';
170 #if defined(ENABLE_ZLIB)
171 ht = zip_open(filename);
174 lce = NEW(list_classpath_entry);
176 lce->type = CLASSPATH_ARCHIVE;
178 lce->path = filename;
179 lce->pathlen = filenamelen;
181 /* SUN compatible -verbose:class output */
183 if (opt_verboseclass)
184 printf("[Opened %s]\n", filename);
188 VM::get_current()->abort("suck_add: zip/jar files not supported");
192 if (filename[filenamelen - 1] != '/') {/* XXX fixme for win32 */
193 filename[filenamelen] = '/';
194 filename[filenamelen + 1] = '\0';
198 lce = NEW(list_classpath_entry);
200 lce->type = CLASSPATH_PATH;
201 lce->path = filename;
202 lce->pathlen = filenamelen;
205 /* add current classpath entry, if no error */
208 list_add_last(list_classpath_entries, lce);
211 /* goto next classpath entry, skip ':' delimiter */
221 /* suck_add_from_property ******************************************************
223 Adds a classpath form a property entry to the global classpath
226 *******************************************************************************/
228 void suck_add_from_property(const char *key)
234 struct dirent **namelist;
240 /* get the property value */
241 Properties properties = VM::get_current()->get_properties();
242 value = properties.get(key);
247 /* get the directory entries of the property */
249 for (start = value; (*start) != '\0'; ) {
251 /* search for ':' delimiter to get the end of the current entry */
253 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
258 /* allocate memory for the path entry */
260 pathlen = end - start;
261 char* path = MNEW(char, pathlen + strlen("0"));
263 /* copy and terminate the string */
265 strncpy(path, start, pathlen);
266 path[pathlen] = '\0';
268 /* Reset namelist to NULL for the freeing in an error case
273 /* scan the directory found for zip/jar files */
275 n = os::scandir((const char*) path, &namelist, &scandir_filter, (int (*)(const void*, const void*)) &alphasort);
277 /* On error, just continue, this should be ok. */
280 for (i = 0; i < n; i++) {
281 #if defined(_DIRENT_HAVE_D_NAMLEN)
282 namlen = namelist[i]->d_namlen;
284 namlen = strlen(namelist[i]->d_name);
287 /* Allocate memory for bootclasspath. */
289 // FIXME Make boot_class_path const char*.
290 char* boot_class_path = (char*) properties.get("sun.boot.class.path");
293 pathlen + strlen("/") + namlen +
295 strlen(boot_class_path) +
298 /* Prepend the file found to the bootclasspath. */
302 strcat(p, namelist[i]->d_name);
304 strcat(p, boot_class_path);
306 properties.put("sun.boot.class.path", p);
307 properties.put("java.boot.class.path", p);
309 MFREE(boot_class_path, char, strlen(boot_class_path));
311 /* free the memory allocated by scandir */
312 /* (We use `free` as the memory came from the C library.) */
318 /* On some systems (like Linux) when n == 0, then namelist
319 returned from scnadir is NULL, thus we don't have to
321 (Use `free` as the memory came from the C library.) */
323 if (namelist != NULL)
326 MFREE(path, char, pathlen + strlen("0"));
329 /* goto next entry, skip ':' delimiter */
339 /* suck_check_classbuffer_size *************************************************
341 Assert that at least <len> bytes are left to read <len> is limited
342 to the range of non-negative s4 values.
344 *******************************************************************************/
346 bool suck_check_classbuffer_size(classbuffer *cb, s4 len)
348 #ifdef ENABLE_VERIFIER
349 if (len < 0 || ((cb->data + cb->size) - cb->pos) < len) {
350 exceptions_throw_classformaterror(cb->clazz, "Truncated class file");
353 #endif /* ENABLE_VERIFIER */
359 u1 suck_u1(classbuffer *cb)
363 a = SUCK_BE_U1(cb->pos);
370 u2 suck_u2(classbuffer *cb)
374 a = SUCK_BE_U2(cb->pos);
381 u4 suck_u4(classbuffer *cb)
385 a = SUCK_BE_U4(cb->pos);
392 u8 suck_u8(classbuffer *cb)
394 #if U8_AVAILABLE == 1
397 a = SUCK_BE_U8(cb->pos);
404 v.high = suck_u4(cb);
412 float suck_float(classbuffer *cb)
416 #if WORDS_BIGENDIAN == 0
420 for (i = 0; i < 4; i++)
421 buffer[3 - i] = suck_u1(cb);
423 MCOPY((u1 *) (&f), buffer, u1, 4);
425 suck_nbytes((u1*) (&f), cb, 4);
428 assert(sizeof(float) == 4);
434 double suck_double(classbuffer *cb)
438 #if WORDS_BIGENDIAN == 0
442 # if defined(__ARM__) && defined(__ARMEL__) && !defined(__VFP_FP__)
444 * On little endian ARM processors when using FPA, word order
445 * of doubles is still big endian. So take that into account
446 * here. When using VFP, word order of doubles follows byte
447 * order. (michi 2005/07/24)
449 for (i = 0; i < 4; i++)
450 buffer[3 - i] = suck_u1(cb);
451 for (i = 0; i < 4; i++)
452 buffer[7 - i] = suck_u1(cb);
454 for (i = 0; i < 8; i++)
455 buffer[7 - i] = suck_u1(cb);
456 # endif /* defined(__ARM__) && ... */
458 MCOPY((u1 *) (&d), buffer, u1, 8);
460 suck_nbytes((u1*) (&d), cb, 8);
463 assert(sizeof(double) == 8);
469 /* suck_nbytes *****************************************************************
471 Transfer block of classfile data into a buffer.
473 *******************************************************************************/
475 void suck_nbytes(u1 *buffer, classbuffer *cb, s4 len)
477 MCOPY(buffer, cb->pos, u1, len);
482 /* suck_skip_nbytes ************************************************************
484 Skip block of classfile data.
486 *******************************************************************************/
488 void suck_skip_nbytes(classbuffer *cb, s4 len)
494 /* suck_start ******************************************************************
496 Returns true if classbuffer is already loaded or a file for the
497 specified class has succussfully been read in. All directories of
498 the searchpath are used to find the classfile (<classname>.class).
499 Returns NULL if no classfile is found and writes an error message.
501 *******************************************************************************/
503 classbuffer *suck_start(classinfo *c)
505 list_classpath_entry *lce;
514 /* initialize return value */
518 /* get the classname as char string (do it here for the warning at
519 the end of the function) */
521 filenamelen = utf_bytes(c->name) + strlen(".class") + strlen("0");
522 filename = MNEW(char, filenamelen);
524 utf_copy(filename, c->name);
525 strcat(filename, ".class");
527 /* walk through all classpath entries */
529 for (lce = (list_classpath_entry*) list_first(list_classpath_entries); lce != NULL && cb == NULL;
530 lce = (list_classpath_entry*) list_next(list_classpath_entries, lce)) {
531 #if defined(ENABLE_ZLIB)
532 if (lce->type == CLASSPATH_ARCHIVE) {
534 /* enter a monitor on zip/jar archives */
538 /* try to get the file in current archive */
540 cb = zip_get(lce, c);
542 /* leave the monitor */
544 lce->mutex->unlock();
547 #endif /* defined(ENABLE_ZLIB) */
548 path = MNEW(char, lce->pathlen + filenamelen);
549 strcpy(path, lce->path);
550 strcat(path, filename);
552 classfile = os::fopen(path, "r");
554 if (classfile) { /* file exists */
555 if (!os::stat(path, &buffer)) { /* read classfile data */
556 cb = NEW(classbuffer);
558 cb->size = buffer.st_size;
559 cb->data = MNEW(u1, cb->size);
561 cb->path = lce->path;
563 /* read class data */
565 len = os::fread((void *) cb->data, 1, cb->size,
568 if (len != buffer.st_size) {
570 /* if (ferror(classfile)) { */
574 /* close the class file */
576 os::fclose(classfile);
580 MFREE(path, char, lce->pathlen + filenamelen);
581 #if defined(ENABLE_ZLIB)
588 dolog("Warning: Can not open class file '%s'", filename);
590 MFREE(filename, char, filenamelen);
596 /* suck_stop *******************************************************************
598 Frees memory for buffer with classfile data.
600 CAUTION: This function may only be called if buffer has been
601 allocated by suck_start with reading a file.
603 *******************************************************************************/
605 void suck_stop(classbuffer *cb)
609 MFREE(cb->data, u1, cb->size);
610 FREE(cb, classbuffer);
615 * These are local overrides for various environment variables in Emacs.
616 * Please do not remove this and leave it at the end of the file, where
617 * Emacs will automagically detect them.
618 * ---------------------------------------------------------------------
621 * indent-tabs-mode: t
625 * vim:noexpandtab:sw=4:ts=4: