From 67e9f03f23a454f273aeb6f83e338c057c3efb93 Mon Sep 17 00:00:00 2001 From: oy Date: Fri, 1 Oct 2010 00:55:16 +0200 Subject: [PATCH 1/2] when setting a config string variable check if it's a utf8 string and encode it if the check fails. Closes #10 --- src/base/system.c | 18 ++++++++++++++++++ src/base/system.h | 16 ++++++++++++++++ src/engine/shared/console.cpp | 23 ++++++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/base/system.c b/src/base/system.c index bf4736504..25e898966 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 f71a03ecd..62fe02e98 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 dc2e1a252..808b64edc 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(*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]; From a62a7413d1ff25c8e07657b96a485f001004619a Mon Sep 17 00:00:00 2001 From: oy Date: Fri, 1 Oct 2010 12:43:44 +0200 Subject: [PATCH 2/2] made linereader work with window line endings. Closes #196 --- src/engine/shared/linereader.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/engine/shared/linereader.cpp b/src/engine/shared/linereader.cpp index b3de233ba..78b574be4 100644 --- a/src/engine/shared/linereader.cpp +++ b/src/engine/shared/linereader.cpp @@ -51,6 +51,8 @@ char *CLineReader::Get() if(m_aBuffer[m_BufferPos] == '\n' || m_aBuffer[m_BufferPos] == '\r') { // line found + if(m_aBuffer[m_BufferPos] == '\r' && m_BufferPos+1 < m_BufferSize && m_aBuffer[m_BufferPos+1] == '\n') + m_aBuffer[m_BufferPos++] = 0; m_aBuffer[m_BufferPos] = 0; m_BufferPos++; return &m_aBuffer[LineStart];