Martin gave his approval to use GPLv3 with this code.
[rsync/rsync.git] / token.c
CommitLineData
d67c8bdf 1/*
0f78b815
WD
2 * Routines used by the file-transfer code.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
ba2133d6 6 * Copyright (C) 2003-2007 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
4fd842f9 9 * it under the terms of the GNU General Public License version 3 as
ba2133d6 10 * published by the Free Software Foundation.
0f78b815
WD
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
e7c67065 17 * You should have received a copy of the GNU General Public License along
4fd842f9 18 * with this program; if not, visit the http://fsf.org website.
0f78b815 19 */
70d794dc
AT
20
21#include "rsync.h"
5914bf15 22#include "zlib/zlib.h"
70d794dc
AT
23
24extern int do_compression;
d67c8bdf 25extern int module_id;
e8a8167a 26extern int def_compress_level;
d67c8bdf 27
ec497df1 28static int compression_level, per_file_default_level;
70d794dc 29
83fff1aa
AT
30/* determine the compression level based on a wildcard filename list */
31void set_compression(char *fname)
32{
ec497df1
WD
33 static char *match_list;
34 char *s;
83fff1aa 35
d67c8bdf
WD
36 if (!do_compression)
37 return;
83fff1aa 38
ec497df1
WD
39 if (!match_list) {
40 char *t, *f = lp_dont_compress(module_id);
41 int len = strlen(f);
42 if (!(match_list = t = new_array(char, len + 2)))
43 out_of_memory("set_compression");
44 while (*f) {
45 if (*f == ' ') {
46 f++;
47 continue;
48 }
49 do {
2dc7b8bd
WD
50 if (isUpper(f))
51 *t++ = toLower(f);
ec497df1
WD
52 else
53 *t++ = *f;
54 } while (*++f != ' ' && *f);
55 *t++ = '\0';
56 }
57 /* Optimize a match-string of "*". */
58 if (t - match_list == 2 && match_list[0] == '*') {
59 t = match_list;
60 per_file_default_level = 0;
61 } else
62 per_file_default_level = def_compress_level;
63 *t++ = '\0';
63f0774f
DD
64 }
65
ec497df1 66 compression_level = per_file_default_level;
0fe987e2 67
ec497df1 68 if (!*match_list)
d67c8bdf 69 return;
83fff1aa 70
ec497df1
WD
71 if ((s = strrchr(fname, '/')) != NULL)
72 fname = s + 1;
83fff1aa 73
ec497df1
WD
74 for (s = match_list; *s; s += strlen(s) + 1) {
75 if (iwildmatch(s, fname)) {
83fff1aa
AT
76 compression_level = 0;
77 break;
78 }
79 }
83fff1aa 80}
70d794dc
AT
81
82/* non-compressing recv token */
7fcbf9e4 83static int32 simple_recv_token(int f, char **data)
70d794dc 84{
acc461c7 85 static int32 residue;
c5eb3650 86 static char *buf;
7fcbf9e4 87 int32 n;
70d794dc 88
c5eb3650 89 if (!buf) {
58cadc86 90 buf = new_array(char, CHUNK_SIZE);
d67c8bdf
WD
91 if (!buf)
92 out_of_memory("simple_recv_token");
c5eb3650 93 }
70d794dc 94
c5eb3650 95 if (residue == 0) {
7fcbf9e4 96 int32 i = read_int(f);
d67c8bdf
WD
97 if (i <= 0)
98 return i;
c5eb3650
AT
99 residue = i;
100 }
70d794dc 101
c5eb3650
AT
102 *data = buf;
103 n = MIN(CHUNK_SIZE,residue);
104 residue -= n;
105 read_buf(f,buf,n);
106 return n;
70d794dc
AT
107}
108
70d794dc 109/* non-compressing send token */
acc461c7 110static void simple_send_token(int f, int32 token, struct map_struct *buf,
7fcbf9e4 111 OFF_T offset, int32 n)
70d794dc 112{
c5eb3650 113 if (n > 0) {
7fcbf9e4
WD
114 int32 len = 0;
115 while (len < n) {
116 int32 n1 = MIN(CHUNK_SIZE, n-len);
117 write_int(f, n1);
118 write_buf(f, map_ptr(buf, offset+len, n1), n1);
119 len += n1;
c5eb3650
AT
120 }
121 }
45f133b9 122 /* a -2 token means to send data only and no token */
7fcbf9e4
WD
123 if (token != -2)
124 write_int(f, -(token+1));
70d794dc
AT
125}
126
861c20b4
PM
127/* Flag bytes in compressed stream are encoded as follows: */
128#define END_FLAG 0 /* that's all folks */
129#define TOKEN_LONG 0x20 /* followed by 32-bit token number */
130#define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
131#define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
132#define TOKEN_REL 0x80 /* + 6-bit relative token number */
133#define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
134
135#define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
136
24733919
WD
137/* zlib.h says that if we want to be able to compress something in a single
138 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
139 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
140 * to ensure that this is a compile-time value). */
141#define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
142
861c20b4 143/* For coding runs of tokens */
acc461c7
WD
144static int32 last_token = -1;
145static int32 run_start;
146static int32 last_run_end;
861c20b4
PM
147
148/* Deflation state */
149static z_stream tx_strm;
150
151/* Output buffer */
3a6a366f 152static char *obuf;
24733919
WD
153
154/* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
155 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
156#if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
157#define OBUF_SIZE (MAX_DATA_COUNT+2)
158#else
159#define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
160#endif
861c20b4
PM
161
162/* Send a deflated token */
163static void
acc461c7 164send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
7fcbf9e4 165 int32 nb, int32 toklen)
861c20b4 166{
7fcbf9e4 167 int32 n, r;
5914bf15
PM
168 static int init_done, flush_pending;
169
170 if (last_token == -1) {
171 /* initialization */
172 if (!init_done) {
173 tx_strm.next_in = NULL;
174 tx_strm.zalloc = NULL;
175 tx_strm.zfree = NULL;
83fff1aa 176 if (deflateInit2(&tx_strm, compression_level,
5914bf15
PM
177 Z_DEFLATED, -15, 8,
178 Z_DEFAULT_STRATEGY) != Z_OK) {
179 rprintf(FERROR, "compression init failed\n");
65417579 180 exit_cleanup(RERR_STREAMIO);
5914bf15 181 }
58cadc86 182 if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
5914bf15
PM
183 out_of_memory("send_deflated_token");
184 init_done = 1;
185 } else
186 deflateReset(&tx_strm);
187 last_run_end = 0;
188 run_start = token;
189 flush_pending = 0;
5914bf15
PM
190 } else if (last_token == -2) {
191 run_start = token;
5914bf15
PM
192 } else if (nb != 0 || token != last_token + 1
193 || token >= run_start + 65536) {
194 /* output previous run */
195 r = run_start - last_run_end;
196 n = last_token - run_start;
197 if (r >= 0 && r <= 63) {
198 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
199 } else {
200 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
201 write_int(f, run_start);
202 }
203 if (n != 0) {
204 write_byte(f, n);
205 write_byte(f, n >> 8);
206 }
207 last_run_end = last_token;
208 run_start = token;
861c20b4 209 }
5914bf15
PM
210
211 last_token = token;
212
213 if (nb != 0 || flush_pending) {
214 /* deflate the data starting at offset */
215 int flush = Z_NO_FLUSH;
216 tx_strm.avail_in = 0;
217 tx_strm.avail_out = 0;
218 do {
219 if (tx_strm.avail_in == 0 && nb != 0) {
220 /* give it some more input */
221 n = MIN(nb, CHUNK_SIZE);
222 tx_strm.next_in = (Bytef *)
223 map_ptr(buf, offset, n);
224 tx_strm.avail_in = n;
225 nb -= n;
226 offset += n;
227 }
228 if (tx_strm.avail_out == 0) {
229 tx_strm.next_out = (Bytef *)(obuf + 2);
230 tx_strm.avail_out = MAX_DATA_COUNT;
231 if (flush != Z_NO_FLUSH) {
232 /*
233 * We left the last 4 bytes in the
234 * buffer, in case they are the
235 * last 4. Move them to the front.
236 */
237 memcpy(tx_strm.next_out,
238 obuf+MAX_DATA_COUNT-2, 4);
239 tx_strm.next_out += 4;
240 tx_strm.avail_out -= 4;
241 }
242 }
243 if (nb == 0 && token != -2)
244 flush = Z_SYNC_FLUSH;
245 r = deflate(&tx_strm, flush);
246 if (r != Z_OK) {
247 rprintf(FERROR, "deflate returned %d\n", r);
65417579 248 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
249 }
250 if (nb == 0 || tx_strm.avail_out == 0) {
251 n = MAX_DATA_COUNT - tx_strm.avail_out;
252 if (flush != Z_NO_FLUSH) {
253 /*
254 * We have to trim off the last 4
255 * bytes of output when flushing
256 * (they are just 0, 0, ff, ff).
257 */
258 n -= 4;
259 }
260 if (n > 0) {
261 obuf[0] = DEFLATED_DATA + (n >> 8);
262 obuf[1] = n;
263 write_buf(f, obuf, n+2);
264 }
265 }
266 } while (nb != 0 || tx_strm.avail_out == 0);
267 flush_pending = token == -2;
861c20b4 268 }
861c20b4 269
5914bf15
PM
270 if (token == -1) {
271 /* end of file - clean up */
272 write_byte(f, END_FLAG);
5914bf15 273 } else if (token != -2) {
acc461c7
WD
274 /* Add the data in the current block to the compressor's
275 * history and hash table. */
01f439ec
WD
276 do {
277 /* Break up long sections in the same way that
278 * see_deflate_token() does. */
279 int32 n1 = toklen > 0xffff ? 0xffff : toklen;
280 toklen -= n1;
281 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n1);
282 tx_strm.avail_in = n1;
283 tx_strm.next_out = (Bytef *) obuf;
284 tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
285 r = deflate(&tx_strm, Z_INSERT_ONLY);
286 if (r != Z_OK || tx_strm.avail_in != 0) {
287 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
288 r, tx_strm.avail_in);
289 exit_cleanup(RERR_STREAMIO);
290 }
291 } while (toklen > 0);
861c20b4 292 }
861c20b4
PM
293}
294
861c20b4
PM
295/* tells us what the receiver is in the middle of doing */
296static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
70d794dc 297
861c20b4
PM
298/* for inflating stuff */
299static z_stream rx_strm;
300static char *cbuf;
301static char *dbuf;
302
303/* for decoding runs of tokens */
7fcbf9e4
WD
304static int32 rx_token;
305static int32 rx_run;
861c20b4
PM
306
307/* Receive a deflated token and inflate it */
7fcbf9e4 308static int32 recv_deflated_token(int f, char **data)
861c20b4 309{
5914bf15 310 static int init_done;
acc461c7
WD
311 static int32 saved_flag;
312 int32 n, flag;
313 int r;
5914bf15
PM
314
315 for (;;) {
316 switch (recv_state) {
317 case r_init:
318 if (!init_done) {
319 rx_strm.next_out = NULL;
320 rx_strm.zalloc = NULL;
321 rx_strm.zfree = NULL;
322 if (inflateInit2(&rx_strm, -15) != Z_OK) {
323 rprintf(FERROR, "inflate init failed\n");
65417579 324 exit_cleanup(RERR_STREAMIO);
5914bf15 325 }
58cadc86
WD
326 if (!(cbuf = new_array(char, MAX_DATA_COUNT))
327 || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
5914bf15
PM
328 out_of_memory("recv_deflated_token");
329 init_done = 1;
330 } else {
331 inflateReset(&rx_strm);
332 }
b8d4524b 333 recv_state = r_idle;
5914bf15
PM
334 rx_token = 0;
335 break;
336
337 case r_idle:
338 case r_inflated:
339 if (saved_flag) {
340 flag = saved_flag & 0xff;
341 saved_flag = 0;
342 } else
343 flag = read_byte(f);
344 if ((flag & 0xC0) == DEFLATED_DATA) {
345 n = ((flag & 0x3f) << 8) + read_byte(f);
346 read_buf(f, cbuf, n);
347 rx_strm.next_in = (Bytef *)cbuf;
348 rx_strm.avail_in = n;
349 recv_state = r_inflating;
350 break;
351 }
352 if (recv_state == r_inflated) {
353 /* check previous inflated stuff ended correctly */
354 rx_strm.avail_in = 0;
355 rx_strm.next_out = (Bytef *)dbuf;
1dbb94ca 356 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
5914bf15 357 r = inflate(&rx_strm, Z_SYNC_FLUSH);
1dbb94ca 358 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
5914bf15
PM
359 /*
360 * Z_BUF_ERROR just means no progress was
361 * made, i.e. the decompressor didn't have
362 * any pending output for us.
363 */
364 if (r != Z_OK && r != Z_BUF_ERROR) {
365 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
366 r, n);
65417579 367 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
368 }
369 if (n != 0 && r != Z_BUF_ERROR) {
370 /* have to return some more data and
371 save the flag for later. */
372 saved_flag = flag + 0x10000;
373 *data = dbuf;
374 return n;
375 }
376 /*
377 * At this point the decompressor should
378 * be expecting to see the 0, 0, ff, ff bytes.
379 */
380 if (!inflateSyncPoint(&rx_strm)) {
381 rprintf(FERROR, "decompressor lost sync!\n");
65417579 382 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
383 }
384 rx_strm.avail_in = 4;
385 rx_strm.next_in = (Bytef *)cbuf;
386 cbuf[0] = cbuf[1] = 0;
387 cbuf[2] = cbuf[3] = 0xff;
388 inflate(&rx_strm, Z_SYNC_FLUSH);
389 recv_state = r_idle;
390 }
391 if (flag == END_FLAG) {
392 /* that's all folks */
393 recv_state = r_init;
394 return 0;
395 }
396
397 /* here we have a token of some kind */
398 if (flag & TOKEN_REL) {
399 rx_token += flag & 0x3f;
400 flag >>= 6;
401 } else
402 rx_token = read_int(f);
403 if (flag & 1) {
404 rx_run = read_byte(f);
405 rx_run += read_byte(f) << 8;
406 recv_state = r_running;
407 }
408 return -1 - rx_token;
409
410 case r_inflating:
411 rx_strm.next_out = (Bytef *)dbuf;
1dbb94ca 412 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
5914bf15 413 r = inflate(&rx_strm, Z_NO_FLUSH);
1dbb94ca 414 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
5914bf15
PM
415 if (r != Z_OK) {
416 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
65417579 417 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
418 }
419 if (rx_strm.avail_in == 0)
420 recv_state = r_inflated;
421 if (n != 0) {
422 *data = dbuf;
423 return n;
424 }
425 break;
426
427 case r_running:
428 ++rx_token;
429 if (--rx_run == 0)
430 recv_state = r_idle;
431 return -1 - rx_token;
b8d4524b 432 }
861c20b4 433 }
861c20b4
PM
434}
435
436/*
437 * put the data corresponding to a token that we've just returned
438 * from recv_deflated_token into the decompressor's history buffer.
439 */
7fcbf9e4 440static void see_deflate_token(char *buf, int32 len)
861c20b4 441{
acc461c7
WD
442 int r;
443 int32 blklen;
5914bf15
PM
444 unsigned char hdr[5];
445
446 rx_strm.avail_in = 0;
447 blklen = 0;
448 hdr[0] = 0;
449 do {
450 if (rx_strm.avail_in == 0 && len != 0) {
451 if (blklen == 0) {
452 /* Give it a fake stored-block header. */
453 rx_strm.next_in = (Bytef *)hdr;
454 rx_strm.avail_in = 5;
455 blklen = len;
456 if (blklen > 0xffff)
457 blklen = 0xffff;
458 hdr[1] = blklen;
459 hdr[2] = blklen >> 8;
460 hdr[3] = ~hdr[1];
461 hdr[4] = ~hdr[2];
462 } else {
463 rx_strm.next_in = (Bytef *)buf;
464 rx_strm.avail_in = blklen;
465 len -= blklen;
466 blklen = 0;
467 }
468 }
469 rx_strm.next_out = (Bytef *)dbuf;
1dbb94ca 470 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
5914bf15
PM
471 r = inflate(&rx_strm, Z_SYNC_FLUSH);
472 if (r != Z_OK) {
473 rprintf(FERROR, "inflate (token) returned %d\n", r);
65417579 474 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
475 }
476 } while (len || rx_strm.avail_out == 0);
861c20b4 477}
70d794dc 478
79f671cc
MP
479/**
480 * Transmit a verbatim buffer of length @p n followed by a token.
d67c8bdf 481 * If token == -1 then we have reached EOF
70d794dc 482 * If n == 0 then don't send a buffer
70d794dc 483 */
acc461c7 484void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
7fcbf9e4 485 int32 n, int32 toklen)
70d794dc 486{
7fcbf9e4
WD
487 if (!do_compression)
488 simple_send_token(f, token, buf, offset, n);
489 else
5914bf15 490 send_deflated_token(f, token, buf, offset, n, toklen);
70d794dc
AT
491}
492
70d794dc
AT
493/*
494 * receive a token or buffer from the other end. If the reurn value is >0 then
495 * it is a data buffer of that length, and *data will point at the data.
496 * if the return value is -i then it represents token i-1
497 * if the return value is 0 then the end has been reached
498 */
7fcbf9e4 499int32 recv_token(int f, char **data)
70d794dc 500{
5914bf15
PM
501 int tok;
502
503 if (!do_compression) {
504 tok = simple_recv_token(f,data);
505 } else {
506 tok = recv_deflated_token(f, data);
507 }
508 return tok;
861c20b4
PM
509}
510
511/*
512 * look at the data corresponding to a token, if necessary
513 */
7fcbf9e4 514void see_token(char *data, int32 toklen)
861c20b4 515{
5914bf15
PM
516 if (do_compression)
517 see_deflate_token(data, toklen);
70d794dc 518}