예제 #1
0
        // Token: 0x06000252 RID: 594 RVA: 0x0000AF40 File Offset: 0x00009140
        internal static MimeStringList EncodeValue(string value, EncodingOptions encodingOptions, ValueEncodingStyle style)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(MimeStringList.Empty);
            }
            if (!MimeCommon.IsEncodingRequired(value, encodingOptions.AllowUTF8))
            {
                return(new MimeStringList(new MimeString(value)));
            }
            MimeStringList result = default(MimeStringList);
            Charset        encodingCharset;

            if (encodingOptions.CharsetName != null)
            {
                encodingCharset = encodingOptions.GetEncodingCharset();
            }
            else
            {
                OutboundCodePageDetector outboundCodePageDetector = new OutboundCodePageDetector();
                outboundCodePageDetector.AddText(value);
                int codePage = outboundCodePageDetector.GetCodePage();
                if (!Charset.TryGetCharset(codePage, out encodingCharset))
                {
                    encodingCharset = MimeCommon.DefaultEncodingOptions.GetEncodingCharset();
                }
            }
            ByteEncoder.Tables.CharClasses charClasses = ByteEncoder.Tables.CharClasses.QEncode;
            if (style == ValueEncodingStyle.Phrase)
            {
                charClasses |= ByteEncoder.Tables.CharClasses.QPhraseUnsafe;
            }
            else if (style == ValueEncodingStyle.Comment)
            {
                charClasses |= ByteEncoder.Tables.CharClasses.QCommentUnsafe;
            }
            bool allowQEncoding = false;

            MimeCommon.CalculateMethodAndChunkSize calculateMethodAndChunkSize;
            if (encodingCharset.Kind == CodePageKind.Sbcs)
            {
                calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeSbcs;
                if (encodingCharset.AsciiSupport >= CodePageAsciiSupport.Fine)
                {
                    allowQEncoding = true;
                }
            }
            else if (encodingCharset.Kind == CodePageKind.Dbcs)
            {
                calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeDbcs;
                if (encodingCharset.AsciiSupport >= CodePageAsciiSupport.Fine)
                {
                    allowQEncoding = true;
                }
            }
            else if (encodingCharset.CodePage == 65001)
            {
                calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeUtf8;
                allowQEncoding = true;
            }
            else if (encodingCharset.CodePage == 1200 || encodingCharset.CodePage == 1201)
            {
                calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeUnicode16;
            }
            else if (encodingCharset.CodePage == 12000 || encodingCharset.CodePage == 12001)
            {
                calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeUnicode32;
            }
            else
            {
                calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeMbcs;
            }
            int num  = 75;
            int num2 = 7 + encodingCharset.Name.Length;
            int num3 = num - num2;

            if (num3 < 32)
            {
                num  = num2 + 32;
                num3 = 32;
            }
            byte[]   byteBuffer = ScratchPad.GetByteBuffer(num3);
            Encoding encoding   = encodingCharset.GetEncoding();

            byte[] array = new byte[5 + encodingCharset.Name.Length];
            int    num4  = 0;

            array[num4++] = 61;
            array[num4++] = 63;
            num4         += ByteString.StringToBytes(encodingCharset.Name, array, num4, false);
            array[num4++] = 63;
            array[num4++] = 88;
            array[num4++] = 63;
            MimeString mimeString = new MimeString(array);
            int        num5       = 0;

            byte[] array2 = null;
            int    num6   = 0;
            int    num7   = num3 / 4;

            while (num5 != value.Length)
            {
                byte b;
                int  num8;
                calculateMethodAndChunkSize(allowQEncoding, charClasses, encoding, value, num5, num3, out b, out num8);
                int bytes;
                int num10;
                for (;;)
                {
                    for (;;)
                    {
                        try
                        {
                            bytes = encoding.GetBytes(value, num5, num8, byteBuffer, 0);
                        }
                        catch (ArgumentException)
                        {
                            if (num8 < 2)
                            {
                                throw;
                            }
                            num8 -= ((num8 > 10) ? 3 : 1);
                            if (MimeCommon.IsLowSurrogate(value[num5 + num8]) && MimeCommon.IsHighSurrogate(value[num5 + num8 - 1]))
                            {
                                num8--;
                            }
                            break;
                        }
                        if (bytes == 0)
                        {
                            goto IL_424;
                        }
                        if (array2 == null || array2.Length - num6 < num + 1)
                        {
                            if (array2 != null)
                            {
                                result.Append(new MimeString(array2, 0, num6));
                                num6 = 0;
                            }
                            int val  = ((value.Length - num5) / num8 + 1) * (num + 1);
                            int num9 = Math.Min(val, 4096 / (num + 1) * (num + 1));
                            array2 = new byte[num9];
                        }
                        num10 = num6;
                        if (result.Count > 0 || num10 > 0)
                        {
                            array2[num10++] = 32;
                        }
                        num10            += mimeString.CopyTo(array2, num10);
                        array2[num10 - 2] = b;
                        if (b != 81)
                        {
                            goto IL_3F5;
                        }
                        int num11 = num10;
                        int num12 = 0;
                        while (num12 < bytes && num10 - num11 + 1 <= num3)
                        {
                            byte b2 = byteBuffer[num12];
                            if (MimeCommon.QEncodingRequired((char)b2, charClasses))
                            {
                                if (num10 - num11 + 3 > num3)
                                {
                                    break;
                                }
                                array2[num10++] = 61;
                                array2[num10++] = ByteEncoder.NibbleToHex[b2 >> 4];
                                array2[num10++] = ByteEncoder.NibbleToHex[(int)(b2 & 15)];
                            }
                            else
                            {
                                if (b2 == 32)
                                {
                                    b2 = 95;
                                }
                                array2[num10++] = b2;
                            }
                            num12++;
                        }
                        if (num12 == bytes)
                        {
                            goto IL_408;
                        }
                        if (num8 < 2)
                        {
                            goto Block_26;
                        }
                        num8 -= ((num8 > 10) ? 3 : 1);
                        if (MimeCommon.IsLowSurrogate(value[num5 + num8]) && MimeCommon.IsHighSurrogate(value[num5 + num8 - 1]))
                        {
                            num8--;
                        }
                    }
                }
IL_424:
                num5 += num8;
                continue;
Block_26:
                throw new InvalidOperationException("unexpected thing just happened");
IL_408:
                array2[num10++] = 63;
                array2[num10++] = 61;
                num6            = num10;
                goto IL_424;
IL_3F5:
                num10 += MimeCommon.Base64EncodeChunk(byteBuffer, 0, bytes, array2, num10);
                goto IL_408;
            }
            if (array2 != null)
            {
                result.Append(new MimeString(array2, 0, num6));
            }
            return(result);
        }