/// <summary> /// decode literal or (offset, length) from remain /// </summary> private void DecodeRemain() { if (decodeState == DecodeState.Init) { // 0x03 in Binary form is 011, it is the flag // to differ offset and literal if ((remain >> (remainBitsCount - 2)) == 0x3) { decodeState = DecodeState.DecodingOffset; } else { decodeState = DecodeState.DecodingLiteral; } } else if (decodeState == DecodeState.DecodingLength) { DecodeLength(); } else if (decodeState == DecodeState.DecodingOffset) { DecodeOffset(); } else if (decodeState == DecodeState.DecodingLiteral) { DecodeLiteral(); } else { //never go into this path } }
/// <summary> /// Decode Literal from remain /// </summary> private void DecodeLiteral() { needMoreData = true; if ((remain >> (remainBitsCount - (int)LiteralSymbolBitSize.MoreThanHex7f)) == (uint)LiteralSymbol.MoreThanHex7f) { //>0x7F if (remainBitsCount >= (int)LiteralBitSize.MoreThanHex7f) { GetLiteralFromRemain(LiteralBitSize.MoreThanHex7f); needMoreData = false; } } else { //<0x80 GetLiteralFromRemain(LiteralBitSize.LessThanHex80); needMoreData = false; } if (!needMoreData) { outputStream.WriteByte((byte)literal); historyBuffer.Update((byte)literal); decodeState = DecodeState.Init; } }
void ResetState(DecodeState state) { _buffer.Clear(); _decodeState = state; _bytesRead = 0; _bytesExpected = 0; }
/// <summary> /// Decodes the expression back into a LINQ expression. /// </summary> /// <param name="expression">The encoded expression to be decoded.</param> /// <param name="state">The state of the decode operation.</param> /// <param name="options">The options that will be used during decoding.</param> /// <returns>A LINQ <see cref="Expression"/> instance.</returns> internal static Expression DecodeExpression(EncodedExpression expression, DecodeState state, DecodeOptions options) { if (!ExpressionTypeMapping.ContainsKey(expression.NodeType)) { throw new ArgumentException($"Encountered unknown node type {expression.NodeType} when decoding expression.", nameof(expression)); } return(ExpressionTypeMapping[expression.NodeType].Decode(expression, state, options)); }
/// <summary> /// Decode offset when sliding window is 64k /// </summary> private void DecodeOffset64K() { needMoreData = true; if ((remain >> (remainBitsCount - (int)OffsetSymbolBitSize64k.LessThan320)) == (uint)OffsetSymbol64k.LessThan64) { //<64 if (remainBitsCount >= (int)OffsetBitSize64k.LessThan64) { GetOffsetFromRemain64K(OffsetBitSize64k.LessThan64, OffsetBitSizeWithoutSymbol64k.LessThan64); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)OffsetSymbolBitSize64k.LessThan320)) == (uint)OffsetSymbol64k.Between64And319) { // 64-320 if (remainBitsCount >= (int)OffsetBitSize64k.Between64And319) { GetOffsetFromRemain64K(OffsetBitSize64k.Between64And319, OffsetBitSizeWithoutSymbol64k.Between64And319); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)OffsetSymbolBitSize64k.Between320And2367)) == (uint)OffsetSymbol64k.Between320And2367) { // 320-2368 if (remainBitsCount >= (int)OffsetBitSize64k.Between320And2367) { GetOffsetFromRemain64K(OffsetBitSize64k.Between320And2367, OffsetBitSizeWithoutSymbol64k.Between320And2367); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)OffsetSymbolBitSize64k.LargerThan2367)) == (uint)OffsetSymbol64k.LargerThan2368) { // >2367 if (remainBitsCount >= (int)OffsetBitSize64k.LargerThan2367) { GetOffsetFromRemain64K(OffsetBitSize64k.LargerThan2367, OffsetBitSizeWithoutSymbol64k.LargerThan2368); needMoreData = false; } } else { throw new InvalidDataException("The compressed data format is not right"); } if (!needMoreData) { decodeState = DecodeState.DecodingLength; } }
/// <summary> /// Initialize all field of this class instance /// </summary> private void InitializeAll() { historyBuffer.Clear(); decodeState = DecodeState.Init; needMoreData = false; remain = 0; remainBitsCount = 0; offset = 0; length = 0; literal = 0; }
public WebSocketSession(long id, TcpClient tcpClient, Stream stream, Action<int> onBytesReceived, Action<int> onBytesSent, Action onDisposed, int initialReadBufferSize=1024, int writeTimeoutMs=5000) : base(id, tcpClient, stream, initialReadBufferSize, writeTimeoutMs) { this.onBytesReceived = onBytesReceived; this.onBytesSent = onBytesSent; this.onDisposed = onDisposed; decodeState = DecodeState.DecodeHeader; dataStart = 0; mask = new byte[4]; }
/// <summary> /// Decode offset when sliding window is 8k /// </summary> private void DecodeOffset8K() { needMoreData = true; if ((remain >> (remainBitsCount - (int)OffsetSymbolBitSize8k.LessThan320)) == (uint)OffsetSymbol8k.LessThan64) { // <64 if (remainBitsCount >= (uint)OffsetBitSize8k.LessThan64) { GetOffsetFromRemain8K(OffsetBitSize8k.LessThan64, OffsetBitSizeWithoutSymbol8k.LessThan64); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)OffsetSymbolBitSize8k.LessThan320)) == (uint)OffsetSymbol8k.Between64And320) { //64 - 320 if (remainBitsCount >= (uint)OffsetBitSize8k.Between64And320) { GetOffsetFromRemain8K(OffsetBitSize8k.Between64And320, OffsetBitSizeWithoutSymbol8k.Between64And320); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)OffsetSymbolBitSize8k.Between320And8191)) == (uint)OffsetSymbol8k.Between320And8191) { //320 - 8191 if (remainBitsCount >= (uint)OffsetBitSize8k.Between320And8191) { GetOffsetFromRemain8K(OffsetBitSize8k.Between320And8191, OffsetBitSizeWithoutSymbol8k.Between320And8191); needMoreData = false; } } else { throw new InvalidDataException("The compressed data format is not right"); } if (!needMoreData) { decodeState = DecodeState.DecodingLength; } }
public WebSocketSession(long id, TcpClient tcpClient, Stream stream, Action <int> onBytesReceived, Action <int> onBytesSent, Action onDisposed, int initialReadBufferSize, int readTimeoutMs, int writeTimeoutMs) : base(id, tcpClient, stream, initialReadBufferSize, readTimeoutMs, writeTimeoutMs) { this.onBytesReceived = onBytesReceived; this.onBytesSent = onBytesSent; this.onDisposed = onDisposed; decodeState = DecodeState.DecodeHeader; dataStart = 0; mask = new byte[4]; }
/// <summary> /// constructor with mode specified /// </summary> /// <param name="mode">indicates which mode are used</param> public Decompressor(SlidingWindowSize mode) { this.mode = mode; if (mode == SlidingWindowSize.EightKB) { historyBuffer = new SlidingWindow(window8k); } else if (mode == SlidingWindowSize.SixtyFourKB) { historyBuffer = new SlidingWindow(window64k); } else { throw new ArgumentException( "mode should be EightKB or SixtyFourKB", "mode"); } decodeState = DecodeState.Init; }
/// <summary> /// 接收数据译码函数 /// </summary> public void GetPortData() { //test function to display any log information from Serial Ports List <byte> buffer = new List <byte>(); //this gives easy access to handy functions in order to process buffer Queue <byte> FIFO_Buffer = new Queue <byte>(); //create FIFO type bufffer CommunicationProtocol.DataPacket packet = new CommunicationProtocol.DataPacket(); packet.dataReady = false; DecodeState currentDecodeState = DecodeState.SearchForHeader1; bool ThreadRunning = true; while (controlPort.IsOpen && ThreadRunning) { try { int data = controlPort.ReadByte(); if (data != -1) { FIFO_Buffer.Enqueue((byte)data); } ProcessData(ref FIFO_Buffer, ref currentDecodeState, ref packet); if (packet.dataReady) { //raise event if (MsgRecieved != null) { MsgRecieved(this, new MsgEventArgs() { msg = packet }); //raise event } packet = new CommunicationProtocol.DataPacket(); packet.dataReady = false; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); ThreadRunning = false; } } }
/// <summary> /// Decodes the parameter expression. /// </summary> /// <param name="expression">The expression to be decoded.</param> /// <param name="state">The current state of the decode operation.</param> /// <param name="options">The options for the decode operation.</param> /// <returns>An <see cref="Expression"/> object.</returns> internal static Expression DecodeParameterExpression(EncodedExpression expression, DecodeState state, DecodeOptions options) { return(state.GetOrAddParameter(expression)); }
private void DecodeLength() { uint lengthFlag = 0; if (remainBitsCount > oneByteBitsCount) { lengthFlag = remain >> (remainBitsCount - oneByteBitsCount); } else { lengthFlag = remain << (oneByteBitsCount - remainBitsCount); } //11111111 can be use to differ lengh > 512 or <= 512 if (lengthFlag == 0xff) { //length value from 512 to 65536 if (remainBitsCount > (2*oneByteBitsCount)) { needMoreData = true; if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between512And1023)) == (uint)LengthSymbol.Between512And1023) { //512-1023 if (remainBitsCount >= (int)LengthBitSize.Between512And1023) { GetLengthFromRemain(LengthBitSize.Between512And1023); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between1024And2047)) == (uint)LengthSymbol.Between1024And2047) { //1024-2047 if (remainBitsCount >= (int)LengthBitSize.Between1024And2047) { GetLengthFromRemain(LengthBitSize.Between1024And2047); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between2048And4095)) == (uint)LengthSymbol.Between2048And4095) { //2048-4095 if (remainBitsCount >= (int)LengthBitSize.Between2048And4095) { GetLengthFromRemain(LengthBitSize.Between2048And4095); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between4096And8191)) == (uint)LengthSymbol.Between4096And8191) { //4096-8191 if (remainBitsCount >= (int)LengthBitSize.Between4096And8191) { GetLengthFromRemain(LengthBitSize.Between4096And8191); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between8192And16383)) == (uint)LengthSymbol.Between8192And16383) { //8192-16383 if (remainBitsCount >= (int)LengthBitSize.Between8192And16383) { GetLengthFromRemain(LengthBitSize.Between8192And16383); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between16384And32767)) == (uint)LengthSymbol.Between16384And32767) { //16384-32767 if (remainBitsCount >= (int)LengthBitSize.Between16384And32767) { GetLengthFromRemain(LengthBitSize.Between16384And32767); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between32768And65535)) == (uint)LengthSymbol.Between32768And65535) { //32767-65535 if (remainBitsCount >= (int)LengthBitSize.Between32768And65535) { GetLengthFromRemain(LengthBitSize.Between32768And65535); needMoreData = false; } } else { throw new InvalidDataException("The compressed data format is not right"); } } else { needMoreData = true; } } else { //length value from 3 to 512 if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between0And3)) == (uint)LengthSymbol.Between0And3) { //3 length = minimumEncodeLength; remainBitsCount -= (int)LengthSymbolBitSize.Between0And3; //elimate the decoded bit from remain remain = remain & (uint)((1 << remainBitsCount) - 1); } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between4And7)) == (uint)LengthSymbol.Between4And7) { //4-7 GetLengthFromRemain(LengthBitSize.Between4And7); } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between8And15)) == (uint)LengthSymbol.Between8And15) { //8-15 GetLengthFromRemain(LengthBitSize.Between8And15); } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between16And31)) == (uint)LengthSymbol.Between16And31) { //16-31 GetLengthFromRemain(LengthBitSize.Between16And31); } else { needMoreData = true; //32-511 if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between32And63)) == (uint)LengthSymbol.Between32And63) { //32-63 if (remainBitsCount >= (int)LengthBitSize.Between32And63) { GetLengthFromRemain(LengthBitSize.Between32And63); needMoreData = false; } } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between64And127)) == (uint)LengthSymbol.Between64And127) { //64-127 if (remainBitsCount >= (int)LengthBitSize.Between64And127) { GetLengthFromRemain(LengthBitSize.Between64And127); needMoreData = false; } } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between128And255)) == (uint)LengthSymbol.Between128And255) { //128-255 if (remainBitsCount >= (int)LengthBitSize.Between128And255) { GetLengthFromRemain(LengthBitSize.Between128And255); needMoreData = false; } } else if (lengthFlag == (uint)LengthSymbol.Between256And511) { //256-511 if (remainBitsCount >= (int)LengthBitSize.Between256And511) { GetLengthFromRemain(LengthBitSize.Between256And511); needMoreData = false; } } else { throw new InvalidDataException("The compressed data format is not right"); } } } // if length has been processed, we can get the refferd data if (!needMoreData) { byte[] refferedData = historyBuffer.GetMatchedData(offset, length); outputStream.Write(refferedData, 0, refferedData.Length); historyBuffer.Update(refferedData); decodeState = DecodeState.Init; } }
bool TryDecodeNextMessage(out OpCode opCode, out byte[] messagePayload) { while (true) { switch (decodeState) { case DecodeState.DecodeHeader: if (FrameDecoder.TryDecodeHeader(readBuffer, ref dataStart, readBufferOffset, out isFin, out frameOpCode, out isMasked, out framePayloadLength, mask)) { decodeState = DecodeState.DecodePayload; switch (frameOpCode) { case OpCode.Continuation: if (framePayloadLength > 0) { BufferUtils.Expand(ref messageBuffer, framePayloadLength); //TODO: limit, sanity guard } break; case OpCode.Text: case OpCode.Binary: messageOpCode = frameOpCode; messageBuffer = new byte[framePayloadLength]; messageBufferOffset = 0; break; case OpCode.Close: case OpCode.Ping: case OpCode.Pong: if (!isFin) { throw new WebSocketException("Control frame cannot be fragmented"); } break; } continue; } break; case DecodeState.DecodePayload: var decodeStart = dataStart; if (FrameDecoder.TryDecodePayload(readBuffer, ref dataStart, readBufferOffset, framePayloadLength, isMasked, mask)) { decodeState = DecodeState.DecodeHeader; switch (frameOpCode) { case OpCode.Continuation: case OpCode.Text: case OpCode.Binary: Array.Copy(readBuffer, decodeStart, messageBuffer, messageBufferOffset, framePayloadLength); CompactReadBuffer(ref dataStart); if (isFin) { opCode = messageOpCode; messagePayload = messageBuffer; messageBuffer = null; messageBufferOffset = 0; return(true); } else { messageBufferOffset += framePayloadLength; continue; } case OpCode.Close: case OpCode.Ping: case OpCode.Pong: opCode = frameOpCode; messagePayload = new byte[framePayloadLength]; Array.Copy(readBuffer, decodeStart, messagePayload, 0, framePayloadLength); CompactReadBuffer(ref dataStart); return(true); // must be Fin } } break; } opCode = OpCode.Continuation; messagePayload = null; return(false); } }
/// <summary> /// Decodes the unary expression. /// </summary> /// <param name="expression">The expression to be decoded.</param> /// <param name="state">The current state of the decode operation.</param> /// <param name="options">The options for the decode operation.</param> /// <returns>An <see cref="Expression"/> object.</returns> internal static Expression DecodeUnaryExpression(EncodedExpression expression, DecodeState state, DecodeOptions options) { var operand = DecodeExpression(expression.Expressions["Operand"], state, options); var type = state.SignatureHelper.GetTypeFromSignature(expression.Values["Type"]); return(Expression.MakeUnary(expression.NodeType, operand, type)); }
/// <summary> /// 接收数据包格式处理 /// </summary> /// <param name="dataStream"></param> /// <param name="state"></param> /// <param name="packet"></param> public void ProcessData(ref Queue <byte> dataStream, ref DecodeState state, ref CommunicationProtocol.DataPacket packet) { //Todo Process fifo data stream from headtracking port bool stopSerching = false; while (dataStream.Count > 0 && stopSerching == false) { switch (state) { case DecodeState.SearchForHeader1: if (dataStream.Count > 0) { byte data = dataStream.Dequeue(); if (data == 0xeb) { state = DecodeState.SearchForHeader2; } } break; case DecodeState.SearchForHeader2: if (dataStream.Count > 0) { byte data = dataStream.Dequeue(); if (data == 0x90) { state = DecodeState.SearchForCmdType; } } break; case DecodeState.SearchForCmdType: if (dataStream.Count > 0) { byte data = dataStream.Dequeue(); packet.cmdType = (CommunicationProtocol.CMDTYPE)data; state = DecodeState.SearchForLen; } break; case DecodeState.SearchForLen: if (dataStream.Count > 0) { byte data = dataStream.Dequeue(); packet.len = data; state = DecodeState.GetContent; } break; case DecodeState.GetContent: if (dataStream.Count >= packet.len) { packet.payload = new byte[packet.len]; for (int i = 0; i < packet.len; i++) { packet.payload[i] = dataStream.Dequeue(); } state = DecodeState.CheckEnder; } else { stopSerching = true; } break; case DecodeState.CheckEnder: if (dataStream.Count > 0) { byte data = dataStream.Dequeue(); if (data == 0xfe) { packet.dataReady = true; state = DecodeState.SearchForHeader1; stopSerching = true; } else { packet = new CommunicationProtocol.DataPacket(); state = DecodeState.SearchForHeader1; stopSerching = true; } } break; } } }
/// <summary> /// Decodes the method call expression. /// </summary> /// <param name="expression">The expression to be decoded.</param> /// <param name="state">The current state of the decode operation.</param> /// <param name="options">The options for the decode operation.</param> /// <returns>An <see cref="Expression"/> object.</returns> internal static Expression DecodeMethodCallExpression(EncodedExpression expression, DecodeState state, DecodeOptions options) { var arguments = new List <Expression>(); Expression objectExpression = null; if (expression.Expressions["Object"] != null) { objectExpression = DecodeExpression(expression.Expressions["Object"], state, options); } int argumentCount = Convert.ToInt32(expression.Values["ArgumentCount"]); for (int i = 0; i < argumentCount; i++) { arguments.Add(DecodeExpression(expression.Expressions[$"a{i}"], state, options)); } var methodInfo = state.SignatureHelper.GetMethodInfoFromSignature(expression.Values["Method"]); if (methodInfo == null) { throw new MethodNotFoundException("A matching method was not found."); } if (!options.IsMethodSafe(methodInfo)) { throw new UnsafeMethodCallException("Attempted to decode an unsafe method call.", methodInfo); } return(Expression.Call(objectExpression, methodInfo, arguments)); }
/// <summary> /// Decodes the member expression. /// </summary> /// <param name="expression">The expression to be decoded.</param> /// <param name="state">The current state of the decode operation.</param> /// <param name="options">The options for the decode operation.</param> /// <returns>An <see cref="Expression"/> object.</returns> internal static Expression DecodeMemberExpression(EncodedExpression expression, DecodeState state, DecodeOptions options) { MemberInfo memberInfo; Type type = state.SignatureHelper.GetTypeFromSignature(expression.Values["Type"]); if (bool.Parse(expression.Values["IsProperty"])) { memberInfo = type.GetProperty(expression.Values["Member"]); } else { memberInfo = type.GetField(expression.Values["Member"]); } var expr = DecodeExpression(expression.Expressions["Expression"], state, options); return(Expression.MakeMemberAccess(expr, memberInfo)); }
/// <summary> /// Decodes the constant expression. /// </summary> /// <param name="expression">The expression to be decoded.</param> /// <param name="state">The current state of the decode operation.</param> /// <param name="options">The options for the decode operation.</param> /// <returns>An <see cref="Expression"/> object.</returns> internal static Expression DecodeConstantExpression(EncodedExpression expression, DecodeState state, DecodeOptions options) { var type = state.SignatureHelper.GetTypeFromSignature(expression.Values["Type"]); var Value = expression.Values["Value"]; return(Expression.Constant(Convert.ChangeType(Value, type))); }
/// <summary> /// Decodes the binary expression. /// </summary> /// <param name="expression">The expression to be decoded.</param> /// <param name="state">The current state of the decode operation.</param> /// <param name="options">The options for the decode operation.</param> /// <returns>An <see cref="Expression"/> object.</returns> internal static Expression DecodeBinaryExpression(EncodedExpression expression, DecodeState state, DecodeOptions options) { var left = DecodeExpression(expression.Expressions["Left"], state, options); var right = DecodeExpression(expression.Expressions["Right"], state, options); if (expression.Expressions["Conversion"] == null) { return(Expression.MakeBinary(expression.NodeType, left, right)); } else { var conversion = ( LambdaExpression )DecodeExpression(expression.Expressions["Conversion"], state, options); return(Expression.MakeBinary(expression.NodeType, left, right, false, null, conversion)); } }
bool TryDecodeNextMessage(out OpCode opCode, out byte[] messagePayload) { while (true) { switch (decodeState) { case DecodeState.DecodeHeader: if (FrameDecoder.TryDecodeHeader(readBuffer, ref dataStart, readBufferOffset, out isFin, out frameOpCode, out isMasked, out framePayloadLength, mask)) { decodeState = DecodeState.DecodePayload; switch (frameOpCode) { case OpCode.Continuation: if (framePayloadLength > 0) { BufferUtils.Expand(ref messageBuffer, framePayloadLength); //TODO: limit, sanity guard } break; case OpCode.Text: case OpCode.Binary: messageOpCode = frameOpCode; messageBuffer = new byte[framePayloadLength]; messageBufferOffset = 0; break; case OpCode.Close: case OpCode.Ping: case OpCode.Pong: if (!isFin) { throw new WebSocketException("Control frame cannot be fragmented"); } break; } continue; } break; case DecodeState.DecodePayload: var decodeStart = dataStart; if (FrameDecoder.TryDecodePayload(readBuffer, ref dataStart, readBufferOffset, framePayloadLength, isMasked, mask)) { decodeState = DecodeState.DecodeHeader; switch (frameOpCode) { case OpCode.Continuation: case OpCode.Text: case OpCode.Binary: Array.Copy(readBuffer, decodeStart, messageBuffer, messageBufferOffset, framePayloadLength); CompactReadBuffer(ref dataStart); if (isFin) { opCode = messageOpCode; messagePayload = messageBuffer; messageBuffer = null; messageBufferOffset = 0; return true; } else { messageBufferOffset += framePayloadLength; continue; } case OpCode.Close: case OpCode.Ping: case OpCode.Pong: opCode = frameOpCode; messagePayload = new byte[framePayloadLength]; Array.Copy(readBuffer, decodeStart, messagePayload, 0, framePayloadLength); CompactReadBuffer(ref dataStart); return true; // must be Fin } } break; } opCode = OpCode.Continuation; messagePayload = null; return false; } }
/// <summary> /// Decodes the lambda expression. /// </summary> /// <param name="expression">The expression to be decoded.</param> /// <param name="state">The current state of the decode operation.</param> /// <param name="options">The options for the decode operation.</param> /// <returns>An <see cref="Expression"/> object.</returns> internal static Expression DecodeLambdaExpression(EncodedExpression expression, DecodeState state, DecodeOptions options) { var body = DecodeExpression(expression.Expressions["Body"], state, options); var parameterCount = Convert.ToInt32(expression.Values["ParameterCount"]); var parameters = new List <ParameterExpression>(); for (int i = 0; i < parameterCount; i++) { parameters.Add(( ParameterExpression )DecodeExpression(expression.Expressions[$"p{i}"], state, options)); } return(Expression.Lambda(body, parameters.ToArray())); }
private void DecodeLength() { uint lengthFlag = 0; if (remainBitsCount > oneByteBitsCount) { lengthFlag = remain >> (remainBitsCount - oneByteBitsCount); } else { lengthFlag = remain << (oneByteBitsCount - remainBitsCount); } //11111111 can be use to differ lengh > 512 or <= 512 if (lengthFlag == 0xff) { //length value from 512 to 65536 if (remainBitsCount > (2 * oneByteBitsCount)) { needMoreData = true; if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between512And1023)) == (uint)LengthSymbol.Between512And1023) { //512-1023 if (remainBitsCount >= (int)LengthBitSize.Between512And1023) { GetLengthFromRemain(LengthBitSize.Between512And1023); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between1024And2047)) == (uint)LengthSymbol.Between1024And2047) { //1024-2047 if (remainBitsCount >= (int)LengthBitSize.Between1024And2047) { GetLengthFromRemain(LengthBitSize.Between1024And2047); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between2048And4095)) == (uint)LengthSymbol.Between2048And4095) { //2048-4095 if (remainBitsCount >= (int)LengthBitSize.Between2048And4095) { GetLengthFromRemain(LengthBitSize.Between2048And4095); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between4096And8191)) == (uint)LengthSymbol.Between4096And8191) { //4096-8191 if (remainBitsCount >= (int)LengthBitSize.Between4096And8191) { GetLengthFromRemain(LengthBitSize.Between4096And8191); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between8192And16383)) == (uint)LengthSymbol.Between8192And16383) { //8192-16383 if (remainBitsCount >= (int)LengthBitSize.Between8192And16383) { GetLengthFromRemain(LengthBitSize.Between8192And16383); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between16384And32767)) == (uint)LengthSymbol.Between16384And32767) { //16384-32767 if (remainBitsCount >= (int)LengthBitSize.Between16384And32767) { GetLengthFromRemain(LengthBitSize.Between16384And32767); needMoreData = false; } } else if ((remain >> (remainBitsCount - (int)LengthSymbolBitSize.Between32768And65535)) == (uint)LengthSymbol.Between32768And65535) { //32767-65535 if (remainBitsCount >= (int)LengthBitSize.Between32768And65535) { GetLengthFromRemain(LengthBitSize.Between32768And65535); needMoreData = false; } } else { throw new InvalidDataException("The compressed data format is not right"); } } else { needMoreData = true; } } else { //length value from 3 to 512 if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between0And3)) == (uint)LengthSymbol.Between0And3) { //3 length = minimumEncodeLength; remainBitsCount -= (int)LengthSymbolBitSize.Between0And3; //elimate the decoded bit from remain remain = remain & (uint)((1 << remainBitsCount) - 1); } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between4And7)) == (uint)LengthSymbol.Between4And7) { //4-7 GetLengthFromRemain(LengthBitSize.Between4And7); } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between8And15)) == (uint)LengthSymbol.Between8And15) { //8-15 GetLengthFromRemain(LengthBitSize.Between8And15); } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between16And31)) == (uint)LengthSymbol.Between16And31) { //16-31 GetLengthFromRemain(LengthBitSize.Between16And31); } else { needMoreData = true; //32-511 if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between32And63)) == (uint)LengthSymbol.Between32And63) { //32-63 if (remainBitsCount >= (int)LengthBitSize.Between32And63) { GetLengthFromRemain(LengthBitSize.Between32And63); needMoreData = false; } } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between64And127)) == (uint)LengthSymbol.Between64And127) { //64-127 if (remainBitsCount >= (int)LengthBitSize.Between64And127) { GetLengthFromRemain(LengthBitSize.Between64And127); needMoreData = false; } } else if ((lengthFlag >> (oneByteBitsCount - (int)LengthSymbolBitSize.Between128And255)) == (uint)LengthSymbol.Between128And255) { //128-255 if (remainBitsCount >= (int)LengthBitSize.Between128And255) { GetLengthFromRemain(LengthBitSize.Between128And255); needMoreData = false; } } else if (lengthFlag == (uint)LengthSymbol.Between256And511) { //256-511 if (remainBitsCount >= (int)LengthBitSize.Between256And511) { GetLengthFromRemain(LengthBitSize.Between256And511); needMoreData = false; } } else { throw new InvalidDataException("The compressed data format is not right"); } } } // if length has been processed, we can get the refferd data if (!needMoreData) { byte[] refferedData = historyBuffer.GetMatchedData(offset, length); outputStream.Write(refferedData, 0, refferedData.Length); historyBuffer.Update(refferedData); decodeState = DecodeState.Init; } }