AndXServerMessageBlock.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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;
  18. using SharpCifs.Util.Sharpen;
  19. namespace SharpCifs.Smb
  20. {
  21. internal abstract class AndXServerMessageBlock : ServerMessageBlock
  22. {
  23. private const int AndxCommandOffset = 1;
  24. private const int AndxReservedOffset = 2;
  25. private const int AndxOffsetOffset = 3;
  26. private byte _andxCommand = unchecked(unchecked(0xFF));
  27. private int _andxOffset;
  28. internal ServerMessageBlock Andx;
  29. public AndXServerMessageBlock()
  30. {
  31. }
  32. internal AndXServerMessageBlock(ServerMessageBlock andx)
  33. {
  34. if (andx != null)
  35. {
  36. this.Andx = andx;
  37. _andxCommand = andx.Command;
  38. }
  39. }
  40. internal virtual int GetBatchLimit(byte command)
  41. {
  42. return 0;
  43. }
  44. internal override int Encode(byte[] dst, int dstIndex)
  45. {
  46. int start = HeaderStart = dstIndex;
  47. dstIndex += WriteHeaderWireFormat(dst, dstIndex);
  48. dstIndex += WriteAndXWireFormat(dst, dstIndex);
  49. Length = dstIndex - start;
  50. if (Digest != null)
  51. {
  52. Digest.Sign(dst, HeaderStart, Length, this, Response);
  53. }
  54. return Length;
  55. }
  56. internal override int Decode(byte[] buffer, int bufferIndex)
  57. {
  58. int start = HeaderStart = bufferIndex;
  59. bufferIndex += ReadHeaderWireFormat(buffer, bufferIndex);
  60. bufferIndex += ReadAndXWireFormat(buffer, bufferIndex);
  61. Length = bufferIndex - start;
  62. return Length;
  63. }
  64. internal virtual int WriteAndXWireFormat(byte[] dst, int dstIndex)
  65. {
  66. int start = dstIndex;
  67. WordCount = WriteParameterWordsWireFormat(dst, start + AndxOffsetOffset + 2);
  68. WordCount += 4;
  69. // for command, reserved, and offset
  70. dstIndex += WordCount + 1;
  71. WordCount /= 2;
  72. dst[start] = unchecked((byte)(WordCount & unchecked(0xFF)));
  73. ByteCount = WriteBytesWireFormat(dst, dstIndex + 2);
  74. dst[dstIndex++] = unchecked((byte)(ByteCount & unchecked(0xFF)));
  75. dst[dstIndex++] = unchecked((byte)((ByteCount >> 8) & unchecked(0xFF)));
  76. dstIndex += ByteCount;
  77. if (Andx == null
  78. || SmbConstants.UseBatching == false
  79. || BatchLevel >= GetBatchLimit(Andx.Command))
  80. {
  81. _andxCommand = unchecked(unchecked(0xFF));
  82. Andx = null;
  83. dst[start + AndxCommandOffset] = unchecked(unchecked(0xFF));
  84. dst[start + AndxReservedOffset] = unchecked(unchecked(0x00));
  85. // dst[start + ANDX_OFFSET_OFFSET] = (byte)0x00;
  86. // dst[start + ANDX_OFFSET_OFFSET + 1] = (byte)0x00;
  87. dst[start + AndxOffsetOffset] = unchecked(unchecked(0xde));
  88. dst[start + AndxOffsetOffset + 1] = unchecked(unchecked(0xde));
  89. // andx not used; return
  90. return dstIndex - start;
  91. }
  92. Andx.BatchLevel = BatchLevel + 1;
  93. dst[start + AndxCommandOffset] = _andxCommand;
  94. dst[start + AndxReservedOffset] = unchecked(unchecked(0x00));
  95. _andxOffset = dstIndex - HeaderStart;
  96. WriteInt2(_andxOffset, dst, start + AndxOffsetOffset);
  97. Andx.UseUnicode = UseUnicode;
  98. if (Andx is AndXServerMessageBlock)
  99. {
  100. Andx.Uid = Uid;
  101. dstIndex += ((AndXServerMessageBlock)Andx).WriteAndXWireFormat(dst, dstIndex);
  102. }
  103. else
  104. {
  105. // the andx smb is not of type andx so lets just write it here and
  106. // were done.
  107. int andxStart = dstIndex;
  108. Andx.WordCount = Andx.WriteParameterWordsWireFormat(dst, dstIndex);
  109. dstIndex += Andx.WordCount + 1;
  110. Andx.WordCount /= 2;
  111. dst[andxStart] = unchecked((byte)(Andx.WordCount & unchecked(0xFF)));
  112. Andx.ByteCount = Andx.WriteBytesWireFormat(dst, dstIndex + 2);
  113. dst[dstIndex++] = unchecked((byte)(Andx.ByteCount & unchecked(0xFF)));
  114. dst[dstIndex++] = unchecked((byte)((Andx.ByteCount >> 8) & unchecked(0xFF)));
  115. dstIndex += Andx.ByteCount;
  116. }
  117. return dstIndex - start;
  118. }
  119. internal virtual int ReadAndXWireFormat(byte[] buffer, int bufferIndex)
  120. {
  121. int start = bufferIndex;
  122. WordCount = buffer[bufferIndex++];
  123. if (WordCount != 0)
  124. {
  125. _andxCommand = buffer[bufferIndex];
  126. _andxOffset = ReadInt2(buffer, bufferIndex + 2);
  127. if (_andxOffset == 0)
  128. {
  129. _andxCommand = unchecked(unchecked(0xFF));
  130. }
  131. if (WordCount > 2)
  132. {
  133. ReadParameterWordsWireFormat(buffer, bufferIndex + 4);
  134. if (Command == SmbComNtCreateAndx && ((SmbComNtCreateAndXResponse)this).IsExtended)
  135. {
  136. WordCount += 8;
  137. }
  138. }
  139. bufferIndex = start + 1 + (WordCount * 2);
  140. }
  141. ByteCount = ReadInt2(buffer, bufferIndex);
  142. bufferIndex += 2;
  143. if (ByteCount != 0)
  144. {
  145. int n;
  146. n = ReadBytesWireFormat(buffer, bufferIndex);
  147. bufferIndex += ByteCount;
  148. }
  149. if (ErrorCode != 0 || _andxCommand == unchecked(unchecked(0xFF)))
  150. {
  151. _andxCommand = unchecked(unchecked(0xFF));
  152. Andx = null;
  153. }
  154. else
  155. {
  156. if (Andx == null)
  157. {
  158. _andxCommand = unchecked(unchecked(0xFF));
  159. throw new RuntimeException("no andx command supplied with response");
  160. }
  161. bufferIndex = HeaderStart + _andxOffset;
  162. Andx.HeaderStart = HeaderStart;
  163. Andx.Command = _andxCommand;
  164. Andx.ErrorCode = ErrorCode;
  165. Andx.Flags = Flags;
  166. Andx.Flags2 = Flags2;
  167. Andx.Tid = Tid;
  168. Andx.Pid = Pid;
  169. Andx.Uid = Uid;
  170. Andx.Mid = Mid;
  171. Andx.UseUnicode = UseUnicode;
  172. if (Andx is AndXServerMessageBlock)
  173. {
  174. bufferIndex += ((AndXServerMessageBlock)Andx).ReadAndXWireFormat(buffer,
  175. bufferIndex);
  176. }
  177. else
  178. {
  179. buffer[bufferIndex++] = unchecked((byte)(Andx.WordCount & unchecked(0xFF)));
  180. if (Andx.WordCount != 0)
  181. {
  182. if (Andx.WordCount > 2)
  183. {
  184. bufferIndex += Andx.ReadParameterWordsWireFormat(buffer, bufferIndex);
  185. }
  186. }
  187. Andx.ByteCount = ReadInt2(buffer, bufferIndex);
  188. bufferIndex += 2;
  189. if (Andx.ByteCount != 0)
  190. {
  191. Andx.ReadBytesWireFormat(buffer, bufferIndex);
  192. bufferIndex += Andx.ByteCount;
  193. }
  194. }
  195. Andx.Received = true;
  196. }
  197. return bufferIndex - start;
  198. }
  199. public override string ToString()
  200. {
  201. return base.ToString()
  202. + ",andxCommand=0x" + Hexdump.ToHexString(_andxCommand, 2)
  203. + ",andxOffset=" + _andxOffset;
  204. }
  205. }
  206. }