From a0c29e7dd67abb15c74c85f07741784877edfdcd Mon Sep 17 00:00:00 2001 From: Joe Ludwig Date: Mon, 2 Sep 2013 11:39:10 -0700 Subject: General: * Fixed a variety of server browser issues with mods based on this SDK * Fixed many warnings on various platforms * Added source code for fgdlib and raytrace * Updated many source files with the latest shared source from TF2. OSX: * Added support for Xcode 4.6 * Switched OSX builds to use Xcode instead of makefiles * Moved libs from src/lib/osx32 to src/lib/public/osx32 or src/lib/common/osx32 to match windows better. Linux: * Moved libs from src/lib/linux32 to src/lib/public/linux32 or src/lib/common/linux32 to match windows better. --- sp/src/tier1/strtools.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'sp/src/tier1/strtools.cpp') diff --git a/sp/src/tier1/strtools.cpp b/sp/src/tier1/strtools.cpp index 6e0ba9f5..5e7fd002 100644 --- a/sp/src/tier1/strtools.cpp +++ b/sp/src/tier1/strtools.cpp @@ -1491,14 +1491,34 @@ int V_UCS2ToUTF8( const ucs2 *pUCS2, char *pUTF8, int cubDestSizeInBytes ) #elif defined(POSIX) iconv_t conv_t = iconv_open( "UTF-8", "UCS-2LE" ); size_t cchResult = -1; - size_t nLenUnicde = cubDestSizeInBytes; - size_t nMaxUTF8 = cubDestSizeInBytes; + + // pUCS2 will be null-terminated so use that to work out the input + // buffer size. Note that we shouldn't assume iconv will stop when it + // finds a zero, and nLenUnicde should be given in bytes, so we multiply + // it by sizeof( ucs2 ) at the end. + size_t nLenUnicde = 0; + while ( pUCS2[nLenUnicde] ) + { + ++nLenUnicde; + } + nLenUnicde *= sizeof( ucs2 ); + + // Calculate number of bytes we want iconv to write, leaving space + // for the null-terminator + size_t nMaxUTF8 = cubDestSizeInBytes - 1; char *pIn = (char *)pUCS2; char *pOut = (char *)pUTF8; if ( conv_t > 0 ) { cchResult = 0; + const size_t nBytesToWrite = nMaxUTF8; cchResult = iconv( conv_t, &pIn, &nLenUnicde, &pOut, &nMaxUTF8 ); + + // Calculate how many bytes were actually written and use that to + // null-terminate our output string. + const size_t nBytesWritten = nBytesToWrite - nMaxUTF8; + pUTF8[nBytesWritten] = 0; + iconv_close( conv_t ); if ( (int)cchResult < 0 ) cchResult = 0; -- cgit v1.2.3