AndXServerMessageBlock.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 || SmbConstants.UseBatching == false || BatchLevel >= GetBatchLimit(Andx.Command
  78. ))
  79. {
  80. _andxCommand = unchecked(unchecked(0xFF));
  81. Andx = null;
  82. dst[start + AndxCommandOffset] = unchecked(unchecked(0xFF));
  83. dst[start + AndxReservedOffset] = unchecked(unchecked(0x00));
  84. // dst[start + ANDX_OFFSET_OFFSET] = (byte)0x00;
  85. // dst[start + ANDX_OFFSET_OFFSET + 1] = (byte)0x00;
  86. dst[start + AndxOffsetOffset] = unchecked(unchecked(0xde));
  87. dst[start + AndxOffsetOffset + 1] = unchecked(unchecked(0xde));
  88. // andx not used; return
  89. return dstIndex - start;
  90. }
  91. Andx.BatchLevel = BatchLevel + 1;
  92. dst[start + AndxCommandOffset] = _andxCommand;
  93. dst[start + AndxReservedOffset] = unchecked(unchecked(0x00));
  94. _andxOffset = dstIndex - HeaderStart;
  95. WriteInt2(_andxOffset, dst, start + AndxOffsetOffset);
  96. Andx.UseUnicode = UseUnicode;
  97. if (Andx is AndXServerMessageBlock)
  98. {
  99. Andx.Uid = Uid;
  100. dstIndex += ((AndXServerMessageBlock)Andx).WriteAndXWireFormat(dst, dstIndex
  101. );
  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. ));
  116. dstIndex += Andx.ByteCount;
  117. }
  118. return dstIndex - start;
  119. }
  120. internal virtual int ReadAndXWireFormat(byte[] buffer, int bufferIndex)
  121. {
  122. int start = bufferIndex;
  123. WordCount = buffer[bufferIndex++];
  124. if (WordCount != 0)
  125. {
  126. _andxCommand = buffer[bufferIndex];
  127. _andxOffset = ReadInt2(buffer, bufferIndex + 2);
  128. if (_andxOffset == 0)
  129. {
  130. _andxCommand = unchecked(unchecked(0xFF));
  131. }
  132. if (WordCount > 2)
  133. {
  134. ReadParameterWordsWireFormat(buffer, bufferIndex + 4);
  135. if (Command == SmbComNtCreateAndx && ((SmbComNtCreateAndXResponse)this).IsExtended)
  136. {
  137. WordCount += 8;
  138. }
  139. }
  140. bufferIndex = start + 1 + (WordCount * 2);
  141. }
  142. ByteCount = ReadInt2(buffer, bufferIndex);
  143. bufferIndex += 2;
  144. if (ByteCount != 0)
  145. {
  146. int n;
  147. n = ReadBytesWireFormat(buffer, bufferIndex);
  148. bufferIndex += ByteCount;
  149. }
  150. if (ErrorCode != 0 || _andxCommand == unchecked(unchecked(0xFF)))
  151. {
  152. _andxCommand = unchecked(unchecked(0xFF));
  153. Andx = null;
  154. }
  155. else
  156. {
  157. if (Andx == null)
  158. {
  159. _andxCommand = unchecked(unchecked(0xFF));
  160. throw new RuntimeException("no andx command supplied with response");
  161. }
  162. bufferIndex = HeaderStart + _andxOffset;
  163. Andx.HeaderStart = HeaderStart;
  164. Andx.Command = _andxCommand;
  165. Andx.ErrorCode = ErrorCode;
  166. Andx.Flags = Flags;
  167. Andx.Flags2 = Flags2;
  168. Andx.Tid = Tid;
  169. Andx.Pid = Pid;
  170. Andx.Uid = Uid;
  171. Andx.Mid = Mid;
  172. Andx.UseUnicode = UseUnicode;
  173. if (Andx is AndXServerMessageBlock)
  174. {
  175. bufferIndex += ((AndXServerMessageBlock)Andx).ReadAndXWireFormat(buffer
  176. , bufferIndex);
  177. }
  178. else
  179. {
  180. buffer[bufferIndex++] = unchecked((byte)(Andx.WordCount & unchecked(0xFF))
  181. );
  182. if (Andx.WordCount != 0)
  183. {
  184. if (Andx.WordCount > 2)
  185. {
  186. bufferIndex += Andx.ReadParameterWordsWireFormat(buffer, bufferIndex);
  187. }
  188. }
  189. Andx.ByteCount = ReadInt2(buffer, bufferIndex);
  190. bufferIndex += 2;
  191. if (Andx.ByteCount != 0)
  192. {
  193. Andx.ReadBytesWireFormat(buffer, bufferIndex);
  194. bufferIndex += Andx.ByteCount;
  195. }
  196. }
  197. Andx.Received = true;
  198. }
  199. return bufferIndex - start;
  200. }
  201. public override string ToString()
  202. {
  203. return base.ToString() + ",andxCommand=0x" + Hexdump.ToHexString(_andxCommand
  204. , 2) + ",andxOffset=" + _andxOffset;
  205. }
  206. }
  207. }