Exemple #1
0
        public static void Convert(Stream source, Stream destination)
        {
            int i = 0;

            byte[][] array  = null;
            byte[]   array2 = null;
            using (Stream stream = new SuppressCloseStream(source))
            {
                using (MimeReader mimeReader = new MimeReader(stream, true, DecodingOptions.Default, MimeLimits.Unlimited, true, false))
                {
                    while (mimeReader.ReadNextPart())
                    {
                        while (i >= mimeReader.Depth)
                        {
                            byte[] array3 = array[--i];
                            if (array3 != null)
                            {
                                destination.Write(array3, 0, array3.Length - 2);
                                destination.Write(MimeString.TwoDashesCRLF, 0, MimeString.TwoDashesCRLF.Length);
                            }
                        }
                        if (i > 0)
                        {
                            byte[] array3 = array[i - 1];
                            if (array3 != null)
                            {
                                destination.Write(array3, 0, array3.Length);
                            }
                        }
                        HeaderList        headerList        = HeaderList.ReadFrom(mimeReader);
                        ContentTypeHeader contentTypeHeader = headerList.FindFirst(HeaderId.ContentType) as ContentTypeHeader;
                        bool flag;
                        bool flag2;
                        bool flag3;
                        EightToSevenBitConverter.Analyse(contentTypeHeader, out flag, out flag2, out flag3);
                        Header header = headerList.FindFirst(HeaderId.ContentTransferEncoding);
                        if (flag2 || flag)
                        {
                            if (header != null)
                            {
                                headerList.RemoveChild(header);
                            }
                            headerList.WriteTo(destination);
                            byte[] array3;
                            if (flag)
                            {
                                array3 = null;
                                destination.Write(MimeString.CrLf, 0, MimeString.CrLf.Length);
                            }
                            else
                            {
                                array3 = MimePart.GetBoundary(contentTypeHeader);
                            }
                            if (array == null)
                            {
                                array = new byte[4][];
                            }
                            else if (array.Length == i)
                            {
                                byte[][] array4 = new byte[array.Length * 2][];
                                Array.Copy(array, 0, array4, 0, i);
                                array = array4;
                            }
                            array[i++] = array3;
                        }
                        else
                        {
                            Stream stream2 = null;
                            try
                            {
                                stream2 = mimeReader.GetRawContentReadStream();
                                if (header != null && stream2 != null)
                                {
                                    ContentTransferEncoding encodingType = MimePart.GetEncodingType(header.FirstRawToken);
                                    if (encodingType == ContentTransferEncoding.EightBit || encodingType == ContentTransferEncoding.Binary)
                                    {
                                        if (flag3)
                                        {
                                            header.RawValue = MimeString.QuotedPrintable;
                                            stream2         = new EncoderStream(stream2, new QPEncoder(), EncoderStreamAccess.Read);
                                        }
                                        else
                                        {
                                            header.RawValue = MimeString.Base64;
                                            stream2         = new EncoderStream(stream2, new Base64Encoder(), EncoderStreamAccess.Read);
                                        }
                                    }
                                }
                                headerList.WriteTo(destination);
                                destination.Write(MimeString.CrLf, 0, MimeString.CrLf.Length);
                                if (stream2 != null)
                                {
                                    DataStorage.CopyStreamToStream(stream2, destination, long.MaxValue, ref array2);
                                }
                            }
                            finally
                            {
                                if (stream2 != null)
                                {
                                    stream2.Dispose();
                                }
                            }
                        }
                    }
                    while (i > 0)
                    {
                        byte[] array3 = array[--i];
                        if (array3 != null)
                        {
                            destination.Write(array3, 0, array3.Length - 2);
                            destination.Write(MimeString.TwoDashesCRLF, 0, MimeString.TwoDashesCRLF.Length);
                        }
                    }
                }
            }
        }