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#include "qemu/osdep.h"
28#include "vnc.h"
29
30#define ZALLOC_ALIGNMENT 16
31
32void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size)
33{
34 void *p;
35
36 size *= items;
37 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
38
39 p = g_malloc0(size);
40
41 return (p);
42}
43
44void vnc_zlib_zfree(void *x, void *addr)
45{
46 g_free(addr);
47}
48
49static void vnc_zlib_start(VncState *vs)
50{
51 buffer_reset(&vs->zlib.zlib);
52
53
54 vs->zlib.tmp = vs->output;
55 vs->output = vs->zlib.zlib;
56}
57
58static int vnc_zlib_stop(VncState *vs)
59{
60 z_streamp zstream = &vs->zlib.stream;
61 int previous_out;
62
63
64 vs->zlib.zlib = vs->output;
65 vs->output = vs->zlib.tmp;
66
67
68
69
70
71 if (zstream->opaque != vs) {
72 int err;
73
74 VNC_DEBUG("VNC: initializing zlib stream\n");
75 VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
76 zstream->zalloc = vnc_zlib_zalloc;
77 zstream->zfree = vnc_zlib_zfree;
78
79 err = deflateInit2(zstream, vs->tight->compression, Z_DEFLATED,
80 MAX_WBITS,
81 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
88 vs->zlib.level = vs->tight->compression;
89 zstream->opaque = vs;
90 }
91
92 if (vs->tight->compression != vs->zlib.level) {
93 if (deflateParams(zstream, vs->tight->compression,
94 Z_DEFAULT_STRATEGY) != Z_OK) {
95 return -1;
96 }
97 vs->zlib.level = vs->tight->compression;
98 }
99
100
101 buffer_reserve(&vs->output, vs->zlib.zlib.offset + 64);
102
103
104 zstream->next_in = vs->zlib.zlib.buffer;
105 zstream->avail_in = vs->zlib.zlib.offset;
106 zstream->next_out = vs->output.buffer + vs->output.offset;
107 zstream->avail_out = vs->output.capacity - vs->output.offset;
108 previous_out = zstream->avail_out;
109 zstream->data_type = Z_BINARY;
110
111
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;
118 return previous_out - zstream->avail_out;
119}
120
121int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
122{
123 int old_offset, new_offset, bytes_written;
124
125 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB);
126
127
128 old_offset = vs->output.offset;
129 vnc_write_s32(vs, 0);
130
131
132 vnc_zlib_start(vs);
133 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
134 bytes_written = vnc_zlib_stop(vs);
135
136 if (bytes_written == -1)
137 return 0;
138
139
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;
144
145 return 1;
146}
147
148void vnc_zlib_clear(VncState *vs)
149{
150 if (vs->zlib.stream.opaque) {
151 deflateEnd(&vs->zlib.stream);
152 }
153 buffer_free(&vs->zlib.zlib);
154}
155