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