PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/SSISSSHFTP/SSISSharpSSHFTP/SharpSSH/jsch/Util.cs

#
C# | 582 lines | 472 code | 36 blank | 74 comment | 97 complexity | 9e4632fc854034152fd7eb3428ba1f63 MD5 | raw file
  1. using System;
  2. using System.Threading;
  3. namespace Tamir.SharpSsh.jsch
  4. {
  5. /* -*-mode:java; c-basic-offset:2; -*- */
  6. /*
  7. Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice,
  11. this list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in
  14. the documentation and/or other materials provided with the distribution.
  15. 3. The names of the authors may not be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  18. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  20. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  23. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. public class Util
  29. {
  30. /// <summary>
  31. /// Converts a time_t to DateTime
  32. /// </summary>
  33. public static DateTime Time_T2DateTime(uint time_t)
  34. {
  35. long win32FileTime = 10000000 * (long)time_t + 116444736000000000;
  36. return DateTime.FromFileTimeUtc(win32FileTime).ToLocalTime();
  37. }
  38. private static byte[] b64 = System.Text.Encoding.Default.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=");
  39. private static byte val(byte foo)
  40. {
  41. if (foo == '=') return 0;
  42. for (int j = 0; j < b64.Length; j++)
  43. {
  44. if (foo == b64[j]) return (byte)j;
  45. }
  46. return 0;
  47. }
  48. internal static byte[] fromBase64(byte[] buf, int start, int length)
  49. {
  50. byte[] foo = new byte[length];
  51. int j = 0;
  52. int l = length;
  53. for (int i = start; i < start + length; i += 4)
  54. {
  55. foo[j] = (byte)((val(buf[i]) << 2) | ((val(buf[i + 1]) & 0x30) >> 4));
  56. if (buf[i + 2] == (byte)'=') { j++; break; }
  57. foo[j + 1] = (byte)(((val(buf[i + 1]) & 0x0f) << 4) | ((val(buf[i + 2]) & 0x3c) >> 2));
  58. if (buf[i + 3] == (byte)'=') { j += 2; break; }
  59. foo[j + 2] = (byte)(((val(buf[i + 2]) & 0x03) << 6) | (val(buf[i + 3]) & 0x3f));
  60. j += 3;
  61. }
  62. byte[] bar = new byte[j];
  63. Array.Copy(foo, 0, bar, 0, j);
  64. return bar;
  65. }
  66. internal static byte[] toBase64(byte[] buf, int start, int length)
  67. {
  68. byte[] tmp = new byte[length * 2];
  69. int i, j, k;
  70. int foo = (length / 3) * 3 + start;
  71. i = 0;
  72. for (j = start; j < foo; j += 3)
  73. {
  74. k = (buf[j] >> 2) & 0x3f;
  75. tmp[i++] = b64[k];
  76. k = (buf[j] & 0x03) << 4 | (buf[j + 1] >> 4) & 0x0f;
  77. tmp[i++] = b64[k];
  78. k = (buf[j + 1] & 0x0f) << 2 | (buf[j + 2] >> 6) & 0x03;
  79. tmp[i++] = b64[k];
  80. k = buf[j + 2] & 0x3f;
  81. tmp[i++] = b64[k];
  82. }
  83. foo = (start + length) - foo;
  84. if (foo == 1)
  85. {
  86. k = (buf[j] >> 2) & 0x3f;
  87. tmp[i++] = b64[k];
  88. k = ((buf[j] & 0x03) << 4) & 0x3f;
  89. tmp[i++] = b64[k];
  90. tmp[i++] = (byte)'=';
  91. tmp[i++] = (byte)'=';
  92. }
  93. else if (foo == 2)
  94. {
  95. k = (buf[j] >> 2) & 0x3f;
  96. tmp[i++] = b64[k];
  97. k = (buf[j] & 0x03) << 4 | (buf[j + 1] >> 4) & 0x0f;
  98. tmp[i++] = b64[k];
  99. k = ((buf[j + 1] & 0x0f) << 2) & 0x3f;
  100. tmp[i++] = b64[k];
  101. tmp[i++] = (byte)'=';
  102. }
  103. byte[] bar = new byte[i];
  104. Array.Copy(tmp, 0, bar, 0, i);
  105. return bar;
  106. // return sun.misc.BASE64Encoder().encode(buf);
  107. }
  108. internal static String[] split(String foo, String split)
  109. {
  110. byte[] buf = Util.getBytes(foo);
  111. System.Collections.ArrayList bar = new System.Collections.ArrayList();
  112. int start = 0;
  113. int index;
  114. while (true)
  115. {
  116. index = foo.IndexOf(split, start);
  117. if (index >= 0)
  118. {
  119. bar.Add(Util.getString(buf, start, index - start));
  120. start = index + 1;
  121. continue;
  122. }
  123. bar.Add(Util.getString(buf, start, buf.Length - start));
  124. break;
  125. }
  126. String[] result = new String[bar.Count];
  127. for (int i = 0; i < result.Length; i++)
  128. {
  129. result[i] = (String)(bar[i]);
  130. }
  131. return result;
  132. }
  133. internal static bool glob(byte[] pattern, byte[] name)
  134. {
  135. return glob(pattern, 0, name, 0);
  136. }
  137. private static bool glob(byte[] pattern, int pattern_index,
  138. byte[] name, int name_index)
  139. {
  140. //System.out.println("glob: "+new String(pattern)+", "+new String(name));
  141. int patternlen = pattern.Length;
  142. if (patternlen == 0)
  143. return false;
  144. int namelen = name.Length;
  145. int i = pattern_index;
  146. int j = name_index;
  147. while (i < patternlen && j < namelen)
  148. {
  149. if (pattern[i] == '\\')
  150. {
  151. if (i + 1 == patternlen)
  152. return false;
  153. i++;
  154. if (pattern[i] != name[j]) return false;
  155. i++; j++;
  156. continue;
  157. }
  158. if (pattern[i] == '*')
  159. {
  160. if (patternlen == i + 1) return true;
  161. i++;
  162. byte foo = pattern[i];
  163. while (j < namelen)
  164. {
  165. if (foo == name[j])
  166. {
  167. if (glob(pattern, i, name, j))
  168. {
  169. return true;
  170. }
  171. }
  172. j++;
  173. }
  174. return false;
  175. /*
  176. if(j==namelen) return false;
  177. i++; j++;
  178. continue;
  179. */
  180. }
  181. if (pattern[i] == '?')
  182. {
  183. i++; j++;
  184. continue;
  185. }
  186. if (pattern[i] != name[j]) return false;
  187. i++; j++;
  188. continue;
  189. }
  190. if (i == patternlen && j == namelen) return true;
  191. return false;
  192. }
  193. private static String[] chars ={
  194. "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
  195. };
  196. internal static String getFingerPrint(HASH hash, byte[] data)
  197. {
  198. try
  199. {
  200. hash.init();
  201. hash.update(data, 0, data.Length);
  202. byte[] foo = hash.digest();
  203. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  204. uint bar;
  205. for (int i = 0; i < foo.Length; i++)
  206. {
  207. bar = (byte)(foo[i] & 0xff);
  208. sb.Append(chars[(bar >> 4) & 0xf]);
  209. sb.Append(chars[(bar) & 0xf]);
  210. if (i + 1 < foo.Length)
  211. sb.Append(":");
  212. }
  213. return sb.ToString();
  214. }
  215. catch
  216. {
  217. return "???";
  218. }
  219. }
  220. internal static SharpSsh.java.net.Socket createSocket(String host, int port, int timeout)
  221. {
  222. SharpSsh.java.net.Socket socket = null;
  223. String message = "";
  224. if (timeout == 0)
  225. {
  226. try
  227. {
  228. socket = new SharpSsh.java.net.Socket(host, port);
  229. return socket;
  230. }
  231. catch (Exception e)
  232. {
  233. message = e.ToString();
  234. throw new JSchException(message);
  235. }
  236. }
  237. String _host = host;
  238. int _port = port;
  239. SharpSsh.java.net.Socket[] sockp = new SharpSsh.java.net.Socket[1];
  240. Thread currentThread = Thread.CurrentThread;
  241. Exception[] ee = new Exception[1];
  242. message = "";
  243. createSocketRun runnable = new createSocketRun(sockp, ee, _host, _port);
  244. Thread tmp = new Thread(new ThreadStart(runnable.run))
  245. {
  246. Name = "Opening Socket " + host
  247. };
  248. tmp.Start();
  249. try
  250. {
  251. tmp.Join(timeout);
  252. message = "timeout: ";
  253. }
  254. catch (ThreadInterruptedException eee)
  255. {
  256. }
  257. if (sockp[0] != null && sockp[0].isConnected())
  258. {
  259. socket = sockp[0];
  260. }
  261. else
  262. {
  263. message += "socket is not established. Please check your username, password or port";
  264. //if (ee[0] != null)
  265. //{
  266. // message = ee[0].ToString();
  267. //}
  268. tmp.Interrupt();
  269. tmp = null;
  270. throw new JSchException(message);
  271. }
  272. return socket;
  273. }
  274. private class createSocketRun
  275. {
  276. SharpSsh.java.net.Socket[] sockp;
  277. Exception[] ee;
  278. string _host;
  279. int _port;
  280. public createSocketRun(SharpSsh.java.net.Socket[] sockp, Exception[] ee, string _host, int _port)
  281. {
  282. this.sockp = sockp;
  283. this.ee = ee;
  284. this._host = _host;
  285. this._port = _port;
  286. }
  287. public void run()
  288. {
  289. sockp[0] = null;
  290. try
  291. {
  292. sockp[0] = new SharpSsh.java.net.Socket(_host, _port);
  293. }
  294. catch (Exception e)
  295. {
  296. ee[0] = e;
  297. if (sockp[0] != null && sockp[0].isConnected())
  298. {
  299. try
  300. {
  301. sockp[0].close();
  302. }
  303. catch (Exception eee) { }
  304. }
  305. sockp[0] = null;
  306. }
  307. }
  308. }
  309. internal static bool array_equals(byte[] foo, byte[] bar)
  310. {
  311. int i = foo.Length;
  312. if (i != bar.Length) return false;
  313. for (int j = 0; j < i; j++) { if (foo[j] != bar[j]) return false; }
  314. //try{while(true){i--; if(foo[i]!=bar[i])return false;}}catch(Exception e){}
  315. return true;
  316. }
  317. public static string getString(byte[] arr, int offset, int len)
  318. {
  319. return System.Text.Encoding.Default.GetString(arr, offset, len);
  320. }
  321. public static string getStringUTF8(byte[] arr, int offset, int len)
  322. {
  323. return System.Text.Encoding.UTF8.GetString(arr, offset, len);
  324. }
  325. public static string getString(byte[] arr)
  326. {
  327. return getString(arr, 0, arr.Length);
  328. }
  329. public static string getStringUTF8(byte[] arr)
  330. {
  331. return getStringUTF8(arr, 0, arr.Length);
  332. }
  333. public static byte[] getBytes(String str)
  334. {
  335. return System.Text.Encoding.Default.GetBytes(str);
  336. }
  337. public static byte[] getBytesUTF8(String str)
  338. {
  339. return System.Text.Encoding.UTF8.GetBytes(str);
  340. }
  341. public static bool regionMatches(String orig, bool ignoreCase, int toffset,
  342. String other, int ooffset, int len)
  343. {
  344. char[] ta = new char[orig.Length];
  345. char[] pa = new char[other.Length];
  346. orig.CopyTo(0, ta, 0, orig.Length);
  347. int to = toffset;
  348. other.CopyTo(0, pa, 0, other.Length);
  349. int po = ooffset;
  350. // Note: toffset, ooffset, or len might be near -1>>>1.
  351. if ((ooffset < 0) || (toffset < 0) || (toffset > (long)orig.Length - len) ||
  352. (ooffset > (long)other.Length - len))
  353. {
  354. return false;
  355. }
  356. while (len-- > 0)
  357. {
  358. char c1 = ta[to++];
  359. char c2 = pa[po++];
  360. if (c1 == c2)
  361. {
  362. continue;
  363. }
  364. if (ignoreCase)
  365. {
  366. // If characters don't match but case may be ignored,
  367. // try converting both characters to uppercase.
  368. // If the results match, then the comparison scan should
  369. // continue.
  370. char u1 = char.ToUpper(c1);
  371. char u2 = char.ToUpper(c2);
  372. if (u1 == u2)
  373. {
  374. continue;
  375. }
  376. // Unfortunately, conversion to uppercase does not work properly
  377. // for the Georgian alphabet, which has strange rules about case
  378. // conversion. So we need to make one last check before
  379. // exiting.
  380. if (char.ToLower(u1) == char.ToLower(u2))
  381. {
  382. continue;
  383. }
  384. }
  385. return false;
  386. }
  387. return true;
  388. }
  389. public static uint ToUInt32(byte[] ptr, int Index)
  390. {
  391. uint ui = 0;
  392. ui = ((uint)ptr[Index++]) << 24;
  393. ui += ((uint)ptr[Index++]) << 16;
  394. ui += ((uint)ptr[Index++]) << 8;
  395. ui += (uint)ptr[Index++];
  396. return ui;
  397. }
  398. public static int ToInt32(byte[] ptr, int Index)
  399. {
  400. return (int)ToUInt32(ptr, Index);
  401. }
  402. public static ushort ToUInt16(byte[] ptr, int Index)
  403. {
  404. ushort u = 0;
  405. u = (ushort)ptr[Index++];
  406. u *= 256;
  407. u += (ushort)ptr[Index++];
  408. return u;
  409. }
  410. public static bool ArrayContains(Object[] arr, Object o)
  411. {
  412. for (int i = 0; i < arr.Length; i++)
  413. {
  414. if (arr[i].Equals(o))
  415. return true;
  416. }
  417. return false;
  418. }
  419. public static bool ArrayContains(char[] arr, char c)
  420. {
  421. for (int i = 0; i < arr.Length; i++)
  422. {
  423. if (arr[i] == c)
  424. return true;
  425. }
  426. return false;
  427. }
  428. public static bool ArrayContains(char[] arr, char c, int count)
  429. {
  430. for (int i = 0; i < count; i++)
  431. {
  432. if (arr[i] == c)
  433. return true;
  434. }
  435. return false;
  436. }
  437. /**
  438. * Utility method to delete the leading zeros from the modulus.
  439. * @param a modulus
  440. * @return modulus
  441. */
  442. public static byte[] stripLeadingZeros(byte[] a)
  443. {
  444. int lastZero = -1;
  445. for (int i = 0; i < a.Length; i++)
  446. {
  447. if (a[i] == 0)
  448. {
  449. lastZero = i;
  450. }
  451. else
  452. {
  453. break;
  454. }
  455. }
  456. lastZero++;
  457. byte[] result = new byte[a.Length - lastZero];
  458. Array.Copy(a, lastZero, result, 0, result.Length);
  459. return result;
  460. }
  461. /*
  462. Based on: http://www.orlingrabbe.com/dsa_java.htm
  463. And on: http://groups.google.com/group/comp.lang.java.security/browse_thread/thread/8f3bb93f9348434f/a1681ec252084483?lnk=st&q=ASN.1+signature+dsa+format&rnum=5&hl=en#a1681ec252084483
  464. If you're asking about the SHA/DSA signature format from
  465. java.security.Signature, it looks like this, for example:
  466. % od -tx1 sig
  467. 0000000 30 2c 02 14 4f 01 01 92 24 7c b3 c7 af 76 71 92
  468. 0000020 9c db c7 fe 1f 4e 42 4f 02 14 78 f3 63 d5 a3 0e
  469. 0000040 be 7d 92 7c 43 8d 0f 5f 02 df ee 35 9e b2
  470. It's DER format, with 0x30 meaning SEQUENCE, 0x2c the length of the
  471. sequence, 0x02 meaning INTEGER, and 0x14 the length of the integer.
  472. The first integer (as a byte array) is r, and the second one is s.
  473. */
  474. public static byte[] FixDsaSig(byte[] sig)
  475. {
  476. byte[] newSig = new byte[40];
  477. Array.Copy(sig, 4, newSig, 0, 20);
  478. int index;
  479. if (sig.Length == 46)
  480. index = 26;
  481. else if (sig.Length == 47)
  482. index = 27;
  483. else
  484. throw new Exception("Can't fix DSA Signature.");
  485. Array.Copy(sig, index, newSig, 20, 20);
  486. return newSig;
  487. }
  488. public static void print(string name, byte[] data)
  489. {
  490. Console.WriteLine();
  491. Console.Write(name + ": ");
  492. Console.WriteLine(hex(data));
  493. Console.WriteLine();
  494. }
  495. public static string hex(byte[] arr)
  496. {
  497. string hex = "0x";
  498. for (int i = 0; i < arr.Length; i++)
  499. {
  500. string mbyte = arr[i].ToString("X");
  501. if (mbyte.Length == 1)
  502. mbyte = "0" + mbyte;
  503. hex += mbyte;
  504. }
  505. return hex;
  506. }
  507. public static void Dump(string fileName, byte[] bytes)
  508. {
  509. System.IO.FileStream s = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate);
  510. s.Write(bytes, 0, bytes.Length);
  511. s.Flush();
  512. s.Close();
  513. }
  514. internal static java.String unquote(java.String _path)
  515. {
  516. byte[] path = _path.getBytes();
  517. int pathlen = path.Length;
  518. int i = 0;
  519. while (i < pathlen)
  520. {
  521. if (path[i] == '\\')
  522. {
  523. if (i + 1 == pathlen)
  524. break;
  525. java.System.arraycopy(path, i + 1, path, i, path.Length - (i + 1));
  526. pathlen--;
  527. continue;
  528. }
  529. i++;
  530. }
  531. if (pathlen == path.Length) return _path;
  532. byte[] foo = new byte[pathlen];
  533. java.System.arraycopy(path, 0, foo, 0, pathlen);
  534. return new java.String(foo);
  535. }
  536. }
  537. }