| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <corecrt_internal_mbstring.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
| #include <locale.h>
|
| #include <stdlib.h>
|
| #include <wchar.h>
|
|
|
| using namespace __crt_mbstring;
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| extern "C" int __cdecl _mbtowc_internal(
|
| wchar_t * pwc,
|
| const char * s,
|
| size_t n,
|
| __crt_cached_ptd_host& ptd
|
| )
|
| {
|
| static mbstate_t internal_state{};
|
| if (!s || n == 0)
|
| {
|
| |
|
|
| internal_state = {};
|
| return 0;
|
| }
|
|
|
| if (!*s)
|
| {
|
|
|
| if (pwc)
|
| {
|
| *pwc = 0;
|
| }
|
| return 0;
|
| }
|
|
|
| const _locale_t locale = ptd.get_locale();
|
|
|
| if (locale->locinfo->_public._locale_lc_codepage == CP_UTF8)
|
| {
|
| int result = static_cast<int>(__mbrtowc_utf8(pwc, s, n, &internal_state, ptd));
|
| if (result < 0)
|
| result = -1;
|
| return result;
|
| }
|
|
|
| _ASSERTE(locale->locinfo->_public._locale_mb_cur_max == 1 ||
|
| locale->locinfo->_public._locale_mb_cur_max == 2);
|
|
|
| if (locale->locinfo->locale_name[LC_CTYPE] == nullptr)
|
| {
|
| if (pwc)
|
| {
|
| *pwc = (wchar_t) (unsigned char) *s;
|
| }
|
| return sizeof(char);
|
| }
|
|
|
| if (_isleadbyte_fast_internal((unsigned char) *s, locale))
|
| {
|
| _ASSERTE(locale->locinfo->_public._locale_lc_codepage != CP_UTF8 && L"UTF-8 isn't supported in this _mbtowc_l function yet!!!");
|
|
|
|
|
|
|
| _ASSERTE(locale->locinfo->_public._locale_mb_cur_max > 1);
|
|
|
| if ((locale->locinfo->_public._locale_mb_cur_max <= 1) || ((int) n < locale->locinfo->_public._locale_mb_cur_max) ||
|
| (__acrt_MultiByteToWideChar(locale->locinfo->_public._locale_lc_codepage,
|
| MB_PRECOMPOSED | MB_ERR_INVALID_CHARS,
|
| s,
|
| locale->locinfo->_public._locale_mb_cur_max,
|
| pwc,
|
| (pwc) ? 1 : 0) == 0))
|
| {
|
|
|
| if ((n < (size_t) locale->locinfo->_public._locale_mb_cur_max) || (!*(s + 1)))
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| return -1;
|
| }
|
| }
|
| return locale->locinfo->_public._locale_mb_cur_max;
|
| }
|
| else {
|
|
|
| if (__acrt_MultiByteToWideChar(locale->locinfo->_public._locale_lc_codepage,
|
| MB_PRECOMPOSED | MB_ERR_INVALID_CHARS,
|
| s,
|
| 1,
|
| pwc,
|
| (pwc) ? 1 : 0) == 0)
|
| {
|
| ptd.get_errno().set(EILSEQ);
|
| return -1;
|
| }
|
| return sizeof(char);
|
| }
|
| }
|
|
|
| extern "C" int __cdecl _mbtowc_l(
|
| wchar_t *pwc,
|
| const char *s,
|
| size_t n,
|
| _locale_t plocinfo
|
| )
|
| {
|
| __crt_cached_ptd_host ptd(plocinfo);
|
| return _mbtowc_internal(pwc, s, n, ptd);
|
| }
|
|
|
| extern "C" int __cdecl mbtowc(
|
| wchar_t *pwc,
|
| const char *s,
|
| size_t n
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _mbtowc_internal(pwc, s, n, ptd);
|
| }
|
|
|