Пример #1
0
        private void ZeroExpandFragment(ValuePosition start, ValuePosition end, bool allowControlCharacters, StringBuilder sb)
        {
            ValueIterator valueIterator = new ValueIterator(this.iterator.Lines, this.iterator.LinesMask, start, end);

            while (!valueIterator.Eof)
            {
                byte b = (byte)valueIterator.Get();
                if (!allowControlCharacters && b < 32)
                {
                    b = (byte)ValueDecoder.ReplaceProhibitedControlCharacter((char)b);
                }
                sb.Append((char)b);
            }
        }
Пример #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
        private void ConvertRawFragment(ValuePosition start, ValuePosition end, Decoder decoder, bool allowControlCharacters, ref char[] charBuffer, StringBuilder sb)
        {
            ValueIterator valueIterator = new ValueIterator(this.iterator.Lines, this.iterator.LinesMask, start, end);

            if (!valueIterator.Eof)
            {
                if (charBuffer == null)
                {
                    charBuffer = ScratchPad.GetCharBuffer(Math.Min(1024, this.iterator.TotalLength));
                }
                int  length;
                int  num;
                bool flag;
                do
                {
                    decoder.Convert(valueIterator.Bytes, valueIterator.Offset, valueIterator.Length, charBuffer, 0, charBuffer.Length, false, out length, out num, out flag);
                    if (num != 0)
                    {
                        if (!allowControlCharacters)
                        {
                            ValueDecoder.RemoveProhibitedControlCharacters(charBuffer, 0, num);
                        }
                        sb.Append(charBuffer, 0, num);
                    }
                    valueIterator.Get(length);
                }while (!flag || !valueIterator.Eof);
                decoder.Convert(MimeString.EmptyByteArray, 0, 0, charBuffer, 0, charBuffer.Length, true, out length, out num, out flag);
                if (num != 0)
                {
                    if (!allowControlCharacters)
                    {
                        ValueDecoder.RemoveProhibitedControlCharacters(charBuffer, 0, num);
                    }
                    sb.Append(charBuffer, 0, num);
                }
            }
        }
Пример #4
0
        private void DecodeEncodedWord(byte bOrQ, Decoder decoder, ValuePosition encodedWordContentStart, ValuePosition encodedWordContentEnd, bool allowControlCharacters, ref byte[] byteBuffer, ref char[] charBuffer, StringBuilder sb)
        {
            ValueIterator valueIterator = new ValueIterator(this.iterator.Lines, this.iterator.LinesMask, encodedWordContentStart, encodedWordContentEnd);

            if (charBuffer == null)
            {
                charBuffer = ScratchPad.GetCharBuffer(Math.Min(1024, this.iterator.TotalLength));
            }
            if (byteBuffer == null)
            {
                byteBuffer = ScratchPad.GetByteBuffer(Math.Max(this.maxCharsetNameLength + 1, Math.Min(1024, this.iterator.TotalLength)));
            }
            int num = 0;

            if (bOrQ == 66)
            {
                int num2 = 0;
                int num3 = 0;
                while (!valueIterator.Eof)
                {
                    byte b = (byte)(valueIterator.Get() - 32);
                    if ((int)b < ByteEncoder.Tables.Base64ToByte.Length)
                    {
                        b = ByteEncoder.Tables.Base64ToByte[(int)b];
                        if (b < 64)
                        {
                            num3 = (num3 << 6 | (int)b);
                            num2++;
                            if (num2 == 4)
                            {
                                byteBuffer[num++] = (byte)(num3 >> 16);
                                byteBuffer[num++] = (byte)(num3 >> 8);
                                byteBuffer[num++] = (byte)num3;
                                num2 = 0;
                                if (num + 3 >= byteBuffer.Length)
                                {
                                    this.FlushDecodedBytes(byteBuffer, num, decoder, allowControlCharacters, charBuffer, sb);
                                    num = 0;
                                }
                            }
                        }
                    }
                }
                if (num2 != 0)
                {
                    if (num2 == 2)
                    {
                        num3 <<= 12;
                        byteBuffer[num++] = (byte)(num3 >> 16);
                    }
                    else if (num2 == 3)
                    {
                        num3 <<= 6;
                        byteBuffer[num++] = (byte)(num3 >> 16);
                        byteBuffer[num++] = (byte)(num3 >> 8);
                    }
                }
            }
            else
            {
                while (!valueIterator.Eof)
                {
                    byte b2 = (byte)valueIterator.Get();
                    if (b2 == 61)
                    {
                        int num4 = valueIterator.Get();
                        int num5 = valueIterator.Get();
                        num4 = (int)((num4 < 0) ? byte.MaxValue : ByteEncoder.Tables.NumFromHex[num4]);
                        num5 = (int)((num5 < 0) ? byte.MaxValue : ByteEncoder.Tables.NumFromHex[num5]);
                        if (num4 == 255 || num5 == 255)
                        {
                            b2 = 61;
                        }
                        else
                        {
                            b2 = (byte)(num4 << 4 | num5);
                        }
                    }
                    else if (b2 == 95)
                    {
                        b2 = 32;
                    }
                    byteBuffer[num++] = b2;
                    if (num >= byteBuffer.Length)
                    {
                        this.FlushDecodedBytes(byteBuffer, num, decoder, allowControlCharacters, charBuffer, sb);
                        num = 0;
                    }
                }
            }
            if (num != 0)
            {
                this.FlushDecodedBytes(byteBuffer, num, decoder, allowControlCharacters, charBuffer, sb);
            }
        }