FreeBSD/sys/dev/hyperv/utilities/hv_kvp.c
/*
 * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
 * Ensure utf16_utf8 takes care of the additional string terminating char!!
 */
Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
Ensure utf16_utf8 takes care of the additional string terminating char
Prepare a user kvp msg based on host kvp msg ( utf16 to utf8 ) Ensure utf16_utf8 takes care of the additional string terminating char
static void
hv_kvp_convert_hostmsg_to_usermsg(struct hv_kvp_msg *hmsg, struct hv_kvp_msg *umsg)
{
	int utf_err = 0;
	uint32_t value_type;
	struct hv_kvp_ip_msg *host_ip_msg;
	host_ip_msg = (struct hv_kvp_ip_msg*)hmsg;
	memset(umsg, 0, sizeof(struct hv_kvp_msg));
	umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
	umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
	switch (umsg->kvp_hdr.operation) {
	case HV_KVP_OP_SET_IP_INFO:
		hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
		break;
	case HV_KVP_OP_GET_IP_INFO:
		utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
		    MAX_ADAPTER_ID_SIZE,
		    (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
		    MAX_ADAPTER_ID_SIZE, 1, &utf_err);
		umsg->body.kvp_ip_val.addr_family =
		    host_ip_msg->kvp_ip_val.addr_family;
		break;
	case HV_KVP_OP_SET:
		value_type = hmsg->body.kvp_set.data.value_type;
		switch (value_type) {
		case HV_REG_SZ:
			umsg->body.kvp_set.data.value_size =
			    utf16_to_utf8(
				(char *)umsg->body.kvp_set.data.msg_value.value,
				HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
				(uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
				hmsg->body.kvp_set.data.value_size,
				1, &utf_err);
			umsg->body.kvp_set.data.value_size =
			    umsg->body.kvp_set.data.value_size / 2;
			break;
		case HV_REG_U32:
			umsg->body.kvp_set.data.value_size =
			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
				hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
			break;
		case HV_REG_U64:
			umsg->body.kvp_set.data.value_size =
			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
				(unsigned long long)
				hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
			break;
		}
		umsg->body.kvp_set.data.key_size =
		    utf16_to_utf8(
			umsg->body.kvp_set.data.key,
			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
			(uint16_t *)hmsg->body.kvp_set.data.key,
			hmsg->body.kvp_set.data.key_size,
			1, &utf_err);
		umsg->body.kvp_set.data.key_size =
		    umsg->body.kvp_set.data.key_size / 2;
		break;
	case HV_KVP_OP_GET:
		umsg->body.kvp_get.data.key_size =
		    utf16_to_utf8(umsg->body.kvp_get.data.key,
			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
			(uint16_t *)hmsg->body.kvp_get.data.key,
			hmsg->body.kvp_get.data.key_size,
			1, &utf_err);
		umsg->body.kvp_get.data.key_size =
		    umsg->body.kvp_get.data.key_size / 2;
		break;
	case HV_KVP_OP_DELETE:
		umsg->body.kvp_delete.key_size =
		    utf16_to_utf8(umsg->body.kvp_delete.key,
			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
			(uint16_t *)hmsg->body.kvp_delete.key,
			hmsg->body.kvp_delete.key_size,
			1, &utf_err);
		umsg->body.kvp_delete.key_size =
		    umsg->body.kvp_delete.key_size / 2;
		break;
	case HV_KVP_OP_ENUMERATE:
		umsg->body.kvp_enum_data.index =
		    hmsg->body.kvp_enum_data.index;
		break;
	default:
		hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
		    __func__, umsg->kvp_hdr.operation);
	}
}
static void hv_kvp_convert_hostmsg_to_usermsg ( struct hv_kvp_msg * hmsg , struct hv_kvp_msg * umsg ) { int utf_err = 0 ; uint32_t value_type ; struct hv_kvp_ip_msg * host_ip_msg ; host_ip_msg = ( struct hv_kvp_ip_msg * ) hmsg ; memset ( umsg , 0 , sizeof ( struct hv_kvp_msg ) ) ; umsg -> kvp_hdr . operation = hmsg -> kvp_hdr . operation ; umsg -> kvp_hdr . pool = hmsg -> kvp_hdr . pool ; switch ( umsg -> kvp_hdr . operation ) { case HV_KVP_OP_SET_IP_INFO : hv_kvp_convert_utf16_ipinfo_to_utf8 ( host_ip_msg , umsg ) ; break ; case HV_KVP_OP_GET_IP_INFO : utf16_to_utf8 ( ( char * ) umsg -> body . kvp_ip_val . adapter_id , MAX_ADAPTER_ID_SIZE , ( uint16_t * ) host_ip_msg -> kvp_ip_val . adapter_id , MAX_ADAPTER_ID_SIZE , 1 , & utf_err ) ; umsg -> body . kvp_ip_val . addr_family = host_ip_msg -> kvp_ip_val . addr_family ; break ; case HV_KVP_OP_SET : value_type = hmsg -> body . kvp_set . data . value_type ; switch ( value_type ) { case HV_REG_SZ : umsg -> body . kvp_set . data . value_size = utf16_to_utf8 ( ( char * ) umsg -> body . kvp_set . data . msg_value . value , HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1 , ( uint16_t * ) hmsg -> body . kvp_set . data . msg_value . value , hmsg -> body . kvp_set . data . value_size , 1 , & utf_err ) ; umsg -> body . kvp_set . data . value_size = umsg -> body . kvp_set . data . value_size / 2 ; break ; case HV_REG_U32 : umsg -> body . kvp_set . data . value_size = sprintf ( umsg -> body . kvp_set . data . msg_value . value , " " , hmsg -> body . kvp_set . data . msg_value . value_u32 ) + 1 ; break ; case HV_REG_U64 : umsg -> body . kvp_set . data . value_size = sprintf ( umsg -> body . kvp_set . data . msg_value . value , " " , ( unsigned long long ) hmsg -> body . kvp_set . data . msg_value . value_u64 ) + 1 ; break ; } umsg -> body . kvp_set . data . key_size = utf16_to_utf8 ( umsg -> body . kvp_set . data . key , HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1 , ( uint16_t * ) hmsg -> body . kvp_set . data . key , hmsg -> body . kvp_set . data . key_size , 1 , & utf_err ) ; umsg -> body . kvp_set . data . key_size = umsg -> body . kvp_set . data . key_size / 2 ; break ; case HV_KVP_OP_GET : umsg -> body . kvp_get . data . key_size = utf16_to_utf8 ( umsg -> body . kvp_get . data . key , HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1 , ( uint16_t * ) hmsg -> body . kvp_get . data . key , hmsg -> body . kvp_get . data . key_size , 1 , & utf_err ) ; umsg -> body . kvp_get . data . key_size = umsg -> body . kvp_get . data . key_size / 2 ; break ; case HV_KVP_OP_DELETE : umsg -> body . kvp_delete . key_size = utf16_to_utf8 ( umsg -> body . kvp_delete . key , HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1 , ( uint16_t * ) hmsg -> body . kvp_delete . key , hmsg -> body . kvp_delete . key_size , 1 , & utf_err ) ; umsg -> body . kvp_delete . key_size = umsg -> body . kvp_delete . key_size / 2 ; break ; case HV_KVP_OP_ENUMERATE : umsg -> body . kvp_enum_data . index = hmsg -> body . kvp_enum_data . index ; break ; default : hv_kvp_log_info ( " \n " , __func__ , umsg -> kvp_hdr . operation ) ; } }
Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
 Ensure utf16_utf8 takes care of the additional string terminating char!
Prepare a user kvp msg based on host kvp msg ( utf16 to utf8 ) Ensure utf16_utf8 takes care of the additional string terminating char!
/* utf8 encoding */ /* utf8 encoding */ /* utf8 encoding */ /* utf8 encoding */