OSDN Git Service

2000-08-07 Alexandre Petit-Bianco <apbianco@cygnus.com
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-reader.c
1 /* This file read a Java(TM) .class file.
2    It is not stand-alone:  It depends on tons of macros, and the
3    intent is you #include this file after you've defined the macros.
4
5    Copyright (C) 1996, 1997, 1998, 1999, 2000  Free Software Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  
21
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
25
26 #include "jcf.h"
27 #include "zipfile.h"
28
29 static int get_attribute PARAMS ((JCF *));
30 static int peek_attribute PARAMS ((JCF *, int, const char *, int));
31 static void skip_attribute PARAMS ((JCF *, int));
32 static int jcf_parse_preamble PARAMS ((JCF *));
33 static int jcf_parse_constant_pool PARAMS ((JCF *));
34 static void jcf_parse_class PARAMS ((JCF *));
35 static int jcf_parse_fields PARAMS ((JCF *));
36 static int jcf_parse_one_method PARAMS ((JCF *));
37 static int jcf_parse_methods PARAMS ((JCF *));
38 static int jcf_parse_final_attributes PARAMS ((JCF *));
39
40 /* Go through all available attribute (ATTRIBUTE_NUMER) and try to
41    identify PEEKED_NAME.  Return 1 if PEEKED_NAME was found, 0
42    otherwise. JCF is restored to its initial position before
43    returning.  */
44
45 static int
46 peek_attribute (jcf, attribute_number, peeked_name, peeked_name_length)
47       JCF *jcf;
48       int attribute_number;
49       const char *peeked_name;
50       int peeked_name_length;
51 {
52   int to_return = 0;
53   long absolute_offset = (long)JCF_TELL (jcf);
54   int i;
55
56   for (i = 0; !to_return && i < attribute_number; i++)
57     {
58       uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
59       uint32 attribute_length = JCF_readu4 (jcf);
60       int name_length;
61       const unsigned char *name_data; 
62
63       JCF_FILL (jcf, (long) attribute_length);
64       if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf)
65           || JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
66         continue;
67
68       name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
69       name_data = JPOOL_UTF_DATA (jcf, attribute_name);
70
71       if (name_length == peeked_name_length 
72           && ! memcmp (name_data, peeked_name, peeked_name_length)) 
73         {
74           to_return = 1; 
75           break;
76         }
77       
78       JCF_SKIP (jcf, attribute_length);
79     }
80
81   JCF_SEEK (jcf, absolute_offset);
82   return to_return;
83 }
84
85 static void
86 skip_attribute (jcf, number_of_attribute)
87      JCF *jcf;
88      int number_of_attribute;
89 {
90   while (number_of_attribute--)
91     {
92       JCF_FILL (jcf, 6);
93       (void) JCF_readu2 (jcf);
94       JCF_SKIP (jcf, JCF_readu4 (jcf));
95     }
96 }
97
98 static int
99 DEFUN(get_attribute, (jcf),
100       JCF *jcf)
101 {
102   uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
103   uint32 attribute_length = JCF_readu4 (jcf);
104   uint32 start_pos = JCF_TELL(jcf);
105   int name_length;
106   const unsigned char *name_data;
107   JCF_FILL (jcf, (long) attribute_length);
108   if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
109     return -2;
110   if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
111     return -2;
112   name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
113   name_data = JPOOL_UTF_DATA (jcf, attribute_name);
114
115 #ifdef IGNORE_ATTRIBUTE
116    if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
117      {
118        JCF_SKIP (jcf, attribute_length);
119      }
120    else
121 #endif
122 #ifdef HANDLE_SOURCEFILE
123   if (name_length == 10 && memcmp (name_data, "SourceFile", 10) == 0)
124     {
125       uint16 sourcefile_index = JCF_readu2 (jcf);
126       HANDLE_SOURCEFILE(sourcefile_index);
127     }
128   else
129 #endif
130 #ifdef HANDLE_CONSTANTVALUE
131   if (name_length == 13 && memcmp (name_data, "ConstantValue", 13) == 0)
132     {
133       uint16 constantvalue_index = JCF_readu2 (jcf);
134       if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
135         return -2;
136       HANDLE_CONSTANTVALUE(constantvalue_index);
137     }
138   else
139 #endif
140 #ifdef HANDLE_CODE_ATTRIBUTE
141   if (name_length == 4 && memcmp (name_data, "Code", 4) == 0)
142     {
143       uint16 j;
144       uint16 max_stack ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
145       uint16 max_locals ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
146       uint32 code_length = JCF_readu4 (jcf);
147       uint16 exception_table_length, attributes_count;
148       if (code_length + 12 > attribute_length)
149         return -1;
150       HANDLE_CODE_ATTRIBUTE(max_stack, max_locals, code_length);
151       JCF_SKIP (jcf, code_length);
152       exception_table_length = JCF_readu2 (jcf);
153       if (code_length + 8 * exception_table_length + 12 > attribute_length)
154         return -1;
155 #ifdef HANDLE_EXCEPTION_TABLE
156       HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
157 #endif
158       JCF_SKIP (jcf, 2 * 4 * exception_table_length);
159       attributes_count = JCF_readu2 (jcf);
160       for (j = 0; j < attributes_count; j++)
161         {
162           int code = get_attribute (jcf);
163           if (code != 0)
164             return code;
165         }
166     }
167   else
168 #endif /* HANDLE_CODE_ATTRIBUTE */
169 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
170   if (name_length == 10 && memcmp (name_data, "Exceptions", 10) == 0)
171     {
172       uint16 count = JCF_readu2 (jcf);
173       HANDLE_EXCEPTIONS_ATTRIBUTE (count);
174     }
175   else
176 #endif
177 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
178   if (name_length == 15 && memcmp (name_data, "LineNumberTable", 15) == 0)
179     {
180       uint16 count = JCF_readu2 (jcf);
181       HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
182     }
183   else
184 #endif
185 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
186   if (name_length == 18 && memcmp (name_data, "LocalVariableTable", 18) == 0)
187     {
188       uint16 count = JCF_readu2 (jcf);
189       HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
190     }
191   else
192 #endif
193 #ifdef HANDLE_INNERCLASSES_ATTRIBUTE
194   if (name_length == 12 && memcmp (name_data, "InnerClasses", 12) == 0)
195     {
196       uint16 count = JCF_readu2 (jcf);
197       HANDLE_INNERCLASSES_ATTRIBUTE (count);
198     }
199   else
200 #endif
201     {
202 #ifdef PROCESS_OTHER_ATTRIBUTE
203       PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
204 #else
205       JCF_SKIP (jcf, attribute_length);
206 #endif
207     }
208   if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
209     return -1;
210   return 0;
211 }
212
213 /* Read and handle the pre-amble. */
214 static int
215 DEFUN(jcf_parse_preamble, (jcf),
216       JCF* jcf)
217 {
218   uint32 magic = (JCF_FILL (jcf, 8), JCF_readu4 (jcf));
219   uint16 minor_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
220   uint16 major_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
221 #ifdef HANDLE_MAGIC
222   HANDLE_MAGIC (magic, minor_version, major_version);
223 #endif
224   if (magic != 0xcafebabe)
225     return -1;
226   else
227     return 0;
228 }
229
230 /* Read and handle the constant pool.
231
232    Return 0 if OK.
233    Return -2 if a bad cross-reference (index of other constant) was seen.
234 */
235 static int
236 DEFUN(jcf_parse_constant_pool, (jcf),
237       JCF* jcf)
238 {
239   int i, n;
240   JPOOL_SIZE (jcf) = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
241   jcf->cpool.tags = ALLOC (JPOOL_SIZE (jcf));
242   jcf->cpool.data = ALLOC (sizeof (jword) * JPOOL_SIZE (jcf));
243   jcf->cpool.tags[0] = 0;
244 #ifdef HANDLE_START_CONSTANT_POOL
245   HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf));
246 #endif
247   for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
248     {
249       int constant_kind;
250        
251       /* Make sure at least 9 bytes are available.  This is enough
252          for all fixed-sized constant pool entries (so we don't need many
253          more JCF_FILL calls below), but is is small enough that
254          we are guaranteed to not hit EOF (in a valid .class file). */
255       JCF_FILL (jcf, 9);
256       constant_kind = JCF_readu (jcf);
257       jcf->cpool.tags[i] = constant_kind;
258       switch (constant_kind)
259         {
260         case CONSTANT_String:
261         case CONSTANT_Class:
262           jcf->cpool.data[i] = JCF_readu2 (jcf);
263           break;
264         case CONSTANT_Fieldref:
265         case CONSTANT_Methodref:
266         case CONSTANT_InterfaceMethodref:
267         case CONSTANT_NameAndType:
268           jcf->cpool.data[i] = JCF_readu2 (jcf);
269           jcf->cpool.data[i] |= JCF_readu2 (jcf) << 16;
270           break;
271         case CONSTANT_Integer:
272         case CONSTANT_Float:
273           jcf->cpool.data[i] = JCF_readu4 (jcf);
274           break;
275         case CONSTANT_Long:
276         case CONSTANT_Double:
277           jcf->cpool.data[i] = JCF_readu4 (jcf);
278           i++; /* These take up two spots in the constant pool */
279           jcf->cpool.tags[i] = 0;
280           jcf->cpool.data[i] = JCF_readu4 (jcf);
281           break;
282         case CONSTANT_Utf8:
283           n = JCF_readu2 (jcf);
284           JCF_FILL (jcf, n);
285 #ifdef HANDLE_CONSTANT_Utf8
286           HANDLE_CONSTANT_Utf8(jcf, i, n);
287 #else
288           jcf->cpool.data[i] = JCF_TELL(jcf) - 2;
289           JCF_SKIP (jcf, n);
290 #endif
291           break;
292         default:
293           return i;
294         }
295     }
296   return 0;
297 }
298
299 /* Read various class flags and numbers. */
300
301 static void
302 DEFUN(jcf_parse_class, (jcf),
303       JCF* jcf)
304 {
305   int i;
306   uint16 interfaces_count;
307   JCF_FILL (jcf, 8);
308   jcf->access_flags = JCF_readu2 (jcf);
309   jcf->this_class = JCF_readu2 (jcf);
310   jcf->super_class = JCF_readu2 (jcf);
311   interfaces_count = JCF_readu2 (jcf);
312
313 #ifdef HANDLE_CLASS_INFO
314   HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
315 #endif
316
317   JCF_FILL (jcf, 2 * interfaces_count);
318
319   /* Read interfaces. */
320   for (i = 0; i < interfaces_count; i++)
321     {
322       uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
323 #ifdef HANDLE_CLASS_INTERFACE
324       HANDLE_CLASS_INTERFACE (index);
325 #endif
326     }
327 }
328
329 /* Read fields. */
330 static int
331 DEFUN(jcf_parse_fields, (jcf),
332       JCF* jcf)
333 {
334   int i, j;
335   uint16 fields_count;
336   JCF_FILL (jcf, 2);
337   fields_count = JCF_readu2 (jcf);
338
339 #ifdef HANDLE_START_FIELDS
340   HANDLE_START_FIELDS (fields_count);
341 #endif
342   for (i = 0; i < fields_count; i++)
343     {
344       uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
345       uint16 name_index = JCF_readu2 (jcf);
346       uint16 signature_index = JCF_readu2 (jcf);
347       uint16 attribute_count = JCF_readu2 (jcf);
348 #ifdef HANDLE_START_FIELD
349       HANDLE_START_FIELD (access_flags, name_index, signature_index,
350                     attribute_count);
351 #endif
352       for (j = 0; j < attribute_count; j++)
353         {
354           int code = get_attribute (jcf);
355           if (code != 0)
356             return code;
357         }
358 #ifdef HANDLE_END_FIELD
359       HANDLE_END_FIELD ();
360 #endif
361     }
362 #ifdef HANDLE_END_FIELDS
363   HANDLE_END_FIELDS ();
364 #endif
365   return 0;
366 }
367
368 /* Read methods. */
369
370 static int
371 DEFUN(jcf_parse_one_method, (jcf),
372       JCF* jcf)
373 {
374   int i;
375   uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
376   uint16 name_index = JCF_readu2 (jcf);
377   uint16 signature_index = JCF_readu2 (jcf);
378   uint16 attribute_count = JCF_readu2 (jcf);
379 #ifdef HANDLE_METHOD
380   HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
381 #endif
382   for (i = 0; i < attribute_count; i++)
383     {
384       int code = get_attribute (jcf);
385       if (code != 0)
386         return code;
387     }
388 #ifdef HANDLE_END_METHOD
389   HANDLE_END_METHOD ();
390 #endif
391   return 0;
392 }
393
394 static int
395 DEFUN(jcf_parse_methods, (jcf),
396       JCF* jcf)
397 {
398   int i;
399   uint16 methods_count;
400   JCF_FILL (jcf, 2);
401   methods_count = JCF_readu2 (jcf);
402 #ifdef HANDLE_START_METHODS
403   HANDLE_START_METHODS (methods_count);
404 #endif
405   for (i = 0; i < methods_count; i++)
406     {
407       int code = jcf_parse_one_method (jcf);
408       if (code != 0)
409         return code;
410     }
411 #ifdef HANDLE_END_METHODS
412   HANDLE_END_METHODS ();
413 #endif
414   return 0;
415 }
416
417 /* Read attributes. */
418 static int
419 DEFUN(jcf_parse_final_attributes, (jcf),
420       JCF *jcf)
421 {
422   int i;
423   uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
424 #ifdef START_FINAL_ATTRIBUTES
425   START_FINAL_ATTRIBUTES (attributes_count)
426 #endif
427   for (i = 0; i < attributes_count; i++)
428     {
429       int code = get_attribute (jcf);
430       if (code != 0)
431         return code;
432     }
433   return 0;
434 }
435