Beispiel #1
0
        public int Decode(byte[] buffer, int offset, int size)
        {
            DecoderHelper.ValidateSize(size);
            if (_isValueDecoded)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SRServiceModel.FramingValueNotAvailable));
            }
            int bytesConsumed = 0;

            while (bytesConsumed < size)
            {
                int next = buffer[offset];
                _value |= (next & 0x7F) << (_index * 7);
                bytesConsumed++;
                if (_index == LastIndex && (next & 0xF8) != 0)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(SRServiceModel.FramingSizeTooLarge));
                }
                _index++;
                if ((next & 0x80) == 0)
                {
                    _isValueDecoded = true;
                    break;
                }
                offset++;
            }
            return(bytesConsumed);
        }
        public int Decode(byte[] buffer, int offset, int size)
        {
            DecoderHelper.ValidateSize(size);
            if (this.isValueDecoded)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("FramingValueNotAvailable")));
            }
            int num = 0;

            while (num < size)
            {
                int num2 = buffer[offset];
                this.value |= (num2 & 0x7f) << (this.index * 7);
                num++;
                if ((this.index == 4) && ((num2 & 0xf8) != 0))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("FramingSizeTooLarge")));
                }
                this.index = (short)(this.index + 1);
                if ((num2 & 0x80) == 0)
                {
                    this.isValueDecoded = true;
                    return(num);
                }
                offset++;
            }
            return(num);
        }
        public int Decode(byte[] buffer, int offset, int size)
        {
            DecoderHelper.ValidateSize(size);

            int bytesConsumed;

            switch (_currentState)
            {
            case State.ReadingSize:
                bytesConsumed = _sizeDecoder.Decode(buffer, offset, size);
                if (_sizeDecoder.IsValueDecoded)
                {
                    _encodedSize = _sizeDecoder.Value;
                    if (_encodedSize > _sizeQuota)
                    {
                        Exception quotaExceeded = OnSizeQuotaExceeded(_encodedSize);
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(quotaExceeded);
                    }
                    if (_encodedBytes == null || _encodedBytes.Length < _encodedSize)
                    {
                        _encodedBytes = Fx.AllocateByteArray(_encodedSize);
                        _value        = null;
                    }
                    _currentState = State.ReadingBytes;
                    _bytesNeeded  = _encodedSize;
                }
                break;

            case State.ReadingBytes:
                if (_value != null && _valueLengthInBytes == _encodedSize && _bytesNeeded == _encodedSize &&
                    size >= _encodedSize && CompareBuffers(_encodedBytes, buffer, offset))
                {
                    bytesConsumed = _bytesNeeded;
                    OnComplete(_value);
                }
                else
                {
                    bytesConsumed = _bytesNeeded;
                    if (size < _bytesNeeded)
                    {
                        bytesConsumed = size;
                    }

                    Buffer.BlockCopy(buffer, offset, _encodedBytes, _encodedSize - _bytesNeeded, bytesConsumed);
                    _bytesNeeded -= bytesConsumed;
                    if (_bytesNeeded == 0)
                    {
                        _value = Encoding.UTF8.GetString(_encodedBytes, 0, _encodedSize);
                        _valueLengthInBytes = _encodedSize;
                        OnComplete(_value);
                    }
                }
                break;

            default:
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(SR.InvalidDecoderStateMachine));
            }

            return(bytesConsumed);
        }
        public int Decode(byte[] bytes, int offset, int size)
        {
            int num2;

            DecoderHelper.ValidateSize(size);
            try
            {
                int num;
                switch (this.currentState)
                {
                case State.ReadingVersionRecord:
                    base.ValidateRecordType(FramingRecordType.Version, (FramingRecordType)bytes[offset]);
                    this.currentState = State.ReadingMajorVersion;
                    num = 1;
                    break;

                case State.ReadingMajorVersion:
                    this.majorVersion = bytes[offset];
                    base.ValidateMajorVersion(this.majorVersion);
                    this.currentState = State.ReadingMinorVersion;
                    num = 1;
                    break;

                case State.ReadingMinorVersion:
                    this.minorVersion = bytes[offset];
                    this.currentState = State.ReadingModeRecord;
                    num = 1;
                    break;

                case State.ReadingModeRecord:
                    base.ValidateRecordType(FramingRecordType.Mode, (FramingRecordType)bytes[offset]);
                    this.currentState = State.ReadingModeValue;
                    num = 1;
                    break;

                case State.ReadingModeValue:
                    this.mode = (FramingMode)bytes[offset];
                    base.ValidateFramingMode(this.mode);
                    this.currentState = State.Done;
                    num = 1;
                    break;

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine"))));
                }
                base.StreamPosition += num;
                num2 = num;
            }
            catch (InvalidDataException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(exception));
            }
            return(num2);
        }
Beispiel #5
0
        public int Decode(byte[] buffer, int offset, int size)
        {
            int bytesNeeded;

            DecoderHelper.ValidateSize(size);
            switch (this.currentState)
            {
            case State.ReadingSize:
                bytesNeeded = this.sizeDecoder.Decode(buffer, offset, size);
                if (this.sizeDecoder.IsValueDecoded)
                {
                    this.encodedSize = this.sizeDecoder.Value;
                    if (this.encodedSize > this.sizeQuota)
                    {
                        Exception exception = this.OnSizeQuotaExceeded(this.encodedSize);
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
                    }
                    if ((this.encodedBytes == null) || (this.encodedBytes.Length < this.encodedSize))
                    {
                        this.encodedBytes = DiagnosticUtility.Utility.AllocateByteArray(this.encodedSize);
                        this.value        = null;
                    }
                    this.currentState = State.ReadingBytes;
                    this.bytesNeeded  = this.encodedSize;
                    return(bytesNeeded);
                }
                return(bytesNeeded);

            case State.ReadingBytes:
                if ((((this.value == null) || (this.valueLengthInBytes != this.encodedSize)) || ((this.bytesNeeded != this.encodedSize) || (size < this.encodedSize))) || !CompareBuffers(this.encodedBytes, buffer, offset))
                {
                    bytesNeeded = this.bytesNeeded;
                    if (size < this.bytesNeeded)
                    {
                        bytesNeeded = size;
                    }
                    Buffer.BlockCopy(buffer, offset, this.encodedBytes, this.encodedSize - this.bytesNeeded, bytesNeeded);
                    this.bytesNeeded -= bytesNeeded;
                    if (this.bytesNeeded == 0)
                    {
                        this.value = Encoding.UTF8.GetString(this.encodedBytes, 0, this.encodedSize);
                        this.valueLengthInBytes = this.encodedSize;
                        this.OnComplete(this.value);
                    }
                    return(bytesNeeded);
                }
                bytesNeeded = this.bytesNeeded;
                this.OnComplete(this.value);
                return(bytesNeeded);
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine")));
        }
        public int Decode(byte[] bytes, int offset, int size)
        {
            int num2;

            DecoderHelper.ValidateSize(size);
            try
            {
                int num;
                FramingRecordType type;
                switch (this.currentState)
                {
                case State.ReadingViaRecord:
                    type = (FramingRecordType)bytes[offset];
                    base.ValidateRecordType(FramingRecordType.Via, type);
                    num = 1;
                    this.viaDecoder.Reset();
                    this.currentState = State.ReadingViaString;
                    goto Label_0148;

                case State.ReadingViaString:
                    num = this.viaDecoder.Decode(bytes, offset, size);
                    if (this.viaDecoder.IsValueDecoded)
                    {
                        this.currentState = State.ReadingContentTypeRecord;
                    }
                    goto Label_0148;

                case State.ReadingContentTypeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.KnownEncoding)
                    {
                        break;
                    }
                    num = 1;
                    this.currentState = State.ReadingContentTypeByte;
                    goto Label_0148;

                case State.ReadingContentTypeString:
                    num = this.contentTypeDecoder.Decode(bytes, offset, size);
                    if (this.contentTypeDecoder.IsValueDecoded)
                    {
                        this.currentState = State.Start;
                        this.contentType  = this.contentTypeDecoder.Value;
                    }
                    goto Label_0148;

                case State.ReadingContentTypeByte:
                    this.contentType = ContentTypeStringDecoder.GetString((FramingEncodingType)bytes[offset]);
                    num = 1;
                    this.currentState = State.Start;
                    goto Label_0148;

                case State.Start:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine"))));
                }
                base.ValidateRecordType(FramingRecordType.ExtensibleEncoding, type);
                num = 1;
                this.contentTypeDecoder.Reset();
                this.currentState = State.ReadingContentTypeString;
Label_0148:
                base.StreamPosition += num;
                num2 = num;
            }
            catch (InvalidDataException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(exception));
            }
            return(num2);
        }
Beispiel #7
0
        public override int Decode(byte[] bytes, int offset, int size)
        {
            DecoderHelper.ValidateSize(size);

            try
            {
                int bytesConsumed;
                FramingRecordType recordType;
                switch (CurrentState)
                {
                case ClientFramingDecoderState.ReadingUpgradeRecord:
                    recordType = (FramingRecordType)bytes[offset];
                    if (recordType == FramingRecordType.UpgradeResponse)
                    {
                        bytesConsumed     = 1;
                        base.CurrentState = ClientFramingDecoderState.UpgradeResponse;
                    }
                    else
                    {
                        bytesConsumed     = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingAckRecord;
                    }
                    break;

                case ClientFramingDecoderState.UpgradeResponse:
                    bytesConsumed     = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingUpgradeRecord;
                    break;

                case ClientFramingDecoderState.ReadingAckRecord:
                    recordType = (FramingRecordType)bytes[offset];
                    if (recordType == FramingRecordType.Fault)
                    {
                        bytesConsumed     = 1;
                        _faultDecoder     = new FaultStringDecoder();
                        base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                        break;
                    }
                    ValidatePreambleAck(recordType);
                    bytesConsumed     = 1;
                    base.CurrentState = ClientFramingDecoderState.Start;
                    break;

                case ClientFramingDecoderState.Start:
                    bytesConsumed     = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeRecord;
                    break;

                case ClientFramingDecoderState.ReadingEnvelopeRecord:
                    recordType = (FramingRecordType)bytes[offset];
                    if (recordType == FramingRecordType.End)
                    {
                        bytesConsumed     = 1;
                        base.CurrentState = ClientFramingDecoderState.End;
                        break;
                    }
                    else if (recordType == FramingRecordType.Fault)
                    {
                        bytesConsumed     = 0;
                        base.CurrentState = ClientFramingDecoderState.ReadingFault;
                        break;
                    }
                    ValidateRecordType(FramingRecordType.UnsizedEnvelope, recordType);
                    bytesConsumed     = 1;
                    base.CurrentState = ClientFramingDecoderState.EnvelopeStart;
                    break;

                case ClientFramingDecoderState.EnvelopeStart:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SRServiceModel.FramingAtEnd)));

                case ClientFramingDecoderState.ReadingFault:
                    recordType = (FramingRecordType)bytes[offset];
                    ValidateRecordType(FramingRecordType.Fault, recordType);
                    bytesConsumed     = 1;
                    _faultDecoder     = new FaultStringDecoder();
                    base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                    break;

                case ClientFramingDecoderState.ReadingFaultString:
                    bytesConsumed = _faultDecoder.Decode(bytes, offset, size);
                    if (_faultDecoder.IsValueDecoded)
                    {
                        base.CurrentState = ClientFramingDecoderState.Fault;
                    }
                    break;

                case ClientFramingDecoderState.Fault:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SRServiceModel.FramingAtEnd)));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SRServiceModel.InvalidDecoderStateMachine)));
                }

                StreamPosition += bytesConsumed;
                return(bytesConsumed);
            }
            catch (InvalidDataException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateException(e));
            }
        }
Beispiel #8
0
        public int Decode(byte[] bytes, int offset, int size)
        {
            DecoderHelper.ValidateSize(size);

            try
            {
                int bytesConsumed;
                switch (_currentState)
                {
                case State.ReadingEnvelopeChunkSize:
                    bytesConsumed = _sizeDecoder.Decode(bytes, offset, size);
                    if (_sizeDecoder.IsValueDecoded)
                    {
                        _chunkSize = _sizeDecoder.Value;
                        _sizeDecoder.Reset();

                        if (_chunkSize == 0)
                        {
                            _currentState = State.EnvelopeEnd;
                        }
                        else
                        {
                            _currentState     = State.ChunkStart;
                            _chunkBytesNeeded = _chunkSize;
                        }
                    }
                    break;

                case State.ChunkStart:
                    bytesConsumed = 0;
                    _currentState = State.ReadingEnvelopeBytes;
                    break;

                case State.ReadingEnvelopeBytes:
                    bytesConsumed = size;
                    if (bytesConsumed > _chunkBytesNeeded)
                    {
                        bytesConsumed = _chunkBytesNeeded;
                    }
                    _chunkBytesNeeded -= bytesConsumed;
                    if (_chunkBytesNeeded == 0)
                    {
                        _currentState = State.ChunkEnd;
                    }
                    break;

                case State.ChunkEnd:
                    bytesConsumed = 0;
                    _currentState = State.ReadingEnvelopeChunkSize;
                    break;

                case State.EnvelopeEnd:
                    ValidateRecordType(FramingRecordType.End, (FramingRecordType)bytes[offset]);
                    bytesConsumed = 1;
                    _currentState = State.End;
                    break;

                case State.End:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SRServiceModel.FramingAtEnd)));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              CreateException(new InvalidDataException(SRServiceModel.InvalidDecoderStateMachine)));
                }

                StreamPosition += bytesConsumed;
                return(bytesConsumed);
            }
            catch (InvalidDataException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateException(e));
            }
        }
        public int Decode(byte[] bytes, int offset, int size)
        {
            int num2;

            DecoderHelper.ValidateSize(size);
            try
            {
                int chunkBytesNeeded;
                switch (this.currentState)
                {
                case State.ReadingEnvelopeChunkSize:
                    chunkBytesNeeded = this.sizeDecoder.Decode(bytes, offset, size);
                    if (this.sizeDecoder.IsValueDecoded)
                    {
                        this.chunkSize = this.sizeDecoder.Value;
                        this.sizeDecoder.Reset();
                        if (this.chunkSize != 0)
                        {
                            break;
                        }
                        this.currentState = State.EnvelopeEnd;
                    }
                    goto Label_0136;

                case State.ChunkStart:
                    chunkBytesNeeded  = 0;
                    this.currentState = State.ReadingEnvelopeBytes;
                    goto Label_0136;

                case State.ReadingEnvelopeBytes:
                    chunkBytesNeeded = size;
                    if (chunkBytesNeeded > this.chunkBytesNeeded)
                    {
                        chunkBytesNeeded = this.chunkBytesNeeded;
                    }
                    this.chunkBytesNeeded -= chunkBytesNeeded;
                    if (this.chunkBytesNeeded == 0)
                    {
                        this.currentState = State.ChunkEnd;
                    }
                    goto Label_0136;

                case State.ChunkEnd:
                    chunkBytesNeeded  = 0;
                    this.currentState = State.ReadingEnvelopeChunkSize;
                    goto Label_0136;

                case State.EnvelopeEnd:
                    base.ValidateRecordType(FramingRecordType.End, (FramingRecordType)bytes[offset]);
                    chunkBytesNeeded  = 1;
                    this.currentState = State.End;
                    goto Label_0136;

                case State.End:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine"))));
                }
                this.currentState     = State.ChunkStart;
                this.chunkBytesNeeded = this.chunkSize;
Label_0136:
                base.StreamPosition += chunkBytesNeeded;
                num2 = chunkBytesNeeded;
            }
            catch (InvalidDataException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(exception));
            }
            return(num2);
        }
        public override int Decode(byte[] bytes, int offset, int size)
        {
            int num2;

            DecoderHelper.ValidateSize(size);
            try
            {
                int num;
                FramingRecordType type;
                switch (base.CurrentState)
                {
                case ClientFramingDecoderState.ReadingUpgradeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.UpgradeResponse)
                    {
                        break;
                    }
                    num = 1;
                    base.CurrentState = ClientFramingDecoderState.UpgradeResponse;
                    goto Label_01A4;

                case ClientFramingDecoderState.UpgradeResponse:
                    num = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingUpgradeRecord;
                    goto Label_01A4;

                case ClientFramingDecoderState.ReadingAckRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.Fault)
                    {
                        goto Label_009A;
                    }
                    num = 1;
                    this.faultDecoder = new FaultStringDecoder();
                    base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                    goto Label_01A4;

                case ClientFramingDecoderState.Start:
                    num = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeRecord;
                    goto Label_01A4;

                case ClientFramingDecoderState.ReadingFault:
                    type = (FramingRecordType)bytes[offset];
                    base.ValidateRecordType(FramingRecordType.Fault, type);
                    num = 1;
                    this.faultDecoder = new FaultStringDecoder();
                    base.CurrentState = ClientFramingDecoderState.ReadingFaultString;
                    goto Label_01A4;

                case ClientFramingDecoderState.ReadingFaultString:
                    num = this.faultDecoder.Decode(bytes, offset, size);
                    if (this.faultDecoder.IsValueDecoded)
                    {
                        base.CurrentState = ClientFramingDecoderState.Fault;
                    }
                    goto Label_01A4;

                case ClientFramingDecoderState.Fault:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                case ClientFramingDecoderState.ReadingEnvelopeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.End)
                    {
                        goto Label_00D4;
                    }
                    num = 1;
                    base.CurrentState = ClientFramingDecoderState.End;
                    goto Label_01A4;

                case ClientFramingDecoderState.EnvelopeStart:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine"))));
                }
                num = 0;
                base.CurrentState = ClientFramingDecoderState.ReadingAckRecord;
                goto Label_01A4;
Label_009A:
                base.ValidatePreambleAck(type);
                num = 1;
                base.CurrentState = ClientFramingDecoderState.Start;
                goto Label_01A4;
Label_00D4:
                if (type == FramingRecordType.Fault)
                {
                    num = 0;
                    base.CurrentState = ClientFramingDecoderState.ReadingFault;
                }
                else
                {
                    base.ValidateRecordType(FramingRecordType.UnsizedEnvelope, type);
                    num = 1;
                    base.CurrentState = ClientFramingDecoderState.EnvelopeStart;
                }
Label_01A4:
                base.StreamPosition += num;
                num2 = num;
            }
            catch (InvalidDataException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(exception));
            }
            return(num2);
        }
Beispiel #11
0
        public override int Decode(byte[] bytes, int offset, int size)
        {
            int num2;

            DecoderHelper.ValidateSize(size);
            try
            {
                int envelopeBytesNeeded;
                FramingRecordType type;
                switch (base.CurrentState)
                {
                case ClientFramingDecoderState.ReadingUpgradeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.UpgradeResponse)
                    {
                        break;
                    }
                    envelopeBytesNeeded = 1;
                    base.CurrentState   = ClientFramingDecoderState.UpgradeResponse;
                    goto Label_0248;

                case ClientFramingDecoderState.UpgradeResponse:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingUpgradeRecord;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingAckRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.Fault)
                    {
                        goto Label_00AA;
                    }
                    envelopeBytesNeeded = 1;
                    this.faultDecoder   = new FaultStringDecoder();
                    base.CurrentState   = ClientFramingDecoderState.ReadingFaultString;
                    goto Label_0248;

                case ClientFramingDecoderState.Start:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEnvelopeRecord;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingFaultString:
                    envelopeBytesNeeded = this.faultDecoder.Decode(bytes, offset, size);
                    if (this.faultDecoder.IsValueDecoded)
                    {
                        base.CurrentState = ClientFramingDecoderState.Fault;
                    }
                    goto Label_0248;

                case ClientFramingDecoderState.Fault:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEndRecord;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingEnvelopeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.End)
                    {
                        goto Label_00E4;
                    }
                    envelopeBytesNeeded = 1;
                    base.CurrentState   = ClientFramingDecoderState.End;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingEnvelopeSize:
                    envelopeBytesNeeded = this.sizeDecoder.Decode(bytes, offset, size);
                    if (this.sizeDecoder.IsValueDecoded)
                    {
                        base.CurrentState        = ClientFramingDecoderState.EnvelopeStart;
                        this.envelopeSize        = this.sizeDecoder.Value;
                        this.envelopeBytesNeeded = this.envelopeSize;
                    }
                    goto Label_0248;

                case ClientFramingDecoderState.EnvelopeStart:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEnvelopeBytes;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingEnvelopeBytes:
                    envelopeBytesNeeded = size;
                    if (envelopeBytesNeeded > this.envelopeBytesNeeded)
                    {
                        envelopeBytesNeeded = this.envelopeBytesNeeded;
                    }
                    this.envelopeBytesNeeded -= envelopeBytesNeeded;
                    if (this.envelopeBytesNeeded == 0)
                    {
                        base.CurrentState = ClientFramingDecoderState.EnvelopeEnd;
                    }
                    goto Label_0248;

                case ClientFramingDecoderState.EnvelopeEnd:
                    envelopeBytesNeeded = 0;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEnvelopeRecord;
                    goto Label_0248;

                case ClientFramingDecoderState.ReadingEndRecord:
                    base.ValidateRecordType(FramingRecordType.End, (FramingRecordType)bytes[offset]);
                    envelopeBytesNeeded = 1;
                    base.CurrentState   = ClientFramingDecoderState.End;
                    goto Label_0248;

                case ClientFramingDecoderState.End:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine"))));
                }
                envelopeBytesNeeded = 0;
                base.CurrentState   = ClientFramingDecoderState.ReadingAckRecord;
                goto Label_0248;
Label_00AA:
                base.ValidatePreambleAck(type);
                envelopeBytesNeeded = 1;
                base.CurrentState   = ClientFramingDecoderState.Start;
                goto Label_0248;
Label_00E4:
                if (type == FramingRecordType.Fault)
                {
                    envelopeBytesNeeded = 1;
                    this.faultDecoder   = new FaultStringDecoder();
                    base.CurrentState   = ClientFramingDecoderState.ReadingFaultString;
                }
                else
                {
                    base.ValidateRecordType(FramingRecordType.SizedEnvelope, type);
                    envelopeBytesNeeded = 1;
                    base.CurrentState   = ClientFramingDecoderState.ReadingEnvelopeSize;
                    this.sizeDecoder.Reset();
                }
Label_0248:
                base.StreamPosition += envelopeBytesNeeded;
                num2 = envelopeBytesNeeded;
            }
            catch (InvalidDataException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(exception));
            }
            return(num2);
        }
        public int Decode(byte[] bytes, int offset, int size)
        {
            int num2;

            DecoderHelper.ValidateSize(size);
            try
            {
                int envelopeBytesNeeded;
                FramingRecordType type;
                switch (this.currentState)
                {
                case State.ReadingViaRecord:
                    type = (FramingRecordType)bytes[offset];
                    base.ValidateRecordType(FramingRecordType.Via, type);
                    envelopeBytesNeeded = 1;
                    this.viaDecoder.Reset();
                    this.currentState = State.ReadingViaString;
                    goto Label_0312;

                case State.ReadingViaString:
                    envelopeBytesNeeded = this.viaDecoder.Decode(bytes, offset, size);
                    if (this.viaDecoder.IsValueDecoded)
                    {
                        this.currentState = State.ReadingContentTypeRecord;
                    }
                    goto Label_0312;

                case State.ReadingContentTypeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.KnownEncoding)
                    {
                        break;
                    }
                    envelopeBytesNeeded = 1;
                    this.currentState   = State.ReadingContentTypeByte;
                    goto Label_0312;

                case State.ReadingContentTypeString:
                    envelopeBytesNeeded = this.contentTypeDecoder.Decode(bytes, offset, size);
                    if (this.contentTypeDecoder.IsValueDecoded)
                    {
                        this.currentState = State.PreUpgradeStart;
                        this.contentType  = this.contentTypeDecoder.Value;
                    }
                    goto Label_0312;

                case State.ReadingContentTypeByte:
                    this.contentType    = ContentTypeStringDecoder.GetString((FramingEncodingType)bytes[offset]);
                    envelopeBytesNeeded = 1;
                    this.currentState   = State.PreUpgradeStart;
                    goto Label_0312;

                case State.PreUpgradeStart:
                    envelopeBytesNeeded = 0;
                    this.currentState   = State.ReadingUpgradeRecord;
                    goto Label_0312;

                case State.ReadingUpgradeRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.UpgradeRequest)
                    {
                        goto Label_0171;
                    }
                    envelopeBytesNeeded = 1;
                    this.contentTypeDecoder.Reset();
                    this.currentState = State.ReadingUpgradeString;
                    goto Label_0312;

                case State.ReadingUpgradeString:
                    envelopeBytesNeeded = this.contentTypeDecoder.Decode(bytes, offset, size);
                    if (this.contentTypeDecoder.IsValueDecoded)
                    {
                        this.currentState = State.UpgradeRequest;
                        this.upgrade      = this.contentTypeDecoder.Value;
                    }
                    goto Label_0312;

                case State.UpgradeRequest:
                    envelopeBytesNeeded = 0;
                    this.currentState   = State.ReadingUpgradeRecord;
                    goto Label_0312;

                case State.ReadingPreambleEndRecord:
                    type = (FramingRecordType)bytes[offset];
                    base.ValidateRecordType(FramingRecordType.PreambleEnd, type);
                    envelopeBytesNeeded = 1;
                    this.currentState   = State.Start;
                    goto Label_0312;

                case State.Start:
                    envelopeBytesNeeded = 0;
                    this.currentState   = State.ReadingEndRecord;
                    goto Label_0312;

                case State.ReadingEnvelopeRecord:
                    base.ValidateRecordType(FramingRecordType.SizedEnvelope, (FramingRecordType)bytes[offset]);
                    envelopeBytesNeeded = 1;
                    this.currentState   = State.ReadingEnvelopeSize;
                    this.sizeDecoder.Reset();
                    goto Label_0312;

                case State.ReadingEnvelopeSize:
                    envelopeBytesNeeded = this.sizeDecoder.Decode(bytes, offset, size);
                    if (this.sizeDecoder.IsValueDecoded)
                    {
                        this.currentState        = State.EnvelopeStart;
                        this.envelopeSize        = this.sizeDecoder.Value;
                        this.envelopeBytesNeeded = this.envelopeSize;
                    }
                    goto Label_0312;

                case State.EnvelopeStart:
                    envelopeBytesNeeded = 0;
                    this.currentState   = State.ReadingEnvelopeBytes;
                    goto Label_0312;

                case State.ReadingEnvelopeBytes:
                    envelopeBytesNeeded = size;
                    if (envelopeBytesNeeded > this.envelopeBytesNeeded)
                    {
                        envelopeBytesNeeded = this.envelopeBytesNeeded;
                    }
                    this.envelopeBytesNeeded -= envelopeBytesNeeded;
                    if (this.envelopeBytesNeeded == 0)
                    {
                        this.currentState = State.EnvelopeEnd;
                    }
                    goto Label_0312;

                case State.EnvelopeEnd:
                    envelopeBytesNeeded = 0;
                    this.currentState   = State.ReadingEndRecord;
                    goto Label_0312;

                case State.ReadingEndRecord:
                    type = (FramingRecordType)bytes[offset];
                    if (type != FramingRecordType.End)
                    {
                        goto Label_020C;
                    }
                    envelopeBytesNeeded = 1;
                    this.currentState   = State.End;
                    goto Label_0312;

                case State.End:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("FramingAtEnd"))));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(new InvalidDataException(System.ServiceModel.SR.GetString("InvalidDecoderStateMachine"))));
                }
                base.ValidateRecordType(FramingRecordType.ExtensibleEncoding, type);
                envelopeBytesNeeded = 1;
                this.contentTypeDecoder.Reset();
                this.currentState = State.ReadingContentTypeString;
                goto Label_0312;
Label_0171:
                envelopeBytesNeeded = 0;
                this.currentState   = State.ReadingPreambleEndRecord;
                goto Label_0312;
Label_020C:
                envelopeBytesNeeded = 0;
                this.currentState   = State.ReadingEnvelopeRecord;
Label_0312:
                base.StreamPosition += envelopeBytesNeeded;
                num2 = envelopeBytesNeeded;
            }
            catch (InvalidDataException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(base.CreateException(exception));
            }
            return(num2);
        }