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