using System; using System.Threading; namespace Tamir.SharpSsh.jsch { /* -*-mode:java; c-basic-offset:2; -*- */ /* Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ public class Util { /// /// Converts a time_t to DateTime /// public static DateTime Time_T2DateTime(uint time_t) { long win32FileTime = 10000000*(long)time_t + 116444736000000000; return DateTime.FromFileTimeUtc(win32FileTime).ToLocalTime(); } private static byte[] b64 = System.Text.Encoding.Default.GetBytes( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="); private static byte val(byte foo) { if(foo == '=') return 0; for(int j=0; j>4)); if(buf[i+2]==(byte)'='){ j++; break;} foo[j+1]=(byte)(((val(buf[i+1])&0x0f)<<4)|((val(buf[i+2])&0x3c)>>2)); if(buf[i+3]==(byte)'='){ j+=2; break;} foo[j+2]=(byte)(((val(buf[i+2])&0x03)<<6)|(val(buf[i+3])&0x3f)); j+=3; } byte[] bar=new byte[j]; Array.Copy(foo, 0, bar, 0, j); return bar; } internal static byte[] toBase64(byte[] buf, int start, int length) { byte[] tmp=new byte[length*2]; int i,j,k; int foo=(length/3)*3+start; i=0; for(j=start; j>2)&0x3f; tmp[i++]=b64[k]; k=(buf[j]&0x03)<<4|(buf[j+1]>>4)&0x0f; tmp[i++]=b64[k]; k=(buf[j+1]&0x0f)<<2|(buf[j+2]>>6)&0x03; tmp[i++]=b64[k]; k=buf[j+2]&0x3f; tmp[i++]=b64[k]; } foo=(start+length)-foo; if(foo==1) { k=(buf[j]>>2)&0x3f; tmp[i++]=b64[k]; k=((buf[j]&0x03)<<4)&0x3f; tmp[i++]=b64[k]; tmp[i++]=(byte)'='; tmp[i++]=(byte)'='; } else if(foo==2) { k=(buf[j]>>2)&0x3f; tmp[i++]=b64[k]; k=(buf[j]&0x03)<<4|(buf[j+1]>>4)&0x0f; tmp[i++]=b64[k]; k=((buf[j+1]&0x0f)<<2)&0x3f; tmp[i++]=b64[k]; tmp[i++]=(byte)'='; } byte[] bar=new byte[i]; Array.Copy(tmp, 0, bar, 0, i); return bar; // return sun.misc.BASE64Encoder().encode(buf); } internal static String[] split(String foo, String split) { byte[] buf=Util.getBytes(foo); System.Collections.ArrayList bar=new System.Collections.ArrayList(); int start=0; int index; while(true) { index=foo.IndexOf(split, start); if(index>=0) { bar.Add(Util.getString(buf, start, index-start)); start=index+1; continue; } bar.Add(Util.getString(buf, start, buf.Length-start)); break; } String[] result=new String[bar.Count]; for(int i=0; i>4)&0xf]); sb.Append(chars[(bar)&0xf]); if(i+1>>1. if ((ooffset < 0) || (toffset < 0) || (toffset > (long)orig.Length - len) || (ooffset > (long)other.Length - len)) { return false; } while (len-- > 0) { char c1 = ta[to++]; char c2 = pa[po++]; if (c1 == c2) { continue; } if (ignoreCase) { // If characters don't match but case may be ignored, // try converting both characters to uppercase. // If the results match, then the comparison scan should // continue. char u1 = char.ToUpper(c1); char u2 = char.ToUpper(c2); if (u1 == u2) { continue; } // Unfortunately, conversion to uppercase does not work properly // for the Georgian alphabet, which has strange rules about case // conversion. So we need to make one last check before // exiting. if (char.ToLower(u1) == char.ToLower(u2)) { continue; } } return false; } return true; } public static uint ToUInt32( byte [] ptr ,int Index) { uint ui = 0; ui = ( (uint) ptr[ Index++ ] ) << 24; ui += ( (uint) ptr[ Index++ ] ) << 16; ui += ( (uint) ptr[ Index++ ] ) << 8; ui += (uint) ptr[ Index++ ]; return ui; } public static int ToInt32( byte [] ptr ,int Index) { return (int)ToUInt32( ptr, Index ); } public static ushort ToUInt16( byte [] ptr , int Index) { ushort u = 0; u = ( ushort ) ptr[ Index++ ]; u *= 256; u += ( ushort ) ptr[ Index++ ]; return u; } public static bool ArrayContains( Object[] arr, Object o) { for(int i=0; i