Exemplo n.º 1
0
        public Byte[] ReadBody(Byte[] boundary, out CheckBoundaryResult result, out Boolean isEndOfBody)
        {
            result = CheckBoundaryResult.None;
            isEndOfBody = false;

            if (this._Current == this._End)
            {
                isEndOfBody = true;
                return _EmptyBytes;
            }

            Int32 boundaryLength = -1;
            if (boundary != null)
            {
                boundaryLength = boundary.Length;
            }

            this._Start = this._Current;
            var line_Start = this._Current;

            UInt32 bbbbXor = 0;

            while (true)
            {
                UInt32* bbbb = (UInt32*)this._Current;
                do
                {
                    bbbbXor = *bbbb++ ^ 0x0A0A0A0A;
                } while (((bbbbXor - 0x01010101) & ~bbbbXor & 0x80808080) == 0);
                this._Current = (byte*)(bbbb - 1);
                // 10 is \n
                while (*this._Current != 10) { this._Current++; }

                if (*line_Start == 46)// .
                {
                    var lastOfLine = this._Current - 2;
                    if (line_Start == lastOfLine)
                    {
                        isEndOfBody = true;
                        //Remove last period from bodydata
                        return MimeParser.CreateNewBytes(new IntPtr(this._Start), lastOfLine - this._Start);
                    }
                }
                // Check this line is started by '-' (Boundary).
                //That avoid method call of CheckBoundary and improve performance.
                if (*line_Start == 45 && boundaryLength > -1)
                {
                    var lastOfLine = this._Current - 2;
                    var length = lastOfLine - line_Start + 1;
                    if (length == boundaryLength || length == boundaryLength + 2)
                    {
                        result = CheckBoundary(line_Start, lastOfLine + 1, boundary);
                        if (result != CheckBoundaryResult.None)
                        {
                            if (result == CheckBoundaryResult.Boundary)
                            {
                                //Back to start of line.To read boundary line on MimeParser.ReadMimeContent method.
                                this._Current = line_Start;
                            }
                            return MimeParser.CreateNewBytes(new IntPtr(this._Start), line_Start - this._Start);
                        }
                    }
                }
                if (this._Current == this._End)
                {
                    //Contains only 1 line and end of buffer
                    if (line_Start == this._Start)
                    {
                        this._Current = line_Start;
                        _IsLastOfLine = true;
                        _LastLine = MimeParser.CreateNewBytes(new IntPtr(this._Start), this._End - this._Start);
                        return _EmptyBytes;
                    }
                    return MimeParser.CreateNewBytes(new IntPtr(this._Start), this._Current - this._Start);
                }
                this._Current++;
                line_Start = this._Current;
            }
        }
Exemplo n.º 2
0
        public Byte[] ReadBody(Byte[] boundary, out CheckBoundaryResult result, out Boolean isEndOfBody)
        {
            result      = CheckBoundaryResult.None;
            isEndOfBody = false;

            if (this._Current == this._End)
            {
                isEndOfBody = true;
                return(_EmptyBytes);
            }

            Int32 boundaryLength = -1;

            if (boundary != null)
            {
                boundaryLength = boundary.Length;
            }

            this._Start = this._Current;
            var line_Start = this._Current;

            UInt32 bbbbXor = 0;

            while (true)
            {
                UInt32 *bbbb = (UInt32 *)this._Current;
                do
                {
                    bbbbXor = *bbbb++ ^ 0x0A0A0A0A;
                } while (((bbbbXor - 0x01010101) & ~bbbbXor & 0x80808080) == 0);
                this._Current = (byte *)(bbbb - 1);
                // 10 is \n
                while (*this._Current != 10)
                {
                    this._Current++;
                }

                if (*line_Start == 46)// .
                {
                    var lastOfLine = this._Current - 2;
                    if (line_Start == lastOfLine)
                    {
                        isEndOfBody = true;
                        //Remove last period from bodydata
                        return(MimeParser.CreateNewBytes(new IntPtr(this._Start), lastOfLine - this._Start));
                    }
                }
                // Check this line is started by '-' (Boundary).
                //That avoid method call of CheckBoundary and improve performance.
                if (*line_Start == 45 && boundaryLength > -1)
                {
                    //We found the end of the string by ASC 10 and assume that the end of the string is \r\n,
                    var lastOfLine = this._Current - 1;
                    if (*lastOfLine == 13)
                    {
                        lastOfLine--;
                    }

                    var length = lastOfLine - line_Start + 1;
                    if (length == boundaryLength || length == boundaryLength + 2)
                    {
                        result = CheckBoundary(line_Start, lastOfLine + 1, boundary);
                        if (result != CheckBoundaryResult.None)
                        {
                            if (result == CheckBoundaryResult.Boundary)
                            {
                                //Back to start of line.To read boundary line on MimeParser.ReadMimeContent method.
                                this._Current = line_Start;
                            }
                            return(MimeParser.CreateNewBytes(new IntPtr(this._Start), line_Start - this._Start));
                        }
                    }
                }
                if (this._Current == this._End)
                {
                    //Contains only 1 line and end of buffer
                    if (line_Start == this._Start)
                    {
                        this._Current = line_Start;
                        _IsLastOfLine = true;
                        _LastLine     = MimeParser.CreateNewBytes(new IntPtr(this._Start), this._End - this._Start);
                        return(_EmptyBytes);
                    }
                    return(MimeParser.CreateNewBytes(new IntPtr(this._Start), this._Current - this._Start));
                }
                this._Current++;
                line_Start = this._Current;
            }
        }