OpenSim.Region.ScriptEngine.Shared.Api.LSL_Api.llXorBase64Strings C# (CSharp) Method

llXorBase64Strings() public method

public llXorBase64Strings ( string str1, string str2 ) : OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString
str1 string
str2 string
return OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString
        public LSL_String llXorBase64Strings(string str1, string str2)
        {
            int padding = 0;

            string b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

            ScriptSleep(300);
            m_host.AddScriptLPS(1);

            if (str1 == String.Empty)
                return String.Empty;
            if (str2 == String.Empty)
                return str1;

            int len = str2.Length;
            if ((len % 4) != 0) // LL is EVIL!!!!
            {
                while (str2.EndsWith("="))
                    str2 = str2.Substring(0, str2.Length - 1);

                len = str2.Length;
                int mod = len % 4;

                if (mod == 1)
                    str2 = str2.Substring(0, str2.Length - 1);
                else if (mod == 2)
                    str2 += "==";
                else if (mod == 3)
                    str2 += "=";
            }

            byte[] data1;
            byte[] data2;
            try
            {
                data1 = Convert.FromBase64String(str1);
                data2 = Convert.FromBase64String(str2);
            }
            catch (Exception)
            {
                return new LSL_String(String.Empty);
            }

            // For cases where the decoded length of s2 is greater
            // than the decoded length of s1, simply perform a normal
            // decode and XOR
            //
            /*
            if (data2.Length >= data1.Length)
            {
                for (int pos = 0 ; pos < data1.Length ; pos++ )
                    data1[pos] ^= data2[pos];

                return Convert.ToBase64String(data1);
            }
            */

            // Remove padding
            while (str1.EndsWith("="))
            {
                str1 = str1.Substring(0, str1.Length - 1);
                padding++;
            }
            while (str2.EndsWith("="))
                str2 = str2.Substring(0, str2.Length - 1);
            
            byte[] d1 = new byte[str1.Length];
            byte[] d2 = new byte[str2.Length];

            for (int i = 0 ; i < str1.Length ; i++)
            {
                int idx = b64.IndexOf(str1.Substring(i, 1));
                if (idx == -1)
                    idx = 0;
                d1[i] = (byte)idx;
            }

            for (int i = 0 ; i < str2.Length ; i++)
            {
                int idx = b64.IndexOf(str2.Substring(i, 1));
                if (idx == -1)
                    idx = 0;
                d2[i] = (byte)idx;
            }

            string output = String.Empty;

            for (int pos = 0 ; pos < d1.Length ; pos++)
                output += b64[d1[pos] ^ d2[pos % d2.Length]];

            // Here's a funny thing: LL blithely violate the base64
            // standard pretty much everywhere. Here, padding is
            // added only if the first input string had it, rather
            // than when the data actually needs it. This can result
            // in invalid base64 being returned. Go figure.

            while (padding-- > 0)
                output += "=";

            return output;
        }
LSL_Api