001: package ch.ethz.ssh2.packets;
002:
003: import java.io.IOException;
004: import java.math.BigInteger;
005:
006: import ch.ethz.ssh2.util.Tokenizer;
007:
008: /**
009: * TypesReader.
010: *
011: * @author Christian Plattner, plattner@inf.ethz.ch
012: * @version $Id: TypesReader.java,v 1.6 2006/08/31 20:04:29 cplattne Exp $
013: */
014: public class TypesReader {
015: byte[] arr;
016: int pos = 0;
017: int max = 0;
018:
019: public TypesReader(byte[] arr) {
020: this .arr = arr;
021: pos = 0;
022: max = arr.length;
023: }
024:
025: public TypesReader(byte[] arr, int off) {
026: this .arr = arr;
027: this .pos = off;
028: this .max = arr.length;
029:
030: if ((pos < 0) || (pos > arr.length))
031: throw new IllegalArgumentException("Illegal offset.");
032: }
033:
034: public TypesReader(byte[] arr, int off, int len) {
035: this .arr = arr;
036: this .pos = off;
037: this .max = off + len;
038:
039: if ((pos < 0) || (pos > arr.length))
040: throw new IllegalArgumentException("Illegal offset.");
041:
042: if ((max < 0) || (max > arr.length))
043: throw new IllegalArgumentException("Illegal length.");
044: }
045:
046: public int readByte() throws IOException {
047: if (pos >= max)
048: throw new IOException("Packet too short.");
049:
050: return (arr[pos++] & 0xff);
051: }
052:
053: public byte[] readBytes(int len) throws IOException {
054: if ((pos + len) > max)
055: throw new IOException("Packet too short.");
056:
057: byte[] res = new byte[len];
058:
059: System.arraycopy(arr, pos, res, 0, len);
060: pos += len;
061:
062: return res;
063: }
064:
065: public void readBytes(byte[] dst, int off, int len)
066: throws IOException {
067: if ((pos + len) > max)
068: throw new IOException("Packet too short.");
069:
070: System.arraycopy(arr, pos, dst, off, len);
071: pos += len;
072: }
073:
074: public boolean readBoolean() throws IOException {
075: if (pos >= max)
076: throw new IOException("Packet too short.");
077:
078: return (arr[pos++] != 0);
079: }
080:
081: public int readUINT32() throws IOException {
082: if ((pos + 4) > max)
083: throw new IOException("Packet too short.");
084:
085: return ((arr[pos++] & 0xff) << 24)
086: | ((arr[pos++] & 0xff) << 16)
087: | ((arr[pos++] & 0xff) << 8) | (arr[pos++] & 0xff);
088: }
089:
090: public long readUINT64() throws IOException {
091: if ((pos + 8) > max)
092: throw new IOException("Packet too short.");
093:
094: long high = ((arr[pos++] & 0xff) << 24)
095: | ((arr[pos++] & 0xff) << 16)
096: | ((arr[pos++] & 0xff) << 8) | (arr[pos++] & 0xff); /* sign extension may take place - will be shifted away =) */
097:
098: long low = ((arr[pos++] & 0xff) << 24)
099: | ((arr[pos++] & 0xff) << 16)
100: | ((arr[pos++] & 0xff) << 8) | (arr[pos++] & 0xff); /* sign extension may take place - handle below */
101:
102: return (high << 32) | (low & 0xffffffffl); /* see Java language spec (15.22.1, 5.6.2) */
103: }
104:
105: public BigInteger readMPINT() throws IOException {
106: BigInteger b;
107:
108: byte raw[] = readByteString();
109:
110: if (raw.length == 0)
111: b = BigInteger.ZERO;
112: else
113: b = new BigInteger(raw);
114:
115: return b;
116: }
117:
118: public byte[] readByteString() throws IOException {
119: int len = readUINT32();
120:
121: if ((len + pos) > max)
122: throw new IOException("Malformed SSH byte string.");
123:
124: byte[] res = new byte[len];
125: System.arraycopy(arr, pos, res, 0, len);
126: pos += len;
127: return res;
128: }
129:
130: public String readString(String charsetName) throws IOException {
131: int len = readUINT32();
132:
133: if ((len + pos) > max)
134: throw new IOException("Malformed SSH string.");
135:
136: String res = (charsetName == null) ? new String(arr, pos, len)
137: : new String(arr, pos, len, charsetName);
138: pos += len;
139:
140: return res;
141: }
142:
143: public String readString() throws IOException {
144: int len = readUINT32();
145:
146: if ((len + pos) > max)
147: throw new IOException("Malformed SSH string.");
148:
149: String res = new String(arr, pos, len);
150: pos += len;
151:
152: return res;
153: }
154:
155: public String[] readNameList() throws IOException {
156: return Tokenizer.parseTokens(readString(), ',');
157: }
158:
159: public int remain() {
160: return max - pos;
161: }
162:
163: }
|