SmbRandomAccessFile.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. using System.IO;
  19. using System.Text;
  20. using SharpCifs.Util;
  21. using SharpCifs.Util.Sharpen;
  22. namespace SharpCifs.Smb
  23. {
  24. public class SmbRandomAccessFile //: DataOutput, DataInput
  25. {
  26. private const int WriteOptions = unchecked(0x0842);
  27. private SmbFile _file;
  28. private long _fp;
  29. private int _openFlags;
  30. private int _access;
  31. private int _readSize;
  32. private int _writeSize;
  33. private int _ch;
  34. private int _options;
  35. private byte[] _tmp = new byte[8];
  36. private SmbComWriteAndXResponse _writeAndxResp;
  37. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  38. /// <exception cref="System.UriFormatException"></exception>
  39. /// <exception cref="UnknownHostException"></exception>
  40. public SmbRandomAccessFile(string url, string mode, int shareAccess)
  41. : this(new SmbFile(url, string.Empty, null, shareAccess), mode)
  42. {
  43. }
  44. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  45. /// <exception cref="System.UriFormatException"></exception>
  46. /// <exception cref="UnknownHostException"></exception>
  47. public SmbRandomAccessFile(SmbFile file, string mode)
  48. {
  49. this._file = file;
  50. if (mode.Equals("r"))
  51. {
  52. _openFlags = SmbFile.OCreat | SmbFile.ORdonly;
  53. }
  54. else
  55. {
  56. if (mode.Equals("rw"))
  57. {
  58. _openFlags = SmbFile.OCreat | SmbFile.ORdwr | SmbFile.OAppend;
  59. _writeAndxResp = new SmbComWriteAndXResponse();
  60. _options = WriteOptions;
  61. _access = SmbConstants.FileReadData | SmbConstants.FileWriteData;
  62. }
  63. else
  64. {
  65. throw new ArgumentException("Invalid mode");
  66. }
  67. }
  68. file.Open(_openFlags, _access, SmbFile.AttrNormal, _options);
  69. _readSize = file.Tree.Session.transport.RcvBufSize - 70;
  70. _writeSize = file.Tree.Session.transport.SndBufSize - 70;
  71. _fp = 0L;
  72. }
  73. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  74. public virtual int Read()
  75. {
  76. if (Read(_tmp, 0, 1) == -1)
  77. {
  78. return -1;
  79. }
  80. return _tmp[0] & unchecked(0xFF);
  81. }
  82. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  83. public virtual int Read(byte[] b)
  84. {
  85. return Read(b, 0, b.Length);
  86. }
  87. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  88. public virtual int Read(byte[] b, int off, int len)
  89. {
  90. if (len <= 0)
  91. {
  92. return 0;
  93. }
  94. long start = _fp;
  95. // ensure file is open
  96. if (_file.IsOpen() == false)
  97. {
  98. _file.Open(_openFlags, 0, SmbFile.AttrNormal, _options);
  99. }
  100. int r;
  101. int n;
  102. SmbComReadAndXResponse response = new SmbComReadAndXResponse(b, off);
  103. do
  104. {
  105. r = len > _readSize ? _readSize : len;
  106. _file.Send(new SmbComReadAndX(_file.Fid, _fp, r, null), response);
  107. if ((n = response.DataLength) <= 0)
  108. {
  109. return (int)((_fp - start) > 0L ? _fp - start : -1);
  110. }
  111. _fp += n;
  112. len -= n;
  113. response.Off += n;
  114. }
  115. while (len > 0 && n == r);
  116. return (int)(_fp - start);
  117. }
  118. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  119. public void ReadFully(byte[] b)
  120. {
  121. ReadFully(b, 0, b.Length);
  122. }
  123. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  124. public void ReadFully(byte[] b, int off, int len)
  125. {
  126. int n = 0;
  127. int count;
  128. do
  129. {
  130. count = Read(b, off + n, len - n);
  131. if (count < 0)
  132. {
  133. throw new SmbException("EOF");
  134. }
  135. n += count;
  136. _fp += count;
  137. }
  138. while (n < len);
  139. }
  140. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  141. public virtual int SkipBytes(int n)
  142. {
  143. if (n > 0)
  144. {
  145. _fp += n;
  146. return n;
  147. }
  148. return 0;
  149. }
  150. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  151. public virtual void Write(int b)
  152. {
  153. _tmp[0] = unchecked((byte)b);
  154. Write(_tmp, 0, 1);
  155. }
  156. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  157. public virtual void Write(byte[] b)
  158. {
  159. Write(b, 0, b.Length);
  160. }
  161. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  162. public virtual void Write(byte[] b, int off, int len)
  163. {
  164. if (len <= 0)
  165. {
  166. return;
  167. }
  168. // ensure file is open
  169. if (_file.IsOpen() == false)
  170. {
  171. _file.Open(_openFlags, 0, SmbFile.AttrNormal, _options);
  172. }
  173. int w;
  174. do
  175. {
  176. w = len > _writeSize ? _writeSize : len;
  177. _file.Send(new SmbComWriteAndX(_file.Fid, _fp, len - w, b, off, w, null),
  178. _writeAndxResp);
  179. _fp += _writeAndxResp.Count;
  180. len -= (int)_writeAndxResp.Count;
  181. off += (int)_writeAndxResp.Count;
  182. }
  183. while (len > 0);
  184. }
  185. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  186. public virtual long GetFilePointer()
  187. {
  188. return _fp;
  189. }
  190. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  191. public virtual void Seek(long pos)
  192. {
  193. _fp = pos;
  194. }
  195. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  196. public virtual long Length()
  197. {
  198. return _file.Length();
  199. }
  200. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  201. public virtual void SetLength(long newLength)
  202. {
  203. // ensure file is open
  204. if (_file.IsOpen() == false)
  205. {
  206. _file.Open(_openFlags, 0, SmbFile.AttrNormal, _options);
  207. }
  208. SmbComWriteResponse rsp = new SmbComWriteResponse();
  209. _file.Send(new SmbComWrite(_file.Fid,
  210. (int)(newLength & unchecked(0xFFFFFFFFL)),
  211. 0,
  212. _tmp,
  213. 0,
  214. 0),
  215. rsp);
  216. }
  217. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  218. public virtual void Close()
  219. {
  220. _file.Close();
  221. }
  222. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  223. public bool ReadBoolean()
  224. {
  225. if ((Read(_tmp, 0, 1)) < 0)
  226. {
  227. throw new SmbException("EOF");
  228. }
  229. return _tmp[0] != unchecked(unchecked(0x00));
  230. }
  231. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  232. public byte ReadByte()
  233. {
  234. if ((Read(_tmp, 0, 1)) < 0)
  235. {
  236. throw new SmbException("EOF");
  237. }
  238. return _tmp[0];
  239. }
  240. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  241. public int ReadUnsignedByte()
  242. {
  243. if ((Read(_tmp, 0, 1)) < 0)
  244. {
  245. throw new SmbException("EOF");
  246. }
  247. return _tmp[0] & unchecked(0xFF);
  248. }
  249. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  250. public short ReadShort()
  251. {
  252. if ((Read(_tmp, 0, 2)) < 0)
  253. {
  254. throw new SmbException("EOF");
  255. }
  256. return Encdec.Dec_uint16be(_tmp, 0);
  257. }
  258. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  259. public int ReadUnsignedShort()
  260. {
  261. if ((Read(_tmp, 0, 2)) < 0)
  262. {
  263. throw new SmbException("EOF");
  264. }
  265. return Encdec.Dec_uint16be(_tmp, 0) & unchecked(0xFFFF);
  266. }
  267. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  268. public char ReadChar()
  269. {
  270. if ((Read(_tmp, 0, 2)) < 0)
  271. {
  272. throw new SmbException("EOF");
  273. }
  274. return (char)Encdec.Dec_uint16be(_tmp, 0);
  275. }
  276. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  277. public int ReadInt()
  278. {
  279. if ((Read(_tmp, 0, 4)) < 0)
  280. {
  281. throw new SmbException("EOF");
  282. }
  283. return Encdec.Dec_uint32be(_tmp, 0);
  284. }
  285. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  286. public long ReadLong()
  287. {
  288. if ((Read(_tmp, 0, 8)) < 0)
  289. {
  290. throw new SmbException("EOF");
  291. }
  292. return Encdec.Dec_uint64be(_tmp, 0);
  293. }
  294. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  295. public float ReadFloat()
  296. {
  297. if ((Read(_tmp, 0, 4)) < 0)
  298. {
  299. throw new SmbException("EOF");
  300. }
  301. return Encdec.Dec_floatbe(_tmp, 0);
  302. }
  303. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  304. public double ReadDouble()
  305. {
  306. if ((Read(_tmp, 0, 8)) < 0)
  307. {
  308. throw new SmbException("EOF");
  309. }
  310. return Encdec.Dec_doublebe(_tmp, 0);
  311. }
  312. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  313. public string ReadLine()
  314. {
  315. StringBuilder input = new StringBuilder();
  316. int c = -1;
  317. bool eol = false;
  318. while (!eol)
  319. {
  320. switch (c = Read())
  321. {
  322. case -1:
  323. case '\n':
  324. {
  325. eol = true;
  326. break;
  327. }
  328. case '\r':
  329. {
  330. eol = true;
  331. long cur = _fp;
  332. if (Read() != '\n')
  333. {
  334. _fp = cur;
  335. }
  336. break;
  337. }
  338. default:
  339. {
  340. input.Append((char)c);
  341. break;
  342. }
  343. }
  344. }
  345. if ((c == -1) && (input.Length == 0))
  346. {
  347. return null;
  348. }
  349. return input.ToString();
  350. }
  351. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  352. public string ReadUtf()
  353. {
  354. int size = ReadUnsignedShort();
  355. byte[] b = new byte[size];
  356. Read(b, 0, size);
  357. try
  358. {
  359. return Encdec.Dec_utf8(b, 0, size);
  360. }
  361. catch (IOException ioe)
  362. {
  363. throw new SmbException(string.Empty, ioe);
  364. }
  365. }
  366. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  367. public void WriteBoolean(bool v)
  368. {
  369. _tmp[0] = unchecked((byte)(v ? 1 : 0));
  370. Write(_tmp, 0, 1);
  371. }
  372. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  373. public void WriteByte(int v)
  374. {
  375. _tmp[0] = unchecked((byte)v);
  376. Write(_tmp, 0, 1);
  377. }
  378. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  379. public void WriteShort(int v)
  380. {
  381. Encdec.Enc_uint16be((short)v, _tmp, 0);
  382. Write(_tmp, 0, 2);
  383. }
  384. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  385. public void WriteChar(int v)
  386. {
  387. Encdec.Enc_uint16be((short)v, _tmp, 0);
  388. Write(_tmp, 0, 2);
  389. }
  390. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  391. public void WriteInt(int v)
  392. {
  393. Encdec.Enc_uint32be(v, _tmp, 0);
  394. Write(_tmp, 0, 4);
  395. }
  396. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  397. public void WriteLong(long v)
  398. {
  399. Encdec.Enc_uint64be(v, _tmp, 0);
  400. Write(_tmp, 0, 8);
  401. }
  402. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  403. public void WriteFloat(float v)
  404. {
  405. Encdec.Enc_floatbe(v, _tmp, 0);
  406. Write(_tmp, 0, 4);
  407. }
  408. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  409. public void WriteDouble(double v)
  410. {
  411. Encdec.Enc_doublebe(v, _tmp, 0);
  412. Write(_tmp, 0, 8);
  413. }
  414. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  415. public void WriteBytes(string s)
  416. {
  417. byte[] b = Runtime.GetBytesForString(s);
  418. Write(b, 0, b.Length);
  419. }
  420. /*
  421. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  422. public void WriteChars(string s)
  423. {
  424. int clen = s.Length;
  425. int blen = 2 * clen;
  426. byte[] b = new byte[blen];
  427. char[] c = new char[clen];
  428. Sharpen.Runtime.GetCharsForString(s, 0, clen, c, 0);
  429. for (int i = 0, j = 0; i < clen; i++)
  430. {
  431. b[j++] = unchecked((byte)((char)(((uchar)c[i]) >> 8)));
  432. b[j++] = unchecked((byte)((char)(((uchar)c[i]) >> 0)));
  433. }
  434. Write(b, 0, blen);
  435. }
  436. */
  437. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  438. public void WriteUtf(string str)
  439. {
  440. int len = str.Length;
  441. int ch;
  442. int size = 0;
  443. byte[] dst;
  444. for (int i = 0; i < len; i++)
  445. {
  446. ch = str[i];
  447. size += ch > unchecked(0x07F)
  448. ? (ch > unchecked(0x7FF)
  449. ? 3
  450. : 2)
  451. : 1;
  452. }
  453. dst = new byte[size];
  454. WriteShort(size);
  455. try
  456. {
  457. Encdec.Enc_utf8(str, dst, 0, size);
  458. }
  459. catch (IOException ioe)
  460. {
  461. throw new SmbException(string.Empty, ioe);
  462. }
  463. Write(dst, 0, size);
  464. }
  465. }
  466. }