Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2001,2002 HorizonLive.com, Inc. All Rights Reserved. |
| 3 | // Copyright (C) 2001 Constantin Kaplinsky. All Rights Reserved. |
| 4 | // Copyright (C) 2000 Tridia Corporation. All Rights Reserved. |
| 5 | // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. |
| 6 | // |
| 7 | // This is free software; you can redistribute it and/or modify |
| 8 | // it under the terms of the GNU General Public License as published by |
| 9 | // the Free Software Foundation; either version 2 of the License, or |
| 10 | // (at your option) any later version. |
| 11 | // |
| 12 | // This software is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this software; if not, write to the Free Software |
| 19 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 20 | // USA. |
| 21 | // |
| 22 | |
| 23 | // |
| 24 | // RfbProto.java |
| 25 | // |
| 26 | |
| 27 | import java.io.*; |
| 28 | import java.awt.*; |
| 29 | import java.awt.event.*; |
| 30 | import java.net.Socket; |
| 31 | |
| 32 | class RfbProto { |
| 33 | |
| 34 | final String versionMsg = "RFB 003.003\n"; |
| 35 | final static int ConnFailed = 0, NoAuth = 1, VncAuth = 2; |
| 36 | final static int VncAuthOK = 0, VncAuthFailed = 1, VncAuthTooMany = 2; |
| 37 | |
| 38 | final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2, |
| 39 | ServerCutText = 3; |
| 40 | |
| 41 | final int SetPixelFormat = 0, FixColourMapEntries = 1, SetEncodings = 2, |
| 42 | FramebufferUpdateRequest = 3, KeyboardEvent = 4, PointerEvent = 5, |
| 43 | ClientCutText = 6; |
| 44 | |
| 45 | final static int |
| 46 | EncodingRaw = 0, |
| 47 | EncodingCopyRect = 1, |
| 48 | EncodingRRE = 2, |
| 49 | EncodingCoRRE = 4, |
| 50 | EncodingHextile = 5, |
| 51 | EncodingZlib = 6, |
| 52 | EncodingTight = 7, |
| 53 | EncodingCompressLevel0 = 0xFFFFFF00, |
| 54 | EncodingQualityLevel0 = 0xFFFFFFE0, |
| 55 | EncodingXCursor = 0xFFFFFF10, |
| 56 | EncodingRichCursor = 0xFFFFFF11, |
| 57 | EncodingLastRect = 0xFFFFFF20, |
| 58 | EncodingNewFBSize = 0xFFFFFF21; |
| 59 | |
| 60 | final int HextileRaw = (1 << 0); |
| 61 | final int HextileBackgroundSpecified = (1 << 1); |
| 62 | final int HextileForegroundSpecified = (1 << 2); |
| 63 | final int HextileAnySubrects = (1 << 3); |
| 64 | final int HextileSubrectsColoured = (1 << 4); |
| 65 | |
| 66 | final static int TightExplicitFilter = 0x04; |
| 67 | final static int TightFill = 0x08; |
| 68 | final static int TightJpeg = 0x09; |
| 69 | final static int TightMaxSubencoding = 0x09; |
| 70 | final static int TightFilterCopy = 0x00; |
| 71 | final static int TightFilterPalette = 0x01; |
| 72 | final static int TightFilterGradient = 0x02; |
| 73 | |
| 74 | final static int TightMinToCompress = 12; |
| 75 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 76 | DataInputStream is; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | // |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 80 | // Constructor. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 81 | // |
| 82 | |
Constantin Kaplinsky | f392f44 | 2002-05-20 13:05:42 +0000 | [diff] [blame] | 83 | RfbProto(InputStream is) throws Exception { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 84 | newInputStream(is); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Constantin Kaplinsky | f392f44 | 2002-05-20 13:05:42 +0000 | [diff] [blame] | 87 | public void newInputStream(InputStream is) throws Exception { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 88 | this.is = new DataInputStream(is); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 89 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 90 | readVersionMsg(); |
| 91 | if (readAuthScheme() != NoAuth) { |
Constantin Kaplinsky | f392f44 | 2002-05-20 13:05:42 +0000 | [diff] [blame] | 92 | throw new Exception("Wrong authentication type in the session file"); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 93 | } |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 94 | readServerInit(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // |
| 98 | // Read server's protocol version message |
| 99 | // |
| 100 | |
| 101 | int serverMajor, serverMinor; |
| 102 | |
| 103 | void readVersionMsg() throws IOException { |
| 104 | |
| 105 | byte[] b = new byte[12]; |
| 106 | |
| 107 | is.readFully(b); |
| 108 | |
| 109 | if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') |
| 110 | || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') |
| 111 | || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') |
| 112 | || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') |
| 113 | || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) |
| 114 | { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 115 | throw new IOException("Incorrect protocol version"); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); |
| 119 | serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 124 | // Find out the authentication scheme. |
| 125 | // |
| 126 | |
| 127 | int readAuthScheme() throws IOException { |
| 128 | int authScheme = is.readInt(); |
| 129 | |
| 130 | switch (authScheme) { |
| 131 | |
| 132 | case ConnFailed: |
| 133 | int reasonLen = is.readInt(); |
| 134 | byte[] reason = new byte[reasonLen]; |
| 135 | is.readFully(reason); |
| 136 | throw new IOException(new String(reason)); |
| 137 | |
| 138 | case NoAuth: |
| 139 | case VncAuth: |
| 140 | return authScheme; |
| 141 | |
| 142 | default: |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 143 | throw new IOException("Unknown authentication scheme " + authScheme); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 144 | |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 150 | // Read the server initialisation message |
| 151 | // |
| 152 | |
| 153 | String desktopName; |
| 154 | int framebufferWidth, framebufferHeight; |
| 155 | int bitsPerPixel, depth; |
| 156 | boolean bigEndian, trueColour; |
| 157 | int redMax, greenMax, blueMax, redShift, greenShift, blueShift; |
| 158 | |
Constantin Kaplinsky | f392f44 | 2002-05-20 13:05:42 +0000 | [diff] [blame] | 159 | void readServerInit() throws Exception { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 160 | framebufferWidth = is.readUnsignedShort(); |
| 161 | framebufferHeight = is.readUnsignedShort(); |
| 162 | bitsPerPixel = is.readUnsignedByte(); |
| 163 | depth = is.readUnsignedByte(); |
| 164 | bigEndian = (is.readUnsignedByte() != 0); |
| 165 | trueColour = (is.readUnsignedByte() != 0); |
| 166 | redMax = is.readUnsignedShort(); |
| 167 | greenMax = is.readUnsignedShort(); |
| 168 | blueMax = is.readUnsignedShort(); |
| 169 | redShift = is.readUnsignedByte(); |
| 170 | greenShift = is.readUnsignedByte(); |
| 171 | blueShift = is.readUnsignedByte(); |
| 172 | byte[] pad = new byte[3]; |
| 173 | is.readFully(pad); |
| 174 | int nameLength = is.readInt(); |
| 175 | byte[] name = new byte[nameLength]; |
| 176 | is.readFully(name); |
| 177 | desktopName = new String(name); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
| 181 | // |
| 182 | // Set new framebuffer size |
| 183 | // |
| 184 | |
| 185 | void setFramebufferSize(int width, int height) { |
| 186 | framebufferWidth = width; |
| 187 | framebufferHeight = height; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | // |
| 192 | // Read the server message type |
| 193 | // |
| 194 | |
| 195 | int readServerMessageType() throws IOException { |
| 196 | return is.readUnsignedByte(); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | // |
| 201 | // Read a FramebufferUpdate message |
| 202 | // |
| 203 | |
| 204 | int updateNRects; |
| 205 | |
| 206 | void readFramebufferUpdate() throws IOException { |
| 207 | is.readByte(); |
| 208 | updateNRects = is.readUnsignedShort(); |
| 209 | } |
| 210 | |
| 211 | // Read a FramebufferUpdate rectangle header |
| 212 | |
| 213 | int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding; |
| 214 | |
| 215 | void readFramebufferUpdateRectHdr() throws IOException { |
| 216 | updateRectX = is.readUnsignedShort(); |
| 217 | updateRectY = is.readUnsignedShort(); |
| 218 | updateRectW = is.readUnsignedShort(); |
| 219 | updateRectH = is.readUnsignedShort(); |
| 220 | updateRectEncoding = is.readInt(); |
| 221 | |
| 222 | if ((updateRectEncoding == EncodingLastRect) || |
| 223 | (updateRectEncoding == EncodingNewFBSize)) |
| 224 | return; |
| 225 | |
| 226 | if ((updateRectX + updateRectW > framebufferWidth) || |
| 227 | (updateRectY + updateRectH > framebufferHeight)) { |
| 228 | throw new IOException("Framebuffer update rectangle too large: " + |
| 229 | updateRectW + "x" + updateRectH + " at (" + |
| 230 | updateRectX + "," + updateRectY + ")"); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Read CopyRect source X and Y. |
| 235 | |
| 236 | int copyRectSrcX, copyRectSrcY; |
| 237 | |
| 238 | void readCopyRect() throws IOException { |
| 239 | copyRectSrcX = is.readUnsignedShort(); |
| 240 | copyRectSrcY = is.readUnsignedShort(); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | // |
| 245 | // Read a ServerCutText message |
| 246 | // |
| 247 | |
| 248 | String readServerCutText() throws IOException { |
| 249 | byte[] pad = new byte[3]; |
| 250 | is.readFully(pad); |
| 251 | int len = is.readInt(); |
| 252 | byte[] text = new byte[len]; |
| 253 | is.readFully(text); |
| 254 | return new String(text); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | // |
| 259 | // Read integer in compact representation |
| 260 | // |
| 261 | |
| 262 | int readCompactLen() throws IOException { |
| 263 | int portion = is.readUnsignedByte(); |
| 264 | int len = portion & 0x7F; |
| 265 | if ((portion & 0x80) != 0) { |
| 266 | portion = is.readUnsignedByte(); |
| 267 | len |= (portion & 0x7F) << 7; |
| 268 | if ((portion & 0x80) != 0) { |
| 269 | portion = is.readUnsignedByte(); |
| 270 | len |= (portion & 0xFF) << 14; |
| 271 | } |
| 272 | } |
| 273 | return len; |
| 274 | } |
| 275 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 276 | } |