1 /* src/vmcore/suck.c - functions to read LE ordered types from a buffer
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: suck.c 7783 2007-04-20 13:28:27Z twisti $
41 #include "mm/memory.h"
43 #if defined(ENABLE_THREADS)
44 # include "threads/native/lock.h"
46 # include "threads/none/lock.h"
49 #include "toolbox/list.h"
50 #include "toolbox/logging.h"
51 #include "toolbox/util.h"
53 #include "vm/exceptions.h"
54 #include "vm/properties.h"
57 #include "vmcore/loader.h"
58 #include "vmcore/options.h"
59 #include "vmcore/suck.h"
60 #include "vmcore/zip.h"
63 /* global variables ***********************************************************/
65 list_t *list_classpath_entries;
68 /* suck_init *******************************************************************
70 Initializes the suck subsystem like initializing the classpath
73 *******************************************************************************/
77 list_classpath_entries = list_create(OFFSET(list_classpath_entry, linkage));
85 /* scandir_filter **************************************************************
87 Filters for zip/jar files.
89 *******************************************************************************/
91 #if defined(__LINUX__)
92 static int scandir_filter(const struct dirent *a)
94 static int scandir_filter(struct dirent *a)
99 #if defined(_DIRENT_HAVE_D_NAMLEN)
100 namlen = a->d_namlen;
102 namlen = strlen(a->d_name);
105 if ((strncasecmp(a->d_name + namlen - 4, ".zip", 4) == 0) ||
106 (strncasecmp(a->d_name + namlen - 4, ".jar", 4) == 0))
113 /* suck_add ********************************************************************
115 Adds a classpath to the global classpath entries list.
117 *******************************************************************************/
119 void suck_add(char *classpath)
121 list_classpath_entry *lce;
129 #if defined(ENABLE_ZLIB)
133 /* parse the classpath string */
135 for (start = classpath; (*start) != '\0'; ) {
137 /* search for ':' delimiter to get the end of the current entry */
138 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
142 filenamelen = end - start;
144 if (filenamelen > 4) {
145 if ((strncasecmp(end - 4, ".zip", 4) == 0) ||
146 (strncasecmp(end - 4, ".jar", 4) == 0)) {
151 /* save classpath entries as absolute pathnames */
156 if (*start != '/') { /* XXX fix me for win32 */
158 cwdlen = strlen(cwd) + strlen("/");
161 /* allocate memory for filename and fill it */
163 filename = MNEW(char, filenamelen + cwdlen + strlen("/") +
167 strcpy(filename, cwd);
168 strcat(filename, "/");
169 strncat(filename, start, filenamelen);
171 /* add cwd length to file length */
172 filenamelen += cwdlen;
175 strncpy(filename, start, filenamelen);
176 filename[filenamelen] = '\0';
182 #if defined(ENABLE_ZLIB)
183 ht = zip_open(filename);
186 lce = NEW(list_classpath_entry);
188 lce->type = CLASSPATH_ARCHIVE;
190 lce->path = filename;
191 lce->pathlen = filenamelen;
193 /* SUN compatible -verbose:class output */
195 if (opt_verboseclass)
196 printf("[Opened %s]\n", filename);
200 vm_abort("suck_add: zip/jar files not supported");
204 if (filename[filenamelen - 1] != '/') {/* XXX fixme for win32 */
205 filename[filenamelen] = '/';
206 filename[filenamelen + 1] = '\0';
210 lce = NEW(list_classpath_entry);
212 lce->type = CLASSPATH_PATH;
213 lce->path = filename;
214 lce->pathlen = filenamelen;
217 /* add current classpath entry, if no error */
220 list_add_last(list_classpath_entries, lce);
223 /* goto next classpath entry, skip ':' delimiter */
233 /* suck_add_from_property ******************************************************
235 Adds a classpath form a property entry to the global classpath
238 *******************************************************************************/
240 void suck_add_from_property(char *key)
247 struct dirent **namelist;
251 char *tmpbootclasspath;
253 /* get the property value */
255 value = properties_get(key);
260 /* get the directory entries of the property */
262 for (start = value; (*start) != '\0'; ) {
264 /* search for ':' delimiter to get the end of the current entry */
266 for (end = start; ((*end) != '\0') && ((*end) != ':'); end++);
271 /* allocate memory for the path entry */
273 pathlen = end - start;
274 path = MNEW(char, pathlen + strlen("0"));
276 /* copy and terminate the string */
278 strncpy(path, start, pathlen);
279 path[pathlen] = '\0';
281 /* Reset namelist to NULL for the freeing in an error case
286 /* scan the directory found for zip/jar files */
288 n = scandir(path, &namelist, scandir_filter, alphasort);
290 /* On error, just continue, this should be ok. */
293 for (i = 0; i < n; i++) {
294 #if defined(_DIRENT_HAVE_D_NAMLEN)
295 namlen = namelist[i]->d_namlen;
297 namlen = strlen(namelist[i]->d_name);
300 /* reallocate memory for bootclasspath */
302 tmpbootclasspath = MNEW(char,
303 pathlen + strlen("/") + namlen +
305 strlen(_Jv_bootclasspath) +
308 /* prepend the file found to bootclasspath */
310 strcpy(tmpbootclasspath, path);
311 strcat(tmpbootclasspath, "/");
312 strcat(tmpbootclasspath, namelist[i]->d_name);
313 strcat(tmpbootclasspath, ":");
315 strcat(tmpbootclasspath, _Jv_bootclasspath);
317 /* free old bootclasspath memory */
319 MFREE(_Jv_bootclasspath, u1, strlen(_Jv_bootclasspath));
321 /* and set the new bootclasspath */
323 _Jv_bootclasspath = tmpbootclasspath;
325 /* free the memory allocated by scandir */
326 /* (We use `free` as the memory came from the C library.) */
332 /* On some systems (like Linux) when n == 0, then namelist
333 returned from scnadir is NULL, thus we don't have to
335 (Use `free` as the memory came from the C library.) */
337 if (namelist != NULL)
340 MFREE(path, char, pathlen + strlen("0"));
343 /* goto next entry, skip ':' delimiter */
353 /* suck_check_classbuffer_size *************************************************
355 Assert that at least <len> bytes are left to read <len> is limited
356 to the range of non-negative s4 values.
358 *******************************************************************************/
360 bool suck_check_classbuffer_size(classbuffer *cb, s4 len)
362 #ifdef ENABLE_VERIFIER
363 if (len < 0 || ((cb->data + cb->size) - cb->pos) < len) {
364 exceptions_throw_classformaterror((cb)->class, "Truncated class file");
367 #endif /* ENABLE_VERIFIER */
373 u1 suck_u1(classbuffer *cb)
377 a = SUCK_BE_U1(cb->pos);
384 u2 suck_u2(classbuffer *cb)
388 a = SUCK_BE_U2(cb->pos);
395 u4 suck_u4(classbuffer *cb)
399 a = SUCK_BE_U4(cb->pos);
406 u8 suck_u8(classbuffer *cb)
408 #if U8_AVAILABLE == 1
411 a = SUCK_BE_U8(cb->pos);
418 v.high = suck_u4(cb);
426 float suck_float(classbuffer *cb)
430 #if WORDS_BIGENDIAN == 0
434 for (i = 0; i < 4; i++)
435 buffer[3 - i] = suck_u1(cb);
437 MCOPY((u1 *) (&f), buffer, u1, 4);
439 suck_nbytes((u1*) (&f), cb, 4);
442 assert(sizeof(float) == 4);
448 double suck_double(classbuffer *cb)
452 #if WORDS_BIGENDIAN == 0
456 # if defined(__ARM__) && defined(__ARMEL__) && !defined(__VFP_FP__)
458 * On little endian ARM processors when using FPA, word order
459 * of doubles is still big endian. So take that into account
460 * here. When using VFP, word order of doubles follows byte
461 * order. (michi 2005/07/24)
463 for (i = 0; i < 4; i++)
464 buffer[3 - i] = suck_u1(cb);
465 for (i = 0; i < 4; i++)
466 buffer[7 - i] = suck_u1(cb);
468 for (i = 0; i < 8; i++)
469 buffer[7 - i] = suck_u1(cb);
470 # endif /* defined(__ARM__) && ... */
472 MCOPY((u1 *) (&d), buffer, u1, 8);
474 suck_nbytes((u1*) (&d), cb, 8);
477 assert(sizeof(double) == 8);
483 /* suck_nbytes *****************************************************************
485 Transfer block of classfile data into a buffer.
487 *******************************************************************************/
489 void suck_nbytes(u1 *buffer, classbuffer *cb, s4 len)
491 MCOPY(buffer, cb->pos, u1, len);
496 /* suck_skip_nbytes ************************************************************
498 Skip block of classfile data.
500 *******************************************************************************/
502 void suck_skip_nbytes(classbuffer *cb, s4 len)
508 /* suck_start ******************************************************************
510 Returns true if classbuffer is already loaded or a file for the
511 specified class has succussfully been read in. All directories of
512 the searchpath are used to find the classfile (<classname>.class).
513 Returns NULL if no classfile is found and writes an error message.
515 *******************************************************************************/
517 classbuffer *suck_start(classinfo *c)
519 list_classpath_entry *lce;
528 /* initialize return value */
532 /* get the classname as char string (do it here for the warning at
533 the end of the function) */
535 filenamelen = utf_bytes(c->name) + strlen(".class") + strlen("0");
536 filename = MNEW(char, filenamelen);
538 utf_copy(filename, c->name);
539 strcat(filename, ".class");
541 /* walk through all classpath entries */
543 for (lce = list_first(list_classpath_entries); lce != NULL && cb == NULL;
544 lce = list_next(list_classpath_entries, lce)) {
545 #if defined(ENABLE_ZLIB)
546 if (lce->type == CLASSPATH_ARCHIVE) {
548 /* enter a monitor on zip/jar archives */
550 LOCK_MONITOR_ENTER(lce);
552 /* try to get the file in current archive */
554 cb = zip_get(lce, c);
556 /* leave the monitor */
558 LOCK_MONITOR_EXIT(lce);
561 #endif /* defined(ENABLE_ZLIB) */
562 path = MNEW(char, lce->pathlen + filenamelen);
563 strcpy(path, lce->path);
564 strcat(path, filename);
566 classfile = fopen(path, "r");
568 if (classfile) { /* file exists */
569 if (!stat(path, &buffer)) { /* read classfile data */
570 cb = NEW(classbuffer);
572 cb->size = buffer.st_size;
573 cb->data = MNEW(u1, cb->size);
575 cb->path = lce->path;
577 /* read class data */
579 len = fread(cb->data, 1, cb->size, classfile);
581 if (len != buffer.st_size) {
583 /* if (ferror(classfile)) { */
587 /* close the class file */
593 MFREE(path, char, lce->pathlen + filenamelen);
594 #if defined(ENABLE_ZLIB)
601 dolog("Warning: Can not open class file '%s'", filename);
603 MFREE(filename, char, filenamelen);
609 /* suck_stop *******************************************************************
611 Frees memory for buffer with classfile data.
613 CAUTION: This function may only be called if buffer has been
614 allocated by suck_start with reading a file.
616 *******************************************************************************/
618 void suck_stop(classbuffer *cb)
622 MFREE(cb->data, u1, cb->size);
623 FREE(cb, classbuffer);
628 * These are local overrides for various environment variables in Emacs.
629 * Please do not remove this and leave it at the end of the file, where
630 * Emacs will automagically detect them.
631 * ---------------------------------------------------------------------
634 * indent-tabs-mode: t