コード例 #1
0
ファイル: MimeUtils.cs プロジェクト: GCAE/OutlookParser
        /// <summary>
        /// Parses headers from message or mime entry.
        /// </summary>
        /// <param name="entryStrm">Stream from where to read headers.</param>
        /// <returns>Returns header lines.</returns>
        public static string ParseHeaders(Stream entryStrm)
        {
            /* Rfc 2822 3.1.  GENERAL DESCRIPTION
             *      A message consists of header fields and, optionally, a body.
             *      The  body  is simply a sequence of lines containing ASCII charac-
             *      ters.  It is separated from the headers by a null line  (i.e.,  a
             *      line with nothing preceding the CRLF).
             */

            byte[]           crlf      = new byte[] { (byte)'\r', (byte)'\n' };
            MemoryStream     msHeaders = new MemoryStream();
            StreamLineReader r         = new StreamLineReader(entryStrm);

            byte[] lineData = r.ReadLine();
            while (lineData != null)
            {
                if (lineData.Length == 0)
                {
                    break;
                }

                msHeaders.Write(lineData, 0, lineData.Length);
                msHeaders.Write(crlf, 0, crlf.Length);
                lineData = r.ReadLine();
            }

            return(System.Text.Encoding.Default.GetString(msHeaders.ToArray()));
        }
コード例 #2
0
        /// <summary>
        /// Does period handling.
        /// </summary>
        /// <param name="strm">Input stream.</param>
        /// <param name="add_Remove">If true add periods, else removes periods.</param>
        /// <param name="setStrmPosTo0">If true sets stream position to 0.</param>
        /// <returns></returns>
        public static MemoryStream DoPeriodHandling(Stream strm, bool add_Remove, bool setStrmPosTo0)
        {
            MemoryStream replyData = new MemoryStream();

            byte[] crlf = new byte[] { (byte)'\r', (byte)'\n' };

            if (setStrmPosTo0)
            {
                strm.Position = 0;
            }

            StreamLineReader r = new StreamLineReader(strm);

            byte[] line = r.ReadLine();

            // Loop through all lines
            while (line != null)
            {
                if (line.Length > 0)
                {
                    if (line[0] == (byte)'.')
                    {
                        /* Add period Rfc 2821 4.5.2
                         * -  Before sending a line of mail text, the SMTP client checks the
                         * first character of the line.  If it is a period, one additional
                         * period is inserted at the beginning of the line.
                         */
                        if (add_Remove)
                        {
                            replyData.WriteByte((byte)'.');
                            replyData.Write(line, 0, line.Length);
                        }

                        /* Remove period Rfc 2821 4.5.2
                         * If the first character is a period , the first characteris deleted.
                         */
                        else
                        {
                            replyData.Write(line, 1, line.Length - 1);
                        }
                    }
                    else
                    {
                        replyData.Write(line, 0, line.Length);
                    }
                }

                replyData.Write(crlf, 0, crlf.Length);

                // Read next line
                line = r.ReadLine();
            }

            replyData.Position = 0;

            return(replyData);
        }
コード例 #3
0
        /// <summary>
        /// Scans invalid CR or LF combination in stream. Returns true if contains invalid CR or LF combination.
        /// </summary>
        /// <param name="strm">Stream which to check.</param>
        /// <returns>Returns true if contains invalid CR or LF combination.</returns>
        public static bool ScanInvalid_CR_or_LF(Stream strm)
        {
            StreamLineReader lineReader = new StreamLineReader(strm);

            byte[] line = lineReader.ReadLine();
            while (line != null)
            {
                foreach (byte b in line)
                {
                    // Contains CR or LF. It cannot conatian such sumbols, because CR must be paired with LF
                    // and we currently reading lines with CRLF combination.
                    if (b == 10 || b == 13)
                    {
                        return(true);
                    }
                }

                line = lineReader.ReadLine();
            }

            return(false);
        }