00001 using System; 00002 00003 namespace Tamir.SharpSsh.jsch 00004 { 00005 /* -*-mode:java; c-basic-offset:2; -*- */ 00006 /* 00007 Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved. 00008 00009 Redistribution and use in source and binary forms, with or without 00010 modification, are permitted provided that the following conditions are met: 00011 00012 1. Redistributions of source code must retain the above copyright notice, 00013 this list of conditions and the following disclaimer. 00014 00015 2. Redistributions in binary form must reproduce the above copyright 00016 notice, this list of conditions and the following disclaimer in 00017 the documentation and/or other materials provided with the distribution. 00018 00019 3. The names of the authors may not be used to endorse or promote products 00020 derived from this software without specific prior written permission. 00021 00022 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 00023 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00024 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, 00025 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00027 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00028 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00031 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 */ 00033 00034 public class Packet 00035 { 00036 00037 private static Random random=null; 00038 internal static void setRandom(Random foo){ random=foo;} 00039 00040 internal Buffer buffer; 00041 internal byte[] tmp=new byte[4]; 00042 public Packet(Buffer buffer) 00043 { 00044 this.buffer=buffer; 00045 } 00046 public void reset() 00047 { 00048 buffer.index=5; 00049 } 00050 // internal void padding() 00051 // { 00052 // uint len=(uint)buffer.index; 00053 // int pad=(int) ((-len)&7); 00054 // if(pad<8) 00055 // { 00056 // pad+=8; 00057 // } 00058 // len=(uint)(len+pad-4); 00059 // tmp[0]=(byte)(len>>24); 00060 // tmp[1]=(byte)(len>>16); 00061 // tmp[2]=(byte)(len>>8); 00062 // tmp[3]=(byte)(len); 00063 // Array.Copy(tmp, 0, buffer.buffer, 0, 4); 00064 // buffer.buffer[4]=(byte)pad; 00065 // lock(random) 00066 // { 00067 // random.fill(buffer.buffer, buffer.index, pad); 00068 // } 00069 // buffer.skip(pad); 00070 // //buffer.putPad(pad); 00071 // /* 00072 // for(int i=0; i<buffer.index; i++){ 00073 // System.out.print(Integer.toHexString(buffer.buffer[i]&0xff)+":"); 00074 // } 00075 // System.out.println(""); 00076 // */ 00077 // } 00078 00079 internal void padding(int bsize) 00080 { 00081 uint len=(uint)buffer.index; 00082 int pad=(int)( (-len)&(bsize-1) ); 00083 if(pad<bsize) 00084 { 00085 pad+=bsize; 00086 } 00087 len=(uint)(len+pad-4); 00088 tmp[0]=(byte)(len>>24); 00089 tmp[1]=(byte)(len>>16); 00090 tmp[2]=(byte)(len>>8); 00091 tmp[3]=(byte)(len); 00092 Array.Copy(tmp, 0, buffer.buffer, 0, 4); 00093 buffer.buffer[4]=(byte)pad; 00094 lock(random) 00095 { 00096 random.fill(buffer.buffer, buffer.index, pad); 00097 } 00098 buffer.skip(pad); 00099 //buffer.putPad(pad); 00100 /* 00101 for(int i=0; i<buffer.index; i++){ 00102 System.out.print(Integer.toHexString(buffer.buffer[i]&0xff)+":"); 00103 } 00104 System.out.println(""); 00105 */ 00106 } 00107 00108 internal int shift(int len, int mac) 00109 { 00110 int s=len+5+9; 00111 int pad=(-s)&7; 00112 if(pad<8)pad+=8; 00113 s+=pad; 00114 s+=mac; 00115 00116 Array.Copy(buffer.buffer, 00117 len+5+9, 00118 buffer.buffer, s, buffer.index-5-9-len); 00119 buffer.index=10; 00120 buffer.putInt(len); 00121 buffer.index=len+5+9; 00122 return s; 00123 } 00124 internal void unshift(byte command, int recipient, int s, int len) 00125 { 00126 Array.Copy(buffer.buffer, 00127 s, 00128 buffer.buffer, 5+9, len); 00129 buffer.buffer[5]=command; 00130 buffer.index=6; 00131 buffer.putInt(recipient); 00132 buffer.putInt(len); 00133 buffer.index=len+5+9; 00134 } 00135 00136 } 00137 00138 }
1.5.9