1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 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.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 package com.jcraft.jsch;
31 import java.net.Socket;
35 private static final byte[] b64 =Util.str2byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=");
36 private static byte val(byte foo){
37 if(foo == '=') return 0;
38 for(int j=0; j<b64.length; j++){
39 if(foo==b64[j]) return (byte)j;
43 static byte[] fromBase64(byte[] buf, int start, int length){
44 byte[] foo=new byte[length];
46 for (int i=start;i<start+length;i+=4){
47 foo[j]=(byte)((val(buf[i])<<2)|((val(buf[i+1])&0x30)>>>4));
48 if(buf[i+2]==(byte)'='){ j++; break;}
49 foo[j+1]=(byte)(((val(buf[i+1])&0x0f)<<4)|((val(buf[i+2])&0x3c)>>>2));
50 if(buf[i+3]==(byte)'='){ j+=2; break;}
51 foo[j+2]=(byte)(((val(buf[i+2])&0x03)<<6)|(val(buf[i+3])&0x3f));
54 byte[] bar=new byte[j];
55 System.arraycopy(foo, 0, bar, 0, j);
58 static byte[] toBase64(byte[] buf, int start, int length){
60 byte[] tmp=new byte[length*2];
63 int foo=(length/3)*3+start;
65 for(j=start; j<foo; j+=3){
68 k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f;
70 k=(buf[j+1]&0x0f)<<2|(buf[j+2]>>>6)&0x03;
76 foo=(start+length)-foo;
80 k=((buf[j]&0x03)<<4)&0x3f;
88 k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f;
90 k=((buf[j+1]&0x0f)<<2)&0x3f;
94 byte[] bar=new byte[i];
95 System.arraycopy(tmp, 0, bar, 0, i);
98 // return sun.misc.BASE64Encoder().encode(buf);
101 static String[] split(String foo, String split){
104 byte[] buf=Util.str2byte(foo);
105 java.util.Vector bar=new java.util.Vector();
109 index=foo.indexOf(split, start);
111 bar.addElement(Util.byte2str(buf, start, index-start));
115 bar.addElement(Util.byte2str(buf, start, buf.length-start));
118 String[] result=new String[bar.size()];
119 for(int i=0; i<result.length; i++){
120 result[i]=(String)(bar.elementAt(i));
124 static boolean glob(byte[] pattern, byte[] name){
125 return glob0(pattern, 0, name, 0);
127 static private boolean glob0(byte[] pattern, int pattern_index,
128 byte[] name, int name_index){
129 if(name.length>0 && name[0]=='.'){
130 if(pattern.length>0 && pattern[0]=='.'){
131 if(pattern.length==2 && pattern[1]=='*') return true;
132 return glob(pattern, pattern_index+1, name, name_index+1);
136 return glob(pattern, pattern_index, name, name_index);
138 static private boolean glob(byte[] pattern, int pattern_index,
139 byte[] name, int name_index){
140 //System.err.println("glob: "+new String(pattern)+", "+pattern_index+" "+new String(name)+", "+name_index);
142 int patternlen=pattern.length;
146 int namelen=name.length;
150 while(i<patternlen && j<namelen){
151 if(pattern[i]=='\\'){
155 if(pattern[i]!=name[j])
157 i+=skipUTF8Char(pattern[i]);
158 j+=skipUTF8Char(name[j]);
176 if(glob(pattern, i, name, j)){
179 j+=skipUTF8Char(name[j]);
190 if(glob(pattern, i+skipUTF8Char(foo),
191 name, j+skipUTF8Char(name[j]))){
195 j+=skipUTF8Char(name[j]);
202 if(glob(pattern, i, name, j)){
206 j+=skipUTF8Char(name[j]);
213 j+=skipUTF8Char(name[j]);
217 if(pattern[i]!=name[j])
220 i+=skipUTF8Char(pattern[i]);
221 j+=skipUTF8Char(name[j]);
223 if(!(j<namelen)){ // name is end
224 if(!(i<patternlen)){ // pattern is end
234 if(i==patternlen && j==namelen)
237 if(!(j<namelen) && // name is end
241 if(pattern[i++]!='*'){
252 static String quote(String path){
253 byte[] _path=str2byte(path);
255 for(int i=0;i<_path.length; i++){
257 if(b=='\\' || b=='?' || b=='*')
262 byte[] _path2=new byte[_path.length+count];
263 for(int i=0, j=0; i<_path.length; i++){
265 if(b=='\\' || b=='?' || b=='*'){
270 return byte2str(_path2);
273 static String unquote(String path){
274 byte[] foo=str2byte(path);
275 byte[] bar=unquote(foo);
276 if(foo.length==bar.length)
278 return byte2str(bar);
280 static byte[] unquote(byte[] path){
281 int pathlen=path.length;
287 System.arraycopy(path, i+1, path, i, path.length-(i+1));
294 if(pathlen==path.length)
296 byte[] foo=new byte[pathlen];
297 System.arraycopy(path, 0, foo, 0, pathlen);
301 private static String[] chars={
302 "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
304 static String getFingerPrint(HASH hash, byte[] data){
307 hash.update(data, 0, data.length);
308 byte[] foo=hash.digest();
309 StringBuffer sb=new StringBuffer();
311 for(int i=0; i<foo.length;i++){
313 sb.append(chars[(bar>>>4)&0xf]);
314 sb.append(chars[(bar)&0xf]);
318 return sb.toString();
324 static boolean array_equals(byte[] foo, byte bar[]){
326 if(i!=bar.length) return false;
327 for(int j=0; j<i; j++){ if(foo[j]!=bar[j]) return false; }
328 //try{while(true){i--; if(foo[i]!=bar[i])return false;}}catch(Exception e){}
331 static Socket createSocket(String host, int port, int timeout) throws JSchException{
335 socket=new Socket(host, port);
339 String message=e.toString();
340 if(e instanceof Throwable)
341 throw new JSchException(message, (Throwable)e);
342 throw new JSchException(message);
345 final String _host=host;
346 final int _port=port;
347 final Socket[] sockp=new Socket[1];
348 final Exception[] ee=new Exception[1];
350 Thread tmp=new Thread(new Runnable(){
354 sockp[0]=new Socket(_host, _port);
358 if(sockp[0]!=null && sockp[0].isConnected()){
362 catch(Exception eee){}
368 tmp.setName("Opening Socket "+host);
374 catch(java.lang.InterruptedException eee){
376 if(sockp[0]!=null && sockp[0].isConnected()){
380 message+="socket is not established";
382 message=ee[0].toString();
386 throw new JSchException(message);
391 static byte[] str2byte(String str, String encoding){
394 try{ return str.getBytes(encoding); }
395 catch(java.io.UnsupportedEncodingException e){
396 return str.getBytes();
400 static byte[] str2byte(String str){
401 return str2byte(str, "UTF-8");
404 static String byte2str(byte[] str, String encoding){
405 return byte2str(str, 0, str.length, encoding);
408 static String byte2str(byte[] str, int s, int l, String encoding){
409 try{ return new String(str, s, l, encoding); }
410 catch(java.io.UnsupportedEncodingException e){
411 return new String(str, s, l);
415 static String byte2str(byte[] str){
416 return byte2str(str, 0, str.length, "UTF-8");
419 static String byte2str(byte[] str, int s, int l){
420 return byte2str(str, s, l, "UTF-8");
423 static final byte[] empty = str2byte("");
426 static byte[] char2byte(char[] foo){
428 for(int i=0; i<foo.length; i++){
429 if((foo[i]&0xff00)==0) len++;
432 byte[] bar=new byte[len];
433 for(int i=0, j=0; i<foo.length; i++){
434 if((foo[i]&0xff00)==0){
435 bar[j++]=(byte)foo[i];
438 bar[j++]=(byte)(foo[i]>>>8);
439 bar[j++]=(byte)foo[i];
445 static void bzero(byte[] foo){
448 for(int i=0; i<foo.length; i++)
452 static String diffString(String str, String[] not_available){
453 String[] stra=Util.split(str, ",");
456 for(int i=0; i<stra.length; i++){
457 for(int j=0; j<not_available.length; j++){
458 if(stra[i].equals(not_available[j])){
462 if(result==null){ result=stra[i]; }
463 else{ result=result+","+stra[i]; }
468 private static int skipUTF8Char(byte b){
469 if((byte)(b&0x80)==0) return 1;
470 if((byte)(b&0xe0)==(byte)0xc0) return 2;
471 if((byte)(b&0xf0)==(byte)0xe0) return 3;