// ----------------------- MaterializeEscapeChar --------------------- // used by the Dequote method. unpacks the standard escape sequences used in // quoted strings. // returns an int/char pair holding the length of the escape sequence and the // materialized character value. private static IntCharPair MaterializeEscapeChar(string InString, int InIx) { char nx = AcCommon.PullCharArray(InString, InIx, 2)[1]; if (nx == 't') { return(new IntCharPair(2, '\t')); } else if (nx == 'r') { return(new IntCharPair(2, '\r')); } else if (nx == 'n') { return(new IntCharPair(2, '\n')); } else if (nx == '\'') { return(new IntCharPair(2, '\'')); } else if (nx == '\\') { return(new IntCharPair(2, '\\')); } else if (nx == '"') { return(new IntCharPair(2, '"')); } else if (nx == '0') { return(new IntCharPair(2, '\0')); } else { throw(new ApplicationException("Unexpected escape sequence starting at " + "position " + InIx + " in string: " + InString)); } }
// -------------------------- CalcEncodedByteBounds ------------------------ // An encoded byte can either be in form "=xx" or a single literal byte character. // The bounds of the encoded byte are either the single char, or run from the // "=" character to the 2nd octet external form character. // This method is passed and encoded string and a position in that string. It returns, // by reference, the bounds of the encoded byte at that position. private static void CalcEncodedByteBounds( ref int OutBx, ref int OutEx, string InEncodedChars, int InIx) { // isolate chars prior to InIx. char[] chars = AcCommon.PullCharArray(InEncodedChars, InIx - 2, 3); // find the begin position of the encoded byte. if (chars[2] == '=') { OutBx = InIx; } else if (chars[1] == '=') { OutBx = InIx - 1; } else if (chars[0] == '=') { OutBx = InIx - 2; } else { OutBx = InIx; } // calc the end position of the encoded byte. if (InEncodedChars[OutBx] == '=') { OutEx = OutBx + 2; } else { OutEx = OutBx; } }
// ------------------------ 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( )); }