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