static int vnc_zlib_stop(VncState *vs)

{

    z_streamp zstream = &vs->zlib_stream;

    int previous_out;



    // switch back to normal output/zlib buffers

    vs->zlib = vs->output;

    vs->output = vs->zlib_tmp;



    // compress the zlib buffer



    // initialize the stream

    // XXX need one stream per session

    if (zstream->opaque != vs) {

        int err;



        VNC_DEBUG("VNC: initializing zlib stream\n");

        VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);

        zstream->zalloc = vnc_zlib_zalloc;

        zstream->zfree = vnc_zlib_zfree;



        err = deflateInit2(zstream, vs->tight_compression, Z_DEFLATED, MAX_WBITS,

                           MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);



        if (err != Z_OK) {

            fprintf(stderr, "VNC: error initializing zlib\n");

            return -1;

        }



        vs->zlib_level = vs->tight_compression;

        zstream->opaque = vs;

    }



    if (vs->tight_compression != vs->zlib_level) {

        if (deflateParams(zstream, vs->tight_compression,

                          Z_DEFAULT_STRATEGY) != Z_OK) {

            return -1;

        }

        vs->zlib_level = vs->tight_compression;

    }



    // reserve memory in output buffer

    buffer_reserve(&vs->output, vs->zlib.offset + 64);



    // set pointers

    zstream->next_in = vs->zlib.buffer;

    zstream->avail_in = vs->zlib.offset;

    zstream->next_out = vs->output.buffer + vs->output.offset;

    zstream->avail_out = vs->output.capacity - vs->output.offset;

    zstream->data_type = Z_BINARY;

    previous_out = zstream->total_out;



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

    return zstream->total_out - previous_out;

}

static int vnc_zlib_stop(VncState *vs)

{

    z_streamp zstream = &vs->zlib_stream;

    int previous_out;



    

    vs->zlib = vs->output;

    vs->output = vs->zlib_tmp;



    



    

    

    if (zstream->opaque != vs) {

        int err;



        VNC_DEBUG("VNC: initializing zlib stream\n");

        VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);

        zstream->zalloc = vnc_zlib_zalloc;

        zstream->zfree = vnc_zlib_zfree;



        err = deflateInit2(zstream, vs->tight_compression, Z_DEFLATED, MAX_WBITS,

                           MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);



        if (err != Z_OK) {

            fprintf(stderr, "VNC: error initializing zlib\n");

            return -1;

        }



        vs->zlib_level = vs->tight_compression;

        zstream->opaque = vs;

    }



    if (vs->tight_compression != vs->zlib_level) {

        if (deflateParams(zstream, vs->tight_compression,

                          Z_DEFAULT_STRATEGY) != Z_OK) {

            return -1;

        }

        vs->zlib_level = vs->tight_compression;

    }



    

    buffer_reserve(&vs->output, vs->zlib.offset + 64);



    

    zstream->next_in = vs->zlib.buffer;

    zstream->avail_in = vs->zlib.offset;

    zstream->next_out = vs->output.buffer + vs->output.offset;

    zstream->avail_out = vs->output.capacity - vs->output.offset;

    zstream->data_type = Z_BINARY;

    previous_out = zstream->total_out;



    

    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;

    return zstream->total_out - previous_out;

}

static int FUNC_0(VncState *VAR_0)

{

    z_streamp zstream = &VAR_0->zlib_stream;

    int VAR_1;



    

    VAR_0->zlib = VAR_0->output;

    VAR_0->output = VAR_0->zlib_tmp;



    



    

    

    if (zstream->opaque != VAR_0) {

        int VAR_2;



        VNC_DEBUG("VNC: initializing zlib stream\n");

        VNC_DEBUG("VNC: opaque = %p | VAR_0 = %p\n", zstream->opaque, VAR_0);

        zstream->zalloc = vnc_zlib_zalloc;

        zstream->zfree = vnc_zlib_zfree;



        VAR_2 = deflateInit2(zstream, VAR_0->tight_compression, Z_DEFLATED, MAX_WBITS,

                           MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);



        if (VAR_2 != Z_OK) {

            fprintf(stderr, "VNC: error initializing zlib\n");

            return -1;

        }



        VAR_0->zlib_level = VAR_0->tight_compression;

        zstream->opaque = VAR_0;

    }



    if (VAR_0->tight_compression != VAR_0->zlib_level) {

        if (deflateParams(zstream, VAR_0->tight_compression,

                          Z_DEFAULT_STRATEGY) != Z_OK) {

            return -1;

        }

        VAR_0->zlib_level = VAR_0->tight_compression;

    }



    

    buffer_reserve(&VAR_0->output, VAR_0->zlib.offset + 64);



    

    zstream->next_in = VAR_0->zlib.buffer;

    zstream->avail_in = VAR_0->zlib.offset;

    zstream->next_out = VAR_0->output.buffer + VAR_0->output.offset;

    zstream->avail_out = VAR_0->output.capacity - VAR_0->output.offset;

    zstream->data_type = Z_BINARY;

    VAR_1 = zstream->total_out;



    

    if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {

        fprintf(stderr, "VNC: error during zlib compression\n");

        return -1;

    }



    VAR_0->output.offset = VAR_0->output.capacity - zstream->avail_out;

    return zstream->total_out - VAR_1;

}

static int FUNC_0(VncState *VAR_0)
{ z_streamp zstream = &VAR_0->zlib_stream; int VAR_1; VAR_0->zlib = VAR_0->output; VAR_0->output = VAR_0->zlib_tmp; if (zstream->opaque != VAR_0) { int VAR_2; VNC_DEBUG("VNC: initializing zlib stream\n"); VNC_DEBUG("VNC: opaque = %p | VAR_0 = %p\n", zstream->opaque, VAR_0); zstream->zalloc = vnc_zlib_zalloc; zstream->zfree = vnc_zlib_zfree; VAR_2 = deflateInit2(zstream, VAR_0->tight_compression, Z_DEFLATED, MAX_WBITS,
MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); if (VAR_2 != Z_OK) { fprintf(stderr, "VNC: error initializing zlib\n"); return -1; } VAR_0->zlib_level = VAR_0->tight_compression; zstream->opaque = VAR_0; } if (VAR_0->tight_compression != VAR_0->zlib_level) { if (deflateParams(zstream, VAR_0->tight_compression,
Z_DEFAULT_STRATEGY) != Z_OK) { return -1; } VAR_0->zlib_level = VAR_0->tight_compression; } buffer_reserve(&VAR_0->output, VAR_0->zlib.offset + 64); zstream->next_in = VAR_0->zlib.buffer; zstream->avail_in = VAR_0->zlib.offset; zstream->next_out = VAR_0->output.buffer + VAR_0->output.offset; zstream->avail_out = VAR_0->output.capacity - VAR_0->output.offset; zstream->data_type = Z_BINARY; VAR_1 = zstream->total_out; if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) { fprintf(stderr, "VNC: error during zlib compression\n"); return -1; } VAR_0->output.offset = VAR_0->output.capacity - zstream->avail_out; return zstream->total_out - VAR_1; }