this fixes two problems:
[rsync/rsync.git] / token.c
CommitLineData
70d794dc
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include "rsync.h"
861c20b4 21#include "lib/zlib.h"
70d794dc
AT
22
23extern int do_compression;
24
25
26/* non-compressing recv token */
27static int simple_recv_token(int f,char **data)
28{
c5eb3650
AT
29 static int residue;
30 static char *buf;
31 int n;
70d794dc 32
c5eb3650
AT
33 if (!buf) {
34 buf = (char *)malloc(CHUNK_SIZE);
35 if (!buf) out_of_memory("simple_recv_token");
36 }
70d794dc 37
c5eb3650
AT
38 if (residue == 0) {
39 int i = read_int(f);
40 if (i <= 0) return i;
41 residue = i;
42 }
70d794dc 43
c5eb3650
AT
44 *data = buf;
45 n = MIN(CHUNK_SIZE,residue);
46 residue -= n;
47 read_buf(f,buf,n);
48 return n;
70d794dc
AT
49}
50
51
52/* non-compressing send token */
c6e7fcb4 53static void simple_send_token(int f,int token,
45f133b9 54 struct map_struct *buf,OFF_T offset,int n)
70d794dc 55{
c5eb3650
AT
56 if (n > 0) {
57 int l = 0;
58 while (l < n) {
59 int n1 = MIN(CHUNK_SIZE,n-l);
60 write_int(f,n1);
61 write_buf(f,map_ptr(buf,offset+l,n1),n1);
62 l += n1;
63 }
64 }
45f133b9
AT
65 /* a -2 token means to send data only and no token */
66 if (token != -2) {
67 write_int(f,-(token+1));
68 }
70d794dc
AT
69}
70
71
861c20b4
PM
72/* Memory allocation/freeing routines, called by zlib stuff. */
73static void *
74z_alloc(void *opaque, uInt items, uInt size)
75{
76 return malloc(items * size);
77}
78
79static void
80z_free(void *opaque, void *adrs, uInt nbytes)
81{
82 free(adrs);
83}
84
85/* Flag bytes in compressed stream are encoded as follows: */
86#define END_FLAG 0 /* that's all folks */
87#define TOKEN_LONG 0x20 /* followed by 32-bit token number */
88#define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
89#define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
90#define TOKEN_REL 0x80 /* + 6-bit relative token number */
91#define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
92
93#define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
94
95/* For coding runs of tokens */
96static int last_token = -1;
97static int run_start;
98static int last_run_end;
99
100/* Deflation state */
101static z_stream tx_strm;
102
103/* Output buffer */
3a6a366f 104static char *obuf;
861c20b4
PM
105
106/* Send a deflated token */
107static void
108send_deflated_token(int f, int token,
45f133b9 109 struct map_struct *buf, OFF_T offset, int nb, int toklen)
861c20b4
PM
110{
111 int n, r;
112 static int init_done;
113
114 if (last_token == -1) {
115 /* initialization */
116 if (!init_done) {
117 tx_strm.next_in = NULL;
118 tx_strm.zalloc = z_alloc;
119 tx_strm.zfree = z_free;
120 if (deflateInit2(&tx_strm, Z_DEFAULT_COMPRESSION, 8,
f8062104 121 -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
9486289c 122 rprintf(FERROR, "compression init failed\n");
861c20b4
PM
123 exit_cleanup(1);
124 }
125 if ((obuf = malloc(MAX_DATA_COUNT+2)) == NULL)
126 out_of_memory("send_deflated_token");
127 init_done = 1;
128 } else
129 deflateReset(&tx_strm);
130 run_start = token;
131 last_run_end = 0;
132
133 } else if (nb != 0 || token != last_token + 1
134 || token >= run_start + 65536) {
135 /* output previous run */
136 r = run_start - last_run_end;
137 n = last_token - run_start;
138 if (r >= 0 && r <= 63) {
139 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
140 } else {
141 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
142 write_int(f, run_start);
143 }
144 if (n != 0) {
145 write_byte(f, n);
146 write_byte(f, n >> 8);
147 }
148 last_run_end = last_token;
149 run_start = token;
150 }
151
152 last_token = token;
153
154 if (nb != 0) {
155 /* deflate the data starting at offset */
156 tx_strm.avail_in = 0;
157 tx_strm.avail_out = 0;
158 do {
159 if (tx_strm.avail_in == 0 && nb != 0) {
160 /* give it some more input */
161 n = MIN(nb, CHUNK_SIZE);
1634f4c4 162 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n);
861c20b4
PM
163 tx_strm.avail_in = n;
164 nb -= n;
165 offset += n;
166 }
167 if (tx_strm.avail_out == 0) {
1634f4c4 168 tx_strm.next_out = (Bytef *)(obuf + 2);
861c20b4
PM
169 tx_strm.avail_out = MAX_DATA_COUNT;
170 }
171 r = deflate(&tx_strm, nb? Z_NO_FLUSH: Z_PACKET_FLUSH);
172 if (r != Z_OK) {
9486289c 173 rprintf(FERROR, "deflate returned %d\n", r);
861c20b4
PM
174 exit_cleanup(1);
175 }
176 if (nb == 0 || tx_strm.avail_out == 0) {
177 n = MAX_DATA_COUNT - tx_strm.avail_out;
178 if (n > 0) {
179 obuf[0] = DEFLATED_DATA + (n >> 8);
180 obuf[1] = n;
181 write_buf(f, obuf, n+2);
182 }
183 }
184 } while (nb != 0 || tx_strm.avail_out == 0);
185 }
186
187 if (token != -1) {
188 /* add the data in the current block to the compressor's
189 history and hash table */
1634f4c4 190 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, toklen);
861c20b4
PM
191 tx_strm.avail_in = toklen;
192 tx_strm.next_out = NULL;
193 tx_strm.avail_out = 2 * toklen;
194 r = deflate(&tx_strm, Z_INSERT_ONLY);
195 if (r != Z_OK || tx_strm.avail_in != 0) {
9486289c 196 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
861c20b4
PM
197 r, tx_strm.avail_in);
198 exit_cleanup(1);
199 }
200
201 } else {
202 /* end of file - clean up */
203 write_byte(f, END_FLAG);
204 }
205}
206
207
208/* tells us what the receiver is in the middle of doing */
209static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
70d794dc 210
861c20b4
PM
211/* for inflating stuff */
212static z_stream rx_strm;
213static char *cbuf;
214static char *dbuf;
215
216/* for decoding runs of tokens */
217static int rx_token;
218static int rx_run;
219
220/* Receive a deflated token and inflate it */
221static int
222recv_deflated_token(int f, char **data)
223{
224 int n, r, flag;
3a6a366f
AT
225 static int init_done;
226 static int saved_flag;
861c20b4
PM
227
228 for (;;) {
229 switch (recv_state) {
230 case r_init:
231 if (!init_done) {
232 rx_strm.next_out = NULL;
233 rx_strm.zalloc = z_alloc;
234 rx_strm.zfree = z_free;
235 if (inflateInit2(&rx_strm, -15) != Z_OK) {
9486289c 236 rprintf(FERROR, "inflate init failed\n");
861c20b4
PM
237 exit_cleanup(1);
238 }
239 if ((cbuf = malloc(MAX_DATA_COUNT)) == NULL
240 || (dbuf = malloc(CHUNK_SIZE)) == NULL)
241 out_of_memory("recv_deflated_token");
242 init_done = 1;
243 } else {
244 inflateReset(&rx_strm);
245 }
246 recv_state = r_idle;
5be59dc5 247 rx_token = 0;
861c20b4
PM
248 break;
249
250 case r_idle:
251 case r_inflated:
b8d4524b
PM
252 if (saved_flag) {
253 flag = saved_flag & 0xff;
254 saved_flag = 0;
255 } else
256 flag = read_byte(f);
861c20b4
PM
257 if ((flag & 0xC0) == DEFLATED_DATA) {
258 n = ((flag & 0x3f) << 8) + read_byte(f);
259 read_buf(f, cbuf, n);
1634f4c4 260 rx_strm.next_in = (Bytef *)cbuf;
861c20b4
PM
261 rx_strm.avail_in = n;
262 recv_state = r_inflating;
263 break;
264 }
265 if (recv_state == r_inflated) {
266 /* check previous inflated stuff ended correctly */
267 rx_strm.avail_in = 0;
1634f4c4 268 rx_strm.next_out = (Bytef *)dbuf;
861c20b4
PM
269 rx_strm.avail_out = CHUNK_SIZE;
270 r = inflate(&rx_strm, Z_PACKET_FLUSH);
271 n = CHUNK_SIZE - rx_strm.avail_out;
b8d4524b 272 if (r != Z_OK) {
9486289c 273 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
861c20b4
PM
274 r, n);
275 exit_cleanup(1);
276 }
b8d4524b
PM
277 if (n != 0) {
278 /* have to return some more data and
279 save the flag for later. */
280 saved_flag = flag + 0x10000;
281 if (rx_strm.avail_out != 0)
282 recv_state = r_idle;
283 *data = dbuf;
284 return n;
285 }
861c20b4
PM
286 recv_state = r_idle;
287 }
288 if (flag == END_FLAG) {
289 /* that's all folks */
290 recv_state = r_init;
291 return 0;
292 }
293
294 /* here we have a token of some kind */
295 if (flag & TOKEN_REL) {
296 rx_token += flag & 0x3f;
297 flag >>= 6;
298 } else
299 rx_token = read_int(f);
300 if (flag & 1) {
301 rx_run = read_byte(f);
302 rx_run += read_byte(f) << 8;
303 recv_state = r_running;
304 }
305 return -1 - rx_token;
306
307 case r_inflating:
1634f4c4 308 rx_strm.next_out = (Bytef *)dbuf;
861c20b4
PM
309 rx_strm.avail_out = CHUNK_SIZE;
310 r = inflate(&rx_strm, Z_NO_FLUSH);
311 n = CHUNK_SIZE - rx_strm.avail_out;
280cbb85 312 if (r != Z_OK) {
9486289c 313 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
861c20b4
PM
314 exit_cleanup(1);
315 }
b8d4524b 316 if (rx_strm.avail_in == 0)
861c20b4 317 recv_state = r_inflated;
280cbb85
PM
318 if (n != 0) {
319 *data = dbuf;
320 return n;
321 }
322 break;
861c20b4
PM
323
324 case r_running:
325 ++rx_token;
326 if (--rx_run == 0)
327 recv_state = r_idle;
328 return -1 - rx_token;
329 }
330 }
331}
332
333/*
334 * put the data corresponding to a token that we've just returned
335 * from recv_deflated_token into the decompressor's history buffer.
336 */
337void
338see_deflate_token(char *buf, int len)
339{
340 int r;
341
1634f4c4 342 rx_strm.next_in = (Bytef *)buf;
861c20b4
PM
343 rx_strm.avail_in = len;
344 r = inflateIncomp(&rx_strm);
345 if (r != Z_OK) {
9486289c 346 rprintf(FERROR, "inflateIncomp returned %d\n", r);
861c20b4
PM
347 exit_cleanup(1);
348 }
349}
70d794dc
AT
350
351/*
352 * transmit a verbatim buffer of length n followed by a token
353 * If token == -1 then we have reached EOF
354 * If n == 0 then don't send a buffer
70d794dc 355 */
45f133b9 356void send_token(int f,int token,struct map_struct *buf,OFF_T offset,
9e31c482 357 int n,int toklen)
70d794dc
AT
358{
359 if (!do_compression) {
360 simple_send_token(f,token,buf,offset,n);
861c20b4
PM
361 } else {
362 send_deflated_token(f, token, buf, offset, n, toklen);
70d794dc 363 }
70d794dc
AT
364}
365
366
367/*
368 * receive a token or buffer from the other end. If the reurn value is >0 then
369 * it is a data buffer of that length, and *data will point at the data.
370 * if the return value is -i then it represents token i-1
371 * if the return value is 0 then the end has been reached
372 */
373int recv_token(int f,char **data)
374{
861c20b4
PM
375 int tok;
376
70d794dc 377 if (!do_compression) {
861c20b4
PM
378 tok = simple_recv_token(f,data);
379 } else {
380 tok = recv_deflated_token(f, data);
70d794dc 381 }
861c20b4
PM
382 return tok;
383}
384
385/*
386 * look at the data corresponding to a token, if necessary
387 */
388void see_token(char *data, int toklen)
389{
861c20b4
PM
390 if (do_compression)
391 see_deflate_token(data, toklen);
70d794dc 392}