Mercurial > 510Connectbot
comparison src/com/trilead/ssh2/packets/TypesReader.java @ 0:0ce5cc452d02
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 22 May 2014 10:41:19 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0ce5cc452d02 |
---|---|
1 | |
2 package com.trilead.ssh2.packets; | |
3 | |
4 import java.io.IOException; | |
5 import java.math.BigInteger; | |
6 | |
7 import com.trilead.ssh2.util.Tokenizer; | |
8 | |
9 | |
10 /** | |
11 * TypesReader. | |
12 * | |
13 * @author Christian Plattner, plattner@trilead.com | |
14 * @version $Id: TypesReader.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $ | |
15 */ | |
16 public class TypesReader { | |
17 byte[] arr; | |
18 int pos = 0; | |
19 int max = 0; | |
20 | |
21 public TypesReader(byte[] arr) { | |
22 this.arr = arr; | |
23 pos = 0; | |
24 max = arr.length; | |
25 } | |
26 | |
27 public TypesReader(byte[] arr, int off) { | |
28 this.arr = arr; | |
29 this.pos = off; | |
30 this.max = arr.length; | |
31 | |
32 if ((pos < 0) || (pos > arr.length)) | |
33 throw new IllegalArgumentException("Illegal offset."); | |
34 } | |
35 | |
36 public TypesReader(byte[] arr, int off, int len) { | |
37 this.arr = arr; | |
38 this.pos = off; | |
39 this.max = off + len; | |
40 | |
41 if ((pos < 0) || (pos > arr.length)) | |
42 throw new IllegalArgumentException("Illegal offset."); | |
43 | |
44 if ((max < 0) || (max > arr.length)) | |
45 throw new IllegalArgumentException("Illegal length."); | |
46 } | |
47 | |
48 public int readByte() throws IOException { | |
49 if (pos >= max) | |
50 throw new IOException("Packet too short."); | |
51 | |
52 return (arr[pos++] & 0xff); | |
53 } | |
54 | |
55 public byte[] readBytes(int len) throws IOException { | |
56 if ((pos + len) > max) | |
57 throw new IOException("Packet too short."); | |
58 | |
59 byte[] res = new byte[len]; | |
60 System.arraycopy(arr, pos, res, 0, len); | |
61 pos += len; | |
62 return res; | |
63 } | |
64 | |
65 public void readBytes(byte[] dst, int off, int len) throws IOException { | |
66 if ((pos + len) > max) | |
67 throw new IOException("Packet too short."); | |
68 | |
69 System.arraycopy(arr, pos, dst, off, len); | |
70 pos += len; | |
71 } | |
72 | |
73 public boolean readBoolean() throws IOException { | |
74 if (pos >= max) | |
75 throw new IOException("Packet too short."); | |
76 | |
77 return (arr[pos++] != 0); | |
78 } | |
79 | |
80 public int readUINT32() throws IOException { | |
81 if ((pos + 4) > max) | |
82 throw new IOException("Packet too short."); | |
83 | |
84 return ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) | |
85 | (arr[pos++] & 0xff); | |
86 } | |
87 | |
88 public long readUINT64() throws IOException { | |
89 if ((pos + 8) > max) | |
90 throw new IOException("Packet too short."); | |
91 | |
92 long high = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) | |
93 | (arr[pos++] & 0xff); /* sign extension may take place - will be shifted away =) */ | |
94 long low = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) | |
95 | (arr[pos++] & 0xff); /* sign extension may take place - handle below */ | |
96 return (high << 32) | (low & 0xffffffffl); /* see Java language spec (15.22.1, 5.6.2) */ | |
97 } | |
98 | |
99 public BigInteger readMPINT() throws IOException { | |
100 BigInteger b; | |
101 byte raw[] = readByteString(); | |
102 | |
103 if (raw.length == 0) | |
104 b = BigInteger.ZERO; | |
105 else | |
106 b = new BigInteger(raw); | |
107 | |
108 return b; | |
109 } | |
110 | |
111 public byte[] readByteString() throws IOException { | |
112 int len = readUINT32(); | |
113 | |
114 if ((len + pos) > max) | |
115 throw new IOException("Malformed SSH byte string."); | |
116 | |
117 byte[] res = new byte[len]; | |
118 System.arraycopy(arr, pos, res, 0, len); | |
119 pos += len; | |
120 return res; | |
121 } | |
122 | |
123 public String readString(String charsetName) throws IOException { | |
124 int len = readUINT32(); | |
125 | |
126 if ((len + pos) > max) | |
127 throw new IOException("Malformed SSH string."); | |
128 | |
129 String res = (charsetName == null) ? new String(arr, pos, len) : new String(arr, pos, len, charsetName); | |
130 pos += len; | |
131 return res; | |
132 } | |
133 | |
134 public String readString() throws IOException { | |
135 int len = readUINT32(); | |
136 | |
137 if ((len + pos) > max) | |
138 throw new IOException("Malformed SSH string."); | |
139 | |
140 String res = new String(arr, pos, len, "ISO-8859-1"); | |
141 pos += len; | |
142 return res; | |
143 } | |
144 | |
145 public String[] readNameList() throws IOException { | |
146 return Tokenizer.parseTokens(readString(), ','); | |
147 } | |
148 | |
149 public int remain() { | |
150 return max - pos; | |
151 } | |
152 | |
153 } |