aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorUbitUmarov2015-10-28 21:27:56 +0000
committerUbitUmarov2015-10-28 21:27:56 +0000
commit45ff7cec805755088dee3dc17f158e89f79bfeb5 (patch)
tree70d45c90dec97d68a16414c9bd51487ee1ba39ca
parentdont tell clients that we can edit a friend's attachments when we do forbid it (diff)
downloadopensim-SC_OLD-45ff7cec805755088dee3dc17f158e89f79bfeb5.zip
opensim-SC_OLD-45ff7cec805755088dee3dc17f158e89f79bfeb5.tar.gz
opensim-SC_OLD-45ff7cec805755088dee3dc17f158e89f79bfeb5.tar.bz2
opensim-SC_OLD-45ff7cec805755088dee3dc17f158e89f79bfeb5.tar.xz
fix cut points of UTF-8 strings
-rw-r--r--OpenSim/Framework/Util.cs38
1 files changed, 28 insertions, 10 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
index d1cdaa6..3e6d8ef 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -1945,17 +1945,26 @@ namespace OpenSim.Framework
1945 /// <returns></returns> 1945 /// <returns></returns>
1946 public static byte[] StringToBytes256(string str) 1946 public static byte[] StringToBytes256(string str)
1947 { 1947 {
1948 if (String.IsNullOrEmpty(str)) { return Utils.EmptyBytes; } 1948 if (String.IsNullOrEmpty(str))
1949 if (str.Length > 254) str = str.Remove(254); 1949 return Utils.EmptyBytes;
1950 if (!str.EndsWith("\0")) { str += "\0"; } 1950
1951 if (!str.EndsWith("\0"))
1952 str += "\0";
1951 1953
1952 // Because this is UTF-8 encoding and not ASCII, it's possible we 1954 // Because this is UTF-8 encoding and not ASCII, it's possible we
1953 // might have gotten an oversized array even after the string trim 1955 // might have gotten an oversized array even after the string trim
1954 byte[] data = UTF8.GetBytes(str); 1956 byte[] data = UTF8.GetBytes(str);
1957
1955 if (data.Length > 256) 1958 if (data.Length > 256)
1956 { 1959 {
1957 Array.Resize<byte>(ref data, 256); 1960 int cut = 255;
1958 data[255] = 0; 1961 if((data[cut] & 0x80 ) != 0 )
1962 {
1963 while(cut > 0 && (data[cut] & 0xc0) != 0xc0)
1964 cut--;
1965 }
1966 Array.Resize<byte>(ref data, cut + 1);
1967 data[cut] = 0;
1959 } 1968 }
1960 1969
1961 return data; 1970 return data;
@@ -1987,17 +1996,26 @@ namespace OpenSim.Framework
1987 /// <returns></returns> 1996 /// <returns></returns>
1988 public static byte[] StringToBytes1024(string str) 1997 public static byte[] StringToBytes1024(string str)
1989 { 1998 {
1990 if (String.IsNullOrEmpty(str)) { return Utils.EmptyBytes; } 1999 if (String.IsNullOrEmpty(str))
1991 if (str.Length > 1023) str = str.Remove(1023); 2000 return Utils.EmptyBytes;
1992 if (!str.EndsWith("\0")) { str += "\0"; } 2001
2002 if (!str.EndsWith("\0"))
2003 str += "\0";
1993 2004
1994 // Because this is UTF-8 encoding and not ASCII, it's possible we 2005 // Because this is UTF-8 encoding and not ASCII, it's possible we
1995 // might have gotten an oversized array even after the string trim 2006 // might have gotten an oversized array even after the string trim
1996 byte[] data = UTF8.GetBytes(str); 2007 byte[] data = UTF8.GetBytes(str);
2008
1997 if (data.Length > 1024) 2009 if (data.Length > 1024)
1998 { 2010 {
1999 Array.Resize<byte>(ref data, 1024); 2011 int cut = 1023;
2000 data[1023] = 0; 2012 if((data[cut] & 0x80 ) != 0 )
2013 {
2014 while(cut > 0 && (data[cut] & 0xc0) != 0xc0)
2015 cut--;
2016 }
2017 Array.Resize<byte>(ref data, cut + 1);
2018 data[cut] = 0;
2001 } 2019 }
2002 2020
2003 return data; 2021 return data;