Ejemplo n.º 1
0
        public void TestGetMimeCharset()
        {
            Encoding encoding;

            Assert.AreEqual("iso-8859-1", CharsetUtils.GetMimeCharset("latin1"));
            Assert.AreEqual("iso-8859-1", CharsetUtils.GetMimeCharset(CharsetUtils.Latin1));
            Assert.AreEqual("gibberish", CharsetUtils.GetMimeCharset("gibberish"));

            Assert.AreEqual("iso-2022-jp", CharsetUtils.GetMimeCharset(Encoding.GetEncoding(932)));
            Assert.AreEqual("iso-2022-jp", CharsetUtils.GetMimeCharset(Encoding.GetEncoding(50220)));
            Assert.AreEqual("iso-2022-jp", CharsetUtils.GetMimeCharset(Encoding.GetEncoding(50221)));

            try {
                encoding = Encoding.GetEncoding(50225);
            } catch (NotSupportedException) {
                encoding = null;
            }

            if (encoding != null)
            {
                Assert.AreEqual("euc-kr", CharsetUtils.GetMimeCharset(encoding));
            }

            Assert.AreEqual("euc-kr", CharsetUtils.GetMimeCharset(Encoding.GetEncoding(949)));
        }
Ejemplo n.º 2
0
		/// <summary>
		/// Sets the text content and the charset parameter in the Content-Type header.
		/// </summary>
		/// <remarks>
		/// This method is similar to setting the <see cref="TextPart.Text"/> property,
		/// but allows specifying a charset encoding to use. Also updates the
		/// <see cref="ContentType.Charset"/> property.
		/// </remarks>
		/// <param name="encoding">The charset encoding.</param>
		/// <param name="text">The text content.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <para><paramref name="encoding"/> is <c>null</c>.</para>
		/// <para>-or-</para>
		/// <para><paramref name="text"/> is <c>null</c>.</para>
		/// </exception>
		public void SetText (Encoding encoding, string text)
		{
			if (encoding == null)
				throw new ArgumentNullException (nameof (encoding));

			if (text == null)
				throw new ArgumentNullException (nameof (text));

			ContentType.Parameters["charset"] = CharsetUtils.GetMimeCharset (encoding);
			var content = new MemoryStream (encoding.GetBytes (text));
			Content = new MimeContent (content);
		}
Ejemplo n.º 3
0
        /// <summary>
        /// Sets the text content and the charset parameter in the Content-Type header.
        /// </summary>
        /// <param name="charset">The charset encoding.</param>
        /// <param name="text">The text content.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="charset"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="text"/> is <c>null</c>.</para>
        /// </exception>
        public void SetText(Encoding charset, string text)
        {
            if (charset == null)
            {
                throw new ArgumentNullException("charset");
            }

            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            var content = new MemoryStream(charset.GetBytes(text));

            ContentObject = new ContentObject(content, ContentEncoding.Default);
            ContentType.Parameters["charset"] = CharsetUtils.GetMimeCharset(charset);
        }
Ejemplo n.º 4
0
        void EncodeRfc2047(FormatOptions options, StringBuilder builder, ref int lineLength, Encoding headerEncoding)
        {
            var bestEncoding = options.International ? CharsetUtils.UTF8 : GetBestEncoding(Value, encoding ?? headerEncoding);
            var charset      = CharsetUtils.GetMimeCharset(bestEncoding);
            var encoder      = (Encoder)bestEncoding.GetEncoder();
            int index        = 0;
            int length;

            builder.Append(';');
            lineLength++;

            // account for: <SPACE> + <NAME> + "=\"=?<CHARSET>?b?<10 chars>?=\""
            if (lineLength + Name.Length + charset.Length + 10 + Math.Min(Value.Length, 10) >= options.MaxLineLength)
            {
                builder.Append(options.NewLine);
                builder.Append('\t');
                lineLength = 1;
            }
            else
            {
                builder.Append(' ');
                lineLength++;
            }

            builder.AppendFormat("{0}=\"", Name);
            lineLength += Name.Length + 2;

            do
            {
                length      = Rfc2047EncodeNextChunk(builder, Value, ref index, bestEncoding, charset, encoder, (options.MaxLineLength - lineLength) - 1);
                lineLength += length;

                if (index >= Value.Length)
                {
                    break;
                }

                builder.Append(options.NewLine);
                builder.Append('\t');
                lineLength = 1;
            } while (true);

            builder.Append('\"');
            lineLength++;
        }
Ejemplo n.º 5
0
        public void TestArgumentExceptions()
        {
            var buffer = new byte[10];

            Assert.Throws <ArgumentNullException> (() => CharsetUtils.ConvertToUnicode((ParserOptions)null, buffer, 0, buffer.Length));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.ConvertToUnicode(ParserOptions.Default, null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.ConvertToUnicode(ParserOptions.Default, buffer, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.ConvertToUnicode(ParserOptions.Default, buffer, 0, -1));

            Assert.Throws <ArgumentNullException> (() => CharsetUtils.ConvertToUnicode((Encoding)null, buffer, 0, buffer.Length));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.ConvertToUnicode(Encoding.UTF8, null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.ConvertToUnicode(Encoding.UTF8, buffer, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.ConvertToUnicode(Encoding.UTF8, buffer, 0, -1));

            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetCodePage(null));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetEncoding(null));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetEncoding(null, "fallback"));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetEncoding("charset", null));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.GetEncoding(-1, "fallback"));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetEncoding(28591, null));

            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetMimeCharset((string)null));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetMimeCharset((Encoding)null));
        }
Ejemplo n.º 6
0
        internal void Encode(FormatOptions options, StringBuilder builder, ref int lineLength, Encoding encoding)
        {
            string quoted;

            var method = GetEncodeMethod(options, Name, Value, out quoted);

            if (method == EncodeMethod.None)
            {
                quoted = Value;
            }

            if (method != EncodeMethod.Rfc2184)
            {
                if (lineLength + 2 + Name.Length + 1 + quoted.Length >= options.MaxLineLength)
                {
                    builder.Append(";\n\t");
                    lineLength = 1;
                }
                else
                {
                    builder.Append("; ");
                    lineLength += 2;
                }

                lineLength += Name.Length + 1 + quoted.Length;
                builder.Append(Name);
                builder.Append('=');
                builder.Append(quoted);
                return;
            }

            int    maxLength = options.MaxLineLength - (Name.Length + 6);
            var    bestEncoding = GetBestEncoding(Value, encoding);
            var    charset = CharsetUtils.GetMimeCharset(bestEncoding);
            var    bytes = new byte[Math.Max(maxLength, 6)];
            var    hexbuf = new byte[bytes.Length * 3 + 3];
            var    encoder = bestEncoding.GetEncoder();
            var    chars = Value.ToCharArray();
            var    hex = new HexEncoder();
            int    index = 0, i = 0;
            string value, id;
            bool   encoded;
            int    length;

            do
            {
                encoded = GetNextValue(charset, encoder, hex, chars, ref index, ref bytes, ref hexbuf, maxLength, out value);
                length  = Name.Length + (encoded ? 1 : 0) + 1 + value.Length;

                if (i == 0 && index == chars.Length)
                {
                    if (lineLength + 2 + length >= options.MaxLineLength)
                    {
                        builder.Append(";\n\t");
                        lineLength = 1;
                    }
                    else
                    {
                        builder.Append("; ");
                        lineLength += 2;
                    }

                    builder.Append(Name);
                    if (encoded)
                    {
                        builder.Append('*');
                    }
                    builder.Append('=');
                    builder.Append(value);
                    lineLength += length;
                    return;
                }

                builder.Append(";\n\t");
                lineLength = 1;

                id      = i.ToString();
                length += id.Length + 1;

                builder.Append(Name);
                builder.Append('*');
                builder.Append(id);
                if (encoded)
                {
                    builder.Append('*');
                }
                builder.Append('=');
                builder.Append(value);
                lineLength += length;
                i++;
            } while (index < chars.Length);
        }
Ejemplo n.º 7
0
        void EncodeRfc2231(FormatOptions options, StringBuilder builder, ref int lineLength, Encoding headerEncoding)
        {
            var    bestEncoding = options.International ? CharsetUtils.UTF8 : GetBestEncoding(Value, encoding ?? headerEncoding);
            int    maxLength = options.MaxLineLength - (Name.Length + 6);
            var    charset = CharsetUtils.GetMimeCharset(bestEncoding);
            var    encoder = (Encoder)bestEncoding.GetEncoder();
            var    bytes = new byte[Math.Max(maxLength, 6)];
            var    hexbuf = new byte[bytes.Length * 3 + 3];
            var    chars = Value.ToCharArray();
            var    hex = new HexEncoder();
            int    index = 0, i = 0;
            string value, id;
            bool   encoded;
            int    length;

            do
            {
                builder.Append(';');
                lineLength++;

                encoded = Rfc2231GetNextValue(options, charset, encoder, hex, chars, ref index, ref bytes, ref hexbuf, maxLength, out value);
                length  = Name.Length + (encoded ? 1 : 0) + 1 + value.Length;

                if (i == 0 && index == chars.Length)
                {
                    if (lineLength + 1 + length >= options.MaxLineLength)
                    {
                        builder.Append(options.NewLine);
                        builder.Append('\t');
                        lineLength = 1;
                    }
                    else
                    {
                        builder.Append(' ');
                        lineLength++;
                    }

                    builder.Append(Name);
                    if (encoded)
                    {
                        builder.Append('*');
                    }
                    builder.Append('=');
                    builder.Append(value);
                    lineLength += length;
                    return;
                }

                builder.Append(options.NewLine);
                builder.Append('\t');
                lineLength = 1;

                id      = i.ToString();
                length += id.Length + 1;

                builder.Append(Name);
                builder.Append('*');
                builder.Append(id);
                if (encoded)
                {
                    builder.Append('*');
                }
                builder.Append('=');
                builder.Append(value);
                lineLength += length;
                i++;
            } while (index < chars.Length);
        }