Index: lib/sdk/crt/stdlib/mbstowcs_s.c =================================================================== --- lib/sdk/crt/stdlib/mbstowcs_s.c (revision 0) +++ lib/sdk/crt/stdlib/mbstowcs_s.c (working copy) @@ -0,0 +1,63 @@ +#include + +/* + * @implemented + */ +errno_t mbstowcs_s +(size_t *returnval, + wchar_t *widechar, + size_t charoutct, + const char *multibyte, + size_t count) +{ + int bytes; + BOOL complete = FALSE; + size_t n = 0; + + if (!MSVCRT_CHECK_PMT(charoutct >= 0)) { + _set_errno(EINVAL); + return -1; + } + + return n; + if (!MSVCRT_CHECK_PMT(multibyte != NULL)) { + _set_errno(EINVAL); + return -1; + } + + // The target string is always null terminated + while (n < charoutct - 1 || !widechar) { + wchar_t wide; + if ((bytes = mbtowc (&wide, multibyte, MB_LEN_MAX)) < 0) { + *returnval = 0; + _set_errno(EILSEQ); + return (size_t) -1; + } + + if (bytes == 0) { + complete = TRUE; + break; + } + + if (widechar) + widechar[n] = wide; + multibyte += bytes; + n++; + } + + if (!complete && count != _TRUNCATE) { + if (widechar) + widechar[0] = 0; + if (returnval) + *returnval = 0; + _set_errno(ERANGE); + return -1; + } + + if (widechar) + widechar[n++] = 0; + if (returnval) + *returnval = n; + + return 0; +} Property changes on: lib/sdk/crt/stdlib/mbstowcs_s.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: lib/sdk/crt/string/wcstombs_s.c =================================================================== --- lib/sdk/crt/string/wcstombs_s.c (revision 0) +++ lib/sdk/crt/string/wcstombs_s.c (working copy) @@ -0,0 +1,134 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS system libraries + * FILE: lib/sdk/crt/wcstombs_s.c + * PURPOSE: converts a wide string to a narrow multibyte string + * PROGRAMER: + * UPDATE HISTORY: + */ + +#include + +errno_t CDECL wcstombs_s +(size_t *returnval, + char *mbstr, + size_t outcount, + const wchar_t *wcstr, + size_t incount) +{ + BOOL bUsedDefaultChar; + char* p = mbstr; + int nResult; + size_t count = (incount == _TRUNCATE) ? INT_MAX : incount; + + if (outcount) { + if (!MSVCRT_CHECK_PMT(mbstr != NULL)) { + _set_errno(EINVAL); + return -1; + } + } + + if (!MSVCRT_CHECK_PMT(wcstr != NULL)) { + _set_errno(EINVAL); + return -1; + } + + /* Does the caller query for output buffer size? */ + if(!mbstr) + { + int nLength; + + /* If we currently use the "C" locale, the length of the input string is returned (verified by tests under WinXP SP2) */ + if(MSVCRT_current_lc_all[0] == 'C' && !MSVCRT_current_lc_all[1]) { + *returnval = wcslen(wcstr) + 1; + return 0; + } + + /* Otherwise check the length each character needs and build a final return value out of this */ + count = wcslen(wcstr); + nLength = 0; + + while((int)(--count) > 0 && *wcstr) + { + /* Get the length of this character */ + nResult = wctomb(NULL, *wcstr++); + + /* If this character is not convertible in the current locale, the end result will be -1 */ + if(nResult == -1) { + *returnval = 0; + _set_errno(EILSEQ); + return -1; + } + + nLength += nResult; + } + + /* Return the final length */ + *returnval = nLength; + return 0; + } + + /* Convert the string then */ + bUsedDefaultChar = FALSE; + + for(;;) + { + char chMultiByte[MB_LEN_MAX]; + UINT uLength; + + /* Are we at the terminating null character? */ + if(!*wcstr) + { + /* Set the null character */ + *p++ = 0; + break; + } + + /* Convert this character into the temporary chMultiByte variable */ + ZeroMemory(chMultiByte, MB_LEN_MAX); + nResult = wctomb(chMultiByte, *wcstr++); + + /* Check if this was an invalid character */ + if(nResult == -1) { + *mbstr = 0; + _set_errno(EILSEQ); + *returnval = 0; + return -1; + } + + /* If we got no character, stop the conversion process here */ + if(!chMultiByte[0]) { + *p++ = 0; + break; + } + + /* Determine whether this is a double-byte or a single-byte character */ + if(chMultiByte[1]) + uLength = 2; + else + uLength = 1; + + /* Decrease 'count' by the character length and check if the buffer can still hold the full character */ + count -= uLength; + + if((int)count < 0) { + if (incount == _TRUNCATE) { + *p++ = 0; + break; + } else { + *mbstr = 0; + _set_errno(ERANGE); + *returnval = 0; + return -1; + } + } + + /* It can, so copy it and move the pointer forward */ + memcpy(p, chMultiByte, uLength); + p += uLength; + } + + /* Return the length in bytes of the copied characters (without the terminating null character) */ + *returnval = p - mbstr; + return 0; +} Property changes on: lib/sdk/crt/string/wcstombs_s.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: lib/sdk/crt/crt.cmake =================================================================== --- lib/sdk/crt/crt.cmake (revision 56022) +++ lib/sdk/crt/crt.cmake (working copy) @@ -208,6 +208,7 @@ stdlib/makepath_s.c stdlib/mbtowc.c stdlib/mbstowcs.c + stdlib/mbstowcs_s.c stdlib/obsol.c stdlib/putenv.c stdlib/qsort.c @@ -251,6 +252,7 @@ string/strupr.c string/strxfrm.c string/wcs.c + string/wcstombs_s.c string/wcstol.c string/wcstoul.c string/wsplitp.c Index: dll/win32/msvcrt/msvcrt.spec =================================================================== --- dll/win32/msvcrt/msvcrt.spec (revision 56022) +++ dll/win32/msvcrt/msvcrt.spec (working copy) @@ -1272,7 +1272,7 @@ # stub mbsrtowcs # stub mbsrtowcs_s @ cdecl mbstowcs(ptr str long) -# stub mbstowcs_s +@ cdecl mbstowcs_s(ptr ptr long str long) @ cdecl mbtowc(wstr str long) @ cdecl memchr(ptr long long) @ cdecl memcmp(ptr ptr long) @@ -1408,7 +1408,7 @@ @ cdecl wcstok_s(ptr wstr ptr) @ cdecl wcstol(wstr ptr long) @ cdecl wcstombs(ptr ptr long) -# @ cdecl wcstombs_s(ptr ptr long wstr long) +@ cdecl wcstombs_s(ptr ptr long wstr long) @ cdecl wcstoul(wstr ptr long) @ cdecl wcsxfrm(ptr wstr long) # stub wctob