ReadAndXRequest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (C) 2014 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.Text;
  10. using Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. /// <summary>
  14. /// SMB_COM_READ_ANDX Request
  15. /// SMB 1.0: The Timeout field becomes Timeout_or_MaxCountHigh (used when the CAP_LARGE_READX capability has been negotiated)
  16. /// </summary>
  17. public class ReadAndXRequest : SMBAndXCommand
  18. {
  19. public const int ParametersFixedLength = 20;
  20. // Parameters:
  21. //CommandName AndXCommand;
  22. //byte AndXReserved;
  23. //ushort AndXOffset;
  24. public ushort FID;
  25. public ulong Offset; // 4 bytes + 4 optional 'OffsetHigh' bytes
  26. private ushort MaxCountOfBytesToReturn; // See 'Timeout_or_MaxCountHigh' comment
  27. public ushort MinCountOfBytesToReturn;
  28. /// <summary>
  29. /// SMB 1.0: When reading from a regular file, the field MUST be interpreted as
  30. /// MaxCountHigh and the two unused bytes MUST be zero.
  31. /// When reading from a name pipe or I/O device, the field MUST be interpreted as Timeout.
  32. /// </summary>
  33. public uint Timeout_or_MaxCountHigh; // CIFS: Timeout only
  34. public ushort Remaining;
  35. public ReadAndXRequest() : base()
  36. {
  37. }
  38. public ReadAndXRequest(byte[] buffer, int offset) : base(buffer, offset, false)
  39. {
  40. FID = LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  41. Offset = LittleEndianConverter.ToUInt32(this.SMBParameters, 6);
  42. MaxCountOfBytesToReturn = LittleEndianConverter.ToUInt16(this.SMBParameters, 10);
  43. MinCountOfBytesToReturn = LittleEndianConverter.ToUInt16(this.SMBParameters, 12);
  44. Timeout_or_MaxCountHigh = LittleEndianConverter.ToUInt32(this.SMBParameters, 14);
  45. Remaining = LittleEndianConverter.ToUInt16(this.SMBParameters, 18);
  46. if (SMBParameters.Length == ParametersFixedLength + 4)
  47. {
  48. uint offsetHigh = LittleEndianConverter.ToUInt32(this.SMBParameters, 20);
  49. Offset |= ((ulong)offsetHigh << 32);
  50. }
  51. }
  52. public override byte[] GetBytes(bool isUnicode)
  53. {
  54. int parametersLength = ParametersFixedLength;
  55. if (Offset > UInt32.MaxValue)
  56. {
  57. parametersLength += 4;
  58. }
  59. this.SMBParameters = new byte[parametersLength];
  60. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, FID);
  61. LittleEndianWriter.WriteUInt32(this.SMBParameters, 6, (uint)(Offset & 0xFFFFFFFF));
  62. LittleEndianWriter.WriteUInt16(this.SMBParameters, 10, (ushort)(MaxCountOfBytesToReturn & 0xFFFF));
  63. LittleEndianWriter.WriteUInt16(this.SMBParameters, 12, MinCountOfBytesToReturn);
  64. LittleEndianWriter.WriteUInt32(this.SMBParameters, 14, Timeout_or_MaxCountHigh);
  65. LittleEndianWriter.WriteUInt16(this.SMBParameters, 18, Remaining);
  66. if (Offset > UInt32.MaxValue)
  67. {
  68. uint offsetHigh = (uint)(Offset >> 32);
  69. LittleEndianWriter.WriteUInt32(this.SMBParameters, 20, offsetHigh);
  70. }
  71. return base.GetBytes(isUnicode);
  72. }
  73. /// <summary>
  74. /// The number of bytes to return when reading from a file and LargeRead is negotiated
  75. /// </summary>
  76. public uint MaxCountLarge
  77. {
  78. get
  79. {
  80. ushort maxCountHigh = (ushort)(Timeout_or_MaxCountHigh & 0xFFFF);
  81. return (uint)(maxCountHigh << 16) | MaxCountOfBytesToReturn;
  82. }
  83. set
  84. {
  85. MaxCountOfBytesToReturn = (ushort)(value & 0xFFFF);
  86. Timeout_or_MaxCountHigh = (ushort)(value >> 16);
  87. }
  88. }
  89. /// <summary>
  90. /// The number of bytes to return when reading from a named pipe or LargeRead is not negotiated
  91. /// </summary>
  92. public ushort MaxCount
  93. {
  94. get
  95. {
  96. return MaxCountOfBytesToReturn;
  97. }
  98. set
  99. {
  100. MaxCountOfBytesToReturn = value;
  101. }
  102. }
  103. public override CommandName CommandName
  104. {
  105. get
  106. {
  107. return CommandName.SMB_COM_READ_ANDX;
  108. }
  109. }
  110. }
  111. }