blob: 75d2b17b48fffc432f70e86da279861a6e337283 [file] [log] [blame]
Wayne Davisond67c8bd2004-06-18 16:22:14 +00001/*
Wayne Davison0f78b812006-04-25 20:23:34 +00002 * Routines used by the file-transfer code.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
Wayne Davisonb3bf9b92009-01-03 10:57:14 -08006 * Copyright (C) 2003-2009 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00007 *
8 * This program is free software; you can redistribute it and/or modify
Wayne Davison8e41b682007-07-10 13:55:49 +00009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
Wayne Davison0f78b812006-04-25 20:23:34 +000012 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
Wayne Davisone7c67062006-04-25 23:51:12 +000018 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000019 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000020 */
Andrew Tridgell70d794d1996-07-01 05:55:05 +000021
22#include "rsync.h"
Wayne Davison5dd14f02008-09-01 19:09:21 -070023#include "itypes.h"
Paul Mackerras5914bf11998-05-22 06:58:52 +000024#include "zlib/zlib.h"
Andrew Tridgell70d794d1996-07-01 05:55:05 +000025
26extern int do_compression;
Wayne Davisond67c8bd2004-06-18 16:22:14 +000027extern int module_id;
Wayne Davisone8a81672005-10-26 16:47:49 +000028extern int def_compress_level;
Wayne Davison6e058b42007-07-14 19:24:35 +000029extern char *skip_compress;
Wayne Davisond67c8bd2004-06-18 16:22:14 +000030
Wayne Davisonec497df2005-12-30 07:19:16 +000031static int compression_level, per_file_default_level;
Andrew Tridgell70d794d1996-07-01 05:55:05 +000032
Wayne Davison6e058b42007-07-14 19:24:35 +000033struct suffix_tree {
34 struct suffix_tree *sibling;
35 struct suffix_tree *child;
36 char letter, word_end;
37};
38
39static char *match_list;
40static struct suffix_tree *suftree;
41
42static void add_suffix(struct suffix_tree **prior, char ltr, const char *str)
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000043{
Wayne Davison6e058b42007-07-14 19:24:35 +000044 struct suffix_tree *node, *newnode;
45
46 if (ltr == '[') {
47 const char *after = strchr(str, ']');
Wayne Davison2f1fb732009-09-05 10:25:42 -070048 /* Treat "[foo" and "[]" as having a literal '['. */
49 if (after && after++ != str+1) {
50 while ((ltr = *str++) != ']')
51 add_suffix(prior, ltr, after);
Wayne Davison6e058b42007-07-14 19:24:35 +000052 return;
Wayne Davison2f1fb732009-09-05 10:25:42 -070053 }
Wayne Davison6e058b42007-07-14 19:24:35 +000054 }
55
56 for (node = *prior; node; prior = &node->sibling, node = node->sibling) {
57 if (node->letter == ltr) {
58 if (*str)
59 add_suffix(&node->child, *str, str+1);
60 else
61 node->word_end = 1;
62 return;
63 }
64 if (node->letter > ltr)
65 break;
66 }
67 if (!(newnode = new(struct suffix_tree)))
68 out_of_memory("add_suffix");
69 newnode->sibling = node;
70 newnode->child = NULL;
71 newnode->letter = ltr;
72 *prior = newnode;
73 if (*str) {
74 add_suffix(&newnode->child, *str, str+1);
75 newnode->word_end = 0;
76 } else
77 newnode->word_end = 1;
78}
79
80static void add_nocompress_suffixes(const char *str)
81{
82 char *buf, *t;
83 const char *f = str;
84
85 if (!(buf = new_array(char, strlen(f) + 1)))
86 out_of_memory("add_nocompress_suffixes");
87
88 while (*f) {
89 if (*f == '/') {
90 f++;
91 continue;
92 }
93
94 t = buf;
95 do {
96 if (isUpper(f))
97 *t++ = toLower(f);
98 else
99 *t++ = *f;
100 } while (*++f != '/' && *f);
101 *t++ = '\0';
102
Wayne Davison6e058b42007-07-14 19:24:35 +0000103 add_suffix(&suftree, *buf, buf+1);
104 }
105
106 free(buf);
107}
108
109static void init_set_compression(void)
110{
111 const char *f;
112 char *t, *start;
113
114 if (skip_compress)
115 add_nocompress_suffixes(skip_compress);
116
117 /* A non-daemon transfer skips the default suffix list if the
118 * user specified --skip-compress. */
119 if (skip_compress && module_id < 0)
120 f = "";
121 else
122 f = lp_dont_compress(module_id);
123
124 if (!(match_list = t = new_array(char, strlen(f) + 2)))
125 out_of_memory("set_compression");
126
127 per_file_default_level = def_compress_level;
128
129 while (*f) {
130 if (*f == ' ') {
131 f++;
132 continue;
133 }
134
135 start = t;
136 do {
137 if (isUpper(f))
138 *t++ = toLower(f);
139 else
140 *t++ = *f;
141 } while (*++f != ' ' && *f);
142 *t++ = '\0';
143
144 if (t - start == 1+1 && *start == '*') {
145 /* Optimize a match-string of "*". */
146 *match_list = '\0';
147 suftree = NULL;
148 per_file_default_level = 0;
149 break;
150 }
151
152 /* Move *.foo items into the stuffix tree. */
153 if (*start == '*' && start[1] == '.' && start[2]
Wayne Davison0d585182007-07-14 19:36:52 +0000154 && !strpbrk(start+2, ".?*")) {
Wayne Davison6e058b42007-07-14 19:24:35 +0000155 add_suffix(&suftree, start[2], start+3);
156 t = start;
157 }
158 }
159 *t++ = '\0';
160}
161
162/* determine the compression level based on a wildcard filename list */
163void set_compression(const char *fname)
164{
165 const struct suffix_tree *node;
166 const char *s;
167 char ltr;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000168
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000169 if (!do_compression)
170 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000171
Wayne Davison6e058b42007-07-14 19:24:35 +0000172 if (!match_list)
173 init_set_compression();
David Dykstra63f07741998-11-25 15:37:50 +0000174
Wayne Davisonec497df2005-12-30 07:19:16 +0000175 compression_level = per_file_default_level;
Wayne Davison0fe987e2005-12-30 06:03:40 +0000176
Wayne Davison6e058b42007-07-14 19:24:35 +0000177 if (!*match_list && !suftree)
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000178 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000179
Wayne Davisonec497df2005-12-30 07:19:16 +0000180 if ((s = strrchr(fname, '/')) != NULL)
181 fname = s + 1;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000182
Wayne Davisonec497df2005-12-30 07:19:16 +0000183 for (s = match_list; *s; s += strlen(s) + 1) {
184 if (iwildmatch(s, fname)) {
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000185 compression_level = 0;
Wayne Davison6e058b42007-07-14 19:24:35 +0000186 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000187 }
188 }
Wayne Davison6e058b42007-07-14 19:24:35 +0000189
190 if (!(node = suftree) || !(s = strrchr(fname, '.'))
191 || s == fname || !(ltr = *++s))
192 return;
193
194 while (1) {
Wayne Davison3be1d9b2010-06-19 09:47:00 -0700195 if (isUpper(&ltr))
196 ltr = toLower(&ltr);
Wayne Davison6e058b42007-07-14 19:24:35 +0000197 while (node->letter != ltr) {
198 if (node->letter > ltr)
199 return;
200 if (!(node = node->sibling))
201 return;
202 }
203 if ((ltr = *++s) == '\0') {
204 if (node->word_end)
205 compression_level = 0;
206 return;
207 }
208 if (!(node = node->child))
209 return;
210 }
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000211}
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000212
213/* non-compressing recv token */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000214static int32 simple_recv_token(int f, char **data)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000215{
Wayne Davisonacc461c2005-02-14 08:19:32 +0000216 static int32 residue;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000217 static char *buf;
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000218 int32 n;
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000219
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000220 if (!buf) {
Wayne Davison58cadc82003-12-06 21:07:27 +0000221 buf = new_array(char, CHUNK_SIZE);
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000222 if (!buf)
223 out_of_memory("simple_recv_token");
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000224 }
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000225
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000226 if (residue == 0) {
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000227 int32 i = read_int(f);
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000228 if (i <= 0)
229 return i;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000230 residue = i;
231 }
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000232
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000233 *data = buf;
234 n = MIN(CHUNK_SIZE,residue);
235 residue -= n;
236 read_buf(f,buf,n);
237 return n;
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000238}
239
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000240/* non-compressing send token */
Wayne Davisonacc461c2005-02-14 08:19:32 +0000241static void simple_send_token(int f, int32 token, struct map_struct *buf,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000242 OFF_T offset, int32 n)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000243{
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000244 if (n > 0) {
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000245 int32 len = 0;
246 while (len < n) {
247 int32 n1 = MIN(CHUNK_SIZE, n-len);
248 write_int(f, n1);
249 write_buf(f, map_ptr(buf, offset+len, n1), n1);
250 len += n1;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000251 }
252 }
Andrew Tridgell45f133b1998-05-22 01:53:02 +0000253 /* a -2 token means to send data only and no token */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000254 if (token != -2)
255 write_int(f, -(token+1));
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000256}
257
Paul Mackerras861c20b1996-07-03 04:05:46 +0000258/* Flag bytes in compressed stream are encoded as follows: */
259#define END_FLAG 0 /* that's all folks */
260#define TOKEN_LONG 0x20 /* followed by 32-bit token number */
261#define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
262#define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
263#define TOKEN_REL 0x80 /* + 6-bit relative token number */
264#define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
265
266#define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
267
Wayne Davison24733912003-07-04 07:47:09 +0000268/* zlib.h says that if we want to be able to compress something in a single
269 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
270 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
271 * to ensure that this is a compile-time value). */
272#define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
273
Paul Mackerras861c20b1996-07-03 04:05:46 +0000274/* For coding runs of tokens */
Wayne Davisonacc461c2005-02-14 08:19:32 +0000275static int32 last_token = -1;
276static int32 run_start;
277static int32 last_run_end;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000278
279/* Deflation state */
280static z_stream tx_strm;
281
282/* Output buffer */
Andrew Tridgell3a6a3661998-03-23 12:52:57 +0000283static char *obuf;
Wayne Davison24733912003-07-04 07:47:09 +0000284
285/* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
286 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
287#if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
288#define OBUF_SIZE (MAX_DATA_COUNT+2)
289#else
290#define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
291#endif
Paul Mackerras861c20b1996-07-03 04:05:46 +0000292
293/* Send a deflated token */
294static void
Wayne Davisonacc461c2005-02-14 08:19:32 +0000295send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000296 int32 nb, int32 toklen)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000297{
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000298 int32 n, r;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000299 static int init_done, flush_pending;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000300
Paul Mackerras5914bf11998-05-22 06:58:52 +0000301 if (last_token == -1) {
302 /* initialization */
303 if (!init_done) {
304 tx_strm.next_in = NULL;
305 tx_strm.zalloc = NULL;
306 tx_strm.zfree = NULL;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000307 if (deflateInit2(&tx_strm, compression_level,
Paul Mackerras5914bf11998-05-22 06:58:52 +0000308 Z_DEFLATED, -15, 8,
309 Z_DEFAULT_STRATEGY) != Z_OK) {
310 rprintf(FERROR, "compression init failed\n");
Wayne Davisone4c598c2009-11-16 12:35:17 -0800311 exit_cleanup(RERR_PROTOCOL);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000312 }
Wayne Davison58cadc82003-12-06 21:07:27 +0000313 if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
Paul Mackerras5914bf11998-05-22 06:58:52 +0000314 out_of_memory("send_deflated_token");
315 init_done = 1;
316 } else
317 deflateReset(&tx_strm);
318 last_run_end = 0;
319 run_start = token;
320 flush_pending = 0;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000321 } else if (last_token == -2) {
322 run_start = token;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000323 } else if (nb != 0 || token != last_token + 1
324 || token >= run_start + 65536) {
325 /* output previous run */
326 r = run_start - last_run_end;
327 n = last_token - run_start;
328 if (r >= 0 && r <= 63) {
329 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
330 } else {
331 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
332 write_int(f, run_start);
Paul Mackerras861c20b1996-07-03 04:05:46 +0000333 }
Paul Mackerras5914bf11998-05-22 06:58:52 +0000334 if (n != 0) {
335 write_byte(f, n);
336 write_byte(f, n >> 8);
337 }
338 last_run_end = last_token;
339 run_start = token;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000340 }
341
Paul Mackerras5914bf11998-05-22 06:58:52 +0000342 last_token = token;
343
344 if (nb != 0 || flush_pending) {
345 /* deflate the data starting at offset */
346 int flush = Z_NO_FLUSH;
347 tx_strm.avail_in = 0;
348 tx_strm.avail_out = 0;
349 do {
350 if (tx_strm.avail_in == 0 && nb != 0) {
351 /* give it some more input */
352 n = MIN(nb, CHUNK_SIZE);
353 tx_strm.next_in = (Bytef *)
354 map_ptr(buf, offset, n);
355 tx_strm.avail_in = n;
356 nb -= n;
357 offset += n;
358 }
359 if (tx_strm.avail_out == 0) {
360 tx_strm.next_out = (Bytef *)(obuf + 2);
361 tx_strm.avail_out = MAX_DATA_COUNT;
362 if (flush != Z_NO_FLUSH) {
363 /*
364 * We left the last 4 bytes in the
365 * buffer, in case they are the
366 * last 4. Move them to the front.
367 */
368 memcpy(tx_strm.next_out,
369 obuf+MAX_DATA_COUNT-2, 4);
370 tx_strm.next_out += 4;
371 tx_strm.avail_out -= 4;
372 }
373 }
374 if (nb == 0 && token != -2)
375 flush = Z_SYNC_FLUSH;
376 r = deflate(&tx_strm, flush);
377 if (r != Z_OK) {
378 rprintf(FERROR, "deflate returned %d\n", r);
Andrew Tridgell65417571998-11-03 07:08:27 +0000379 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000380 }
381 if (nb == 0 || tx_strm.avail_out == 0) {
382 n = MAX_DATA_COUNT - tx_strm.avail_out;
383 if (flush != Z_NO_FLUSH) {
384 /*
385 * We have to trim off the last 4
386 * bytes of output when flushing
387 * (they are just 0, 0, ff, ff).
388 */
389 n -= 4;
390 }
391 if (n > 0) {
392 obuf[0] = DEFLATED_DATA + (n >> 8);
393 obuf[1] = n;
394 write_buf(f, obuf, n+2);
395 }
396 }
397 } while (nb != 0 || tx_strm.avail_out == 0);
398 flush_pending = token == -2;
399 }
400
401 if (token == -1) {
402 /* end of file - clean up */
403 write_byte(f, END_FLAG);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000404 } else if (token != -2) {
Wayne Davisonacc461c2005-02-14 08:19:32 +0000405 /* Add the data in the current block to the compressor's
406 * history and hash table. */
Wayne Davison01f439e2005-02-14 08:28:00 +0000407 do {
408 /* Break up long sections in the same way that
409 * see_deflate_token() does. */
410 int32 n1 = toklen > 0xffff ? 0xffff : toklen;
411 toklen -= n1;
412 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n1);
413 tx_strm.avail_in = n1;
414 tx_strm.next_out = (Bytef *) obuf;
415 tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
416 r = deflate(&tx_strm, Z_INSERT_ONLY);
417 if (r != Z_OK || tx_strm.avail_in != 0) {
418 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
419 r, tx_strm.avail_in);
420 exit_cleanup(RERR_STREAMIO);
421 }
422 } while (toklen > 0);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000423 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000424}
425
Paul Mackerras861c20b1996-07-03 04:05:46 +0000426/* tells us what the receiver is in the middle of doing */
427static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
428
429/* for inflating stuff */
430static z_stream rx_strm;
431static char *cbuf;
432static char *dbuf;
433
434/* for decoding runs of tokens */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000435static int32 rx_token;
436static int32 rx_run;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000437
438/* Receive a deflated token and inflate it */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000439static int32 recv_deflated_token(int f, char **data)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000440{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000441 static int init_done;
Wayne Davisonacc461c2005-02-14 08:19:32 +0000442 static int32 saved_flag;
443 int32 n, flag;
444 int r;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000445
Paul Mackerras5914bf11998-05-22 06:58:52 +0000446 for (;;) {
447 switch (recv_state) {
448 case r_init:
449 if (!init_done) {
450 rx_strm.next_out = NULL;
451 rx_strm.zalloc = NULL;
452 rx_strm.zfree = NULL;
453 if (inflateInit2(&rx_strm, -15) != Z_OK) {
454 rprintf(FERROR, "inflate init failed\n");
Wayne Davisone4c598c2009-11-16 12:35:17 -0800455 exit_cleanup(RERR_PROTOCOL);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000456 }
Wayne Davison58cadc82003-12-06 21:07:27 +0000457 if (!(cbuf = new_array(char, MAX_DATA_COUNT))
458 || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
Paul Mackerras5914bf11998-05-22 06:58:52 +0000459 out_of_memory("recv_deflated_token");
460 init_done = 1;
461 } else {
462 inflateReset(&rx_strm);
463 }
Paul Mackerrasb8d45241996-11-06 04:49:53 +0000464 recv_state = r_idle;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000465 rx_token = 0;
466 break;
467
468 case r_idle:
469 case r_inflated:
470 if (saved_flag) {
471 flag = saved_flag & 0xff;
472 saved_flag = 0;
473 } else
474 flag = read_byte(f);
475 if ((flag & 0xC0) == DEFLATED_DATA) {
476 n = ((flag & 0x3f) << 8) + read_byte(f);
477 read_buf(f, cbuf, n);
478 rx_strm.next_in = (Bytef *)cbuf;
479 rx_strm.avail_in = n;
480 recv_state = r_inflating;
481 break;
482 }
483 if (recv_state == r_inflated) {
484 /* check previous inflated stuff ended correctly */
485 rx_strm.avail_in = 0;
486 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000487 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000488 r = inflate(&rx_strm, Z_SYNC_FLUSH);
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000489 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000490 /*
491 * Z_BUF_ERROR just means no progress was
492 * made, i.e. the decompressor didn't have
493 * any pending output for us.
494 */
495 if (r != Z_OK && r != Z_BUF_ERROR) {
496 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
497 r, n);
Andrew Tridgell65417571998-11-03 07:08:27 +0000498 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000499 }
500 if (n != 0 && r != Z_BUF_ERROR) {
501 /* have to return some more data and
502 save the flag for later. */
503 saved_flag = flag + 0x10000;
504 *data = dbuf;
505 return n;
506 }
507 /*
508 * At this point the decompressor should
509 * be expecting to see the 0, 0, ff, ff bytes.
510 */
511 if (!inflateSyncPoint(&rx_strm)) {
512 rprintf(FERROR, "decompressor lost sync!\n");
Andrew Tridgell65417571998-11-03 07:08:27 +0000513 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000514 }
515 rx_strm.avail_in = 4;
516 rx_strm.next_in = (Bytef *)cbuf;
517 cbuf[0] = cbuf[1] = 0;
518 cbuf[2] = cbuf[3] = 0xff;
519 inflate(&rx_strm, Z_SYNC_FLUSH);
520 recv_state = r_idle;
521 }
522 if (flag == END_FLAG) {
523 /* that's all folks */
524 recv_state = r_init;
525 return 0;
526 }
527
528 /* here we have a token of some kind */
529 if (flag & TOKEN_REL) {
530 rx_token += flag & 0x3f;
531 flag >>= 6;
532 } else
533 rx_token = read_int(f);
534 if (flag & 1) {
535 rx_run = read_byte(f);
536 rx_run += read_byte(f) << 8;
537 recv_state = r_running;
538 }
539 return -1 - rx_token;
540
541 case r_inflating:
542 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000543 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000544 r = inflate(&rx_strm, Z_NO_FLUSH);
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000545 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000546 if (r != Z_OK) {
547 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
Andrew Tridgell65417571998-11-03 07:08:27 +0000548 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000549 }
550 if (rx_strm.avail_in == 0)
551 recv_state = r_inflated;
552 if (n != 0) {
553 *data = dbuf;
554 return n;
555 }
556 break;
557
558 case r_running:
559 ++rx_token;
560 if (--rx_run == 0)
561 recv_state = r_idle;
562 return -1 - rx_token;
Paul Mackerrasb8d45241996-11-06 04:49:53 +0000563 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000564 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000565}
566
567/*
568 * put the data corresponding to a token that we've just returned
569 * from recv_deflated_token into the decompressor's history buffer.
570 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000571static void see_deflate_token(char *buf, int32 len)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000572{
Wayne Davisonacc461c2005-02-14 08:19:32 +0000573 int r;
574 int32 blklen;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000575 unsigned char hdr[5];
Paul Mackerras861c20b1996-07-03 04:05:46 +0000576
Paul Mackerras5914bf11998-05-22 06:58:52 +0000577 rx_strm.avail_in = 0;
578 blklen = 0;
579 hdr[0] = 0;
580 do {
581 if (rx_strm.avail_in == 0 && len != 0) {
582 if (blklen == 0) {
583 /* Give it a fake stored-block header. */
584 rx_strm.next_in = (Bytef *)hdr;
585 rx_strm.avail_in = 5;
586 blklen = len;
587 if (blklen > 0xffff)
588 blklen = 0xffff;
589 hdr[1] = blklen;
590 hdr[2] = blklen >> 8;
591 hdr[3] = ~hdr[1];
592 hdr[4] = ~hdr[2];
593 } else {
594 rx_strm.next_in = (Bytef *)buf;
595 rx_strm.avail_in = blklen;
596 len -= blklen;
597 blklen = 0;
598 }
599 }
600 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000601 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000602 r = inflate(&rx_strm, Z_SYNC_FLUSH);
Wayne Davison4286ea62009-12-21 10:13:52 -0800603 if (r != Z_OK && r != Z_BUF_ERROR) {
Paul Mackerras5914bf11998-05-22 06:58:52 +0000604 rprintf(FERROR, "inflate (token) returned %d\n", r);
Andrew Tridgell65417571998-11-03 07:08:27 +0000605 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000606 }
607 } while (len || rx_strm.avail_out == 0);
Paul Mackerras861c20b1996-07-03 04:05:46 +0000608}
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000609
Martin Pool79f671c2002-04-08 08:35:30 +0000610/**
611 * Transmit a verbatim buffer of length @p n followed by a token.
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000612 * If token == -1 then we have reached EOF
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000613 * If n == 0 then don't send a buffer
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000614 */
Wayne Davisonacc461c2005-02-14 08:19:32 +0000615void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000616 int32 n, int32 toklen)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000617{
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000618 if (!do_compression)
619 simple_send_token(f, token, buf, offset, n);
620 else
Paul Mackerras5914bf11998-05-22 06:58:52 +0000621 send_deflated_token(f, token, buf, offset, n, toklen);
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000622}
623
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000624/*
625 * receive a token or buffer from the other end. If the reurn value is >0 then
626 * it is a data buffer of that length, and *data will point at the data.
627 * if the return value is -i then it represents token i-1
628 * if the return value is 0 then the end has been reached
629 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000630int32 recv_token(int f, char **data)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000631{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000632 int tok;
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000633
Paul Mackerras5914bf11998-05-22 06:58:52 +0000634 if (!do_compression) {
635 tok = simple_recv_token(f,data);
636 } else {
637 tok = recv_deflated_token(f, data);
638 }
639 return tok;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000640}
641
642/*
643 * look at the data corresponding to a token, if necessary
644 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000645void see_token(char *data, int32 toklen)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000646{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000647 if (do_compression)
648 see_deflate_token(data, toklen);
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000649}