static int vnc_zlib_stop(VncState *vs, int stream_id)

{

    z_streamp zstream = &vs->zlib_stream[stream_id];

    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 %d\n", stream_id);

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

        zstream->zalloc = Z_NULL;

        zstream->zfree = Z_NULL;



        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;

        }



        zstream->opaque = vs;

    }



    // XXX what to do if tight_compression changed in between?



    // 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, int stream_id)

{

    z_streamp zstream = &vs->zlib_stream[stream_id];

    int previous_out;



    

    vs->zlib = vs->output;

    vs->output = vs->zlib_tmp;



    



    

    

    if (zstream->opaque != vs) {

        int err;



        VNC_DEBUG("VNC: initializing zlib stream %d\n", stream_id);

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

        zstream->zalloc = Z_NULL;

        zstream->zfree = Z_NULL;



        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;

        }



        zstream->opaque = vs;

    }



    



    

    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;

}

        zstream->zalloc = Z_NULL;         zstream->zfree = Z_NULL;
static int FUNC_0(VncState *VAR_0, int VAR_1)

{

    z_streamp zstream = &VAR_0->zlib_stream[VAR_1];

    int VAR_2;



    

    VAR_0->zlib = VAR_0->output;

    VAR_0->output = VAR_0->zlib_tmp;



    



    

    

    if (zstream->opaque != VAR_0) {

        int VAR_3;



        VNC_DEBUG("VNC: initializing zlib stream %d\n", VAR_1);

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

        zstream->zalloc = Z_NULL;

        zstream->zfree = Z_NULL;



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

                           MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);



        if (VAR_3 != Z_OK) {

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

            return -1;

        }



        zstream->opaque = VAR_0;

    }



    



    

    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_2 = 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_2;

}

static int FUNC_0(VncState *VAR_0, int VAR_1)
{ z_streamp zstream = &VAR_0->zlib_stream[VAR_1]; int VAR_2; VAR_0->zlib = VAR_0->output; VAR_0->output = VAR_0->zlib_tmp; if (zstream->opaque != VAR_0) { int VAR_3; VNC_DEBUG("VNC: initializing zlib stream %d\n", VAR_1); VNC_DEBUG("VNC: opaque = %p | VAR_0 = %p\n", zstream->opaque, VAR_0); zstream->zalloc = Z_NULL; zstream->zfree = Z_NULL; VAR_3 = deflateInit2(zstream, VAR_0->tight_compression, Z_DEFLATED, MAX_WBITS,
MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); if (VAR_3 != Z_OK) { fprintf(stderr, "VNC: error initializing zlib\n"); return -1; } zstream->opaque = VAR_0; } 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_2 = 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_2; }