aboutsummaryrefslogtreecommitdiffstats
path: root/sshlib/src/main/java/com/trilead/ssh2/packets/TypesWriter.java
blob: 9f7336b0741fa9f6a7b8ebe3f3043bdbd1d79277 (plain)
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
163
164
165
166
167
168
169
package com.trilead.ssh2.packets;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

/**
 * TypesWriter.
 * 
 * @author Christian Plattner, plattner@trilead.com
 * @version $Id: TypesWriter.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
 */
public class TypesWriter
{
	byte arr[];
	int pos;

	public TypesWriter()
	{
		arr = new byte[256];
		pos = 0;
	}

	private void resize(int len)
	{
		byte new_arr[] = new byte[len];
		System.arraycopy(arr, 0, new_arr, 0, arr.length);
		arr = new_arr;
	}

	public int length()
	{
		return pos;
	}

	public byte[] getBytes()
	{
		byte[] dst = new byte[pos];
		System.arraycopy(arr, 0, dst, 0, pos);
		return dst;
	}

	public void getBytes(byte dst[])
	{
		System.arraycopy(arr, 0, dst, 0, pos);
	}

	public void writeUINT32(int val, int off)
	{
		if ((off + 4) > arr.length)
			resize(off + 32);

		arr[off++] = (byte) (val >> 24);
		arr[off++] = (byte) (val >> 16);
		arr[off++] = (byte) (val >> 8);
		arr[off++] = (byte) val;
	}

	public void writeUINT32(int val)
	{
		writeUINT32(val, pos);
		pos += 4;
	}

	public void writeUINT64(long val)
	{
		if ((pos + 8) > arr.length)
			resize(arr.length + 32);

		arr[pos++] = (byte) (val >> 56);
		arr[pos++] = (byte) (val >> 48);
		arr[pos++] = (byte) (val >> 40);
		arr[pos++] = (byte) (val >> 32);
		arr[pos++] = (byte) (val >> 24);
		arr[pos++] = (byte) (val >> 16);
		arr[pos++] = (byte) (val >> 8);
		arr[pos++] = (byte) val;
	}

	public void writeBoolean(boolean v)
	{
		if ((pos + 1) > arr.length)
			resize(arr.length + 32);

		arr[pos++] = v ? (byte) 1 : (byte) 0;
	}

	public void writeByte(int v, int off)
	{
		if ((off + 1) > arr.length)
			resize(off + 32);

		arr[off] = (byte) v;
	}

	public void writeByte(int v)
	{
		writeByte(v, pos);
		pos++;
	}

	public void writeMPInt(BigInteger b)
	{
		byte raw[] = b.toByteArray();

		if ((raw.length == 1) && (raw[0] == 0))
			writeUINT32(0); /* String with zero bytes of data */
		else
			writeString(raw, 0, raw.length);
	}

	public void writeBytes(byte[] buff)
	{
		writeBytes(buff, 0, buff.length);
	}

	public void writeBytes(byte[] buff, int off, int len)
	{
		if ((pos + len) > arr.length)
			resize(arr.length + len + 32);

		System.arraycopy(buff, off, arr, pos, len);
		pos += len;
	}

	public void writeString(byte[] buff, int off, int len)
	{
		writeUINT32(len);
		writeBytes(buff, off, len);
	}

	public void writeString(String v)
	{
		byte[] b;

		try
		{
			/* All Java JVMs must support ISO-8859-1 */
			b = v.getBytes("ISO-8859-1");
		}
		catch (UnsupportedEncodingException ignore)
		{
			b = v.getBytes();
		}

		writeUINT32(b.length);
		writeBytes(b, 0, b.length);
	}

	public void writeString(String v, String charsetName) throws UnsupportedEncodingException
	{
		byte[] b = (charsetName == null) ? v.getBytes() : v.getBytes(charsetName);

		writeUINT32(b.length);
		writeBytes(b, 0, b.length);
	}

	public void writeNameList(String v[])
	{
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < v.length; i++)
		{
			if (i > 0)
				sb.append(',');
			sb.append(v[i]);
		}
		writeString(sb.toString());
	}
}