1 2 package ch.ethz.ssh2.packets; 3 4 import java.io.IOException ; 5 import java.math.BigInteger ; 6 7 import ch.ethz.ssh2.util.Tokenizer; 8 9 15 public class TypesReader 16 { 17 byte[] arr; 18 int pos = 0; 19 int max = 0; 20 21 public TypesReader(byte[] arr) 22 { 23 this.arr = arr; 24 pos = 0; 25 max = arr.length; 26 } 27 28 public TypesReader(byte[] arr, int off) 29 { 30 this.arr = arr; 31 this.pos = off; 32 this.max = arr.length; 33 34 if ((pos < 0) || (pos > arr.length)) 35 throw new IllegalArgumentException ("Illegal offset."); 36 } 37 38 public TypesReader(byte[] arr, int off, int len) 39 { 40 this.arr = arr; 41 this.pos = off; 42 this.max = off + len; 43 44 if ((pos < 0) || (pos > arr.length)) 45 throw new IllegalArgumentException ("Illegal offset."); 46 47 if ((max < 0) || (max > arr.length)) 48 throw new IllegalArgumentException ("Illegal length."); 49 } 50 51 public int readByte() throws IOException 52 { 53 if (pos >= max) 54 throw new IOException ("Packet too short."); 55 56 return (arr[pos++] & 0xff); 57 } 58 59 public byte[] readBytes(int len) throws IOException 60 { 61 if ((pos + len) > max) 62 throw new IOException ("Packet too short."); 63 64 byte[] res = new byte[len]; 65 66 System.arraycopy(arr, pos, res, 0, len); 67 pos += len; 68 69 return res; 70 } 71 72 public void readBytes(byte[] dst, int off, int len) throws IOException 73 { 74 if ((pos + len) > max) 75 throw new IOException ("Packet too short."); 76 77 System.arraycopy(arr, pos, dst, off, len); 78 pos += len; 79 } 80 81 public boolean readBoolean() throws IOException 82 { 83 if (pos >= max) 84 throw new IOException ("Packet too short."); 85 86 return (arr[pos++] != 0); 87 } 88 89 public int readUINT32() throws IOException 90 { 91 if ((pos + 4) > max) 92 throw new IOException ("Packet too short."); 93 94 return ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) 95 | (arr[pos++] & 0xff); 96 } 97 98 public long readUINT64() throws IOException 99 { 100 if ((pos + 8) > max) 101 throw new IOException ("Packet too short."); 102 103 long high = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) 104 | (arr[pos++] & 0xff); 105 106 long low = ((arr[pos++] & 0xff) << 24) | ((arr[pos++] & 0xff) << 16) | ((arr[pos++] & 0xff) << 8) 107 | (arr[pos++] & 0xff); 108 109 return (high << 32) | (low & 0xffffffffl); 110 } 111 112 public BigInteger readMPINT() throws IOException 113 { 114 BigInteger b; 115 116 byte raw[] = readByteString(); 117 118 if (raw.length == 0) 119 b = BigInteger.ZERO; 120 else 121 b = new BigInteger (raw); 122 123 return b; 124 } 125 126 public byte[] readByteString() throws IOException 127 { 128 int len = readUINT32(); 129 130 if ((len + pos) > max) 131 throw new IOException ("Malformed SSH byte string."); 132 133 byte[] res = new byte[len]; 134 System.arraycopy(arr, pos, res, 0, len); 135 pos += len; 136 return res; 137 } 138 139 public String readString(String charsetName) throws IOException 140 { 141 int len = readUINT32(); 142 143 if ((len + pos) > max) 144 throw new IOException ("Malformed SSH string."); 145 146 String res = (charsetName == null) ? new String (arr, pos, len) : new String (arr, pos, len, charsetName); 147 pos += len; 148 149 return res; 150 } 151 152 public String readString() throws IOException 153 { 154 int len = readUINT32(); 155 156 if ((len + pos) > max) 157 throw new IOException ("Malformed SSH string."); 158 159 String res = new String (arr, pos, len); 160 pos += len; 161 162 return res; 163 } 164 165 public String [] readNameList() throws IOException 166 { 167 return Tokenizer.parseTokens(readString(), ','); 168 } 169 170 public int remain() 171 { 172 return max - pos; 173 } 174 175 } 176 | Popular Tags |