Esempio n. 1
0
            /// <summary>
            /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
            /// </summary>
            /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
            /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
            /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
            /// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
            /// <exception cref="ArgumentNullException">Is raised when <b>buffer</b> is null reference.</exception>
            public override int Read(byte[] buffer,int offset,int count)
            {
                if(buffer == null){
                    throw new ArgumentNullException("buffer");
                }
                if(m_State == State.SeekFirst){
                    throw new InvalidOperationException("Read method is not valid in '" + m_State + "' state.");
                }
                if(m_State == State.ReadNext || m_State == State.Done){
                    return 0;
                }

                /* RFC 2046 5.1.1.
                    NOTE:  The CRLF preceding the boundary delimiter line is conceptually
                    attached to the boundary so that it is possible to have a part that
                    does not end with a CRLF (line  break).  
                 
                    NOTE: We just need read 1 line ahead, otherwise we can't remove boundary preceeding CRLF.
                  
                   dash-boundary := "--" boundary
                 
                    multipart-body := [preamble CRLF]
                                      dash-boundary transport-padding CRLF
                                      body-part *encapsulation
                                      close-delimiter transport-padding
                                      [CRLF epilogue]
                 
                    encapsulation := delimiter transport-padding
                                     CRLF body-part

                    delimiter := CRLF dash-boundary

                    close-delimiter := delimiter "--"
                */
                                
                // Read line ahead, if none available. This is done for the boundary first line only.
                if(m_pPreviousLine == null){
                    m_pPreviousLine = new _DataLine(m_pStream.LineBufferSize);

                    m_pStream.ReadLine(m_pReadLineOP,false);
                    if(m_pReadLineOP.Error != null){
                        throw m_pReadLineOP.Error;
                    }
                    // We reached end of stream. Bad boundary: boundary end tag missing.
                    else if(m_pReadLineOP.BytesInBuffer == 0){
                        m_State = State.Done;

                        return 0;
                    }
                    // We have readed all MIME entity body parts.(boundary end tag reached)
                    else if(m_pReadLineOP.Buffer[0] == '-' && string.Equals("--" + m_Boundary + "--",m_pReadLineOP.LineUtf8)){
                        m_State = State.Done;

                        // Read "epilogoue",if has any.
                        while(true){
                            m_pStream.ReadLine(m_pReadLineOP,false);

                            if(m_pReadLineOP.Error != null){
                                throw m_pReadLineOP.Error;
                            }
                            // We reached end of stream. Epilogue reading completed.
                            else if(m_pReadLineOP.BytesInBuffer == 0){
                                break;
                            }
                            else{
                                m_pTextEpilogue.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                            }
                        }

                        return 0;
                    }
                    // We have readed all active boundary data, next boundary start tag.
                    else if(m_pReadLineOP.Buffer[0] == '-' && string.Equals("--" + m_Boundary,m_pReadLineOP.LineUtf8)){
                        m_State = State.ReadNext;

                        return 0;
                    }
                    // Store first read-ahed line.
                    else{
                        m_pPreviousLine.AssignFrom(m_pReadLineOP);
                    }
                }

                m_pStream.ReadLine(m_pReadLineOP,false);
                if(m_pReadLineOP.Error != null){
                    throw m_pReadLineOP.Error;
                }
                // We reached end of stream. Bad boundary: boundary end tag missing.
                else if(m_pReadLineOP.BytesInBuffer == 0){
                    m_State = State.Done;

                    if(count < m_pPreviousLine.BytesInBuffer){
                        throw new ArgumentException("Argument 'buffer' is to small. This should never happen.");
                    }
                    if(m_pPreviousLine.BytesInBuffer > 0){
                        Array.Copy(m_pPreviousLine.LineBuffer,0,buffer,offset,m_pPreviousLine.BytesInBuffer);
                    }

                    return m_pPreviousLine.BytesInBuffer;
                }
                // We have readed all MIME entity body parts.(boundary end tag reached)
                else if(m_pReadLineOP.Buffer[0] == '-' && string.Equals("--" + m_Boundary + "--",m_pReadLineOP.LineUtf8)){
                    m_State = State.Done;

                    // The CRLF at the end of boundry is part of epilogue.
                    if(m_pReadLineOP.Buffer[m_pReadLineOP.BytesInBuffer - 1] == '\n'){
                        m_pTextEpilogue.Append("\r\n");
                    }

                    // Read "epilogoue",if has any.
                    while(true){
                        m_pStream.ReadLine(m_pReadLineOP,false);

                        if(m_pReadLineOP.Error != null){
                            throw m_pReadLineOP.Error;
                        }
                        // We reached end of stream. Epilogue reading completed.
                        else if(m_pReadLineOP.BytesInBuffer == 0){
                            break;
                        }
                        else{
                            m_pTextEpilogue.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                        }
                    }
                                        
                    if(count < m_pPreviousLine.BytesInBuffer){
                        throw new ArgumentException("Argument 'buffer' is to small. This should never happen.");
                    }
                    // Return previous line data - CRLF, because CRLF if part of boundary tag.
                    if(m_pPreviousLine.BytesInBuffer > 2){
                        Array.Copy(m_pPreviousLine.LineBuffer,0,buffer,offset,m_pPreviousLine.BytesInBuffer - 2);

                        return m_pPreviousLine.BytesInBuffer - 2;
                    }
                    else{
                        return 0;
                    }
                }
                // We have readed all active boundary data, next boundary start tag.
                else if(m_pReadLineOP.Buffer[0] == '-' && string.Equals("--" + m_Boundary,m_pReadLineOP.LineUtf8)){
                    m_State = State.ReadNext;

                    // Return previous line data - CRLF, because CRLF if part of boundary tag.
                    if(count < m_pPreviousLine.BytesInBuffer){
                        throw new ArgumentException("Argument 'buffer' is to small. This should never happen.");
                    }
                    if(m_pPreviousLine.BytesInBuffer > 2){
                        Array.Copy(m_pPreviousLine.LineBuffer,0,buffer,offset,m_pPreviousLine.BytesInBuffer - 2);

                        return m_pPreviousLine.BytesInBuffer - 2;
                    }
                    else{
                        return 0;
                    }
                }
                // We have boundary data-line.
                else{
                    // Here we actually process previous line and store current.

                    if(count < m_pPreviousLine.BytesInBuffer){
                        throw new ArgumentException("Argument 'buffer' is to small. This should never happen.");
                    }                    
                    Array.Copy(m_pPreviousLine.LineBuffer,0,buffer,offset,m_pPreviousLine.BytesInBuffer);

                    int countCopied = m_pPreviousLine.BytesInBuffer;

                    // Store current line as previous.
                    m_pPreviousLine.AssignFrom(m_pReadLineOP);
                                        
                    return countCopied;
                }
            }
Esempio n. 2
0
            /// <summary>
            /// Moves to next "body part". Returns true if moved to next "body part" or false if there are no more parts.
            /// </summary>
            /// <returns>Returns true if moved to next "body part" or false if there are no more body parts.</returns>
            public bool Next()
            {
                /* RFC 2046 5.1.1.                 
                    dash-boundary := "--" boundary
                 
                    multipart-body := [preamble CRLF]
                                      dash-boundary transport-padding CRLF
                                      body-part *encapsulation
                                      close-delimiter transport-padding
                                      [CRLF epilogue]
                 
                    encapsulation := delimiter transport-padding
                                     CRLF body-part

                    delimiter := CRLF dash-boundary

                    close-delimiter := delimiter "--"
                */

                if(m_State == State.InBoundary){
                    throw new InvalidOperationException("You must read all boundary data, before calling this method.");
                }
                else if(m_State == State.Done){
                    return false;
                }
                else if(m_State == State.SeekFirst){
                    m_pPreviousLine = null;

                    // Read preamble and move into first boundary. 
                    while(true){
                        m_pStream.ReadLine(m_pReadLineOP,false);                                                
                        if(m_pReadLineOP.Error != null){
                            throw m_pReadLineOP.Error;
                        }
                        // We reached end of stream. Bad boundary: boundary end tag missing.
                        else if(m_pReadLineOP.BytesInBuffer == 0){
                            m_State = State.Done;

                            return false;
                        }
                        // We have boundary start.
                        else if(m_pReadLineOP.LineUtf8.Trim() == ("--" + m_Boundary)){
                            m_State = State.InBoundary;

                            return true;
                        }
                        // Preamble line.
                        else{
                            m_pTextPreamble.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                        }                    
                    }                   
                }
                else if(m_State == State.ReadNext){
                    m_pPreviousLine = null;
                    m_State = State.InBoundary;

                    return true;
                }
           
                return false;
            }
Esempio n. 3
0
            /// <summary>
            /// Moves to next "body part". Returns true if moved to next "body part" or false if there are no more parts.
            /// </summary>
            /// <returns>Returns true if moved to next "body part" or false if there are no more body parts.</returns>
            public bool Next()
            {
                /* RFC 2046 5.1.1.
                    NOTE:  The CRLF preceding the boundary delimiter line is conceptually
                    attached to the boundary so that it is possible to have a part that
                    does not end with a CRLF (line  break).
                */

                if(m_State == State.InBoundary){
                    throw new InvalidOperationException("You must read all boundary data, before calling this method.");
                }
                else if(m_State == State.Done){
                    return false;
                }
                else if(m_State == State.SeekFirst){
                    m_pPreviousLine = null;

                    while(true){
                        m_pStream.ReadLine(m_pReadLineOP,false);
                        if(m_pReadLineOP.Error != null){
                            throw m_pReadLineOP.Error;
                        }
                        // We reached end of stream. Bad boundary: boundary end tag missing.
                        else if(m_pReadLineOP.BytesInBuffer == 0){
                            m_State = State.Done;

                            return false;
                        }
                        else{
                            // Check if we have boundary start/end.
                            if(m_pReadLineOP.Buffer[0] == '-'){
                                string boundary = m_pReadLineOP.LineUtf8;
                                // We have readed all MIME entity body parts.
                                if("--" + m_Boundary + "--" == boundary){
                                    m_State = State.Done;

                                    // Last CRLF is no part of preamble, but is part of boundary-tag.
                                    if(m_pTextPreamble.Length >= 2){
                                        m_pTextPreamble.Remove(m_pTextPreamble.Length - 2,2);
                                    }

                                    // Read "epilogoue",if has any.
                                    while(true){
                                        m_pStream.ReadLine(m_pReadLineOP,false);

                                        if(m_pReadLineOP.Error != null){
                                            throw m_pReadLineOP.Error;
                                        }
                                        // We reached end of stream. Epilogue reading completed.
                                        else if(m_pReadLineOP.BytesInBuffer == 0){
                                            break;
                                        }
                                        else{
                                            m_pTextEpilogue.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                                        }
                                    }

                                    return false;
                                }
                                // We have next boundary.
                                else if("--" + m_Boundary == boundary){
                                    m_State = State.InBoundary;

                                    // Last CRLF is no part of preamble, but is part of boundary-tag.
                                    if(m_pTextPreamble.Length >= 2){
                                        m_pTextPreamble.Remove(m_pTextPreamble.Length - 2,2);
                                    }

                                    return true;
                                }
                                // Not boundary or not boundary we want.
                                //else{
                            }

                            m_pTextPreamble.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                        }
                    }
                }
                else if(m_State == State.ReadNext){
                    m_pPreviousLine = null;
                    m_State = State.InBoundary;

                    return true;
                }

                return false;
            }
Esempio n. 4
0
            /// <summary>
            /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
            /// </summary>
            /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
            /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
            /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
            /// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
            /// <exception cref="ArgumentNullException">Is raised when <b>buffer</b> is null reference.</exception>
            public override int Read(byte[] buffer, int offset, int count)
            {
                if (buffer == null)
                {
                    throw new ArgumentNullException("buffer");
                }
                if (m_State == State.SeekFirst)
                {
                    throw new InvalidOperationException("Read method is not valid in '" + m_State + "' state.");
                }
                if (m_State == State.ReadNext || m_State == State.Done)
                {
                    return(0);
                }

                /* RFC 2046 5.1.1.
                 *  NOTE:  The CRLF preceding the boundary delimiter line is conceptually
                 *  attached to the boundary so that it is possible to have a part that
                 *  does not end with a CRLF (line  break).
                 *
                 * NOTE: We just need read 1 line ahead, oterwise we can't remove boundary preceeding CRLF.
                 */

                // Read line ahead, if none available. This is done for the boundary first line only.
                if (m_pPreviousLine == null)
                {
                    m_pPreviousLine = new _DataLine();

                    m_pStream.ReadLine(m_pReadLineOP, false);
                    if (m_pReadLineOP.Error != null)
                    {
                        throw m_pReadLineOP.Error;
                    }
                    // We reached end of stream. Bad boundary: boundary end tag missing.
                    else if (m_pReadLineOP.BytesInBuffer == 0)
                    {
                        m_State = State.Done;

                        return(0);
                    }
                    // We have readed all MIME entity body parts.(boundary end tag reached)
                    else if (m_pReadLineOP.Buffer[0] == '-' && string.Equals("--" + m_Boundary + "--", m_pReadLineOP.LineUtf8))
                    {
                        m_State = State.Done;

                        // Read "epilogoue",if has any.
                        while (true)
                        {
                            m_pStream.ReadLine(m_pReadLineOP, false);

                            if (m_pReadLineOP.Error != null)
                            {
                                throw m_pReadLineOP.Error;
                            }
                            // We reached end of stream. Epilogue reading completed.
                            else if (m_pReadLineOP.BytesInBuffer == 0)
                            {
                                break;
                            }
                            else
                            {
                                m_pTextEpilogue.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                            }
                        }

                        return(0);
                    }
                    // We have readed all active boundary data, next boundary start tag.
                    else if (m_pReadLineOP.Buffer[0] == '-' && string.Equals("--" + m_Boundary, m_pReadLineOP.LineUtf8))
                    {
                        m_State = State.ReadNext;

                        return(0);
                    }
                    // Store first read-ahed line.
                    else
                    {
                        m_pPreviousLine.AssignFrom(m_pReadLineOP);
                    }
                }

                m_pStream.ReadLine(m_pReadLineOP, false);
                if (m_pReadLineOP.Error != null)
                {
                    throw m_pReadLineOP.Error;
                }
                // We reached end of stream. Bad boundary: boundary end tag missing.
                else if (m_pReadLineOP.BytesInBuffer == 0)
                {
                    m_State = State.Done;

                    if (count < m_pPreviousLine.BytesInBuffer)
                    {
                        throw new ArgumentException("Argument 'buffer' is to small. This should never happen.");
                    }
                    if (m_pPreviousLine.BytesInBuffer > 0)
                    {
                        Array.Copy(m_pPreviousLine.LineBuffer, 0, buffer, offset, m_pPreviousLine.BytesInBuffer);
                    }

                    return(m_pPreviousLine.BytesInBuffer);
                }
                // We have readed all MIME entity body parts.(boundary end tag reached)
                else if (m_pReadLineOP.Buffer[0] == '-' && string.Equals("--" + m_Boundary + "--", m_pReadLineOP.LineUtf8))
                {
                    m_State = State.Done;

                    // Read "epilogoue",if has any.
                    while (true)
                    {
                        m_pStream.ReadLine(m_pReadLineOP, false);

                        if (m_pReadLineOP.Error != null)
                        {
                            throw m_pReadLineOP.Error;
                        }
                        // We reached end of stream. Epilogue reading completed.
                        else if (m_pReadLineOP.BytesInBuffer == 0)
                        {
                            break;
                        }
                        else
                        {
                            m_pTextEpilogue.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                        }
                    }

                    if (count < m_pPreviousLine.BytesInBuffer)
                    {
                        throw new ArgumentException("Argument 'buffer' is to small. This should never happen.");
                    }
                    // Return previous line data - CRLF, because CRLF if part of boundary tag.
                    if (m_pPreviousLine.BytesInBuffer > 2)
                    {
                        Array.Copy(m_pPreviousLine.LineBuffer, 0, buffer, offset, m_pPreviousLine.BytesInBuffer - 2);

                        return(m_pPreviousLine.BytesInBuffer - 2);
                    }
                    else
                    {
                        return(0);
                    }
                }
                // We have readed all active boundary data, next boundary start tag.
                else if (m_pReadLineOP.Buffer[0] == '-' && string.Equals("--" + m_Boundary, m_pReadLineOP.LineUtf8))
                {
                    m_State = State.ReadNext;

                    // Return previous line data - CRLF, because CRLF if part of boundary tag.
                    if (count < m_pPreviousLine.BytesInBuffer)
                    {
                        throw new ArgumentException("Argument 'buffer' is to small. This should never happen.");
                    }
                    if (m_pPreviousLine.BytesInBuffer > 2)
                    {
                        Array.Copy(m_pPreviousLine.LineBuffer, 0, buffer, offset, m_pPreviousLine.BytesInBuffer - 2);

                        return(m_pPreviousLine.BytesInBuffer - 2);
                    }
                    else
                    {
                        return(0);
                    }
                }
                // We have boundary data-line.
                else
                {
                    // Here we actually process previous line and store current.

                    if (count < m_pPreviousLine.BytesInBuffer)
                    {
                        throw new ArgumentException("Argument 'buffer' is to small. This should never happen.");
                    }
                    Array.Copy(m_pPreviousLine.LineBuffer, 0, buffer, offset, m_pPreviousLine.BytesInBuffer);

                    int countCopied = m_pPreviousLine.BytesInBuffer;

                    // Store current line as previous.
                    m_pPreviousLine.AssignFrom(m_pReadLineOP);

                    return(countCopied);
                }
            }
Esempio n. 5
0
            /// <summary>
            /// Moves to next "body part". Returns true if moved to next "body part" or false if there are no more parts.
            /// </summary>
            /// <returns>Returns true if moved to next "body part" or false if there are no more body parts.</returns>
            public bool Next()
            {
                /* RFC 2046 5.1.1.
                 *  NOTE:  The CRLF preceding the boundary delimiter line is conceptually
                 *  attached to the boundary so that it is possible to have a part that
                 *  does not end with a CRLF (line  break).
                 */

                if (m_State == State.InBoundary)
                {
                    throw new InvalidOperationException("You must read all boundary data, before calling this method.");
                }
                else if (m_State == State.Done)
                {
                    return(false);
                }
                else if (m_State == State.SeekFirst)
                {
                    m_pPreviousLine = null;

                    while (true)
                    {
                        m_pStream.ReadLine(m_pReadLineOP, false);
                        if (m_pReadLineOP.Error != null)
                        {
                            throw m_pReadLineOP.Error;
                        }
                        // We reached end of stream. Bad boundary: boundary end tag missing.
                        else if (m_pReadLineOP.BytesInBuffer == 0)
                        {
                            m_State = State.Done;

                            return(false);
                        }
                        else
                        {
                            // Check if we have boundary start/end.
                            if (m_pReadLineOP.Buffer[0] == '-')
                            {
                                string boundary = m_pReadLineOP.LineUtf8;
                                // We have readed all MIME entity body parts.
                                if ("--" + m_Boundary + "--" == boundary)
                                {
                                    m_State = State.Done;

                                    // Last CRLF is no part of preamble, but is part of boundary-tag.
                                    if (m_pTextPreamble.Length >= 2)
                                    {
                                        m_pTextPreamble.Remove(m_pTextPreamble.Length - 2, 2);
                                    }

                                    // Read "epilogoue",if has any.
                                    while (true)
                                    {
                                        m_pStream.ReadLine(m_pReadLineOP, false);

                                        if (m_pReadLineOP.Error != null)
                                        {
                                            throw m_pReadLineOP.Error;
                                        }
                                        // We reached end of stream. Epilogue reading completed.
                                        else if (m_pReadLineOP.BytesInBuffer == 0)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            m_pTextEpilogue.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                                        }
                                    }

                                    return(false);
                                }
                                // We have next boundary.
                                else if ("--" + m_Boundary == boundary)
                                {
                                    m_State = State.InBoundary;

                                    // Last CRLF is no part of preamble, but is part of boundary-tag.
                                    if (m_pTextPreamble.Length >= 2)
                                    {
                                        m_pTextPreamble.Remove(m_pTextPreamble.Length - 2, 2);
                                    }

                                    return(true);
                                }
                                // Not boundary or not boundary we want.
                                //else{
                            }

                            m_pTextPreamble.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                        }
                    }
                }
                else if (m_State == State.ReadNext)
                {
                    m_pPreviousLine = null;
                    m_State         = State.InBoundary;

                    return(true);
                }

                return(false);
            }
Esempio n. 6
0
            /// <summary>
            /// Moves to next "body part". Returns true if moved to next "body part" or false if there are no more parts.
            /// </summary>
            /// <returns>Returns true if moved to next "body part" or false if there are no more body parts.</returns>
            public bool Next()
            {
                /* RFC 2046 5.1.1.
                 *  dash-boundary := "--" boundary
                 *
                 *  multipart-body := [preamble CRLF]
                 *                    dash-boundary transport-padding CRLF
                 *                    body-part *encapsulation
                 *                    close-delimiter transport-padding
                 *                    [CRLF epilogue]
                 *
                 *  encapsulation := delimiter transport-padding
                 *                   CRLF body-part
                 *
                 *  delimiter := CRLF dash-boundary
                 *
                 *  close-delimiter := delimiter "--"
                 */

                if (m_State == State.InBoundary)
                {
                    throw new InvalidOperationException("You must read all boundary data, before calling this method.");
                }
                else if (m_State == State.Done)
                {
                    return(false);
                }
                else if (m_State == State.SeekFirst)
                {
                    m_pPreviousLine = null;

                    // Read preamble and move into first boundary.
                    while (true)
                    {
                        m_pStream.ReadLine(m_pReadLineOP, false);
                        if (m_pReadLineOP.Error != null)
                        {
                            throw m_pReadLineOP.Error;
                        }
                        // We reached end of stream. Bad boundary: boundary end tag missing.
                        else if (m_pReadLineOP.BytesInBuffer == 0)
                        {
                            m_State = State.Done;

                            return(false);
                        }
                        // We have boundary start.
                        else if (m_pReadLineOP.LineUtf8.Trim() == ("--" + m_Boundary))
                        {
                            m_State = State.InBoundary;

                            return(true);
                        }
                        // Preamble line.
                        else
                        {
                            m_pTextPreamble.Append(m_pReadLineOP.LineUtf8 + "\r\n");
                        }
                    }
                }
                else if (m_State == State.ReadNext)
                {
                    m_pPreviousLine = null;
                    m_State         = State.InBoundary;

                    return(true);
                }

                return(false);
            }