SmbComWriteAndX.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 System;
  18. namespace SharpCifs.Smb
  19. {
  20. internal class SmbComWriteAndX : AndXServerMessageBlock
  21. {
  22. private static readonly int ReadAndxBatchLimit
  23. = Config.GetInt("jcifs.smb.client.WriteAndX.ReadAndX", 1);
  24. private static readonly int CloseBatchLimit
  25. = Config.GetInt("jcifs.smb.client.WriteAndX.Close", 1);
  26. private int _fid;
  27. private int _remaining;
  28. private int _dataLength;
  29. private int _dataOffset;
  30. private int _off;
  31. private byte[] _b;
  32. private long _offset;
  33. private int _pad;
  34. internal int WriteMode;
  35. public SmbComWriteAndX() : base(null)
  36. {
  37. Command = SmbComWriteAndx;
  38. }
  39. internal SmbComWriteAndX(int fid,
  40. long offset,
  41. int remaining,
  42. byte[] b,
  43. int off,
  44. int len,
  45. ServerMessageBlock andx) : base(andx)
  46. {
  47. this._fid = fid;
  48. this._offset = offset;
  49. this._remaining = remaining;
  50. this._b = b;
  51. this._off = off;
  52. _dataLength = len;
  53. Command = SmbComWriteAndx;
  54. }
  55. internal virtual void SetParam(int fid,
  56. long offset,
  57. int remaining,
  58. byte[] b,
  59. int off,
  60. int len)
  61. {
  62. this._fid = fid;
  63. this._offset = offset;
  64. this._remaining = remaining;
  65. this._b = b;
  66. this._off = off;
  67. _dataLength = len;
  68. Digest = null;
  69. }
  70. internal override int GetBatchLimit(byte command)
  71. {
  72. if (command == SmbComReadAndx)
  73. {
  74. return ReadAndxBatchLimit;
  75. }
  76. if (command == SmbComClose)
  77. {
  78. return CloseBatchLimit;
  79. }
  80. return 0;
  81. }
  82. internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
  83. {
  84. int start = dstIndex;
  85. _dataOffset = (dstIndex - HeaderStart) + 26;
  86. // 26 = off from here to pad
  87. _pad = (_dataOffset - HeaderStart) % 4;
  88. _pad = _pad == 0 ? 0 : 4 - _pad;
  89. _dataOffset += _pad;
  90. WriteInt2(_fid, dst, dstIndex);
  91. dstIndex += 2;
  92. WriteInt4(_offset, dst, dstIndex);
  93. dstIndex += 4;
  94. for (int i = 0; i < 4; i++)
  95. {
  96. dst[dstIndex++] = 0xFF;
  97. }
  98. WriteInt2(WriteMode, dst, dstIndex);
  99. dstIndex += 2;
  100. WriteInt2(_remaining, dst, dstIndex);
  101. dstIndex += 2;
  102. dst[dstIndex++] = 0x00;
  103. dst[dstIndex++] = 0x00;
  104. WriteInt2(_dataLength, dst, dstIndex);
  105. dstIndex += 2;
  106. WriteInt2(_dataOffset, dst, dstIndex);
  107. dstIndex += 2;
  108. WriteInt4(_offset >> 32, dst, dstIndex);
  109. dstIndex += 4;
  110. return dstIndex - start;
  111. }
  112. internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
  113. {
  114. int start = dstIndex;
  115. while (_pad-- > 0)
  116. {
  117. dst[dstIndex++] = 0xEE;
  118. }
  119. Array.Copy(_b, _off, dst, dstIndex, _dataLength);
  120. dstIndex += _dataLength;
  121. return dstIndex - start;
  122. }
  123. internal override int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex)
  124. {
  125. return 0;
  126. }
  127. internal override int ReadBytesWireFormat(byte[] buffer, int bufferIndex)
  128. {
  129. return 0;
  130. }
  131. public override string ToString()
  132. {
  133. return "SmbComWriteAndX["
  134. + base.ToString()
  135. + ",fid=" + _fid
  136. + ",offset=" + _offset
  137. + ",writeMode=" + WriteMode
  138. + ",remaining=" + _remaining
  139. + ",dataLength=" + _dataLength
  140. + ",dataOffset=" + _dataOffset + "]";
  141. }
  142. }
  143. }