00001 using System;
00002 using System.Threading;
00003
00004 namespace Tamir.SharpSsh.jsch
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
00031
00032
00033
00034
00035
00036 public class Util
00037 {
00038
00042 public static DateTime Time_T2DateTime(uint time_t)
00043 {
00044 long win32FileTime = 10000000*(long)time_t + 116444736000000000;
00045 return DateTime.FromFileTimeUtc(win32FileTime).ToLocalTime();
00046 }
00047
00048 private static byte[] b64 = System.Text.Encoding.Default.GetBytes( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=");
00049 private static byte val(byte foo)
00050 {
00051 if(foo == '=') return 0;
00052 for(int j=0; j<b64.Length; j++)
00053 {
00054 if(foo==b64[j]) return (byte)j;
00055 }
00056 return 0;
00057 }
00058 internal static byte[] fromBase64(byte[] buf, int start, int length)
00059 {
00060 byte[] foo=new byte[length];
00061 int j=0;
00062 int l=length;
00063 for (int i=start;i<start+length;i+=4)
00064 {
00065 foo[j]=(byte)((val(buf[i])<<2)|((val(buf[i+1])&0x30)>>4));
00066 if(buf[i+2]==(byte)'='){ j++; break;}
00067 foo[j+1]=(byte)(((val(buf[i+1])&0x0f)<<4)|((val(buf[i+2])&0x3c)>>2));
00068 if(buf[i+3]==(byte)'='){ j+=2; break;}
00069 foo[j+2]=(byte)(((val(buf[i+2])&0x03)<<6)|(val(buf[i+3])&0x3f));
00070 j+=3;
00071 }
00072 byte[] bar=new byte[j];
00073 Array.Copy(foo, 0, bar, 0, j);
00074 return bar;
00075 }
00076 internal static byte[] toBase64(byte[] buf, int start, int length)
00077 {
00078
00079 byte[] tmp=new byte[length*2];
00080 int i,j,k;
00081
00082 int foo=(length/3)*3+start;
00083 i=0;
00084 for(j=start; j<foo; j+=3)
00085 {
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|(buf[j+2]>>6)&0x03;
00091 tmp[i++]=b64[k];
00092 k=buf[j+2]&0x3f;
00093 tmp[i++]=b64[k];
00094 }
00095
00096 foo=(start+length)-foo;
00097 if(foo==1)
00098 {
00099 k=(buf[j]>>2)&0x3f;
00100 tmp[i++]=b64[k];
00101 k=((buf[j]&0x03)<<4)&0x3f;
00102 tmp[i++]=b64[k];
00103 tmp[i++]=(byte)'=';
00104 tmp[i++]=(byte)'=';
00105 }
00106 else if(foo==2)
00107 {
00108 k=(buf[j]>>2)&0x3f;
00109 tmp[i++]=b64[k];
00110 k=(buf[j]&0x03)<<4|(buf[j+1]>>4)&0x0f;
00111 tmp[i++]=b64[k];
00112 k=((buf[j+1]&0x0f)<<2)&0x3f;
00113 tmp[i++]=b64[k];
00114 tmp[i++]=(byte)'=';
00115 }
00116 byte[] bar=new byte[i];
00117 Array.Copy(tmp, 0, bar, 0, i);
00118 return bar;
00119
00120
00121 }
00122
00123 internal static String[] split(String foo, String split)
00124 {
00125 byte[] buf=Util.getBytes(foo);
00126 System.Collections.ArrayList bar=new System.Collections.ArrayList();
00127 int start=0;
00128 int index;
00129 while(true)
00130 {
00131 index=foo.IndexOf(split, start);
00132 if(index>=0)
00133 {
00134 bar.Add(Util.getString(buf, start, index-start));
00135 start=index+1;
00136 continue;
00137 }
00138 bar.Add(Util.getString(buf, start, buf.Length-start));
00139 break;
00140 }
00141 String[] result=new String[bar.Count];
00142 for(int i=0; i<result.Length; i++)
00143 {
00144 result[i]=(String)(bar[i]);
00145 }
00146 return result;
00147 }
00148 internal static bool glob(byte[] pattern, byte[] name)
00149 {
00150 return glob(pattern, 0, name, 0);
00151 }
00152 private static bool glob(byte[] pattern, int pattern_index,
00153 byte[] name, int name_index)
00154 {
00155
00156 int patternlen=pattern.Length;
00157 if(patternlen==0)
00158 return false;
00159 int namelen=name.Length;
00160 int i=pattern_index;
00161 int j=name_index;
00162 while(i<patternlen && j<namelen)
00163 {
00164 if(pattern[i]=='\\')
00165 {
00166 if(i+1==patternlen)
00167 return false;
00168 i++;
00169 if(pattern[i]!=name[j]) return false;
00170 i++; j++;
00171 continue;
00172 }
00173 if(pattern[i]=='*')
00174 {
00175 if(patternlen==i+1) return true;
00176 i++;
00177 byte foo=pattern[i];
00178 while(j<namelen)
00179 {
00180 if(foo==name[j])
00181 {
00182 if(glob(pattern, i, name, j))
00183 {
00184 return true;
00185 }
00186 }
00187 j++;
00188 }
00189 return false;
00190
00191
00192
00193
00194
00195 }
00196 if(pattern[i]=='?')
00197 {
00198 i++; j++;
00199 continue;
00200 }
00201 if(pattern[i]!=name[j]) return false;
00202 i++; j++;
00203 continue;
00204 }
00205 if(i==patternlen && j==namelen) return true;
00206 return false;
00207 }
00208
00209 private static String[] chars={
00210 "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
00211 };
00212 internal static String getFingerPrint(HASH hash, byte[] data)
00213 {
00214 try
00215 {
00216 hash.init();
00217 hash.update(data, 0, data.Length);
00218 byte[] foo=hash.digest();
00219 System.Text.StringBuilder sb=new System.Text.StringBuilder();
00220 uint bar;
00221 for(int i=0; i<foo.Length;i++)
00222 {
00223 bar=(byte)(foo[i]&0xff);
00224 sb.Append(chars[(bar>>4)&0xf]);
00225 sb.Append(chars[(bar)&0xf]);
00226 if(i+1<foo.Length)
00227 sb.Append(":");
00228 }
00229 return sb.ToString();
00230 }
00231 catch
00232 {
00233 return "???";
00234 }
00235 }
00236
00237 internal static SharpSsh.java.net.Socket createSocket(String host, int port, int timeout)
00238 {
00239 SharpSsh.java.net.Socket socket=null;
00240 String message="";
00241 if(timeout==0)
00242 {
00243 try
00244 {
00245 socket=new SharpSsh.java.net.Socket(host, port);
00246 return socket;
00247 }
00248 catch(Exception e)
00249 {
00250 message=e.ToString();
00251 throw new JSchException(message);
00252 }
00253 }
00254 String _host=host;
00255 int _port=port;
00256 SharpSsh.java.net.Socket[] sockp=new SharpSsh.java.net.Socket[1];
00257 Thread currentThread=Thread.CurrentThread;
00258 Exception[] ee=new Exception[1];
00259 message="";
00260 createSocketRun runnable = new createSocketRun(sockp, ee, _host, _port);
00261 Thread tmp=new Thread(new ThreadStart(runnable.run));
00262 tmp.Name = "Opening Socket "+host;
00263 tmp.Start();
00264 try
00265 {
00266 tmp.Join(timeout);
00267 message="timeout: ";
00268 }
00269 catch(ThreadInterruptedException eee)
00270 {
00271 }
00272 if(sockp[0]!=null && sockp[0].isConnected())
00273 {
00274 socket=sockp[0];
00275 }
00276 else
00277 {
00278 message+="socket is not established";
00279 if(ee[0]!=null)
00280 {
00281 message=ee[0].ToString();
00282 }
00283 tmp.Interrupt();
00284 tmp=null;
00285 throw new JSchException(message);
00286 }
00287 return socket;
00288 }
00289
00290 private class createSocketRun
00291 {
00292 SharpSsh.java.net.Socket[] sockp;
00293 Exception[] ee;
00294 string _host;
00295 int _port;
00296
00297 public createSocketRun(SharpSsh.java.net.Socket[] sockp, Exception[] ee, string _host, int _port)
00298 {
00299 this.sockp=sockp;
00300 this.ee=ee;
00301 this._host=_host;
00302 this._port=_port;
00303 }
00304
00305 public void run()
00306 {
00307 sockp[0]=null;
00308 try
00309 {
00310 sockp[0]=new SharpSsh.java.net.Socket(_host, _port);
00311 }
00312 catch(Exception e)
00313 {
00314 ee[0]=e;
00315 if(sockp[0]!=null && sockp[0].isConnected())
00316 {
00317 try
00318 {
00319 sockp[0].close();
00320 }
00321 catch(Exception eee){}
00322 }
00323 sockp[0]=null;
00324 }
00325 }
00326 }
00327
00328 internal static bool array_equals(byte[] foo, byte[] bar)
00329 {
00330 int i=foo.Length;
00331 if(i!=bar.Length) return false;
00332 for(int j=0; j<i; j++){ if(foo[j]!=bar[j]) return false; }
00333
00334 return true;
00335 }
00336
00337 public static string getString(byte[] arr, int offset, int len)
00338 {
00339 return System.Text.Encoding.Default.GetString(arr, offset, len);
00340 }
00341 public static string getStringUTF8(byte[] arr, int offset, int len)
00342 {
00343 return System.Text.Encoding.UTF8.GetString(arr, offset, len);
00344 }
00345
00346 public static string getString(byte[] arr)
00347 {
00348 return getString(arr, 0, arr.Length);
00349 }
00350 public static string getStringUTF8(byte[] arr)
00351 {
00352 return getStringUTF8(arr, 0, arr.Length);
00353 }
00354
00355 public static byte[] getBytes(String str)
00356 {
00357 return System.Text.Encoding.Default.GetBytes( str );
00358 }
00359 public static byte[] getBytesUTF8(String str)
00360 {
00361 return System.Text.Encoding.UTF8.GetBytes( str );
00362 }
00363
00364 public static bool regionMatches(String orig, bool ignoreCase, int toffset,
00365 String other, int ooffset, int len)
00366 {
00367 char[] ta = new char[orig.Length];
00368 char[] pa = new char[other.Length];
00369 orig.CopyTo(0, ta, 0, orig.Length);
00370 int to = toffset;
00371 other.CopyTo(0, pa, 0, other.Length);
00372 int po = ooffset;
00373
00374 if ((ooffset < 0) || (toffset < 0) || (toffset > (long)orig.Length - len) ||
00375 (ooffset > (long)other.Length - len))
00376 {
00377 return false;
00378 }
00379 while (len-- > 0)
00380 {
00381 char c1 = ta[to++];
00382 char c2 = pa[po++];
00383 if (c1 == c2)
00384 {
00385 continue;
00386 }
00387 if (ignoreCase)
00388 {
00389
00390
00391
00392
00393 char u1 = char.ToUpper(c1);
00394 char u2 = char.ToUpper(c2);
00395 if (u1 == u2)
00396 {
00397 continue;
00398 }
00399
00400
00401
00402
00403 if (char.ToLower(u1) == char.ToLower(u2))
00404 {
00405 continue;
00406 }
00407 }
00408 return false;
00409 }
00410 return true;
00411 }
00412
00413 public static uint ToUInt32( byte [] ptr ,int Index)
00414 {
00415 uint ui = 0;
00416
00417 ui = ( (uint) ptr[ Index++ ] ) << 24;
00418 ui += ( (uint) ptr[ Index++ ] ) << 16;
00419 ui += ( (uint) ptr[ Index++ ] ) << 8;
00420 ui += (uint) ptr[ Index++ ];
00421
00422 return ui;
00423 }
00424
00425 public static int ToInt32( byte [] ptr ,int Index)
00426 {
00427 return (int)ToUInt32( ptr, Index );
00428 }
00429
00430 public static ushort ToUInt16( byte [] ptr , int Index)
00431 {
00432 ushort u = 0;
00433
00434 u = ( ushort ) ptr[ Index++ ];
00435 u *= 256;
00436 u += ( ushort ) ptr[ Index++ ];
00437
00438 return u;
00439 }
00440 public static bool ArrayContains( Object[] arr, Object o)
00441 {
00442 for(int i=0; i<arr.Length; i++)
00443 {
00444 if (arr[i].Equals(o))
00445 return true;
00446 }
00447 return false;
00448 }
00449 public static bool ArrayContains( char[] arr, char c)
00450 {
00451 for(int i=0; i<arr.Length; i++)
00452 {
00453 if (arr[i] == c)
00454 return true;
00455 }
00456 return false;
00457 }
00458 public static bool ArrayContains( char[] arr, char c, int count)
00459 {
00460 for(int i=0; i<count; i++)
00461 {
00462 if (arr[i] == c)
00463 return true;
00464 }
00465 return false;
00466 }
00467
00473 public static byte[] stripLeadingZeros(byte[] a)
00474 {
00475 int lastZero = -1;
00476 for (int i = 0; i < a.Length; i++)
00477 {
00478 if (a[i] == 0)
00479 {
00480 lastZero = i;
00481 }
00482 else
00483 {
00484 break;
00485 }
00486 }
00487 lastZero++;
00488 byte[] result = new byte[a.Length - lastZero];
00489 Array.Copy(a, lastZero, result, 0, result.Length);
00490 return result;
00491 }
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510 public static byte[] FixDsaSig( byte[] sig )
00511 {
00512 byte[] newSig = new byte[40];
00513 Array.Copy(sig, 4, newSig, 0, 20);
00514 int index;
00515 if(sig.Length==46)
00516 index = 26;
00517 else if(sig.Length==47)
00518 index = 27;
00519 else
00520 throw new Exception("Can't fix DSA Signature.");
00521 Array.Copy(sig, index, newSig, 20, 20);
00522 return newSig;
00523 }
00524
00525 public static void print(string name, byte[] data)
00526 {
00527 Console.WriteLine();
00528 Console.Write(name+": ");
00529 Console.WriteLine( hex(data) );
00530 Console.WriteLine();
00531 }
00532
00533 public static string hex(byte[] arr)
00534 {
00535 string hex = "0x";
00536 for(int i=0;i<arr.Length; i++)
00537 {
00538 string mbyte = arr[i].ToString("X");
00539 if (mbyte.Length == 1)
00540 mbyte = "0"+mbyte;
00541 hex += mbyte;
00542 }
00543 return hex;
00544 }
00545
00546 public static void Dump(string fileName, byte[] bytes)
00547 {
00548 System.IO.FileStream s = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate);
00549 s.Write(bytes, 0, bytes.Length);
00550 s.Flush();
00551 s.Close();
00552 }
00553
00554 internal static java.String unquote(java.String _path)
00555 {
00556 byte[] path=_path.getBytes();
00557 int pathlen=path.Length;
00558 int i=0;
00559 while(i<pathlen)
00560 {
00561 if(path[i]=='\\')
00562 {
00563 if(i+1==pathlen)
00564 break;
00565 java.System.arraycopy(path, i+1, path, i, path.Length-(i+1));
00566 pathlen--;
00567 continue;
00568 }
00569 i++;
00570 }
00571 if(pathlen==path.Length)return _path;
00572 byte[] foo=new byte[pathlen];
00573 java.System.arraycopy(path, 0, foo, 0, pathlen);
00574 return new java.String(foo);
00575 }
00576 }
00577 }
00578
00579
00580