vnc-enc-zlib.c 4.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * 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.
 */

#include "vnc.h"

#define ZALLOC_ALIGNMENT 16

31
void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size)
32 33 34 35 36 37
{
    void *p;

    size *= items;
    size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);

38
    p = g_malloc0(size);
39 40 41 42

    return (p);
}

43
void vnc_zlib_zfree(void *x, void *addr)
44
{
45
    g_free(addr);
46 47 48 49
}

static void vnc_zlib_start(VncState *vs)
{
50
    buffer_reset(&vs->zlib.zlib);
51 52

    // make the output buffer be the zlib buffer, so we can compress it later
53 54
    vs->zlib.tmp = vs->output;
    vs->output = vs->zlib.zlib;
55 56
}

57
static int vnc_zlib_stop(VncState *vs)
58
{
59
    z_streamp zstream = &vs->zlib.stream;
60 61 62
    int previous_out;

    // switch back to normal output/zlib buffers
63 64
    vs->zlib.zlib = vs->output;
    vs->output = vs->zlib.tmp;
65 66 67 68 69 70 71 72

    // compress the zlib buffer

    // initialize the stream
    // XXX need one stream per session
    if (zstream->opaque != vs) {
        int err;

73
        VNC_DEBUG("VNC: initializing zlib stream\n");
74
        VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
75 76
        zstream->zalloc = vnc_zlib_zalloc;
        zstream->zfree = vnc_zlib_zfree;
77

78
        err = deflateInit2(zstream, vs->tight.compression, Z_DEFLATED, MAX_WBITS,
79 80 81 82 83 84 85
                           MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);

        if (err != Z_OK) {
            fprintf(stderr, "VNC: error initializing zlib\n");
            return -1;
        }

86
        vs->zlib.level = vs->tight.compression;
87 88 89
        zstream->opaque = vs;
    }

90 91
    if (vs->tight.compression != vs->zlib.level) {
        if (deflateParams(zstream, vs->tight.compression,
92 93 94
                          Z_DEFAULT_STRATEGY) != Z_OK) {
            return -1;
        }
95
        vs->zlib.level = vs->tight.compression;
96
    }
97 98

    // reserve memory in output buffer
99
    buffer_reserve(&vs->output, vs->zlib.zlib.offset + 64);
100 101

    // set pointers
102 103
    zstream->next_in = vs->zlib.zlib.buffer;
    zstream->avail_in = vs->zlib.zlib.offset;
104 105
    zstream->next_out = vs->output.buffer + vs->output.offset;
    zstream->avail_out = vs->output.capacity - vs->output.offset;
106
    previous_out = zstream->avail_out;
107 108 109 110 111 112 113 114 115
    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;
116
    return previous_out - zstream->avail_out;
117 118
}

119
int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
120 121 122 123 124 125 126 127 128 129 130 131
{
    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);
132
    bytes_written = vnc_zlib_stop(vs);
133 134

    if (bytes_written == -1)
135
        return 0;
136 137 138 139 140 141

    // 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;
142 143

    return 1;
144
}
145 146 147

void vnc_zlib_clear(VncState *vs)
{
148 149
    if (vs->zlib.stream.opaque) {
        deflateEnd(&vs->zlib.stream);
150
    }
151
    buffer_free(&vs->zlib.zlib);
152
}