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