浏览代码

Provide a safer version of convertUTF16toUTF8

Ben Payne 10 年之前
父节点
当前提交
e3bbc42925

+ 2 - 2
Engine/source/core/stringBuffer.cpp

@@ -359,7 +359,7 @@ void StringBuffer::getCopy8(UTF8 *buff, const U32 buffSize) const
 {
    incRequestCount8();
    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
@@ -408,7 +408,7 @@ void StringBuffer::updateBuffer8()
 {
    U32 slackLen = getUTF8BufferSizeEstimate();
    mBuffer8.setSize(slackLen);
-   U32 len = convertUTF16toUTF8(mBuffer.address(), mBuffer8.address(), slackLen);
+   U32 len = convertUTF16toUTF8N(mBuffer.address(), mBuffer8.address(), slackLen);
    mBuffer8.setSize(len+1);
    mBuffer8.compact();
    mDirty8 = false;

+ 2 - 2
Engine/source/core/strings/unicode.cpp

@@ -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.");
    PROFILE_START(convertUTF16toUTF8);
@@ -274,7 +274,7 @@ UTF8*  convertUTF16toUTF8( const UTF16* unistring)
    FrameTemp<UTF8> buf(len);
       
    // perform conversion
-   nCodeunits = convertUTF16toUTF8( unistring, buf, len);
+   nCodeunits = convertUTF16toUTF8N( unistring, buf, len);
    
    // add 1 for the NULL terminator the converter promises it included.
    nCodeunits++;

+ 7 - 1
Engine/source/core/strings/unicode.h

@@ -81,7 +81,7 @@ UTF8*  convertUTF16toUTF8( const UTF16 *unistring);
 /// - If the provided buffer is too small, the output will be truncated.
 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>
 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);
 }
 
+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
 /// - Since these functions are designed to be used in tight loops, they do not

+ 4 - 4
Engine/source/platformWin32/nativeDialogs/fileDialog.cpp

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

+ 2 - 2
Engine/source/platformWin32/winDInputDevice.cpp

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

+ 6 - 6
Engine/source/platformWin32/winFileio.cpp

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

+ 1 - 1
Engine/source/platformWin32/winFont.cpp

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

+ 2 - 2
Engine/source/platformWin32/winUser.cpp

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

+ 2 - 2
Engine/source/platformWin32/winVolume.cpp

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

+ 3 - 3
Engine/source/platformWin32/winWindow.cpp

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