aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim
diff options
context:
space:
mode:
authorUbitUmarov2015-10-28 21:27:56 +0000
committerDiva Canto2015-11-10 20:08:44 -0800
commitb383cdae339631d009a07cde4f10fcd3d61d9e77 (patch)
treebc27a285ed279ca55700bbc8e769079ca2e65204 /OpenSim
parentDon't crash the sim if the map image is null. (diff)
downloadopensim-SC_OLD-b383cdae339631d009a07cde4f10fcd3d61d9e77.zip
opensim-SC_OLD-b383cdae339631d009a07cde4f10fcd3d61d9e77.tar.gz
opensim-SC_OLD-b383cdae339631d009a07cde4f10fcd3d61d9e77.tar.bz2
opensim-SC_OLD-b383cdae339631d009a07cde4f10fcd3d61d9e77.tar.xz
fix cut points of UTF-8 strings
Diffstat (limited to 'OpenSim')
-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 a5f798d..1f74168 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -1818,17 +1818,26 @@ namespace OpenSim.Framework
1818 /// <returns></returns> 1818 /// <returns></returns>
1819 public static byte[] StringToBytes256(string str) 1819 public static byte[] StringToBytes256(string str)
1820 { 1820 {
1821 if (String.IsNullOrEmpty(str)) { return Utils.EmptyBytes; } 1821 if (String.IsNullOrEmpty(str))
1822 if (str.Length > 254) str = str.Remove(254); 1822 return Utils.EmptyBytes;
1823 if (!str.EndsWith("\0")) { str += "\0"; } 1823
1824 if (!str.EndsWith("\0"))
1825 str += "\0";
1824 1826
1825 // Because this is UTF-8 encoding and not ASCII, it's possible we 1827 // Because this is UTF-8 encoding and not ASCII, it's possible we
1826 // might have gotten an oversized array even after the string trim 1828 // might have gotten an oversized array even after the string trim
1827 byte[] data = UTF8.GetBytes(str); 1829 byte[] data = UTF8.GetBytes(str);
1830
1828 if (data.Length > 256) 1831 if (data.Length > 256)
1829 { 1832 {
1830 Array.Resize<byte>(ref data, 256); 1833 int cut = 255;
1831 data[255] = 0; 1834 if((data[cut] & 0x80 ) != 0 )
1835 {
1836 while(cut > 0 && (data[cut] & 0xc0) != 0xc0)
1837 cut--;
1838 }
1839 Array.Resize<byte>(ref data, cut + 1);
1840 data[cut] = 0;
1832 } 1841 }
1833 1842
1834 return data; 1843 return data;
@@ -1860,17 +1869,26 @@ namespace OpenSim.Framework
1860 /// <returns></returns> 1869 /// <returns></returns>
1861 public static byte[] StringToBytes1024(string str) 1870 public static byte[] StringToBytes1024(string str)
1862 { 1871 {
1863 if (String.IsNullOrEmpty(str)) { return Utils.EmptyBytes; } 1872 if (String.IsNullOrEmpty(str))
1864 if (str.Length > 1023) str = str.Remove(1023); 1873 return Utils.EmptyBytes;
1865 if (!str.EndsWith("\0")) { str += "\0"; } 1874
1875 if (!str.EndsWith("\0"))
1876 str += "\0";
1866 1877
1867 // Because this is UTF-8 encoding and not ASCII, it's possible we 1878 // Because this is UTF-8 encoding and not ASCII, it's possible we
1868 // might have gotten an oversized array even after the string trim 1879 // might have gotten an oversized array even after the string trim
1869 byte[] data = UTF8.GetBytes(str); 1880 byte[] data = UTF8.GetBytes(str);
1881
1870 if (data.Length > 1024) 1882 if (data.Length > 1024)
1871 { 1883 {
1872 Array.Resize<byte>(ref data, 1024); 1884 int cut = 1023;
1873 data[1023] = 0; 1885 if((data[cut] & 0x80 ) != 0 )
1886 {
1887 while(cut > 0 && (data[cut] & 0xc0) != 0xc0)
1888 cut--;
1889 }
1890 Array.Resize<byte>(ref data, cut + 1);
1891 data[cut] = 0;
1874 } 1892 }
1875 1893
1876 return data; 1894 return data;