ReadResponse.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright (C) 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 Utilities;
  10. namespace SMBLibrary.SMB2
  11. {
  12. /// <summary>
  13. /// SMB2 READ Response
  14. /// </summary>
  15. public class ReadResponse : SMB2Command
  16. {
  17. public const int FixedSize = 16;
  18. public const int DeclaredSize = 17;
  19. private ushort StructureSize;
  20. private byte DataOffset;
  21. public byte Reserved;
  22. private uint DataLength;
  23. public uint DataRemaining;
  24. public uint Reserved2;
  25. public byte[] Data = new byte[0];
  26. public ReadResponse() : base(SMB2CommandName.Read)
  27. {
  28. Header.IsResponse = true;
  29. StructureSize = DeclaredSize;
  30. }
  31. public ReadResponse(byte[] buffer, int offset) : base(buffer, offset)
  32. {
  33. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  34. DataOffset = ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 2);
  35. Reserved = ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 3);
  36. DataLength = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  37. DataRemaining = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 8);
  38. Reserved2 = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 12);
  39. if (DataLength > 0)
  40. {
  41. Data = ByteReader.ReadBytes(buffer, offset + DataOffset, (int)DataLength);
  42. }
  43. }
  44. public override void WriteCommandBytes(byte[] buffer, int offset)
  45. {
  46. DataOffset = 0;
  47. DataLength = (uint)Data.Length;
  48. if (Data.Length > 0)
  49. {
  50. DataOffset = SMB2Header.Length + FixedSize;
  51. }
  52. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  53. ByteWriter.WriteByte(buffer, offset + 2, DataOffset);
  54. ByteWriter.WriteByte(buffer, offset + 3, Reserved);
  55. LittleEndianWriter.WriteUInt32(buffer, offset + 4, DataLength);
  56. LittleEndianWriter.WriteUInt32(buffer, offset + 8, DataRemaining);
  57. LittleEndianWriter.WriteUInt32(buffer, offset + 12, Reserved2);
  58. if (Data.Length > 0)
  59. {
  60. ByteWriter.WriteBytes(buffer, offset + FixedSize, Data);
  61. }
  62. }
  63. public override int CommandLength
  64. {
  65. get
  66. {
  67. return FixedSize + Data.Length;
  68. }
  69. }
  70. }
  71. }