File: SshPacket.java

package info (click to toggle)
jta 2.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,384 kB
  • sloc: java: 12,691; ansic: 1,066; makefile: 243; xml: 72; sh: 7
file content (162 lines) | stat: -rw-r--r-- 4,049 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * This file is part of "JTA - Telnet/SSH for the JAVA(tm) platform".
 *
 * (c) Matthias L. Jugel, Marcus Meißner 1996-2005. All Rights Reserved.
 *
 * Please visit http://javatelnet.org/ for updates and contact.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * --LICENSE NOTICE--
 *
 */

package de.mud.ssh;

import java.math.BigInteger;

abstract class SshPacket {
  public SshPacket() { /* nothing */
  }

  // Data management
  protected byte[] byteArray = new byte[0];
  protected int offset;
  private boolean finished = false;

  public byte[] getData() {
    return byteArray;
  }

  public void putData(byte[] data) {
    byteArray = data;
    offset = 0;
    finished = true;
  }

  public boolean isFinished() {
    return finished;
  }

  abstract public byte[] addPayload(byte[] buff);


  // Type
  private byte packet_type;

  public byte getType() {
    return packet_type;
  }

  public void setType(byte ntype) {
    packet_type = ntype;
  }

  public abstract void putMpInt(BigInteger bi);

  public int getInt32() {
    short d0 = byteArray[offset++];
    short d1 = byteArray[offset++];
    short d2 = byteArray[offset++];
    short d3 = byteArray[offset++];

    if (d0 < 0) d0 = (short) (256 + d0);
    if (d1 < 0) d1 = (short) (256 + d1);
    if (d2 < 0) d2 = (short) (256 + d2);
    if (d3 < 0) d3 = (short) (256 + d3);

    return (d0 << 24) + (d1 << 16) + (d2 << 8) + d3;
  }

  public int getInt16() {
    short d0 = byteArray[offset++];
    short d1 = byteArray[offset++];

    if (d0 < 0) d0 = (short) (256 + d0);
    if (d1 < 0) d1 = (short) (256 + d1);

    return (d0 << 8) + d1;
  }

  public String getString() {
    int length = getInt32();

    String str = "";
    for (int i = 0; i < length; i++) {
      if (byteArray[offset] >= 0)
        str += (char) (byteArray[offset++]);
      else
        str += (char) (256 + byteArray[offset++]);
    }
    return str;
  }

  public byte getByte() {
    return byteArray[offset++];
  }

  public byte[] getBytes(int cnt) {
    byte[] bytes = new byte[cnt];

    System.arraycopy(byteArray, offset, bytes, 0, cnt);
    offset += cnt;
    return bytes;
  }

  private void grow(int howmuch) {
    byte[] value = new byte[byteArray.length + howmuch];
    System.arraycopy(byteArray, 0, value, 0, byteArray.length);
    byteArray = value;
  }

  public void putInt16(int xint) {
    int boffset = byteArray.length;
    grow(2);
    byteArray[boffset + 1] = (byte) ((xint) & 0xff);
    byteArray[boffset] = (byte) ((xint >> 8) & 0xff);
  }

  public void putInt32(int xint) {
    int boffset = byteArray.length;
    grow(4);
    byteArray[boffset + 3] = (byte) ((xint) & 0xff);
    byteArray[boffset + 2] = (byte) ((xint >> 8) & 0xff);
    byteArray[boffset + 1] = (byte) ((xint >> 16) & 0xff);
    byteArray[boffset + 0] = (byte) ((xint >> 24) & 0xff);
  }

  public void putByte(byte xbyte) {
    grow(1);
    byteArray[byteArray.length - 1] = xbyte;
  }

  public void putBytes(byte[] bytes) {
    int oldlen = byteArray.length;
    grow(bytes.length);
    System.arraycopy(bytes, 0, byteArray, oldlen, bytes.length);
  }


  /**
   * Add a SSH String to a packet. The incore representation is a
   * 	INT32		length
   * 	BYTE[length]	data
   * @param str: The string to be added.
   */
  public void putString(String str) {
    putInt32(str.length());
    putBytes(str.getBytes());
  }
}