Mercurial > 510Connectbot
comparison app/src/main/java/ch/ethz/ssh2/packets/TypesReader.java @ 438:d29cce60f393
migrate from Eclipse to Android Studio
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 03 Dec 2015 11:23:55 -0800 |
parents | src/ch/ethz/ssh2/packets/TypesReader.java@071eccdff8ea |
children |
comparison
equal
deleted
inserted
replaced
437:208b31032318 | 438:d29cce60f393 |
---|---|
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 System.arraycopy(arr, pos, res, 0, len); | |
67 pos += len; | |
68 return res; | |
69 } | |
70 | |
71 public void readBytes(byte[] dst, int off, int len) throws IOException { | |
72 if ((pos + len) > max) { | |
73 throw new PacketFormatException("Packet too short."); | |
74 } | |
75 | |
76 System.arraycopy(arr, pos, dst, off, len); | |
77 pos += len; | |
78 } | |
79 | |
80 public boolean readBoolean() throws IOException { | |
81 if (pos >= max) { | |
82 throw new PacketFormatException("Packet too short."); | |
83 } | |
84 | |
85 return (arr[pos++] != 0); | |
86 } | |
87 | |
88 public int readUINT32() throws IOException { | |
89 if ((pos + 4) > max) { | |
90 throw new PacketFormatException("Packet too short."); | |
91 } | |
92 | |
93 return ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) | |
94 | (arr[pos++] & 0xff); | |
95 } | |
96 | |
97 public long readUINT64() throws IOException { | |
98 if ((pos + 8) > max) { | |
99 throw new PacketFormatException("Packet too short."); | |
100 } | |
101 | |
102 long high = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) | |
103 | (arr[pos++] & 0xff); /* sign extension may take place - will be shifted away =) */ | |
104 long low = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) | |
105 | (arr[pos++] & 0xff); /* sign extension may take place - handle below */ | |
106 return (high << 32) | (low & 0xffffffffl); /* see Java language spec (15.22.1, 5.6.2) */ | |
107 } | |
108 | |
109 public BigInteger readMPINT() throws IOException { | |
110 BigInteger b; | |
111 byte raw[] = readByteString(); | |
112 | |
113 if (raw.length == 0) { | |
114 b = BigInteger.ZERO; | |
115 } | |
116 else { | |
117 b = new BigInteger(raw); | |
118 } | |
119 | |
120 return b; | |
121 } | |
122 | |
123 public byte[] readByteString() throws IOException { | |
124 int len = readUINT32(); | |
125 | |
126 if ((len + pos) > max) { | |
127 throw new PacketFormatException("Malformed SSH byte string."); | |
128 } | |
129 | |
130 byte[] res = new byte[len]; | |
131 System.arraycopy(arr, pos, res, 0, len); | |
132 pos += len; | |
133 return res; | |
134 } | |
135 | |
136 public String readString(String charsetName) throws IOException { | |
137 int len = readUINT32(); | |
138 | |
139 if ((len + pos) > max) { | |
140 throw new PacketFormatException("Malformed SSH string."); | |
141 } | |
142 | |
143 String res = (charsetName == null) ? new String(arr, pos, len) : new String(arr, pos, len, charsetName); | |
144 pos += len; | |
145 return res; | |
146 } | |
147 | |
148 public String readString() throws IOException { | |
149 int len = readUINT32(); | |
150 | |
151 if ((len + pos) > max) { | |
152 throw new PacketFormatException("Malformed SSH string."); | |
153 } | |
154 | |
155 String res = StringEncoder.GetString(arr, pos, len); | |
156 pos += len; | |
157 return res; | |
158 } | |
159 | |
160 public String[] readNameList() throws IOException { | |
161 return readString().split(","); | |
162 } | |
163 | |
164 public int remain() { | |
165 return max - pos; | |
166 } | |
167 | |
168 } |