Пример #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 IList<Word> GetRfc822Words (FormatOptions options, Encoding charset, string text, bool phrase)
		{
			var encoder = charset.GetEncoder ();
			var words = new List<Word> ();
			var chars = new char[2];
			var saved = new Word ();
			var word = new Word ();
			int nchars, n, i = 0;
			char c;

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

				if (c < 256 && IsBlank (c)) {
					if (word.ByteCount > 0) {
						words.Add (word);
						word = new Word ();
					}

					word.StartIndex = i;
				} else {
					// save state in case adding this character exceeds the max line length
					word.CopyTo (saved);

					if (c < 127) {
						if (IsCtrl (c)) {
							word.Encoding = options.AllowMixedHeaderCharsets ? Math.Max (word.Encoding, 1) : 2;
							word.Type = WordType.EncodedWord;
							word.EncodeCount++;
						} else if (phrase && !IsAtom (c)) {
							// phrases can have quoted strings
							if (word.Type == WordType.Atom)
								word.Type = WordType.QuotedString;
						}

						if (c == '"' || c == '\\')
							word.QuotedPairs++;

						word.ByteCount++;
						word.CharCount++;
						nchars = 1;
					} else if (c < 256) {
						// iso-8859-1
						word.Encoding = options.AllowMixedHeaderCharsets ? Math.Max (word.Encoding, 1) : 2;
						word.Type = WordType.EncodedWord;
						word.EncodeCount++;
						word.ByteCount++;
						word.CharCount++;
						nchars = 1;
					} else {
						if (char.IsSurrogatePair (text, i - 1)) {
							chars[1] = text[i++];
							nchars = 2;
						} else {
							nchars = 1;
						}

						chars[0] = c;

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

						word.Type = WordType.EncodedWord;
						word.CharCount += nchars;
						word.EncodeCount += n;
						word.ByteCount += n;
						word.Encoding = 2;
					}

					if (ExceedsMaxLineLength (options, charset, word)) {
						// restore our previous state
						saved.CopyTo (word);
						i -= nchars;

						// Note: if the word is longer than what we can fit on
						// one line, then we need to encode it.
						if (word.Type == WordType.Atom) {
							word.Type = WordType.EncodedWord;

							// in order to fit this long atom under MaxLineLength, we need to
							// account for the added length of =?us-ascii?q?...?=
							n = "us-ascii".Length + 7;
							word.CharCount -= n;
							word.ByteCount -= n;
							i -= n;
						}

						words.Add (word);

						saved.Type = word.Type;
						word = new Word ();

						// Note: the word-type needs to be preserved when breaking long words.
						word.Type = saved.Type;
						word.StartIndex = i;
					}
				}
			}

			if (word.ByteCount > 0)
				words.Add (word);

			return words;
		}
Пример #3
0
		static byte[] CharsetConvert (Encoding charset, char[] word, int length, out int converted)
		{
			var encoder = charset.GetEncoder ();
			int count = encoder.GetByteCount (word, 0, length, true);
			var encoded = new byte[count];

			converted = encoder.GetBytes (word, 0, length, encoded, 0, true);

			return encoded;
		}
Пример #4
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 ("sourceEncoding");

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

			SourceEncoding = sourceEncoding;
			TargetEncoding = targetEncoding;

			decoder = (Decoder) SourceEncoding.GetDecoder ();
			encoder = (Encoder) TargetEncoding.GetEncoder ();
		}