ReadAndXResponse.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Response
  15. /// SMB 1.0: The 2 reserved bytes at offset 14 become DataLengthHigh (used when the CAP_LARGE_READX capability has been negotiated)
  16. /// </summary>
  17. public class ReadAndXResponse : SMBAndXCommand
  18. {
  19. public const int ParametersLength = 24;
  20. // Parameters:
  21. //CommandName AndXCommand;
  22. //byte AndXReserved;
  23. //ushort AndXOffset;
  24. public ushort Available;
  25. public ushort DataCompactionMode; // Not used and MUST be 0x0000
  26. public ushort Reserved1;
  27. //uint DataLength; // 2 bytes + 2 'DataLengthHigh' bytes
  28. //ushort DataOffset;
  29. public byte[] Reserved2; // 8 bytes
  30. // Data:
  31. // 1 byte padding - if unicode strings are being used, this field MUST be present, otherwise it's optional.
  32. public byte[] Data;
  33. public ReadAndXResponse() : base()
  34. {
  35. Reserved2 = new byte[8];
  36. }
  37. public ReadAndXResponse(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  38. {
  39. Available = LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  40. DataCompactionMode = LittleEndianConverter.ToUInt16(this.SMBParameters, 6);
  41. Reserved1 = LittleEndianConverter.ToUInt16(this.SMBParameters, 8);
  42. uint DataLength = LittleEndianConverter.ToUInt16(this.SMBParameters, 10);
  43. ushort DataOffset = LittleEndianConverter.ToUInt16(this.SMBParameters, 12);
  44. ushort dataLengthHigh = LittleEndianConverter.ToUInt16(this.SMBParameters, 14);
  45. Reserved2 = ByteReader.ReadBytes(buffer, 16, 8);
  46. DataLength |= (uint)(dataLengthHigh << 16);
  47. Data = ByteReader.ReadBytes(buffer, DataOffset, (int)DataLength);
  48. }
  49. public override byte[] GetBytes(bool isUnicode)
  50. {
  51. uint DataLength = (uint)Data.Length;
  52. // WordCount + ByteCount are additional 3 bytes
  53. ushort DataOffset = SMB1Header.Length + 3 + ParametersLength;
  54. if (isUnicode)
  55. {
  56. DataOffset++;
  57. }
  58. ushort dataLengthHigh = (ushort)(DataLength >> 16);
  59. this.SMBParameters = new byte[ParametersLength];
  60. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, Available);
  61. LittleEndianWriter.WriteUInt16(this.SMBParameters, 6, DataCompactionMode);
  62. LittleEndianWriter.WriteUInt16(this.SMBParameters, 8, Reserved1);
  63. LittleEndianWriter.WriteUInt16(this.SMBParameters, 10, (ushort)(DataLength & 0xFFFF));
  64. LittleEndianWriter.WriteUInt16(this.SMBParameters, 12, DataOffset);
  65. LittleEndianWriter.WriteUInt16(this.SMBParameters, 14, dataLengthHigh);
  66. ByteWriter.WriteBytes(this.SMBParameters, 16, Reserved2);
  67. int smbDataLength = Data.Length;
  68. if (isUnicode)
  69. {
  70. smbDataLength++;
  71. }
  72. this.SMBData = new byte[smbDataLength];
  73. int offset = 0;
  74. if (isUnicode)
  75. {
  76. offset++;
  77. }
  78. ByteWriter.WriteBytes(this.SMBData, offset, this.Data);
  79. return base.GetBytes(isUnicode);
  80. }
  81. public override CommandName CommandName
  82. {
  83. get
  84. {
  85. return CommandName.SMB_COM_READ_ANDX;
  86. }
  87. }
  88. }
  89. }