QueryDirectoryResponse.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 QUERY_DIRECTORY Response
  14. /// </summary>
  15. public class QueryDirectoryResponse : SMB2Command
  16. {
  17. public const int FixedLength = 8;
  18. public const int DeclaredSize = 9;
  19. private ushort StructureSize;
  20. private ushort OutputBufferOffset;
  21. private uint OutputBufferLength;
  22. public byte[] OutputBuffer = new byte[0];
  23. public QueryDirectoryResponse() : base(SMB2CommandName.QueryDirectory)
  24. {
  25. Header.IsResponse = true;
  26. StructureSize = DeclaredSize;
  27. }
  28. public QueryDirectoryResponse(byte[] buffer, int offset) : base(buffer, offset)
  29. {
  30. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  31. OutputBufferOffset = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 2);
  32. OutputBufferLength = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  33. OutputBuffer = ByteReader.ReadBytes(buffer, offset + (int)OutputBufferOffset, (int)OutputBufferLength);
  34. }
  35. public override void WriteCommandBytes(byte[] buffer, int offset)
  36. {
  37. OutputBufferOffset = 0;
  38. OutputBufferLength = (uint)OutputBuffer.Length;
  39. if (OutputBuffer.Length > 0)
  40. {
  41. OutputBufferOffset = SMB2Header.Length + FixedLength;
  42. }
  43. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  44. LittleEndianWriter.WriteUInt16(buffer, offset + 2, OutputBufferOffset);
  45. LittleEndianWriter.WriteUInt32(buffer, offset + 4, OutputBufferLength);
  46. ByteWriter.WriteBytes(buffer, offset + FixedLength, OutputBuffer);
  47. }
  48. public List<QueryDirectoryFileInformation> GetFileInformationList(FileInformationClass fileInformationClass)
  49. {
  50. if (OutputBuffer.Length > 0)
  51. {
  52. return QueryDirectoryFileInformation.ReadFileInformationList(OutputBuffer, 0, fileInformationClass);
  53. }
  54. else
  55. {
  56. return new List<QueryDirectoryFileInformation>();
  57. }
  58. }
  59. public void SetFileInformationList(List<QueryDirectoryFileInformation> fileInformationList)
  60. {
  61. OutputBuffer = QueryDirectoryFileInformation.GetBytes(fileInformationList);
  62. }
  63. public override int CommandLength
  64. {
  65. get
  66. {
  67. return FixedLength + OutputBuffer.Length;
  68. }
  69. }
  70. }
  71. }