0
|
1 /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
|
|
2 *
|
|
3 * This is free software; you can redistribute it and/or modify
|
|
4 * it under the terms of the GNU General Public License as published by
|
|
5 * the Free Software Foundation; either version 2 of the License, or
|
|
6 * (at your option) any later version.
|
|
7 *
|
|
8 * This software is distributed in the hope that it will be useful,
|
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 * GNU General Public License for more details.
|
|
12 *
|
|
13 * You should have received a copy of the GNU General Public License
|
|
14 * along with this software; if not, write to the Free Software
|
|
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
16 * USA.
|
|
17 */
|
|
18
|
|
19 //
|
|
20 // rdr::InStream marshalls data from a buffer stored in RDR (RFB Data
|
|
21 // Representation).
|
|
22 //
|
|
23
|
|
24 abstract public class InStream {
|
|
25
|
4
|
26 // check() ensures there is buffer data for at least one item of size
|
|
27 // itemSize bytes. Returns the number of items in the buffer (up to a
|
|
28 // maximum of nItems).
|
|
29
|
|
30 public final int check(int itemSize, int nItems) throws Exception {
|
|
31 if (ptr + itemSize * nItems > end) {
|
|
32 if (ptr + itemSize > end)
|
|
33 return overrun(itemSize, nItems);
|
0
|
34
|
4
|
35 nItems = (end - ptr) / itemSize;
|
|
36 }
|
|
37 return nItems;
|
|
38 }
|
0
|
39
|
4
|
40 public final void check(int itemSize) throws Exception {
|
|
41 if (ptr + itemSize > end)
|
|
42 overrun(itemSize, 1);
|
|
43 }
|
|
44
|
|
45 // readU/SN() methods read unsigned and signed N-bit integers.
|
0
|
46
|
4
|
47 public final int readS8() throws Exception {
|
|
48 check(1);
|
|
49 return b[ptr++];
|
|
50 }
|
0
|
51
|
4
|
52 public final int readS16() throws Exception {
|
|
53 check(2);
|
|
54 int b0 = b[ptr++];
|
|
55 int b1 = b[ptr++] & 0xff;
|
|
56 return b0 << 8 | b1;
|
|
57 }
|
0
|
58
|
4
|
59 public final int readS32() throws Exception {
|
|
60 check(4);
|
|
61 int b0 = b[ptr++];
|
|
62 int b1 = b[ptr++] & 0xff;
|
|
63 int b2 = b[ptr++] & 0xff;
|
|
64 int b3 = b[ptr++] & 0xff;
|
|
65 return b0 << 24 | b1 << 16 | b2 << 8 | b3;
|
|
66 }
|
0
|
67
|
4
|
68 public final int readU8() throws Exception {
|
|
69 return readS8() & 0xff;
|
|
70 }
|
|
71
|
|
72 public final int readU16() throws Exception {
|
|
73 return readS16() & 0xffff;
|
|
74 }
|
0
|
75
|
4
|
76 public final int readU32() throws Exception {
|
|
77 return readS32() & 0xffffffff;
|
|
78 }
|
0
|
79
|
4
|
80 // readString() reads a string - a U32 length followed by the data.
|
0
|
81
|
4
|
82 public final String readString() throws Exception {
|
|
83 int len = readU32();
|
|
84 if (len > maxStringLength)
|
|
85 throw new Exception("InStream max string length exceeded");
|
0
|
86
|
4
|
87 char[] str = new char[len];
|
|
88 int i = 0;
|
|
89 while (i < len) {
|
|
90 int j = i + check(1, len - i);
|
|
91 while (i < j) {
|
|
92 str[i++] = (char) b[ptr++];
|
|
93 }
|
|
94 }
|
|
95
|
|
96 return new String(str);
|
|
97 }
|
0
|
98
|
4
|
99 // maxStringLength protects against allocating a huge buffer. Set it
|
|
100 // higher if you need longer strings.
|
|
101
|
|
102 public static int maxStringLength = 65535;
|
0
|
103
|
4
|
104 public final void skip(int bytes) throws Exception {
|
|
105 while (bytes > 0) {
|
|
106 int n = check(1, bytes);
|
|
107 ptr += n;
|
|
108 bytes -= n;
|
|
109 }
|
|
110 }
|
0
|
111
|
4
|
112 // readBytes() reads an exact number of bytes into an array at an offset.
|
0
|
113
|
4
|
114 public void readBytes(byte[] data, int offset, int length) throws Exception {
|
|
115 int offsetEnd = offset + length;
|
|
116 while (offset < offsetEnd) {
|
|
117 int n = check(1, offsetEnd - offset);
|
|
118 System.arraycopy(b, ptr, data, offset, n);
|
|
119 ptr += n;
|
|
120 offset += n;
|
|
121 }
|
|
122 }
|
0
|
123
|
4
|
124 // readOpaqueN() reads a quantity "without byte-swapping". Because java has
|
|
125 // no byte-ordering, we just use big-endian.
|
|
126
|
|
127 public final int readOpaque8() throws Exception {
|
|
128 return readU8();
|
|
129 }
|
|
130
|
|
131 public final int readOpaque16() throws Exception {
|
|
132 return readU16();
|
|
133 }
|
0
|
134
|
4
|
135 public final int readOpaque32() throws Exception {
|
|
136 return readU32();
|
|
137 }
|
0
|
138
|
4
|
139 public final int readOpaque24A() throws Exception {
|
|
140 check(3);
|
|
141 int b0 = b[ptr++];
|
|
142 int b1 = b[ptr++];
|
|
143 int b2 = b[ptr++];
|
|
144 return b0 << 24 | b1 << 16 | b2 << 8;
|
|
145 }
|
0
|
146
|
4
|
147 public final int readOpaque24B() throws Exception {
|
|
148 check(3);
|
|
149 int b0 = b[ptr++];
|
|
150 int b1 = b[ptr++];
|
|
151 int b2 = b[ptr++];
|
|
152 return b0 << 16 | b1 << 8 | b2;
|
|
153 }
|
0
|
154
|
4
|
155 // pos() returns the position in the stream.
|
0
|
156
|
4
|
157 abstract public int pos();
|
|
158
|
|
159 // bytesAvailable() returns true if at least one byte can be read from the
|
|
160 // stream without blocking. i.e. if false is returned then readU8() would
|
|
161 // block.
|
|
162
|
|
163 public boolean bytesAvailable() {
|
|
164 return end != ptr;
|
|
165 }
|
0
|
166
|
4
|
167 // getbuf(), getptr(), getend() and setptr() are "dirty" methods which allow
|
|
168 // you to manipulate the buffer directly. This is useful for a stream which
|
|
169 // is a wrapper around an underlying stream.
|
0
|
170
|
4
|
171 public final byte[] getbuf() {
|
|
172 return b;
|
|
173 }
|
0
|
174
|
4
|
175 public final int getptr() {
|
|
176 return ptr;
|
|
177 }
|
0
|
178
|
4
|
179 public final int getend() {
|
|
180 return end;
|
|
181 }
|
0
|
182
|
4
|
183 public final void setptr(int p) {
|
|
184 ptr = p;
|
|
185 }
|
0
|
186
|
4
|
187 // overrun() is implemented by a derived class to cope with buffer overrun.
|
|
188 // It ensures there are at least itemSize bytes of buffer data. Returns
|
|
189 // the number of items in the buffer (up to a maximum of nItems). itemSize
|
|
190 // is supposed to be "small" (a few bytes).
|
|
191
|
|
192 abstract protected int overrun(int itemSize, int nItems) throws Exception;
|
0
|
193
|
4
|
194 protected InStream() {
|
|
195 }
|
0
|
196
|
4
|
197 protected byte[] b;
|
|
198 protected int ptr;
|
|
199 protected int end;
|
0
|
200 }
|