Mercurial > 510Connectbot
comparison src/ch/ethz/ssh2/packets/TypesReader.java @ 273:91a31873c42a ganymed
start conversion from trilead to ganymed
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 18 Jul 2014 11:21:46 -0700 |
parents | |
children | 071eccdff8ea |
comparison
equal
deleted
inserted
replaced
272:ce2f4e397703 | 273:91a31873c42a |
---|---|
1 /* | |
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved. | |
3 * Please refer to the LICENSE.txt for licensing details. | |
4 */ | |
5 package ch.ethz.ssh2.packets; | |
6 | |
7 import java.io.IOException; | |
8 import java.math.BigInteger; | |
9 | |
10 import ch.ethz.ssh2.PacketFormatException; | |
11 import ch.ethz.ssh2.util.StringEncoder; | |
12 | |
13 /** | |
14 * @author Christian Plattner | |
15 * @version $Id: TypesReader.java 160 2014-05-01 14:30:26Z dkocher@sudo.ch $ | |
16 */ | |
17 public final class TypesReader { | |
18 byte[] arr; | |
19 int pos = 0; | |
20 int max = 0; | |
21 | |
22 public TypesReader(byte[] arr) { | |
23 this.arr = arr; | |
24 pos = 0; | |
25 max = arr.length; | |
26 } | |
27 | |
28 public TypesReader(byte[] arr, int off) { | |
29 this.arr = arr; | |
30 this.pos = off; | |
31 this.max = arr.length; | |
32 | |
33 if((pos < 0) || (pos > arr.length)) { | |
34 throw new IllegalArgumentException("Illegal offset."); | |
35 } | |
36 } | |
37 | |
38 public TypesReader(byte[] arr, int off, int len) { | |
39 this.arr = arr; | |
40 this.pos = off; | |
41 this.max = off + len; | |
42 | |
43 if((pos < 0) || (pos > arr.length)) { | |
44 throw new IllegalArgumentException("Illegal offset."); | |
45 } | |
46 | |
47 if((max < 0) || (max > arr.length)) { | |
48 throw new IllegalArgumentException("Illegal length."); | |
49 } | |
50 } | |
51 | |
52 public int readByte() throws IOException { | |
53 if(pos >= max) { | |
54 throw new PacketFormatException("Packet too short."); | |
55 } | |
56 | |
57 return (arr[pos++] & 0xff); | |
58 } | |
59 | |
60 public byte[] readBytes(int len) throws IOException { | |
61 if((pos + len) > max) { | |
62 throw new PacketFormatException("Packet too short."); | |
63 } | |
64 | |
65 byte[] res = new byte[len]; | |
66 | |
67 System.arraycopy(arr, pos, res, 0, len); | |
68 pos += len; | |
69 | |
70 return res; | |
71 } | |
72 | |
73 public void readBytes(byte[] dst, int off, int len) throws IOException { | |
74 if((pos + len) > max) { | |
75 throw new PacketFormatException("Packet too short."); | |
76 } | |
77 | |
78 System.arraycopy(arr, pos, dst, off, len); | |
79 pos += len; | |
80 } | |
81 | |
82 public boolean readBoolean() throws IOException { | |
83 if(pos >= max) { | |
84 throw new PacketFormatException("Packet too short."); | |
85 } | |
86 | |
87 return (arr[pos++] != 0); | |
88 } | |
89 | |
90 public int readUINT32() throws IOException { | |
91 if((pos + 4) > max) { | |
92 throw new PacketFormatException("Packet too short."); | |
93 } | |
94 | |
95 return ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) | |
96 | (arr[pos++] & 0xff); | |
97 } | |
98 | |
99 public long readUINT64() throws IOException { | |
100 if((pos + 8) > max) { | |
101 throw new PacketFormatException("Packet too short."); | |
102 } | |
103 | |
104 long high = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) | |
105 | (arr[pos++] & 0xff); /* sign extension may take place - will be shifted away =) */ | |
106 | |
107 long low = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) | |
108 | (arr[pos++] & 0xff); /* sign extension may take place - handle below */ | |
109 | |
110 return (high << 32) | (low & 0xffffffffl); /* see Java language spec (15.22.1, 5.6.2) */ | |
111 } | |
112 | |
113 public BigInteger readMPINT() throws IOException { | |
114 BigInteger b; | |
115 | |
116 byte raw[] = readByteString(); | |
117 | |
118 if(raw.length == 0) { | |
119 b = BigInteger.ZERO; | |
120 } | |
121 else { | |
122 b = new BigInteger(raw); | |
123 } | |
124 | |
125 return b; | |
126 } | |
127 | |
128 public byte[] readByteString() throws IOException { | |
129 int len = readUINT32(); | |
130 | |
131 if((len + pos) > max) { | |
132 throw new PacketFormatException("Malformed SSH byte string."); | |
133 } | |
134 | |
135 byte[] res = new byte[len]; | |
136 System.arraycopy(arr, pos, res, 0, len); | |
137 pos += len; | |
138 return res; | |
139 } | |
140 | |
141 public String readString(String charsetName) throws IOException { | |
142 int len = readUINT32(); | |
143 | |
144 if((len + pos) > max) { | |
145 throw new PacketFormatException("Malformed SSH string."); | |
146 } | |
147 | |
148 String res = (charsetName == null) ? new String(arr, pos, len) : new String(arr, pos, len, charsetName); | |
149 pos += len; | |
150 | |
151 return res; | |
152 } | |
153 | |
154 public String readString() throws IOException { | |
155 int len = readUINT32(); | |
156 | |
157 if((len + pos) > max) { | |
158 throw new PacketFormatException("Malformed SSH string."); | |
159 } | |
160 | |
161 String res = StringEncoder.GetString(arr, pos, len); | |
162 pos += len; | |
163 | |
164 return res; | |
165 } | |
166 | |
167 public String[] readNameList() throws IOException { | |
168 return readString().split(","); | |
169 } | |
170 | |
171 public int remain() { | |
172 return max - pos; | |
173 } | |
174 | |
175 } |