Attempting to make the mask test compatible with Solaris 8.
[rsync/rsync.git] / token.c
1 /*
2  * Routines used by the file-transfer code.
3  *
4  * Copyright (C) 1996 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2003-2007 Wayne Davison
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
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  *
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.
20  */
21
22 #include "rsync.h"
23 #include "zlib/zlib.h"
24
25 extern int do_compression;
26 extern int module_id;
27 extern int def_compress_level;
28
29 static int compression_level, per_file_default_level;
30
31 /* determine the compression level based on a wildcard filename list */
32 void set_compression(char *fname)
33 {
34         static char *match_list;
35         char *s;
36
37         if (!do_compression)
38                 return;
39
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 {
51                                 if (isUpper(f))
52                                         *t++ = toLower(f);
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';
65         }
66
67         compression_level = per_file_default_level;
68
69         if (!*match_list)
70                 return;
71
72         if ((s = strrchr(fname, '/')) != NULL)
73                 fname = s + 1;
74
75         for (s = match_list; *s; s += strlen(s) + 1) {
76                 if (iwildmatch(s, fname)) {
77                         compression_level = 0;
78                         break;
79                 }
80         }
81 }
82
83 /* non-compressing recv token */
84 static int32 simple_recv_token(int f, char **data)
85 {
86         static int32 residue;
87         static char *buf;
88         int32 n;
89
90         if (!buf) {
91                 buf = new_array(char, CHUNK_SIZE);
92                 if (!buf)
93                         out_of_memory("simple_recv_token");
94         }
95
96         if (residue == 0) {
97                 int32 i = read_int(f);
98                 if (i <= 0)
99                         return i;
100                 residue = i;
101         }
102
103         *data = buf;
104         n = MIN(CHUNK_SIZE,residue);
105         residue -= n;
106         read_buf(f,buf,n);
107         return n;
108 }
109
110 /* non-compressing send token */
111 static void simple_send_token(int f, int32 token, struct map_struct *buf,
112                               OFF_T offset, int32 n)
113 {
114         if (n > 0) {
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;
121                 }
122         }
123         /* a -2 token means to send data only and no token */
124         if (token != -2)
125                 write_int(f, -(token+1));
126 }
127
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
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
144 /* For coding runs of tokens */
145 static int32 last_token = -1;
146 static int32 run_start;
147 static int32 last_run_end;
148
149 /* Deflation state */
150 static z_stream tx_strm;
151
152 /* Output buffer */
153 static char *obuf;
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
162
163 /* Send a deflated token */
164 static void
165 send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
166                     int32 nb, int32 toklen)
167 {
168         int32 n, r;
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;
177                         if (deflateInit2(&tx_strm, compression_level,
178                                          Z_DEFLATED, -15, 8,
179                                          Z_DEFAULT_STRATEGY) != Z_OK) {
180                                 rprintf(FERROR, "compression init failed\n");
181                                 exit_cleanup(RERR_STREAMIO);
182                         }
183                         if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
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;
191         } else if (last_token == -2) {
192                 run_start = token;
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;
210         }
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);
249                                 exit_cleanup(RERR_STREAMIO);
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;
269         }
270
271         if (token == -1) {
272                 /* end of file - clean up */
273                 write_byte(f, END_FLAG);
274         } else if (token != -2) {
275                 /* Add the data in the current block to the compressor's
276                  * history and hash table. */
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);
293         }
294 }
295
296 /* tells us what the receiver is in the middle of doing */
297 static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
298
299 /* for inflating stuff */
300 static z_stream rx_strm;
301 static char *cbuf;
302 static char *dbuf;
303
304 /* for decoding runs of tokens */
305 static int32 rx_token;
306 static int32 rx_run;
307
308 /* Receive a deflated token and inflate it */
309 static int32 recv_deflated_token(int f, char **data)
310 {
311         static int init_done;
312         static int32 saved_flag;
313         int32 n, flag;
314         int r;
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");
325                                         exit_cleanup(RERR_STREAMIO);
326                                 }
327                                 if (!(cbuf = new_array(char, MAX_DATA_COUNT))
328                                     || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
329                                         out_of_memory("recv_deflated_token");
330                                 init_done = 1;
331                         } else {
332                                 inflateReset(&rx_strm);
333                         }
334                         recv_state = r_idle;
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;
357                                 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
358                                 r = inflate(&rx_strm, Z_SYNC_FLUSH);
359                                 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
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);
368                                         exit_cleanup(RERR_STREAMIO);
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");
383                                         exit_cleanup(RERR_STREAMIO);
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;
413                         rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
414                         r = inflate(&rx_strm, Z_NO_FLUSH);
415                         n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
416                         if (r != Z_OK) {
417                                 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
418                                 exit_cleanup(RERR_STREAMIO);
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;
433                 }
434         }
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  */
441 static void see_deflate_token(char *buf, int32 len)
442 {
443         int r;
444         int32 blklen;
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;
471                 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
472                 r = inflate(&rx_strm, Z_SYNC_FLUSH);
473                 if (r != Z_OK) {
474                         rprintf(FERROR, "inflate (token) returned %d\n", r);
475                         exit_cleanup(RERR_STREAMIO);
476                 }
477         } while (len || rx_strm.avail_out == 0);
478 }
479
480 /**
481  * Transmit a verbatim buffer of length @p n followed by a token.
482  * If token == -1 then we have reached EOF
483  * If n == 0 then don't send a buffer
484  */
485 void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
486                 int32 n, int32 toklen)
487 {
488         if (!do_compression)
489                 simple_send_token(f, token, buf, offset, n);
490         else
491                 send_deflated_token(f, token, buf, offset, n, toklen);
492 }
493
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  */
500 int32 recv_token(int f, char **data)
501 {
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;
510 }
511
512 /*
513  * look at the data corresponding to a token, if necessary
514  */
515 void see_token(char *data, int32 toklen)
516 {
517         if (do_compression)
518                 see_deflate_token(data, toklen);
519 }