Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Core/GameEngine/Include/Common/AsciiString.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ class AsciiString
Bool isNotEmpty() const { return !isEmpty(); }
Bool isNotNone() const { return !isNone(); }

/**
return true iff the string is valid UTF-8.
*/
Bool isValidUtf8() const;

/**
convert the given UnicodeString to UTF-8 and store it in self.
*/
void convertToUtf8(const UnicodeString& unicodeStr);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should merge with the established translate functions? Or would this cause trouble for some callers?

See:

void AsciiString::translate(const UnicodeString& stringSrc)
{
	validate();
	/// @todo srj put in a real translation here; this will only work for 7-bit ascii
	clear();
	Int len = stringSrc.getLength();
	for (Int i = 0; i < len; i++)
		concat((char)stringSrc.getCharAt(i));
	validate();
}

void UnicodeString::translate(const AsciiString& stringSrc)
{
	validate();
	/// @todo srj put in a real translation here; this will only work for 7-bit ascii
	clear();
	Int len = stringSrc.getLength();
	for (Int i = 0; i < len; i++)
		concat((WideChar)stringSrc.getCharAt(i));
	validate();
}


//
// You might think it would be a good idea to overload the * operator
// to allow for an implicit conversion to an char*. This is
Expand Down
5 changes: 5 additions & 0 deletions Core/GameEngine/Include/Common/UnicodeString.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ class UnicodeString
*/
Bool nextToken(UnicodeString* token, UnicodeString delimiters = UnicodeString::TheEmptyString);

/**
convert the given UTF-8 encoded AsciiString to WideChar and store it in self.
*/
void convertFromUtf8(const AsciiString& asciiStr);

//
// You might think it would be a good idea to overload the * operator
// to allow for an implicit conversion to an WideChar*. This is
Expand Down
22 changes: 22 additions & 0 deletions Core/GameEngine/Source/Common/System/AsciiString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine

#include "Common/CriticalSection.h"
#include "utf8.h"


// -----------------------------------------------------
Expand Down Expand Up @@ -534,3 +535,24 @@ Bool AsciiString::nextToken(AsciiString* tok, const char* seps)
return false;
}
}

//-----------------------------------------------------------------------------
Bool AsciiString::isValidUtf8() const
{
return utf8_validate_string(str());
}

//-----------------------------------------------------------------------------
void AsciiString::convertToUtf8(const UnicodeString& unicodeStr)
{
const size_t requiredBytes = get_size_as_utf8(unicodeStr.str());
if (requiredBytes == 0)
{
DEBUG_CRASH(("AsciiString::convertToUtf8: failed to get size as UTF-8"));
return;
}

ensureUniqueBufferOfSize(static_cast<int>(requiredBytes), false, NULL, NULL);
convert_widechar_to_utf8(unicodeStr.str(), peek(), requiredBytes);
validate();
}
16 changes: 16 additions & 0 deletions Core/GameEngine/Source/Common/System/UnicodeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine

#include "Common/CriticalSection.h"
#include "utf8.h"


// -----------------------------------------------------
Expand Down Expand Up @@ -465,3 +466,18 @@ Bool UnicodeString::nextToken(UnicodeString* tok, UnicodeString delimiters)
return false;
}
}

//-----------------------------------------------------------------------------
void UnicodeString::convertFromUtf8(const AsciiString& asciiStr)
{
const size_t requiredBytes = get_size_as_widechar(asciiStr.str());
if (requiredBytes == 0)
{
DEBUG_CRASH(("UnicodeString::convertFromUtf8: failed to get size as WideChar"));
return;
}

ensureUniqueBufferOfSize(static_cast<int>(requiredBytes / sizeof(WideChar)), false, NULL, NULL);
convert_utf8_to_widechar(asciiStr.str(), peek(), requiredBytes);
validate();
}
9 changes: 8 additions & 1 deletion Core/GameEngine/Source/GameNetwork/GameInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "GameNetwork/LANAPI.h" // for testing packet size
#include "GameNetwork/LANAPICallbacks.h" // for testing packet size
#include "strtok_r.h"
#include "utf8.h"



Expand Down Expand Up @@ -1032,6 +1033,11 @@ Bool ParseAsciiStringToGameInfo(GameInfo *game, AsciiString options)
//DEBUG_LOG(("Saw options of %s", options.str()));
DEBUG_LOG(("ParseAsciiStringToGameInfo - parsing [%s]", options.str()));

if (!options.isValidUtf8())
{
DEBUG_LOG(("ParseAsciiStringToGameInfo - options string is not valid UTF-8"));
return false;
}

while ( (keyValPair = strtok_r(bufPtr, ";", &strPos)) != NULL )
{
Expand Down Expand Up @@ -1167,8 +1173,9 @@ Bool ParseAsciiStringToGameInfo(GameInfo *game, AsciiString options)
DEBUG_LOG(("ParseAsciiStringToGameInfo - slotValue name is empty, quitting"));
break;
}

UnicodeString name;
name.set(MultiByteToWideCharSingleLine(slotValue.str() +1).c_str());
name.convertFromUtf8(slotValue.str() + 1);

//DEBUG_LOG(("ParseAsciiStringToGameInfo - name is %s", slotValue.str()+1));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
// Author: Matthew D. Campbell, July 2002

#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#include "utf8.h"

//-------------------------------------------------------------------------

std::wstring MultiByteToWideCharSingleLine( const char *orig )
{
Int len = strlen(orig);
WideChar *dest = NEW WideChar[len+1];
const size_t size = get_size_as_widechar( orig );
WideChar *dest = NEW WideChar[size / sizeof(WideChar)];

MultiByteToWideChar(CP_UTF8, 0, orig, -1, dest, len);
convert_utf8_to_widechar( orig, dest, size );
WideChar *c = NULL;
do
{
Expand All @@ -56,7 +57,6 @@ std::wstring MultiByteToWideCharSingleLine( const char *orig )
}
while ( c != NULL );

dest[len] = 0;
std::wstring ret = dest;
delete[] dest;
return ret;
Expand All @@ -65,12 +65,11 @@ std::wstring MultiByteToWideCharSingleLine( const char *orig )
std::string WideCharStringToMultiByte( const WideChar *orig )
{
std::string ret;
Int len = WideCharToMultiByte( CP_UTF8, 0, orig, wcslen(orig), NULL, 0, NULL, NULL ) + 1;
if (len > 0)
const size_t size = get_size_as_utf8( orig );
if (size > 0)
{
char *dest = NEW char[len];
WideCharToMultiByte( CP_UTF8, 0, orig, -1, dest, len, NULL, NULL );
dest[len-1] = 0;
char *dest = NEW char[size];
convert_widechar_to_utf8( orig, dest, size );
ret = dest;
delete[] dest;
}
Expand Down
2 changes: 2 additions & 0 deletions Core/Libraries/Source/WWVegas/WWLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ set(WWLIB_SRC
trim.cpp
trim.h
uarray.h
utf8.h
utf8.cpp
vector.cpp
Vector.h
visualc.h
Expand Down
Loading
Loading