Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 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 | |
Adam Tkac | f53e62a | 2009-03-13 13:20:26 +0000 | [diff] [blame] | 24 | package com.tigervnc.vncviewer; |
Constantin Kaplinsky | 90d8a50 | 2008-04-14 09:45:50 +0000 | [diff] [blame] | 25 | |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 26 | abstract public class InStream { |
| 27 | |
| 28 | // check() ensures there is buffer data for at least one item of size |
| 29 | // itemSize bytes. Returns the number of items in the buffer (up to a |
| 30 | // maximum of nItems). |
| 31 | |
| 32 | public final int check(int itemSize, int nItems) throws Exception { |
| 33 | if (ptr + itemSize * nItems > end) { |
| 34 | if (ptr + itemSize > end) |
| 35 | return overrun(itemSize, nItems); |
| 36 | |
| 37 | nItems = (end - ptr) / itemSize; |
| 38 | } |
| 39 | return nItems; |
| 40 | } |
| 41 | |
| 42 | public final void check(int itemSize) throws Exception { |
| 43 | if (ptr + itemSize > end) |
| 44 | overrun(itemSize, 1); |
| 45 | } |
| 46 | |
| 47 | // readU/SN() methods read unsigned and signed N-bit integers. |
| 48 | |
| 49 | public final int readS8() throws Exception { |
| 50 | check(1); return b[ptr++]; |
| 51 | } |
| 52 | |
| 53 | public final int readS16() throws Exception { |
| 54 | check(2); int b0 = b[ptr++]; |
| 55 | int b1 = b[ptr++] & 0xff; return b0 << 8 | b1; |
| 56 | } |
| 57 | |
| 58 | public final int readS32() throws Exception { |
| 59 | check(4); int b0 = b[ptr++]; |
| 60 | int b1 = b[ptr++] & 0xff; |
| 61 | int b2 = b[ptr++] & 0xff; |
| 62 | int b3 = b[ptr++] & 0xff; |
| 63 | return b0 << 24 | b1 << 16 | b2 << 8 | b3; |
| 64 | } |
| 65 | |
| 66 | public final int readU8() throws Exception { |
| 67 | return readS8() & 0xff; |
| 68 | } |
| 69 | |
| 70 | public final int readU16() throws Exception { |
| 71 | return readS16() & 0xffff; |
| 72 | } |
| 73 | |
| 74 | public final int readU32() throws Exception { |
| 75 | return readS32() & 0xffffffff; |
| 76 | } |
| 77 | |
| 78 | // readString() reads a string - a U32 length followed by the data. |
| 79 | |
| 80 | public final String readString() throws Exception { |
| 81 | int len = readU32(); |
| 82 | if (len > maxStringLength) |
| 83 | throw new Exception("InStream max string length exceeded"); |
| 84 | |
| 85 | char[] str = new char[len]; |
| 86 | int i = 0; |
| 87 | while (i < len) { |
| 88 | int j = i + check(1, len - i); |
| 89 | while (i < j) { |
| 90 | str[i++] = (char)b[ptr++]; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return new String(str); |
| 95 | } |
| 96 | |
| 97 | // maxStringLength protects against allocating a huge buffer. Set it |
| 98 | // higher if you need longer strings. |
| 99 | |
| 100 | public static int maxStringLength = 65535; |
| 101 | |
| 102 | public final void skip(int bytes) throws Exception { |
| 103 | while (bytes > 0) { |
| 104 | int n = check(1, bytes); |
| 105 | ptr += n; |
| 106 | bytes -= n; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // readBytes() reads an exact number of bytes into an array at an offset. |
| 111 | |
| 112 | public void readBytes(byte[] data, int offset, int length) throws Exception { |
| 113 | int offsetEnd = offset + length; |
| 114 | while (offset < offsetEnd) { |
| 115 | int n = check(1, offsetEnd - offset); |
| 116 | System.arraycopy(b, ptr, data, offset, n); |
| 117 | ptr += n; |
| 118 | offset += n; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // readOpaqueN() reads a quantity "without byte-swapping". Because java has |
| 123 | // no byte-ordering, we just use big-endian. |
| 124 | |
| 125 | public final int readOpaque8() throws Exception { |
| 126 | return readU8(); |
| 127 | } |
| 128 | |
| 129 | public final int readOpaque16() throws Exception { |
| 130 | return readU16(); |
| 131 | } |
| 132 | |
| 133 | public final int readOpaque32() throws Exception { |
| 134 | return readU32(); |
| 135 | } |
| 136 | |
| 137 | public final int readOpaque24A() throws Exception { |
| 138 | check(3); int b0 = b[ptr++]; |
| 139 | int b1 = b[ptr++]; int b2 = b[ptr++]; |
| 140 | return b0 << 24 | b1 << 16 | b2 << 8; |
| 141 | } |
| 142 | |
| 143 | public final int readOpaque24B() throws Exception { |
| 144 | check(3); int b0 = b[ptr++]; |
| 145 | int b1 = b[ptr++]; int b2 = b[ptr++]; |
| 146 | return b0 << 16 | b1 << 8 | b2; |
| 147 | } |
| 148 | |
| 149 | // pos() returns the position in the stream. |
| 150 | |
| 151 | abstract public int pos(); |
| 152 | |
| 153 | // bytesAvailable() returns true if at least one byte can be read from the |
| 154 | // stream without blocking. i.e. if false is returned then readU8() would |
| 155 | // block. |
| 156 | |
| 157 | public boolean bytesAvailable() { return end != ptr; } |
| 158 | |
| 159 | // getbuf(), getptr(), getend() and setptr() are "dirty" methods which allow |
| 160 | // you to manipulate the buffer directly. This is useful for a stream which |
| 161 | // is a wrapper around an underlying stream. |
| 162 | |
| 163 | public final byte[] getbuf() { return b; } |
| 164 | public final int getptr() { return ptr; } |
| 165 | public final int getend() { return end; } |
| 166 | public final void setptr(int p) { ptr = p; } |
| 167 | |
| 168 | // overrun() is implemented by a derived class to cope with buffer overrun. |
| 169 | // It ensures there are at least itemSize bytes of buffer data. Returns |
| 170 | // the number of items in the buffer (up to a maximum of nItems). itemSize |
| 171 | // is supposed to be "small" (a few bytes). |
| 172 | |
| 173 | abstract protected int overrun(int itemSize, int nItems) throws Exception; |
| 174 | |
| 175 | protected InStream() {} |
| 176 | protected byte[] b; |
| 177 | protected int ptr; |
| 178 | protected int end; |
| 179 | } |