示例#1
0
        internal void ValidateCharset(bool charsetWasDefaulted, Stream contentStream)
        {
            bool strongDefault = !charsetWasDefaulted && this.charset.CodePage != 20127;
            FeInboundCharsetDetector feInboundCharsetDetector = new FeInboundCharsetDetector(this.charset.CodePage, strongDefault, true, true, true);

            byte[] byteBuffer = ScratchPad.GetByteBuffer(1024);
            int    num        = 0;
            int    num2       = 0;

            while (num2 < 4096 && (num = contentStream.Read(byteBuffer, 0, byteBuffer.Length)) != 0)
            {
                feInboundCharsetDetector.AddBytes(byteBuffer, 0, num, false);
                num2 += num;
            }
            if (num == 0)
            {
                feInboundCharsetDetector.AddBytes(byteBuffer, 0, 0, true);
            }
            int codePageChoice = feInboundCharsetDetector.GetCodePageChoice();

            if (codePageChoice != this.charset.CodePage)
            {
                this.charset = Charset.GetCharset(codePageChoice);
            }
        }
示例#2
0
        private Charset DetectValueCharset(Charset defaultCharset, bool enableJisDetection, bool enableUtf8Detection, bool enableDbcsDetection, out EncodingScheme encodingScheme)
        {
            ValueIterator            valueIterator            = new ValueIterator(this.iterator.Lines, this.iterator.LinesMask);
            FeInboundCharsetDetector feInboundCharsetDetector = new FeInboundCharsetDetector(defaultCharset.CodePage, false, enableJisDetection, enableUtf8Detection, enableDbcsDetection);

            while (!valueIterator.Eof)
            {
                feInboundCharsetDetector.AddBytes(valueIterator.Bytes, valueIterator.Offset, valueIterator.Length, false);
                valueIterator.Get(valueIterator.Length);
            }
            feInboundCharsetDetector.AddBytes(null, 0, 0, true);
            int codePageChoice = feInboundCharsetDetector.GetCodePageChoice();

            if (codePageChoice != defaultCharset.CodePage)
            {
                defaultCharset = Charset.GetCharset(codePageChoice);
            }
            if (!feInboundCharsetDetector.PureAscii)
            {
                if (feInboundCharsetDetector.Iso2022JpLikely || feInboundCharsetDetector.Iso2022KrLikely)
                {
                    encodingScheme = EncodingScheme.Jis;
                }
                else
                {
                    encodingScheme = EncodingScheme.EightBit;
                }
            }
            else if (defaultCharset.Name == "iso-2022-jp" && !feInboundCharsetDetector.Iso2022KrLikely)
            {
                encodingScheme = EncodingScheme.Jis;
            }
            else
            {
                encodingScheme = EncodingScheme.None;
            }
            return(defaultCharset);
        }
示例#3
0
        public bool TryDecodeValue(Charset defaultCharset, bool enableFallback, bool allowControlCharacters, bool enable2047, bool enableJisDetection, bool enableUtf8Detection, bool enableDbcsDetection, out string charsetName, out string cultureName, out EncodingScheme encodingScheme, out string value)
        {
            charsetName    = null;
            cultureName    = null;
            encodingScheme = EncodingScheme.None;
            value          = null;
            StringBuilder stringBuilder = ScratchPad.GetStringBuilder(Math.Min(1024, this.iterator.TotalLength));

            char[]        array           = null;
            byte[]        array2          = null;
            ValuePosition currentPosition = this.iterator.CurrentPosition;
            bool          flag            = false;
            bool          flag2           = false;

            if (defaultCharset != null && (enableJisDetection || enableUtf8Detection || (enableDbcsDetection && FeInboundCharsetDetector.IsSupportedFarEastCharset(defaultCharset))))
            {
                defaultCharset = this.DetectValueCharset(defaultCharset, enableJisDetection, enableUtf8Detection, enableDbcsDetection, out encodingScheme);
                flag2          = true;
            }
            Decoder decoder = null;
            string  text    = null;

            if (!enable2047)
            {
                this.iterator.SkipToEof();
            }
            else
            {
                string  lastEncodedWordLanguage = null;
                Charset charset  = null;
                Decoder decoder2 = null;
                bool    flag3    = true;
                string  text2;
                for (;;)
                {
                    this.ParseRawFragment(ref flag);
                    if (this.iterator.Eof)
                    {
                        goto IL_217;
                    }
                    ValuePosition currentPosition2 = this.iterator.CurrentPosition;
                    string        text3;
                    byte          bOrQ;
                    ValuePosition encodedWordContentStart;
                    ValuePosition encodedWordContentEnd;
                    if (!this.ParseEncodedWord(text, lastEncodedWordLanguage, ref array2, out text2, out text3, out bOrQ, out encodedWordContentStart, out encodedWordContentEnd))
                    {
                        flag = false;
                    }
                    else
                    {
                        if (text == null)
                        {
                            encodingScheme = EncodingScheme.Rfc2047;
                            charsetName    = text2;
                            cultureName    = text3;
                        }
                        text = text2;
                        if (currentPosition != currentPosition2 && !flag)
                        {
                            if (!flag3)
                            {
                                this.FlushDecoder(decoder2, allowControlCharacters, ref array2, ref array, stringBuilder);
                                flag3 = true;
                            }
                            if (decoder == null)
                            {
                                if (defaultCharset == null || !defaultCharset.IsAvailable)
                                {
                                    if (!enableFallback)
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    decoder = defaultCharset.GetEncoding().GetDecoder();
                                }
                            }
                            if (decoder != null)
                            {
                                this.ConvertRawFragment(currentPosition, currentPosition2, decoder, allowControlCharacters, ref array, stringBuilder);
                            }
                            else
                            {
                                this.ZeroExpandFragment(currentPosition, currentPosition2, allowControlCharacters, stringBuilder);
                            }
                        }
                        Charset charset2;
                        if (!Charset.TryGetCharset(text2, out charset2))
                        {
                            if (!flag3)
                            {
                                this.FlushDecoder(decoder2, allowControlCharacters, ref array2, ref array, stringBuilder);
                                flag3 = true;
                            }
                            if (!enableFallback)
                            {
                                goto Block_19;
                            }
                            decoder2 = null;
                        }
                        else if (charset2 != charset)
                        {
                            if (!flag3)
                            {
                                this.FlushDecoder(decoder2, allowControlCharacters, ref array2, ref array, stringBuilder);
                                flag3 = true;
                            }
                            if (!charset2.IsAvailable)
                            {
                                if (!enableFallback)
                                {
                                    goto Block_23;
                                }
                                decoder2 = null;
                            }
                            else
                            {
                                decoder2 = charset2.GetEncoding().GetDecoder();
                                charset  = charset2;
                            }
                        }
                        if (decoder2 != null)
                        {
                            this.DecodeEncodedWord(bOrQ, decoder2, encodedWordContentStart, encodedWordContentEnd, allowControlCharacters, ref array2, ref array, stringBuilder);
                            flag3 = false;
                        }
                        else
                        {
                            this.ZeroExpandFragment(currentPosition2, this.iterator.CurrentPosition, allowControlCharacters, stringBuilder);
                        }
                        currentPosition = this.iterator.CurrentPosition;
                        flag            = true;
                    }
                }
                charsetName = ((defaultCharset == null) ? null : defaultCharset.Name);
                return(false);

Block_19:
                charsetName = text2;
                return(false);

Block_23:
                charsetName = text2;
                return(false);

IL_217:
                if (!flag3)
                {
                    this.FlushDecoder(decoder2, allowControlCharacters, ref array2, ref array, stringBuilder);
                }
            }
            if (currentPosition != this.iterator.CurrentPosition)
            {
                if (text == null)
                {
                    if (flag2 && encodingScheme == EncodingScheme.None && defaultCharset != null && !defaultCharset.IsSevenBit && defaultCharset.AsciiSupport == CodePageAsciiSupport.Complete)
                    {
                        charsetName = Charset.ASCII.Name;
                    }
                    else
                    {
                        charsetName = ((defaultCharset == null) ? null : defaultCharset.Name);
                    }
                }
                if (decoder == null)
                {
                    if (defaultCharset == null || !defaultCharset.IsAvailable)
                    {
                        if (!enableFallback)
                        {
                            charsetName = ((defaultCharset == null) ? null : defaultCharset.Name);
                            return(false);
                        }
                        decoder = null;
                    }
                    else
                    {
                        decoder = defaultCharset.GetEncoding().GetDecoder();
                    }
                }
                if (decoder != null)
                {
                    this.ConvertRawFragment(currentPosition, this.iterator.CurrentPosition, decoder, allowControlCharacters, ref array, stringBuilder);
                }
                else
                {
                    this.ZeroExpandFragment(currentPosition, this.iterator.CurrentPosition, allowControlCharacters, stringBuilder);
                }
            }
            ScratchPad.ReleaseStringBuilder();
            value = stringBuilder.ToString();
            return(true);
        }
 public void Reset()
 {
     this = new FeInboundCharsetDetector(0, false, true, true, true);
 }
 public void Reset(int codePage, bool strongDefault, bool enableIsoDetection, bool enableUtf8Detection, bool enableDbcsDetection)
 {
     this = new FeInboundCharsetDetector(codePage, strongDefault, enableIsoDetection, enableUtf8Detection, enableDbcsDetection);
 }