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