コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MimeKit.IO.Filters.CharsetFilter"/> class.
        /// </summary>
        /// <remarks>
        /// Creates a new <see cref="CharsetFilter"/> to convert text from the specified
        /// source encoding into the target charset encoding.
        /// </remarks>
        /// <param name="sourceEncoding">Source encoding.</param>
        /// <param name="targetEncoding">Target encoding.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="sourceEncoding"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="targetEncoding"/> is <c>null</c>.</para>
        /// </exception>
        public CharsetFilter(Encoding sourceEncoding, Encoding targetEncoding)
        {
            if (sourceEncoding == null)
            {
                throw new ArgumentNullException(nameof(sourceEncoding));
            }

            if (targetEncoding == null)
            {
                throw new ArgumentNullException(nameof(targetEncoding));
            }

            SourceEncoding = sourceEncoding;
            TargetEncoding = targetEncoding;

            decoder = (Decoder)SourceEncoding.GetDecoder();
            encoder = (Encoder)TargetEncoding.GetEncoder();
        }
コード例 #2
0
        static int Rfc2047EncodeNextChunk(StringBuilder str, string text, ref int index, Encoding encoding, string charset, Encoder encoder, int maxLength)
        {
            int  byteCount = 0, charCount = 0, encodeCount = 0;
            var  buffer = new char[2];
            int  startIndex = index;
            int  nchars, n;
            char c;

            while (index < text.Length)
            {
                c = text[index++];

                if (c < 127)
                {
                    if (IsCtrl(c) || c == '"' || c == '\\')
                    {
                        encodeCount++;
                    }

                    byteCount++;
                    charCount++;
                    nchars = 1;
                    n      = 1;
                }
                else if (c < 256)
                {
                    // iso-8859-1
                    encodeCount++;
                    byteCount++;
                    charCount++;
                    nchars = 1;
                    n      = 1;
                }
                else
                {
                    if (char.IsSurrogatePair(text, index - 1))
                    {
                        buffer[1] = text[index++];
                        nchars    = 2;
                    }
                    else
                    {
                        nchars = 1;
                    }

                    buffer[0] = c;

                    try {
                        n = encoder.GetByteCount(buffer, 0, nchars, true);
                    } catch {
                        n = 3;
                    }

                    charCount   += nchars;
                    encodeCount += n;
                    byteCount   += n;
                }

                if (ExceedsMaxWordLength(charset, byteCount, encodeCount, maxLength))
                {
                    // restore our previous state
                    charCount -= nchars;
                    index     -= nchars;
                    byteCount -= n;
                    break;
                }
            }

            return(Rfc2047.AppendEncodedWord(str, encoding, text, startIndex, charCount, QEncodeMode.Text));
        }
コード例 #3
0
        static bool Rfc2231GetNextValue(FormatOptions options, string charset, Encoder encoder, HexEncoder hex, char[] chars, ref int index, ref byte[] bytes, ref byte[] encoded, int maxLength, out string value)
        {
            int length = chars.Length - index;

            if (length < maxLength)
            {
                switch (GetEncodeMethod(options, chars, index, length))
                {
                case EncodeMethod.Quote:
                    value  = MimeUtils.Quote(new string (chars, index, length));
                    index += length;
                    return(false);

                case EncodeMethod.None:
                    value  = new string (chars, index, length);
                    index += length;
                    return(false);
                }
            }

            length = Math.Min(maxLength, length);
            int ratio, count, n;

            do
            {
                count = encoder.GetByteCount(chars, index, length, true);
                if (count > maxLength && length > 1)
                {
                    if ((ratio = (int)Math.Round((double)count / (double)length)) > 1)
                    {
                        length -= Math.Max((count - maxLength) / ratio, 1);
                    }
                    else
                    {
                        length--;
                    }
                    continue;
                }

                if (bytes.Length < count)
                {
                    Array.Resize <byte> (ref bytes, count);
                }

                count = encoder.GetBytes(chars, index, length, bytes, 0, true);

                // Note: the first chunk needs to be encoded in order to declare the charset
                if (index > 0 || charset == "us-ascii")
                {
                    var method = GetEncodeMethod(bytes, count);

                    if (method == EncodeMethod.Quote)
                    {
                        value  = MimeUtils.Quote(Encoding.ASCII.GetString(bytes, 0, count));
                        index += length;
                        return(false);
                    }

                    if (method == EncodeMethod.None)
                    {
                        value  = Encoding.ASCII.GetString(bytes, 0, count);
                        index += length;
                        return(false);
                    }
                }

                n = hex.EstimateOutputLength(count);
                if (encoded.Length < n)
                {
                    Array.Resize <byte> (ref encoded, n);
                }

                // only the first value gets a charset declaration
                int charsetLength = index == 0 ? charset.Length + 2 : 0;

                n = hex.Encode(bytes, 0, count, encoded);
                if (n > 3 && (charsetLength + n) > maxLength)
                {
                    int x = 0;

                    for (int i = n - 1; i >= 0 && charsetLength + i >= maxLength; i--)
                    {
                        if (encoded[i] == (byte)'%')
                        {
                            x--;
                        }
                        else
                        {
                            x++;
                        }
                    }

                    if ((ratio = (int)Math.Round((double)count / (double)length)) > 1)
                    {
                        length -= Math.Max(x / ratio, 1);
                    }
                    else
                    {
                        length--;
                    }
                    continue;
                }

                if (index == 0)
                {
                    value = charset + "''" + Encoding.ASCII.GetString(encoded, 0, n);
                }
                else
                {
                    value = Encoding.ASCII.GetString(encoded, 0, n);
                }
                index += length;
                return(true);
            } while (true);
        }
コード例 #4
0
ファイル: CharsetFilter.cs プロジェクト: dcga/MimeKit
		/// <summary>
		/// Initializes a new instance of the <see cref="MimeKit.IO.Filters.CharsetFilter"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="CharsetFilter"/> to convert text from the specified
		/// source encoding into the target charset encoding.
		/// </remarks>
		/// <param name="sourceEncoding">Source encoding.</param>
		/// <param name="targetEncoding">Target encoding.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <para><paramref name="sourceEncoding"/> is <c>null</c>.</para>
		/// <para>-or-</para>
		/// <para><paramref name="targetEncoding"/> is <c>null</c>.</para>
		/// </exception>
		public CharsetFilter (Encoding sourceEncoding, Encoding targetEncoding)
		{
			if (sourceEncoding == null)
				throw new ArgumentNullException ("sourceEncoding");

			if (targetEncoding == null)
				throw new ArgumentNullException ("targetEncoding");

			SourceEncoding = sourceEncoding;
			TargetEncoding = targetEncoding;

			decoder = (Decoder) SourceEncoding.GetDecoder ();
			encoder = (Encoder) TargetEncoding.GetEncoder ();
		}
コード例 #5
0
		static bool GetNextValue (string charset, Encoder encoder, HexEncoder hex, char[] chars, ref int index,
		                          ref byte[] bytes, ref byte[] encoded, int maxLength, out string value)
		{
			int length = chars.Length - index;

			if (length < maxLength) {
				switch (GetEncodeMethod (chars, index, length)) {
				case EncodeMethod.Quote:
					value = MimeUtils.Quote (new string (chars, index, length));
					index += length;
					return false;
				case EncodeMethod.None:
					value = new string (chars, index, length);
					index += length;
					return false;
				}
			}

			length = Math.Min (maxLength, length);
			int ratio, count, n;

			do {
				count = encoder.GetByteCount (chars, index, length, true);
				if (count > maxLength && length > 1) {
					ratio = (int) Math.Round ((double) count / (double) length);
					length -= Math.Max ((count - maxLength) / ratio, 1);
					continue;
				}

				if (bytes.Length < count)
					Array.Resize<byte> (ref bytes, count);

				count = encoder.GetBytes (chars, index, length, bytes, 0, true);

				// Note: the first chunk needs to be encoded in order to declare the charset
				if (index > 0 || charset == "us-ascii") {
					var method = GetEncodeMethod (bytes, count);

					if (method == EncodeMethod.Quote) {
						value = MimeUtils.Quote (Encoding.ASCII.GetString (bytes, 0, count));
						index += length;
						return false;
					}

					if (method == EncodeMethod.None) {
						value = Encoding.ASCII.GetString (bytes, 0, count);
						index += length;
						return false;
					}
				}

				n = hex.EstimateOutputLength (count);
				if (encoded.Length < n)
					Array.Resize<byte> (ref encoded, n);

				// only the first value gets a charset declaration
				int charsetLength = index == 0 ? charset.Length + 2 : 0;

				n = hex.Encode (bytes, 0, count, encoded);
				if (n > 3 && (charsetLength + n) > maxLength) {
					int x = 0;

					for (int i = n - 1; i >= 0 && charsetLength + i >= maxLength; i--) {
						if (encoded[i] == (byte) '%')
							x--;
						else
							x++;
					}

					ratio = (int) Math.Round ((double) count / (double) length);
					length -= Math.Max (x / ratio, 1);
					continue;
				}

				if (index == 0)
					value = charset + "''" + Encoding.ASCII.GetString (encoded, 0, n);
				else
					value = Encoding.ASCII.GetString (encoded, 0, n);
				index += length;
				return true;
			} while (true);
		}
コード例 #6
0
ファイル: Parameter.cs プロジェクト: nachocove/MimeKit
		static int Rfc2047EncodeNextChunk (StringBuilder str, string text, ref int index, Encoding encoding, string charset, Encoder encoder, int maxLength)
		{
			int byteCount = 0, charCount = 0, encodeCount = 0;
			var buffer = new char[2];
			int startIndex = index;
			int nchars, n;
			char c;

			while (index < text.Length) {
				c = text[index++];

				if (c < 127) {
					if (IsCtrl (c) || c == '"' || c == '\\')
						encodeCount++;

					byteCount++;
					charCount++;
					nchars = 1;
					n = 1;
				} else if (c < 256) {
					// iso-8859-1
					encodeCount++;
					byteCount++;
					charCount++;
					nchars = 1;
					n = 1;
				} else {
					if (char.IsSurrogatePair (text, index - 1)) {
						buffer[1] = text[index++];
						nchars = 2;
					} else {
						nchars = 1;
					}

					buffer[0] = c;

					try {
						n = encoder.GetByteCount (buffer, 0, nchars, true);
					} catch {
						n = 3;
					}

					charCount += nchars;
					encodeCount += n;
					byteCount += n;
				}

				if (ExceedsMaxWordLength (charset, byteCount, encodeCount, maxLength)) {
					// restore our previous state
					charCount -= nchars;
					index -= nchars;
					byteCount -= n;
					break;
				}
			}

			return Rfc2047.AppendEncodedWord (str, encoding, text, startIndex, charCount, QEncodeMode.Text);
		}
コード例 #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MimeKit.Text.TextConverter"/> class.
 /// </summary>
 /// <remarks>
 /// Initializes a new instance of the <see cref="MimeKit.Text.TextConverter"/> class.
 /// </remarks>
 protected TextConverter()
 {
     encoder = Encoding.UTF8.GetEncoder();
     decoder = Encoding.UTF8.GetDecoder();
 }
コード例 #8
0
		/// <summary>
		/// Initializes a new instance of the <see cref="MimeKit.Text.TextConverter"/> class.
		/// </summary>
		/// <remarks>
		/// Initializes a new instance of the <see cref="MimeKit.Text.TextConverter"/> class.
		/// </remarks>
		protected TextConverter ()
		{
			encoder = Encoding.UTF8.GetEncoder ();
			decoder = Encoding.UTF8.GetDecoder ();
		}