| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <corecrt_internal_mbstring.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
| #include <corecrt_internal_securecrt.h>
|
| #include <ctype.h>
|
| #include <errno.h>
|
| #include <locale.h>
|
| #include <stdint.h>
|
| #include <stdlib.h>
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| _Post_satisfies_(return <= cnt && return <= _String_length_(string + 1))
|
| static size_t __cdecl wcsncnt (
|
| _In_reads_or_z_(cnt) const wchar_t *string,
|
| _In_ size_t cnt
|
| )
|
| {
|
| size_t n = cnt+1;
|
| wchar_t *cp = (wchar_t *)string;
|
|
|
| while (--n && *cp)
|
| cp++;
|
|
|
| if (n && !*cp)
|
| return cp - string + 1;
|
| return cnt;
|
| }
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| _Success_(return != static_cast<size_t>(-1))
|
| static size_t __cdecl _wcstombs_l_helper(
|
| _Out_writes_(n) char * s,
|
| _In_z_ const wchar_t * pwcs,
|
| _In_ size_t n,
|
| _Inout_ __crt_cached_ptd_host& ptd
|
| )
|
| {
|
| size_t count = 0;
|
| int i, retval;
|
| char buffer[MB_LEN_MAX];
|
| BOOL defused = 0;
|
|
|
| if (s && n == 0)
|
|
|
| return 0;
|
|
|
|
|
| _UCRT_VALIDATE_RETURN(ptd, pwcs != nullptr, EINVAL, (size_t)-1);
|
|
|
|
|
|
|
| const _locale_t locale = ptd.get_locale();
|
|
|
| if (locale->locinfo->_public._locale_lc_codepage == CP_UTF8)
|
| {
|
| mbstate_t state{};
|
| return __crt_mbstring::__wcsrtombs_utf8(s, &pwcs, n, &state, ptd);
|
| }
|
|
|
| if (s)
|
| {
|
| if ( locale->locinfo->locale_name[LC_CTYPE] == nullptr )
|
| {
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| while(count < n)
|
| {
|
| if (static_cast<uint16_t>(*pwcs) > 0xFF)
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| return (size_t)-1;
|
| }
|
| s[count] = (char) *pwcs;
|
| if (*pwcs++ == L'\0')
|
| {
|
| return count;
|
| }
|
| count++;
|
| }
|
| return count;
|
| }
|
| else
|
| {
|
|
|
| if (1 == locale->locinfo->_public._locale_mb_cur_max)
|
| {
|
|
|
|
|
|
|
| if (n > 0)
|
| {
|
| n = wcsncnt(pwcs, n);
|
| }
|
| if ( ((count = __acrt_WideCharToMultiByte( locale->locinfo->_public._locale_lc_codepage,
|
| 0,
|
| pwcs,
|
| (int)n,
|
| s,
|
| (int)n,
|
| nullptr,
|
| &defused )) != 0) &&
|
| (!defused) )
|
| {
|
| if (s[count - 1] == '\0')
|
| {
|
| count--;
|
| }
|
|
|
| return count;
|
| }
|
|
|
| ptd.get_errno().set(EILSEQ);
|
| return (size_t)-1;
|
| }
|
| else
|
| {
|
|
|
|
|
|
|
|
|
| if ( ((count = __acrt_WideCharToMultiByte( locale->locinfo->_public._locale_lc_codepage,
|
| 0,
|
| pwcs,
|
| -1,
|
| s,
|
| (int)n,
|
| nullptr,
|
| &defused )) != 0) &&
|
| (!defused) )
|
| {
|
| return count - 1;
|
| }
|
|
|
| if (defused || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| return (size_t)-1;
|
| }
|
|
|
|
|
| while (count < n)
|
| {
|
| int mb_cur_max = locale->locinfo->_public._locale_mb_cur_max;
|
| if ( ((retval = __acrt_WideCharToMultiByte( locale->locinfo->_public._locale_lc_codepage,
|
| 0,
|
| pwcs,
|
| 1,
|
| buffer,
|
| __min(MB_LEN_MAX, mb_cur_max),
|
| nullptr,
|
| &defused )) == 0)
|
| || defused )
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| return (size_t)-1;
|
| }
|
|
|
|
|
| if (retval < 0 ||
|
| retval > _countof(buffer))
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| return (size_t)-1;
|
| }
|
|
|
| if (count + retval > n)
|
| return count;
|
|
|
| for (i = 0; i < retval; i++, count++)
|
| {
|
| if((s[count] = buffer[i])=='\0')
|
| {
|
| return count;
|
| }
|
| }
|
|
|
| pwcs++;
|
| }
|
|
|
| return count;
|
| }
|
| }
|
| }
|
| else
|
| {
|
| if ( locale->locinfo->locale_name[LC_CTYPE] == nullptr )
|
| {
|
| size_t len = 0;
|
| for (wchar_t *pw = (wchar_t *)pwcs; *pw != 0; pw++)
|
| {
|
| if (*pw > 255)
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| return (size_t)-1;
|
| }
|
| ++len;
|
| }
|
|
|
| return len;
|
| }
|
| else
|
| {
|
| if ( ((count = __acrt_WideCharToMultiByte( locale->locinfo->_public._locale_lc_codepage,
|
| 0,
|
| pwcs,
|
| -1,
|
| nullptr,
|
| 0,
|
| nullptr,
|
| &defused )) == 0) ||
|
| (defused) )
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| return (size_t)-1;
|
| }
|
|
|
| return count - 1;
|
| }
|
| }
|
| }
|
|
|
| extern "C" size_t __cdecl _wcstombs_l(
|
| char * s,
|
| const wchar_t * pwcs,
|
| size_t n,
|
| _locale_t plocinfo
|
| )
|
| {
|
| __crt_cached_ptd_host ptd(plocinfo);
|
| return _wcstombs_l_helper(s, pwcs, n, ptd);
|
| }
|
|
|
| extern "C" size_t __cdecl wcstombs(
|
| char * s,
|
| const wchar_t * pwcs,
|
| size_t n
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _wcstombs_l_helper(s, pwcs, n, ptd);
|
| }
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| static errno_t __cdecl _wcstombs_internal (
|
| size_t * pConvertedChars,
|
| char * dst,
|
| size_t sizeInBytes,
|
| const wchar_t * src,
|
| size_t n,
|
| __crt_cached_ptd_host& ptd
|
| )
|
| {
|
| size_t retsize;
|
| errno_t retvalue = 0;
|
|
|
|
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, (dst != nullptr && sizeInBytes > 0) || (dst == nullptr && sizeInBytes == 0), EINVAL);
|
| if (dst != nullptr)
|
| {
|
| _RESET_STRING(dst, sizeInBytes);
|
| }
|
|
|
| if (pConvertedChars != nullptr)
|
| {
|
| *pConvertedChars = 0;
|
| }
|
|
|
| size_t bufferSize = n > sizeInBytes ? sizeInBytes : n;
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, bufferSize <= INT_MAX, EINVAL);
|
|
|
| retsize = _wcstombs_l_helper(dst, src, bufferSize, ptd);
|
|
|
| if (retsize == (size_t)-1)
|
| {
|
| if (dst != nullptr)
|
| {
|
| _RESET_STRING(dst, sizeInBytes);
|
| }
|
| return ptd.get_errno().value_or(0);
|
| }
|
|
|
|
|
| retsize++;
|
|
|
| if (dst != nullptr)
|
| {
|
|
|
| if (retsize > sizeInBytes)
|
| {
|
| if (n != _TRUNCATE)
|
| {
|
| _RESET_STRING(dst, sizeInBytes);
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, sizeInBytes > retsize, ERANGE);
|
| }
|
| retsize = sizeInBytes;
|
| retvalue = STRUNCATE;
|
| }
|
|
|
|
|
| dst[retsize - 1] = '\0';
|
| }
|
|
|
| if (pConvertedChars != nullptr)
|
| {
|
| *pConvertedChars = retsize;
|
| }
|
|
|
| return retvalue;
|
| }
|
|
|
| extern "C" errno_t __cdecl _wcstombs_s_l (
|
| size_t *pConvertedChars,
|
| char * dst,
|
| size_t sizeInBytes,
|
| const wchar_t * src,
|
| size_t n,
|
| _locale_t plocinfo
|
| )
|
| {
|
| __crt_cached_ptd_host ptd(plocinfo);
|
| return _wcstombs_internal(pConvertedChars, dst, sizeInBytes, src, n, ptd);
|
| }
|
|
|
| extern "C" errno_t __cdecl wcstombs_s (
|
| size_t *pConvertedChars,
|
| char * dst,
|
| size_t sizeInBytes,
|
| const wchar_t * src,
|
| size_t n
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _wcstombs_internal(pConvertedChars, dst, sizeInBytes, src, n, ptd);
|
| }
|
|
|