reactos/sdk/lib/crt/stdlib/mbtowc.c
Bișoc George 8266f62ff6
[CRT][STDLIB] Wine-sync _mbtowc_l function code
The current _mbtowc_l code directly passes the string count size argument to cbMultiByte argument parameter to MultiByteToWideChar(). As the size of lpMultiByteStr expressed in bytes by cbMultiByte is dependent of the actual given string, the size of lpWideCharStr expressed in characters by cchWideChar is always 1.

The resulting parameters to MultiByteToWideChar() will lead to a failure as lpWideCharStr cannot accommodate the converted string if the size pointed by lpMultiByteStr is larger than the buffer wide string to receive the converted output, a Win32 error code of ERROR_INSUFFICIENT_BUFFER in other words. The fact that _mbtowc_l never fails without WINE Gecko package installed is because the codepage pointed by lc_codepage is CP_ACP and the converted output is directly given to the destination wide string parameter thus never calling MultiByteToWideChar(). Installing WINE Gecko merely unhides this problem.

As _mbtowc_l is imported from WINE and that they have updated their code (which at the same time this fixes the aforementioned problem), the following commit syncs the updated code from WINE.

CORE-16885
2020-04-28 22:41:57 +02:00

57 lines
1.4 KiB
C

/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: lib/sdk/crt/stdlib/mbtowc.c
* PURPOSE: Unknown
* PROGRAMER: Unknown
* UPDATE HISTORY:
* 25/11/05: Added license header
*/
#include <precomp.h>
/*********************************************************************
* _mbtowc_l(MSVCRT.@)
*/
int CDECL _mbtowc_l(wchar_t *dst, const char* str, size_t n, _locale_t locale)
{
MSVCRT_pthreadlocinfo locinfo;
wchar_t tmpdst = '\0';
if (!locale)
locinfo = get_locinfo();
else
locinfo = (MSVCRT_pthreadlocinfo)(locale->locinfo);
if (n <= 0 || !str)
return 0;
if (!*str) {
if (dst) *dst = 0;
return 0;
}
if (!locinfo->lc_codepage) {
if (dst) *dst = (unsigned char)*str;
return 1;
}
if (n >= 2 && _isleadbyte_l((unsigned char)*str, locale)) {
if (!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 2, &tmpdst, 1))
return -1;
if (dst) *dst = tmpdst;
return 2;
}
if (!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 1, &tmpdst, 1))
return -1;
if (dst) *dst = tmpdst;
return 1;
}
/*********************************************************************
* mbtowc(MSVCRT.@)
*/
int CDECL mbtowc(wchar_t *dst, const char* str, size_t n)
{
return _mbtowc_l(dst, str, n, NULL);
}