static int cafebabe_annotation_parse(struct cafebabe_annotation *a, struct cafebabe_stream *s) { int err; err = cafebabe_stream_read_uint16(s, &a->type_index); if (err) goto out; err = cafebabe_stream_read_uint16(s, &a->num_element_value_pairs); if (err) goto out; if (!a->num_element_value_pairs) goto out; a->element_value_pairs = calloc(a->num_element_value_pairs, sizeof(struct cafebabe_element_value_pair)); if (!a->element_value_pairs) { err = -1; goto out; } for (unsigned int i = 0; i < a->num_element_value_pairs; i++) { err = cafebabe_element_value_pair_parse(&a->element_value_pairs[i], s); if (err) goto out; } out: return err; }
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_annotations_attribute_init(struct cafebabe_annotations_attribute *a, struct cafebabe_stream *s) { int err = 0; err = cafebabe_stream_read_uint16(s, &a->num_annotations); if (err) goto out; if (!a->num_annotations) goto out; a->annotations = calloc(a->num_annotations, sizeof(struct cafebabe_annotation)); if (!a->annotations) { err = -1; goto out; } for (unsigned int i = 0; i < a->num_annotations; i++) { err = cafebabe_annotation_parse(&a->annotations[i], s); if (err) goto out; } out: return err; }
int cafebabe_stream_read_verification_type_info(struct cafebabe_stream *s, struct cafebabe_verification_type_info *info) { uint8_t raw_tag; if (cafebabe_stream_read_uint8(s, &raw_tag)) return 1; info->tag = raw_tag; switch(info->tag) { case CAFEBABE_VERIFICATION_TAG_OBJECT_VARIABLE_INFO: if (cafebabe_stream_read_uint16(s, &info->object.cpool_index)) return 1; break; case CAFEBABE_VERIFICATION_TAG_UNINITIALIZED_VARIABLE_INFO: if (cafebabe_stream_read_uint16(s, &info->uninitialized.offset)) return 1; default: break; } return 0; }
static int cafebabe_element_value_pair_parse(struct cafebabe_element_value_pair *p, struct cafebabe_stream *s) { int err; err = cafebabe_stream_read_uint16(s, &p->element_name_index); if (err) goto out; err = cafebabe_element_value_parse(&p->value, s); out: return err; }
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_source_file_attribute_init(struct cafebabe_source_file_attribute *a, struct cafebabe_stream *s) { if (cafebabe_stream_read_uint16(s, &a->sourcefile_index)) goto out; if (!cafebabe_stream_eof(s)) { s->cafebabe_errno = CAFEBABE_ERROR_EXPECTED_EOF; goto out; } /* Success */ return 0; 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; }
static int cafebabe_element_value_parse(struct cafebabe_element_value *v, struct cafebabe_stream *s) { int err; err = cafebabe_stream_read_uint8(s, &v->tag); if (err) goto out; switch (v->tag) { case ELEMENT_TYPE_BYTE: case ELEMENT_TYPE_CHAR: case ELEMENT_TYPE_DOUBLE: case ELEMENT_TYPE_FLOAT: case ELEMENT_TYPE_INTEGER: case ELEMENT_TYPE_LONG: case ELEMENT_TYPE_SHORT: case ELEMENT_TYPE_BOOLEAN: case ELEMENT_TYPE_STRING: { err = cafebabe_stream_read_uint16(s, &v->value.const_value_index); if (err) goto out; break; } case ELEMENT_TYPE_ENUM_CONSTANT: { err = cafebabe_stream_read_uint16(s, &v->value.enum_const_value.type_name_index); if (err) goto out; err = cafebabe_stream_read_uint16(s, &v->value.enum_const_value.const_name_index); if (err) goto out; break; } case ELEMENT_TYPE_CLASS: { err = cafebabe_stream_read_uint16(s, &v->value.class_info_index); if (err) goto out; break; } case ELEMENT_TYPE_ANNOTATION_TYPE: { v->value.annotation_value = malloc(sizeof(struct cafebabe_annotation)); if (!v->value.annotation_value) goto out; err = cafebabe_annotation_parse(v->value.annotation_value, s); if (err) goto out; break; } case ELEMENT_TYPE_ARRAY: { err = cafebabe_stream_read_uint16(s, &v->value.array_value.num_values); if (err) goto out; v->value.array_value.values = calloc(v->value.array_value.num_values, sizeof(struct cafebabe_element_value)); for (unsigned int i = 0; i < v->value.array_value.num_values; i++) { struct cafebabe_element_value *array_value = &v->value.array_value.values[i]; err = cafebabe_element_value_parse(array_value, s); if (err) goto out; } break; } default: warn("unknown annotation element type %d", v->tag); err = -1; goto out; }; out: return err; }