ReadRequest.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Request
  14. /// </summary>
  15. public class ReadRequest : SMB2Command
  16. {
  17. public const int FixedSize = 48;
  18. public const int DeclaredSize = 49;
  19. private ushort StructureSize;
  20. public byte Padding;
  21. public ReadFlags Flags;
  22. public uint ReadLength;
  23. public ulong Offset;
  24. public FileID FileId;
  25. public uint MinimumCount;
  26. public uint Channel;
  27. public uint RemainingBytes;
  28. private ushort ReadChannelInfoOffset;
  29. private ushort ReadChannelInfoLength;
  30. public byte[] ReadChannelInfo = new byte[0];
  31. public ReadRequest() : base(SMB2CommandName.Read)
  32. {
  33. StructureSize = DeclaredSize;
  34. }
  35. public ReadRequest(byte[] buffer, int offset) : base(buffer, offset)
  36. {
  37. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  38. Padding = ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 2);
  39. Flags = (ReadFlags)ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 3);
  40. ReadLength = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  41. Offset = LittleEndianConverter.ToUInt64(buffer, offset + SMB2Header.Length + 8);
  42. FileId = new FileID(buffer, offset + SMB2Header.Length + 16);
  43. MinimumCount = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 32);
  44. Channel = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 36);
  45. RemainingBytes = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 40);
  46. ReadChannelInfoOffset = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 44);
  47. ReadChannelInfoLength = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 46);
  48. if (ReadChannelInfoLength > 0)
  49. {
  50. ReadChannelInfo = ByteReader.ReadBytes(buffer, offset + ReadChannelInfoOffset, ReadChannelInfoLength);
  51. }
  52. }
  53. public override void WriteCommandBytes(byte[] buffer, int offset)
  54. {
  55. ReadChannelInfoOffset = 0;
  56. ReadChannelInfoLength = (ushort)ReadChannelInfo.Length;
  57. if (ReadChannelInfo.Length > 0)
  58. {
  59. ReadChannelInfoOffset = SMB2Header.Length + FixedSize;
  60. }
  61. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  62. ByteWriter.WriteByte(buffer, offset + 2, Padding);
  63. ByteWriter.WriteByte(buffer, offset + 3, (byte)Flags);
  64. LittleEndianWriter.WriteUInt32(buffer, offset + 4, ReadLength);
  65. LittleEndianWriter.WriteUInt64(buffer, offset + 8, Offset);
  66. FileId.WriteBytes(buffer, offset + 16);
  67. LittleEndianWriter.WriteUInt32(buffer, offset + 32, MinimumCount);
  68. LittleEndianWriter.WriteUInt32(buffer, offset + 36, Channel);
  69. LittleEndianWriter.WriteUInt32(buffer, offset + 40, RemainingBytes);
  70. LittleEndianWriter.WriteUInt16(buffer, offset + 44, ReadChannelInfoOffset);
  71. LittleEndianWriter.WriteUInt16(buffer, offset + 46, ReadChannelInfoLength);
  72. if (ReadChannelInfo.Length > 0)
  73. {
  74. ByteWriter.WriteBytes(buffer, offset + FixedSize, ReadChannelInfo);
  75. }
  76. else
  77. {
  78. // The client MUST set one byte of [the buffer] field to 0
  79. ByteWriter.WriteBytes(buffer, offset + FixedSize, new byte[1]);
  80. }
  81. }
  82. public override int CommandLength
  83. {
  84. get
  85. {
  86. // The client MUST set one byte of [the buffer] field to 0
  87. return Math.Max(FixedSize + ReadChannelInfo.Length, DeclaredSize);
  88. }
  89. }
  90. }
  91. }