Provide a safer version of convertUTF16toUTF8

This commit is contained in:
Ben Payne 2015-01-20 17:45:31 -05:00
parent a88339c219
commit e3bbc42925
10 changed files with 31 additions and 25 deletions

View file

@ -359,7 +359,7 @@ void StringBuffer::getCopy8(UTF8 *buff, const U32 buffSize) const
{ {
incRequestCount8(); incRequestCount8();
AssertFatal(mBuffer.last() == 0, "StringBuffer::get UTF8 - not a null terminated string!"); AssertFatal(mBuffer.last() == 0, "StringBuffer::get UTF8 - not a null terminated string!");
convertUTF16toUTF8(mBuffer.address(), buff, buffSize); convertUTF16toUTF8N(mBuffer.address(), buff, buffSize);
} }
void StringBuffer::getCopy(UTF16 *buff, const U32 buffSize) const void StringBuffer::getCopy(UTF16 *buff, const U32 buffSize) const
@ -408,7 +408,7 @@ void StringBuffer::updateBuffer8()
{ {
U32 slackLen = getUTF8BufferSizeEstimate(); U32 slackLen = getUTF8BufferSizeEstimate();
mBuffer8.setSize(slackLen); mBuffer8.setSize(slackLen);
U32 len = convertUTF16toUTF8(mBuffer.address(), mBuffer8.address(), slackLen); U32 len = convertUTF16toUTF8N(mBuffer.address(), mBuffer8.address(), slackLen);
mBuffer8.setSize(len+1); mBuffer8.setSize(len+1);
mBuffer8.compact(); mBuffer8.compact();
mDirty8 = false; mDirty8 = false;

View file

@ -191,7 +191,7 @@ U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
U32 convertUTF16toUTF8( const UTF16 *unistring, UTF8 *outbuffer, U32 len) U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len)
{ {
AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator."); AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator.");
PROFILE_START(convertUTF16toUTF8); PROFILE_START(convertUTF16toUTF8);
@ -274,7 +274,7 @@ UTF8* convertUTF16toUTF8( const UTF16* unistring)
FrameTemp<UTF8> buf(len); FrameTemp<UTF8> buf(len);
// perform conversion // perform conversion
nCodeunits = convertUTF16toUTF8( unistring, buf, len); nCodeunits = convertUTF16toUTF8N( unistring, buf, len);
// add 1 for the NULL terminator the converter promises it included. // add 1 for the NULL terminator the converter promises it included.
nCodeunits++; nCodeunits++;

View file

@ -81,7 +81,7 @@ UTF8* convertUTF16toUTF8( const UTF16 *unistring);
/// - If the provided buffer is too small, the output will be truncated. /// - If the provided buffer is too small, the output will be truncated.
U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len); U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len);
U32 convertUTF16toUTF8( const UTF16 *unistring, UTF8 *outbuffer, U32 len); U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len);
template <size_t N> template <size_t N>
inline U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 (&outbuffer)[N]) inline U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 (&outbuffer)[N])
@ -89,6 +89,12 @@ inline U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 (&outbuffer)[N])
return convertUTF8toUTF16N(unistring, outbuffer, (U32) N); return convertUTF8toUTF16N(unistring, outbuffer, (U32) N);
} }
template <size_t N>
inline U32 convertUTF16toUTF8(const UTF16 *unistring, UTF8 (&outbuffer)[N])
{
return convertUTF16toUTF8N(unistring, outbuffer, (U32) N);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/// Functions that converts one unicode codepoint at a time /// Functions that converts one unicode codepoint at a time
/// - Since these functions are designed to be used in tight loops, they do not /// - Since these functions are designed to be used in tight loops, they do not

View file

@ -79,7 +79,7 @@ static LRESULT PASCAL OKBtnFolderHackProc(HWND hWnd, UINT uMsg, WPARAM wParam, L
char *filePath; char *filePath;
#ifdef UNICODE #ifdef UNICODE
char fileBuf[MAX_PATH]; char fileBuf[MAX_PATH];
convertUTF16toUTF8(ofn->lpstrFile, fileBuf, sizeof(fileBuf)); convertUTF16toUTF8(ofn->lpstrFile, fileBuf);
filePath = fileBuf; filePath = fileBuf;
#else #else
filePath = ofn->lpstrFile; filePath = ofn->lpstrFile;
@ -140,7 +140,7 @@ static UINT_PTR CALLBACK FolderHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPA
char filePath[MAX_PATH]; char filePath[MAX_PATH];
#ifdef UNICODE #ifdef UNICODE
convertUTF16toUTF8(buf, filePath, sizeof(filePath)); convertUTF16toUTF8(buf, filePath);
#else #else
dStrcpy( filePath, buf ); dStrcpy( filePath, buf );
#endif #endif
@ -158,7 +158,7 @@ static UINT_PTR CALLBACK FolderHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPA
#ifdef UNICODE #ifdef UNICODE
char buf2[MAX_PATH]; char buf2[MAX_PATH];
convertUTF16toUTF8(buf, buf2, sizeof(buf2)); convertUTF16toUTF8(buf, buf2);
#else #else
char *buf2 = buf; char *buf2 = buf;
#endif #endif
@ -442,7 +442,7 @@ bool FileDialog::Execute()
// Handle Result Properly for Unicode as well as ANSI // Handle Result Properly for Unicode as well as ANSI
#ifdef UNICODE #ifdef UNICODE
if(pszFileTitle[0] || ! ( mData.mStyle & FileDialogData::FDS_OPEN && mData.mStyle & FileDialogData::FDS_MULTIPLEFILES )) if(pszFileTitle[0] || ! ( mData.mStyle & FileDialogData::FDS_OPEN && mData.mStyle & FileDialogData::FDS_MULTIPLEFILES ))
convertUTF16toUTF8( (UTF16*)pszFile, (UTF8*)pszResult, sizeof(pszResult)); convertUTF16toUTF8( (UTF16*)pszFile, pszResult);
else else
convertUTF16toUTF8DoubleNULL( (UTF16*)pszFile, (UTF8*)pszResult, sizeof(pszResult)); convertUTF16toUTF8DoubleNULL( (UTF16*)pszFile, (UTF8*)pszResult, sizeof(pszResult));
#else #else

View file

@ -588,7 +588,7 @@ const char* DInputDevice::getName()
{ {
#ifdef UNICODE #ifdef UNICODE
static UTF8 buf[512]; static UTF8 buf[512];
convertUTF16toUTF8(mDeviceInstance.tszInstanceName, buf, sizeof(buf)); convertUTF16toUTF8(mDeviceInstance.tszInstanceName, buf);
return (const char *)buf; return (const char *)buf;
#else #else
return mDeviceInstance.tszInstanceName; return mDeviceInstance.tszInstanceName;
@ -600,7 +600,7 @@ const char* DInputDevice::getProductName()
{ {
#ifdef UNICODE #ifdef UNICODE
static UTF8 buf[512]; static UTF8 buf[512];
convertUTF16toUTF8(mDeviceInstance.tszProductName, buf, sizeof(buf)); convertUTF16toUTF8(mDeviceInstance.tszProductName, buf);
return (const char *)buf; return (const char *)buf;
#else #else
return mDeviceInstance.tszProductName; return mDeviceInstance.tszProductName;

View file

@ -601,7 +601,7 @@ static bool recurseDumpPath(const char *path, const char *pattern, Vector<Platfo
do do
{ {
#ifdef UNICODE #ifdef UNICODE
convertUTF16toUTF8( findData.cFileName, buf, buf.size ); convertUTF16toUTF8N( findData.cFileName, buf, buf.size );
char* fnbuf = buf; char* fnbuf = buf;
#else #else
char *fnbuf = findData.cFileName; char *fnbuf = findData.cFileName;
@ -1213,10 +1213,10 @@ void Platform::getVolumeInformationList( Vector<VolumeInformation>& out_rVolumeI
#ifdef UNICODE #ifdef UNICODE
char buf[ sizeof( lpszFileSystem ) / sizeof( lpszFileSystem[ 0 ] ) * 3 + 1 ]; char buf[ sizeof( lpszFileSystem ) / sizeof( lpszFileSystem[ 0 ] ) * 3 + 1 ];
convertUTF16toUTF8( lpszFileSystem, buf, sizeof( buf ) / sizeof( buf[ 0 ] ) ); convertUTF16toUTF8( lpszFileSystem, buf );
info.FileSystem = StringTable->insert( buf ); info.FileSystem = StringTable->insert( buf );
convertUTF16toUTF8( lpszVolumeName, buf, sizeof( buf ) / sizeof( buf[ 0 ] ) ); convertUTF16toUTF8( lpszVolumeName );
info.Name = StringTable->insert( buf ); info.Name = StringTable->insert( buf );
#else #else
info.FileSystem = StringTable->insert( lpszFileSystem ); info.FileSystem = StringTable->insert( lpszFileSystem );
@ -1276,7 +1276,7 @@ bool Platform::hasSubDirectory(const char *pPath)
#ifdef UNICODE #ifdef UNICODE
char fileName[ 1024 ]; char fileName[ 1024 ];
convertUTF16toUTF8( findData.cFileName, fileName, sizeof( fileName ) / sizeof( fileName[ 0 ] ) ); convertUTF16toUTF8( findData.cFileName, fileName );
#else #else
char* fileName = findData.cFileName; char* fileName = findData.cFileName;
#endif #endif
@ -1397,7 +1397,7 @@ static bool recurseDumpDirectories(const char *basePath, const char *subPath, Ve
continue; continue;
#ifdef UNICODE #ifdef UNICODE
convertUTF16toUTF8( findData.cFileName, fileName, fileName.size ); convertUTF16toUTF8N( findData.cFileName, fileName, fileName.size );
#else #else
char* fileName = findData.cFileName; char* fileName = findData.cFileName;
#endif #endif
@ -1472,7 +1472,7 @@ StringTableEntry osGetTemporaryDirectory()
#ifdef UNICODE #ifdef UNICODE
TempAlloc< char > dirBuffer( len * 3 + 1 ); TempAlloc< char > dirBuffer( len * 3 + 1 );
char* dir = dirBuffer; char* dir = dirBuffer;
convertUTF16toUTF8( buffer, dir, dirBuffer.size ); convertUTF16toUTF8N( buffer, dir, dirBuffer.size );
#else #else
char* dir = buf; char* dir = buf;
#endif #endif

View file

@ -104,7 +104,7 @@ BOOL CALLBACK EnumFamCallBack(LPLOGFONT logFont, LPNEWTEXTMETRIC textMetric, DWO
const U32 len = dStrlen( logFont->lfFaceName ) * 3 + 1; const U32 len = dStrlen( logFont->lfFaceName ) * 3 + 1;
FrameTemp<UTF8> buffer( len ); FrameTemp<UTF8> buffer( len );
convertUTF16toUTF8( logFont->lfFaceName, buffer, len ); convertUTF16toUTF8N( logFont->lfFaceName, buffer, len );
fonts->push_back( StringTable->insert( buffer ) ); fonts->push_back( StringTable->insert( buffer ) );

View file

@ -52,7 +52,7 @@ const char *Platform::getUserDataDirectory()
#ifdef UNICODE #ifdef UNICODE
char path[ MAX_PATH * 3 + 1 ]; char path[ MAX_PATH * 3 + 1 ];
convertUTF16toUTF8( szBuffer, path, sizeof( path ) ); convertUTF16toUTF8( szBuffer, path );
#else #else
char* path = szBuffer; char* path = szBuffer;
#endif #endif
@ -78,7 +78,7 @@ const char *Platform::getUserHomeDirectory()
#ifdef UNICODE #ifdef UNICODE
char path[ MAX_PATH * 3 + 1 ]; char path[ MAX_PATH * 3 + 1 ];
convertUTF16toUTF8( szBuffer, path, sizeof( path ) ); convertUTF16toUTF8( szBuffer, path );
#else #else
char* path = szBuffer; char* path = szBuffer;
#endif #endif

View file

@ -720,13 +720,13 @@ String Platform::FS::getAssetDir()
{ {
TCHAR buf[ 2048 ]; TCHAR buf[ 2048 ];
::GetModuleFileNameW( NULL, buf, sizeof( buf ) ); ::GetModuleFileNameW( NULL, buf, sizeof( buf ) );
convertUTF16toUTF8( buf, cen_buf, sizeof( cen_buf ) ); convertUTF16toUTF8( buf, cen_buf );
} }
else else
{ {
TCHAR buf[ 2048 ]; TCHAR buf[ 2048 ];
GetCurrentDirectoryW( sizeof( buf ) / sizeof( buf[ 0 ] ), buf ); GetCurrentDirectoryW( sizeof( buf ) / sizeof( buf[ 0 ] ), buf );
convertUTF16toUTF8( buf, cen_buf, sizeof( cen_buf ) ); convertUTF16toUTF8( buf, cen_buf );
return Path::CleanSeparators(cen_buf); return Path::CleanSeparators(cen_buf);
} }
#else #else

View file

@ -362,7 +362,7 @@ S32 WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, S32)
{ {
TCHAR buf[ moduleNameSize ]; TCHAR buf[ moduleNameSize ];
GetModuleFileNameW( NULL, buf, moduleNameSize ); GetModuleFileNameW( NULL, buf, moduleNameSize );
convertUTF16toUTF8( buf, moduleName, moduleNameSize ); convertUTF16toUTF8( buf, moduleName );
} }
#else #else
GetModuleFileNameA(NULL, moduleName, moduleNameSize); GetModuleFileNameA(NULL, moduleName, moduleNameSize);
@ -440,7 +440,7 @@ S32 torque_winmain( HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, S32)
{ {
TCHAR buf[ moduleNameSize ]; TCHAR buf[ moduleNameSize ];
GetModuleFileNameW( NULL, buf, moduleNameSize ); GetModuleFileNameW( NULL, buf, moduleNameSize );
convertUTF16toUTF8( buf, moduleName, moduleNameSize ); convertUTF16toUTF8( buf, moduleName );
} }
#else #else
GetModuleFileNameA(NULL, moduleName, moduleNameSize); GetModuleFileNameA(NULL, moduleName, moduleNameSize);
@ -541,7 +541,7 @@ bool Platform::openWebBrowser( const char* webAddress )
RegCloseKey( regKey ); RegCloseKey( regKey );
sHaveKey = true; sHaveKey = true;
convertUTF16toUTF8(sWebKey,utf8WebKey,512); convertUTF16toUTF8(sWebKey,utf8WebKey);
#ifdef UNICODE #ifdef UNICODE
char *p = dStrstr((const char *)utf8WebKey, "%1"); char *p = dStrstr((const char *)utf8WebKey, "%1");