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