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.hpp"
37 #include "threads/mutex.hpp"
39 #include "toolbox/list.hpp"
40 #include "toolbox/logging.hpp"
42 #include "vm/exceptions.hpp"
43 #include "vm/loader.hpp"
44 #include "vm/options.h"
46 #include "vm/properties.hpp"
47 #include "vm/suck.hpp"
52 /* scandir_filter **************************************************************
54 Filters for zip/jar files.
56 *******************************************************************************/
58 static int scandir_filter(const struct dirent *a)
62 #if defined(_DIRENT_HAVE_D_NAMLEN)
65 namlen = strlen(a->d_name);
68 if ((strncasecmp(a->d_name + namlen - 4, ".zip", 4) == 0) ||
69 (strncasecmp(a->d_name + namlen - 4, ".jar", 4) == 0))
77 * Adds a classpath to the global classpath entries list.
79 void SuckClasspath::add(char *classpath)
81 list_classpath_entry *lce;
89 #if defined(ENABLE_ZLIB)
93 /* parse the classpath string */
95 for (start = classpath; (*start) != '\0'; ) {
97 /* search for ':' delimiter to get the end of the current entry */
98 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
102 filenamelen = end - start;
104 if (filenamelen > 4) {
105 if ((strncasecmp(end - 4, ".zip", 4) == 0) ||
106 (strncasecmp(end - 4, ".jar", 4) == 0)) {
111 /* save classpath entries as absolute pathnames */
116 if (*start != '/') { /* XXX fix me for win32 */
118 cwdlen = strlen(cwd) + strlen("/");
121 /* allocate memory for filename and fill it */
123 filename = MNEW(char, filenamelen + cwdlen + strlen("/") +
127 strcpy(filename, cwd);
128 strcat(filename, "/");
129 strncat(filename, start, filenamelen);
131 /* add cwd length to file length */
132 filenamelen += cwdlen;
135 strncpy(filename, start, filenamelen);
136 filename[filenamelen] = '\0';
142 #if defined(ENABLE_ZLIB)
143 ht = zip_open(filename);
146 lce = NEW(list_classpath_entry);
148 lce->type = CLASSPATH_ARCHIVE;
150 lce->path = filename;
151 lce->pathlen = filenamelen;
153 /* SUN compatible -verbose:class output */
155 if (opt_verboseclass)
156 printf("[Opened %s]\n", filename);
160 os::abort("suck_add: zip/jar files not supported");
164 if (filename[filenamelen - 1] != '/') {/* XXX fixme for win32 */
165 filename[filenamelen] = '/';
166 filename[filenamelen + 1] = '\0';
170 lce = NEW(list_classpath_entry);
172 lce->type = CLASSPATH_PATH;
173 lce->path = filename;
174 lce->pathlen = filenamelen;
177 /* add current classpath entry, if no error */
183 /* goto next classpath entry, skip ':' delimiter */
194 * Adds a classpath form a property entry to the global classpath
197 void SuckClasspath::add_from_property(const char *key)
203 struct dirent **namelist;
209 // Get the property value.
210 Properties& properties = VM::get_current()->get_properties();
211 value = properties.get(key);
216 /* get the directory entries of the property */
218 for (start = value; (*start) != '\0'; ) {
220 /* search for ':' delimiter to get the end of the current entry */
222 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
227 /* allocate memory for the path entry */
229 pathlen = end - start;
230 char* path = MNEW(char, pathlen + strlen("0"));
232 /* copy and terminate the string */
234 strncpy(path, start, pathlen);
235 path[pathlen] = '\0';
237 /* Reset namelist to NULL for the freeing in an error case
242 /* scan the directory found for zip/jar files */
244 n = os::scandir((const char*) path, &namelist, &scandir_filter, (int (*)(const void*, const void*)) &alphasort);
246 /* On error, just continue, this should be ok. */
249 for (i = 0; i < n; i++) {
250 #if defined(_DIRENT_HAVE_D_NAMLEN)
251 namlen = namelist[i]->d_namlen;
253 namlen = strlen(namelist[i]->d_name);
256 /* Allocate memory for bootclasspath. */
258 // FIXME Make boot_class_path const char*.
259 char* boot_class_path = (char*) properties.get("sun.boot.class.path");
262 pathlen + strlen("/") + namlen +
264 strlen(boot_class_path) +
267 /* Prepend the file found to the bootclasspath. */
271 strcat(p, namelist[i]->d_name);
273 strcat(p, boot_class_path);
275 properties.put("sun.boot.class.path", p);
276 properties.put("java.boot.class.path", p);
278 MFREE(boot_class_path, char, strlen(boot_class_path));
280 /* free the memory allocated by scandir */
281 /* (We use `free` as the memory came from the C library.) */
287 /* On some systems (like Linux) when n == 0, then namelist
288 returned from scnadir is NULL, thus we don't have to
290 (Use `free` as the memory came from the C library.) */
292 if (namelist != NULL)
295 MFREE(path, char, pathlen + strlen("0"));
298 /* goto next entry, skip ':' delimiter */
308 /* suck_check_classbuffer_size *************************************************
310 Assert that at least <len> bytes are left to read <len> is limited
311 to the range of non-negative s4 values.
313 *******************************************************************************/
315 bool suck_check_classbuffer_size(classbuffer *cb, s4 len)
317 #ifdef ENABLE_VERIFIER
318 if (len < 0 || ((cb->data + cb->size) - cb->pos) < len) {
319 exceptions_throw_classformaterror(cb->clazz, "Truncated class file");
322 #endif /* ENABLE_VERIFIER */
328 u1 suck_u1(classbuffer *cb)
332 a = SUCK_BE_U1(cb->pos);
339 u2 suck_u2(classbuffer *cb)
343 a = SUCK_BE_U2(cb->pos);
350 u4 suck_u4(classbuffer *cb)
354 a = SUCK_BE_U4(cb->pos);
361 u8 suck_u8(classbuffer *cb)
365 a = SUCK_BE_U8(cb->pos);
372 float suck_float(classbuffer *cb)
376 #if WORDS_BIGENDIAN == 0
380 for (i = 0; i < 4; i++)
381 buffer[3 - i] = suck_u1(cb);
383 MCOPY((u1 *) (&f), buffer, u1, 4);
385 suck_nbytes((u1*) (&f), cb, 4);
388 assert(sizeof(float) == 4);
394 double suck_double(classbuffer *cb)
398 #if WORDS_BIGENDIAN == 0
402 # if defined(__ARM__) && defined(__ARMEL__) && !defined(__VFP_FP__)
404 * On little endian ARM processors when using FPA, word order
405 * of doubles is still big endian. So take that into account
406 * here. When using VFP, word order of doubles follows byte
407 * order. (michi 2005/07/24)
409 for (i = 0; i < 4; i++)
410 buffer[3 - i] = suck_u1(cb);
411 for (i = 0; i < 4; i++)
412 buffer[7 - i] = suck_u1(cb);
414 for (i = 0; i < 8; i++)
415 buffer[7 - i] = suck_u1(cb);
416 # endif /* defined(__ARM__) && ... */
418 MCOPY((u1 *) (&d), buffer, u1, 8);
420 suck_nbytes((u1*) (&d), cb, 8);
423 assert(sizeof(double) == 8);
429 /* suck_nbytes *****************************************************************
431 Transfer block of classfile data into a buffer.
433 *******************************************************************************/
435 void suck_nbytes(u1 *buffer, classbuffer *cb, s4 len)
437 MCOPY(buffer, cb->pos, u1, len);
442 /* suck_skip_nbytes ************************************************************
444 Skip block of classfile data.
446 *******************************************************************************/
448 void suck_skip_nbytes(classbuffer *cb, s4 len)
454 /* suck_start ******************************************************************
456 Returns true if classbuffer is already loaded or a file for the
457 specified class has succussfully been read in. All directories of
458 the searchpath are used to find the classfile (<classname>.class).
459 Returns NULL if no classfile is found and writes an error message.
461 *******************************************************************************/
463 classbuffer *suck_start(classinfo *c)
465 list_classpath_entry *lce;
474 /* initialize return value */
478 /* get the classname as char string (do it here for the warning at
479 the end of the function) */
481 filenamelen = utf_bytes(c->name) + strlen(".class") + strlen("0");
482 filename = MNEW(char, filenamelen);
484 utf_copy(filename, c->name);
485 strcat(filename, ".class");
487 // Get current list of classpath entries.
488 SuckClasspath& suckclasspath = VM::get_current()->get_suckclasspath();
490 /* walk through all classpath entries */
492 for (SuckClasspath::iterator it = suckclasspath.begin(); it != suckclasspath.end() && cb == NULL; it++) {
495 #if defined(ENABLE_ZLIB)
496 if (lce->type == CLASSPATH_ARCHIVE) {
498 /* enter a monitor on zip/jar archives */
502 /* try to get the file in current archive */
504 cb = zip_get(lce, c);
506 /* leave the monitor */
508 lce->mutex->unlock();
511 #endif /* defined(ENABLE_ZLIB) */
512 path = MNEW(char, lce->pathlen + filenamelen);
513 strcpy(path, lce->path);
514 strcat(path, filename);
516 classfile = os::fopen(path, "r");
518 if (classfile) { /* file exists */
519 if (!os::stat(path, &buffer)) { /* read classfile data */
520 cb = NEW(classbuffer);
522 cb->size = buffer.st_size;
523 cb->data = MNEW(u1, cb->size);
525 cb->path = lce->path;
527 /* read class data */
529 len = os::fread((void *) cb->data, 1, cb->size,
532 if (len != buffer.st_size) {
534 /* if (ferror(classfile)) { */
538 /* close the class file */
540 os::fclose(classfile);
544 MFREE(path, char, lce->pathlen + filenamelen);
545 #if defined(ENABLE_ZLIB)
552 dolog("Warning: Can not open class file '%s'", filename);
554 MFREE(filename, char, filenamelen);
560 /* suck_stop *******************************************************************
562 Frees memory for buffer with classfile data.
564 CAUTION: This function may only be called if buffer has been
565 allocated by suck_start with reading a file.
567 *******************************************************************************/
569 void suck_stop(classbuffer *cb)
573 MFREE(cb->data, u1, cb->size);
574 FREE(cb, classbuffer);
579 * These are local overrides for various environment variables in Emacs.
580 * Please do not remove this and leave it at the end of the file, where
581 * Emacs will automagically detect them.
582 * ---------------------------------------------------------------------
585 * indent-tabs-mode: t
589 * vim:noexpandtab:sw=4:ts=4: