Esempio n. 1
0
        private static string punycode_encode(string unicode)
        {
            if (unicode.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
            }
            StringBuilder stringBuilder = new StringBuilder(unicode.Length);
            int           num1          = 0;
            int           num2          = 0;
            int           startIndex    = 0;

            while (num1 < unicode.Length)
            {
                num1 = unicode.IndexOfAny(IdnMapping.M_Dots, num2);
                if (num1 < 0)
                {
                    num1 = unicode.Length;
                }
                if (num1 == num2)
                {
                    if (num1 != unicode.Length)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
                    }
                    break;
                }
                stringBuilder.Append("xn--");
                bool flag = false;
                switch (CharUnicodeInfo.GetBidiCategory(unicode, num2))
                {
                case BidiCategory.RightToLeft:
                case BidiCategory.RightToLeftArabic:
                    flag = true;
                    int index1 = num1 - 1;
                    if (char.IsLowSurrogate(unicode, index1))
                    {
                        --index1;
                    }
                    switch (CharUnicodeInfo.GetBidiCategory(unicode, index1))
                    {
                    case BidiCategory.RightToLeft:
                    case BidiCategory.RightToLeftArabic:
                        break;

                    default:
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                    }
                }
                int num3 = 0;
                for (int index2 = num2; index2 < num1; ++index2)
                {
                    BidiCategory bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, index2);
                    if (flag && bidiCategory == BidiCategory.LeftToRight)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                    }
                    if (!flag && (bidiCategory == BidiCategory.RightToLeft || bidiCategory == BidiCategory.RightToLeftArabic))
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                    }
                    if (IdnMapping.basic((uint)unicode[index2]))
                    {
                        stringBuilder.Append(IdnMapping.encode_basic(unicode[index2]));
                        ++num3;
                    }
                    else if (char.IsSurrogatePair(unicode, index2))
                    {
                        ++index2;
                    }
                }
                int num4 = num3;
                if (num4 == num1 - num2)
                {
                    stringBuilder.Remove(startIndex, "xn--".Length);
                }
                else
                {
                    if (unicode.Length - num2 >= "xn--".Length && unicode.Substring(num2, "xn--".Length).Equals("xn--", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "unicode");
                    }
                    int num5 = 0;
                    if (num4 > 0)
                    {
                        stringBuilder.Append('-');
                    }
                    int num6 = 128;
                    int num7 = 0;
                    int num8 = 72;
                    while (num3 < num1 - num2)
                    {
                        int cTest  = 134217727;
                        int index2 = num2;
                        while (index2 < num1)
                        {
                            int utf32 = char.ConvertToUtf32(unicode, index2);
                            if (utf32 >= num6 && utf32 < cTest)
                            {
                                cTest = utf32;
                            }
                            index2 += IdnMapping.IsSupplementary(utf32) ? 2 : 1;
                        }
                        int delta  = num7 + (cTest - num6) * (num3 - num5 + 1);
                        int num9   = cTest;
                        int index3 = num2;
                        while (index3 < num1)
                        {
                            int utf32 = char.ConvertToUtf32(unicode, index3);
                            if (utf32 < num9)
                            {
                                ++delta;
                            }
                            if (utf32 == num9)
                            {
                                int d     = delta;
                                int num10 = 36;
                                while (true)
                                {
                                    int num11 = num10 <= num8 ? 1 : (num10 >= num8 + 26 ? 26 : num10 - num8);
                                    if (d >= num11)
                                    {
                                        stringBuilder.Append(IdnMapping.encode_digit(num11 + (d - num11) % (36 - num11)));
                                        d      = (d - num11) / (36 - num11);
                                        num10 += 36;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                stringBuilder.Append(IdnMapping.encode_digit(d));
                                num8  = IdnMapping.adapt(delta, num3 - num5 + 1, num3 == num4);
                                delta = 0;
                                ++num3;
                                if (IdnMapping.IsSupplementary(cTest))
                                {
                                    ++num3;
                                    ++num5;
                                }
                            }
                            index3 += IdnMapping.IsSupplementary(utf32) ? 2 : 1;
                        }
                        num7 = delta + 1;
                        num6 = num9 + 1;
                    }
                }
                if (stringBuilder.Length - startIndex > 63)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
                }
                if (num1 != unicode.Length)
                {
                    stringBuilder.Append('.');
                }
                num2       = num1 + 1;
                startIndex = stringBuilder.Length;
            }
            int    length = stringBuilder.Length;
            int    num12  = (int)byte.MaxValue;
            string str1   = unicode;
            int    index4 = str1.Length - 1;
            int    num13  = IdnMapping.IsDot(str1[index4]) ? 0 : 1;
            int    num14  = num12 - num13;

            if (length > num14)
            {
                string   key      = "Argument_IdnBadNameSize";
                object[] objArray = new object[1];
                int      index1   = 0;
                int      num3     = (int)byte.MaxValue;
                string   str2     = unicode;
                int      index2   = str2.Length - 1;
                int      num4     = IdnMapping.IsDot(str2[index2]) ? 0 : 1;
                // ISSUE: variable of a boxed type
                __Boxed <int> local = (ValueType)(num3 - num4);
                objArray[index1] = (object)local;
                throw new ArgumentException(Environment.GetResourceString(key, objArray), "unicode");
            }
            return(stringBuilder.ToString());
        }
Esempio n. 2
0
        // Token: 0x06002FE2 RID: 12258 RVA: 0x000B7924 File Offset: 0x000B5B24
        private static string punycode_encode(string unicode)
        {
            if (unicode.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
            }
            StringBuilder stringBuilder = new StringBuilder(unicode.Length);
            int           i             = 0;
            int           num           = 0;
            int           num2          = 0;

            while (i < unicode.Length)
            {
                i = unicode.IndexOfAny(IdnMapping.M_Dots, num);
                if (i < 0)
                {
                    i = unicode.Length;
                }
                if (i == num)
                {
                    if (i != unicode.Length)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
                    }
                    break;
                }
                else
                {
                    stringBuilder.Append("xn--");
                    bool         flag         = false;
                    BidiCategory bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, num);
                    if (bidiCategory == BidiCategory.RightToLeft || bidiCategory == BidiCategory.RightToLeftArabic)
                    {
                        flag = true;
                        int num3 = i - 1;
                        if (char.IsLowSurrogate(unicode, num3))
                        {
                            num3--;
                        }
                        bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, num3);
                        if (bidiCategory != BidiCategory.RightToLeft && bidiCategory != BidiCategory.RightToLeftArabic)
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                        }
                    }
                    int j = 0;
                    for (int k = num; k < i; k++)
                    {
                        BidiCategory bidiCategory2 = CharUnicodeInfo.GetBidiCategory(unicode, k);
                        if (flag && bidiCategory2 == BidiCategory.LeftToRight)
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                        }
                        if (!flag && (bidiCategory2 == BidiCategory.RightToLeft || bidiCategory2 == BidiCategory.RightToLeftArabic))
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                        }
                        if (IdnMapping.basic((uint)unicode[k]))
                        {
                            stringBuilder.Append(IdnMapping.encode_basic(unicode[k]));
                            j++;
                        }
                        else if (char.IsSurrogatePair(unicode, k))
                        {
                            k++;
                        }
                    }
                    int num4 = j;
                    if (num4 == i - num)
                    {
                        stringBuilder.Remove(num2, "xn--".Length);
                    }
                    else
                    {
                        if (unicode.Length - num >= "xn--".Length && unicode.Substring(num, "xn--".Length).Equals("xn--", StringComparison.OrdinalIgnoreCase))
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "unicode");
                        }
                        int num5 = 0;
                        if (num4 > 0)
                        {
                            stringBuilder.Append('-');
                        }
                        int num6 = 128;
                        int num7 = 0;
                        int num8 = 72;
                        while (j < i - num)
                        {
                            int num9 = 134217727;
                            int num10;
                            for (int l = num; l < i; l += (IdnMapping.IsSupplementary(num10) ? 2 : 1))
                            {
                                num10 = char.ConvertToUtf32(unicode, l);
                                if (num10 >= num6 && num10 < num9)
                                {
                                    num9 = num10;
                                }
                            }
                            num7 += (num9 - num6) * (j - num5 + 1);
                            num6  = num9;
                            for (int l = num; l < i; l += (IdnMapping.IsSupplementary(num10) ? 2 : 1))
                            {
                                num10 = char.ConvertToUtf32(unicode, l);
                                if (num10 < num6)
                                {
                                    num7++;
                                }
                                if (num10 == num6)
                                {
                                    int num11 = num7;
                                    int num12 = 36;
                                    for (;;)
                                    {
                                        int num13 = (num12 <= num8) ? 1 : ((num12 >= num8 + 26) ? 26 : (num12 - num8));
                                        if (num11 < num13)
                                        {
                                            break;
                                        }
                                        stringBuilder.Append(IdnMapping.encode_digit(num13 + (num11 - num13) % (36 - num13)));
                                        num11  = (num11 - num13) / (36 - num13);
                                        num12 += 36;
                                    }
                                    stringBuilder.Append(IdnMapping.encode_digit(num11));
                                    num8 = IdnMapping.adapt(num7, j - num5 + 1, j == num4);
                                    num7 = 0;
                                    j++;
                                    if (IdnMapping.IsSupplementary(num9))
                                    {
                                        j++;
                                        num5++;
                                    }
                                }
                            }
                            num7++;
                            num6++;
                        }
                    }
                    if (stringBuilder.Length - num2 > 63)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
                    }
                    if (i != unicode.Length)
                    {
                        stringBuilder.Append('.');
                    }
                    num  = i + 1;
                    num2 = stringBuilder.Length;
                }
            }
            if (stringBuilder.Length > 255 - (IdnMapping.IsDot(unicode[unicode.Length - 1]) ? 0 : 1))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", new object[]
                {
                    255 - (IdnMapping.IsDot(unicode[unicode.Length - 1]) ? 0 : 1)
                }), "unicode");
            }
            return(stringBuilder.ToString());
        }