summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Tools/ProtoProxy/Connection.cpp8
-rw-r--r--src/ByteBuffer.cpp34
-rw-r--r--src/ByteBuffer.h4
-rw-r--r--src/Protocol/ProtocolRecognizer.cpp6
-rw-r--r--src/StringUtils.cpp18
-rw-r--r--src/StringUtils.h2
6 files changed, 17 insertions, 55 deletions
diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp
index f24747a24..62d64c49e 100644
--- a/Tools/ProtoProxy/Connection.cpp
+++ b/Tools/ProtoProxy/Connection.cpp
@@ -1835,12 +1835,12 @@ bool cConnection::HandleServerKick(void)
Reason.append(Split[4]);
Reason.push_back(0);
Reason.append(Split[5]);
- AString ReasonBE16 = UTF8ToRawBEUTF16(Reason.data(), Reason.size());
+ auto ReasonBE16 = UTF8ToRawBEUTF16(Reason);
AString PacketStart("\xff");
- PacketStart.push_back(static_cast<char>((ReasonBE16.size() / 2) / 256));
- PacketStart.push_back(static_cast<char>((ReasonBE16.size() / 2) % 256));
+ PacketStart.push_back(static_cast<char>(ReasonBE16.size() / 256));
+ PacketStart.push_back(static_cast<char>(ReasonBE16.size() % 256));
CLIENTSEND(PacketStart.data(), PacketStart.size());
- CLIENTSEND(ReasonBE16.data(), ReasonBE16.size());
+ CLIENTSEND(reinterpret_cast<const char *>(ReasonBE16.data()), ReasonBE16.size() * sizeof(char16_t));
return true;
}
else
diff --git a/src/ByteBuffer.cpp b/src/ByteBuffer.cpp
index 7088b55f3..7999226e8 100644
--- a/src/ByteBuffer.cpp
+++ b/src/ByteBuffer.cpp
@@ -473,22 +473,6 @@ bool cByteBuffer::ReadBool(bool & a_Value)
-bool cByteBuffer::ReadBEUTF16String16(AString & a_Value)
-{
- CHECK_THREAD
- CheckValid();
- UInt16 Length;
- if (!ReadBEUInt16(Length))
- {
- return false;
- }
- return ReadUTF16String(a_Value, Length);
-}
-
-
-
-
-
bool cByteBuffer::ReadVarInt32(UInt32 & a_Value)
{
CHECK_THREAD
@@ -929,24 +913,6 @@ bool cByteBuffer::ReadString(AString & a_String, size_t a_Count)
-bool cByteBuffer::ReadUTF16String(AString & a_String, size_t a_NumChars)
-{
- // Reads 2 * a_NumChars bytes and interprets it as a UTF16 string, converting it into UTF8 string a_String
- CHECK_THREAD
- CheckValid();
- AString RawData;
- if (!ReadString(RawData, a_NumChars * 2))
- {
- return false;
- }
- RawBEToUTF8(RawData.data(), a_NumChars, a_String);
- return true;
-}
-
-
-
-
-
bool cByteBuffer::SkipRead(size_t a_Count)
{
CHECK_THREAD
diff --git a/src/ByteBuffer.h b/src/ByteBuffer.h
index 0a4935327..cec85a404 100644
--- a/src/ByteBuffer.h
+++ b/src/ByteBuffer.h
@@ -63,7 +63,6 @@ public:
bool ReadBEFloat (float & a_Value);
bool ReadBEDouble (double & a_Value);
bool ReadBool (bool & a_Value);
- bool ReadBEUTF16String16(AString & a_Value); // string length as BE short, then string as UTF-16BE
bool ReadVarInt32 (UInt32 & a_Value);
bool ReadVarInt64 (UInt64 & a_Value);
bool ReadVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8
@@ -109,9 +108,6 @@ public:
/** Reads a_Count bytes into a_String; returns true if successful */
bool ReadString(AString & a_String, size_t a_Count);
- /** Reads 2 * a_NumChars bytes and interprets it as a UTF16-BE string, converting it into UTF8 string a_String */
- bool ReadUTF16String(AString & a_String, size_t a_NumChars);
-
/** Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer */
bool SkipRead(size_t a_Count);
diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp
index 0d16262f9..845e7b56d 100644
--- a/src/Protocol/ProtocolRecognizer.cpp
+++ b/src/Protocol/ProtocolRecognizer.cpp
@@ -200,10 +200,10 @@ void cProtocolRecognizer::SendDisconnect(const AString & a_Reason)
static const int Packet = 0xff; // PACKET_DISCONNECT
SendData(reinterpret_cast<const char *>(&Packet), 1); // WriteByte()
- AString UTF16 = UTF8ToRawBEUTF16(a_Reason.c_str(), a_Reason.length());
- static const u_short Size = htons(static_cast<u_short>(UTF16.size() / 2));
+ auto UTF16 = UTF8ToRawBEUTF16(a_Reason);
+ static const u_short Size = htons(static_cast<u_short>(UTF16.size()));
SendData(reinterpret_cast<const char *>(&Size), 2); // WriteShort()
- SendData(UTF16.data(), UTF16.size()); // WriteString()
+ SendData(reinterpret_cast<const char *>(UTF16.data()), UTF16.size() * sizeof(char16_t)); // WriteString()
}
}
diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp
index 337613674..cc71e048b 100644
--- a/src/StringUtils.cpp
+++ b/src/StringUtils.cpp
@@ -500,13 +500,13 @@ static bool isLegalUTF8(const unsigned char * source, int length)
-AString UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length)
+std::u16string UTF8ToRawBEUTF16(const AString & a_UTF8)
{
- AString UTF16;
- UTF16.reserve(a_UTF8Length * 3);
+ std::u16string UTF16;
+ UTF16.reserve(a_UTF8.size() * 2);
- const unsigned char * source = reinterpret_cast<const unsigned char *>(a_UTF8);
- const unsigned char * sourceEnd = source + a_UTF8Length;
+ const unsigned char * source = reinterpret_cast<const unsigned char *>(a_UTF8.data());
+ const unsigned char * sourceEnd = source + a_UTF8.size();
const int halfShift = 10; // used for shifting by 10 bits
const unsigned int halfBase = 0x0010000UL;
const unsigned int halfMask = 0x3ffUL;
@@ -546,13 +546,13 @@ AString UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length)
ch = ' ';
}
unsigned short v = htons(static_cast<unsigned short>(ch));
- UTF16.append(reinterpret_cast<const char *>(&v), 2);
+ UTF16.push_back(static_cast<char16_t>(v));
}
else if (ch > UNI_MAX_UTF16)
{
// Invalid value, replace with a space
unsigned short v = htons(' ');
- UTF16.append(reinterpret_cast<const char *>(&v), 2);
+ UTF16.push_back(static_cast<char16_t>(v));
}
else
{
@@ -560,8 +560,8 @@ AString UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length)
ch -= halfBase;
unsigned short v1 = htons((ch >> halfShift) + UNI_SUR_HIGH_START);
unsigned short v2 = htons((ch & halfMask) + UNI_SUR_LOW_START);
- UTF16.append(reinterpret_cast<const char *>(&v1), 2);
- UTF16.append(reinterpret_cast<const char *>(&v2), 2);
+ UTF16.push_back(static_cast<char16_t>(v1));
+ UTF16.push_back(static_cast<char16_t>(v2));
}
}
return UTF16;
diff --git a/src/StringUtils.h b/src/StringUtils.h
index f2a9e372b..00504d358 100644
--- a/src/StringUtils.h
+++ b/src/StringUtils.h
@@ -82,7 +82,7 @@ extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AS
extern AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8);
/** Converts a UTF-8 string into a UTF-16 BE string. */
-extern AString UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length);
+extern std::u16string UTF8ToRawBEUTF16(const AString & a_String);
/** Creates a nicely formatted HEX dump of the given memory block.
Max a_BytesPerLine is 120. */