1 | /************************************************************************ |
---|
2 | * |
---|
3 | * Copyright (C) 2000 Tridia Corporation. All Rights Reserved. |
---|
4 | * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. |
---|
5 | * |
---|
6 | * This is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This software is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this software; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
---|
19 | * USA. |
---|
20 | */ |
---|
21 | |
---|
22 | /* |
---|
23 | * zlib.c - handle zlib encoding. |
---|
24 | * |
---|
25 | * This file shouldn't be compiled directly. It is included multiple times by |
---|
26 | * rfbproto.c, each time with a different definition of the macro BPP. For |
---|
27 | * each value of BPP, this file defines a function which handles an zlib |
---|
28 | * encoded rectangle with BPP bits per pixel. |
---|
29 | */ |
---|
30 | #ifdef HandleZlibBPP |
---|
31 | #undef HandleZlibBPP |
---|
32 | #endif |
---|
33 | #ifdef ReceiveZlibBPP |
---|
34 | #undef ReceiveZlibBPP |
---|
35 | #endif |
---|
36 | |
---|
37 | #define HandleZlibBPP CONCAT2E(HandleZlib,BPP) |
---|
38 | #define ReceiveZlibBPP CONCAT2E(ReceiveZlib,BPP) |
---|
39 | #define flushZlibBPP CONCAT2E(flushZlib,BPP) |
---|
40 | #define CARDBPP CONCAT2E(CARD,BPP) |
---|
41 | |
---|
42 | Bool |
---|
43 | VNCViewer::HandleZlibBPP (sgVNCViewer *ct, int rx, int ry, int rw, int rh) |
---|
44 | { |
---|
45 | rfbZlibHeader hdr; |
---|
46 | int remaining; |
---|
47 | int inflateResult; |
---|
48 | int toRead; |
---|
49 | |
---|
50 | /* First make sure we have a large enough raw buffer to hold the |
---|
51 | * decompressed data. In practice, with a fixed BPP, fixed frame |
---|
52 | * buffer size and the first update containing the entire frame |
---|
53 | * buffer, this buffer allocation should only happen once, on the |
---|
54 | * first update. |
---|
55 | */ |
---|
56 | if ( raw_buffer_size < (( rw * rh ) * ( BPP / 8 ))) { |
---|
57 | |
---|
58 | if ( raw_buffer != NULL ) { |
---|
59 | |
---|
60 | free( raw_buffer ); |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | raw_buffer_size = (( rw * rh ) * ( BPP / 8 )); |
---|
65 | raw_buffer = (char*) malloc( raw_buffer_size ); |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | if (!ReadFromRFBServer((char *)&hdr, sz_rfbZlibHeader)) |
---|
70 | return False; |
---|
71 | |
---|
72 | remaining = Swap32IfLE(hdr.nBytes); |
---|
73 | |
---|
74 | /* Need to initialize the decompressor state. */ |
---|
75 | decompStream.next_in = ( Bytef * )buffer; |
---|
76 | decompStream.avail_in = 0; |
---|
77 | decompStream.next_out = ( Bytef * )raw_buffer; |
---|
78 | decompStream.avail_out = raw_buffer_size; |
---|
79 | decompStream.data_type = Z_BINARY; |
---|
80 | |
---|
81 | /* Initialize the decompression stream structures on the first invocation. */ |
---|
82 | if ( decompStreamInited == False ) { |
---|
83 | |
---|
84 | inflateResult = inflateInit( &decompStream ); |
---|
85 | |
---|
86 | if ( inflateResult != Z_OK ) { |
---|
87 | aLog("inflateInit returned error: %d, msg: %s\n", |
---|
88 | inflateResult, decompStream.msg); |
---|
89 | return False; |
---|
90 | } |
---|
91 | |
---|
92 | decompStreamInited = True; |
---|
93 | |
---|
94 | } |
---|
95 | |
---|
96 | inflateResult = Z_OK; |
---|
97 | |
---|
98 | /* Process buffer full of data until no more to process, or |
---|
99 | * some type of inflater error, or Z_STREAM_END. |
---|
100 | */ |
---|
101 | while (( remaining > 0 ) && |
---|
102 | ( inflateResult == Z_OK )) { |
---|
103 | |
---|
104 | if ( remaining > BUFFER_SIZE ) { |
---|
105 | toRead = BUFFER_SIZE; |
---|
106 | } |
---|
107 | else { |
---|
108 | toRead = remaining; |
---|
109 | } |
---|
110 | |
---|
111 | /* Fill the buffer, obtaining data from the server. */ |
---|
112 | if (!ReadFromRFBServer(buffer,toRead)) |
---|
113 | return False; |
---|
114 | |
---|
115 | decompStream.next_in = ( Bytef * )buffer; |
---|
116 | decompStream.avail_in = toRead; |
---|
117 | |
---|
118 | /* Need to uncompress buffer full. */ |
---|
119 | inflateResult = inflate( &decompStream, Z_SYNC_FLUSH ); |
---|
120 | |
---|
121 | /* We never supply a dictionary for compression. */ |
---|
122 | if ( inflateResult == Z_NEED_DICT ) { |
---|
123 | aLog("zlib inflate needs a dictionary!\n"); |
---|
124 | return False; |
---|
125 | } |
---|
126 | if ( inflateResult < 0 ) { |
---|
127 | aLog("zlib inflate returned error: %d, msg: %s\n", |
---|
128 | inflateResult, decompStream.msg); |
---|
129 | return False; |
---|
130 | } |
---|
131 | |
---|
132 | /* Result buffer allocated to be at least large enough. We should |
---|
133 | * never run out of space! |
---|
134 | */ |
---|
135 | if (( decompStream.avail_in > 0 ) && |
---|
136 | ( decompStream.avail_out <= 0 )) { |
---|
137 | aLog("zlib inflate ran out of space!\n"); |
---|
138 | return False; |
---|
139 | } |
---|
140 | |
---|
141 | remaining -= toRead; |
---|
142 | |
---|
143 | } /* while ( remaining > 0 ) */ |
---|
144 | |
---|
145 | if ( inflateResult == Z_OK ) { |
---|
146 | |
---|
147 | /* Put the uncompressed contents of the update on the screen. */ |
---|
148 | ct->CopyDataToScreen(raw_buffer, rx, ry, rw, rh); |
---|
149 | |
---|
150 | } |
---|
151 | else { |
---|
152 | aLog("zlib inflate returned error: %d, msg: %s\n", |
---|
153 | inflateResult, decompStream.msg); |
---|
154 | return False; |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | return True; |
---|
159 | } |
---|
160 | |
---|