// --------------------- CalcQuotedPrintableTraits ------------------------ // Setup an object containing the traits for the Quoted-Printable encoding of // contents of the MimeHeaderLine. public static QuotedPrintableTraits CalcQuotedPrintableTraits( MimeHeaderLineTraits InMimeLineTraits) { QuotedPrintableTraits qpTraits = new QuotedPrintableTraits( ); qpTraits.SetCharSet(InMimeLineTraits.EncoderCharSet); return(qpTraits); }
public static string Encode(string InValue, MimeHeaderLineTraits InHdrTraits) { QuotedPrintableTraits qpTraits = CalcQuotedPrintableTraits(InHdrTraits); string ec = QuotedPrintable.Encode(InValue, qpTraits); string results = "=?" + InHdrTraits.EncoderCharSet + "?Q?" + ec + "?="; return(results); }
// ------------------------- Encode ----------------------------------- // Quoted-Printable encode a string. public static string Encode(string InValue, QuotedPrintableTraits InTraits) { // first pass. encode one char at a time, without regard to 76 char line // limit. string im = EncodeChars(InValue, InTraits); // split into max 76 char lines. string result = SplitEncodedResultsIntoLines(im); return(result); }
// ----------------------- EncodeChar ---------------------------------- // Quoted-Printable encode a single unicode character. // ( depending on the charset, the unicode char could be encoded as more than // one byte. ) public static string EncodeChar(QuotedPrintableTraits InTraits, char InChar) { byte[] bytes = new byte[10]; StringBuilder sb = new StringBuilder(10); int ByteCx = InTraits.Encoder.GetBytes("" + InChar, 0, 1, bytes, 0); for (int Ix = 0; Ix < ByteCx; ++Ix) { sb.Append(EncodeByte(bytes[Ix])); } return(sb.ToString( )); }
// --------------------- CalcQuotedPrintableTraits ------------------------ // Setup an object containing the traits for the Quoted-Printable encoding of // contents of the MimeHeaderLine. private static QuotedPrintableTraits CalcQuotedPrintableTraits( MimeHeaderLineTraits InTraits) { string otherEncodeChars = " \t\n\r?"; QuotedPrintableTraits qpTraits = new QuotedPrintableTraits( ); qpTraits.SetEncoder(InTraits.Encoder); if (InTraits.OtherEncodeChars != null) { otherEncodeChars = otherEncodeChars + InTraits.OtherEncodeChars; } qpTraits.SetOtherEncodeChars(otherEncodeChars); return(qpTraits); }
// -------------------------- EncodeAsRequired ------------------------------ // encode the string if "RequiresEncoding". Otherwise, return the input string // as is. public static string EncodeAsRequired( string InValue, QuotedPrintableTraits InTraits) { if (QuotedPrintable.RequiresEncoding(InValue, InTraits) == true) { StringBuilder sb = new StringBuilder(InValue.Length * 2); sb.Append("=?" + InTraits.CharSet + "?Q?"); sb.Append(QuotedPrintable.Encode(InValue, InTraits)); sb.Append("?="); return(sb.ToString( )); } else { return(InValue); } }
// ------------------------ RequiresEncoding ------------------------- // evaluate if the string requires encoding according to the QuotedPrintableTraits. public static bool RequiresEncoding(string InValue, QuotedPrintableTraits InTraits) { bool requiresEncoding = false; char[] newLineChars = Environment.NewLine.ToCharArray( ); // encode always. if (InTraits.EncodeAlways == true) { requiresEncoding = true; } // length of string exceeds the "RequiresEncodingTriggerLength" else if ((InTraits.RequiresEncodingTriggerLength != -1) && (InValue.Length > InTraits.RequiresEncodingTriggerLength)) { requiresEncoding = true; } // loop for each character in the string. Test each to determine if the // string requires Quoted-Printable encoding. for (int Ix = 0; Ix < InValue.Length; ++Ix) { if (requiresEncoding == true) { break; } char ch1 = InValue[Ix]; // one of the "other" chars to encode. if ((InTraits.OtherEncodeChars != null) && (InTraits.OtherEncodeChars.IndexOf(ch1) != -1)) { requiresEncoding = true; } // space or tab. encoding depends on if followed by crlf or not. else if ((ch1 == 9) || (ch1 == 32)) { char[] nxChars = AcCommon.PullCharArray(InValue, Ix + 1, newLineChars.Length); if ((InTraits.LinebreakTreatment == LinebreakTreatment.Break) && (AcCommon.CompareEqual(nxChars, newLineChars) == true)) { requiresEncoding = true; } } // LineBreak sequence handled as a line break. else if ((ch1 == newLineChars[0]) && (AcCommon.CompareEqual(newLineChars, AcCommon.PullCharArray(InValue, Ix, newLineChars.Length)) == true)) { if (InTraits.LinebreakTreatment == LinebreakTreatment.Encode) { requiresEncoding = true; } } // a basic ascii char. literal representation. else if (((ch1 >= 33) && (ch1 <= 60)) || ((ch1 >= 62) && (ch1 <= 126))) { } // an equal sign. by itself, does not trigger QP encoding. else if (ch1 == '=') { } // an encode required character. else { requiresEncoding = true; } } return(requiresEncoding); }
// ------------------------- EncodeChars ----------------------------------- // Quoted-Printable encode the chars of a string without regard for the line // length maximum. private static string EncodeChars( string InValue, QuotedPrintableTraits InTraits) { char[] newLineChars = Environment.NewLine.ToCharArray( ); StringBuilder sb = new StringBuilder(InValue.Length * 2); // first pass. encode one char at a time, without regard to 76 char line // limit. for (int Ix = 0; Ix < InValue.Length; ++Ix) { char ch1 = InValue[Ix]; // one of the "other" chars to encode. if ((InTraits.OtherEncodeChars != null) && (InTraits.OtherEncodeChars.IndexOf(ch1) != -1)) { sb.Append(EncodeChar(InTraits, ch1)); } // space or tab. encoding depends on if followed by crlf or not. else if ((ch1 == 9) || (ch1 == 32)) { char[] nxChars = AcCommon.PullCharArray(InValue, Ix + 1, newLineChars.Length); if ((InTraits.LinebreakTreatment == LinebreakTreatment.Break) && (AcCommon.CompareEqual(nxChars, newLineChars) == true)) { sb.Append(EncodeChar(InTraits, ch1)); } else { sb.Append(ch1); } } // Linebreak sequence handled as a line break. else if ((ch1 == newLineChars[0]) && (InTraits.LinebreakTreatment == LinebreakTreatment.Break) && (AcCommon.CompareEqual( newLineChars, AcCommon.PullCharArray(InValue, Ix, newLineChars.Length)) == true)) { sb.Append("\r\n"); } // a basic ascii char. literal representation. else if (((ch1 >= 33) && (ch1 <= 60)) || ((ch1 >= 62) && (ch1 <= 126))) { sb.Append(ch1); } else { sb.Append(EncodeChar(InTraits, ch1)); } } return(sb.ToString( )); }