예제 #1
0
        // ------------------------ 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);
        }
예제 #2
0
        // ------------------------- 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( ));
        }