SmbComWriteAndX.cs 3.8 KB

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