00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package com.jcraft.jsch;
00031 import java.net.Socket;
00032
00033 class Util{
00034
00035 private static final byte[] b64 =Util.str2byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=");
00036 private static byte val(byte foo){
00037 if(foo == '=') return 0;
00038 for(int j=0; j<b64.length; j++){
00039 if(foo==b64[j]) return (byte)j;
00040 }
00041 return 0;
00042 }
00043 static byte[] fromBase64(byte[] buf, int start, int length){
00044 byte[] foo=new byte[length];
00045 int j=0;
00046 for (int i=start;i<start+length;i+=4){
00047 foo[j]=(byte)((val(buf[i])<<2)|((val(buf[i+1])&0x30)>>>4));
00048 if(buf[i+2]==(byte)'='){ j++; break;}
00049 foo[j+1]=(byte)(((val(buf[i+1])&0x0f)<<4)|((val(buf[i+2])&0x3c)>>>2));
00050 if(buf[i+3]==(byte)'='){ j+=2; break;}
00051 foo[j+2]=(byte)(((val(buf[i+2])&0x03)<<6)|(val(buf[i+3])&0x3f));
00052 j+=3;
00053 }
00054 byte[] bar=new byte[j];
00055 System.arraycopy(foo, 0, bar, 0, j);
00056 return bar;
00057 }
00058 static byte[] toBase64(byte[] buf, int start, int length){
00059
00060 byte[] tmp=new byte[length*2];
00061 int i,j,k;
00062
00063 int foo=(length/3)*3+start;
00064 i=0;
00065 for(j=start; j<foo; j+=3){
00066 k=(buf[j]>>>2)&0x3f;
00067 tmp[i++]=b64[k];
00068 k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f;
00069 tmp[i++]=b64[k];
00070 k=(buf[j+1]&0x0f)<<2|(buf[j+2]>>>6)&0x03;
00071 tmp[i++]=b64[k];
00072 k=buf[j+2]&0x3f;
00073 tmp[i++]=b64[k];
00074 }
00075
00076 foo=(start+length)-foo;
00077 if(foo==1){
00078 k=(buf[j]>>>2)&0x3f;
00079 tmp[i++]=b64[k];
00080 k=((buf[j]&0x03)<<4)&0x3f;
00081 tmp[i++]=b64[k];
00082 tmp[i++]=(byte)'=';
00083 tmp[i++]=(byte)'=';
00084 }
00085 else if(foo==2){
00086 k=(buf[j]>>>2)&0x3f;
00087 tmp[i++]=b64[k];
00088 k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f;
00089 tmp[i++]=b64[k];
00090 k=((buf[j+1]&0x0f)<<2)&0x3f;
00091 tmp[i++]=b64[k];
00092 tmp[i++]=(byte)'=';
00093 }
00094 byte[] bar=new byte[i];
00095 System.arraycopy(tmp, 0, bar, 0, i);
00096 return bar;
00097
00098
00099 }
00100
00101 static String[] split(String foo, String split){
00102 if(foo==null)
00103 return null;
00104 byte[] buf=Util.str2byte(foo);
00105 java.util.Vector bar=new java.util.Vector();
00106 int start=0;
00107 int index;
00108 while(true){
00109 index=foo.indexOf(split, start);
00110 if(index>=0){
00111 bar.addElement(Util.byte2str(buf, start, index-start));
00112 start=index+1;
00113 continue;
00114 }
00115 bar.addElement(Util.byte2str(buf, start, buf.length-start));
00116 break;
00117 }
00118 String[] result=new String[bar.size()];
00119 for(int i=0; i<result.length; i++){
00120 result[i]=(String)(bar.elementAt(i));
00121 }
00122 return result;
00123 }
00124 static boolean glob(byte[] pattern, byte[] name){
00125 return glob0(pattern, 0, name, 0);
00126 }
00127 static private boolean glob0(byte[] pattern, int pattern_index,
00128 byte[] name, int name_index){
00129 if(name.length>0 && name[0]=='.'){
00130 if(pattern.length>0 && pattern[0]=='.'){
00131 if(pattern.length==2 && pattern[1]=='*') return true;
00132 return glob(pattern, pattern_index+1, name, name_index+1);
00133 }
00134 return false;
00135 }
00136 return glob(pattern, pattern_index, name, name_index);
00137 }
00138 static private boolean glob(byte[] pattern, int pattern_index,
00139 byte[] name, int name_index){
00140
00141
00142 int patternlen=pattern.length;
00143 if(patternlen==0)
00144 return false;
00145
00146 int namelen=name.length;
00147 int i=pattern_index;
00148 int j=name_index;
00149
00150 while(i<patternlen && j<namelen){
00151 if(pattern[i]=='\\'){
00152 if(i+1==patternlen)
00153 return false;
00154 i++;
00155 if(pattern[i]!=name[j])
00156 return false;
00157 i+=skipUTF8Char(pattern[i]);
00158 j+=skipUTF8Char(name[j]);
00159 continue;
00160 }
00161
00162 if(pattern[i]=='*'){
00163 while(i<patternlen){
00164 if(pattern[i]=='*'){
00165 i++;
00166 continue;
00167 }
00168 break;
00169 }
00170 if(patternlen==i)
00171 return true;
00172
00173 byte foo=pattern[i];
00174 if(foo=='?'){
00175 while(j<namelen){
00176 if(glob(pattern, i, name, j)){
00177 return true;
00178 }
00179 j+=skipUTF8Char(name[j]);
00180 }
00181 return false;
00182 }
00183 else if(foo=='\\'){
00184 if(i+1==patternlen)
00185 return false;
00186 i++;
00187 foo=pattern[i];
00188 while(j<namelen){
00189 if(foo==name[j]){
00190 if(glob(pattern, i+skipUTF8Char(foo),
00191 name, j+skipUTF8Char(name[j]))){
00192 return true;
00193 }
00194 }
00195 j+=skipUTF8Char(name[j]);
00196 }
00197 return false;
00198 }
00199
00200 while(j<namelen){
00201 if(foo==name[j]){
00202 if(glob(pattern, i, name, j)){
00203 return true;
00204 }
00205 }
00206 j+=skipUTF8Char(name[j]);
00207 }
00208 return false;
00209 }
00210
00211 if(pattern[i]=='?'){
00212 i++;
00213 j+=skipUTF8Char(name[j]);
00214 continue;
00215 }
00216
00217 if(pattern[i]!=name[j])
00218 return false;
00219
00220 i+=skipUTF8Char(pattern[i]);
00221 j+=skipUTF8Char(name[j]);
00222
00223 if(!(j<namelen)){
00224 if(!(i<patternlen)){
00225 return true;
00226 }
00227 if(pattern[i]=='*'){
00228 break;
00229 }
00230 }
00231 continue;
00232 }
00233
00234 if(i==patternlen && j==namelen)
00235 return true;
00236
00237 if(!(j<namelen) &&
00238 pattern[i]=='*'){
00239 boolean ok=true;
00240 while(i<patternlen){
00241 if(pattern[i++]!='*'){
00242 ok=false;
00243 break;
00244 }
00245 }
00246 return ok;
00247 }
00248
00249 return false;
00250 }
00251
00252 static String quote(String path){
00253 byte[] _path=str2byte(path);
00254 int count=0;
00255 for(int i=0;i<_path.length; i++){
00256 byte b=_path[i];
00257 if(b=='\\' || b=='?' || b=='*')
00258 count++;
00259 }
00260 if(count==0)
00261 return path;
00262 byte[] _path2=new byte[_path.length+count];
00263 for(int i=0, j=0; i<_path.length; i++){
00264 byte b=_path[i];
00265 if(b=='\\' || b=='?' || b=='*'){
00266 _path2[j++]='\\';
00267 }
00268 _path2[j++]=b;
00269 }
00270 return byte2str(_path2);
00271 }
00272
00273 static String unquote(String path){
00274 byte[] foo=str2byte(path);
00275 byte[] bar=unquote(foo);
00276 if(foo.length==bar.length)
00277 return path;
00278 return byte2str(bar);
00279 }
00280 static byte[] unquote(byte[] path){
00281 int pathlen=path.length;
00282 int i=0;
00283 while(i<pathlen){
00284 if(path[i]=='\\'){
00285 if(i+1==pathlen)
00286 break;
00287 System.arraycopy(path, i+1, path, i, path.length-(i+1));
00288 pathlen--;
00289 i++;
00290 continue;
00291 }
00292 i++;
00293 }
00294 if(pathlen==path.length)
00295 return path;
00296 byte[] foo=new byte[pathlen];
00297 System.arraycopy(path, 0, foo, 0, pathlen);
00298 return foo;
00299 }
00300
00302 private static String[] chars={
00303 "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
00304 };
00305
00312 static String getFingerPrint(HASH hash, byte[] data){
00313 try{
00314 hash.init();
00315 hash.update(data, 0, data.length);
00316 byte[] foo=hash.digest();
00317 StringBuffer sb=new StringBuffer();
00318 int bar;
00319 for(int i=0; i<foo.length;i++){
00320 bar=foo[i]&0xff;
00321 sb.append(chars[(bar>>>4)&0xf]);
00322 sb.append(chars[(bar)&0xf]);
00323 if(i+1<foo.length)
00324 sb.append(":");
00325 }
00326 return sb.toString();
00327 }
00328 catch(Exception e){
00329 return "???";
00330 }
00331 }
00332 static boolean array_equals(byte[] foo, byte bar[]){
00333 int i=foo.length;
00334 if(i!=bar.length) return false;
00335 for(int j=0; j<i; j++){ if(foo[j]!=bar[j]) return false; }
00336
00337 return true;
00338 }
00339 static Socket createSocket(String host, int port, int timeout) throws JSchException{
00340 Socket socket=null;
00341 if(timeout==0){
00342 try{
00343 socket=new Socket(host, port);
00344 return socket;
00345 }
00346 catch(Exception e){
00347 String message=e.toString();
00348 if(e instanceof Throwable)
00349 throw new JSchException(message, (Throwable)e);
00350 throw new JSchException(message);
00351 }
00352 }
00353 final String _host=host;
00354 final int _port=port;
00355 final Socket[] sockp=new Socket[1];
00356 final Exception[] ee=new Exception[1];
00357 String message="";
00358 Thread tmp=new Thread(new Runnable(){
00359 public void run(){
00360 sockp[0]=null;
00361 try{
00362 sockp[0]=new Socket(_host, _port);
00363 }
00364 catch(Exception e){
00365 ee[0]=e;
00366 if(sockp[0]!=null && sockp[0].isConnected()){
00367 try{
00368 sockp[0].close();
00369 }
00370 catch(Exception eee){}
00371 }
00372 sockp[0]=null;
00373 }
00374 }
00375 });
00376 tmp.setName("Opening Socket "+host);
00377 tmp.start();
00378 try{
00379 tmp.join(timeout);
00380 message="timeout: ";
00381 }
00382 catch(java.lang.InterruptedException eee){
00383 }
00384 if(sockp[0]!=null && sockp[0].isConnected()){
00385 socket=sockp[0];
00386 }
00387 else{
00388 message+="socket is not established";
00389 if(ee[0]!=null){
00390 message=ee[0].toString();
00391 }
00392 tmp.interrupt();
00393 tmp=null;
00394 throw new JSchException(message);
00395 }
00396 return socket;
00397 }
00398
00399 static byte[] str2byte(String str, String encoding){
00400 if(str==null)
00401 return null;
00402 try{ return str.getBytes(encoding); }
00403 catch(java.io.UnsupportedEncodingException e){
00404 return str.getBytes();
00405 }
00406 }
00407
00408 static byte[] str2byte(String str){
00409 return str2byte(str, "UTF-8");
00410 }
00411
00412 static String byte2str(byte[] str, String encoding){
00413 return byte2str(str, 0, str.length, encoding);
00414 }
00415
00416 static String byte2str(byte[] str, int s, int l, String encoding){
00417 try{ return new String(str, s, l, encoding); }
00418 catch(java.io.UnsupportedEncodingException e){
00419 return new String(str, s, l);
00420 }
00421 }
00422
00423 static String byte2str(byte[] str){
00424 return byte2str(str, 0, str.length, "UTF-8");
00425 }
00426
00427 static String byte2str(byte[] str, int s, int l){
00428 return byte2str(str, s, l, "UTF-8");
00429 }
00430
00431 static final byte[] empty = str2byte("");
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 static void bzero(byte[] foo){
00454 if(foo==null)
00455 return;
00456 for(int i=0; i<foo.length; i++)
00457 foo[i]=0;
00458 }
00459
00460 static String diffString(String str, String[] not_available){
00461 String[] stra=Util.split(str, ",");
00462 String result=null;
00463 loop:
00464 for(int i=0; i<stra.length; i++){
00465 for(int j=0; j<not_available.length; j++){
00466 if(stra[i].equals(not_available[j])){
00467 continue loop;
00468 }
00469 }
00470 if(result==null){ result=stra[i]; }
00471 else{ result=result+","+stra[i]; }
00472 }
00473 return result;
00474 }
00475
00476 private static int skipUTF8Char(byte b){
00477 if((byte)(b&0x80)==0) return 1;
00478 if((byte)(b&0xe0)==(byte)0xc0) return 2;
00479 if((byte)(b&0xf0)==(byte)0xe0) return 3;
00480 return 1;
00481 }
00482 }