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.
5 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
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)
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.
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.
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. */
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 *));
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
46 peek_attribute (jcf, attribute_number, peeked_name, peeked_name_length)
49 const char *peeked_name;
50 int peeked_name_length;
53 long absolute_offset = (long)JCF_TELL (jcf);
56 for (i = 0; !to_return && i < attribute_number; i++)
58 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
59 uint32 attribute_length = JCF_readu4 (jcf);
61 const unsigned char *name_data;
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)
68 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
69 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
71 if (name_length == peeked_name_length
72 && ! memcmp (name_data, peeked_name, peeked_name_length))
78 JCF_SKIP (jcf, attribute_length);
81 JCF_SEEK (jcf, absolute_offset);
86 skip_attribute (jcf, number_of_attribute)
88 int number_of_attribute;
90 while (number_of_attribute--)
93 (void) JCF_readu2 (jcf);
94 JCF_SKIP (jcf, JCF_readu4 (jcf));
99 DEFUN(get_attribute, (jcf),
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);
106 const unsigned char *name_data;
107 JCF_FILL (jcf, (long) attribute_length);
108 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
110 if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
112 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
113 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
115 #ifdef IGNORE_ATTRIBUTE
116 if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
118 JCF_SKIP (jcf, attribute_length);
122 #ifdef HANDLE_SOURCEFILE
123 if (name_length == 10 && memcmp (name_data, "SourceFile", 10) == 0)
125 uint16 sourcefile_index = JCF_readu2 (jcf);
126 HANDLE_SOURCEFILE(sourcefile_index);
130 #ifdef HANDLE_CONSTANTVALUE
131 if (name_length == 13 && memcmp (name_data, "ConstantValue", 13) == 0)
133 uint16 constantvalue_index = JCF_readu2 (jcf);
134 if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
136 HANDLE_CONSTANTVALUE(constantvalue_index);
140 #ifdef HANDLE_CODE_ATTRIBUTE
141 if (name_length == 4 && memcmp (name_data, "Code", 4) == 0)
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)
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)
155 #ifdef HANDLE_EXCEPTION_TABLE
156 HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
158 JCF_SKIP (jcf, 2 * 4 * exception_table_length);
159 attributes_count = JCF_readu2 (jcf);
160 for (j = 0; j < attributes_count; j++)
162 int code = get_attribute (jcf);
168 #endif /* HANDLE_CODE_ATTRIBUTE */
169 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
170 if (name_length == 10 && memcmp (name_data, "Exceptions", 10) == 0)
172 uint16 count = JCF_readu2 (jcf);
173 HANDLE_EXCEPTIONS_ATTRIBUTE (count);
177 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
178 if (name_length == 15 && memcmp (name_data, "LineNumberTable", 15) == 0)
180 uint16 count = JCF_readu2 (jcf);
181 HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
185 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
186 if (name_length == 18 && memcmp (name_data, "LocalVariableTable", 18) == 0)
188 uint16 count = JCF_readu2 (jcf);
189 HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
193 #ifdef HANDLE_INNERCLASSES_ATTRIBUTE
194 if (name_length == 12 && memcmp (name_data, "InnerClasses", 12) == 0)
196 uint16 count = JCF_readu2 (jcf);
197 HANDLE_INNERCLASSES_ATTRIBUTE (count);
202 #ifdef PROCESS_OTHER_ATTRIBUTE
203 PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
205 JCF_SKIP (jcf, attribute_length);
208 if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
213 /* Read and handle the pre-amble. */
215 DEFUN(jcf_parse_preamble, (jcf),
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);
222 HANDLE_MAGIC (magic, minor_version, major_version);
224 if (magic != 0xcafebabe)
230 /* Read and handle the constant pool.
233 Return -2 if a bad cross-reference (index of other constant) was seen.
236 DEFUN(jcf_parse_constant_pool, (jcf),
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));
247 for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
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). */
256 constant_kind = JCF_readu (jcf);
257 jcf->cpool.tags[i] = constant_kind;
258 switch (constant_kind)
260 case CONSTANT_String:
262 jcf->cpool.data[i] = JCF_readu2 (jcf);
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;
271 case CONSTANT_Integer:
273 jcf->cpool.data[i] = JCF_readu4 (jcf);
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);
283 n = JCF_readu2 (jcf);
285 #ifdef HANDLE_CONSTANT_Utf8
286 HANDLE_CONSTANT_Utf8(jcf, i, n);
288 jcf->cpool.data[i] = JCF_TELL(jcf) - 2;
299 /* Read various class flags and numbers. */
302 DEFUN(jcf_parse_class, (jcf),
306 uint16 interfaces_count;
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);
313 #ifdef HANDLE_CLASS_INFO
314 HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
317 JCF_FILL (jcf, 2 * interfaces_count);
319 /* Read interfaces. */
320 for (i = 0; i < interfaces_count; i++)
322 uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
323 #ifdef HANDLE_CLASS_INTERFACE
324 HANDLE_CLASS_INTERFACE (index);
331 DEFUN(jcf_parse_fields, (jcf),
337 fields_count = JCF_readu2 (jcf);
339 #ifdef HANDLE_START_FIELDS
340 HANDLE_START_FIELDS (fields_count);
342 for (i = 0; i < fields_count; i++)
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,
352 for (j = 0; j < attribute_count; j++)
354 int code = get_attribute (jcf);
358 #ifdef HANDLE_END_FIELD
362 #ifdef HANDLE_END_FIELDS
363 HANDLE_END_FIELDS ();
371 DEFUN(jcf_parse_one_method, (jcf),
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);
380 HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
382 for (i = 0; i < attribute_count; i++)
384 int code = get_attribute (jcf);
388 #ifdef HANDLE_END_METHOD
389 HANDLE_END_METHOD ();
395 DEFUN(jcf_parse_methods, (jcf),
399 uint16 methods_count;
401 methods_count = JCF_readu2 (jcf);
402 #ifdef HANDLE_START_METHODS
403 HANDLE_START_METHODS (methods_count);
405 for (i = 0; i < methods_count; i++)
407 int code = jcf_parse_one_method (jcf);
411 #ifdef HANDLE_END_METHODS
412 HANDLE_END_METHODS ();
417 /* Read attributes. */
419 DEFUN(jcf_parse_final_attributes, (jcf),
423 uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
424 #ifdef START_FINAL_ATTRIBUTES
425 START_FINAL_ATTRIBUTES (attributes_count)
427 for (i = 0; i < attributes_count; i++)
429 int code = get_attribute (jcf);