Trans2FindFirst2Response.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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["
  73. + "nextEntryOffset=" + NextEntryOffset
  74. + ",fileIndex=" + FileIndex
  75. + ",creationTime=" + Extensions.CreateDate(CreationTime)
  76. + ",lastAccessTime=" + Extensions.CreateDate(LastAccessTime)
  77. + ",lastWriteTime=" + Extensions.CreateDate(LastWriteTime)
  78. + ",changeTime=" + Extensions.CreateDate(ChangeTime)
  79. + ",endOfFile=" + EndOfFile
  80. + ",allocationSize=" + AllocationSize
  81. + ",extFileAttributes=" + ExtFileAttributes
  82. + ",fileNameLength=" + FileNameLength
  83. + ",eaSize=" + EaSize
  84. + ",shortNameLength=" + ShortNameLength
  85. + ",shortName=" + ShortName
  86. + ",filename=" + Filename + "]";
  87. }
  88. internal SmbFindFileBothDirectoryInfo(Trans2FindFirst2Response enclosing)
  89. {
  90. this._enclosing = enclosing;
  91. }
  92. private readonly Trans2FindFirst2Response _enclosing;
  93. }
  94. internal int Sid;
  95. internal bool IsEndOfSearch;
  96. internal int EaErrorOffset;
  97. internal int LastNameOffset;
  98. internal int LastNameBufferIndex;
  99. internal string LastName;
  100. internal int ResumeKey;
  101. public Trans2FindFirst2Response()
  102. {
  103. Command = SmbComTransaction2;
  104. SubCommand = Smb.SmbComTransaction.Trans2FindFirst2;
  105. }
  106. internal virtual string ReadString(byte[] src, int srcIndex, int len)
  107. {
  108. string str = null;
  109. try
  110. {
  111. if (UseUnicode)
  112. {
  113. // should Unicode alignment be corrected for here?
  114. str = Runtime.GetStringForBytes(src,
  115. srcIndex,
  116. len,
  117. SmbConstants.UniEncoding);
  118. }
  119. else
  120. {
  121. if (len > 0 && src[srcIndex + len - 1] == '\0')
  122. {
  123. len--;
  124. }
  125. str = Runtime.GetStringForBytes(src,
  126. srcIndex,
  127. len,
  128. SmbConstants.OemEncoding);
  129. }
  130. }
  131. catch (UnsupportedEncodingException uee)
  132. {
  133. if (Log.Level > 1)
  134. {
  135. Runtime.PrintStackTrace(uee, Log);
  136. }
  137. }
  138. return str;
  139. }
  140. internal override int WriteSetupWireFormat(byte[] dst, int dstIndex)
  141. {
  142. return 0;
  143. }
  144. internal override int WriteParametersWireFormat(byte[] dst, int dstIndex)
  145. {
  146. return 0;
  147. }
  148. internal override int WriteDataWireFormat(byte[] dst, int dstIndex)
  149. {
  150. return 0;
  151. }
  152. internal override int ReadSetupWireFormat(byte[] buffer, int bufferIndex, int len)
  153. {
  154. return 0;
  155. }
  156. internal override int ReadParametersWireFormat(byte[] buffer,
  157. int bufferIndex,
  158. int len)
  159. {
  160. int start = bufferIndex;
  161. if (SubCommand == Smb.SmbComTransaction.Trans2FindFirst2)
  162. {
  163. Sid = ReadInt2(buffer, bufferIndex);
  164. bufferIndex += 2;
  165. }
  166. NumEntries = ReadInt2(buffer, bufferIndex);
  167. bufferIndex += 2;
  168. IsEndOfSearch = (buffer[bufferIndex] & unchecked(0x01)) == unchecked(0x01)
  169. ? true
  170. : false;
  171. bufferIndex += 2;
  172. EaErrorOffset = ReadInt2(buffer, bufferIndex);
  173. bufferIndex += 2;
  174. LastNameOffset = ReadInt2(buffer, bufferIndex);
  175. bufferIndex += 2;
  176. return bufferIndex - start;
  177. }
  178. internal override int ReadDataWireFormat(byte[] buffer, int bufferIndex, int len)
  179. {
  180. int start = bufferIndex;
  181. SmbFindFileBothDirectoryInfo e;
  182. LastNameBufferIndex = bufferIndex + LastNameOffset;
  183. Results = new SmbFindFileBothDirectoryInfo[NumEntries];
  184. for (int i = 0; i < NumEntries; i++)
  185. {
  186. Results[i] = e = new SmbFindFileBothDirectoryInfo(this);
  187. e.NextEntryOffset = ReadInt4(buffer, bufferIndex);
  188. e.FileIndex = ReadInt4(buffer, bufferIndex + 4);
  189. e.CreationTime = ReadTime(buffer, bufferIndex + 8);
  190. // e.lastAccessTime = readTime( buffer, bufferIndex + 16 );
  191. e.LastWriteTime = ReadTime(buffer, bufferIndex + 24);
  192. // e.changeTime = readTime( buffer, bufferIndex + 32 );
  193. e.EndOfFile = ReadInt8(buffer, bufferIndex + 40);
  194. // e.allocationSize = readInt8( buffer, bufferIndex + 48 );
  195. e.ExtFileAttributes = ReadInt4(buffer, bufferIndex + 56);
  196. e.FileNameLength = ReadInt4(buffer, bufferIndex + 60);
  197. // e.eaSize = readInt4( buffer, bufferIndex + 64 );
  198. // e.shortNameLength = buffer[bufferIndex + 68] & 0xFF;
  199. // e.shortName = readString( buffer, bufferIndex + 70, e.shortNameLength );
  200. e.Filename = ReadString(buffer, bufferIndex + 94, e.FileNameLength);
  201. if (LastNameBufferIndex >= bufferIndex
  202. && (e.NextEntryOffset == 0
  203. || LastNameBufferIndex < (bufferIndex + e.NextEntryOffset)))
  204. {
  205. LastName = e.Filename;
  206. ResumeKey = e.FileIndex;
  207. }
  208. bufferIndex += e.NextEntryOffset;
  209. }
  210. //return bufferIndex - start;
  211. return DataCount;
  212. }
  213. public override string ToString()
  214. {
  215. string c;
  216. if (SubCommand == Smb.SmbComTransaction.Trans2FindFirst2)
  217. {
  218. c = "Trans2FindFirst2Response[";
  219. }
  220. else
  221. {
  222. c = "Trans2FindNext2Response[";
  223. }
  224. return c + base.ToString()
  225. + ",sid=" + Sid
  226. + ",searchCount=" + NumEntries
  227. + ",isEndOfSearch=" + IsEndOfSearch
  228. + ",eaErrorOffset=" + EaErrorOffset
  229. + ",lastNameOffset=" + LastNameOffset
  230. + ",lastName=" + LastName + "]";
  231. }
  232. }
  233. }