diff --git a/classfile.c b/classfile.c index 54e5147..463d0e9 100644 --- a/classfile.c +++ b/classfile.c @@ -160,6 +160,8 @@ ClassFile *ClassFile_load(const char *path) { // attribute_name_index 2bytes, attribute_length 4bytes read_u2(&(cf->fields[i].attributes[j].attribute_name_index), 1, file); read_u4(&(cf->fields[i].attributes[j].attribute_length), 1, file); + // TODO: parse attributes by tag + // this has an unknown amount of layers, should be moved out to a function cf->fields[i].attributes[j].info = (u1 *)malloc(cf->fields[i].attributes[j].attribute_length); fread(cf->fields[i].attributes[j].info, @@ -182,6 +184,8 @@ ClassFile *ClassFile_load(const char *path) { // attribute_name_index 2bytes, attribute_length 4bytes read_u2(&(cf->methods[i].attributes[j].attribute_name_index), 1, file); read_u4(&(cf->methods[i].attributes[j].attribute_length), 1, file); + // TODO: parse attributes by tag + // this has an unknown amount of layers, should be moved out to a function cf->methods[i].attributes[j].info = (u1 *)malloc(cf->methods[i].attributes[j].attribute_length); fread(cf->methods[i].attributes[j].info, @@ -200,6 +204,8 @@ ClassFile *ClassFile_load(const char *path) { read_u2(&(cf->attributes[i].attribute_name_index), 1, file); read_u4(&(cf->attributes[i].attribute_length), 1, file); cf->attributes[i].info = (u1 *)malloc(cf->attributes[i].attribute_length); + // TODO: parse attributes by tag + // this has an unknown amount of layers, should be moved out to a function fread(cf->attributes[i].info, cf->attributes[i].attribute_length, 1, file); } diff --git a/classfile.h b/classfile.h index 62d930d..cf3899f 100644 --- a/classfile.h +++ b/classfile.h @@ -133,10 +133,79 @@ typedef struct { } info; } cp_info; +typedef struct { + u2 constantvalue_index; +} ConstantValue_attribute; + typedef struct { u2 attribute_name_index; u4 attribute_length; - u1 *info; + u2 max_stack; + u2 max_locals; + u4 code_length; + u1 *code; + u2 exception_table_length; + struct { + u2 start_pc; + u2 end_pc; + u2 handler_pc; + u2 catch_type; + } * exception_table; + u2 attributes_count; + void *attributes; +} Code_attribute; + +// TODO: stack_map_frame +typedef struct { + u2 attribute_name_index; + u4 attribute_length; + u2 number_of_entries; + //stack_map_frame *entries; +} StackMapTable_attribute; + +typedef struct { + u2 attribute_name_index; + u4 attribute_length; + u2 num_bootstrap_methods; + struct { + u2 bootstrap_method_ref; + u2 num_bootstrap_arguments; + u2 *bootstrap_arguments; + } * bootstrap_methods; +} BootstrapMethods_attribute; + +typedef struct { + u2 attribute_name_index; + u4 attribute_length; + u2 host_class_index; +} NestHost_attribute; + +typedef struct { + u2 attribute_name_index; + u4 attribute_length; + u2 number_of_classes; + u2 *classes; +} NestMembers_attribute; + +typedef struct { + u2 attribute_name_index; + u4 attribute_length; + u2 number_of_classes; + u2 *classes; +} PermittedSubclasses_attribute; + +typedef struct { + u2 attribute_name_index; + u4 attribute_length; + union { + ConstantValue_attribute constant_value_attribute; + Code_attribute code_attribute; + StackMapTable_attribute stack_map_table_attribute; + BootstrapMethods_attribute bootstrap_methods_attribute; + NestHost_attribute nest_host_attribute; + NestMembers_attribute nest_members_attribute; + PermittedSubclasses_attribute permitted_subclasses_attribute; + } info; } attribute_info; typedef struct {