PageRenderTime 45ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/com.jcraft.jsch.source_0.1.41.v201101211617/com/jcraft/jsch/Util.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 464 lines | 382 code | 31 blank | 51 comment | 82 complexity | a1ead0098abda057bdf34e00c19e531c MD5 | raw file
  1. /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
  2. /*
  3. Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the distribution.
  11. 3. The names of the authors may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  14. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  15. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  16. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  19. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  22. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. package com.jcraft.jsch;
  25. import java.net.Socket;
  26. class Util{
  27. private static final byte[] b64 ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".getBytes();
  28. private static byte val(byte foo){
  29. if(foo == '=') return 0;
  30. for(int j=0; j<b64.length; j++){
  31. if(foo==b64[j]) return (byte)j;
  32. }
  33. return 0;
  34. }
  35. static byte[] fromBase64(byte[] buf, int start, int length){
  36. byte[] foo=new byte[length];
  37. int j=0;
  38. for (int i=start;i<start+length;i+=4){
  39. foo[j]=(byte)((val(buf[i])<<2)|((val(buf[i+1])&0x30)>>>4));
  40. if(buf[i+2]==(byte)'='){ j++; break;}
  41. foo[j+1]=(byte)(((val(buf[i+1])&0x0f)<<4)|((val(buf[i+2])&0x3c)>>>2));
  42. if(buf[i+3]==(byte)'='){ j+=2; break;}
  43. foo[j+2]=(byte)(((val(buf[i+2])&0x03)<<6)|(val(buf[i+3])&0x3f));
  44. j+=3;
  45. }
  46. byte[] bar=new byte[j];
  47. System.arraycopy(foo, 0, bar, 0, j);
  48. return bar;
  49. }
  50. static byte[] toBase64(byte[] buf, int start, int length){
  51. byte[] tmp=new byte[length*2];
  52. int i,j,k;
  53. int foo=(length/3)*3+start;
  54. i=0;
  55. for(j=start; j<foo; j+=3){
  56. k=(buf[j]>>>2)&0x3f;
  57. tmp[i++]=b64[k];
  58. k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f;
  59. tmp[i++]=b64[k];
  60. k=(buf[j+1]&0x0f)<<2|(buf[j+2]>>>6)&0x03;
  61. tmp[i++]=b64[k];
  62. k=buf[j+2]&0x3f;
  63. tmp[i++]=b64[k];
  64. }
  65. foo=(start+length)-foo;
  66. if(foo==1){
  67. k=(buf[j]>>>2)&0x3f;
  68. tmp[i++]=b64[k];
  69. k=((buf[j]&0x03)<<4)&0x3f;
  70. tmp[i++]=b64[k];
  71. tmp[i++]=(byte)'=';
  72. tmp[i++]=(byte)'=';
  73. }
  74. else if(foo==2){
  75. k=(buf[j]>>>2)&0x3f;
  76. tmp[i++]=b64[k];
  77. k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f;
  78. tmp[i++]=b64[k];
  79. k=((buf[j+1]&0x0f)<<2)&0x3f;
  80. tmp[i++]=b64[k];
  81. tmp[i++]=(byte)'=';
  82. }
  83. byte[] bar=new byte[i];
  84. System.arraycopy(tmp, 0, bar, 0, i);
  85. return bar;
  86. // return sun.misc.BASE64Encoder().encode(buf);
  87. }
  88. static String[] split(String foo, String split){
  89. if(foo==null)
  90. return null;
  91. byte[] buf=foo.getBytes();
  92. java.util.Vector bar=new java.util.Vector();
  93. int start=0;
  94. int index;
  95. while(true){
  96. index=foo.indexOf(split, start);
  97. if(index>=0){
  98. bar.addElement(new String(buf, start, index-start));
  99. start=index+1;
  100. continue;
  101. }
  102. bar.addElement(new String(buf, start, buf.length-start));
  103. break;
  104. }
  105. String[] result=new String[bar.size()];
  106. for(int i=0; i<result.length; i++){
  107. result[i]=(String)(bar.elementAt(i));
  108. }
  109. return result;
  110. }
  111. static boolean glob(byte[] pattern, byte[] name){
  112. return glob0(pattern, 0, name, 0);
  113. }
  114. static private boolean glob0(byte[] pattern, int pattern_index,
  115. byte[] name, int name_index){
  116. if(name.length>0 && name[0]=='.'){
  117. if(pattern.length>0 && pattern[0]=='.'){
  118. if(pattern.length==2 && pattern[1]=='*') return true;
  119. return glob(pattern, pattern_index+1, name, name_index+1);
  120. }
  121. return false;
  122. }
  123. return glob(pattern, pattern_index, name, name_index);
  124. }
  125. static private boolean glob(byte[] pattern, int pattern_index,
  126. byte[] name, int name_index){
  127. //System.err.println("glob: "+new String(pattern)+", "+pattern_index+" "+new String(name)+", "+name_index);
  128. int patternlen=pattern.length;
  129. if(patternlen==0)
  130. return false;
  131. int namelen=name.length;
  132. int i=pattern_index;
  133. int j=name_index;
  134. while(i<patternlen && j<namelen){
  135. if(pattern[i]=='\\'){
  136. if(i+1==patternlen)
  137. return false;
  138. i++;
  139. if(pattern[i]!=name[j])
  140. return false;
  141. i+=skipUTF8Char(pattern[i]);
  142. j+=skipUTF8Char(name[j]);
  143. continue;
  144. }
  145. if(pattern[i]=='*'){
  146. while(i<patternlen){
  147. if(pattern[i]=='*'){
  148. i++;
  149. continue;
  150. }
  151. break;
  152. }
  153. if(patternlen==i)
  154. return true;
  155. byte foo=pattern[i];
  156. if(foo=='?'){
  157. while(j<namelen){
  158. if(glob(pattern, i, name, j)){
  159. return true;
  160. }
  161. j+=skipUTF8Char(name[j]);
  162. }
  163. return false;
  164. }
  165. else if(foo=='\\'){
  166. if(i+1==patternlen)
  167. return false;
  168. i++;
  169. foo=pattern[i];
  170. while(j<namelen){
  171. if(foo==name[j]){
  172. if(glob(pattern, i+skipUTF8Char(foo),
  173. name, j+skipUTF8Char(name[j]))){
  174. return true;
  175. }
  176. }
  177. j+=skipUTF8Char(name[j]);
  178. }
  179. return false;
  180. }
  181. while(j<namelen){
  182. if(foo==name[j]){
  183. if(glob(pattern, i, name, j)){
  184. return true;
  185. }
  186. }
  187. j+=skipUTF8Char(name[j]);
  188. }
  189. return false;
  190. }
  191. if(pattern[i]=='?'){
  192. i++;
  193. j+=skipUTF8Char(name[j]);
  194. continue;
  195. }
  196. if(pattern[i]!=name[j])
  197. return false;
  198. i+=skipUTF8Char(pattern[i]);
  199. j+=skipUTF8Char(name[j]);
  200. if(!(j<namelen)){ // name is end
  201. if(!(i<patternlen)){ // pattern is end
  202. return true;
  203. }
  204. if(pattern[i]=='*'){
  205. break;
  206. }
  207. }
  208. continue;
  209. }
  210. if(i==patternlen && j==namelen)
  211. return true;
  212. if(!(j<namelen) && // name is end
  213. pattern[i]=='*'){
  214. boolean ok=true;
  215. while(i<patternlen){
  216. if(pattern[i++]!='*'){
  217. ok=false;
  218. break;
  219. }
  220. }
  221. return ok;
  222. }
  223. return false;
  224. }
  225. static String quote(String path){
  226. byte[] _path=str2byte(path);
  227. int count=0;
  228. for(int i=0;i<_path.length; i++){
  229. byte b=_path[i];
  230. if(b=='\\' || b=='?' || b=='*')
  231. count++;
  232. }
  233. if(count==0)
  234. return path;
  235. byte[] _path2=new byte[_path.length+count];
  236. for(int i=0, j=0; i<_path.length; i++){
  237. byte b=_path[i];
  238. if(b=='\\' || b=='?' || b=='*'){
  239. _path2[j++]='\\';
  240. }
  241. _path2[j++]=b;
  242. }
  243. return byte2str(_path2);
  244. }
  245. static String unquote(String path){
  246. byte[] foo=str2byte(path);
  247. byte[] bar=unquote(foo);
  248. if(foo.length==bar.length)
  249. return path;
  250. return byte2str(bar);
  251. }
  252. static byte[] unquote(byte[] path){
  253. int pathlen=path.length;
  254. int i=0;
  255. while(i<pathlen){
  256. if(path[i]=='\\'){
  257. if(i+1==pathlen)
  258. break;
  259. System.arraycopy(path, i+1, path, i, path.length-(i+1));
  260. pathlen--;
  261. i++;
  262. continue;
  263. }
  264. i++;
  265. }
  266. if(pathlen==path.length)
  267. return path;
  268. byte[] foo=new byte[pathlen];
  269. System.arraycopy(path, 0, foo, 0, pathlen);
  270. return foo;
  271. }
  272. private static String[] chars={
  273. "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
  274. };
  275. static String getFingerPrint(HASH hash, byte[] data){
  276. try{
  277. hash.init();
  278. hash.update(data, 0, data.length);
  279. byte[] foo=hash.digest();
  280. StringBuffer sb=new StringBuffer();
  281. int bar;
  282. for(int i=0; i<foo.length;i++){
  283. bar=foo[i]&0xff;
  284. sb.append(chars[(bar>>>4)&0xf]);
  285. sb.append(chars[(bar)&0xf]);
  286. if(i+1<foo.length)
  287. sb.append(":");
  288. }
  289. return sb.toString();
  290. }
  291. catch(Exception e){
  292. return "???";
  293. }
  294. }
  295. static boolean array_equals(byte[] foo, byte bar[]){
  296. int i=foo.length;
  297. if(i!=bar.length) return false;
  298. for(int j=0; j<i; j++){ if(foo[j]!=bar[j]) return false; }
  299. //try{while(true){i--; if(foo[i]!=bar[i])return false;}}catch(Exception e){}
  300. return true;
  301. }
  302. static Socket createSocket(String host, int port, int timeout) throws JSchException{
  303. Socket socket=null;
  304. if(timeout==0){
  305. try{
  306. socket=new Socket(host, port);
  307. return socket;
  308. }
  309. catch(Exception e){
  310. String message=e.toString();
  311. if(e instanceof Throwable)
  312. throw new JSchException(message, (Throwable)e);
  313. throw new JSchException(message);
  314. }
  315. }
  316. final String _host=host;
  317. final int _port=port;
  318. final Socket[] sockp=new Socket[1];
  319. final Exception[] ee=new Exception[1];
  320. String message="";
  321. Thread tmp=new Thread(new Runnable(){
  322. public void run(){
  323. sockp[0]=null;
  324. try{
  325. sockp[0]=new Socket(_host, _port);
  326. }
  327. catch(Exception e){
  328. ee[0]=e;
  329. if(sockp[0]!=null && sockp[0].isConnected()){
  330. try{
  331. sockp[0].close();
  332. }
  333. catch(Exception eee){}
  334. }
  335. sockp[0]=null;
  336. }
  337. }
  338. });
  339. tmp.setName("Opening Socket "+host);
  340. tmp.start();
  341. try{
  342. tmp.join(timeout);
  343. message="timeout: ";
  344. }
  345. catch(java.lang.InterruptedException eee){
  346. }
  347. if(sockp[0]!=null && sockp[0].isConnected()){
  348. socket=sockp[0];
  349. }
  350. else{
  351. message+="socket is not established";
  352. if(ee[0]!=null){
  353. message=ee[0].toString();
  354. }
  355. tmp.interrupt();
  356. tmp=null;
  357. throw new JSchException(message);
  358. }
  359. return socket;
  360. }
  361. static byte[] str2byte(String str, String encoding){
  362. if(str==null)
  363. return null;
  364. try{ return str.getBytes(encoding); }
  365. catch(java.io.UnsupportedEncodingException e){
  366. return str.getBytes();
  367. }
  368. }
  369. static byte[] str2byte(String str){
  370. return str2byte(str, "UTF-8");
  371. }
  372. static String byte2str(byte[] str, String encoding){
  373. try{ return new String(str, encoding); }
  374. catch(java.io.UnsupportedEncodingException e){
  375. return new String(str);
  376. }
  377. }
  378. static String byte2str(byte[] str){
  379. return byte2str(str, "UTF-8");
  380. }
  381. /*
  382. static byte[] char2byte(char[] foo){
  383. int len=0;
  384. for(int i=0; i<foo.length; i++){
  385. if((foo[i]&0xff00)==0) len++;
  386. else len+=2;
  387. }
  388. byte[] bar=new byte[len];
  389. for(int i=0, j=0; i<foo.length; i++){
  390. if((foo[i]&0xff00)==0){
  391. bar[j++]=(byte)foo[i];
  392. }
  393. else{
  394. bar[j++]=(byte)(foo[i]>>>8);
  395. bar[j++]=(byte)foo[i];
  396. }
  397. }
  398. return bar;
  399. }
  400. */
  401. static void bzero(byte[] foo){
  402. if(foo==null)
  403. return;
  404. for(int i=0; i<foo.length; i++)
  405. foo[i]=0;
  406. }
  407. static String diffString(String str, String[] not_available){
  408. String[] stra=Util.split(str, ",");
  409. String result=null;
  410. loop:
  411. for(int i=0; i<stra.length; i++){
  412. for(int j=0; j<not_available.length; j++){
  413. if(stra[i].equals(not_available[j])){
  414. continue loop;
  415. }
  416. }
  417. if(result==null){ result=stra[i]; }
  418. else{ result=result+","+stra[i]; }
  419. }
  420. return result;
  421. }
  422. private static int skipUTF8Char(byte b){
  423. if((byte)(b&0x80)==0) return 1;
  424. if((byte)(b&0xe0)==(byte)0xc0) return 2;
  425. if((byte)(b&0xf0)==(byte)0xe0) return 3;
  426. return 1;
  427. }
  428. }