1 /* src/vm/suck.c - functions to read LE ordered types from a buffer
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
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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Christian Thalinger
31 $Id: suck.c 4095 2006-01-08 21:32:25Z twisti $
43 #include "mm/memory.h"
44 #include "toolbox/list.h"
45 #include "toolbox/logging.h"
46 #include "toolbox/util.h"
47 #include "vm/exceptions.h"
48 #include "vm/loader.h"
49 #include "vm/options.h"
50 #include "vm/properties.h"
51 #include "vm/stringlocal.h"
56 /* global variables ***********************************************************/
58 char *bootclasspath; /* contains the boot classpath */
59 char *classpath; /* contains the classpath */
61 list *list_classpath_entries;
64 /* suck_init *******************************************************************
66 Initializes the suck subsystem like initializing the classpath
69 *******************************************************************************/
73 list_classpath_entries = NEW(list);
75 list_init(list_classpath_entries, OFFSET(list_classpath_entry, linkage));
83 /* scandir_filter **************************************************************
85 Filters for zip/jar files.
87 *******************************************************************************/
89 static int scandir_filter(const struct dirent *a)
93 #if defined(_DIRENT_HAVE_D_NAMLEN)
96 namlen = strlen(a->d_name);
99 if ((strncasecmp(a->d_name + namlen - 4, ".zip", 4) == 0) ||
100 (strncasecmp(a->d_name + namlen - 4, ".jar", 4) == 0))
107 /* suck_add ********************************************************************
109 Adds a classpath to the global classpath entries list.
111 *******************************************************************************/
113 void suck_add(char *classpath)
115 list_classpath_entry *lce;
125 /* parse the classpath string */
127 for (start = classpath; (*start) != '\0'; ) {
129 /* search for ':' delimiter to get the end of the current entry */
130 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
134 filenamelen = end - start;
136 if (filenamelen > 4) {
137 if ((strncasecmp(end - 4, ".zip", 4) == 0) ||
138 (strncasecmp(end - 4, ".jar", 4) == 0)) {
143 /* save classpath entries as absolute pathnames */
148 if (*start != '/') { /* XXX fix me for win32 */
150 cwdlen = strlen(cwd) + strlen("/");
153 /* allocate memory for filename and fill it */
155 filename = MNEW(char, filenamelen + cwdlen + strlen("/") +
159 strcpy(filename, cwd);
160 strcat(filename, "/");
161 strncat(filename, start, filenamelen);
163 /* add cwd length to file length */
164 filenamelen += cwdlen;
167 strncpy(filename, start, filenamelen);
168 filename[filenamelen] = '\0';
174 #if defined(ENABLE_ZLIB)
175 ht = zip_open(filename);
178 lce = NEW(list_classpath_entry);
180 lce->type = CLASSPATH_ARCHIVE;
182 lce->path = filename;
183 lce->pathlen = filenamelen;
185 /* SUN compatible -verbose:class output */
187 if (opt_verboseclass)
188 printf("[Opened %s]\n", filename);
192 throw_cacao_exception_exit(string_java_lang_InternalError,
193 "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_addlast(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 *tmpbootclasspath;
246 /* get the property value */
248 value = properties_get(key);
253 /* get the directory entries of the property */
255 for (start = value; (*start) != '\0'; ) {
257 /* search for ':' delimiter to get the end of the current entry */
259 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
264 /* allocate memory for the path entry */
266 pathlen = end - start;
267 path = MNEW(char, pathlen + strlen("0"));
269 /* copy and terminate the string */
271 strncpy(path, start, pathlen);
272 path[pathlen] = '\0';
274 /* scan the directory found for zip/jar files */
276 n = scandir(path, &namelist, scandir_filter, alphasort);
278 /* on error, just continue, this should be ok */
281 for (i = 0; i < n; i++) {
282 #if defined(_DIRENT_HAVE_D_NAMLEN)
283 namlen = namelist[i]->d_namlen;
285 namlen = strlen(namelist[i]->d_name);
288 /* reallocate memory for bootclasspath */
290 tmpbootclasspath = MNEW(char,
291 pathlen + strlen("/") + namlen +
293 strlen(bootclasspath) +
296 /* prepend the file found to bootclasspath */
298 strcpy(tmpbootclasspath, path);
299 strcat(tmpbootclasspath, "/");
300 strcat(tmpbootclasspath, namelist[i]->d_name);
301 strcat(tmpbootclasspath, ":");
303 strcat(tmpbootclasspath, bootclasspath);
305 /* free old bootclasspath memory */
307 MFREE(bootclasspath, u1, strlen(bootclasspath));
309 /* and set the new bootclasspath */
311 bootclasspath = tmpbootclasspath;
313 /* free the memory allocated by scandir */
315 FREE(namelist[i], struct dirent);
318 FREE(namelist, struct dirent);
321 MFREE(path, char, pathlen + strlen("0"));
324 /* goto next entry, skip ':' delimiter */
334 /* suck_check_classbuffer_size *************************************************
336 Assert that at least <len> bytes are left to read <len> is limited
337 to the range of non-negative s4 values.
339 *******************************************************************************/
341 bool suck_check_classbuffer_size(classbuffer *cb, s4 len)
343 #ifdef ENABLE_VERIFIER
344 if (len < 0 || ((cb->data + cb->size) - cb->pos) < len) {
346 new_classformaterror((cb)->class, "Truncated class file");
350 #endif /* ENABLE_VERIFIER */
356 u1 suck_u1(classbuffer *cb)
360 a = SUCK_BE_U1(cb->pos);
367 u2 suck_u2(classbuffer *cb)
371 a = SUCK_BE_U2(cb->pos);
378 u4 suck_u4(classbuffer *cb)
382 a = SUCK_BE_U4(cb->pos);
389 u8 suck_u8(classbuffer *cb)
391 #if U8_AVAILABLE == 1
394 a = SUCK_BE_U8(cb->pos);
401 v.high = suck_u4(cb);
409 float suck_float(classbuffer *cb)
413 #if WORDS_BIGENDIAN == 0
417 for (i = 0; i < 4; i++)
418 buffer[3 - i] = suck_u1(cb);
420 MCOPY((u1 *) (&f), buffer, u1, 4);
422 suck_nbytes((u1*) (&f), cb, 4);
425 if (sizeof(float) != 4) {
426 *exceptionptr = new_internalerror("Incompatible float-format");
428 /* XXX should we exit in such a case? */
429 throw_exception_exit();
436 double suck_double(classbuffer *cb)
440 #if WORDS_BIGENDIAN == 0
444 #if defined(__ARM__) && defined(__ARMEL__) && !defined(__VFP_FP__)
446 * On little endian ARM processors when using FPA, word order
447 * of doubles is still big endian. So take that into account
448 * here. When using VFP, word order of doubles follows byte
449 * order. (michi 2005/07/24)
451 for (i = 0; i < 4; i++)
452 buffer[3 - i] = suck_u1(cb);
453 for (i = 0; i < 4; i++)
454 buffer[7 - i] = suck_u1(cb);
456 for (i = 0; i < 8; i++)
457 buffer[7 - i] = suck_u1(cb);
458 #endif /* defined(__ARM__) && ... */
460 MCOPY((u1 *) (&d), buffer, u1, 8);
462 suck_nbytes((u1*) (&d), cb, 8);
465 if (sizeof(double) != 8) {
466 *exceptionptr = new_internalerror("Incompatible double-format");
468 /* XXX should we exit in such a case? */
469 throw_exception_exit();
476 /* suck_nbytes *****************************************************************
478 Transfer block of classfile data into a buffer.
480 *******************************************************************************/
482 void suck_nbytes(u1 *buffer, classbuffer *cb, s4 len)
484 MCOPY(buffer, cb->pos, u1, len);
489 /* suck_skip_nbytes ************************************************************
491 Skip block of classfile data.
493 *******************************************************************************/
495 void suck_skip_nbytes(classbuffer *cb, s4 len)
501 /* suck_start ******************************************************************
503 Returns true if classbuffer is already loaded or a file for the
504 specified class has succussfully been read in. All directories of
505 the searchpath are used to find the classfile (<classname>.class).
506 Returns NULL if no classfile is found and writes an error message.
508 *******************************************************************************/
510 classbuffer *suck_start(classinfo *c)
512 list_classpath_entry *lce;
521 /* initialize return value */
525 /* get the classname as char string (do it here for the warning at
526 the and of the function) */
528 filenamelen = utf_strlen(c->name) + strlen(".class") + strlen("0");
529 filename = MNEW(char, filenamelen);
531 utf_sprint(filename, c->name);
532 strcat(filename, ".class");
534 /* walk through all classpath entries */
536 for (lce = list_first(list_classpath_entries); lce != NULL && cb == NULL;
537 lce = list_next(list_classpath_entries, lce)) {
538 #if defined(ENABLE_ZLIB)
539 if (lce->type == CLASSPATH_ARCHIVE) {
541 #if defined(USE_THREADS)
542 /* enter a monitor on zip/jar archives */
544 builtin_monitorenter((java_objectheader *) lce);
547 /* try to get the file in current archive */
549 cb = zip_get(lce, c);
551 #if defined(USE_THREADS)
552 /* leave the monitor */
554 builtin_monitorexit((java_objectheader *) lce);
558 #endif /* defined(ENABLE_ZLIB) */
559 path = MNEW(char, lce->pathlen + filenamelen);
560 strcpy(path, lce->path);
561 strcat(path, filename);
563 classfile = fopen(path, "r");
565 if (classfile) { /* file exists */
566 if (!stat(path, &buffer)) { /* read classfile data */
567 cb = NEW(classbuffer);
569 cb->size = buffer.st_size;
570 cb->data = MNEW(u1, cb->size);
572 cb->path = lce->path;
574 /* read class data */
575 len = fread(cb->data, 1, cb->size, classfile);
577 if (len != buffer.st_size) {
579 /* if (ferror(classfile)) { */
585 MFREE(path, char, lce->pathlen + filenamelen);
586 #if defined(ENABLE_ZLIB)
593 dolog("Warning: Can not open class file '%s'", filename);
595 MFREE(filename, char, filenamelen);
601 /* suck_stop *******************************************************************
603 Frees memory for buffer with classfile data.
605 CAUTION: This function may only be called if buffer has been
606 allocated by suck_start with reading a file.
608 *******************************************************************************/
610 void suck_stop(classbuffer *cb)
614 MFREE(cb->data, u1, cb->size);
615 FREE(cb, classbuffer);
620 * These are local overrides for various environment variables in Emacs.
621 * Please do not remove this and leave it at the end of the file, where
622 * Emacs will automagically detect them.
623 * ---------------------------------------------------------------------
626 * indent-tabs-mode: t