/// <summary>
        /// Clone the <see cref="QuotedPrintableDecoder"/> with its current state.
        /// </summary>
        /// <remarks>
        /// Creates a new <see cref="QuotedPrintableDecoder"/> with exactly the same state as the current decoder.
        /// </remarks>
        /// <returns>A new <see cref="QuotedPrintableDecoder"/> with identical state.</returns>
        public IMimeDecoder Clone()
        {
            var decoder = new QuotedPrintableDecoder(rfc2047);

            decoder.state = state;
            decoder.saved = saved;

            return(decoder);
        }
Пример #2
0
		/// <summary>
		/// Clone the <see cref="QuotedPrintableDecoder"/> with its current state.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="QuotedPrintableDecoder"/> with exactly the same state as the current decoder.
		/// </remarks>
		/// <returns>A new <see cref="QuotedPrintableDecoder"/> with identical state.</returns>
		public IMimeDecoder Clone ()
		{
			var decoder = new QuotedPrintableDecoder (rfc2047);

			decoder.state = state;
			decoder.saved = saved;

			return decoder;
		}
Пример #3
0
		static unsafe string DecodeTokens (ParserOptions options, IList<Token> tokens, byte[] input, byte* inbuf, int length)
		{
			var decoded = new StringBuilder (length);
			var qp = new QuotedPrintableDecoder (true);
			var base64 = new Base64Decoder ();
			var output = new byte[length];
			Token token;
			int len;

			fixed (byte* outbuf = output) {
				for (int i = 0; i < tokens.Count; i++) {
					token = tokens[i];

					if (token.Encoding != ContentEncoding.Default) {
						// In order to work around broken mailers, we need to combine the raw
						// decoded content of runs of identically encoded word tokens before
						// converting to unicode strings.
						ContentEncoding encoding = token.Encoding;
						int codepage = token.CodePage;
						IMimeDecoder decoder;
						int outlen, n;
						byte* outptr;

						// find the end of this run (and measure the buffer length we'll need)
						for (n = i + 1; n < tokens.Count; n++) {
							if (tokens[n].Encoding != encoding || tokens[n].CodePage != codepage)
								break;
						}

						// base64 / quoted-printable decode each of the tokens...
						if (encoding == ContentEncoding.Base64)
							decoder = base64;
						else
							decoder = qp;

						outptr = outbuf;
						outlen = 0;

						do {
							// Note: by not resetting the decoder state each loop, we effectively
							// treat the payloads as one continuous block, thus allowing us to
							// handle cases where a hex-encoded triplet of a quoted-printable
							// encoded payload is split between 2 or more encoded-word tokens.
							len = DecodeToken (tokens[i], decoder, inbuf, outptr);
							outptr += len;
							outlen += len;
							i++;
						} while (i < n);

						decoder.Reset ();
						i--;

						var unicode = CharsetUtils.ConvertToUnicode (options, codepage, output, 0, outlen, out len);
						decoded.Append (unicode, 0, len);
					} else if (token.Is8bit) {
						// *sigh* I hate broken mailers...
						var unicode = CharsetUtils.ConvertToUnicode (options, input, token.StartIndex, token.Length, out len);
						decoded.Append (unicode, 0, len);
					} else {
						// pure 7bit ascii, a breath of fresh air...
						byte* inptr = inbuf + token.StartIndex;
						byte* inend = inptr + token.Length;

						while (inptr < inend)
							decoded.Append ((char) *inptr++);
					}
				}
			}

			return decoded.ToString ();
		}
Пример #4
0
		public void TestQuotedPrintableDecode ()
		{
			const string input = "This is an ordinary text message in which my name (=ED=E5=EC=F9 =EF=E1 =E9=EC=E8=F4=F0)\nis in Hebrew (=FA=E9=F8=E1=F2).";
			const string expected = "This is an ordinary text message in which my name (םולש ןב ילטפנ)\nis in Hebrew (תירבע).";
			var encoding = Encoding.GetEncoding ("iso-8859-8");
			var decoder = new QuotedPrintableDecoder ();
			var output = new byte[4096];
			string actual;
			byte[] buf;
			int n;

			Assert.AreEqual (ContentEncoding.QuotedPrintable, decoder.Encoding);

			buf = Encoding.ASCII.GetBytes (input);
			n = decoder.Decode (buf, 0, buf.Length, output);
			actual = encoding.GetString (output, 0, n);
			Assert.AreEqual (expected, actual);

			encoding = CharsetUtils.Latin1;

			for (int i = 0; i < qpEncodedPatterns.Length; i++) {
				decoder.Reset ();
				buf = encoding.GetBytes (qpEncodedPatterns[i]);
				n = decoder.Decode (buf, 0, buf.Length, output);
				actual = encoding.GetString (output, 0, n);
				Assert.AreEqual (qpDecodedPatterns[i], actual, "Failed to decode qpEncodedPatterns[{0}]", i);
			}
		}
Пример #5
0
		public void TestQuotedPrintableDecode ()
		{
			const string input = "This is an ordinary text message in which my name (=ED=E5=EC=F9 =EF=E1 =E9=EC=E8=F4=F0)\nis in Hebrew (=FA=E9=F8=E1=F2).";
			const string expected = "This is an ordinary text message in which my name (םולש ןב ילטפנ)\nis in Hebrew (תירבע).";
			var encoding = Encoding.GetEncoding ("iso-8859-8");
			var decoder = new QuotedPrintableDecoder ();
			var output = new byte[4096];

			var buf = Encoding.ASCII.GetBytes (input);
			int n = decoder.Decode (buf, 0, buf.Length, output);
			var actual = encoding.GetString (output, 0, n);

			Assert.AreEqual (expected, actual);
		}