SmbRandomAccessFile.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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) : this(new SmbFile
  41. (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), _writeAndxResp
  178. );
  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, (int)(newLength & unchecked(0xFFFFFFFFL)), 0, _tmp, 0, 0), rsp);
  210. }
  211. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  212. public virtual void Close()
  213. {
  214. _file.Close();
  215. }
  216. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  217. public bool ReadBoolean()
  218. {
  219. if ((Read(_tmp, 0, 1)) < 0)
  220. {
  221. throw new SmbException("EOF");
  222. }
  223. return _tmp[0] != unchecked(unchecked(0x00));
  224. }
  225. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  226. public byte ReadByte()
  227. {
  228. if ((Read(_tmp, 0, 1)) < 0)
  229. {
  230. throw new SmbException("EOF");
  231. }
  232. return _tmp[0];
  233. }
  234. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  235. public int ReadUnsignedByte()
  236. {
  237. if ((Read(_tmp, 0, 1)) < 0)
  238. {
  239. throw new SmbException("EOF");
  240. }
  241. return _tmp[0] & unchecked(0xFF);
  242. }
  243. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  244. public short ReadShort()
  245. {
  246. if ((Read(_tmp, 0, 2)) < 0)
  247. {
  248. throw new SmbException("EOF");
  249. }
  250. return Encdec.Dec_uint16be(_tmp, 0);
  251. }
  252. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  253. public int ReadUnsignedShort()
  254. {
  255. if ((Read(_tmp, 0, 2)) < 0)
  256. {
  257. throw new SmbException("EOF");
  258. }
  259. return Encdec.Dec_uint16be(_tmp, 0) & unchecked(0xFFFF);
  260. }
  261. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  262. public char ReadChar()
  263. {
  264. if ((Read(_tmp, 0, 2)) < 0)
  265. {
  266. throw new SmbException("EOF");
  267. }
  268. return (char)Encdec.Dec_uint16be(_tmp, 0);
  269. }
  270. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  271. public int ReadInt()
  272. {
  273. if ((Read(_tmp, 0, 4)) < 0)
  274. {
  275. throw new SmbException("EOF");
  276. }
  277. return Encdec.Dec_uint32be(_tmp, 0);
  278. }
  279. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  280. public long ReadLong()
  281. {
  282. if ((Read(_tmp, 0, 8)) < 0)
  283. {
  284. throw new SmbException("EOF");
  285. }
  286. return Encdec.Dec_uint64be(_tmp, 0);
  287. }
  288. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  289. public float ReadFloat()
  290. {
  291. if ((Read(_tmp, 0, 4)) < 0)
  292. {
  293. throw new SmbException("EOF");
  294. }
  295. return Encdec.Dec_floatbe(_tmp, 0);
  296. }
  297. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  298. public double ReadDouble()
  299. {
  300. if ((Read(_tmp, 0, 8)) < 0)
  301. {
  302. throw new SmbException("EOF");
  303. }
  304. return Encdec.Dec_doublebe(_tmp, 0);
  305. }
  306. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  307. public string ReadLine()
  308. {
  309. StringBuilder input = new StringBuilder();
  310. int c = -1;
  311. bool eol = false;
  312. while (!eol)
  313. {
  314. switch (c = Read())
  315. {
  316. case -1:
  317. case '\n':
  318. {
  319. eol = true;
  320. break;
  321. }
  322. case '\r':
  323. {
  324. eol = true;
  325. long cur = _fp;
  326. if (Read() != '\n')
  327. {
  328. _fp = cur;
  329. }
  330. break;
  331. }
  332. default:
  333. {
  334. input.Append((char)c);
  335. break;
  336. }
  337. }
  338. }
  339. if ((c == -1) && (input.Length == 0))
  340. {
  341. return null;
  342. }
  343. return input.ToString();
  344. }
  345. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  346. public string ReadUtf()
  347. {
  348. int size = ReadUnsignedShort();
  349. byte[] b = new byte[size];
  350. Read(b, 0, size);
  351. try
  352. {
  353. return Encdec.Dec_utf8(b, 0, size);
  354. }
  355. catch (IOException ioe)
  356. {
  357. throw new SmbException(string.Empty, ioe);
  358. }
  359. }
  360. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  361. public void WriteBoolean(bool v)
  362. {
  363. _tmp[0] = unchecked((byte)(v ? 1 : 0));
  364. Write(_tmp, 0, 1);
  365. }
  366. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  367. public void WriteByte(int v)
  368. {
  369. _tmp[0] = unchecked((byte)v);
  370. Write(_tmp, 0, 1);
  371. }
  372. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  373. public void WriteShort(int v)
  374. {
  375. Encdec.Enc_uint16be((short)v, _tmp, 0);
  376. Write(_tmp, 0, 2);
  377. }
  378. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  379. public void WriteChar(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 WriteInt(int v)
  386. {
  387. Encdec.Enc_uint32be(v, _tmp, 0);
  388. Write(_tmp, 0, 4);
  389. }
  390. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  391. public void WriteLong(long v)
  392. {
  393. Encdec.Enc_uint64be(v, _tmp, 0);
  394. Write(_tmp, 0, 8);
  395. }
  396. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  397. public void WriteFloat(float v)
  398. {
  399. Encdec.Enc_floatbe(v, _tmp, 0);
  400. Write(_tmp, 0, 4);
  401. }
  402. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  403. public void WriteDouble(double v)
  404. {
  405. Encdec.Enc_doublebe(v, _tmp, 0);
  406. Write(_tmp, 0, 8);
  407. }
  408. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  409. public void WriteBytes(string s)
  410. {
  411. byte[] b = Runtime.GetBytesForString(s);
  412. Write(b, 0, b.Length);
  413. }
  414. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  415. /* public void WriteChars(string s)
  416. {
  417. int clen = s.Length;
  418. int blen = 2 * clen;
  419. byte[] b = new byte[blen];
  420. char[] c = new char[clen];
  421. Sharpen.Runtime.GetCharsForString(s, 0, clen, c, 0);
  422. for (int i = 0, j = 0; i < clen; i++)
  423. {
  424. b[j++] = unchecked((byte)((char)(((uchar)c[i]) >> 8)));
  425. b[j++] = unchecked((byte)((char)(((uchar)c[i]) >> 0)));
  426. }
  427. Write(b, 0, blen);
  428. }*/
  429. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  430. public void WriteUtf(string str)
  431. {
  432. int len = str.Length;
  433. int ch;
  434. int size = 0;
  435. byte[] dst;
  436. for (int i = 0; i < len; i++)
  437. {
  438. ch = str[i];
  439. size += ch > unchecked(0x07F) ? (ch > unchecked(0x7FF) ? 3 : 2) : 1;
  440. }
  441. dst = new byte[size];
  442. WriteShort(size);
  443. try
  444. {
  445. Encdec.Enc_utf8(str, dst, 0, size);
  446. }
  447. catch (IOException ioe)
  448. {
  449. throw new SmbException(string.Empty, ioe);
  450. }
  451. Write(dst, 0, size);
  452. }
  453. }
  454. }