int cafebabe_attribute_info_init(struct cafebabe_attribute_info *a, struct cafebabe_stream *s) { if (cafebabe_stream_read_uint16(s, &a->attribute_name_index)) goto out; if (cafebabe_stream_read_uint32(s, &a->attribute_length)) goto out; a->info = cafebabe_stream_malloc(s, a->attribute_length); if (!a->info) goto out; for (uint32_t i = 0; i < a->attribute_length; ++i) { if (cafebabe_stream_read_uint8(s, &a->info[i])) goto out_info; } return 0; out_info: free(a->info); out: return 1; }
int cafebabe_field_info_init(struct cafebabe_field_info *f, struct cafebabe_stream *s) { if (cafebabe_stream_read_uint16(s, &f->access_flags)) goto out; if (cafebabe_stream_read_uint16(s, &f->name_index)) goto out; if (cafebabe_stream_read_uint16(s, &f->descriptor_index)) goto out; if (cafebabe_stream_read_uint16(s, &f->attributes.count)) goto out; f->attributes.array = cafebabe_stream_malloc(s, sizeof(*f->attributes.array) * f->attributes.count); if (!f->attributes.array) goto out; uint16_t attributes_i; for (uint16_t i = 0; i < f->attributes.count; ++i) { if (cafebabe_attribute_info_init(&f->attributes.array[i], s)) { attributes_i = i; goto out_attributes_init; } } attributes_i = f->attributes.count; return 0; out_attributes_init: for (uint16_t i = 0; i < attributes_i; ++i) cafebabe_attribute_info_deinit(&f->attributes.array[i]); free(f->attributes.array); out: return 1; }
int cafebabe_line_number_table_attribute_init( struct cafebabe_line_number_table_attribute *a, struct cafebabe_stream *s) { if (cafebabe_stream_read_uint16(s, &a->line_number_table_length)) goto out; a->line_number_table = cafebabe_stream_malloc(s, sizeof(*a->line_number_table) * a->line_number_table_length); if (!a->line_number_table) goto out; for (uint16_t i = 0; i < a->line_number_table_length; ++i) { struct cafebabe_line_number_table_entry *e = &a->line_number_table[i]; if (cafebabe_stream_read_uint16(s, &e->start_pc)) goto out_line_number_table; if (cafebabe_stream_read_uint16(s, &e->line_number)) goto out_line_number_table; } if (!cafebabe_stream_eof(s)) { s->cafebabe_errno = CAFEBABE_ERROR_EXPECTED_EOF; goto out_line_number_table; } /* Success */ return 0; out_line_number_table: free(a->line_number_table); out: return 1; }
int cafebabe_stack_map_table_attribute_init( struct cafebabe_stack_map_table_attribute *a, struct cafebabe_stream *s) { if (cafebabe_stream_read_uint16(s, &a->stack_map_frame_length)) goto out; a->stack_map_frame = cafebabe_stream_malloc(s, sizeof(*a->stack_map_frame) * a->stack_map_frame_length); if (!a->stack_map_frame) goto out; for (uint16_t i = 0; i < a->stack_map_frame_length; i++) { struct cafebabe_stack_map_frame_entry *e = &a->stack_map_frame[i]; uint8_t raw_tag; if (cafebabe_stream_read_uint8(s, &raw_tag)) goto out_stack_map_frame; /* SAME_FRAME */ if (raw_tag < 64) { e->tag = CAFEBABE_STACK_MAP_TAG_SAME_FRAME; e->offset_delta = raw_tag; } /* SAME_LOCALS_1_STACK_ITEM_FRAME */ else if (raw_tag < 128) { e->tag = CAFEBABE_STACK_MAP_TAG_SAME_LOCAlS_1_STACK_ITEM_FRAME; e->offset_delta = raw_tag - 64; if (cafebabe_stream_read_verification_type_info(s, &e->same_locals_1_stack_item_frame.stack[0])) goto out_stack_map_frame; } /* SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED */ else if (raw_tag == 247) { e->tag = CAFEBABE_STACK_MAP_TAG_SAME_LOCAlS_1_STACK_ITEM_FRAME; if (cafebabe_stream_read_uint16(s, &e->offset_delta)) goto out_stack_map_frame; if (cafebabe_stream_read_verification_type_info(s, &e->same_locals_1_stack_item_frame.stack[0])) goto out_stack_map_frame; } /* CHOP_FRAME */ else if (raw_tag >= 248 && raw_tag < 251) { e->tag = CAFEBABE_STACK_MAP_TAG_CHOP_FRAME; e->chop_frame.chopped = 251 - raw_tag; if (cafebabe_stream_read_uint16(s, &e->offset_delta)) goto out_stack_map_frame; } /* SAME_FRAME_EXTENDED */ else if (raw_tag == 251) { e->tag = CAFEBABE_STACK_MAP_TAG_SAME_FRAME; if (cafebabe_stream_read_uint16(s, &e->offset_delta)) goto out_stack_map_frame; } /* APPEND_FRAME */ else if (raw_tag >= 252 && raw_tag < 255) { e->tag = CAFEBABE_STACK_MAP_TAG_APPEND_FRAME; e->append_frame.nr_locals = raw_tag - 251; if (cafebabe_stream_read_uint16(s, &e->offset_delta)) goto out_stack_map_frame; e->append_frame.locals = cafebabe_stream_malloc(s, sizeof(*e->append_frame.locals) * e->append_frame.nr_locals); if (!e->append_frame.locals) goto out_stack_map_frame; for (uint16_t j = 0; j < e->append_frame.nr_locals; j++) { if (cafebabe_stream_read_verification_type_info(s, &e->append_frame.locals[j])) { free(e->append_frame.locals); goto out_stack_map_frame; } } } /* FULL_FRAME */ else if (raw_tag == 255) { e->tag = CAFEBABE_STACK_MAP_TAG_FULL_FRAME; if (cafebabe_stream_read_uint16(s, &e->offset_delta)) goto out_stack_map_frame; if (cafebabe_stream_read_uint16(s, &e->full_frame.nr_locals)) goto out_stack_map_frame; e->full_frame.locals = cafebabe_stream_malloc(s, sizeof(*e->full_frame.locals) * e->full_frame.nr_locals); if (!e->full_frame.locals) goto out_stack_map_frame; for (uint16_t j = 0; j < e->full_frame.nr_locals; j++) { if (cafebabe_stream_read_verification_type_info(s, &e->full_frame.locals[j])) { free(e->full_frame.locals); goto out_stack_map_frame; } } if (cafebabe_stream_read_uint16(s, &e->full_frame.nr_stack_items)) goto out_stack_map_frame; e->full_frame.stack = cafebabe_stream_malloc(s, sizeof(*e->full_frame.stack) * e->full_frame.nr_stack_items); if (!e->full_frame.stack) goto out_stack_map_frame; for (uint16_t j = 0; j < e->full_frame.nr_stack_items; j++) { if (cafebabe_stream_read_verification_type_info(s, &e->full_frame.stack[j])) { free(e->full_frame.locals); free(e->full_frame.stack); goto out_stack_map_frame; } } } /* Unknown frame type */ else { s->cafebabe_errno = CAFEBABE_ERROR_INVALID_STACK_FRAME_TAG; goto out_stack_map_frame; } } if (!cafebabe_stream_eof(s)) { s->cafebabe_errno = CAFEBABE_ERROR_EXPECTED_EOF; goto out_stack_map_frame; } /* Success */ return 0; out_stack_map_frame: free(a->stack_map_frame); out: return 1; }
int cafebabe_code_attribute_init(struct cafebabe_code_attribute *a, struct cafebabe_stream *s) { if (cafebabe_stream_read_uint16(s, &a->max_stack)) goto out; if (cafebabe_stream_read_uint16(s, &a->max_locals)) goto out; if (cafebabe_stream_read_uint32(s, &a->code_length)) goto out; a->code = cafebabe_stream_pointer(s); if (cafebabe_stream_skip(s, a->code_length)) goto out; if (cafebabe_stream_read_uint16(s, &a->exception_table_length)) goto out; a->exception_table = cafebabe_stream_malloc(s, sizeof(*a->exception_table) * a->exception_table_length); if (!a->exception_table) goto out; for (uint16_t i = 0; i < a->exception_table_length; ++i) { struct cafebabe_code_attribute_exception *e = &a->exception_table[i]; if (cafebabe_stream_read_uint16(s, &e->start_pc)) goto out_exception_table; if (cafebabe_stream_read_uint16(s, &e->end_pc)) goto out_exception_table; if (cafebabe_stream_read_uint16(s, &e->handler_pc)) goto out_exception_table; if (cafebabe_stream_read_uint16(s, &e->catch_type)) goto out_exception_table; } if (cafebabe_stream_read_uint16(s, &a->attributes.count)) goto out_exception_table; a->attributes.array = cafebabe_stream_malloc(s, sizeof(*a->attributes.array) * a->attributes.count); if (!a->attributes.array) goto out_exception_table; uint16_t attributes_i; for (uint16_t i = 0; i < a->attributes.count; ++i) { if (cafebabe_attribute_info_init(&a->attributes.array[i], s)) { attributes_i = i; goto out_attributes_init; } } attributes_i = a->attributes.count; if (!cafebabe_stream_eof(s)) { s->cafebabe_errno = CAFEBABE_ERROR_EXPECTED_EOF; goto out_attributes_init; } /* Success */ return 0; out_attributes_init: for (uint16_t i = 0; i < attributes_i; ++i) cafebabe_attribute_info_deinit(&a->attributes.array[i]); free(a->attributes.array); out_exception_table: free(a->exception_table); out: return 1; }