Esempio n. 1
0
 public bool TryDecodeValue(
     Globalization.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;
     var stringBuilder = Internal.ScratchPad.GetStringBuilder(System.Math.Min(1024, iterator.TotalLength));
     char[] charBuffer = null;
     byte[] byteBuffer = null;
     var currentPosition1 = iterator.CurrentPosition;
     var whitespaceOnly = false;
     var flag1 = false;
     if (defaultCharset != null && (enableJisDetection || enableUtf8Detection || enableDbcsDetection && Globalization.FeInboundCharsetDetector.IsSupportedFarEastCharset(defaultCharset))) {
         defaultCharset = this.DetectValueCharset(defaultCharset, enableJisDetection, enableUtf8Detection, enableDbcsDetection, out encodingScheme);
         flag1 = true;
     }
     System.Text.Decoder decoder1 = null;
     string lastEncodedWordCharsetName = null;
     if (!enable2047)
         iterator.SkipToEof();
     else {
         string lastEncodedWordLanguage = null;
         Globalization.Charset charset1 = null;
         System.Text.Decoder decoder2 = null;
         var flag2 = true;
         string encodedWordCharsetName;
         while (true) {
             this.ParseRawFragment(ref whitespaceOnly);
             if (!iterator.Eof) {
                 var currentPosition2 = iterator.CurrentPosition;
                 string encodedWordLanguage;
                 byte bOrQ;
                 ValuePosition encodedWordContentStart;
                 ValuePosition encodedWordContentEnd;
                 if (!this.ParseEncodedWord(lastEncodedWordCharsetName, lastEncodedWordLanguage, ref byteBuffer, out encodedWordCharsetName, out encodedWordLanguage, out bOrQ, out encodedWordContentStart, out encodedWordContentEnd))
                     whitespaceOnly = false;
                 else {
                     if (lastEncodedWordCharsetName == null) {
                         encodingScheme = EncodingScheme.Rfc2047;
                         charsetName = encodedWordCharsetName;
                         cultureName = encodedWordLanguage;
                     }
                     lastEncodedWordCharsetName = encodedWordCharsetName;
                     if (currentPosition1 != currentPosition2 && !whitespaceOnly) {
                         if (!flag2) {
                             this.FlushDecoder(decoder2, allowControlCharacters, ref byteBuffer, ref charBuffer, stringBuilder);
                             flag2 = true;
                         }
                         if (decoder1 == null) {
                             if (defaultCharset == null || !defaultCharset.IsAvailable) {
                                 if (!enableFallback)
                                     break;
                             } else {
                                 decoder1 = defaultCharset.GetEncoding()
                                                          .GetDecoder();
                             }
                         }
                         if (decoder1 != null)
                             this.ConvertRawFragment(currentPosition1, currentPosition2, decoder1, allowControlCharacters, ref charBuffer, stringBuilder);
                         else
                             this.ZeroExpandFragment(currentPosition1, currentPosition2, allowControlCharacters, stringBuilder);
                     }
                     Globalization.Charset charset2;
                     if (!Globalization.Charset.TryGetCharset(encodedWordCharsetName, out charset2)) {
                         if (!flag2) {
                             this.FlushDecoder(decoder2, allowControlCharacters, ref byteBuffer, ref charBuffer, stringBuilder);
                             flag2 = true;
                         }
                         if (enableFallback)
                             decoder2 = null;
                         else
                             goto label_25;
                     } else if (charset2 != charset1) {
                         if (!flag2) {
                             this.FlushDecoder(decoder2, allowControlCharacters, ref byteBuffer, ref charBuffer, stringBuilder);
                             flag2 = true;
                         }
                         if (!charset2.IsAvailable) {
                             if (enableFallback)
                                 decoder2 = null;
                             else
                                 goto label_32;
                         } else {
                             decoder2 = charset2.GetEncoding()
                                                .GetDecoder();
                             charset1 = charset2;
                         }
                     }
                     if (decoder2 != null) {
                         this.DecodeEncodedWord(bOrQ, decoder2, encodedWordContentStart, encodedWordContentEnd, allowControlCharacters, ref byteBuffer, ref charBuffer, stringBuilder);
                         flag2 = false;
                     } else
                         this.ZeroExpandFragment(currentPosition2, iterator.CurrentPosition, allowControlCharacters, stringBuilder);
                     currentPosition1 = iterator.CurrentPosition;
                     whitespaceOnly = true;
                 }
             } else
                 goto label_39;
         }
         charsetName = defaultCharset == null ? null : defaultCharset.Name;
         return false;
         label_25:
         charsetName = encodedWordCharsetName;
         return false;
         label_32:
         charsetName = encodedWordCharsetName;
         return false;
         label_39:
         if (!flag2)
             this.FlushDecoder(decoder2, allowControlCharacters, ref byteBuffer, ref charBuffer, stringBuilder);
     }
     if (currentPosition1 != iterator.CurrentPosition) {
         if (lastEncodedWordCharsetName == null) {
             charsetName = !flag1 || encodingScheme != EncodingScheme.None || (defaultCharset == null || defaultCharset.IsSevenBit) || defaultCharset.AsciiSupport != Globalization.CodePageAsciiSupport.Complete
                               ? (defaultCharset == null ? null : defaultCharset.Name)
                               : Globalization.Charset.ASCII.Name;
         }
         if (decoder1 == null) {
             if (defaultCharset == null || !defaultCharset.IsAvailable) {
                 if (!enableFallback) {
                     charsetName = defaultCharset == null ? null : defaultCharset.Name;
                     return false;
                 }
                 decoder1 = null;
             } else {
                 decoder1 = defaultCharset.GetEncoding()
                                          .GetDecoder();
             }
         }
         if (decoder1 != null)
             this.ConvertRawFragment(currentPosition1, iterator.CurrentPosition, decoder1, allowControlCharacters, ref charBuffer, stringBuilder);
         else
             this.ZeroExpandFragment(currentPosition1, iterator.CurrentPosition, allowControlCharacters, stringBuilder);
     }
     Internal.ScratchPad.ReleaseStringBuilder();
     value = stringBuilder.ToString();
     return true;
 }