| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #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 <stdlib.h>
|
|
|
| using namespace __crt_mbstring;
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
|
|
|
|
| static size_t __cdecl _mbstowcs_l_helper(
|
| _Out_writes_opt_z_(n) wchar_t * pwcs,
|
| _In_reads_or_z_(n) _Pre_z_ const char * s,
|
| _In_ size_t n,
|
| _In_opt_ __crt_cached_ptd_host& ptd
|
| ) throw()
|
| {
|
| size_t count = 0;
|
|
|
| if (pwcs && n == 0)
|
| {
|
|
|
| return (size_t) 0;
|
| }
|
|
|
| if (pwcs && n > 0)
|
| {
|
| *pwcs = '\0';
|
| }
|
|
|
|
|
| _UCRT_VALIDATE_RETURN(ptd, s != nullptr, EINVAL, (size_t) - 1);
|
|
|
| _locale_t const locale = ptd.get_locale();
|
|
|
| if (locale->locinfo->_public._locale_lc_codepage == CP_UTF8)
|
| {
|
| mbstate_t state{};
|
| return __mbsrtowcs_utf8(pwcs, &s, n, &state, ptd);
|
| }
|
|
|
|
|
| if (pwcs)
|
| {
|
| if (locale->locinfo->locale_name[LC_CTYPE] == nullptr)
|
| {
|
|
|
| while (count < n)
|
| {
|
| *pwcs = (wchar_t) ((unsigned char) s[count]);
|
| if (!s[count])
|
| {
|
| return count;
|
| }
|
| count++;
|
| pwcs++;
|
| }
|
| return count;
|
|
|
| }
|
| else {
|
| int bytecnt, charcnt;
|
| unsigned char *p;
|
|
|
|
|
| if ((count = __acrt_MultiByteToWideChar(locale->locinfo->_public._locale_lc_codepage,
|
| MB_PRECOMPOSED |
|
| MB_ERR_INVALID_CHARS,
|
| s,
|
| -1,
|
| pwcs,
|
| (int) n)) != 0)
|
| {
|
| return count - 1;
|
| }
|
|
|
| if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| *pwcs = '\0';
|
| return (size_t) - 1;
|
| }
|
|
|
|
|
|
|
|
|
| charcnt = (int) n;
|
| for (p = (unsigned char *) s; (charcnt-- && *p); p++)
|
| {
|
| if (_isleadbyte_fast_internal(*p, locale))
|
| {
|
| if (p[1] == '\0')
|
| {
|
| |
| |
| |
| |
|
|
| ptd.get_errno().set(EILSEQ);
|
| *pwcs = '\0';
|
| return (size_t) - 1;
|
| }
|
| else
|
| {
|
| p++;
|
| }
|
| }
|
| }
|
| bytecnt = ((int) ((char *) p - (char *) s));
|
|
|
| if ((count = __acrt_MultiByteToWideChar(locale->locinfo->_public._locale_lc_codepage,
|
| MB_PRECOMPOSED,
|
| s,
|
| bytecnt,
|
| pwcs,
|
| (int) n)) == 0)
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| *pwcs = '\0';
|
| return (size_t) - 1;
|
| }
|
|
|
| return count;
|
| }
|
| }
|
| else {
|
|
|
| if (locale->locinfo->locale_name[LC_CTYPE] == nullptr)
|
| {
|
| return strlen(s);
|
| }
|
| else if ((count = __acrt_MultiByteToWideChar(locale->locinfo->_public._locale_lc_codepage,
|
| MB_PRECOMPOSED | MB_ERR_INVALID_CHARS,
|
| s,
|
| -1,
|
| nullptr,
|
| 0)) == 0)
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| return (size_t) - 1;
|
| }
|
| else
|
| {
|
| return count - 1;
|
| }
|
| }
|
|
|
| }
|
|
|
| extern "C" size_t __cdecl _mbstowcs_l(
|
| wchar_t *pwcs,
|
| const char *s,
|
| size_t n,
|
| _locale_t plocinfo
|
| )
|
| {
|
|
|
| __crt_cached_ptd_host ptd(plocinfo);
|
| return _mbstowcs_l_helper(pwcs, s, n, ptd);
|
| }
|
|
|
| extern "C" size_t __cdecl mbstowcs(
|
| wchar_t *pwcs,
|
| const char *s,
|
| size_t n
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _mbstowcs_l_helper(pwcs, s, n, ptd);
|
| }
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| static errno_t __cdecl _mbstowcs_internal(
|
| size_t * pConvertedChars,
|
| wchar_t * pwcs,
|
| size_t sizeInWords,
|
| const char * s,
|
| size_t n,
|
| __crt_cached_ptd_host& ptd
|
| )
|
| {
|
| size_t retsize;
|
| errno_t retvalue = 0;
|
|
|
|
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, (pwcs == nullptr && sizeInWords == 0) || (pwcs != nullptr && sizeInWords > 0), EINVAL);
|
|
|
| if (pwcs != nullptr)
|
| {
|
| _RESET_STRING(pwcs, sizeInWords);
|
| }
|
|
|
| if (pConvertedChars != nullptr)
|
| {
|
| *pConvertedChars = 0;
|
| }
|
|
|
| size_t bufferSize = n > sizeInWords ? sizeInWords : n;
|
|
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, bufferSize <= INT_MAX, EINVAL);
|
|
|
|
|
|
|
| retsize = _mbstowcs_l_helper(pwcs, s, bufferSize, ptd);
|
|
|
| if (retsize == (size_t) - 1)
|
| {
|
| if (pwcs != nullptr)
|
| {
|
| _RESET_STRING(pwcs, sizeInWords);
|
| }
|
| return ptd.get_errno().value_or(0);
|
| }
|
|
|
|
|
| retsize++;
|
|
|
| if (pwcs != nullptr)
|
| {
|
|
|
| if (retsize > sizeInWords)
|
| {
|
| if (n != _TRUNCATE)
|
| {
|
| _RESET_STRING(pwcs, sizeInWords);
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, retsize <= sizeInWords, ERANGE);
|
| }
|
| retsize = sizeInWords;
|
| retvalue = STRUNCATE;
|
| }
|
|
|
|
|
| pwcs[retsize - 1] = '\0';
|
| }
|
|
|
| if (pConvertedChars != nullptr)
|
| {
|
| *pConvertedChars = retsize;
|
| }
|
|
|
| return retvalue;
|
| }
|
|
|
| extern "C" errno_t __cdecl _mbstowcs_s_l(
|
| size_t * pConvertedChars,
|
| wchar_t * pwcs,
|
| size_t sizeInWords,
|
| const char * s,
|
| size_t n,
|
| _locale_t plocinfo
|
| )
|
| {
|
| __crt_cached_ptd_host ptd(plocinfo);
|
| return _mbstowcs_internal(pConvertedChars, pwcs, sizeInWords, s, n, ptd);
|
| }
|
|
|
| extern "C" errno_t __cdecl mbstowcs_s(
|
| size_t *pConvertedChars,
|
| wchar_t *pwcs,
|
| size_t sizeInWords,
|
| const char *s,
|
| size_t n
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _mbstowcs_internal(pConvertedChars, pwcs, sizeInWords, s, n, ptd);
|
| }
|
|
|