static DexClassDef *dex_class_def_new(RzBuffer *buf, ut64 offset, ut64 base, RzPVector *method_ids) {
	DexClassDef *class_def = RZ_NEW0(DexClassDef);
	if (!class_def) {
		return NULL;
	}

	ut64 static_fields_size = 0;
	ut64 instance_fields_size = 0;
	ut64 direct_methods_size = 0;
	ut64 virtual_methods_size = 0;
	ut64 diff_value_prev;

	class_def->static_fields = /*  */ rz_list_newf((RzListFree)free);
	class_def->instance_fields = /**/ rz_list_newf((RzListFree)free);
	class_def->direct_methods = /* */ rz_list_newf((RzListFree)free);
	class_def->virtual_methods = /**/ rz_list_newf((RzListFree)free);

	read_le16_or_fail(buf, class_def->class_idx, dex_class_def_new_fail);
	read_le16_or_fail(buf, class_def->_padding1, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->access_flags, dex_class_def_new_fail);
	read_le16_or_fail(buf, class_def->superclass_idx, dex_class_def_new_fail);
	read_le16_or_fail(buf, class_def->_padding2, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->interfaces_offset, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->source_file_idx, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->annotations_offset, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->class_data_offset, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->static_values_offset, dex_class_def_new_fail);
	class_def->offset = offset;

	if (class_def->interfaces_offset > 0) {
		if (rz_buf_seek(buf, class_def->interfaces_offset, RZ_BUF_SET) < 0) {
			goto dex_class_def_new_fail;
		}
		read_le32_or_fail(buf, class_def->n_interfaces, dex_class_def_new_fail);
		if (class_def->n_interfaces > 0) {
			class_def->interfaces = RZ_NEWS0(ut16, class_def->n_interfaces);
			if (!class_def->interfaces) {
				goto dex_class_def_new_fail;
			}
			for (ut32 i = 0; i < class_def->n_interfaces; ++i) {
				read_le16_or_fail(buf, class_def->interfaces[i], dex_class_def_new_fail);
			}
		}
	}

	if (!class_def->class_data_offset) {
		return class_def;
	}

	if (rz_buf_seek(buf, class_def->class_data_offset, RZ_BUF_SET) < 0) {
		goto dex_class_def_new_fail;
	}

	rz_buf_uleb128(buf, &static_fields_size);
	rz_buf_uleb128(buf, &instance_fields_size);
	rz_buf_uleb128(buf, &direct_methods_size);
	rz_buf_uleb128(buf, &virtual_methods_size);

	for (ut64 i = 0; i < static_fields_size; ++i) {
		DexEncodedField *encoded_field = dex_new_encoded_field(buf, base, &diff_value_prev, i < 1);
		if (!encoded_field || !rz_list_append(class_def->static_fields, encoded_field)) {
			free(encoded_field);
			goto dex_class_def_new_fail;
		}
	}

	for (ut64 i = 0; i < instance_fields_size; ++i) {
		DexEncodedField *encoded_field = dex_new_encoded_field(buf, base, &diff_value_prev, i < 1);
		if (!encoded_field || !rz_list_append(class_def->instance_fields, encoded_field)) {
			free(encoded_field);
			goto dex_class_def_new_fail;
		}
	}

	for (ut64 i = 0; i < direct_methods_size; ++i) {
		DexEncodedMethod *encoded_method = dex_new_encoded_method(buf, base, &diff_value_prev, i < 1, method_ids);
		if (!encoded_method || !rz_list_append(class_def->direct_methods, encoded_method)) {
			free(encoded_method);
			goto dex_class_def_new_fail;
		}
	}

	for (ut64 i = 0; i < virtual_methods_size; ++i) {
		DexEncodedMethod *encoded_method = dex_new_encoded_method(buf, base, &diff_value_prev, i < 1, method_ids);
		if (!encoded_method || !rz_list_append(class_def->virtual_methods, encoded_method)) {
			free(encoded_method);
			goto dex_class_def_new_fail;
		}
	}

	return class_def;

dex_class_def_new_fail:
	dex_class_def_free(class_def);
	return NULL;
}
static DexClassDef *dex_class_def_new(RzBuffer *buf, ut64 offset, ut64 base, RzPVector *method_ids) {
	DexClassDef *class_def = RZ_NEW0(DexClassDef);
	if (!class_def) {
		return NULL;
	}

	ut64 static_fields_size = 0;
	ut64 instance_fields_size = 0;
	ut64 direct_methods_size = 0;
	ut64 virtual_methods_size = 0;
	ut64 diff_value_prev;

	class_def->static_fields =   rz_list_newf((RzListFree)free);
	class_def->instance_fields =   rz_list_newf((RzListFree)free);
	class_def->direct_methods =   rz_list_newf((RzListFree)free);
	class_def->virtual_methods =   rz_list_newf((RzListFree)free);

	read_le16_or_fail(buf, class_def->class_idx, dex_class_def_new_fail);
	read_le16_or_fail(buf, class_def->_padding1, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->access_flags, dex_class_def_new_fail);
	read_le16_or_fail(buf, class_def->superclass_idx, dex_class_def_new_fail);
	read_le16_or_fail(buf, class_def->_padding2, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->interfaces_offset, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->source_file_idx, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->annotations_offset, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->class_data_offset, dex_class_def_new_fail);
	read_le32_or_fail(buf, class_def->static_values_offset, dex_class_def_new_fail);
	class_def->offset = offset;

	if (class_def->interfaces_offset > 0) {
		if (rz_buf_seek(buf, class_def->interfaces_offset, RZ_BUF_SET) < 0) {
			goto dex_class_def_new_fail;
		}
		read_le32_or_fail(buf, class_def->n_interfaces, dex_class_def_new_fail);
		if (class_def->n_interfaces > 0) {
			class_def->interfaces = RZ_NEWS0(ut16, class_def->n_interfaces);
			if (!class_def->interfaces) {
				goto dex_class_def_new_fail;
			}
			for (ut32 i = 0; i < class_def->n_interfaces; ++i) {
				read_le16_or_fail(buf, class_def->interfaces[i], dex_class_def_new_fail);
			}
		}
	}

	if (!class_def->class_data_offset) {
		return class_def;
	}

	if (rz_buf_seek(buf, class_def->class_data_offset, RZ_BUF_SET) < 0) {
		goto dex_class_def_new_fail;
	}

	rz_buf_uleb128(buf, &static_fields_size);
	rz_buf_uleb128(buf, &instance_fields_size);
	rz_buf_uleb128(buf, &direct_methods_size);
	rz_buf_uleb128(buf, &virtual_methods_size);

	for (ut64 i = 0; i < static_fields_size; ++i) {
		DexEncodedField *encoded_field = dex_new_encoded_field(buf, base, &diff_value_prev, i < 1);
		if (!encoded_field || !rz_list_append(class_def->static_fields, encoded_field)) {
			free(encoded_field);
			goto dex_class_def_new_fail;
		}
	}

	for (ut64 i = 0; i < instance_fields_size; ++i) {
		DexEncodedField *encoded_field = dex_new_encoded_field(buf, base, &diff_value_prev, i < 1);
		if (!encoded_field || !rz_list_append(class_def->instance_fields, encoded_field)) {
			free(encoded_field);
			goto dex_class_def_new_fail;
		}
	}

	for (ut64 i = 0; i < direct_methods_size; ++i) {
		DexEncodedMethod *encoded_method = dex_new_encoded_method(buf, base, &diff_value_prev, i < 1, method_ids);
		if (!encoded_method || !rz_list_append(class_def->direct_methods, encoded_method)) {
			free(encoded_method);
			goto dex_class_def_new_fail;
		}
	}

	for (ut64 i = 0; i < virtual_methods_size; ++i) {
		DexEncodedMethod *encoded_method = dex_new_encoded_method(buf, base, &diff_value_prev, i < 1, method_ids);
		if (!encoded_method || !rz_list_append(class_def->virtual_methods, encoded_method)) {
			free(encoded_method);
			goto dex_class_def_new_fail;
		}
	}

	return class_def;

dex_class_def_new_fail:
	dex_class_def_free(class_def);
	return NULL;
}