From e72ae7bebbeb0d587e943629548d4c0c6d7dfb41 Mon Sep 17 00:00:00 2001 From: BENEDEK Date: Fri, 12 Apr 2024 21:54:28 +0200 Subject: [PATCH] neater code --- classfile.c | 83 ++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/classfile.c b/classfile.c index b2a8b6a..5049f3c 100644 --- a/classfile.c +++ b/classfile.c @@ -81,71 +81,68 @@ void parse_attribute(ClassFile* cf, attribute_info* base, FILE* file) { read_u2(&(base->info.stack_map_table_attribute.number_of_entries), 1, file); base->info.stack_map_table_attribute.entries = malloc(sizeof(stack_map_frame) * base->info.stack_map_table_attribute.number_of_entries); for (u2 i = 0; i < base->info.stack_map_table_attribute.number_of_entries; i++) { - fread(&(base->info.stack_map_table_attribute.entries[i].frame_type), 1, 1, file); - u1 frame_type = base->info.stack_map_table_attribute.entries[i].frame_type; + stack_map_frame* entry = &(base->info.stack_map_table_attribute.entries[i]); + fread(&(entry->frame_type), 1, 1, file); + u1 frame_type = entry->frame_type; switch (stack_frame_type_enum(frame_type)) { case SAME: break; case SAME_LOCALS_1_STACK_ITEM_FRAME: - fread(&(base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame.stack[0].tag), 1, 1, file); - if (base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame.stack[0].tag == ITEM_Object) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame.stack[0].Object_variable_info.cpool_index), 1, file); - } else if (base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame.stack[0].tag == ITEM_Uninitialized) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame.stack[0].Uninitialized_variable_info.offset), 1, file); + fread(&(entry->same_locals_1_stack_item_frame.stack[0].tag), 1, 1, file); + if (entry->same_locals_1_stack_item_frame.stack[0].tag == ITEM_Object) { + read_u2(&(entry->same_locals_1_stack_item_frame.stack[0].Object_variable_info.cpool_index), 1, file); + } else if (entry->same_locals_1_stack_item_frame.stack[0].tag == ITEM_Uninitialized) { + read_u2(&(entry->same_locals_1_stack_item_frame.stack[0].Uninitialized_variable_info.offset), 1, file); } break; case SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED: - read_u2(&(base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame_extended.offset_delta), 1, file); - fread(&(base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame_extended.stack[0].tag), 1, 1, file); - if (base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame_extended.stack[0].tag == ITEM_Object) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame_extended.stack[0].Object_variable_info.cpool_index), 1, - file); - } else if (base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame_extended.stack[0].tag == ITEM_Uninitialized) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].same_locals_1_stack_item_frame_extended.stack[0].Uninitialized_variable_info.offset), 1, - file); + read_u2(&(entry->same_locals_1_stack_item_frame_extended.offset_delta), 1, file); + fread(&(entry->same_locals_1_stack_item_frame_extended.stack[0].tag), 1, 1, file); + if (entry->same_locals_1_stack_item_frame_extended.stack[0].tag == ITEM_Object) { + read_u2(&(entry->same_locals_1_stack_item_frame_extended.stack[0].Object_variable_info.cpool_index), 1, file); + } else if (entry->same_locals_1_stack_item_frame_extended.stack[0].tag == ITEM_Uninitialized) { + read_u2(&(entry->same_locals_1_stack_item_frame_extended.stack[0].Uninitialized_variable_info.offset), 1, file); } break; case CHOP: - read_u2(&(base->info.stack_map_table_attribute.entries[i].chop_frame.offset_delta), 1, file); + read_u2(&(entry->chop_frame.offset_delta), 1, file); break; case SAME_FRAME_EXTENDED: - read_u2(&(base->info.stack_map_table_attribute.entries[i].same_frame_extended.offset_delta), 1, file); + read_u2(&(entry->same_frame_extended.offset_delta), 1, file); break; case APPEND: - read_u2(&(base->info.stack_map_table_attribute.entries[i].append_frame.offset_delta), 1, file); + read_u2(&(entry->append_frame.offset_delta), 1, file); u2 count = frame_type - 251; - base->info.stack_map_table_attribute.entries[i].append_frame.locals = malloc(sizeof(verification_type_info) * count); + entry->append_frame.locals = malloc(sizeof(verification_type_info) * count); for (u2 j = 0; j < count; j++) { - fread(&(base->info.stack_map_table_attribute.entries[i].append_frame.locals[j].tag), 1, 1, file); - if (base->info.stack_map_table_attribute.entries[i].append_frame.locals[j].tag == ITEM_Object) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].append_frame.locals[j].Object_variable_info.cpool_index), 1, file); - } else if (base->info.stack_map_table_attribute.entries[i].append_frame.locals[j].tag == ITEM_Uninitialized) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].append_frame.locals[j].Uninitialized_variable_info.offset), 1, file); + fread(&(entry->append_frame.locals[j].tag), 1, 1, file); + if (entry->append_frame.locals[j].tag == ITEM_Object) { + read_u2(&(entry->append_frame.locals[j].Object_variable_info.cpool_index), 1, file); + } else if (entry->append_frame.locals[j].tag == ITEM_Uninitialized) { + read_u2(&(entry->append_frame.locals[j].Uninitialized_variable_info.offset), 1, file); } } break; case FULL_FRAME: - read_u2(&(base->info.stack_map_table_attribute.entries[i].full_frame.offset_delta), 1, file); - read_u2(&(base->info.stack_map_table_attribute.entries[i].full_frame.number_of_locals), 1, file); - base->info.stack_map_table_attribute.entries[i].full_frame.locals = - malloc(sizeof(verification_type_info) * base->info.stack_map_table_attribute.entries[i].full_frame.number_of_locals); - for (u2 j = 0; j < base->info.stack_map_table_attribute.entries[i].full_frame.number_of_locals; j++) { - fread(&(base->info.stack_map_table_attribute.entries[i].full_frame.locals[j].tag), 1, 1, file); - if (base->info.stack_map_table_attribute.entries[i].full_frame.locals[j].tag == ITEM_Object) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].full_frame.locals[j].Object_variable_info.cpool_index), 1, file); - } else if (base->info.stack_map_table_attribute.entries[i].full_frame.locals[j].tag == ITEM_Uninitialized) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].full_frame.locals[j].Uninitialized_variable_info.offset), 1, file); + read_u2(&(entry->full_frame.offset_delta), 1, file); + read_u2(&(entry->full_frame.number_of_locals), 1, file); + entry->full_frame.locals = malloc(sizeof(verification_type_info) * entry->full_frame.number_of_locals); + for (u2 j = 0; j < entry->full_frame.number_of_locals; j++) { + fread(&(entry->full_frame.locals[j].tag), 1, 1, file); + if (entry->full_frame.locals[j].tag == ITEM_Object) { + read_u2(&(entry->full_frame.locals[j].Object_variable_info.cpool_index), 1, file); + } else if (entry->full_frame.locals[j].tag == ITEM_Uninitialized) { + read_u2(&(entry->full_frame.locals[j].Uninitialized_variable_info.offset), 1, file); } } - read_u2(&(base->info.stack_map_table_attribute.entries[i].full_frame.number_of_stack_items), 1, file); - base->info.stack_map_table_attribute.entries[i].full_frame.stack = - malloc(sizeof(verification_type_info) * base->info.stack_map_table_attribute.entries[i].full_frame.number_of_stack_items); - for (u2 j = 0; j < base->info.stack_map_table_attribute.entries[i].full_frame.number_of_stack_items; j++) { - fread(&(base->info.stack_map_table_attribute.entries[i].full_frame.stack[j].tag), 1, 1, file); - if (base->info.stack_map_table_attribute.entries[i].full_frame.stack[j].tag == ITEM_Object) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].full_frame.stack[j].Object_variable_info.cpool_index), 1, file); - } else if (base->info.stack_map_table_attribute.entries[i].full_frame.stack[j].tag == ITEM_Uninitialized) { - read_u2(&(base->info.stack_map_table_attribute.entries[i].full_frame.stack[j].Uninitialized_variable_info.offset), 1, file); + read_u2(&(entry->full_frame.number_of_stack_items), 1, file); + entry->full_frame.stack = malloc(sizeof(verification_type_info) * entry->full_frame.number_of_stack_items); + for (u2 j = 0; j < entry->full_frame.number_of_stack_items; j++) { + fread(&(entry->full_frame.stack[j].tag), 1, 1, file); + if (entry->full_frame.stack[j].tag == ITEM_Object) { + read_u2(&(entry->full_frame.stack[j].Object_variable_info.cpool_index), 1, file); + } else if (entry->full_frame.stack[j].tag == ITEM_Uninitialized) { + read_u2(&(entry->full_frame.stack[j].Uninitialized_variable_info.offset), 1, file); } } break;