SQLLEN bindpara_wchar_to_msg()

in win_unicode.c [1058:1126]


SQLLEN bindpara_wchar_to_msg(const SQLWCHAR *utf16, char **wcsbuf, SQLLEN used)
{
	SQLLEN	l = (-2);
	char			*ldt = NULL;
	SQLWCHAR	*utf16_nts, *alloc_nts = NULL, ntsbuf[128];
	int		count;

	if (SQL_NTS == used)
	{
		count = ucs2strlen(utf16);
		utf16_nts = (SQLWCHAR *) utf16;
	}
	else if (used < 0)
		return -1;
	else
	{
		count = used / WCLEN;
		if (used + WCLEN <= sizeof(ntsbuf))
			utf16_nts = ntsbuf;
		else
		{
			if (NULL == (alloc_nts = (SQLWCHAR *) malloc(used + WCLEN)))
				return l;
			utf16_nts = alloc_nts;
		}
		memcpy(utf16_nts, utf16, used);
		utf16_nts[count] = 0;
	}

	get_convtype();
MYLOG(MIN_LOG_LEVEL, "\n");
#if defined(__WCS_ISO10646__)
	if (use_wcs)
	{
		if (sizeof(SQLWCHAR) == sizeof(wchar_t))
		{
			ldt = (char *) malloc(2 * count + 1);
			l = wstrtomsg((wchar_t *) utf16_nts, ldt, 2 * count + 1);
		}
		else
		{
			unsigned int	*utf32 = (unsigned int *) malloc((count + 1) * sizeof(unsigned int));

			l = ucs2_to_ucs4(utf16_nts, -1, utf32, count + 1);
			if ((l = wstrtomsg((wchar_t *)utf32, NULL, 0)) >= 0)
			{
				ldt = (char *) malloc(l + 1);
				l = wstrtomsg((wchar_t *)utf32, ldt, l + 1);
			}
			free(utf32);
		}
	}
#endif /* __WCS_ISO10646__ */
#ifdef __CHAR16_UTF_16__
	if (use_c16)
	{
		ldt = (char *) malloc(4 * count + 1);
		l = c16tombs(ldt, (const char16_t *) utf16_nts, 4 * count + 1);
	}
#endif /* __CHAR16_UTF_16__ */
	if (l < 0 && NULL != ldt)
		free(ldt);
	else
		*wcsbuf = ldt;

	if (NULL != alloc_nts)
		free(alloc_nts);
	return l;
}