2 * Copyright (C) 2000 Tridia Corporation. All Rights Reserved.
3 * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 #ifdef LIBVNCSERVER_HAVE_LIBZ
24 * zlib.c - handle zlib encoding.
26 * This file shouldn't be compiled directly. It is included multiple times by
27 * rfbproto.c, each time with a different definition of the macro BPP. For
28 * each value of BPP, this file defines a function which handles an zlib
29 * encoded rectangle with BPP bits per pixel.
32 #define HandleZlibBPP CONCAT2E(HandleZlib,BPP)
33 #define CARDBPP CONCAT3E(uint,BPP,_t)
36 HandleZlibBPP (rfbClient* client, int rx, int ry, int rw, int rh)
43 /* First make sure we have a large enough raw buffer to hold the
44 * decompressed data. In practice, with a fixed BPP, fixed frame
45 * buffer size and the first update containing the entire frame
46 * buffer, this buffer allocation should only happen once, on the
49 if ( client->raw_buffer_size < (( rw * rh ) * ( BPP / 8 ))) {
51 if ( client->raw_buffer != NULL ) {
53 free( client->raw_buffer );
57 client->raw_buffer_size = (( rw * rh ) * ( BPP / 8 ));
58 client->raw_buffer = (char*) malloc( client->raw_buffer_size );
62 if (!ReadFromRFBServer(client, (char *)&hdr, sz_rfbZlibHeader))
65 remaining = rfbClientSwap32IfLE(hdr.nBytes);
67 /* Need to initialize the decompressor state. */
68 client->decompStream.next_in = ( Bytef * )client->buffer;
69 client->decompStream.avail_in = 0;
70 client->decompStream.next_out = ( Bytef * )client->raw_buffer;
71 client->decompStream.avail_out = client->raw_buffer_size;
72 client->decompStream.data_type = Z_BINARY;
74 /* Initialize the decompression stream structures on the first invocation. */
75 if ( client->decompStreamInited == FALSE ) {
77 inflateResult = inflateInit( &client->decompStream );
79 if ( inflateResult != Z_OK ) {
81 "inflateInit returned error: %d, msg: %s\n",
83 client->decompStream.msg);
87 client->decompStreamInited = TRUE;
93 /* Process buffer full of data until no more to process, or
94 * some type of inflater error, or Z_STREAM_END.
96 while (( remaining > 0 ) &&
97 ( inflateResult == Z_OK )) {
99 if ( remaining > RFB_BUFFER_SIZE ) {
100 toRead = RFB_BUFFER_SIZE;
106 /* Fill the buffer, obtaining data from the server. */
107 if (!ReadFromRFBServer(client, client->buffer,toRead))
110 client->decompStream.next_in = ( Bytef * )client->buffer;
111 client->decompStream.avail_in = toRead;
113 /* Need to uncompress buffer full. */
114 inflateResult = inflate( &client->decompStream, Z_SYNC_FLUSH );
116 /* We never supply a dictionary for compression. */
117 if ( inflateResult == Z_NEED_DICT ) {
118 rfbClientLog("zlib inflate needs a dictionary!\n");
121 if ( inflateResult < 0 ) {
123 "zlib inflate returned error: %d, msg: %s\n",
125 client->decompStream.msg);
129 /* Result buffer allocated to be at least large enough. We should
130 * never run out of space!
132 if (( client->decompStream.avail_in > 0 ) &&
133 ( client->decompStream.avail_out <= 0 )) {
134 rfbClientLog("zlib inflate ran out of space!\n");
140 } /* while ( remaining > 0 ) */
142 if ( inflateResult == Z_OK ) {
144 /* Put the uncompressed contents of the update on the screen. */
145 CopyRectangle(client, (uint8_t *)client->raw_buffer, rx, ry, rw, rh);
150 "zlib inflate returned error: %d, msg: %s\n",
152 client->decompStream.msg);