System.Net.Connection.FindChunkEntitySize C# (CSharp) Method

FindChunkEntitySize() private static method

private static FindChunkEntitySize ( byte buffer, int offset, int size ) : int
buffer byte
offset int
size int
return int
        private static int FindChunkEntitySize(byte[] buffer, int offset, int size) {
            BufferChunkBytes BufferStruct = new BufferChunkBytes();

            int EndOffset, StartOffset, BytesTaken, ChunkLength;
            StartOffset = offset;
            EndOffset = offset + size;
            BufferStruct.Buffer = buffer;

            //
            // While we haven't reached the end, loop through the buffer. Get
            // the chunk length, and if we can do that and it's not zero figure
            // out how many bytes are taken up by extensions and CRLF. If we
            // have enough for that, add the chunk length to our offset and see
            // if we've reached the end. If the chunk length is 0 at any point
            // we might have all the chunks. Skip the CRLF and footers and next
            // CRLF, and if that all works return the index where we are.
            //

            while (offset < EndOffset) {
                // Read the chunk size.

                BufferStruct.Offset = offset;
                BufferStruct.Count = size;

                BytesTaken = ChunkParse.GetChunkSize(BufferStruct, out ChunkLength);

                // See if we have enough data to read the chunk size.

                if (BytesTaken == -1) {
                    // Didn't,  so return -1.
                    return -1;
                }

                // Make sure we didn't have a syntax error in the parse.

                if (BytesTaken == 0) {
                    return 0;
                }

                // Update our state for what we've taken.

                offset += BytesTaken;
                size -= BytesTaken;

                // If the chunk length isn't 0, skip the extensions and CRLF.

                if (ChunkLength != 0) {
                    // Not zero, skip extensions.

                    BufferStruct.Offset = offset;
                    BufferStruct.Count = size;

                    BytesTaken = ChunkParse.SkipPastCRLF(BufferStruct);

                    // If we ran out of buffer doing it or had an error, return -1.

                    if (BytesTaken <= 0) {
                        return BytesTaken;
                    }

                    // Update our state for what we took.

                    offset += BytesTaken;
                    size -= BytesTaken;

                    // Now update our state for the chunk length and trailing CRLF.
                    offset += (ChunkLength + CRLFSize);
                    size -= (ChunkLength + CRLFSize);


                }
                else {
                    // The chunk length is 0. Skip the CRLF, then the footers.

                    if (size < CRLFSize) {
                        // Not enough left for CRLF

                        return -1;
                    }

                    offset += CRLFSize;
                    size -= CRLFSize;

                    // Skip the footers. We'll loop while we don't have a CRLF
                    // at the current offset.
                    while (size >= CRLFSize && (buffer[offset] != '\r' && buffer[offset + 1] != '\n')) {
                        BufferStruct.Offset = offset;
                        BufferStruct.Count = size;

                        BytesTaken = ChunkParse.SkipPastCRLF(BufferStruct);

                        // Make sure we had enough.

                        if (BytesTaken <= 0) {
                            return BytesTaken;
                        }

                        // Had enough, so update our sizes.
                        offset += BytesTaken;
                        size -= BytesTaken;
                    }

                    // If we get here, either we found the last CRLF or we ran out
                    // of buffer. See which it is.

                    if (size >= CRLFSize) {
                        // Found the last bit, return the size including the last CRLF
                        // after that.

                        return(offset + CRLFSize) - StartOffset;
                    }
                    else {
                        // Ran out of buffer.
                        return -1;
                    }
                }

            }

            return -1;
        }