do no append zero when clipping strings ( may still happen on other places)

master
UbitUmarov 2020-02-26 16:58:41 +00:00
parent 7d7fc8f06a
commit ea8eeaa307
2 changed files with 35 additions and 13 deletions

View File

@ -2597,7 +2597,7 @@ namespace OpenSim.Framework
return Utils.EmptyBytes; return Utils.EmptyBytes;
if (!str.EndsWith("\0")) if (!str.EndsWith("\0"))
str += "\0"; str += "\0";
// Because this is UTF-8 encoding and not ASCII, it's possible we // Because this is UTF-8 encoding and not ASCII, it's possible we
// might have gotten an oversized array even after the string trim // might have gotten an oversized array even after the string trim
@ -2605,18 +2605,41 @@ namespace OpenSim.Framework
if (data.Length > MaxLength) if (data.Length > MaxLength)
{ {
int cut = MaxLength - 1 ; int cut = MaxLength - 1;
if((data[cut] & 0x80 ) != 0 ) if ((data[cut] & 0x80) != 0)
{ {
while(cut > 0 && (data[cut] & 0xc0) != 0xc0) while (cut > 0 && (data[cut] & 0xc0) != 0xc0)
cut--; cut--;
} }
Array.Resize<byte>(ref data, cut + 1); Array.Resize<byte>(ref data, cut + 1);
data[cut] = 0; data[cut] = 0;
} }
return data; return data;
} }
public static byte[] StringToBytesNoTerm(string str, int MaxLength)
{
if (String.IsNullOrEmpty(str))
return Utils.EmptyBytes;
// Because this is UTF-8 encoding and not ASCII, it's possible we
// might have gotten an oversized array even after the string trim
byte[] data = UTF8.GetBytes(str);
if (data.Length > MaxLength)
{
int cut = MaxLength - 1;
if ((data[cut] & 0x80) != 0)
{
while (cut > 0 && (data[cut] & 0xc0) != 0xc0)
cut--;
}
Array.Resize<byte>(ref data, cut);
}
return data;
}
/// <summary> /// <summary>
/// Pretty format the hashtable contents to a single line. /// Pretty format the hashtable contents to a single line.
/// </summary> /// </summary>

View File

@ -1199,7 +1199,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
if (text.Length > 1023) if (text.Length > 1023)
text = text.Substring(0, 1023); text = text.Substring(0, 1023);
byte[] binText = Util.StringToBytes(text, 1023); byte[] binText = Util.StringToBytesNoTerm(text, 1023);
World.SimChat(binText, World.SimChat(binText,
ChatTypeEnum.Whisper, channelID, m_host.AbsolutePosition, m_host.Name, m_host.UUID, false); ChatTypeEnum.Whisper, channelID, m_host.AbsolutePosition, m_host.Name, m_host.UUID, false);
@ -1237,7 +1237,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
} }
else else
{ {
byte[] binText = Util.StringToBytes(text, 1023); byte[] binText = Util.StringToBytesNoTerm(text, 1023);
World.SimChat(binText, World.SimChat(binText,
ChatTypeEnum.Say, channelID, m_host.AbsolutePosition, m_host.Name, m_host.UUID, false); ChatTypeEnum.Say, channelID, m_host.AbsolutePosition, m_host.Name, m_host.UUID, false);
@ -1258,7 +1258,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
if (m_SayShoutCount >= 11) if (m_SayShoutCount >= 11)
ScriptSleep(2000); ScriptSleep(2000);
byte[] binText = Util.StringToBytes(text, 1023); byte[] binText = Util.StringToBytesNoTerm(text, 1023);
World.SimChat(binText, World.SimChat(binText,
ChatTypeEnum.Shout, channelID, m_host.AbsolutePosition, m_host.Name, m_host.UUID, true); ChatTypeEnum.Shout, channelID, m_host.AbsolutePosition, m_host.Name, m_host.UUID, true);
@ -1276,22 +1276,21 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
return; return;
} }
if (text.Length > 1023) byte[] binText = Util.StringToBytesNoTerm(text, 1023);
text = text.Substring(0, 1023);
m_host.AddScriptLPS(1); m_host.AddScriptLPS(1);
// debug channel is also sent to avatars // debug channel is also sent to avatars
if (channelID == ScriptBaseClass.DEBUG_CHANNEL) if (channelID == ScriptBaseClass.DEBUG_CHANNEL)
{ {
World.SimChat(Utils.StringToBytes(text), World.SimChat(binText,
ChatTypeEnum.Shout, channelID, m_host.ParentGroup.RootPart.AbsolutePosition, m_host.Name, m_host.UUID, true); ChatTypeEnum.Shout, channelID, m_host.ParentGroup.RootPart.AbsolutePosition, m_host.Name, m_host.UUID, true);
} }
IWorldComm wComm = m_ScriptEngine.World.RequestModuleInterface<IWorldComm>(); IWorldComm wComm = m_ScriptEngine.World.RequestModuleInterface<IWorldComm>();
if (wComm != null) if (wComm != null)
wComm.DeliverMessage(ChatTypeEnum.Region, channelID, m_host.Name, m_host.UUID, text); wComm.DeliverMessage(ChatTypeEnum.Region, channelID, m_host.Name, m_host.UUID, Util.UTF8.GetString(binText));
} }
public void llRegionSayTo(string target, int channel, string msg) public void llRegionSayTo(string target, int channel, string msg)