FindFileBothDirectoryInfo.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 2014-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 System.Text;
  10. using Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. /// <summary>
  14. /// SMB_FIND_FILE_BOTH_DIRECTORY_INFO
  15. /// </summary>
  16. public class FindFileBothDirectoryInfo : FindInformation
  17. {
  18. public const int FixedLength = 94;
  19. public uint NextEntryOffset;
  20. public uint FileIndex; // SHOULD be set to zero when sent in a response and SHOULD be ignored when received by the client
  21. public DateTime? CreationTime;
  22. public DateTime? LastAccessTime;
  23. public DateTime? LastWriteTime;
  24. public DateTime? LastChangeTime;
  25. public long EndOfFile;
  26. public long AllocationSize;
  27. public ExtendedFileAttributes ExtFileAttributes;
  28. //uint FileNameLength; // In bytes, MUST exclude the null termination.
  29. public uint EASize;
  30. //byte ShortNameLength; // In bytes
  31. public byte Reserved;
  32. public string ShortName; // 24 bytes, 8.3 name of the file in Unicode format
  33. public string FileName; // OEM / Unicode character array. MUST be written as SMB_STRING, and read as fixed length string.
  34. // Omitting the NULL termination from the FileName field in a single SMB_FIND_FILE_BOTH_DIRECTORY_INFO structure
  35. // (as a response to TRANS2_QUERY_PATH_INFORMATION on a single directory)
  36. // Will, in some rare but repeatable cases, cause issues with Windows XP SP3 as a client
  37. // (the client will display an error message that the folder "refers to a location that is unavailable"...)
  38. public FindFileBothDirectoryInfo() : base(false)
  39. {
  40. }
  41. public FindFileBothDirectoryInfo(byte[] buffer, ref int offset, bool isUnicode) : base(false)
  42. {
  43. NextEntryOffset = LittleEndianReader.ReadUInt32(buffer, ref offset);
  44. FileIndex = LittleEndianReader.ReadUInt32(buffer, ref offset);
  45. CreationTime = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
  46. LastAccessTime = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
  47. LastWriteTime = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
  48. LastChangeTime = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
  49. EndOfFile = LittleEndianReader.ReadInt64(buffer, ref offset);
  50. AllocationSize = LittleEndianReader.ReadInt64(buffer, ref offset);
  51. ExtFileAttributes = (ExtendedFileAttributes)LittleEndianReader.ReadUInt32(buffer, ref offset);
  52. uint fileNameLength = LittleEndianReader.ReadUInt32(buffer, ref offset);
  53. EASize = LittleEndianReader.ReadUInt32(buffer, ref offset);
  54. byte shortNameLength = ByteReader.ReadByte(buffer, ref offset);
  55. Reserved = ByteReader.ReadByte(buffer, ref offset);
  56. ShortName = ByteReader.ReadUTF16String(buffer, ref offset, 12);
  57. ShortName = ShortName.Substring(0, shortNameLength);
  58. FileName = SMB1Helper.ReadFixedLengthString(buffer, ref offset, isUnicode, (int)fileNameLength);
  59. }
  60. public override void WriteBytes(byte[] buffer, ref int offset, bool isUnicode)
  61. {
  62. uint fileNameLength = (uint)(isUnicode ? FileName.Length * 2 : FileName.Length);
  63. byte shortNameLength = (byte)(ShortName.Length * 2);
  64. LittleEndianWriter.WriteUInt32(buffer, ref offset, NextEntryOffset);
  65. LittleEndianWriter.WriteUInt32(buffer, ref offset, FileIndex);
  66. FileTimeHelper.WriteFileTime(buffer, ref offset, CreationTime);
  67. FileTimeHelper.WriteFileTime(buffer, ref offset, LastAccessTime);
  68. FileTimeHelper.WriteFileTime(buffer, ref offset, LastWriteTime);
  69. FileTimeHelper.WriteFileTime(buffer, ref offset, LastChangeTime);
  70. LittleEndianWriter.WriteInt64(buffer, ref offset, EndOfFile);
  71. LittleEndianWriter.WriteInt64(buffer, ref offset, AllocationSize);
  72. LittleEndianWriter.WriteUInt32(buffer, ref offset, (uint)ExtFileAttributes);
  73. LittleEndianWriter.WriteUInt32(buffer, ref offset, fileNameLength);
  74. LittleEndianWriter.WriteUInt32(buffer, ref offset, EASize);
  75. ByteWriter.WriteByte(buffer, ref offset, shortNameLength);
  76. ByteWriter.WriteByte(buffer, ref offset, Reserved);
  77. ByteWriter.WriteUTF16String(buffer, ref offset, ShortName, 12);
  78. SMB1Helper.WriteSMBString(buffer, ref offset, isUnicode, FileName);
  79. }
  80. public override int GetLength(bool isUnicode)
  81. {
  82. int length = FixedLength;
  83. if (isUnicode)
  84. {
  85. length += FileName.Length * 2 + 2;
  86. }
  87. else
  88. {
  89. length += FileName.Length + 1;
  90. }
  91. return length;
  92. }
  93. public override FindInformationLevel InformationLevel
  94. {
  95. get
  96. {
  97. return FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
  98. }
  99. }
  100. }
  101. }