From 45ff7cec805755088dee3dc17f158e89f79bfeb5 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Wed, 28 Oct 2015 21:27:56 +0000 Subject: fix cut points of UTF-8 strings --- OpenSim/Framework/Util.cs | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'OpenSim/Framework') 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 /// public static byte[] StringToBytes256(string str) { - if (String.IsNullOrEmpty(str)) { return Utils.EmptyBytes; } - if (str.Length > 254) str = str.Remove(254); - if (!str.EndsWith("\0")) { str += "\0"; } + if (String.IsNullOrEmpty(str)) + return Utils.EmptyBytes; + + if (!str.EndsWith("\0")) + str += "\0"; // 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 > 256) { - Array.Resize(ref data, 256); - data[255] = 0; + int cut = 255; + if((data[cut] & 0x80 ) != 0 ) + { + while(cut > 0 && (data[cut] & 0xc0) != 0xc0) + cut--; + } + Array.Resize(ref data, cut + 1); + data[cut] = 0; } return data; @@ -1987,17 +1996,26 @@ namespace OpenSim.Framework /// public static byte[] StringToBytes1024(string str) { - if (String.IsNullOrEmpty(str)) { return Utils.EmptyBytes; } - if (str.Length > 1023) str = str.Remove(1023); - if (!str.EndsWith("\0")) { str += "\0"; } + if (String.IsNullOrEmpty(str)) + return Utils.EmptyBytes; + + if (!str.EndsWith("\0")) + str += "\0"; // 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 > 1024) { - Array.Resize(ref data, 1024); - data[1023] = 0; + int cut = 1023; + if((data[cut] & 0x80 ) != 0 ) + { + while(cut > 0 && (data[cut] & 0xc0) != 0xc0) + cut--; + } + Array.Resize(ref data, cut + 1); + data[cut] = 0; } return data; -- cgit v1.1