Example #1
0
        private int DecodeContByteData(ArraySegment <byte> buf)
        {
            var offset = buf.Offset;
            var count  = buf.Count;

            // Check how many bytes are available and how much we need
            var available = count;
            var need      = this._octetLength - this._bufferOffset;

            var toCopy = available >= need ? need : available;

            if (toCopy > 0)
            {
                // Return is wrong because it doesn't handle 0byte strings
                // Copy that amount of data into our target buffer
                Array.Copy(buf.Array, offset, this._stringBuffer, this._bufferOffset, toCopy);
                this._bufferOffset += toCopy;
                // Adjust the offset of the input and output buffer
                offset += toCopy;
                count  -= toCopy;
            }

            if (this._bufferOffset == this._octetLength)
            {
                // Copied everything
                var view = new ArraySegment <byte>(
                    this._stringBuffer, 0, this._octetLength
                    );
                if (this._huffman)
                {
                    // We need to perform huffman decoding
                    this.Result = Huffman.Decode(view, _bufferPool);
                }
                else
                {
                    // TODO: Check if encoding is really correct
                    this.Result =
                        Encoding.ASCII.GetString(view.Array, view.Offset, view.Count);
                }
                // TODO: Optionally check here for valid HTTP/2 header names
                this.Done = true;
                // The string length for the table is used without huffman encoding
                // TODO: This might by a different result than Encoding.ASCII.GetByteCount
                // Might be required to streamline that
                this.StringLength = this.Result.Length;
                this._state       = State.StartDecode;

                _bufferPool.Return(this._stringBuffer);
                this._stringBuffer = null;
            }
            // Else we need more input data

            return(offset - buf.Offset);
        }