private static TryShiftResult TryShift(Context bc, Byte[] Buffer, int Position, int Length) { if (bc.State == 0) { if (Length >= 4) { using (var s = new ByteArrayStream(Buffer, Position, Length)) { bc.CommandNameLength = s.ReadInt32(); } if (bc.CommandNameLength < 0 || bc.CommandNameLength > 128) { throw new InvalidOperationException("CommandNameLengthOutOfBound"); } var r = new TryShiftResult { Command = null, Position = Position + 4 }; bc.InputCommandByteLength += 4; bc.State = 1; return(r); } return(null); } else if (bc.State == 1) { if (Length >= bc.CommandNameLength) { using (var s = new ByteArrayStream(Buffer, Position, Length)) { bc.CommandName = TextEncoding.UTF16.GetString(s.Read(bc.CommandNameLength)); } var r = new TryShiftResult { Command = null, Position = Position + bc.CommandNameLength }; bc.InputCommandByteLength += bc.CommandNameLength; bc.State = 2; return(r); } return(null); } else if (bc.State == 2) { if (Length >= 4) { using (var s = new ByteArrayStream(Buffer, Position, Length)) { bc.CommandHash = s.ReadUInt32(); } var r = new TryShiftResult { Command = null, Position = Position + 4 }; bc.InputCommandByteLength += 4; bc.State = 3; return(r); } return(null); } if (bc.State == 3) { if (Length >= 4) { using (var s = new ByteArrayStream(Buffer, Position, Length)) { bc.ParametersLength = s.ReadInt32(); } if (bc.ParametersLength < 0 || bc.ParametersLength > Buffer.Length) { throw new InvalidOperationException("ParametersLengthOutOfBound"); } var r = new TryShiftResult { Command = null, Position = Position + 4 }; bc.InputCommandByteLength += 4; bc.State = 4; return(r); } return(null); } else if (bc.State == 4) { if (Length >= bc.ParametersLength) { Byte[] Parameters; using (var s = new ByteArrayStream(Buffer, Position, Length)) { Parameters = s.Read(bc.ParametersLength); } bc.InputCommandByteLength += bc.ParametersLength; var cmd = new Command { CommandName = bc.CommandName, CommandHash = bc.CommandHash, Parameters = Parameters, ByteLength = bc.InputCommandByteLength }; var r = new TryShiftResult { Command = cmd, Position = Position + bc.ParametersLength }; bc.CommandNameLength = 0; bc.CommandName = null; bc.CommandHash = 0; bc.ParametersLength = 0; bc.InputCommandByteLength = 0; bc.State = 0; return(r); } return(null); } else { throw new InvalidOperationException(); } }
public TryShiftResult TryShift(Byte[] Buffer, int Position, int Length) { if (State == 0) { if (Length >= 4) { using (var s = new ByteArrayStream(Buffer, Position, Length)) { CommandNameLength = s.ReadInt32(); } if (CommandNameLength < 0 || CommandNameLength > 128) { throw new InvalidOperationException(); } var r = new TryShiftResult { Command = null, Position = Position + 4 }; State = 1; return(r); } return(null); } else if (State == 1) { if (Length >= CommandNameLength) { using (var s = new ByteArrayStream(Buffer, Position, Length)) { CommandName = TextEncoding.UTF16.GetString(s.Read(CommandNameLength)); } var r = new TryShiftResult { Command = null, Position = Position + CommandNameLength }; State = 2; return(r); } return(null); } else if (State == 2) { if (Length >= 4) { using (var s = new ByteArrayStream(Buffer, Position, Length)) { CommandHash = s.ReadUInt32(); } var r = new TryShiftResult { Command = null, Position = Position + 4 }; State = 3; return(r); } return(null); } if (State == 3) { if (Length >= 4) { using (var s = new ByteArrayStream(Buffer, Position, Length)) { ParametersLength = s.ReadInt32(); } if (ParametersLength < 0 || ParametersLength > 8 * 1024) { throw new InvalidOperationException(); } var r = new TryShiftResult { Command = null, Position = Position + 4 }; State = 4; return(r); } return(null); } else if (State == 4) { if (Length >= ParametersLength) { Byte[] Parameters; using (var s = new ByteArrayStream(Buffer, Position, Length)) { Parameters = s.Read(ParametersLength); } var cmd = new Command { CommandName = CommandName, CommandHash = CommandHash, Parameters = Parameters }; var r = new TryShiftResult { Command = cmd, Position = Position + ParametersLength }; CommandNameLength = 0; CommandName = null; CommandHash = 0; ParametersLength = 0; State = 0; return(r); } return(null); } else { throw new InvalidOperationException(); } }