FindFileIDBothDirectoryInfo.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_ID_BOTH_DIRECTORY_INFO
  15. /// </summary>
  16. public class FindFileIDBothDirectoryInfo : FindInformation
  17. {
  18. public const int FixedLength = 104;
  19. // 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 ushort Reserved2;
  34. public ulong FileID;
  35. public string FileName; // OEM / Unicode character array. MUST be written as SMB_STRING, and read as fixed length string.
  36. // Omitting the NULL termination from the FileName field in a single SMB_FIND_FILE_BOTH_DIRECTORY_INFO structure
  37. // (as a response to TRANS2_QUERY_PATH_INFORMATION on a single directory)
  38. // Will, in some rare but repeatable cases, cause issues with Windows XP SP3 as a client
  39. // (the client will display an error message that the folder "refers to a location that is unavailable"...)
  40. public FindFileIDBothDirectoryInfo() : base()
  41. {
  42. }
  43. public FindFileIDBothDirectoryInfo(byte[] buffer, int offset, bool isUnicode) : base()
  44. {
  45. NextEntryOffset = LittleEndianReader.ReadUInt32(buffer, ref offset);
  46. FileIndex = LittleEndianReader.ReadUInt32(buffer, ref offset);
  47. CreationTime = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
  48. LastAccessTime = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
  49. LastWriteTime = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
  50. LastChangeTime = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
  51. EndOfFile = LittleEndianReader.ReadInt64(buffer, ref offset);
  52. AllocationSize = LittleEndianReader.ReadInt64(buffer, ref offset);
  53. ExtFileAttributes = (ExtendedFileAttributes)LittleEndianReader.ReadUInt32(buffer, ref offset);
  54. uint fileNameLength = LittleEndianReader.ReadUInt32(buffer, ref offset);
  55. EASize = LittleEndianReader.ReadUInt32(buffer, ref offset);
  56. byte shortNameLength = ByteReader.ReadByte(buffer, ref offset);
  57. Reserved = ByteReader.ReadByte(buffer, ref offset);
  58. ShortName = ByteReader.ReadUTF16String(buffer, ref offset, 12);
  59. ShortName = ShortName.Substring(0, shortNameLength);
  60. Reserved2 = LittleEndianReader.ReadUInt16(buffer, ref offset);
  61. FileID = LittleEndianReader.ReadUInt64(buffer, ref offset);
  62. FileName = SMB1Helper.ReadFixedLengthString(buffer, ref offset, isUnicode, (int)fileNameLength);
  63. }
  64. public override void WriteBytes(byte[] buffer, ref int offset, bool isUnicode)
  65. {
  66. uint fileNameLength = (uint)(isUnicode ? FileName.Length * 2 : FileName.Length);
  67. byte shortNameLength = (byte)(ShortName.Length * 2);
  68. LittleEndianWriter.WriteUInt32(buffer, ref offset, NextEntryOffset);
  69. LittleEndianWriter.WriteUInt32(buffer, ref offset, FileIndex);
  70. FileTimeHelper.WriteFileTime(buffer, ref offset, CreationTime);
  71. FileTimeHelper.WriteFileTime(buffer, ref offset, LastAccessTime);
  72. FileTimeHelper.WriteFileTime(buffer, ref offset, LastWriteTime);
  73. FileTimeHelper.WriteFileTime(buffer, ref offset, LastChangeTime);
  74. LittleEndianWriter.WriteInt64(buffer, ref offset, EndOfFile);
  75. LittleEndianWriter.WriteInt64(buffer, ref offset, AllocationSize);
  76. LittleEndianWriter.WriteUInt32(buffer, ref offset, (uint)ExtFileAttributes);
  77. LittleEndianWriter.WriteUInt32(buffer, ref offset, fileNameLength);
  78. LittleEndianWriter.WriteUInt32(buffer, ref offset, EASize);
  79. ByteWriter.WriteByte(buffer, ref offset, shortNameLength);
  80. ByteWriter.WriteByte(buffer, ref offset, Reserved);
  81. ByteWriter.WriteUTF16String(buffer, ref offset, ShortName, 12);
  82. LittleEndianWriter.WriteUInt16(buffer, ref offset, Reserved2);
  83. LittleEndianWriter.WriteUInt64(buffer, ref offset, FileID);
  84. SMB1Helper.WriteSMBString(buffer, ref offset, isUnicode, FileName);
  85. }
  86. public override int GetLength(bool isUnicode)
  87. {
  88. int length = FixedLength;
  89. if (isUnicode)
  90. {
  91. length += FileName.Length * 2 + 2;
  92. }
  93. else
  94. {
  95. length += FileName.Length + 1;
  96. }
  97. return length;
  98. }
  99. public override FindInformationLevel InformationLevel
  100. {
  101. get
  102. {
  103. return FindInformationLevel.SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO;
  104. }
  105. }
  106. }
  107. }