aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/tightvnc/rfbplayer/RfbProto.java
blob: 14186b6a6032a33a0072967fe385f691f96ce47f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
//  Copyright (C) 2001,2002 HorizonLive.com, Inc.  All Rights Reserved.
//  Copyright (C) 2001 Constantin Kaplinsky.  All Rights Reserved.
//  Copyright (C) 2000 Tridia Corporation.  All Rights Reserved.
//  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
//
//  This is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This software is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this software; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//

//
// RfbProto.java
//

package com.wimba.RfbPlayer;

import java.io.*;
import java.net.*;

class RfbProto {

  final String versionMsg = "RFB 003.003\n";
  final static int ConnFailed = 0,  NoAuth = 1,  VncAuth = 2;
  final static int VncAuthOK = 0,  VncAuthFailed = 1,  VncAuthTooMany = 2;

  final static int FramebufferUpdate = 0,  SetColourMapEntries = 1,  Bell = 2,  ServerCutText =
      3;

  final int SetPixelFormat = 0,  FixColourMapEntries = 1,  SetEncodings = 2,  FramebufferUpdateRequest =
      3,  KeyboardEvent = 4,  PointerEvent = 5,  ClientCutText = 6;

  final static int EncodingRaw = 0,  EncodingCopyRect = 1,  EncodingRRE = 2,  EncodingCoRRE =
      4,  EncodingHextile = 5,  EncodingZlib = 6,  EncodingTight = 7,  EncodingCompressLevel0 =
      0xFFFFFF00,  EncodingQualityLevel0 = 0xFFFFFFE0,  EncodingXCursor =
      0xFFFFFF10,  EncodingRichCursor = 0xFFFFFF11,  EncodingPointerPos =
      0xFFFFFF18,  EncodingLastRect = 0xFFFFFF20,  EncodingNewFBSize =
      0xFFFFFF21;

  final static int MaxNormalEncoding = 7;

  final int HextileRaw = (1 << 0);
  final int HextileBackgroundSpecified = (1 << 1);
  final int HextileForegroundSpecified = (1 << 2);
  final int HextileAnySubrects = (1 << 3);
  final int HextileSubrectsColoured = (1 << 4);

  final static int TightExplicitFilter = 0x04;
  final static int TightFill = 0x08;
  final static int TightJpeg = 0x09;
  final static int TightMaxSubencoding = 0x09;
  final static int TightFilterCopy = 0x00;
  final static int TightFilterPalette = 0x01;
  final static int TightFilterGradient = 0x02;

  final static int TightMinToCompress = 12;

  FbsInputStream fbs;
  DataInputStream is;


  //
  // Constructor.
  //
  RfbProto(URL url) throws Exception {
    fbs = null;
    newSession(url);
  }

  // Force processing to quit
  public void quit() {
    fbs.quit();
    try {
      fbs.close();
    } catch (IOException e) {
      System.out.println("IOException quitting RfbProto: " + e);
    }
  }

  //
  // Open new session URL.
  //
  public void newSession(URL url) throws Exception {
    if (fbs != null)
      fbs.close();

    // open the connection, and use caching
    URLConnection connection = url.openConnection();
    connection.setUseCaches(true);

    fbs = new FbsInputStream(connection.getInputStream());
    is = new DataInputStream(fbs);

    readVersionMsg();
    if (readAuthScheme() != NoAuth) {
      throw new Exception("Wrong authentication type in the session file");
    }
    readServerInit();
  }

  //
  // Read server's protocol version message.
  //
  int serverMajor, serverMinor;

  void readVersionMsg() throws IOException {

    byte[] b = new byte[12];

    for (int i = 0; i < b.length; i++)
      b[i] = (byte)'0';

    is.readFully(b);

    if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') ||
        (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') || (b[6] <
        '0') || (b[6] > '9') || (b[7] != '.') || (b[8] < '0') || (b[8] > '9') ||
        (b[9] < '0') || (b[9] > '9') || (b[10] < '0') || (b[10] > '9') ||
        (b[11] != '\n')) {
      throw new IOException("Incorrect protocol version");
    }

    serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
    serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
  }


  //
  // Find out the authentication scheme.
  //
  int readAuthScheme() throws IOException {
    int authScheme = is.readInt();

    switch (authScheme) {

    case ConnFailed:
      int reasonLen = is.readInt();
      byte[] reason = new byte[reasonLen];
      is.readFully(reason);
      throw new IOException(new String(reason));

    case NoAuth:
    case VncAuth:
      return authScheme;

    default:
      throw new IOException("Unknown authentication scheme " + authScheme);

    }
  }


  //
  // Read the server initialisation message
  //
  String desktopName;
  int framebufferWidth, framebufferHeight;
  int bitsPerPixel, depth;
  boolean bigEndian, trueColour;
  int redMax, greenMax, blueMax, redShift, greenShift, blueShift;

  void readServerInit() throws Exception {
    framebufferWidth = is.readUnsignedShort();
    framebufferHeight = is.readUnsignedShort();
    bitsPerPixel = is.readUnsignedByte();
    depth = is.readUnsignedByte();
    bigEndian = (is.readUnsignedByte() != 0);
    trueColour = (is.readUnsignedByte() != 0);
    redMax = is.readUnsignedShort();
    greenMax = is.readUnsignedShort();
    blueMax = is.readUnsignedShort();
    redShift = is.readUnsignedByte();
    greenShift = is.readUnsignedByte();
    blueShift = is.readUnsignedByte();
    byte[] pad = new byte[3];
    is.readFully(pad);
    int nameLength = is.readInt();
    byte[] name = new byte[nameLength];
    is.readFully(name);
    desktopName = new String(name);
  }


  //
  // Set new framebuffer size
  //
  void setFramebufferSize(int width, int height) {
    framebufferWidth = width;
    framebufferHeight = height;
  }


  //
  // Read the server message type
  //
  int readServerMessageType() throws IOException {
    return is.readUnsignedByte();
  }


  //
  // Read a FramebufferUpdate message
  //
  int updateNRects;

  void readFramebufferUpdate() throws IOException {
    is.readByte();
    updateNRects = is.readUnsignedShort();
  }

  // Read a FramebufferUpdate rectangle header
  int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;

  void readFramebufferUpdateRectHdr() throws IOException {
    updateRectX = is.readUnsignedShort();
    updateRectY = is.readUnsignedShort();
    updateRectW = is.readUnsignedShort();
    updateRectH = is.readUnsignedShort();
    updateRectEncoding = is.readInt();

    if (updateRectEncoding < 0 || updateRectEncoding > MaxNormalEncoding)
      return;

    if ((updateRectX + updateRectW > framebufferWidth) ||
        (updateRectY + updateRectH > framebufferHeight)) {
      throw new IOException("Framebuffer update rectangle too large: " +
                            updateRectW + "x" + updateRectH + " at (" +
                            updateRectX + "," + updateRectY + ")");
    }
  }

  // Read CopyRect source X and Y.
  int copyRectSrcX, copyRectSrcY;

  void readCopyRect() throws IOException {
    copyRectSrcX = is.readUnsignedShort();
    copyRectSrcY = is.readUnsignedShort();
  }


  //
  // Read a ServerCutText message
  //
  String readServerCutText() throws IOException {
    byte[] pad = new byte[3];
    is.readFully(pad);
    int len = is.readInt();
    byte[] text = new byte[len];
    is.readFully(text);
    return new String(text);
  }


  //
  // Read integer in compact representation
  //
  int readCompactLen() throws IOException {
    int portion = is.readUnsignedByte();
    int len = portion & 0x7F;
    if ((portion & 0x80) != 0) {
      portion = is.readUnsignedByte();
      len |= (portion & 0x7F) << 7;
      if ((portion & 0x80) != 0) {
        portion = is.readUnsignedByte();
        len |= (portion & 0xFF) << 14;
      }
    }
    return len;
  }

}