2010-05-03 16:31:34 +04:00
|
|
|
/*
|
|
|
|
* QEMU VNC display driver: zlib encoding
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
|
|
|
|
* Copyright (C) 2006 Fabrice Bellard
|
|
|
|
* Copyright (C) 2009 Red Hat, Inc
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2010-05-03 16:31:34 +04:00
|
|
|
#include "vnc.h"
|
|
|
|
|
|
|
|
#define ZALLOC_ALIGNMENT 16
|
|
|
|
|
2010-05-19 11:24:10 +04:00
|
|
|
void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size)
|
2010-05-03 16:31:34 +04:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
size *= items;
|
|
|
|
size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
p = g_malloc0(size);
|
2010-05-03 16:31:34 +04:00
|
|
|
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2010-05-19 11:24:10 +04:00
|
|
|
void vnc_zlib_zfree(void *x, void *addr)
|
2010-05-03 16:31:34 +04:00
|
|
|
{
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(addr);
|
2010-05-03 16:31:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vnc_zlib_start(VncState *vs)
|
|
|
|
{
|
2010-07-07 22:57:59 +04:00
|
|
|
buffer_reset(&vs->zlib.zlib);
|
2010-05-03 16:31:34 +04:00
|
|
|
|
|
|
|
// make the output buffer be the zlib buffer, so we can compress it later
|
2010-07-07 22:57:59 +04:00
|
|
|
vs->zlib.tmp = vs->output;
|
|
|
|
vs->output = vs->zlib.zlib;
|
2010-05-03 16:31:34 +04:00
|
|
|
}
|
|
|
|
|
2010-05-19 11:24:04 +04:00
|
|
|
static int vnc_zlib_stop(VncState *vs)
|
2010-05-03 16:31:34 +04:00
|
|
|
{
|
2010-07-07 22:57:59 +04:00
|
|
|
z_streamp zstream = &vs->zlib.stream;
|
2010-05-03 16:31:34 +04:00
|
|
|
int previous_out;
|
|
|
|
|
|
|
|
// switch back to normal output/zlib buffers
|
2010-07-07 22:57:59 +04:00
|
|
|
vs->zlib.zlib = vs->output;
|
|
|
|
vs->output = vs->zlib.tmp;
|
2010-05-03 16:31:34 +04:00
|
|
|
|
|
|
|
// compress the zlib buffer
|
|
|
|
|
|
|
|
// initialize the stream
|
|
|
|
// XXX need one stream per session
|
|
|
|
if (zstream->opaque != vs) {
|
|
|
|
int err;
|
|
|
|
|
2010-05-19 11:24:04 +04:00
|
|
|
VNC_DEBUG("VNC: initializing zlib stream\n");
|
2010-05-03 16:31:34 +04:00
|
|
|
VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
|
2010-05-19 11:24:10 +04:00
|
|
|
zstream->zalloc = vnc_zlib_zalloc;
|
|
|
|
zstream->zfree = vnc_zlib_zfree;
|
2010-05-03 16:31:34 +04:00
|
|
|
|
2010-07-07 22:57:59 +04:00
|
|
|
err = deflateInit2(zstream, vs->tight.compression, Z_DEFLATED, MAX_WBITS,
|
2010-05-03 16:31:34 +04:00
|
|
|
MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
|
|
|
|
|
|
|
|
if (err != Z_OK) {
|
|
|
|
fprintf(stderr, "VNC: error initializing zlib\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-07 22:57:59 +04:00
|
|
|
vs->zlib.level = vs->tight.compression;
|
2010-05-03 16:31:34 +04:00
|
|
|
zstream->opaque = vs;
|
|
|
|
}
|
|
|
|
|
2010-07-07 22:57:59 +04:00
|
|
|
if (vs->tight.compression != vs->zlib.level) {
|
|
|
|
if (deflateParams(zstream, vs->tight.compression,
|
2010-05-19 11:24:05 +04:00
|
|
|
Z_DEFAULT_STRATEGY) != Z_OK) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-07 22:57:59 +04:00
|
|
|
vs->zlib.level = vs->tight.compression;
|
2010-05-19 11:24:05 +04:00
|
|
|
}
|
2010-05-03 16:31:34 +04:00
|
|
|
|
|
|
|
// reserve memory in output buffer
|
2010-07-07 22:57:59 +04:00
|
|
|
buffer_reserve(&vs->output, vs->zlib.zlib.offset + 64);
|
2010-05-03 16:31:34 +04:00
|
|
|
|
|
|
|
// set pointers
|
2010-07-07 22:57:59 +04:00
|
|
|
zstream->next_in = vs->zlib.zlib.buffer;
|
|
|
|
zstream->avail_in = vs->zlib.zlib.offset;
|
2010-05-03 16:31:34 +04:00
|
|
|
zstream->next_out = vs->output.buffer + vs->output.offset;
|
|
|
|
zstream->avail_out = vs->output.capacity - vs->output.offset;
|
2011-03-21 11:34:35 +03:00
|
|
|
previous_out = zstream->avail_out;
|
2010-05-03 16:31:34 +04:00
|
|
|
zstream->data_type = Z_BINARY;
|
|
|
|
|
|
|
|
// start encoding
|
|
|
|
if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
|
|
|
|
fprintf(stderr, "VNC: error during zlib compression\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vs->output.offset = vs->output.capacity - zstream->avail_out;
|
2011-03-21 11:34:35 +03:00
|
|
|
return previous_out - zstream->avail_out;
|
2010-05-03 16:31:34 +04:00
|
|
|
}
|
|
|
|
|
2010-05-19 11:24:09 +04:00
|
|
|
int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
|
2010-05-03 16:31:34 +04:00
|
|
|
{
|
|
|
|
int old_offset, new_offset, bytes_written;
|
|
|
|
|
|
|
|
vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB);
|
|
|
|
|
|
|
|
// remember where we put in the follow-up size
|
|
|
|
old_offset = vs->output.offset;
|
|
|
|
vnc_write_s32(vs, 0);
|
|
|
|
|
|
|
|
// compress the stream
|
|
|
|
vnc_zlib_start(vs);
|
|
|
|
vnc_raw_send_framebuffer_update(vs, x, y, w, h);
|
2010-05-19 11:24:04 +04:00
|
|
|
bytes_written = vnc_zlib_stop(vs);
|
2010-05-03 16:31:34 +04:00
|
|
|
|
|
|
|
if (bytes_written == -1)
|
2010-05-19 11:24:09 +04:00
|
|
|
return 0;
|
2010-05-03 16:31:34 +04:00
|
|
|
|
|
|
|
// hack in the size
|
|
|
|
new_offset = vs->output.offset;
|
|
|
|
vs->output.offset = old_offset;
|
|
|
|
vnc_write_u32(vs, bytes_written);
|
|
|
|
vs->output.offset = new_offset;
|
2010-05-19 11:24:09 +04:00
|
|
|
|
|
|
|
return 1;
|
2010-05-03 16:31:34 +04:00
|
|
|
}
|
2010-05-19 11:24:08 +04:00
|
|
|
|
|
|
|
void vnc_zlib_clear(VncState *vs)
|
|
|
|
{
|
2010-07-07 22:57:59 +04:00
|
|
|
if (vs->zlib.stream.opaque) {
|
|
|
|
deflateEnd(&vs->zlib.stream);
|
2010-05-19 11:24:08 +04:00
|
|
|
}
|
2010-07-07 22:57:59 +04:00
|
|
|
buffer_free(&vs->zlib.zlib);
|
2010-05-19 11:24:08 +04:00
|
|
|
}
|