5776784 [rkeene@sledge /home/rkeene/archive/floydssh/ssh]$ cat -n SshPacket.java
  1 /*
  2  * This file is part of "The Java Telnet Application".
  3  *
  4  * (c) Matthias L. Jugel, Marcus Meißner 1996-2002. All Rights Reserved.
  5  * The file was changed by Radek Polak to work as midlet in MIDP 1.0
  6  *
  7  * Please visit http://javatelnet.org/ for updates and contact.
  8  *
  9  * --LICENSE NOTICE--
 10  * This program is free software; you can redistribute it and/or
 11  * modify it under the terms of the GNU General Public License
 12  * as published by the Free Software Foundation; either version 2
 13  * of the License, or (at your option) any later version.
 14  *
 15  * This program is distributed in the hope that it will be useful,
 16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18  * GNU General Public License for more details.
 19  *
 20  * You should have received a copy of the GNU General Public License
 21  * along with this program; if not, write to the Free Software
 22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 23  * --LICENSE NOTICE--
 24  */
 25 
 26 package ssh;
 27 
 28 abstract class SshPacket {
 29   public SshPacket() { /* nothing */
 30   }
 31 
 32   // Data management
 33   protected byte[] byteArray = new byte[0];
 34   protected int offset;
 35   private boolean finished = false;
 36 
 37   public byte[] getData() {
 38     return byteArray;
 39   }
 40 
 41   public void putData(byte[] data) {
 42     byteArray = data;
 43     offset = 0;
 44     finished = true;
 45   }
 46 
 47   public boolean isFinished() {
 48     return finished;
 49   }
 50 
 51   abstract public byte[] addPayload(byte[] buff);
 52 
 53 
 54   // Type
 55   private byte packet_type;
 56 
 57   public byte getType() {
 58     return packet_type;
 59   }
 60 
 61   public void setType(byte ntype) {
 62     packet_type = ntype;
 63   }
 64 
 65   public abstract void putMpInt(BigInteger bi);
 66 
 67   public int getInt32() {
 68     short d0 = byteArray[offset++];
 69     short d1 = byteArray[offset++];
 70     short d2 = byteArray[offset++];
 71     short d3 = byteArray[offset++];
 72 
 73     if (d0 < 0) d0 = (short) (256 + d0);
 74     if (d1 < 0) d1 = (short) (256 + d1);
 75     if (d2 < 0) d2 = (short) (256 + d2);
 76     if (d3 < 0) d3 = (short) (256 + d3);
 77 
 78     return (d0 << 24) + (d1 << 16) + (d2 << 8) + d3;
 79   }
 80 
 81   public int getInt16() {
 82     short d0 = byteArray[offset++];
 83     short d1 = byteArray[offset++];
 84 
 85     if (d0 < 0) d0 = (short) (256 + d0);
 86     if (d1 < 0) d1 = (short) (256 + d1);
 87 
 88     return (d0 << 8) + d1;
 89   }
 90 
 91   public String getString() {
 92     int length = getInt32();
 93 
 94     String str = "";
 95     for (int i = 0; i < length; i++) {
 96       if (byteArray[offset] >= 0)
 97         str += (char) (byteArray[offset++]);
 98       else
 99         str += (char) (256 + byteArray[offset++]);
100     }
101     return str;
102   }
103 
104   public byte getByte() {
105     return byteArray[offset++];
106   }
107 
108   public byte[] getBytes(int cnt) {
109     byte[] bytes = new byte[cnt];
110 
111     System.arraycopy(byteArray, offset, bytes, 0, cnt);
112     offset += cnt;
113     return bytes;
114   }
115 
116   private void grow(int howmuch) {
117     byte[] value = new byte[byteArray.length + howmuch];
118     System.arraycopy(byteArray, 0, value, 0, byteArray.length);
119     byteArray = value;
120   }
121 
122   public void putInt16(int xint) {
123     int boffset = byteArray.length;
124     grow(2);
125     byteArray[boffset + 1] = (byte) ((xint) & 0xff);
126     byteArray[boffset] = (byte) ((xint >> 8) & 0xff);
127   }
128 
129   public void putInt32(int xint) {
130     int boffset = byteArray.length;
131     grow(4);
132     byteArray[boffset + 3] = (byte) ((xint) & 0xff);
133     byteArray[boffset + 2] = (byte) ((xint >> 8) & 0xff);
134     byteArray[boffset + 1] = (byte) ((xint >> 16) & 0xff);
135     byteArray[boffset + 0] = (byte) ((xint >> 24) & 0xff);
136   }
137 
138   public void putByte(byte xbyte) {
139     grow(1);
140     byteArray[byteArray.length - 1] = xbyte;
141   }
142 
143   public void putBytes(byte[] bytes) {
144     int oldlen = byteArray.length;
145     grow(bytes.length);
146     System.arraycopy(bytes, 0, byteArray, oldlen, bytes.length);
147   }
148 
149 
150   /**
151    * Add a SSH String to a packet. The incore representation is a
152    *    INT32       length
153    *    BYTE[length]    data
154    * @param str: The string to be added.
155    */
156   public void putString(String str) {
157     putInt32(str.length());
158     putBytes(str.getBytes());
159   }
160 }
5776785 [rkeene@sledge /home/rkeene/archive/floydssh/ssh]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2004-03-06 22:44:06