1245f7b51SCorentin Chary /*
2245f7b51SCorentin Chary * QEMU VNC display driver: zlib encoding
3245f7b51SCorentin Chary *
4245f7b51SCorentin Chary * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5245f7b51SCorentin Chary * Copyright (C) 2006 Fabrice Bellard
6245f7b51SCorentin Chary * Copyright (C) 2009 Red Hat, Inc
7245f7b51SCorentin Chary *
8245f7b51SCorentin Chary * Permission is hereby granted, free of charge, to any person obtaining a copy
9245f7b51SCorentin Chary * of this software and associated documentation files (the "Software"), to deal
10245f7b51SCorentin Chary * in the Software without restriction, including without limitation the rights
11245f7b51SCorentin Chary * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12245f7b51SCorentin Chary * copies of the Software, and to permit persons to whom the Software is
13245f7b51SCorentin Chary * furnished to do so, subject to the following conditions:
14245f7b51SCorentin Chary *
15245f7b51SCorentin Chary * The above copyright notice and this permission notice shall be included in
16245f7b51SCorentin Chary * all copies or substantial portions of the Software.
17245f7b51SCorentin Chary *
18245f7b51SCorentin Chary * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19245f7b51SCorentin Chary * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20245f7b51SCorentin Chary * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21245f7b51SCorentin Chary * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22245f7b51SCorentin Chary * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23245f7b51SCorentin Chary * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24245f7b51SCorentin Chary * THE SOFTWARE.
25245f7b51SCorentin Chary */
26245f7b51SCorentin Chary
27e16f4c87SPeter Maydell #include "qemu/osdep.h"
28245f7b51SCorentin Chary #include "vnc.h"
29245f7b51SCorentin Chary
30245f7b51SCorentin Chary #define ZALLOC_ALIGNMENT 16
31245f7b51SCorentin Chary
vnc_zlib_zalloc(void * x,unsigned items,unsigned size)32245f7b51SCorentin Chary void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size)
33245f7b51SCorentin Chary {
34245f7b51SCorentin Chary void *p;
35245f7b51SCorentin Chary
36245f7b51SCorentin Chary size *= items;
37245f7b51SCorentin Chary size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
38245f7b51SCorentin Chary
397267c094SAnthony Liguori p = g_malloc0(size);
40245f7b51SCorentin Chary
41245f7b51SCorentin Chary return (p);
42245f7b51SCorentin Chary }
43245f7b51SCorentin Chary
vnc_zlib_zfree(void * x,void * addr)44245f7b51SCorentin Chary void vnc_zlib_zfree(void *x, void *addr)
45245f7b51SCorentin Chary {
467267c094SAnthony Liguori g_free(addr);
47245f7b51SCorentin Chary }
48245f7b51SCorentin Chary
vnc_zlib_start(VncState * vs)49245f7b51SCorentin Chary static void vnc_zlib_start(VncState *vs)
50245f7b51SCorentin Chary {
51d1af0e05SCorentin Chary buffer_reset(&vs->zlib.zlib);
52245f7b51SCorentin Chary
53245f7b51SCorentin Chary // make the output buffer be the zlib buffer, so we can compress it later
54d1af0e05SCorentin Chary vs->zlib.tmp = vs->output;
55d1af0e05SCorentin Chary vs->output = vs->zlib.zlib;
56245f7b51SCorentin Chary }
57245f7b51SCorentin Chary
vnc_zlib_stop(VncState * vs)58245f7b51SCorentin Chary static int vnc_zlib_stop(VncState *vs)
59245f7b51SCorentin Chary {
60d1af0e05SCorentin Chary z_streamp zstream = &vs->zlib.stream;
61245f7b51SCorentin Chary int previous_out;
62245f7b51SCorentin Chary
63245f7b51SCorentin Chary // switch back to normal output/zlib buffers
64d1af0e05SCorentin Chary vs->zlib.zlib = vs->output;
65d1af0e05SCorentin Chary vs->output = vs->zlib.tmp;
66245f7b51SCorentin Chary
67245f7b51SCorentin Chary // compress the zlib buffer
68245f7b51SCorentin Chary
69245f7b51SCorentin Chary // initialize the stream
70245f7b51SCorentin Chary // XXX need one stream per session
71245f7b51SCorentin Chary if (zstream->opaque != vs) {
72245f7b51SCorentin Chary int err;
73245f7b51SCorentin Chary
74245f7b51SCorentin Chary VNC_DEBUG("VNC: initializing zlib stream\n");
75245f7b51SCorentin Chary VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
76245f7b51SCorentin Chary zstream->zalloc = vnc_zlib_zalloc;
77245f7b51SCorentin Chary zstream->zfree = vnc_zlib_zfree;
78245f7b51SCorentin Chary
79*6bf21f3dSLi Qiang err = deflateInit2(zstream, vs->tight->compression, Z_DEFLATED,
80*6bf21f3dSLi Qiang MAX_WBITS,
81245f7b51SCorentin Chary MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
82245f7b51SCorentin Chary
83245f7b51SCorentin Chary if (err != Z_OK) {
84245f7b51SCorentin Chary fprintf(stderr, "VNC: error initializing zlib\n");
85245f7b51SCorentin Chary return -1;
86245f7b51SCorentin Chary }
87245f7b51SCorentin Chary
88*6bf21f3dSLi Qiang vs->zlib.level = vs->tight->compression;
89245f7b51SCorentin Chary zstream->opaque = vs;
90245f7b51SCorentin Chary }
91245f7b51SCorentin Chary
92*6bf21f3dSLi Qiang if (vs->tight->compression != vs->zlib.level) {
93*6bf21f3dSLi Qiang if (deflateParams(zstream, vs->tight->compression,
94245f7b51SCorentin Chary Z_DEFAULT_STRATEGY) != Z_OK) {
95245f7b51SCorentin Chary return -1;
96245f7b51SCorentin Chary }
97*6bf21f3dSLi Qiang vs->zlib.level = vs->tight->compression;
98245f7b51SCorentin Chary }
99245f7b51SCorentin Chary
100245f7b51SCorentin Chary // reserve memory in output buffer
101d1af0e05SCorentin Chary buffer_reserve(&vs->output, vs->zlib.zlib.offset + 64);
102245f7b51SCorentin Chary
103245f7b51SCorentin Chary // set pointers
104d1af0e05SCorentin Chary zstream->next_in = vs->zlib.zlib.buffer;
105d1af0e05SCorentin Chary zstream->avail_in = vs->zlib.zlib.offset;
106245f7b51SCorentin Chary zstream->next_out = vs->output.buffer + vs->output.offset;
107245f7b51SCorentin Chary zstream->avail_out = vs->output.capacity - vs->output.offset;
1082caa9e9dSMichael Tokarev previous_out = zstream->avail_out;
109245f7b51SCorentin Chary zstream->data_type = Z_BINARY;
110245f7b51SCorentin Chary
111245f7b51SCorentin Chary // start encoding
112245f7b51SCorentin Chary if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
113245f7b51SCorentin Chary fprintf(stderr, "VNC: error during zlib compression\n");
114245f7b51SCorentin Chary return -1;
115245f7b51SCorentin Chary }
116245f7b51SCorentin Chary
117245f7b51SCorentin Chary vs->output.offset = vs->output.capacity - zstream->avail_out;
1182caa9e9dSMichael Tokarev return previous_out - zstream->avail_out;
119245f7b51SCorentin Chary }
120245f7b51SCorentin Chary
vnc_zlib_send_framebuffer_update(VncState * vs,int x,int y,int w,int h)121245f7b51SCorentin Chary int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
122245f7b51SCorentin Chary {
123245f7b51SCorentin Chary int old_offset, new_offset, bytes_written;
124245f7b51SCorentin Chary
125245f7b51SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB);
126245f7b51SCorentin Chary
127245f7b51SCorentin Chary // remember where we put in the follow-up size
128245f7b51SCorentin Chary old_offset = vs->output.offset;
129245f7b51SCorentin Chary vnc_write_s32(vs, 0);
130245f7b51SCorentin Chary
131245f7b51SCorentin Chary // compress the stream
132245f7b51SCorentin Chary vnc_zlib_start(vs);
133245f7b51SCorentin Chary vnc_raw_send_framebuffer_update(vs, x, y, w, h);
134245f7b51SCorentin Chary bytes_written = vnc_zlib_stop(vs);
135245f7b51SCorentin Chary
136245f7b51SCorentin Chary if (bytes_written == -1)
137245f7b51SCorentin Chary return 0;
138245f7b51SCorentin Chary
139245f7b51SCorentin Chary // hack in the size
140245f7b51SCorentin Chary new_offset = vs->output.offset;
141245f7b51SCorentin Chary vs->output.offset = old_offset;
142245f7b51SCorentin Chary vnc_write_u32(vs, bytes_written);
143245f7b51SCorentin Chary vs->output.offset = new_offset;
144245f7b51SCorentin Chary
145245f7b51SCorentin Chary return 1;
146245f7b51SCorentin Chary }
147245f7b51SCorentin Chary
vnc_zlib_clear(VncState * vs)148245f7b51SCorentin Chary void vnc_zlib_clear(VncState *vs)
149245f7b51SCorentin Chary {
150d1af0e05SCorentin Chary if (vs->zlib.stream.opaque) {
151d1af0e05SCorentin Chary deflateEnd(&vs->zlib.stream);
152245f7b51SCorentin Chary }
153d1af0e05SCorentin Chary buffer_free(&vs->zlib.zlib);
154245f7b51SCorentin Chary }
155