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); } }
static StringSegment DecodeBody(Message message) { StringSegment innerMessageText = message.Body.SourceText; TransferEncoding encoding = message.GetTransferEncoding(); switch (encoding) { default: throw new MimeException(MimeError.TransferEncodingNotSupported); case TransferEncoding.SevenBit: break; // Nothing to do case TransferEncoding.QuotedPrintable: string decodedText = QuotedPrintableDecoder.Decode(innerMessageText); innerMessageText = new StringSegment(decodedText); break; case TransferEncoding.Base64: byte[] bytes = Convert.FromBase64String(innerMessageText.ToString()); string textFromBytes = Encoding.ASCII.GetString(bytes); innerMessageText = new StringSegment(textFromBytes); break; } return(innerMessageText); }
static string decode(string str) { var inBytes = TextEncoding.UTF8.GetBytes(str); var encoder = new QuotedPrintableDecoder(inBytes.ToInStream()); var encoded = encoder.ToArray(); return TextEncoding.UTF8.GetString(encoded); }
public void TestRandom(int textLength, double encodeProbability, double lowerCaseProbability) { TestString testString = this.MakeRandom(textLength, encodeProbability, lowerCaseProbability); string decodedString = null; Assert.DoesNotThrow(() => decodedString = new QuotedPrintableDecoder(testString.Encoded).GetString()); Assert.True(testString.Expected.Length == decodedString.Length); Assert.True(testString.Expected.Equals(decodedString)); }
public void TestFiles(string fileName) { string text = LoadFile(fileName); QuotedPrintableDecoder decoder = new QuotedPrintableDecoder(text); char[] chars = null; Assert.DoesNotThrow(() => chars = decoder.GetChars().ToArray()); string decodedString = null; Assert.DoesNotThrow(() => decodedString = new string(chars)); }
public void TestStandard(string text, string expectedText) { QuotedPrintableDecoder decoder = new QuotedPrintableDecoder(text); char[] chars = null; Assert.DoesNotThrow(() => chars = decoder.GetChars().ToArray()); Assert.True(chars.Length == expectedText.Length); string decodedString = null; Assert.DoesNotThrow(() => decodedString = new string(chars)); Assert.True(string.Equals(decodedString, expectedText)); }
public static Byte[] DecodeQuoted(String data) { Byte[] buffer = Encoding.ASCII.GetBytes(data); QuotedPrintableDecoder decoder = new QuotedPrintableDecoder(); Byte[] output = new Byte[decoder.EstimateOutputLength(buffer.Length)]; Int32 length = decoder.Decode(buffer, 0, buffer.Length, output); MemoryStream stream = new MemoryStream(output, 0, length); return(stream.ToArray()); }
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); }
public void TestEmoji() { var expected = "<html><body>😱<br/></body></html>"; var buffer = Encoding.ASCII.GetBytes("=F0=9F=98=B1"); var decoder = new QuotedPrintableDecoder(); var length = decoder.EstimateOutputLength(buffer.Length); var decoded = new byte[length]; var n = decoder.Decode(buffer, 0, buffer.Length, decoded); var emoji = Encoding.UTF8.GetString(decoded, 0, n); var converter = new TextToHtml(); var result = converter.Convert(emoji); Assert.AreEqual(expected, result); }
public void TestAllChars(bool lowerCase) { // Quoted printable all possible chars TestString testString = this.MakeAllEncodeChars(lowerCase); string decoded = null; Assert.DoesNotThrow(() => decoded = new QuotedPrintableDecoder(testString.Encoded).GetString()); Assert.True(testString.Expected.Length == decoded.Length); for (byte i = 0; i < decoded.Length; ++i) { Assert.True(testString.Expected[i] == decoded[i]); } }
public void TestQuotedPrintableDecodeInvalidSoftBreak() { const string input = "This is an invalid=\rsoft break."; const string expected = "This is an invalid=\rsoft break."; var decoder = new QuotedPrintableDecoder(); var output = new byte[1024]; 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.ASCII.GetString(output, 0, n); Assert.AreEqual(expected, actual); }
protected byte[] DecodeQuotedPrintable(string input) { try { QuotedPrintableDecoder encoder = new QuotedPrintableDecoder(false); byte[] inputBytes = Encoding.UTF8.GetBytes(input); byte[] outputBytes = new byte[encoder.EstimateOutputLength(inputBytes.Length)]; int length = encoder.Decode(inputBytes, 0, inputBytes.Length, outputBytes); return(outputBytes.Take(length).ToArray()); } catch { return(null); } }
public void TestQuotedPrintableDecodePatterns() { var decoder = new QuotedPrintableDecoder(); var encoding = CharsetUtils.Latin1; var output = new byte[4096]; string actual; byte[] buf; int n; 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); } }
public void TestRoundTrip(int toCount, int ccCount, int bodyLength) { // Manually wrap the mail... MailMessage mail = MailMessageGenerator.GenerateRandomMail(toCount, ccCount, bodyLength); MailMessage wrappedMessage = MailMessageGenerator.WrappedMailMessage(mail); string wrappedMessageText = wrappedMessage.Serialize(); Message parsedMessage = null; Assert.DoesNotThrow(() => parsedMessage = Message.Load(wrappedMessageText)); Message extracted = null; Assert.DoesNotThrow(() => extracted = WrappedMessage.ExtractInner(parsedMessage)); string extractedBody = extracted.Body.Text; if (extracted.GetTransferEncoding() == TransferEncoding.QuotedPrintable) { extractedBody = QuotedPrintableDecoder.Decode(new StringSegment(extractedBody)); extractedBody = extractedBody.TrimEnd(); } Assert.True(extractedBody == mail.Body); }
public void TestQuotedPrintableEncodeFlush() { const string input = "This line ends with a space "; const string expected = "This line ends with a space=20=\n"; var encoder = new QuotedPrintableEncoder(); var decoder = new QuotedPrintableDecoder(); var output = new byte[1024]; string actual; byte[] buf; int n; Assert.AreEqual(ContentEncoding.QuotedPrintable, encoder.Encoding); buf = Encoding.ASCII.GetBytes(input); n = encoder.Flush(buf, 0, buf.Length, output); actual = Encoding.ASCII.GetString(output, 0, n); Assert.AreEqual(expected, actual); buf = Encoding.ASCII.GetBytes(expected); n = decoder.Decode(buf, 0, buf.Length, output); actual = Encoding.ASCII.GetString(output, 0, n); Assert.AreEqual(input, actual); }
public void Setup() { m_Decoder = new QuotedPrintableDecoder(); }
public void Setup() { m_Decoder = new QuotedPrintableDecoder(); }
public void TestFailures(string badString) { string decoded = null; Assert.Throws <MimeException>(() => decoded = new QuotedPrintableDecoder(badString).GetString()); }