Trans2FindFirst2Response.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using SharpCifs.Util.Sharpen;
  18. namespace SharpCifs.Smb
  19. {
  20. internal class Trans2FindFirst2Response : SmbComTransactionResponse
  21. {
  22. internal const int SmbInfoStandard = 1;
  23. internal const int SmbInfoQueryEaSize = 2;
  24. internal const int SmbInfoQueryEasFromList = 3;
  25. internal const int SmbFindFileDirectoryInfo = unchecked(0x101);
  26. internal const int SmbFindFileFullDirectoryInfo = unchecked(0x102);
  27. internal const int SmbFileNamesInfo = unchecked(0x103);
  28. internal const int SmbFileBothDirectoryInfo = unchecked(0x104);
  29. internal class SmbFindFileBothDirectoryInfo : IFileEntry
  30. {
  31. internal int NextEntryOffset;
  32. internal int FileIndex;
  33. internal long CreationTime;
  34. internal long LastAccessTime;
  35. internal long LastWriteTime;
  36. internal long ChangeTime;
  37. internal long EndOfFile;
  38. internal long AllocationSize;
  39. internal int ExtFileAttributes;
  40. internal int FileNameLength;
  41. internal int EaSize;
  42. internal int ShortNameLength;
  43. internal string ShortName;
  44. internal string Filename;
  45. // information levels
  46. public virtual string GetName()
  47. {
  48. return Filename;
  49. }
  50. public virtual int GetType()
  51. {
  52. return SmbFile.TypeFilesystem;
  53. }
  54. public virtual int GetAttributes()
  55. {
  56. return ExtFileAttributes;
  57. }
  58. public virtual long CreateTime()
  59. {
  60. return CreationTime;
  61. }
  62. public virtual long LastModified()
  63. {
  64. return LastWriteTime;
  65. }
  66. public virtual long Length()
  67. {
  68. return EndOfFile;
  69. }
  70. public override string ToString()
  71. {
  72. return "SmbFindFileBothDirectoryInfo[" + "nextEntryOffset=" + NextEntryOffset
  73. + ",fileIndex=" + FileIndex + ",creationTime=" + Extensions.CreateDate
  74. (CreationTime) + ",lastAccessTime=" + Extensions.CreateDate(LastAccessTime
  75. ) + ",lastWriteTime=" + Extensions.CreateDate(LastWriteTime) + ",changeTime="
  76. + Extensions.CreateDate(ChangeTime) + ",endOfFile=" + EndOfFile
  77. + ",allocationSize=" + AllocationSize + ",extFileAttributes=" + ExtFileAttributes
  78. + ",fileNameLength=" + FileNameLength + ",eaSize=" + EaSize + ",shortNameLength="
  79. + ShortNameLength + ",shortName=" + ShortName + ",filename=" + Filename
  80. + "]";
  81. }
  82. internal SmbFindFileBothDirectoryInfo(Trans2FindFirst2Response enclosing)
  83. {
  84. this._enclosing = enclosing;
  85. }
  86. private readonly Trans2FindFirst2Response _enclosing;
  87. }
  88. internal int Sid;
  89. internal bool IsEndOfSearch;
  90. internal int EaErrorOffset;
  91. internal int LastNameOffset;
  92. internal int LastNameBufferIndex;
  93. internal string LastName;
  94. internal int ResumeKey;
  95. public Trans2FindFirst2Response()
  96. {
  97. Command = SmbComTransaction2;
  98. SubCommand = Smb.SmbComTransaction.Trans2FindFirst2;
  99. }
  100. internal virtual string ReadString(byte[] src, int srcIndex, int len)
  101. {
  102. string str = null;
  103. try
  104. {
  105. if (UseUnicode)
  106. {
  107. // should Unicode alignment be corrected for here?
  108. str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.UniEncoding);
  109. }
  110. else
  111. {
  112. if (len > 0 && src[srcIndex + len - 1] == '\0')
  113. {
  114. len--;
  115. }
  116. str = Runtime.GetStringForBytes(src, srcIndex, len, SmbConstants.OemEncoding
  117. );
  118. }
  119. }
  120. catch (UnsupportedEncodingException uee)
  121. {
  122. if (Log.Level > 1)
  123. {
  124. Runtime.PrintStackTrace(uee, Log);
  125. }
  126. }
  127. return str;
  128. }
  129. internal override int WriteSetupWireFormat(byte[] dst, int dstIndex)
  130. {
  131. return 0;
  132. }
  133. internal override int WriteParametersWireFormat(byte[] dst, int dstIndex)
  134. {
  135. return 0;
  136. }
  137. internal override int WriteDataWireFormat(byte[] dst, int dstIndex)
  138. {
  139. return 0;
  140. }
  141. internal override int ReadSetupWireFormat(byte[] buffer, int bufferIndex, int len
  142. )
  143. {
  144. return 0;
  145. }
  146. internal override int ReadParametersWireFormat(byte[] buffer, int bufferIndex, int
  147. len)
  148. {
  149. int start = bufferIndex;
  150. if (SubCommand == Smb.SmbComTransaction.Trans2FindFirst2)
  151. {
  152. Sid = ReadInt2(buffer, bufferIndex);
  153. bufferIndex += 2;
  154. }
  155. NumEntries = ReadInt2(buffer, bufferIndex);
  156. bufferIndex += 2;
  157. IsEndOfSearch = (buffer[bufferIndex] & unchecked(0x01)) == unchecked(0x01) ? true : false;
  158. bufferIndex += 2;
  159. EaErrorOffset = ReadInt2(buffer, bufferIndex);
  160. bufferIndex += 2;
  161. LastNameOffset = ReadInt2(buffer, bufferIndex);
  162. bufferIndex += 2;
  163. return bufferIndex - start;
  164. }
  165. internal override int ReadDataWireFormat(byte[] buffer, int bufferIndex, int len)
  166. {
  167. int start = bufferIndex;
  168. SmbFindFileBothDirectoryInfo e;
  169. LastNameBufferIndex = bufferIndex + LastNameOffset;
  170. Results = new SmbFindFileBothDirectoryInfo[NumEntries];
  171. for (int i = 0; i < NumEntries; i++)
  172. {
  173. Results[i] = e = new SmbFindFileBothDirectoryInfo(this);
  174. e.NextEntryOffset = ReadInt4(buffer, bufferIndex);
  175. e.FileIndex = ReadInt4(buffer, bufferIndex + 4);
  176. e.CreationTime = ReadTime(buffer, bufferIndex + 8);
  177. // e.lastAccessTime = readTime( buffer, bufferIndex + 16 );
  178. e.LastWriteTime = ReadTime(buffer, bufferIndex + 24);
  179. // e.changeTime = readTime( buffer, bufferIndex + 32 );
  180. e.EndOfFile = ReadInt8(buffer, bufferIndex + 40);
  181. // e.allocationSize = readInt8( buffer, bufferIndex + 48 );
  182. e.ExtFileAttributes = ReadInt4(buffer, bufferIndex + 56);
  183. e.FileNameLength = ReadInt4(buffer, bufferIndex + 60);
  184. // e.eaSize = readInt4( buffer, bufferIndex + 64 );
  185. // e.shortNameLength = buffer[bufferIndex + 68] & 0xFF;
  186. // e.shortName = readString( buffer, bufferIndex + 70, e.shortNameLength );
  187. e.Filename = ReadString(buffer, bufferIndex + 94, e.FileNameLength);
  188. if (LastNameBufferIndex >= bufferIndex && (e.NextEntryOffset == 0 || LastNameBufferIndex
  189. < (bufferIndex + e.NextEntryOffset)))
  190. {
  191. LastName = e.Filename;
  192. ResumeKey = e.FileIndex;
  193. }
  194. bufferIndex += e.NextEntryOffset;
  195. }
  196. //return bufferIndex - start;
  197. return DataCount;
  198. }
  199. public override string ToString()
  200. {
  201. string c;
  202. if (SubCommand == Smb.SmbComTransaction.Trans2FindFirst2)
  203. {
  204. c = "Trans2FindFirst2Response[";
  205. }
  206. else
  207. {
  208. c = "Trans2FindNext2Response[";
  209. }
  210. return c + base.ToString() + ",sid=" + Sid + ",searchCount=" + NumEntries
  211. + ",isEndOfSearch=" + IsEndOfSearch + ",eaErrorOffset=" + EaErrorOffset + ",lastNameOffset="
  212. + LastNameOffset + ",lastName=" + LastName + "]";
  213. }
  214. }
  215. }