public byte[] GetBytes() { if (Commands.Count == 0) { throw new ArgumentException("Invalid command sequence"); } for (int index = 0; index < Commands.Count - 1; index++) { if (!(Commands[index] is SMBAndXCommand)) { throw new ArgumentException("Invalid command sequence"); } } SMBCommand lastCommand = Commands[Commands.Count - 1]; if (lastCommand is SMBAndXCommand) { ((SMBAndXCommand)lastCommand).AndXCommand = CommandName.SMB_COM_NO_ANDX_COMMAND; } List <byte[]> sequence = new List <byte[]>(); int length = SMBHeader.Length; byte[] commandBytes; for (int index = 0; index < Commands.Count - 1; index++) { SMBAndXCommand andXCommand = (SMBAndXCommand)Commands[index]; andXCommand.AndXCommand = Commands[index + 1].CommandName; commandBytes = Commands[index].GetBytes(Header.UnicodeFlag); ushort nextOffset = (ushort)(length + commandBytes.Length); SMBAndXCommand.WriteAndXOffset(commandBytes, 0, nextOffset); sequence.Add(commandBytes); length += commandBytes.Length; } commandBytes = lastCommand.GetBytes(Header.UnicodeFlag); sequence.Add(commandBytes); length += commandBytes.Length; Header.Command = Commands[0].CommandName; byte[] buffer = new byte[length]; Header.WriteBytes(buffer, 0); int offset = SMBHeader.Length; foreach (byte[] bytes in sequence) { ByteWriter.WriteBytes(buffer, ref offset, bytes); } return(buffer); }
public SMBMessage(byte[] buffer) { Header = new SMBHeader(buffer); SMBCommand command = SMBCommand.ReadCommand(buffer, SMBHeader.Length, Header.Command, Header); Commands.Add(command); while (command is SMBAndXCommand) { SMBAndXCommand andXCommand = (SMBAndXCommand)command; if (andXCommand.AndXCommand == CommandName.SMB_COM_NO_ANDX_COMMAND) { break; } command = SMBCommand.ReadCommand(buffer, andXCommand.AndXOffset, andXCommand.AndXCommand, Header); Commands.Add(command); } }