Example #1
0
        /*

            FindChunkEntitySize - Find the total size of a chunked entity body.

            An internal utility function. This is called when we have chunk encoded
            data in our internal receive buffer and want to know how big the total
            entity body is. We'll parse through it, looking for the terminating
            0CRLFCRLF string. We return -1 if we can't find it.

            Input:
                    buffer              - Buffer to be checked
                    offset              - Offset into buffer to start checking.
                    size               - Number of bytes to check.

            Returns:

                    The total size of the chunked entity body, or -1
                    if it can't find determine it. We'll return 0 if there's a
                    syntax error ensizeered.

        --*/
        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;
        }
        private static int FindChunkEntitySize(byte[] buffer, int offset, int size)
        {
            BufferChunkBytes source = new BufferChunkBytes();
            int num2 = offset;
            int num = offset + size;
            source.Buffer = buffer;
            while (offset < num)
            {
                int num4;
                source.Offset = offset;
                source.Count = size;
                int chunkSize = ChunkParse.GetChunkSize(source, out num4);
                switch (chunkSize)
                {
                    case -1:
                        return -1;

                    case 0:
                        return 0;
                }
                offset += chunkSize;
                size -= chunkSize;
                if (num4 != 0)
                {
                    source.Offset = offset;
                    source.Count = size;
                    chunkSize = ChunkParse.SkipPastCRLF(source);
                    if (chunkSize <= 0)
                    {
                        return chunkSize;
                    }
                    offset += chunkSize;
                    size -= chunkSize;
                    offset += num4 + 2;
                    size -= num4 + 2;
                }
                else
                {
                    if (size >= 2)
                    {
                        offset += 2;
                        size -= 2;
                        while (((size >= 2) && (buffer[offset] != 13)) && (buffer[offset + 1] != 10))
                        {
                            source.Offset = offset;
                            source.Count = size;
                            chunkSize = ChunkParse.SkipPastCRLF(source);
                            if (chunkSize <= 0)
                            {
                                return chunkSize;
                            }
                            offset += chunkSize;
                            size -= chunkSize;
                        }
                        if (size >= 2)
                        {
                            return ((offset + 2) - num2);
                        }
                    }
                    return -1;
                }
            }
            return -1;
        }