blob: 900ae5b30f6bd2ddbcd797d212c010c48d451094 [file] [log] [blame]
Corentin Chary70a45682010-05-03 14:31:34 +02001/*
2 * QEMU VNC display driver: zlib encoding
3 *
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
6 * Copyright (C) 2009 Red Hat, Inc
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Peter Maydelle16f4c82016-01-29 17:49:51 +000027#include "qemu/osdep.h"
Corentin Chary70a45682010-05-03 14:31:34 +020028#include "vnc.h"
29
30#define ZALLOC_ALIGNMENT 16
31
Corentin Chary380282b2010-05-19 09:24:10 +020032void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size)
Corentin Chary70a45682010-05-03 14:31:34 +020033{
34 void *p;
35
36 size *= items;
37 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
38
Anthony Liguori7267c092011-08-20 22:09:37 -050039 p = g_malloc0(size);
Corentin Chary70a45682010-05-03 14:31:34 +020040
41 return (p);
42}
43
Corentin Chary380282b2010-05-19 09:24:10 +020044void vnc_zlib_zfree(void *x, void *addr)
Corentin Chary70a45682010-05-03 14:31:34 +020045{
Anthony Liguori7267c092011-08-20 22:09:37 -050046 g_free(addr);
Corentin Chary70a45682010-05-03 14:31:34 +020047}
48
49static void vnc_zlib_start(VncState *vs)
50{
Corentin Charyd1af0e02010-07-07 20:57:59 +020051 buffer_reset(&vs->zlib.zlib);
Corentin Chary70a45682010-05-03 14:31:34 +020052
53 // make the output buffer be the zlib buffer, so we can compress it later
Corentin Charyd1af0e02010-07-07 20:57:59 +020054 vs->zlib.tmp = vs->output;
55 vs->output = vs->zlib.zlib;
Corentin Chary70a45682010-05-03 14:31:34 +020056}
57
Corentin Charyb05ad292010-05-19 09:24:04 +020058static int vnc_zlib_stop(VncState *vs)
Corentin Chary70a45682010-05-03 14:31:34 +020059{
Corentin Charyd1af0e02010-07-07 20:57:59 +020060 z_streamp zstream = &vs->zlib.stream;
Corentin Chary70a45682010-05-03 14:31:34 +020061 int previous_out;
62
63 // switch back to normal output/zlib buffers
Corentin Charyd1af0e02010-07-07 20:57:59 +020064 vs->zlib.zlib = vs->output;
65 vs->output = vs->zlib.tmp;
Corentin Chary70a45682010-05-03 14:31:34 +020066
67 // compress the zlib buffer
68
69 // initialize the stream
70 // XXX need one stream per session
71 if (zstream->opaque != vs) {
72 int err;
73
Corentin Charyb05ad292010-05-19 09:24:04 +020074 VNC_DEBUG("VNC: initializing zlib stream\n");
Corentin Chary70a45682010-05-03 14:31:34 +020075 VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
Corentin Chary380282b2010-05-19 09:24:10 +020076 zstream->zalloc = vnc_zlib_zalloc;
77 zstream->zfree = vnc_zlib_zfree;
Corentin Chary70a45682010-05-03 14:31:34 +020078
Li Qiang6bf21f32019-08-31 08:39:22 -070079 err = deflateInit2(zstream, vs->tight->compression, Z_DEFLATED,
80 MAX_WBITS,
Corentin Chary70a45682010-05-03 14:31:34 +020081 MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
82
83 if (err != Z_OK) {
84 fprintf(stderr, "VNC: error initializing zlib\n");
85 return -1;
86 }
87
Li Qiang6bf21f32019-08-31 08:39:22 -070088 vs->zlib.level = vs->tight->compression;
Corentin Chary70a45682010-05-03 14:31:34 +020089 zstream->opaque = vs;
90 }
91
Li Qiang6bf21f32019-08-31 08:39:22 -070092 if (vs->tight->compression != vs->zlib.level) {
93 if (deflateParams(zstream, vs->tight->compression,
Corentin Chary9f643ec2010-05-19 09:24:05 +020094 Z_DEFAULT_STRATEGY) != Z_OK) {
95 return -1;
96 }
Li Qiang6bf21f32019-08-31 08:39:22 -070097 vs->zlib.level = vs->tight->compression;
Corentin Chary9f643ec2010-05-19 09:24:05 +020098 }
Corentin Chary70a45682010-05-03 14:31:34 +020099
100 // reserve memory in output buffer
Corentin Charyd1af0e02010-07-07 20:57:59 +0200101 buffer_reserve(&vs->output, vs->zlib.zlib.offset + 64);
Corentin Chary70a45682010-05-03 14:31:34 +0200102
103 // set pointers
Corentin Charyd1af0e02010-07-07 20:57:59 +0200104 zstream->next_in = vs->zlib.zlib.buffer;
105 zstream->avail_in = vs->zlib.zlib.offset;
Corentin Chary70a45682010-05-03 14:31:34 +0200106 zstream->next_out = vs->output.buffer + vs->output.offset;
107 zstream->avail_out = vs->output.capacity - vs->output.offset;
Michael Tokarev2caa9e92011-03-21 09:34:35 +0100108 previous_out = zstream->avail_out;
Corentin Chary70a45682010-05-03 14:31:34 +0200109 zstream->data_type = Z_BINARY;
Corentin Chary70a45682010-05-03 14:31:34 +0200110
111 // start encoding
112 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
113 fprintf(stderr, "VNC: error during zlib compression\n");
114 return -1;
115 }
116
117 vs->output.offset = vs->output.capacity - zstream->avail_out;
Michael Tokarev2caa9e92011-03-21 09:34:35 +0100118 return previous_out - zstream->avail_out;
Corentin Chary70a45682010-05-03 14:31:34 +0200119}
120
Corentin Charya8852112010-05-19 09:24:09 +0200121int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
Corentin Chary70a45682010-05-03 14:31:34 +0200122{
123 int old_offset, new_offset, bytes_written;
124
125 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB);
126
127 // remember where we put in the follow-up size
128 old_offset = vs->output.offset;
129 vnc_write_s32(vs, 0);
130
131 // compress the stream
132 vnc_zlib_start(vs);
133 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
Corentin Charyb05ad292010-05-19 09:24:04 +0200134 bytes_written = vnc_zlib_stop(vs);
Corentin Chary70a45682010-05-03 14:31:34 +0200135
136 if (bytes_written == -1)
Corentin Charya8852112010-05-19 09:24:09 +0200137 return 0;
Corentin Chary70a45682010-05-03 14:31:34 +0200138
139 // hack in the size
140 new_offset = vs->output.offset;
141 vs->output.offset = old_offset;
142 vnc_write_u32(vs, bytes_written);
143 vs->output.offset = new_offset;
Corentin Charya8852112010-05-19 09:24:09 +0200144
145 return 1;
Corentin Chary70a45682010-05-03 14:31:34 +0200146}
Corentin Chary161c4f22010-05-19 09:24:08 +0200147
148void vnc_zlib_clear(VncState *vs)
149{
Corentin Charyd1af0e02010-07-07 20:57:59 +0200150 if (vs->zlib.stream.opaque) {
151 deflateEnd(&vs->zlib.stream);
Corentin Chary161c4f22010-05-19 09:24:08 +0200152 }
Corentin Charyd1af0e02010-07-07 20:57:59 +0200153 buffer_free(&vs->zlib.zlib);
Corentin Chary161c4f22010-05-19 09:24:08 +0200154}