System.Net.ChunkParse.GetChunkSize C# (CSharp) Method

GetChunkSize() static private method

static private GetChunkSize ( IReadChunkBytes Source, int &chunkSize ) : int
Source IReadChunkBytes
chunkSize int
return int
        GetChunkSize(IReadChunkBytes Source, out int chunkSize) {
            int         BytesRead;
            int         Size;
            int         Current;

            GlobalLog.Enter("GetChunkSize");
            Size = 0;

            //
            // Loop while we have data. If we run out of data and exit the loop
            // at the bottom, then we've run out of data and haven't found the
            // length.

            Current = Source.NextByte;
            BytesRead = 0;

            if (Current == 10 || Current == 13) {
                GlobalLog.Print(" Got Char: " + Current.ToString("X"));
                BytesRead++;
                Current = Source.NextByte;
            }

            while (Current != -1) {
                // Get the next byte, and decode it.

                if (Current >= '0' && Current <= '9') {
                    // Normalize it to 0 based.

                    Current -= '0';

                }
                else {
                    // If we're here, this might be a hex digit.
                    // If it is, normalize it to 10 based.

                    if (Current >= 'a' && Current <= 'f') {
                        Current -= 'a';
                    }
                    else {
                        if (Current >= 'A' && Current <= 'F') {
                            Current -= 'A';
                        }
                        else {
                            // Done with the decoding. If we haven't actually
                            // decoded a digit yet, we'll end up returning
                            // 0, which will signal the error.


                            // Push back the byte we read and didn't use.

                            Source.NextByte = Current;

                            chunkSize = Size;

                            // Return how many bytes we took.

                            GlobalLog.Print("Chunk Size = " + Size.ToString());
                            GlobalLog.Leave("GetChunkSize", BytesRead);
                            return BytesRead;

                        }
                    }

                    // If we get here we've had an A-F digit, add 10
                    // to it to normalize it.

                    Current += 10;
                }

                // Update the size and our state.

                Size *= 16;
                Size += Current;

                BytesRead++;
                Current = Source.NextByte;
            }

            chunkSize = Size;
            GlobalLog.Print("*Chunk Size* = " + Size.ToString());
            GlobalLog.Leave("GetChunkSize", -1);
            return -1;
        }
    } // ChunkParse