source: trunk/src/testing/app/vnc/rfbproto.cpp @ 4

Revision 4, 29.0 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/************************************************************************
2 *
3 *  Copyright (C) 2000, 2001 Const 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/*
25 * rfbproto.c - functions to deal with client side of RFB protocol.
26 */
27
28#if defined(WIN32)
29#include <io.h>
30#include <Winsock2.h>
31#else
32#include <unistd.h>
33#include <pwd.h>
34#endif
35
36#include <errno.h>
37#include <stdio.h>
38#include <string.h>
39
40#include <sys/types.h>
41#include <sys/time.h>
42#include <unistd.h>
43
44#include "sgVNCViewer.h"
45#include "vncauth.h"
46// RJ added for decrypting the password received from SAGE UI
47extern void decryptUIpass(unsigned char *outpass, unsigned char *inpass, unsigned char *key);
48extern unsigned char fixedkey[8]; // = {23,82,107,6,35,78,88,7};
49
50#if defined(WIN32)
51extern "C"
52{
53#define XMD_H
54#include "jpeglib.h"
55}
56#define strncasecmp strnicmp
57#define strcasecmp stricmp
58#include <zlib.h>
59#else
60
61#include <zlib.h>
62
63extern "C" {
64#include <jpeglib.h>
65}
66
67#endif
68
69
70#define TRUE 1
71
72/* The zlib encoding requires expansion/decompression/deflation of the
73   compressed data in the "buffer" above into another, result buffer.
74   However, the size of the result buffer can be determined precisely
75   based on the bitsPerPixel, height and width of the rectangle.  We
76   allocate this buffer one time to be the full size of the buffer. */
77
78static int raw_buffer_size = -1;
79static char *raw_buffer;
80
81static z_stream decompStream;
82static Bool decompStreamInited = False;
83
84
85/*
86 * Variables for the ``tight'' encoding implementation.
87 */
88
89/* Separate buffer for compressed data. */
90#define ZLIB_BUFFER_SIZE 512
91static char zlib_buffer[ZLIB_BUFFER_SIZE];
92
93/* Four independent compression streams for zlib library. */
94static z_stream zlibStream[4];
95static Bool zlibStreamActive[4] = {
96  False, False, False, False
97};
98
99/* Filter stuff. Should be initialized by filter initialization code. */
100static Bool cutZeros;
101static int rectWidth, rectColors;
102static char tightPalette[256*4];
103static CARD8 tightPrevRow[2048*3*sizeof(CARD16)];
104
105/* JPEG decoder state. */
106static Bool jpegError;
107
108static void JpegInitSource(j_decompress_ptr cinfo);
109static boolean JpegFillInputBuffer(j_decompress_ptr cinfo);
110static void JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes);
111static void JpegTermSource(j_decompress_ptr cinfo);
112static void JpegSetSrcManager(j_decompress_ptr cinfo, CARD8 *compressedData,
113                              int compressedLen);
114
115/*
116 * ConnectToRFBServer.
117 */
118
119Bool
120VNCViewer::ConnectToRFBServer()
121{
122    unsigned long host;
123
124    if (!StringToIPAddr(vncServerHost, &host)) {
125        aLog("Couldn't convert '%s' to host address\n", vncServerHost);
126        return False;
127    }
128
129    rfbsock = ConnectToTcpAddr(host, vncServerPort);
130
131    if (rfbsock < 0) {
132        aLog("Unable to connect to VNC server\n");
133        return False;
134    }
135        // return SetNonBlocking(rfbsock); // does not work
136    return TRUE;
137}
138
139
140/*
141 * InitialiseRFBConnection.
142 */
143
144Bool
145VNCViewer::InitialiseRFBConnection(char *pass)
146{
147    char *desktopName;
148    rfbProtocolVersionMsg pv;
149    int major,minor;
150    CARD32 authScheme, reasonLen, authResult;
151    char *reason;
152    CARD8 challenge[CHALLENGESIZE];
153    char passwd[8];
154    int i;
155    rfbClientInitMsg ci;
156
157        /* if the connection is immediately closed, don't report anything, so
158           that pmw's monitor can make test connections */
159
160    if (!ReadFromRFBServer(pv, sz_rfbProtocolVersionMsg)) return False;
161   
162    pv[sz_rfbProtocolVersionMsg] = 0;
163
164    if (sscanf(pv,rfbProtocolVersionFormat,&major,&minor) != 2) {
165        aLog("Not a valid VNC server\n");
166        return False;
167    }
168
169    aLog("VNC server supports protocol version %d.%d (viewer %d.%d)\n",
170            major, minor, rfbProtocolMajorVersion, rfbProtocolMinorVersion);
171
172    major = rfbProtocolMajorVersion;
173    minor = rfbProtocolMinorVersion;
174
175    sprintf(pv,rfbProtocolVersionFormat,major,minor);
176
177    if (!WriteExact(rfbsock, pv, sz_rfbProtocolVersionMsg)) return False;
178
179    if (!ReadFromRFBServer((char *)&authScheme, 4)) return False;
180   
181    authScheme = Swap32IfLE(authScheme);
182
183    switch (authScheme) {
184
185        case rfbConnFailed:
186            if (!ReadFromRFBServer((char *)&reasonLen, 4)) return False;
187            reasonLen = Swap32IfLE(reasonLen);
188
189            reason = (char*) malloc(reasonLen);
190
191            if (!ReadFromRFBServer(reason, reasonLen)) return False;
192
193            aLog("VNC connection failed: %.*s\n",(int)reasonLen, reason);
194            return False;
195
196        case rfbNoAuth:
197            aLog("No authentication needed\n");
198            break;
199
200        case rfbVncAuth:
201            aLog("here\n");
202           
203            if (!ReadFromRFBServer((char *)challenge, CHALLENGESIZE)) return False;
204            if (passwordFile) {
205                strcpy(passwd, vncDecryptPasswdFromFile(passwordFile));
206                if (!passwd) {
207                    aLog("Cannot read valid password from file \"%s\"\n",
208                            passwordFile);
209                    return False;
210                }
211            } else
212              {
213                // sageui will send the password as a DES encrypted string so decrypt it here
214                //decryptUIpass((unsigned char*)passwd, (unsigned char*)pass, fixedkey);
215                strcpy(passwd, pass);
216              }
217
218            if ((!passwd) || (strlen(passwd) == 0)) {
219                aLog("Reading password failed\n");
220                return False;
221            }
222            if (strlen(passwd) > 8) {
223                passwd[8] = '\0';
224            }
225
226            vncEncryptBytes(challenge, passwd);
227                /* Lose the password from memory */
228            for (i = strlen(passwd); i >= 0; i--) {
229                passwd[i] = '\0';
230            }
231
232            if (!WriteExact(rfbsock, (char *)challenge, CHALLENGESIZE)) return False;
233
234            if (!ReadFromRFBServer((char *)&authResult, 4)) return False;
235
236            authResult = Swap32IfLE(authResult);
237
238            switch (authResult) {
239                case rfbVncAuthOK:
240                    aLog("VNC authentication succeeded\n");
241                    break;
242                case rfbVncAuthFailed:
243                    aLog("VNC authentication failed\n");
244                    return False;
245                case rfbVncAuthTooMany:
246                    aLog("VNC authentication failed - too many tries\n");
247                    return False;
248                default:
249                    aLog("Unknown VNC authentication result: %d\n",
250                            (int)authResult);
251                    return False;
252            }
253            break;
254
255        default:
256            aLog("Unknown authentication scheme from VNC server: %d\n",
257                    (int)authScheme);
258            return False;
259    }
260
261    ci.shared = (shareDesktop ? 1 : 0);
262
263    if (!WriteExact(rfbsock, (char *)&ci, sz_rfbClientInitMsg)) return False;
264
265    if (!ReadFromRFBServer((char *)&si, sz_rfbServerInitMsg)) return False;
266
267    si.framebufferWidth = Swap16IfLE(si.framebufferWidth);
268    si.framebufferHeight = Swap16IfLE(si.framebufferHeight);
269    si.format.redMax = Swap16IfLE(si.format.redMax);
270    si.format.greenMax = Swap16IfLE(si.format.greenMax);
271    si.format.blueMax = Swap16IfLE(si.format.blueMax);
272    si.nameLength = Swap32IfLE(si.nameLength);
273
274    desktopName = (char*) malloc(si.nameLength + 1);
275
276    if (!ReadFromRFBServer(desktopName, si.nameLength)) return False;
277
278    desktopName[si.nameLength] = 0;
279
280    aLog("Desktop name \"%s\"\n",desktopName);
281    aLog("Connected to VNC server, using protocol version %d.%d\n",
282            rfbProtocolMajorVersion, rfbProtocolMinorVersion);
283    aLog("VNC server default format:\n");
284
285    PrintPixelFormat(&si.format);
286
287    return True;
288}
289
290
291/*
292 * SetFormatAndEncodings.
293 */
294
295Bool
296VNCViewer::SetFormatAndEncodings()
297{
298    rfbSetPixelFormatMsg spf;
299    char buf[sz_rfbSetEncodingsMsg + MAX_ENCODINGS * 4];
300    rfbSetEncodingsMsg *se = (rfbSetEncodingsMsg *)buf;
301    CARD32 *encs = (CARD32 *)(&buf[sz_rfbSetEncodingsMsg]);
302    int len = 0;
303    Bool requestCompressLevel = False;
304    Bool requestQualityLevel = False;
305    Bool requestLastRectEncoding = False;
306
307    spf.type            = rfbSetPixelFormat;
308    spf.format          = myFormat;
309    spf.format.redMax   = Swap16IfLE(spf.format.redMax);
310    spf.format.greenMax = Swap16IfLE(spf.format.greenMax);
311    spf.format.blueMax  = Swap16IfLE(spf.format.blueMax);
312
313    if (!WriteExact(rfbsock, (char *)&spf, sz_rfbSetPixelFormatMsg))
314        return False;
315
316    se->type = rfbSetEncodings;
317    se->nEncodings = 0;
318
319    if (encodingsString)
320    {
321        char *encStr = encodingsString;
322        int encStrLen;
323        do {
324            char *nextEncStr = strchr(encStr, ' ');
325            if (nextEncStr)
326            {
327                encStrLen = nextEncStr - encStr;
328                nextEncStr++;
329            } else
330            {
331                encStrLen = strlen(encStr);
332            }
333
334            if (strncmp(encStr,"raw",encStrLen) == 0)
335            {
336                encs[se->nEncodings++] = Swap32IfLE(rfbEncodingRaw);
337            } else if (strncmp(encStr,"copyrect",encStrLen) == 0)
338            {
339                encs[se->nEncodings++] = Swap32IfLE(rfbEncodingCopyRect);
340            } else if (strncasecmp(encStr,"tight",encStrLen) == 0)
341            {
342                encs[se->nEncodings++] = Swap32IfLE(rfbEncodingTight);
343                requestLastRectEncoding = True;
344                if (compressLevel >= 0 && compressLevel <= 9)
345                    requestCompressLevel = True;
346                if (qualityLevel >= 0 && qualityLevel <= 9)
347                    requestQualityLevel = True;
348            } else if (strncmp(encStr,"hextile",encStrLen) == 0)
349            {
350                encs[se->nEncodings++] = Swap32IfLE(rfbEncodingHextile);
351            } else if (strncasecmp(encStr,"zlib",encStrLen) == 0) {
352                encs[se->nEncodings++] = Swap32IfLE(rfbEncodingZlib);
353                if (compressLevel >= 0 && compressLevel <= 9)
354                    requestCompressLevel = True;
355            } else if (strncmp(encStr,"corre",encStrLen) == 0)
356            {
357                encs[se->nEncodings++] = Swap32IfLE(rfbEncodingCoRRE);
358            } else if (strncmp(encStr,"rre",encStrLen) == 0)
359            {
360                encs[se->nEncodings++] = Swap32IfLE(rfbEncodingRRE);
361            } else
362            {
363                aLog("Unknown encoding '%.*s'\n",encStrLen,encStr);
364            }
365           
366            encStr = nextEncStr;
367        } while (encStr && se->nEncodings < MAX_ENCODINGS);
368       
369        if (se->nEncodings < MAX_ENCODINGS && requestCompressLevel)
370        {
371            encs[se->nEncodings++] = Swap32IfLE(compressLevel +
372                                                rfbEncodingCompressLevel0);
373        }
374       
375        if (se->nEncodings < MAX_ENCODINGS && requestQualityLevel)
376        {
377            encs[se->nEncodings++] = Swap32IfLE(qualityLevel +
378                                                rfbEncodingQualityLevel0);
379        }
380       
381        if (useRemoteCursor)
382        {
383            if (se->nEncodings < MAX_ENCODINGS)
384                encs[se->nEncodings++] = Swap32IfLE(rfbEncodingXCursor);
385            if (se->nEncodings < MAX_ENCODINGS)
386                encs[se->nEncodings++] = Swap32IfLE(rfbEncodingRichCursor);
387        }
388       
389        if (se->nEncodings < MAX_ENCODINGS && requestLastRectEncoding) {
390            encs[se->nEncodings++] = Swap32IfLE(rfbEncodingLastRect);
391        }
392       
393    } else
394    {
395        if (SameMachine(rfbsock))
396        {
397            aLog("Same machine: preferring raw encoding\n");
398            encs[se->nEncodings++] = Swap32IfLE(rfbEncodingRaw);
399        }
400
401        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingCopyRect);
402        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingTight);
403        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingHextile);
404        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingZlib);
405        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingCoRRE);
406        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingRRE);
407
408        if (compressLevel >= 0 && compressLevel <= 9)
409        {
410            encs[se->nEncodings++] = Swap32IfLE(compressLevel +
411                                                rfbEncodingCompressLevel0);
412        }
413        else
414        {
415            encs[se->nEncodings++] = Swap32IfLE(rfbEncodingCompressLevel1);
416        }
417       
418        if (qualityLevel >= 0 && qualityLevel <= 9)
419        {
420            encs[se->nEncodings++] = Swap32IfLE(qualityLevel +
421                                                rfbEncodingQualityLevel0);
422        }
423
424        if (useRemoteCursor)
425        {
426            encs[se->nEncodings++] = Swap32IfLE(rfbEncodingXCursor);
427            encs[se->nEncodings++] = Swap32IfLE(rfbEncodingRichCursor);
428        }
429
430        encs[se->nEncodings++] = Swap32IfLE(rfbEncodingLastRect);
431    }
432
433    len = sz_rfbSetEncodingsMsg + se->nEncodings * 4;
434
435    se->nEncodings = Swap16IfLE(se->nEncodings);
436
437    if (!WriteExact(rfbsock, buf, len)) return False;
438
439    return True;
440}
441
442
443/*
444 * SendIncrementalFramebufferUpdateRequest.
445 */
446
447Bool
448VNCViewer::SendIncrementalFramebufferUpdateRequest()
449{
450    return SendFramebufferUpdateRequest(0, 0, si.framebufferWidth,
451                                        si.framebufferHeight, True);
452}
453
454
455/*
456 * SendFramebufferUpdateRequest.
457 */
458
459Bool
460VNCViewer::SendFramebufferUpdateRequest(int x, int y,
461                                        int w, int h,
462                                        Bool incremental)
463{
464    Bool res;
465   
466    rfbFramebufferUpdateRequestMsg fur;
467
468    fur.type = rfbFramebufferUpdateRequest;
469    fur.incremental = incremental ? 1 : 0;
470    fur.x = Swap16IfLE(x);
471    fur.y = Swap16IfLE(y);
472    fur.w = Swap16IfLE(w);
473    fur.h = Swap16IfLE(h);
474
475    res = WriteExact(rfbsock, (char *)&fur, sz_rfbFramebufferUpdateRequestMsg);
476
477    if (!res)
478        return False;
479
480    return True;
481}
482
483
484/*
485 * SendPointerEvent.
486 */
487
488Bool
489VNCViewer::SendPointerEvent(int x, int y, int buttonMask)
490{
491    rfbPointerEventMsg pe;
492
493    pe.type = rfbPointerEvent;
494    pe.buttonMask = buttonMask;
495    if (x < 0) x = 0;
496    if (y < 0) y = 0;
497    pe.x = Swap16IfLE(x);
498    pe.y = Swap16IfLE(y);
499    return WriteExact(rfbsock, (char *)&pe, sz_rfbPointerEventMsg);
500}
501
502
503/*
504 * SendKeyEvent.
505 */
506
507Bool
508VNCViewer::SendKeyEvent(CARD32 key, Bool down)
509{
510    rfbKeyEventMsg ke;
511
512    ke.type = rfbKeyEvent;
513    ke.down = down ? 1 : 0;
514    ke.key = Swap32IfLE(key);
515    return WriteExact(rfbsock, (char *)&ke, sz_rfbKeyEventMsg);
516}
517
518
519/*
520 * SendClientCutText.
521 */
522
523Bool
524VNCViewer::SendClientCutText(char *str, int len)
525{
526    rfbClientCutTextMsg cct;
527
528    if (serverCutText)
529        free(serverCutText);
530    serverCutText = NULL;
531
532    cct.type = rfbClientCutText;
533    cct.length = Swap32IfLE(len);
534    aLog( "Send SendClientCutText\n");
535    return  (WriteExact(rfbsock, (char *)&cct, sz_rfbClientCutTextMsg) &&
536             WriteExact(rfbsock, str, len));
537}
538
539
540/*
541 * HandleRFBServerMessage.
542 */
543
544Bool
545VNCViewer::HandleRFBServerMessage(sgVNCViewer *ct)
546{
547    rfbServerToClientMsg msg;
548
549    fd_set fds;
550   
551    FD_ZERO(&fds);
552    FD_SET(rfbsock,&fds);
553
554    struct timeval timeout;
555    timeout.tv_usec = 5000000;
556    timeout.tv_sec  = 0;
557
558    if (!select(rfbsock+1, &fds, NULL, NULL, &timeout))
559    {
560      //fprintf(stderr, "VNC Timeout --> closing VNCViewer.\n");
561      //return False;
562      ;   
563    }
564   
565    //fprintf(stderr, "NO Timeout\n");
566   
567
568        if (!ReadFromRFBServer((char *)&msg, 1))
569    {
570        aLog("VNC> Failed to read message\n");
571        return False;
572    }
573   
574    switch (msg.type)
575    {
576        case rfbSetColourMapEntries:
577            break;
578
579        case rfbFramebufferUpdate:
580            rfbFramebufferUpdateRectHeader rect;
581            int linesToRead;
582            int bytesPerLine;
583            int i;
584
585            if (!ReadFromRFBServer(((char *)&msg.fu) + 1,
586                                   sz_rfbFramebufferUpdateMsg - 1))
587                return False;
588
589            msg.fu.nRects = Swap16IfLE(msg.fu.nRects);
590
591            for (i = 0; i < msg.fu.nRects; i++)
592            {
593                if (!ReadFromRFBServer((char *)&rect, sz_rfbFramebufferUpdateRectHeader))
594                    return False;
595
596                rect.encoding = Swap32IfLE(rect.encoding);
597
598                if (rect.encoding == rfbEncodingLastRect)
599                    break;
600
601                rect.r.x = Swap16IfLE(rect.r.x);
602                rect.r.y = Swap16IfLE(rect.r.y);
603                rect.r.w = Swap16IfLE(rect.r.w);
604                rect.r.h = Swap16IfLE(rect.r.h);
605
606                if (rect.encoding == rfbEncodingXCursor)
607                {
608                    continue;
609                }
610                if (rect.encoding == rfbEncodingRichCursor)
611                {
612                    continue;
613                }
614
615                if ((rect.r.x + rect.r.w > si.framebufferWidth) ||
616                    (rect.r.y + rect.r.h > si.framebufferHeight))
617                {
618                    aLog("Rect too large: %dx%d at (%d, %d)\n",
619                         rect.r.w, rect.r.h, rect.r.x, rect.r.y);
620                    return False;
621                }
622
623                if ((rect.r.h * rect.r.w) == 0) {
624                    aLog("Zero size rect - ignoring\n");
625                    continue;
626                }
627
628                    // If RichCursor encoding is used, we should prevent collisions
629                    // between framebuffer updates and cursor drawing operations.
630//                  SoftCursorLockArea(rect.r.x, rect.r.y, rect.r.w, rect.r.h);
631
632                switch (rect.encoding)
633                {
634                    case rfbEncodingRaw:
635                        bytesPerLine = rect.r.w * myFormat.bitsPerPixel / 8;
636                        linesToRead = BUFFER_SIZE / bytesPerLine;
637
638                        while (rect.r.h > 0)
639                        {
640                            if (linesToRead > rect.r.h)
641                                linesToRead = rect.r.h;
642
643                            if (!ReadFromRFBServer(buffer,
644                                                   bytesPerLine*linesToRead))
645                                return False;
646
647                            ct->CopyDataToScreen(buffer,
648                                                 rect.r.x, rect.r.y, rect.r.w,
649                                                 linesToRead);
650
651                            rect.r.h -= linesToRead;
652                            rect.r.y += linesToRead;
653
654                        }
655                        break;
656
657                    case rfbEncodingCopyRect:
658                    {
659                        rfbCopyRect cr;
660
661                        if (!ReadFromRFBServer((char *)&cr, sz_rfbCopyRect))
662                            return False;
663
664                        cr.srcX = Swap16IfLE(cr.srcX);
665                        cr.srcY = Swap16IfLE(cr.srcY);
666                       
667                            // If RichCursor encoding is used, we should extend our
668                            //  "cursor lock area" (previously set to destination
669                            // rectangle) to the source rectangle as well.
670//                        SoftCursorLockArea(cr.srcX, cr.srcY, rect.r.w, rect.r.h);
671
672                        ct->RectDataToScreen(rect.r.x, rect.r.y,
673                                             rect.r.w, rect.r.h,
674                                             cr.srcX, cr.srcY);
675                        break;
676                    }
677
678                    case rfbEncodingRRE:
679                        switch (myFormat.bitsPerPixel)
680                        {
681                            case 8:
682                                if (!HandleRRE8(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
683                                    return False;
684                                break;
685                            case 16:
686                                if (!HandleRRE16(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
687                                    return False;
688                                break;
689                            case 32:
690                                if (!HandleRRE32(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
691                                    return False;
692                                break;
693                        }
694                        break;
695
696                    case rfbEncodingCoRRE:
697                        switch (myFormat.bitsPerPixel)
698                        {
699                            case 8:
700                                if (!HandleCoRRE8(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
701                                    return False;
702                                break;
703                            case 16:
704                                if (!HandleCoRRE16(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
705                                    return False;
706                                break;
707                            case 32:
708                                if (!HandleCoRRE32(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
709                                    return False;
710                                break;
711                        }
712                        break;
713
714                    case rfbEncodingHextile:
715                        switch (myFormat.bitsPerPixel)
716                        {
717                            case 8:
718                                if (!HandleHextile8(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
719                                    return False;
720                                break;
721                            case 16:
722                                if (!HandleHextile16(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
723                                    return False;
724                                break;
725                            case 32:
726                                if (!HandleHextile32(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
727                                    return False;
728                                break;
729                        }
730                        break;
731
732                    case rfbEncodingZlib:
733                        switch (myFormat.bitsPerPixel) {
734                            case 8:
735                                if (!HandleZlib8(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
736                                    return False;
737                                break;
738                            case 16:
739                                if (!HandleZlib16(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
740                                    return False;
741                                break;
742                            case 32:
743                                if (!HandleZlib32(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
744                                    return False;
745                                break;
746                        }
747                        break;
748
749                    case rfbEncodingTight:
750                        switch (myFormat.bitsPerPixel) {
751                            case 8:
752                                if (!HandleTight8(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
753                                    return False;
754                                break;
755                            case 16:
756                                if (!HandleTight16(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
757                                    return False;
758                                break;
759                            case 32:
760                                if (!HandleTight32(ct, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
761                                    return False;
762                                break;
763                        }
764                        break;
765
766                    default:
767                        aLog("Unknown rect encoding %d\n",
768                             (int)rect.encoding);
769                        return False;
770                }
771                    // Now we may discard "soft cursor locks".
772//                  SoftCursorUnlockScreen();
773               
774            }
775           
776            if (!SendIncrementalFramebufferUpdateRequest())
777            {
778                aLog("VNC> !SendIncrementalFramebufferUpdateRequest() == true\n");
779                return False;
780            }
781           
782            break;
783
784        case rfbBell:
785            break;
786
787        case rfbServerCutText:
788            if (!ReadFromRFBServer(((char *)&msg) + 1,
789                                   sz_rfbServerCutTextMsg - 1))
790                return False;
791
792            msg.sct.length = Swap32IfLE(msg.sct.length);
793
794            if (serverCutText)
795                free(serverCutText);
796
797            serverCutText = (char*) malloc(msg.sct.length+1);
798
799            if (!ReadFromRFBServer(serverCutText, msg.sct.length))
800                return False;
801
802            serverCutText[msg.sct.length] = 0;
803
804            newServerCutText = True;
805
806            break;
807
808        default:
809            aLog("Unknown message type %d from VNC server\n",msg.type);
810            return False;
811    }
812
813    return True;
814}
815
816
817#define GET_PIXEL8(pix, ptr) ((pix) = *(ptr)++)
818
819#define GET_PIXEL16(pix, ptr) (((CARD8*)&(pix))[0] = *(ptr)++, \
820                               ((CARD8*)&(pix))[1] = *(ptr)++)
821
822#define GET_PIXEL32(pix, ptr) (((CARD8*)&(pix))[0] = *(ptr)++, \
823                               ((CARD8*)&(pix))[1] = *(ptr)++, \
824                               ((CARD8*)&(pix))[2] = *(ptr)++, \
825                               ((CARD8*)&(pix))[3] = *(ptr)++)
826
827/* CONCAT2 concatenates its two arguments.  CONCAT2E does the same but also
828   expands its arguments if they are macros */
829
830#define CONCAT2(a,b) a##b
831#define CONCAT2E(a,b) CONCAT2(a,b)
832
833#define BPP 8
834#include "rre.cpp"
835#include "corre.cpp"
836#include "hextile.cpp"
837#include "zlib.cpp"
838#include "tight.cpp"
839#undef BPP
840#define BPP 16
841#include "rre.cpp"
842#include "corre.cpp"
843#include "hextile.cpp"
844#include "zlib.cpp"
845#include "tight.cpp"
846#undef BPP
847#define BPP 32
848#include "rre.cpp"
849#include "corre.cpp"
850#include "hextile.cpp"
851#include "zlib.cpp"
852#include "tight.cpp"
853#undef BPP
854
855
856/*
857 * PrintPixelFormat.
858 */
859
860void
861VNCViewer::PrintPixelFormat(rfbPixelFormat *format)
862{
863    if (format->bitsPerPixel == 1)
864    {
865        aLog("  Single bit per pixel.\n");
866        aLog( "  %s significant bit in each byte is leftmost on the screen.\n",
867                (format->bigEndian ? "Most" : "Least"));
868    }
869    else
870    {
871        aLog("  %d bits per pixel.\n",format->bitsPerPixel);
872        if (format->bitsPerPixel != 8)
873        {
874            aLog("  %s significant byte first in each pixel.\n",
875                    (format->bigEndian ? "Most" : "Least"));
876        }
877        if (format->trueColour)
878        {
879            aLog("  True colour: max red %d green %d blue %d",
880                    format->redMax, format->greenMax, format->blueMax);
881            aLog(", shift red %d green %d blue %d\n",
882                    format->redShift, format->greenShift, format->blueShift);
883        }
884        else
885        {
886            aLog("  Colour map (not true colour).\n");
887        }
888    }
889}
890
891long
892VNCViewer::ReadCompactLen (void)
893{
894    long len;
895    CARD8 b;
896
897    if (!ReadFromRFBServer((char *)&b, 1))
898        return -1;
899    len = (int)b & 0x7F;
900    if (b & 0x80) {
901        if (!ReadFromRFBServer((char *)&b, 1))
902            return -1;
903        len |= ((int)b & 0x7F) << 7;
904        if (b & 0x80) {
905            if (!ReadFromRFBServer((char *)&b, 1))
906                return -1;
907            len |= ((int)b & 0xFF) << 14;
908        }
909    }
910    return len;
911}
912
913/*
914 * JPEG source manager functions for JPEG decompression in Tight decoder.
915 */
916
917static struct jpeg_source_mgr jpegSrcManager;
918static JOCTET *jpegBufferPtr;
919static size_t jpegBufferLen;
920
921static void
922JpegInitSource(j_decompress_ptr cinfo)
923{
924  jpegError = False;
925}
926
927static boolean
928JpegFillInputBuffer(j_decompress_ptr cinfo)
929{
930  jpegError = True;
931  jpegSrcManager.bytes_in_buffer = jpegBufferLen;
932  jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr;
933
934  return TRUE;
935}
936
937static void
938JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes)
939{
940  if (num_bytes < 0 || num_bytes > jpegSrcManager.bytes_in_buffer) {
941    jpegError = True;
942    jpegSrcManager.bytes_in_buffer = jpegBufferLen;
943    jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr;
944  } else {
945    jpegSrcManager.next_input_byte += (size_t) num_bytes;
946    jpegSrcManager.bytes_in_buffer -= (size_t) num_bytes;
947  }
948}
949
950static void
951JpegTermSource(j_decompress_ptr cinfo)
952{
953  /* No work necessary here. */
954}
955
956static void
957JpegSetSrcManager(j_decompress_ptr cinfo, CARD8 *compressedData,
958                  int compressedLen)
959{
960  jpegBufferPtr = (JOCTET *)compressedData;
961  jpegBufferLen = (size_t)compressedLen;
962
963  jpegSrcManager.init_source = JpegInitSource;
964  jpegSrcManager.fill_input_buffer = JpegFillInputBuffer;
965  jpegSrcManager.skip_input_data = JpegSkipInputData;
966  jpegSrcManager.resync_to_restart = jpeg_resync_to_restart;
967  jpegSrcManager.term_source = JpegTermSource;
968  jpegSrcManager.next_input_byte = jpegBufferPtr;
969  jpegSrcManager.bytes_in_buffer = jpegBufferLen;
970
971  cinfo->src = &jpegSrcManager;
972}
Note: See TracBrowser for help on using the repository browser.