1 2 29 30 package com.jcraft.jsch; 31 import java.net.Socket ; 32 33 class Util{ 34 35 private static final byte[] b64 ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".getBytes(); 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; 40 } 41 return 0; 42 } 43 static byte[] fromBase64(byte[] buf, int start, int length){ 44 byte[] foo=new byte[length]; 45 int j=0; 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)); 52 j+=3; 53 } 54 byte[] bar=new byte[j]; 55 System.arraycopy(foo, 0, bar, 0, j); 56 return bar; 57 } 58 static byte[] toBase64(byte[] buf, int start, int length){ 59 60 byte[] tmp=new byte[length*2]; 61 int i,j,k; 62 63 int foo=(length/3)*3+start; 64 i=0; 65 for(j=start; j<foo; j+=3){ 66 k=(buf[j]>>>2)&0x3f; 67 tmp[i++]=b64[k]; 68 k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f; 69 tmp[i++]=b64[k]; 70 k=(buf[j+1]&0x0f)<<2|(buf[j+2]>>>6)&0x03; 71 tmp[i++]=b64[k]; 72 k=buf[j+2]&0x3f; 73 tmp[i++]=b64[k]; 74 } 75 76 foo=(start+length)-foo; 77 if(foo==1){ 78 k=(buf[j]>>>2)&0x3f; 79 tmp[i++]=b64[k]; 80 k=((buf[j]&0x03)<<4)&0x3f; 81 tmp[i++]=b64[k]; 82 tmp[i++]=(byte)'='; 83 tmp[i++]=(byte)'='; 84 } 85 else if(foo==2){ 86 k=(buf[j]>>>2)&0x3f; 87 tmp[i++]=b64[k]; 88 k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f; 89 tmp[i++]=b64[k]; 90 k=((buf[j+1]&0x0f)<<2)&0x3f; 91 tmp[i++]=b64[k]; 92 tmp[i++]=(byte)'='; 93 } 94 byte[] bar=new byte[i]; 95 System.arraycopy(tmp, 0, bar, 0, i); 96 return bar; 97 98 } 100 101 static String [] split(String foo, String split){ 102 if(foo==null) 103 return null; 104 byte[] buf=foo.getBytes(); 105 java.util.Vector bar=new java.util.Vector (); 106 int start=0; 107 int index; 108 while(true){ 109 index=foo.indexOf(split, start); 110 if(index>=0){ 111 bar.addElement(new String (buf, start, index-start)); 112 start=index+1; 113 continue; 114 } 115 bar.addElement(new String (buf, start, buf.length-start)); 116 break; 117 } 118 String [] result=new String [bar.size()]; 119 for(int i=0; i<result.length; i++){ 120 result[i]=(String )(bar.elementAt(i)); 121 } 122 return result; 123 } 124 static boolean glob(byte[] pattern, byte[] name){ 125 return glob0(pattern, 0, name, 0); 126 } 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); 133 } 134 return false; 135 } 136 return glob(pattern, pattern_index, name, name_index); 137 } 138 static private boolean glob(byte[] pattern, int pattern_index, 139 byte[] name, int name_index){ 140 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 if(pattern[i]=='\\'){ 150 if(i+1==patternlen) 151 return false; 152 i++; 153 if(pattern[i]!=name[j]) return false; 154 i++; j++; 155 continue; 156 } 157 if(pattern[i]=='*'){ 158 if(patternlen==i+1) return true; 159 i++; 160 byte foo=pattern[i]; 161 while(j<namelen){ 162 if(foo==name[j]){ 163 if(glob(pattern, i, name, j)){ 164 return true; 165 } 166 } 167 j++; 168 } 169 return false; 170 175 } 176 if(pattern[i]=='?'){ 177 i++; j++; 178 continue; 179 } 180 if(pattern[i]!=name[j]) return false; 181 i++; j++; 182 if(!(j<namelen)){ 183 if(!(i<patternlen)){ 184 return true; 185 } 186 if(pattern[i]=='*' 187 ){ 189 return true; 190 } 191 } 192 continue; 193 } 194 if(i==patternlen && j==namelen) return true; 195 return false; 196 } 197 198 static String unquote(String _path){ 199 byte[] path=_path.getBytes(); 200 int pathlen=path.length; 201 int i=0; 202 while(i<pathlen){ 203 if(path[i]=='\\'){ 204 if(i+1==pathlen) 205 break; 206 System.arraycopy(path, i+1, path, i, path.length-(i+1)); 207 pathlen--; 208 continue; 209 } 210 i++; 211 } 212 if(pathlen==path.length)return _path; 213 byte[] foo=new byte[pathlen]; 214 System.arraycopy(path, 0, foo, 0, pathlen); 215 return new String (foo); 216 } 217 218 private static String [] chars={ 219 "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f" 220 }; 221 static String getFingerPrint(HASH hash, byte[] data){ 222 try{ 223 hash.init(); 224 hash.update(data, 0, data.length); 225 byte[] foo=hash.digest(); 226 StringBuffer sb=new StringBuffer (); 227 int bar; 228 for(int i=0; i<foo.length;i++){ 229 bar=foo[i]&0xff; 230 sb.append(chars[(bar>>>4)&0xf]); 231 sb.append(chars[(bar)&0xf]); 232 if(i+1<foo.length) 233 sb.append(":"); 234 } 235 return sb.toString(); 236 } 237 catch(Exception e){ 238 return "???"; 239 } 240 } 241 static boolean array_equals(byte[] foo, byte bar[]){ 242 int i=foo.length; 243 if(i!=bar.length) return false; 244 for(int j=0; j<i; j++){ if(foo[j]!=bar[j]) return false; } 245 return true; 247 } 248 static Socket createSocket(String host, int port, int timeout) throws JSchException{ 249 Socket socket=null; 250 if(timeout==0){ 251 try{ 252 socket=new Socket (host, port); 253 return socket; 254 } 255 catch(Exception e){ 256 String message=e.toString(); 257 if(e instanceof Throwable ) 258 throw new JSchException(message, (Throwable )e); 259 throw new JSchException(message); 260 } 261 } 262 final String _host=host; 263 final int _port=port; 264 final Socket [] sockp=new Socket [1]; 265 final Exception [] ee=new Exception [1]; 266 String message=""; 267 Thread tmp=new Thread (new Runnable (){ 268 public void run(){ 269 sockp[0]=null; 270 try{ 271 sockp[0]=new Socket (_host, _port); 272 } 273 catch(Exception e){ 274 ee[0]=e; 275 if(sockp[0]!=null && sockp[0].isConnected()){ 276 try{ 277 sockp[0].close(); 278 } 279 catch(Exception eee){} 280 } 281 sockp[0]=null; 282 } 283 } 284 }); 285 tmp.setName("Opening Socket "+host); 286 tmp.start(); 287 try{ 288 tmp.join(timeout); 289 message="timeout: "; 290 } 291 catch(java.lang.InterruptedException eee){ 292 } 293 if(sockp[0]!=null && sockp[0].isConnected()){ 294 socket=sockp[0]; 295 } 296 else{ 297 message+="socket is not established"; 298 if(ee[0]!=null){ 299 message=ee[0].toString(); 300 } 301 tmp.interrupt(); 302 tmp=null; 303 throw new JSchException(message); 304 } 305 return socket; 306 } 307 308 static byte[] str2byte(String str){ 309 if(str==null) 310 return null; 311 try{ return str.getBytes("UTF-8"); } 312 catch(java.io.UnsupportedEncodingException e){ 313 return str.getBytes(); 314 } 315 } 316 static String byte2str(byte[] str){ 317 try{ return new String (str, "UTF-8"); } 318 catch(java.io.UnsupportedEncodingException e){ 319 return new String (str); 320 } 321 } 322 323 343 static void bzero(byte[] foo){ 344 if(foo==null) 345 return; 346 for(int i=0; i<foo.length; i++) 347 foo[i]=0; 348 } 349 } 350 | Popular Tags |