1 /* Utility routines for finding and reading Java(TM) .class files.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GNU CC; see the file COPYING. If not, write to
16 the Free Software Foundation, 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
19 Java and all Java-based marks are trademarks or registered trademarks
20 of Sun Microsystems, Inc. in the United States and other countries.
21 The Free Software Foundation is independent of Sun Microsystems, Inc. */
23 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
31 #include "java-tree.h"
40 /* DOS brain-damage */
42 #define O_BINARY 0 /* MS-DOS brain-damage */
46 DEFUN(jcf_unexpected_eof
, (jcf
, count
),
47 JCF
*jcf AND
int count ATTRIBUTE_UNUSED
)
50 fprintf (stderr
, "Premature end of .class file %s.\n", jcf
->filename
);
52 fprintf (stderr
, "Premature end of .class file <stdin>.\n");
57 DEFUN(jcf_trim_old_input
, (jcf
),
60 int count
= jcf
->read_ptr
- jcf
->buffer
;
63 memmove (jcf
->buffer
, jcf
->read_ptr
, jcf
->read_end
- jcf
->read_ptr
);
64 jcf
->read_ptr
-= count
;
65 jcf
->read_end
-= count
;
70 DEFUN(jcf_filbuf_from_stdio
, (jcf
, count
),
71 JCF
*jcf AND
int count
)
73 FILE *file
= (FILE*) (jcf
->read_state
);
74 if (count
> jcf
->buffer_end
- jcf
->read_ptr
)
76 JCF_u4 old_read_ptr
= jcf
->read_ptr
- jcf
->buffer
;
77 JCF_u4 old_read_end
= jcf
->read_end
- jcf
->buffer
;
78 JCF_u4 old_size
= jcf
->buffer_end
- jcf
->buffer
;
79 JCF_u4 new_size
= (old_size
== 0 ? 2000 : 2 * old_size
) + count
;
80 unsigned char *new_buffer
= jcf
->buffer
== NULL
? ALLOC (new_size
)
81 : REALLOC (jcf
->buffer
, new_size
);
82 jcf
->buffer
= new_buffer
;
83 jcf
->buffer_end
= new_buffer
+ new_size
;
84 jcf
->read_ptr
= new_buffer
+ old_read_ptr
;
85 jcf
->read_end
= new_buffer
+ old_read_end
;
87 count
-= jcf
->read_end
- jcf
->read_ptr
;
90 if ((int) fread (jcf
->read_end
, 1, count
, file
) != count
)
91 jcf_unexpected_eof (jcf
, count
);
92 jcf
->read_end
+= count
;
98 struct ZipFile
*SeenZipFiles
= NULL
;
100 /* Open a zip file with the given name, and cache directory and file
101 descriptor. If the file is missing, treat it as an empty archive.
102 Return NULL if the .zip file is malformed.
106 DEFUN(opendir_in_zip
, (zipfile
, is_system
),
107 const char *zipfile AND
int is_system
)
109 struct ZipFile
* zipf
;
112 for (zipf
= SeenZipFiles
; zipf
!= NULL
; zipf
= zipf
->next
)
114 if (strcmp (zipf
->name
, zipfile
) == 0)
118 zipf
= ALLOC (sizeof (struct ZipFile
) + strlen (zipfile
) + 1);
119 zipf
->next
= SeenZipFiles
;
120 zipf
->name
= (char*)(zipf
+1);
121 strcpy (zipf
->name
, zipfile
);
123 fd
= open (zipfile
, O_RDONLY
| O_BINARY
);
127 /* A missing zip file is not considered an error.
128 We may want to re-consider that. FIXME. */
131 zipf
->central_directory
= NULL
;
135 jcf_dependency_add_file (zipfile
, is_system
);
136 if (read (fd
, magic
, 4) != 4 || GET_u4 (magic
) != (JCF_u4
)ZIPMAGIC
)
138 lseek (fd
, 0L, SEEK_SET
);
139 if (read_zip_archive (zipf
) != 0)
146 0: OK - zipmember found.
148 -2: Malformed archive.
152 DEFUN(open_in_zip
, (jcf
, zipfile
, zipmember
, is_system
),
153 JCF
*jcf AND
const char *zipfile AND
const char *zipmember
158 ZipFile
*zipf
= opendir_in_zip (zipfile
, is_system
);
166 len
= strlen (zipmember
);
168 zipd
= (struct ZipDirectory
*) zipf
->central_directory
;
169 for (i
= 0; i
< zipf
->count
; i
++, zipd
= ZIPDIR_NEXT (zipd
))
171 if (len
== zipd
->filename_length
&&
172 strncmp (ZIPDIR_FILENAME (zipd
), zipmember
, len
) == 0)
176 jcf
->filename
= xstrdup (zipfile
);
177 jcf
->classname
= xstrdup (zipmember
);
178 return read_zip_member(jcf
, zipd
, zipf
);
184 /* Read data from zip archive member. */
187 DEFUN(read_zip_member
, (jcf
, zipd
, zipf
),
188 JCF
*jcf AND ZipDirectory
*zipd AND ZipFile
*zipf
)
190 jcf
->filbuf
= jcf_unexpected_eof
;
191 jcf
->zipd
= (void *)zipd
;
193 if (zipd
->compression_method
== Z_NO_COMPRESSION
)
195 jcf
->buffer
= ALLOC (zipd
->size
);
196 jcf
->buffer_end
= jcf
->buffer
+ zipd
->size
;
197 jcf
->read_ptr
= jcf
->buffer
;
198 jcf
->read_end
= jcf
->buffer_end
;
199 if (lseek (zipf
->fd
, zipd
->filestart
, 0) < 0
200 || read (zipf
->fd
, jcf
->buffer
, zipd
->size
) != (long) zipd
->size
)
206 z_stream d_stream
; /* decompression stream */
207 d_stream
.zalloc
= (alloc_func
) 0;
208 d_stream
.zfree
= (free_func
) 0;
209 d_stream
.opaque
= (voidpf
) 0;
211 jcf
->buffer
= ALLOC (zipd
->uncompressed_size
);
212 d_stream
.next_out
= jcf
->buffer
;
213 d_stream
.avail_out
= zipd
->uncompressed_size
;
214 jcf
->buffer_end
= jcf
->buffer
+ zipd
->uncompressed_size
;
215 jcf
->read_ptr
= jcf
->buffer
;
216 jcf
->read_end
= jcf
->buffer_end
;
217 buffer
= ALLOC (zipd
->size
);
218 d_stream
.next_in
= buffer
;
219 d_stream
.avail_in
= zipd
->size
;
220 if (lseek (zipf
->fd
, zipd
->filestart
, 0) < 0
221 || read (zipf
->fd
, buffer
, zipd
->size
) != (long) zipd
->size
)
223 /* Handle NO_HEADER using undocumented zlib feature.
224 This is a very common hack. */
225 inflateInit2 (&d_stream
, -MAX_WBITS
);
226 inflate (&d_stream
, Z_NO_FLUSH
);
227 inflateEnd (&d_stream
);
235 DEFUN(open_class
, (filename
, jcf
, fd
, dep_name
),
236 const char *filename AND JCF
*jcf AND
int fd AND
const char *dep_name
)
240 struct stat stat_buf
;
241 if (fstat (fd
, &stat_buf
) != 0
242 || ! S_ISREG (stat_buf
.st_mode
))
244 perror ("Could not figure length of .class file");
247 if (dep_name
!= NULL
)
248 jcf_dependency_add_file (dep_name
, 0);
250 jcf
->buffer
= ALLOC (stat_buf
.st_size
);
251 jcf
->buffer_end
= jcf
->buffer
+ stat_buf
.st_size
;
252 jcf
->read_ptr
= jcf
->buffer
;
253 jcf
->read_end
= jcf
->buffer_end
;
254 jcf
->read_state
= NULL
;
255 jcf
->filename
= filename
;
256 if (read (fd
, jcf
->buffer
, stat_buf
.st_size
) != stat_buf
.st_size
)
258 perror ("Failed to read .class file");
262 jcf
->filbuf
= jcf_unexpected_eof
;
271 DEFUN(find_classfile
, (filename
, jcf
, dep_name
),
272 char *filename AND JCF
*jcf AND
const char *dep_name
)
274 int fd
= open (filename
, O_RDONLY
| O_BINARY
);
277 return open_class (filename
, jcf
, fd
, dep_name
);
282 /* A comparison function (as for qsort) that compares KEY (a char *
283 giving the basename of a file) with the name stored in ENTRY (a
287 DEFUN(compare_path
, (key
, entry
),
288 const void *key AND
const void *entry
)
290 return strcmp ((const char *) key
,
291 (*((const struct dirent
**) entry
))->d_name
);
294 /* Returns nonzero if ENTRY names a .java or .class file. */
297 DEFUN(java_or_class_file
, (entry
),
298 const struct dirent
*entry
)
300 const char *base
= basename (entry
->d_name
);
301 return (fnmatch ("*.java", base
, 0) == 0 ||
302 fnmatch ("*.class", base
, 0) == 0);
305 /* Information about the files present in a particular directory. */
306 typedef struct memoized_dirlist_entry
308 /* The name of the directory. */
310 /* The number of .java and .class files present, or -1 if we could
311 not, for some reason, obtain the list. */
313 /* The .java and .class files in the directory, in alphabetical
315 struct dirent
**files
;
316 } memoized_dirlist_entry
;
318 /* Returns true if ENTRY (a memoized_dirlist_entry *) correponds to
319 the directory given by KEY (a char *) giving the directory
323 DEFUN(memoized_dirlist_lookup_eq
, (entry
, key
),
324 const void *entry AND
const void *key
)
326 return strcmp ((const char *) key
,
327 ((const memoized_dirlist_entry
*) entry
)->dir
) == 0;
330 /* A hash table mapping directory names to the lists of .java and
331 .class files in that directory. */
333 static htab_t memoized_dirlists
;
337 /* Like stat, but avoids actually making the stat system call if we
338 know that it cannot succeed. FILENAME and BUF are as for stat. */
341 DEFUN(caching_stat
, (filename
, buf
),
342 char *filename AND
struct stat
*buf
)
347 memoized_dirlist_entry
*dent
;
350 /* If the hashtable has not already been created, create it now. */
351 if (!memoized_dirlists
)
352 memoized_dirlists
= htab_create (37,
354 memoized_dirlist_lookup_eq
,
357 /* Get the name of the directory. */
358 sep
= strrchr (filename
, DIR_SEPARATOR
);
367 /* Obtain the entry for this directory form the hash table. */
368 slot
= htab_find_slot (memoized_dirlists
, filename
, INSERT
);
371 /* We have not already scanned this directory; scan it now. */
372 dent
= ((memoized_dirlist_entry
*)
373 ALLOC (sizeof (memoized_dirlist_entry
)));
374 dent
->dir
= xstrdup (filename
);
375 /* Unfortunately, scandir is not fully standardized. In
376 particular, the type of the function pointer passed as the
377 third argument sometimes takes a "const struct dirent *"
378 parameter, and sometimes just a "struct dirent *". We rely
379 on the ability to interchange these two types of function
381 dent
->num_files
= scandir (filename
, &dent
->files
,
387 dent
= *((memoized_dirlist_entry
**) slot
);
389 /* Put the spearator back. */
391 *sep
= DIR_SEPARATOR
;
393 /* If the file is not in the list, there is no need to stat it; it
395 if (dent
->num_files
!= -1
396 && !bsearch (base
, dent
->files
, dent
->num_files
,
397 sizeof (struct dirent
*), compare_path
))
401 return stat (filename
, buf
);
404 /* Returns 1 if the CLASSNAME (really a char *) matches the name
405 stored in TABLE_ENTRY (also a char *). */
408 DEFUN(memoized_class_lookup_eq
, (table_entry
, classname
),
409 const void *table_entry AND
const void *classname
)
411 return strcmp ((const char *)classname
, (const char *)table_entry
) == 0;
414 /* A hash table keeping track of class names that were not found
415 during class lookup. (There is no need to cache the values
416 associated with names that were found; they are saved in
417 IDENTIFIER_CLASS_VALUE.) */
418 static htab_t memoized_class_lookups
;
420 /* Returns a freshly malloc'd string with the fully qualified pathname
421 of the .class file for the class CLASSNAME. CLASSNAME must be
422 allocated in permanent storage; this function may retain a pointer
423 to it. Returns NULL on failure. If JCF != NULL, it is suitably
424 initialized. SOURCE_OK is true if we should also look for .java
428 DEFUN(find_class
, (classname
, classname_length
, jcf
, source_ok
),
429 const char *classname AND
int classname_length AND JCF
*jcf AND
int source_ok
)
433 int i
, k
, java
= -1, class = -1;
434 struct stat java_buf
, class_buf
;
442 /* Create the hash table, if it does not already exist. */
443 if (!memoized_class_lookups
)
444 memoized_class_lookups
= htab_create (37,
446 memoized_class_lookup_eq
,
449 /* Loop for this class in the hashtable. If it is present, we've
450 already looked for this class and failed to find it. */
451 hash
= htab_hash_string (classname
);
452 if (htab_find_with_hash (memoized_class_lookups
, classname
, hash
))
455 /* Allocate and zero out the buffer, since we don't explicitly put a
456 null pointer when we're copying it below. */
457 buflen
= jcf_path_max_len () + classname_length
+ 10;
458 buffer
= ALLOC (buflen
);
459 memset (buffer
, 0, buflen
);
461 java_buffer
= alloca (buflen
);
463 jcf
->java_source
= 0;
465 for (entry
= jcf_path_start (); entry
!= NULL
; entry
= jcf_path_next (entry
))
467 const char *path_name
= jcf_path_name (entry
);
472 strcpy (buffer
, path_name
);
475 /* This is right because we know that `.zip' entries will have a
476 trailing slash. See jcf-path.c. */
479 for (k
= 0; k
< classname_length
; k
++, i
++)
481 char ch
= classname
[k
];
482 buffer
[i
] = ch
== '.' ? '/' : ch
;
484 strcpy (buffer
+i
, ".class");
486 if (jcf_path_is_zipfile (entry
))
490 buffer
[dir_len
] = '\0';
491 SOURCE_FRONTEND_DEBUG
492 (("Trying [...%s]:%s",
493 &buffer
[dir_len
-(dir_len
> 15 ? 15 : dir_len
)],
497 err_code
= open_in_zip (jcf
, buffer
, buffer
+dir_len
+1,
498 jcf_path_is_system (entry
));
501 /* Should we check if .zip is out-of-date wrt .java? */
502 buffer
[dir_len
] = '(';
503 strcpy (buffer
+i
, ".class)");
511 class = caching_stat(buffer
, &class_buf
);
516 /* Compute name of .java file. */
518 strcpy (java_buffer
, path_name
);
519 l
= strlen (java_buffer
);
520 for (m
= 0; m
< classname_length
; ++m
)
521 java_buffer
[m
+ l
] = (classname
[m
] == '.' ? '/' : classname
[m
]);
522 strcpy (java_buffer
+ m
+ l
, ".java");
523 java
= caching_stat (java_buffer
, &java_buf
);
529 /* We preferably pick a class file if we have a chance. If the source
530 file is newer than the class file, we issue a warning and parse the
532 There should be a flag to allow people have the class file picked
533 up no matter what. FIXME. */
534 if (! java
&& ! class && java_buf
.st_mtime
> class_buf
.st_mtime
)
537 warning ("source file for class `%s' is newer than its matching class file. Source file `%s' used instead", classname
, java_buffer
);
542 dep_file
= java_buffer
;
547 SOURCE_FRONTEND_DEBUG ((stderr
, "[Class selected: %s]\n",
548 classname
+classname_length
-
549 (classname_length
<= 30 ?
550 classname_length
: 30)));
551 fd
= open (buffer
, O_RDONLY
| O_BINARY
);
555 /* Give .java a try, if necessary */
558 strcpy (buffer
, java_buffer
);
559 SOURCE_FRONTEND_DEBUG ((stderr
, "[Source selected: %s]\n",
560 classname
+classname_length
-
561 (classname_length
<= 30 ?
562 classname_length
: 30)));
563 fd
= open (buffer
, O_RDONLY
);
566 jcf
->java_source
= 1;
573 /* Remember that this class could not be found so that we do not
574 have to look again. */
575 *htab_find_slot_with_hash (memoized_class_lookups
, classname
, hash
, INSERT
)
576 = (void *) classname
;
580 if (jcf
->java_source
)
582 JCF_ZERO (jcf
); /* JCF_FINISH relies on this */
583 jcf
->java_source
= 1;
584 jcf
->filename
= xstrdup (buffer
);
585 close (fd
); /* We use STDIO for source file */
588 buffer
= (char *) open_class (buffer
, jcf
, fd
, dep_file
);
589 jcf
->classname
= xstrdup (classname
);
594 DEFUN(jcf_print_char
, (stream
, ch
),
595 FILE *stream AND
int ch
)
602 fprintf (stream
, "\\%c", ch
);
605 fprintf (stream
, "\\n");
608 fprintf (stream
, "\\t");
611 fprintf (stream
, "\\r");
614 if (ch
>= ' ' && ch
< 127)
617 fprintf (stream
, "\\%03x", ch
);
619 fprintf (stream
, "\\u%04x", ch
);
623 /* Print UTF8 string at STR of length LENGTH bytes to STREAM. */
626 DEFUN(jcf_print_utf8
, (stream
, str
, length
),
627 FILE *stream AND
register const unsigned char *str AND
int length
)
629 const unsigned char * limit
= str
+ length
;
632 int ch
= UTF8_GET (str
, limit
);
635 fprintf (stream
, "\\<invalid>");
638 jcf_print_char (stream
, ch
);
642 /* Same as jcf_print_utf8, but print IN_CHAR as OUT_CHAR. */
645 DEFUN(jcf_print_utf8_replace
, (stream
, str
, length
, in_char
, out_char
),
646 FILE *stream AND
const unsigned char *str AND
int length
647 AND
int in_char AND
int out_char
)
649 const unsigned char *limit
= str
+ length
;
652 int ch
= UTF8_GET (str
, limit
);
655 fprintf (stream
, "\\<invalid>");
658 jcf_print_char (stream
, ch
== in_char
? out_char
: ch
);
662 /* Check that all the cross-references in the constant pool are
663 valid. Returns 0 on success.
664 Otherwise, returns the index of the (first) invalid entry.
665 Only checks internal consistency, but does not check that
666 any classes, fields, or methods are valid.*/
669 DEFUN(verify_constant_pool
, (jcf
),
673 for (i
= 1; i
< JPOOL_SIZE (jcf
); i
++)
675 switch (JPOOL_TAG (jcf
, i
))
677 case CONSTANT_NameAndType
:
678 n
= JPOOL_USHORT2 (jcf
, i
);
679 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
680 || JPOOL_TAG (jcf
, n
) != CONSTANT_Utf8
)
682 /* ... fall through ... */
684 case CONSTANT_String
:
685 n
= JPOOL_USHORT1 (jcf
, i
);
686 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
687 || JPOOL_TAG (jcf
, n
) != CONSTANT_Utf8
)
690 case CONSTANT_Fieldref
:
691 case CONSTANT_Methodref
:
692 case CONSTANT_InterfaceMethodref
:
693 n
= JPOOL_USHORT1 (jcf
, i
);
694 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
695 || JPOOL_TAG (jcf
, n
) != CONSTANT_Class
)
697 n
= JPOOL_USHORT2 (jcf
, i
);
698 if (n
<= 0 || n
>= JPOOL_SIZE(jcf
)
699 || JPOOL_TAG (jcf
, n
) != CONSTANT_NameAndType
)
703 case CONSTANT_Double
:
707 case CONSTANT_Integer
:
709 case CONSTANT_Unicode
:
719 DEFUN(format_uint
, (buffer
, value
, base
),
720 char *buffer AND uint64 value AND
int base
)
722 #define WRITE_BUF_SIZE (4 + sizeof(uint64) * 8)
723 char buf
[WRITE_BUF_SIZE
];
724 register char *buf_ptr
= buf
+WRITE_BUF_SIZE
; /* End of buf. */
728 /* Now do the actual conversion, placing the result at the *end* of buf. */
729 /* Note this code does not pretend to be optimized. */
731 int digit
= value
% base
;
732 static const char digit_chars
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
733 *--buf_ptr
= digit_chars
[digit
];
735 } while (value
!= 0);
737 chars_written
= buf
+WRITE_BUF_SIZE
- buf_ptr
;
738 for (i
= 0; i
< chars_written
; i
++)
739 buffer
[i
] = *buf_ptr
++;
744 DEFUN(format_int
, (buffer
, value
, base
),
745 char *buffer AND jlong value AND
int base
)
750 abs_value
= -(uint64
)value
;
754 abs_value
= (uint64
) value
;
755 format_uint (buffer
, abs_value
, base
);