if (!same_encoding || wcs_debug)
{
mylog("%s:locale param convert\n", __FUNCTION__);
- if ((used = bindpara_msg_to_utf8(buffer, &allocbuf)) < 0)
+ if ((used = bindpara_msg_to_utf8(buffer, &allocbuf, used)) < 0)
{
qb->errormsg = "Could not convert from the current locale to wide characters";
qb->errornumber = STMT_EXEC_ERROR;
if (!is_utf8 || (same_encoding && wcs_debug))
{
mylog("%s:hybrid param convert\n", __FUNCTION__);
- if ((used = bindpara_wchar_to_msg((SQLWCHAR *) buffer, &allocbuf)) < 0)
+ if ((used = bindpara_wchar_to_msg((SQLWCHAR *) buffer, &allocbuf, used)) < 0)
{
qb->errormsg = "Could not convert from wide characters to the current locale";
qb->errornumber = STMT_EXEC_ERROR;
SQLLEN bindcol_hybrid_exec(SQLWCHAR *utf16, const char *ldt, size_t n, BOOL lf_conv, char **wcsbuf);
SQLLEN bindcol_localize_estimate(const char *utf8dt, BOOL lf_conv, char **wcsbuf);
SQLLEN bindcol_localize_exec(char *ldt, size_t n, BOOL lf_conv, char **wcsbuf);
-SQLLEN bindpara_msg_to_utf8(const char *ldt, char **wcsbuf);
-SQLLEN bindpara_wchar_to_msg(const SQLWCHAR *utf16, char **wcsbuf);
+SQLLEN bindpara_msg_to_utf8(const char *ldt, char **wcsbuf, SQLLEN used);
+SQLLEN bindpara_wchar_to_msg(const SQLWCHAR *utf16, char **wcsbuf, SQLLEN used);
SQLLEN locale_to_sqlwchar(SQLWCHAR *utf16, const char *ldt, size_t n, BOOL lf_conv);
SQLLEN utf8_to_locale(char *ldt, const char * utf8dt, size_t n, BOOL lf_conv);
// SQLBindParameter SQL_C_CHAR to UTF-8 case
// the current locale => UTF-8
//
-SQLLEN bindpara_msg_to_utf8(const char *ldt, char **wcsbuf)
+SQLLEN bindpara_msg_to_utf8(const char *ldt, char **wcsbuf, SQLLEN used)
{
SQLLEN l = (-2);
- char *utf8 = NULL;
- int count = strlen(ldt);
+ char *utf8 = NULL, *ldt_nts, *alloc_nts = NULL, ntsbuf[128];
+ int count;
+
+ if (SQL_NTS == used)
+ {
+ count = strlen(ldt);
+ ldt_nts = (char *) ldt;
+ }
+ else if (used < 0)
+ {
+ return -1;
+ }
+ else
+ {
+ count = used;
+ if (used < sizeof(ntsbuf))
+ ldt_nts = ntsbuf;
+ else
+ {
+ if (NULL == (alloc_nts = malloc(used + 1)))
+ return l;
+ ldt_nts = alloc_nts;
+ }
+ memcpy(ldt_nts, ldt, used);
+ ldt_nts[used] = '\0';
+ }
get_convtype();
mylog(" %s\n", __FUNCTION__);
{
wchar_t *wcsdt = (wchar_t *) malloc((count + 1) * sizeof(wchar_t));
- if ((l = msgtowstr(ldt, (wchar_t *) wcsdt, count + 1)) >= 0)
+ if ((l = msgtowstr(ldt_nts, (wchar_t *) wcsdt, count + 1)) >= 0)
utf8 = wcs_to_utf8(wcsdt, -1, &l, FALSE);
free(wcsdt);
}
{
SQLWCHAR *utf16 = (SQLWCHAR *) malloc((count + 1) * sizeof(SQLWCHAR));
- if ((l = mbstoc16_lf((char16_t *) utf16, ldt, count + 1, FALSE)) >= 0)
+ if ((l = mbstoc16_lf((char16_t *) utf16, ldt_nts, count + 1, FALSE)) >= 0)
utf8 = ucs2_to_utf8(utf16, -1, &l, FALSE);
free(utf16);
}
else
*wcsbuf = (char *) utf8;
+ if (NULL != alloc_nts)
+ free(alloc_nts);
return l;
}
// SQLBindParameter hybrid case
// SQLWCHAR(UTF-16) => the current locale
//
-SQLLEN bindpara_wchar_to_msg(const SQLWCHAR *utf16, char **wcsbuf)
+SQLLEN bindpara_wchar_to_msg(const SQLWCHAR *utf16, char **wcsbuf, SQLLEN used)
{
SQLLEN l = (-2);
char *ldt = NULL;
- int count = ucs2strlen(utf16);
+ 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(" %s\n", __FUNCTION__);
if (sizeof(SQLWCHAR) == sizeof(wchar_t))
{
ldt = (char *) malloc(2 * count + 1);
- l = wstrtomsg((wchar_t *) utf16, ldt, 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, -1, utf32, count + 1);
+ l = ucs2_to_ucs4(utf16_nts, -1, utf32, count + 1);
if ((l = wstrtomsg((wchar_t *)utf32, NULL, 0)) >= 0)
{
ldt = (char *) malloc(l + 1);
if (use_c16)
{
ldt = (char *) malloc(4 * count + 1);
- l = c16tombs(ldt, (const char16_t *) utf16, 4 * count + 1);
+ l = c16tombs(ldt, (const char16_t *) utf16_nts, 4 * count + 1);
}
#endif /* __CHAR16_UTF_16__ */
if (l < 0 && NULL != ldt)
else
*wcsbuf = ldt;
+ if (NULL != alloc_nts)
+ free(alloc_nts);
return l;
}