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