|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal.h>
|
| #include <corecrt_internal_mbstring.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
| #include <errno.h>
|
| #include <limits.h>
|
| #include <locale.h>
|
| #include <stdlib.h>
|
|
|
|
|
| extern "C" int __cdecl _wctomb_internal(
|
| int* const return_value,
|
| char* const destination,
|
| size_t const destination_count,
|
| wchar_t const wchar,
|
| __crt_cached_ptd_host& ptd
|
| )
|
| {
|
|
|
| if (!destination && destination_count > 0)
|
| {
|
|
|
| if (return_value != nullptr)
|
| *return_value = 0;
|
|
|
| return 0;
|
| }
|
|
|
| if (return_value)
|
| *return_value = -1;
|
|
|
|
|
|
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, destination_count <= INT_MAX, EINVAL);
|
| _locale_t const locale = ptd.get_locale();
|
|
|
| if (locale->locinfo->_public._locale_lc_codepage == CP_UTF8)
|
| {
|
|
|
|
|
|
|
| mbstate_t state{};
|
| int result = static_cast<int>(__crt_mbstring::__c32rtomb_utf8(destination, static_cast<char32_t>(wchar), &state, ptd));
|
| if (return_value != nullptr)
|
| {
|
| *return_value = result;
|
| }
|
| if (result <= 4)
|
| {
|
| return 0;
|
| }
|
| else
|
| {
|
| return ptd.get_errno().value_or(0);
|
| }
|
| }
|
|
|
|
|
|
|
| if (!locale->locinfo->locale_name[LC_CTYPE])
|
| {
|
|
|
| if (wchar > 255)
|
| {
|
|
|
| if (destination != nullptr && destination_count > 0)
|
| {
|
| memset(destination, 0, destination_count);
|
| }
|
|
|
| return ptd.get_errno().set(EILSEQ);
|
| }
|
|
|
|
|
| if (destination != nullptr)
|
| {
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, destination_count > 0, ERANGE);
|
| *destination = static_cast<char>(wchar);
|
| }
|
|
|
| if (return_value != nullptr)
|
| {
|
|
|
| *return_value = 1;
|
| }
|
|
|
|
|
| return 0;
|
| }
|
| else
|
| {
|
| BOOL default_used{};
|
| int const size = __acrt_WideCharToMultiByte(
|
| locale->locinfo->_public._locale_lc_codepage,
|
| 0,
|
| &wchar,
|
| 1,
|
| destination,
|
| static_cast<int>(destination_count),
|
| nullptr,
|
| &default_used);
|
|
|
| if (size == 0 || default_used)
|
| {
|
| if (size == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
|
| {
|
| if (destination && destination_count > 0)
|
| {
|
| memset(destination, 0, destination_count);
|
| }
|
|
|
| _UCRT_VALIDATE_RETURN_ERRCODE(ptd, ("Buffer too small", 0), ERANGE);
|
| }
|
|
|
| return ptd.get_errno().set(EILSEQ);
|
| }
|
|
|
| if (return_value)
|
| {
|
| *return_value = size;
|
| }
|
|
|
| return 0;
|
| }
|
|
|
|
|
| }
|
|
|
| extern "C" int __cdecl _wctomb_s_l(
|
| int* const return_value,
|
| char* const destination,
|
| size_t const destination_count,
|
| wchar_t const wchar,
|
| _locale_t const locale
|
| )
|
| {
|
| __crt_cached_ptd_host ptd(locale);
|
| return _wctomb_internal(return_value, destination, destination_count, wchar, ptd);
|
| }
|
|
|
| extern "C" errno_t __cdecl wctomb_s (
|
| int* const return_value,
|
| char* const destination,
|
| size_t const destination_count,
|
| wchar_t const wchar
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _wctomb_internal(return_value, destination, destination_count, wchar, ptd);
|
| }
|
|
|
| extern "C" int __cdecl _wctomb_l(
|
| char* const destination,
|
| wchar_t const wchar,
|
| _locale_t const locale
|
| )
|
| {
|
| __crt_cached_ptd_host ptd(locale);
|
|
|
| int return_value{};
|
| errno_t const e = _wctomb_internal(
|
| &return_value,
|
| destination,
|
| ptd.get_locale()->locinfo->_public._locale_mb_cur_max,
|
| wchar,
|
| ptd);
|
|
|
| if (e != 0)
|
| {
|
| return -1;
|
| }
|
|
|
| return return_value;
|
| }
|
|
|
|
|
|
|
| __pragma(warning(push))
|
| __pragma(warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_HIGH_PRIORITY))
|
|
|
| extern "C" int __cdecl wctomb(
|
| char* const destination,
|
| wchar_t const wchar
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
|
|
| int return_value{};
|
| errno_t const e = _wctomb_internal(&return_value, destination, MB_CUR_MAX, wchar, ptd);
|
| if (e != 0)
|
| {
|
| return -1;
|
| }
|
|
|
| return return_value;
|
| }
|
|
|
| __pragma(warning(pop))
|
|
|