WriteRequest.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. /// <summary>
  14. /// SMB_COM_WRITE Request.
  15. /// This command is obsolete.
  16. /// Windows NT4 SP6 will send this command with empty data for some reason.
  17. /// </summary>
  18. public class WriteRequest : SMB1Command
  19. {
  20. public const int ParametersLength = 8;
  21. public const int SupportedBufferFormat = 0x01;
  22. // Parameters:
  23. public ushort FID;
  24. public ushort CountOfBytesToWrite;
  25. public ushort WriteOffsetInBytes;
  26. public ushort EstimateOfRemainingBytesToBeWritten;
  27. // Data:
  28. public byte BufferFormat;
  29. // ushort DataLength;
  30. public byte[] Data;
  31. public WriteRequest() : base()
  32. {
  33. BufferFormat = SupportedBufferFormat;
  34. Data = new byte[0];
  35. }
  36. public WriteRequest(byte[] buffer, int offset) : base(buffer, offset, false)
  37. {
  38. FID = LittleEndianConverter.ToUInt16(this.SMBParameters, 0);
  39. CountOfBytesToWrite = LittleEndianConverter.ToUInt16(this.SMBParameters, 2);
  40. WriteOffsetInBytes = LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  41. EstimateOfRemainingBytesToBeWritten = LittleEndianConverter.ToUInt16(this.SMBParameters, 6);
  42. BufferFormat = ByteReader.ReadByte(this.SMBData, 0);
  43. if (BufferFormat != SupportedBufferFormat)
  44. {
  45. throw new InvalidDataException("Unsupported Buffer Format");
  46. }
  47. ushort dataLength = LittleEndianConverter.ToUInt16(this.SMBData, 1);
  48. Data = ByteReader.ReadBytes(this.SMBData, 3, dataLength);
  49. }
  50. public override byte[] GetBytes(bool isUnicode)
  51. {
  52. if (Data.Length > UInt16.MaxValue)
  53. {
  54. throw new ArgumentException("Invalid Data length");
  55. }
  56. this.SMBParameters = new byte[ParametersLength];
  57. LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, FID);
  58. LittleEndianWriter.WriteUInt16(this.SMBParameters, 2, CountOfBytesToWrite);
  59. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, WriteOffsetInBytes);
  60. LittleEndianWriter.WriteUInt16(this.SMBParameters, 6, EstimateOfRemainingBytesToBeWritten);
  61. this.SMBData = new byte[3 + Data.Length];
  62. ByteWriter.WriteByte(this.SMBData, 0, BufferFormat);
  63. LittleEndianWriter.WriteUInt16(this.SMBData, 1, (ushort)Data.Length);
  64. ByteWriter.WriteBytes(this.SMBData, 3, Data);
  65. return base.GetBytes(isUnicode);
  66. }
  67. public override CommandName CommandName
  68. {
  69. get
  70. {
  71. return CommandName.SMB_COM_WRITE;
  72. }
  73. }
  74. }
  75. }