1 /* Utility routines for finding and reading Java(TM) .class files.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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"
35 /* DOS brain-damage */
37 #define O_BINARY 0 /* MS-DOS brain-damage */
41 DEFUN(jcf_unexpected_eof, (jcf, count),
42 JCF *jcf AND int count ATTRIBUTE_UNUSED)
45 fprintf (stderr, "Premature end of .class file %s.\n", jcf->filename);
47 fprintf (stderr, "Premature end of .class file <stdin>.\n");
52 DEFUN(jcf_trim_old_input, (jcf),
55 int count = jcf->read_ptr - jcf->buffer;
58 memmove (jcf->buffer, jcf->read_ptr, jcf->read_end - jcf->read_ptr);
59 jcf->read_ptr -= count;
60 jcf->read_end -= count;
65 DEFUN(jcf_filbuf_from_stdio, (jcf, count),
66 JCF *jcf AND int count)
68 FILE *file = (FILE*) (jcf->read_state);
69 if (count > jcf->buffer_end - jcf->read_ptr)
71 JCF_u4 old_read_ptr = jcf->read_ptr - jcf->buffer;
72 JCF_u4 old_read_end = jcf->read_end - jcf->buffer;
73 JCF_u4 old_size = jcf->buffer_end - jcf->buffer;
74 JCF_u4 new_size = (old_size == 0 ? 2000 : 2 * old_size) + count;
75 unsigned char *new_buffer = jcf->buffer == NULL ? ALLOC (new_size)
76 : REALLOC (jcf->buffer, new_size);
77 jcf->buffer = new_buffer;
78 jcf->buffer_end = new_buffer + new_size;
79 jcf->read_ptr = new_buffer + old_read_ptr;
80 jcf->read_end = new_buffer + old_read_end;
82 count -= jcf->read_end - jcf->read_ptr;
85 if ((int) fread (jcf->read_end, 1, count, file) != count)
86 jcf_unexpected_eof (jcf, count);
87 jcf->read_end += count;
93 struct ZipFileCache *SeenZipFiles = NULL;
95 /* Open a zip file with the given name, and cache directory and file
96 descriptor. If the file is missing, treat it as an empty archive.
97 Return NULL if the .zip file is malformed.
101 DEFUN(opendir_in_zip, (zipfile, is_system),
102 const char *zipfile AND int is_system)
104 struct ZipFileCache* zipf;
107 for (zipf = SeenZipFiles; zipf != NULL; zipf = zipf->next)
109 if (strcmp (zipf->name, zipfile) == 0)
113 zipf = ALLOC (sizeof (struct ZipFileCache) + strlen (zipfile) + 1);
114 zipf->next = SeenZipFiles;
115 zipf->name = (char*)(zipf+1);
116 strcpy (zipf->name, zipfile);
118 fd = open (zipfile, O_RDONLY | O_BINARY);
122 /* A missing zip file is not considered an error.
123 We may want to re-consider that. FIXME. */
125 zipf->z.dir_size = 0;
126 zipf->z.central_directory = NULL;
130 jcf_dependency_add_file (zipfile, is_system);
131 if (read (fd, magic, 4) != 4 || GET_u4 (magic) != (JCF_u4)ZIPMAGIC)
133 lseek (fd, 0L, SEEK_SET);
134 if (read_zip_archive (&zipf->z) != 0)
141 0: OK - zipmember found.
143 -2: Malformed archive.
147 DEFUN(open_in_zip, (jcf, zipfile, zipmember, is_system),
148 JCF *jcf AND const char *zipfile AND const char *zipmember
153 ZipFile *zipf = opendir_in_zip (zipfile, is_system);
154 z_stream d_stream; /* decompression stream */
162 d_stream.zalloc = (alloc_func) 0;
163 d_stream.zfree = (free_func) 0;
164 d_stream.opaque = (voidpf) 0;
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->filbuf = jcf_unexpected_eof;
177 jcf->filename = xstrdup (zipfile);
178 jcf->classname = xstrdup (zipmember);
179 jcf->zipd = (void *)zipd;
181 if (zipd->compression_method == Z_NO_COMPRESSION)
183 jcf->buffer = ALLOC (zipd->size);
184 jcf->buffer_end = jcf->buffer + zipd->size;
185 jcf->read_ptr = jcf->buffer;
186 jcf->read_end = jcf->buffer_end;
187 if (lseek (zipf->fd, zipd->filestart, 0) < 0
188 || read (zipf->fd, jcf->buffer, zipd->size) != zipd->size)
194 jcf->buffer = ALLOC (zipd->uncompressed_size);
195 d_stream.next_out = jcf->buffer;
196 d_stream.avail_out = zipd->uncompressed_size;
197 jcf->buffer_end = jcf->buffer + zipd->uncompressed_size;
198 jcf->read_ptr = jcf->buffer;
199 jcf->read_end = jcf->buffer_end;
200 buffer = ALLOC (zipd->size);
201 d_stream.next_in = buffer;
202 d_stream.avail_in = zipd->size;
203 if (lseek (zipf->fd, zipd->filestart, 0) < 0
204 || read (zipf->fd, buffer, zipd->size) != zipd->size)
206 /* Handle NO_HEADER using undocumented zlib feature.
207 This is a very common hack. */
208 inflateInit2 (&d_stream, -MAX_WBITS);
209 inflate (&d_stream, Z_NO_FLUSH);
210 inflateEnd (&d_stream);
222 DEFUN(open_class, (filename, jcf, stream, dep_name),
223 const char *filename AND JCF *jcf AND FILE* stream
224 AND const char *dep_name)
228 if (dep_name != NULL)
229 jcf_dependency_add_file (dep_name, 0);
232 jcf->buffer_end = NULL;
233 jcf->read_ptr = NULL;
234 jcf->read_end = NULL;
235 jcf->read_state = stream;
236 jcf->filbuf = jcf_filbuf_from_stdio;
244 DEFUN(open_class, (filename, jcf, fd, dep_name),
245 const char *filename AND JCF *jcf AND int fd AND const char *dep_name)
249 struct stat stat_buf;
250 if (fstat (fd, &stat_buf) != 0
251 || ! S_ISREG (stat_buf.st_mode))
253 perror ("Could not figure length of .class file");
256 if (dep_name != NULL)
257 jcf_dependency_add_file (dep_name, 0);
259 jcf->buffer = ALLOC (stat_buf.st_size);
260 jcf->buffer_end = jcf->buffer + stat_buf.st_size;
261 jcf->read_ptr = jcf->buffer;
262 jcf->read_end = jcf->buffer_end;
263 jcf->read_state = NULL;
264 jcf->filename = filename;
265 if (read (fd, jcf->buffer, stat_buf.st_size) != stat_buf.st_size)
267 perror ("Failed to read .class file");
271 jcf->filbuf = jcf_unexpected_eof;
281 DEFUN(find_classfile, (filename, jcf, dep_name),
282 char *filename AND JCF *jcf AND const char *dep_name)
285 FILE *stream = fopen (filename, "rb");
288 return open_class (arg, jcf, stream, dep_name);
290 int fd = open (filename, O_RDONLY | O_BINARY);
293 return open_class (filename, jcf, fd, dep_name);
297 /* Returns a freshly malloc'd string with the fully qualified pathname
298 of the .class file for the class CLASSNAME. Returns NULL on
299 failure. If JCF != NULL, it is suitably initialized.
300 SOURCE_OK is true if we should also look for .java file. */
303 DEFUN(find_class, (classname, classname_length, jcf, source_ok),
304 const char *classname AND int classname_length AND JCF *jcf AND int source_ok)
312 int i, k, java = -1, class = -1;
313 struct stat java_buf, class_buf;
318 /* Allocate and zero out the buffer, since we don't explicitly put a
319 null pointer when we're copying it below. */
320 int buflen = jcf_path_max_len () + classname_length + 10;
321 char *buffer = (char *) ALLOC (buflen);
322 memset (buffer, 0, buflen);
324 java_buffer = (char *) alloca (buflen);
326 jcf->java_source = 0;
328 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
330 const char *path_name = jcf_path_name (entry);
335 strcpy (buffer, path_name);
338 /* This is right because we know that `.zip' entries will have a
339 trailing slash. See jcf-path.c. */
342 for (k = 0; k < classname_length; k++, i++)
344 char ch = classname[k];
345 buffer[i] = ch == '.' ? '/' : ch;
347 strcpy (buffer+i, ".class");
349 if (jcf_path_is_zipfile (entry))
353 buffer[dir_len] = '\0';
354 SOURCE_FRONTEND_DEBUG
355 (("Trying [...%s]:%s",
356 &buffer[dir_len-(dir_len > 15 ? 15 : dir_len)],
360 err_code = open_in_zip (jcf, buffer, buffer+dir_len+1,
361 jcf_path_is_system (entry));
364 /* Should we check if .zip is out-of-date wrt .java? */
365 buffer[dir_len] = '(';
366 strcpy (buffer+i, ".class)");
374 class = stat (buffer, &class_buf);
379 /* Compute name of .java file. */
381 strcpy (java_buffer, path_name);
382 l = strlen (java_buffer);
383 for (m = 0; m < classname_length; ++m)
384 java_buffer[m + l] = (classname[m] == '.' ? '/' : classname[m]);
385 strcpy (java_buffer + m + l, ".java");
386 java = stat (java_buffer, &java_buf);
392 /* We preferably pick a class file if we have a chance. If the source
393 file is newer than the class file, we issue a warning and parse the
395 There should be a flag to allow people have the class file picked
396 up no matter what. FIXME. */
397 if (! java && ! class && java_buf.st_mtime >= class_buf.st_mtime)
399 char *stripped_class_name = xstrdup (classname);
400 int i = strlen (stripped_class_name);
402 while (stripped_class_name [i] != '.')
405 stripped_class_name [i] = '\0';
407 warning ("Source file for class `%s' is newer than its matching class file. Source file used instead", stripped_class_name);
408 free (stripped_class_name);
413 dep_file = java_buffer;
419 SOURCE_FRONTEND_DEBUG (("Trying %s", buffer));
420 stream = fopen (buffer, "rb");
424 /* Give .java a try, if necessary */
427 strcpy (buffer, java_buffer);
428 SOURCE_FRONTEND_DEBUG (("Trying %s", buffer));
429 stream = fopen (buffer, "r");
432 jcf->java_source = 1;
439 SOURCE_FRONTEND_DEBUG ((stderr, "[Class selected: %s]\n",
440 classname+classname_length-
441 (classname_length <= 30 ?
442 classname_length : 30)));
443 fd = open (buffer, O_RDONLY | O_BINARY);
447 /* Give .java a try, if necessary */
450 strcpy (buffer, java_buffer);
451 SOURCE_FRONTEND_DEBUG ((stderr, "[Source selected: %s]\n",
452 classname+classname_length-
453 (classname_length <= 30 ?
454 classname_length : 30)));
455 fd = open (buffer, O_RDONLY);
458 jcf->java_source = 1;
468 if (jcf->java_source)
469 return NULL; /* FIXME */
471 return open_class (buffer, jcf, stream, dep_file);
473 if (jcf->java_source)
475 JCF_ZERO (jcf); /* JCF_FINISH relies on this */
476 jcf->java_source = 1;
477 jcf->filename = xstrdup (buffer);
478 close (fd); /* We use STDIO for source file */
481 buffer = (char *) open_class (buffer, jcf, fd, dep_file);
482 jcf->classname = xstrdup (classname);
488 DEFUN(jcf_print_char, (stream, ch),
489 FILE *stream AND int ch)
496 fprintf (stream, "\\%c", ch);
499 fprintf (stream, "\\n");
502 fprintf (stream, "\\t");
505 fprintf (stream, "\\r");
508 if (ch >= ' ' && ch < 127)
511 fprintf (stream, "\\%03x", ch);
513 fprintf (stream, "\\u%04x", ch);
517 /* Print UTF8 string at STR of length LENGTH bytes to STREAM. */
520 DEFUN(jcf_print_utf8, (stream, str, length),
521 FILE *stream AND register const unsigned char *str AND int length)
523 const unsigned char * limit = str + length;
526 int ch = UTF8_GET (str, limit);
529 fprintf (stream, "\\<invalid>");
532 jcf_print_char (stream, ch);
536 /* Same as jcf_print_utf8, but print IN_CHAR as OUT_CHAR. */
539 DEFUN(jcf_print_utf8_replace, (stream, str, length, in_char, out_char),
540 FILE *stream AND const unsigned char *str AND int length
541 AND int in_char AND int out_char)
543 const unsigned char *limit = str + length;
546 int ch = UTF8_GET (str, limit);
549 fprintf (stream, "\\<invalid>");
552 jcf_print_char (stream, ch == in_char ? out_char : ch);
556 /* Check that all the cross-references in the constant pool are
557 valid. Returns 0 on success.
558 Otherwise, returns the index of the (first) invalid entry. */
561 DEFUN(verify_constant_pool, (jcf),
565 for (i = 1; i < JPOOL_SIZE (jcf); i++)
567 switch (JPOOL_TAG (jcf, i))
569 case CONSTANT_NameAndType:
570 n = JPOOL_USHORT2 (jcf, i);
571 if (n <= 0 || n >= JPOOL_SIZE(jcf)
572 || JPOOL_TAG (jcf, n) != CONSTANT_Utf8)
574 /* ... fall through ... */
576 case CONSTANT_String:
577 n = JPOOL_USHORT1 (jcf, i);
578 if (n <= 0 || n >= JPOOL_SIZE(jcf)
579 || JPOOL_TAG (jcf, n) != CONSTANT_Utf8)
582 case CONSTANT_Fieldref:
583 case CONSTANT_Methodref:
584 case CONSTANT_InterfaceMethodref:
585 n = JPOOL_USHORT1 (jcf, i);
586 if (n <= 0 || n >= JPOOL_SIZE(jcf)
587 || JPOOL_TAG (jcf, n) != CONSTANT_Class)
589 n = JPOOL_USHORT2 (jcf, i);
590 if (n <= 0 || n >= JPOOL_SIZE(jcf)
591 || JPOOL_TAG (jcf, n) != CONSTANT_NameAndType)
595 case CONSTANT_Double:
599 case CONSTANT_Integer:
601 case CONSTANT_Unicode:
611 DEFUN(format_uint, (buffer, value, base),
612 char *buffer AND uint64 value AND int base)
614 #define WRITE_BUF_SIZE (4 + sizeof(uint64) * 8)
615 char buf[WRITE_BUF_SIZE];
616 register char *buf_ptr = buf+WRITE_BUF_SIZE; /* End of buf. */
620 /* Now do the actual conversion, placing the result at the *end* of buf. */
621 /* Note this code does not pretend to be optimized. */
623 int digit = value % base;
624 static char digit_chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
625 *--buf_ptr = digit_chars[digit];
627 } while (value != 0);
629 chars_written = buf+WRITE_BUF_SIZE - buf_ptr;
630 for (i = 0; i < chars_written; i++)
631 buffer[i] = *buf_ptr++;
636 DEFUN(format_int, (buffer, value, base),
637 char *buffer AND jlong value AND int base)
642 abs_value = -(uint64)value;
646 abs_value = (uint64) value;
647 format_uint (buffer, abs_value, base);