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 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 = 1; _faultDecoder = new FaultStringDecoder(); base.CurrentState = ClientFramingDecoderState.ReadingFaultString; break; } ValidateRecordType(FramingRecordType.SizedEnvelope, recordType); bytesConsumed = 1; base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeSize; _sizeDecoder.Reset(); break; case ClientFramingDecoderState.ReadingEnvelopeSize: bytesConsumed = _sizeDecoder.Decode(bytes, offset, size); if (_sizeDecoder.IsValueDecoded) { base.CurrentState = ClientFramingDecoderState.EnvelopeStart; _envelopeSize = _sizeDecoder.Value; _envelopeBytesNeeded = _envelopeSize; } break; case ClientFramingDecoderState.EnvelopeStart: bytesConsumed = 0; base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeBytes; break; case ClientFramingDecoderState.ReadingEnvelopeBytes: bytesConsumed = size; if (bytesConsumed > _envelopeBytesNeeded) { bytesConsumed = _envelopeBytesNeeded; } _envelopeBytesNeeded -= bytesConsumed; if (_envelopeBytesNeeded == 0) { base.CurrentState = ClientFramingDecoderState.EnvelopeEnd; } break; case ClientFramingDecoderState.EnvelopeEnd: bytesConsumed = 0; base.CurrentState = ClientFramingDecoderState.ReadingEnvelopeRecord; break; case ClientFramingDecoderState.ReadingFaultString: bytesConsumed = _faultDecoder.Decode(bytes, offset, size); if (_faultDecoder.IsValueDecoded) { base.CurrentState = ClientFramingDecoderState.Fault; } break; case ClientFramingDecoderState.Fault: bytesConsumed = 0; base.CurrentState = ClientFramingDecoderState.ReadingEndRecord; break; case ClientFramingDecoderState.ReadingEndRecord: ValidateRecordType(FramingRecordType.End, (FramingRecordType)bytes[offset]); bytesConsumed = 1; base.CurrentState = ClientFramingDecoderState.End; break; case ClientFramingDecoderState.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) { 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)); } }
private ArraySegment <byte> ExtractSessionInformationFromMessage(ArraySegment <byte> messageData) { int num3; int num4; if (this.isReaderSessionInvalid) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("BinaryEncoderSessionInvalid"))); } byte[] array = messageData.Array; bool flag = true; try { IntDecoder decoder = new IntDecoder(); int num2 = decoder.Decode(array, messageData.Offset, messageData.Count); int num = decoder.Value; if (num > messageData.Count) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("BinaryEncoderSessionMalformed"))); } num3 = (messageData.Offset + num2) + num; num4 = (messageData.Count - num2) - num; if (num4 < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("BinaryEncoderSessionMalformed"))); } if (num > 0) { if (num > this.remainingReaderSessionSize) { string message = System.ServiceModel.SR.GetString("BinaryEncoderSessionTooLarge", new object[] { this.maxSessionSize }); Exception innerException = new QuotaExceededException(message); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(message, innerException)); } this.remainingReaderSessionSize -= num; int size = num; int offset = messageData.Offset + num2; while (size > 0) { decoder.Reset(); int num7 = decoder.Decode(array, offset, size); int count = decoder.Value; offset += num7; size -= num7; if (count > size) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("BinaryEncoderSessionMalformed"))); } string str2 = Encoding.UTF8.GetString(array, offset, count); offset += count; size -= count; this.readerSession.Add(this.idCounter, str2); this.idCounter++; } } flag = false; } finally { if (flag) { this.isReaderSessionInvalid = true; } } return(new ArraySegment <byte>(array, num3, num4)); }
private ArraySegment<byte> ExtractSessionInformationFromMessage(ArraySegment<byte> messageData) { if (_isReaderSessionInvalid) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(SR.BinaryEncoderSessionInvalid)); } byte[] buffer = messageData.Array; int dictionarySize; int headerSize; int newOffset; int newSize; bool throwing = true; try { IntDecoder decoder = new IntDecoder(); headerSize = decoder.Decode(buffer, messageData.Offset, messageData.Count); dictionarySize = decoder.Value; if (dictionarySize > messageData.Count) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(SR.BinaryEncoderSessionMalformed)); } newOffset = messageData.Offset + headerSize + dictionarySize; newSize = messageData.Count - headerSize - dictionarySize; if (newSize < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(SR.BinaryEncoderSessionMalformed)); } if (dictionarySize > 0) { if (dictionarySize > _remainingReaderSessionSize) { string message = SR.Format(SR.BinaryEncoderSessionTooLarge, _maxSessionSize); if (WcfEventSource.Instance.MaxSessionSizeReachedIsEnabled()) { WcfEventSource.Instance.MaxSessionSizeReached(message); } Exception inner = new QuotaExceededException(message); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(message, inner)); } else { _remainingReaderSessionSize -= dictionarySize; } int size = dictionarySize; int offset = messageData.Offset + headerSize; while (size > 0) { decoder.Reset(); int bytesDecoded = decoder.Decode(buffer, offset, size); int utf8ValueSize = decoder.Value; offset += bytesDecoded; size -= bytesDecoded; if (utf8ValueSize > size) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(SR.BinaryEncoderSessionMalformed)); } string value = Encoding.UTF8.GetString(buffer, offset, utf8ValueSize); offset += utf8ValueSize; size -= utf8ValueSize; _readerSession.Add(_idCounter, value); _idCounter++; } } throwing = false; } finally { if (throwing) { _isReaderSessionInvalid = true; } } return new ArraySegment<byte>(buffer, newOffset, newSize); }
private ArraySegment<byte> ExtractSessionInformationFromMessage(ArraySegment<byte> messageData) { int num3; int num4; if (this.isReaderSessionInvalid) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("BinaryEncoderSessionInvalid"))); } byte[] array = messageData.Array; bool flag = true; try { IntDecoder decoder = new IntDecoder(); int num2 = decoder.Decode(array, messageData.Offset, messageData.Count); int num = decoder.Value; if (num > messageData.Count) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("BinaryEncoderSessionMalformed"))); } num3 = (messageData.Offset + num2) + num; num4 = (messageData.Count - num2) - num; if (num4 < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("BinaryEncoderSessionMalformed"))); } if (num > 0) { if (num > this.remainingReaderSessionSize) { string message = System.ServiceModel.SR.GetString("BinaryEncoderSessionTooLarge", new object[] { this.maxSessionSize }); Exception innerException = new QuotaExceededException(message); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(message, innerException)); } this.remainingReaderSessionSize -= num; int size = num; int offset = messageData.Offset + num2; while (size > 0) { decoder.Reset(); int num7 = decoder.Decode(array, offset, size); int count = decoder.Value; offset += num7; size -= num7; if (count > size) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(System.ServiceModel.SR.GetString("BinaryEncoderSessionMalformed"))); } string str2 = Encoding.UTF8.GetString(array, offset, count); offset += count; size -= count; this.readerSession.Add(this.idCounter, str2); this.idCounter++; } } flag = false; } finally { if (flag) { this.isReaderSessionInvalid = true; } } return new ArraySegment<byte>(array, num3, num4); }