Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/coreclr/debug/daccess/daccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,19 @@ ConvertUtf8(_In_ LPCUTF8 utf8,
ULONG32* nameLen,
_Out_writes_to_opt_(bufLen, *nameLen) PWSTR buffer)
{
size_t cchUtf8 = strlen(utf8) + 1;

if (nameLen)
{
*nameLen = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
if (!*nameLen)
{
return HRESULT_FROM_GetLastError();
}
size_t cchName = minipal_get_length_utf8_to_utf16(utf8, cchUtf8, 0);
*nameLen = (ULONG32)cchName;
}

if (buffer && bufLen)
{
if (!MultiByteToWideChar(CP_UTF8, 0, utf8, -1, buffer, bufLen))
if (!minipal_convert_utf8_to_utf16(utf8, cchUtf8, (CHAR16_T*)buffer, bufLen, 0))
{
return HRESULT_FROM_GetLastError();
return HRESULT_FROM_WIN32(errno);
}
}

Expand All @@ -120,20 +119,23 @@ AllocUtf8(_In_opt_ LPCWSTR wstr,
ULONG32 srcChars,
_Outptr_ LPUTF8* utf8)
{
ULONG32 chars = WideCharToMultiByte(CP_UTF8, 0, wstr, srcChars,
NULL, 0, NULL, NULL);
if (!chars)
if (wstr == NULL || srcChars == 0)
{
return HRESULT_FROM_GetLastError();
return E_INVALIDARG;
}

size_t cchSrc = srcChars == (ULONG32)-1 ? u16_strlen(wstr) + 1 : srcChars;
size_t chars = minipal_get_length_utf16_to_utf8((const CHAR16_T*)wstr, cchSrc, 0);

// Make sure the converted string is always terminated.
if (srcChars != (ULONG32)-1)
{
if (!ClrSafeInt<ULONG32>::addition(chars, 1, chars))
S_SIZE_T charsWithNull = S_SIZE_T(chars) + S_SIZE_T(1);
if (charsWithNull.IsOverflow())
{
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
}
chars = charsWithNull.Value();
}

char* mem = new (nothrow) char[chars];
Expand All @@ -142,10 +144,9 @@ AllocUtf8(_In_opt_ LPCWSTR wstr,
return E_OUTOFMEMORY;
}

if (!WideCharToMultiByte(CP_UTF8, 0, wstr, srcChars,
mem, chars, NULL, NULL))
if (!minipal_convert_utf16_to_utf8((const CHAR16_T*)wstr, cchSrc, mem, chars, 0))
{
HRESULT hr = HRESULT_FROM_GetLastError();
HRESULT hr = HRESULT_FROM_WIN32(errno);
delete [] mem;
return hr;
}
Expand Down
10 changes: 3 additions & 7 deletions src/coreclr/debug/di/rsmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1873,20 +1873,16 @@ HRESULT Cordb::CreateObject(CorDebugInterfaceVersion iDebuggerVersion, DWORD pid
if (lpApplicationGroupId != NULL)
{
// Get length of target string
int cbMultiByte = WideCharToMultiByte(CP_ACP, 0, lpApplicationGroupId, -1, NULL, 0, NULL, NULL);
if (cbMultiByte == 0)
{
return E_FAIL;
}
size_t cchApplicationGroupId = u16_strlen(lpApplicationGroupId) + 1;
size_t cbMultiByte = minipal_get_length_utf16_to_utf8((const CHAR16_T*)lpApplicationGroupId, cchApplicationGroupId, 0);

applicationGroupId = new (nothrow) CHAR[cbMultiByte];
if (applicationGroupId == NULL)
{
return (E_OUTOFMEMORY);
}

/* Convert to ASCII */
cbMultiByte = WideCharToMultiByte(CP_ACP, 0, lpApplicationGroupId, -1, applicationGroupId, cbMultiByte, NULL, NULL);
cbMultiByte = minipal_convert_utf16_to_utf8((const CHAR16_T*)lpApplicationGroupId, cchApplicationGroupId, applicationGroupId, cbMultiByte, 0);
if (cbMultiByte == 0)
{
delete [] applicationGroupId;
Expand Down
8 changes: 5 additions & 3 deletions src/coreclr/debug/ee/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,10 @@ void Debugger::InitDebugEventCounting()
if (wstrValue != NULL)
{
LPSTR strValue;
int cbReq;
cbReq = WideCharToMultiByte(CP_UTF8, 0, wstrValue,-1, 0,0, 0,0);
size_t cbReq;
const CHAR16_T* wszValue = (const CHAR16_T*)(LPWSTR)wstrValue;
size_t cchValue = u16_strlen(wstrValue) + 1;
cbReq = minipal_get_length_utf16_to_utf8(wszValue, cchValue, 0);

strValue = new (nothrow) char[cbReq+1];
// This is a debug only thingy, if it fails, not worth taking
Expand All @@ -931,7 +933,7 @@ void Debugger::InitDebugEventCounting()


// now translate the unicode to ansi string
WideCharToMultiByte(CP_UTF8, 0, wstrValue, -1, strValue, cbReq+1, 0,0);
minipal_convert_utf16_to_utf8(wszValue, cchValue, strValue, cbReq+1, 0);
char *szEnd = (char *)strchr(strValue, ';');
char *szStart = strValue;
while (szEnd != NULL)
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ nativeStringResourceTable_mscorrc
#GetSystemInfo
#LoadLibraryExA
#LoadLibraryExW
#MultiByteToWideChar
#OutputDebugStringW
#OutputDebugStringA
#RaiseException
Expand All @@ -86,6 +85,5 @@ nativeStringResourceTable_mscorrc
#SetThreadDescription
#SwitchToThread
#TerminateProcess
#WideCharToMultiByte

#GUID_NULL
5 changes: 3 additions & 2 deletions src/coreclr/dlls/mscoree/exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ struct HostPropertyArraysTraits final
// Convert 8 bit string to unicode
static LPCWSTR StringToUnicode(LPCSTR str)
{
int length = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
size_t cch = strlen(str) + 1;
size_t length = minipal_get_length_utf8_to_utf16(str, cch, 0);
ASSERTE_ALL_BUILDS(length != 0);

LPWSTR result = new (nothrow) WCHAR[length];
ASSERTE_ALL_BUILDS(result != NULL);

length = MultiByteToWideChar(CP_UTF8, 0, str, -1, result, length);
length = minipal_convert_utf8_to_utf16(str, cch, (CHAR16_T*)result, length, 0);
ASSERTE_ALL_BUILDS(length != 0);

return result;
Expand Down
10 changes: 10 additions & 0 deletions src/coreclr/gc/gccommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "gcenv.h"
#include "gc.h"
#include <minipal/utf8.h>

IGCHeapInternal* g_theGCHeap;
IGCHandleManager* g_theGCHandleManager;
Expand Down Expand Up @@ -131,7 +132,16 @@ FILE* CreateLogFile(const GCConfigStringHolder& temp_logfile_name, bool is_confi
const char* suffix = is_config ? ".config.log" : ".log";
//_snprintf_s(logfile_name, MAX_LONGPATH+1, _TRUNCATE, "%s.%d%s", temp_logfile_name.Get(), pid, suffix);
_snprintf_s(logfile_name, MAX_LONGPATH+1, _TRUNCATE, "%s%s", temp_logfile_name.Get(), suffix);
#ifdef TARGET_WINDOWS
// Config strings are UTF-8; fopen would interpret them as the ANSI code page.
size_t nameLen = strlen(logfile_name) + 1;
size_t wlen = minipal_get_length_utf8_to_utf16(logfile_name, nameLen, 0);
WCHAR* wpath = (WCHAR*)_alloca(wlen * sizeof(WCHAR));
minipal_convert_utf8_to_utf16(logfile_name, nameLen, (CHAR16_T*)wpath, wlen, 0);
logFile = _wfopen(wpath, L"wb");
#else
logFile = fopen(logfile_name, "wb");
#endif

if (logFile == NULL)
{
Expand Down
11 changes: 7 additions & 4 deletions src/coreclr/hosts/corerun/corerun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <functional>
#include <memory>
#include <minipal/utils.h>
#include <minipal/utf8.h>

// Class used to perform specific actions.
class platform_specific_actions;
Expand Down Expand Up @@ -211,25 +212,27 @@ namespace pal

inline string_utf8_t convert_to_utf8(const char_t* str)
{
int bytes_req = ::WideCharToMultiByte(CP_UTF8, 0, str, -1, nullptr, 0, nullptr, nullptr);
size_t length = ::wcslen(str) + 1;
size_t bytes_req = minipal_get_length_utf16_to_utf8((const CHAR16_T*)str, length, 0);

malloc_ptr<char> buffer{ (char*)::malloc(bytes_req) };
assert(buffer != nullptr);

int written = ::WideCharToMultiByte(CP_UTF8, 0, str, -1, buffer.get(), bytes_req, nullptr, nullptr);
size_t written = minipal_convert_utf16_to_utf8((const CHAR16_T*)str, length, buffer.get(), bytes_req, 0);
assert(bytes_req == written);

return { buffer.get() };
}

inline string_t convert_from_utf8(const char* str)
{
int wchar_req = ::MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
size_t length = ::strlen(str) + 1;
size_t wchar_req = minipal_get_length_utf8_to_utf16(str, length, 0);

malloc_ptr<wchar_t> buffer{ (wchar_t*)::malloc(wchar_req * sizeof(wchar_t)) };
assert(buffer != nullptr);

int written = ::MultiByteToWideChar(CP_UTF8, 0, str, -1, buffer.get(), wchar_req);
size_t written = minipal_convert_utf8_to_utf16(str, length, (CHAR16_T*)buffer.get(), wchar_req, 0);
assert(wchar_req == written);

return { buffer.get() };
Expand Down
14 changes: 7 additions & 7 deletions src/coreclr/ilasm/asmman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BinStr* BinStrToUnicode(BinStr* pSource, bool Swap)
if(wz)
{
memset(wz,0,L);
MultiByteToWideChar(g_uCodePage,0,pb,-1,wz,l);
minipal_convert_utf8_to_utf16(pb,strlen(pb)+1,(CHAR16_T*)wz,l,0);
tmp->remove(L-(DWORD)u16_strlen(wz)*sizeof(WCHAR));
#if BIGENDIAN
if (Swap)
Expand Down Expand Up @@ -205,7 +205,7 @@ void AsmMan::EmitFiles()
if(!tmp->m_fNew) continue;
tmp->m_fNew = FALSE;

MultiByteToWideChar(g_uCodePage,0,tmp->szName,-1,wzUniBuf,dwUniBuf);
minipal_convert_utf8_to_utf16(tmp->szName,strlen(tmp->szName)+1,(CHAR16_T*)wzUniBuf,dwUniBuf,0);
if(tmp->pHash==NULL) // if hash not explicitly specified
{
if(m_pAssembly // and assembly is defined
Expand Down Expand Up @@ -553,7 +553,7 @@ void AsmMan::EmitAssemblyRefs()
dwFlags |= afPublicKey;
}
// Convert name to Unicode
MultiByteToWideChar(g_uCodePage,0,m_pCurAsmRef->szName,-1,wzUniBuf,dwUniBuf);
minipal_convert_utf8_to_utf16(m_pCurAsmRef->szName,strlen(m_pCurAsmRef->szName)+1,(CHAR16_T*)wzUniBuf,dwUniBuf,0);
hr = m_pAsmEmitter->DefineAssemblyRef( // S_OK or error.
pbPublicKeyOrToken, // [IN] Public key or token of the assembly.
cbPublicKeyOrToken, // [IN] Count of bytes in the key or token.
Expand Down Expand Up @@ -588,7 +588,7 @@ void AsmMan::EmitAssembly()
FillAssemblyMetadata(m_pAssembly, &md);

// Convert name to Unicode
MultiByteToWideChar(g_uCodePage,0,m_pAssembly->szName,-1,wzUniBuf,dwUniBuf);
minipal_convert_utf8_to_utf16(m_pAssembly->szName,strlen(m_pAssembly->szName)+1,(CHAR16_T*)wzUniBuf,dwUniBuf,0);

hr = m_pAsmEmitter->DefineAssembly( // S_OK or error.
(const void*)(m_sStrongName.m_pbPublicKey), // [IN] Public key of the assembly.
Expand Down Expand Up @@ -857,7 +857,7 @@ HRESULT AsmMan::EmitManifest()
if(!pComType->m_fNew) continue;
pComType->m_fNew = FALSE;

MultiByteToWideChar(g_uCodePage,0,pComType->szName,-1,wzUniBuf,dwUniBuf);
minipal_convert_utf8_to_utf16(pComType->szName,strlen(pComType->szName)+1,(CHAR16_T*)wzUniBuf,dwUniBuf,0);
mdToken tkImplementation = mdTokenNil;
if(pComType->tkImpl) tkImplementation = pComType->tkImpl;
else if(pComType->szFileName)
Expand Down Expand Up @@ -930,7 +930,7 @@ HRESULT AsmMan::EmitManifest()
if(!pManRes->m_fNew) continue;
pManRes->m_fNew = FALSE;

MultiByteToWideChar(g_uCodePage,0,pManRes->szAlias,-1,wzUniBuf,dwUniBuf);
minipal_convert_utf8_to_utf16(pManRes->szAlias,strlen(pManRes->szAlias)+1,(CHAR16_T*)wzUniBuf,dwUniBuf,0);
if(pManRes->szAsmRefName)
{
tkImplementation = GetAsmRefTokByName(pManRes->szAsmRefName);
Expand Down Expand Up @@ -1007,7 +1007,7 @@ HRESULT AsmMan::EmitManifest()
}
if(fOK || ((Assembler*)m_pAssembler)->OnErrGo)
{
MultiByteToWideChar(g_uCodePage,0,pManRes->szName,-1,wzUniBuf,dwUniBuf);
minipal_convert_utf8_to_utf16(pManRes->szName,strlen(pManRes->szName)+1,(CHAR16_T*)wzUniBuf,dwUniBuf,0);
hr = m_pAsmEmitter->DefineManifestResource( // S_OK or error.
(LPCWSTR)wzUniBuf, // [IN] Name of the resource.
tkImplementation, // [IN] mdFile or mdAssemblyRef that provides the resource.
Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/ilasm/asmparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ class MappedFileStream : public ReadStream {

if (IsValid())
{
int len = WideCharToMultiByte(CP_UTF8,0,wFileName,-1,NULL,0,NULL,NULL);
size_t cch = u16_strlen(wFileName)+1;
size_t len = minipal_get_length_utf16_to_utf8((const CHAR16_T*)wFileName,cch,0);
m_fileNameUtf8 = new char[len+1];
WideCharToMultiByte(CP_UTF8,0,wFileName,-1,m_fileNameUtf8,len+1,NULL,NULL);
minipal_convert_utf16_to_utf8((const CHAR16_T*)wFileName,cch,m_fileNameUtf8,len+1,0);
}
}
~MappedFileStream()
Expand Down
23 changes: 11 additions & 12 deletions src/coreclr/ilasm/assem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
void indexKeywords(Indx* indx); // defined in asmparse.y

unsigned int g_uCodePage = CP_ACP;
unsigned int g_uConsoleCP = CP_ACP;

char g_szSourceFileName[MAX_FILENAME_LENGTH*3];

Expand Down Expand Up @@ -605,7 +604,7 @@ void Assembler::EmitImports()
mdToken tk;
for(i=0; (pID = m_ImportList.PEEK(i)); i++)
{
MultiByteToWideChar(g_uCodePage,0,pID->szDllName,-1,wzDllName,dwUniBuf-1);
minipal_convert_utf8_to_utf16(pID->szDllName,strlen(pID->szDllName)+1,(CHAR16_T*)wzDllName,dwUniBuf-1,0);
if(FAILED(m_pEmitter->DefineModuleRef( // S_OK or error.
wzDllName, // [IN] DLL name
&tk))) // [OUT] returned
Expand All @@ -619,7 +618,7 @@ HRESULT Assembler::EmitPinvokeMap(mdToken tk, PInvokeDescriptor* pDescr)
{
WCHAR* wzAlias=&wzUniBuf[0];

if(pDescr->szAlias) MultiByteToWideChar(g_uCodePage,0,pDescr->szAlias,-1,wzAlias,dwUniBuf-1);
if(pDescr->szAlias) minipal_convert_utf8_to_utf16(pDescr->szAlias,strlen(pDescr->szAlias)+1,(CHAR16_T*)wzAlias,dwUniBuf-1,0);

return m_pEmitter->DefinePinvokeMap( // Return code.
tk, // [IN] FieldDef, MethodDef or MethodImpl.
Expand Down Expand Up @@ -657,7 +656,7 @@ BOOL Assembler::EmitMethod(Method *pMethod)
ClassToken = (pMethod->IsGlobalMethod())? mdTokenNil
: pMethod->m_pClass->m_cl;
// Convert name to UNICODE
MultiByteToWideChar(g_uCodePage,0,pszMethodName,-1,wzMemberName,dwUniBuf-1);
minipal_convert_utf8_to_utf16(pszMethodName,strlen(pszMethodName)+1,(CHAR16_T*)wzMemberName,dwUniBuf-1,0);

if(IsMdPrivateScope(pMethod->m_Attr))
{
Expand Down Expand Up @@ -801,7 +800,7 @@ BOOL Assembler::EmitMethod(Method *pMethod)
if(pAN->dwName) strcpy_s(szPhonyName,dwUniBuf >> 1,pAN->szName);
else sprintf_s(szPhonyName,(dwUniBuf >> 1),"A_%d",pAN->nNum);

MultiByteToWideChar(g_uCodePage,0,szPhonyName,-1,wzParName,dwUniBuf >> 1);
minipal_convert_utf8_to_utf16(szPhonyName,strlen(szPhonyName)+1,(CHAR16_T*)wzParName,dwUniBuf >> 1,0);

if(pAN->pValue)
{
Expand Down Expand Up @@ -904,7 +903,7 @@ BOOL Assembler::EmitEvent(EventDescriptor* pED)

if(!pED) return FALSE;

MultiByteToWideChar(g_uCodePage,0,pED->m_szName,-1,wzMemberName,dwUniBuf-1);
minipal_convert_utf8_to_utf16(pED->m_szName,strlen(pED->m_szName)+1,(CHAR16_T*)wzMemberName,dwUniBuf-1,0);

mdAddOn = ResolveLocalMemberRef(pED->m_tkAddOn);
if(TypeFromToken(mdAddOn) != mdtMethodDef)
Expand Down Expand Up @@ -964,7 +963,7 @@ BOOL Assembler::EmitProp(PropDescriptor* pPD)

if(!pPD) return FALSE;

MultiByteToWideChar(g_uCodePage,0,pPD->m_szName,-1,wzMemberName,dwUniBuf-1);
minipal_convert_utf8_to_utf16(pPD->m_szName,strlen(pPD->m_szName)+1,(CHAR16_T*)wzMemberName,dwUniBuf-1,0);

mdSet = ResolveLocalMemberRef(pPD->m_tkSet);
if((RidFromToken(mdSet)!=0)&&(TypeFromToken(mdSet) != mdtMethodDef))
Expand Down Expand Up @@ -1091,7 +1090,7 @@ BOOL Assembler::EmitClass(Class *pClass)
else
szFullName = pClass->m_szFQN;

MultiByteToWideChar(g_uCodePage,0,szFullName,-1,wzFullName,dwUniBuf);
minipal_convert_utf8_to_utf16(szFullName,strlen(szFullName)+1,(CHAR16_T*)wzFullName,dwUniBuf,0);

L = u16_strlen(wzFullName);
if((L==0)||(wzFullName[L-1]==L'.')) // Missing class name!
Expand Down Expand Up @@ -1364,7 +1363,7 @@ char* Assembler::ReflectionNotation(mdToken tk)
mdToken tkResScope;
if(SUCCEEDED(m_pImporter->GetTypeRefProps(tk,&tkResScope,wzUniBuf,cchUniBuf,&N)))
{
int ret = WideCharToMultiByte(CP_UTF8,0,wzUniBuf,-1,sz,szSizeBytes,NULL,NULL);
size_t ret = minipal_convert_utf16_to_utf8((const CHAR16_T*)wzUniBuf,u16_strlen(wzUniBuf)+1,sz,szSizeBytes,0);
if(TypeFromToken(tkResScope)==mdtAssemblyRef)
{
AsmManAssembly *pAsmRef = m_pManifest->m_AsmRefLst.PEEK(RidFromToken(tkResScope)-1);
Expand Down Expand Up @@ -1403,8 +1402,8 @@ char* Assembler::ReflectionNotation(mdToken tk)
{
memcpy(wzUniBuf,pAsmRef->pLocale->ptr(),L);
wzUniBuf[L>>1] = 0;
ret = WideCharToMultiByte(CP_UTF8,0,wzUniBuf,-1,pc,(int)szRemainingSizeBytes,NULL,NULL);
if (ret <= 0)
ret = minipal_convert_utf16_to_utf8((const CHAR16_T*)wzUniBuf,u16_strlen(wzUniBuf)+1,pc,szRemainingSizeBytes,0);
if (ret == 0)
{
report->error("Locale too long.\n");
*sz=0;
Expand All @@ -1413,7 +1412,7 @@ char* Assembler::ReflectionNotation(mdToken tk)
else
{
pc += ret;
szRemainingSizeBytes -= (size_t)ret;
szRemainingSizeBytes -= ret;
}
}
}
Expand Down
Loading
Loading