about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/base/system.c18
-rw-r--r--src/base/system.h16
-rw-r--r--src/engine/shared/console.cpp23
3 files changed, 56 insertions, 1 deletions
diff --git a/src/base/system.c b/src/base/system.c
index bf473650..25e89896 100644
--- a/src/base/system.c
+++ b/src/base/system.c
@@ -1454,6 +1454,24 @@ int str_utf8_decode(const char **ptr)
 	
 }
 
+int str_utf8_check(const char *str)
+{
+	while(*str)
+	{
+		if((*str&0x80) == 0x0)
+			str++;	
+		else if((*str&0xE0) == 0xC0 && (*(str+1)&0xC0) == 0x80)
+			str += 2;
+		else if((*str&0xF0) == 0xE0 && (*(str+1)&0xC0) == 0x80 && (*(str+2)&0xC0) == 0x80)
+			str += 3;
+		else if((*str&0xF8) == 0xF0 && (*(str+1)&0xC0) == 0x80 && (*(str+2)&0xC0) == 0x80 && (*(str+3)&0xC0) == 0x80)
+			str += 4;
+		else
+			return 0;
+	}
+	return 1;
+}
+
 
 unsigned str_quickhash(const char *str)
 {
diff --git a/src/base/system.h b/src/base/system.h
index f71a03ec..62fe02e9 100644
--- a/src/base/system.h
+++ b/src/base/system.h
@@ -1147,6 +1147,22 @@ int str_utf8_decode(const char **ptr);
 */
 int str_utf8_encode(char *ptr, int chr);
 
+/*
+	Function: str_utf8_check
+		Checks if a strings contains just valid utf8 characters.
+	
+	Parameters:
+		str - Pointer to a possible utf8 string.
+		
+	Returns:
+		0 - invalid characters found.
+		1 - only valid characters found.
+
+	Remarks:
+		- The string is treated as zero-terminated utf8 string.
+*/
+int str_utf8_check(const char *str);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/engine/shared/console.cpp b/src/engine/shared/console.cpp
index dc2e1a25..808b64ed 100644
--- a/src/engine/shared/console.cpp
+++ b/src/engine/shared/console.cpp
@@ -383,7 +383,28 @@ static void StrVariableCommand(IConsole::IResult *pResult, void *pUserData)
 	CStrVariableData *pData = (CStrVariableData *)pUserData;
 
 	if(pResult->NumArguments())
-		str_copy(pData->m_pStr, pResult->GetString(0), pData->m_MaxSize);
+	{
+		const char *pString = pResult->GetString(0);
+		if(!str_utf8_check(pString))
+		{
+			char Temp[4];
+			int Length = 0;
+			while(*pString)
+			{
+				int Size = str_utf8_encode(Temp, static_cast<const unsigned char>(*pString++));
+				if(Length+Size < pData->m_MaxSize)
+				{
+					mem_copy(pData->m_pStr+Length, &Temp, Size);
+					Length += Size;
+				}
+				else
+					break;
+			}
+			pData->m_pStr[Length] = 0;
+		}
+		else
+			str_copy(pData->m_pStr, pString, pData->m_MaxSize);
+	}
 	else
 	{
 		char aBuf[1024];