fix cut points of UTF-8 strings
parent
af7c002d52
commit
b383cdae33
|
@ -1818,17 +1818,26 @@ namespace OpenSim.Framework
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static byte[] StringToBytes256(string str)
|
public static byte[] StringToBytes256(string str)
|
||||||
{
|
{
|
||||||
if (String.IsNullOrEmpty(str)) { return Utils.EmptyBytes; }
|
if (String.IsNullOrEmpty(str))
|
||||||
if (str.Length > 254) str = str.Remove(254);
|
return Utils.EmptyBytes;
|
||||||
if (!str.EndsWith("\0")) { str += "\0"; }
|
|
||||||
|
if (!str.EndsWith("\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
|
||||||
byte[] data = UTF8.GetBytes(str);
|
byte[] data = UTF8.GetBytes(str);
|
||||||
|
|
||||||
if (data.Length > 256)
|
if (data.Length > 256)
|
||||||
{
|
{
|
||||||
Array.Resize<byte>(ref data, 256);
|
int cut = 255;
|
||||||
data[255] = 0;
|
if((data[cut] & 0x80 ) != 0 )
|
||||||
|
{
|
||||||
|
while(cut > 0 && (data[cut] & 0xc0) != 0xc0)
|
||||||
|
cut--;
|
||||||
|
}
|
||||||
|
Array.Resize<byte>(ref data, cut + 1);
|
||||||
|
data[cut] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
@ -1860,17 +1869,26 @@ namespace OpenSim.Framework
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static byte[] StringToBytes1024(string str)
|
public static byte[] StringToBytes1024(string str)
|
||||||
{
|
{
|
||||||
if (String.IsNullOrEmpty(str)) { return Utils.EmptyBytes; }
|
if (String.IsNullOrEmpty(str))
|
||||||
if (str.Length > 1023) str = str.Remove(1023);
|
return Utils.EmptyBytes;
|
||||||
if (!str.EndsWith("\0")) { str += "\0"; }
|
|
||||||
|
if (!str.EndsWith("\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
|
||||||
byte[] data = UTF8.GetBytes(str);
|
byte[] data = UTF8.GetBytes(str);
|
||||||
|
|
||||||
if (data.Length > 1024)
|
if (data.Length > 1024)
|
||||||
{
|
{
|
||||||
Array.Resize<byte>(ref data, 1024);
|
int cut = 1023;
|
||||||
data[1023] = 0;
|
if((data[cut] & 0x80 ) != 0 )
|
||||||
|
{
|
||||||
|
while(cut > 0 && (data[cut] & 0xc0) != 0xc0)
|
||||||
|
cut--;
|
||||||
|
}
|
||||||
|
Array.Resize<byte>(ref data, cut + 1);
|
||||||
|
data[cut] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
|
Loading…
Reference in New Issue