Packet.java

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

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1