blob: ca95e0145e604a3f46239946772a93d8a0ba7711 [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 Davison3ba4db72020-04-16 09:29:22 -07006 * Copyright (C) 2003-2020 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"
Wayne Davison7da17142011-11-21 09:22:14 -080024#include <zlib.h>
25
Andrew Tridgell70d794d1996-07-01 05:55:05 +000026extern int do_compression;
Wayne Davisoncbdff742011-11-21 09:13:11 -080027extern int protocol_version;
Wayne Davisond67c8bd2004-06-18 16:22:14 +000028extern int module_id;
Wayne Davisond999efe2020-05-25 10:47:57 -070029extern int do_compression_level;
Wayne Davison6e058b42007-07-14 19:24:35 +000030extern char *skip_compress;
Wayne Davisond67c8bd2004-06-18 16:22:14 +000031
Wayne Davison64d5ea32020-05-24 15:45:59 -070032#ifndef Z_INSERT_ONLY
33#define Z_INSERT_ONLY Z_SYNC_FLUSH
34#endif
35
Wayne Davisonec497df2005-12-30 07:19:16 +000036static int compression_level, per_file_default_level;
Andrew Tridgell70d794d1996-07-01 05:55:05 +000037
Wayne Davison6e058b42007-07-14 19:24:35 +000038struct suffix_tree {
39 struct suffix_tree *sibling;
40 struct suffix_tree *child;
41 char letter, word_end;
42};
43
44static char *match_list;
45static struct suffix_tree *suftree;
46
Wayne Davisond999efe2020-05-25 10:47:57 -070047void init_compression_level(void)
48{
49 int min_level, max_level, def_level, off_level;
50
51 switch (do_compression) {
52 case CPRES_ZLIB:
53 case CPRES_ZLIBX:
54 min_level = 1;
55 max_level = Z_BEST_COMPRESSION;
56 def_level = 6; /* Z_DEFAULT_COMPRESSION is -1, so set it to the real default */
57 off_level = Z_NO_COMPRESSION;
58 if (do_compression_level == Z_DEFAULT_COMPRESSION)
59 do_compression_level = def_level;
60 break;
61 default: /* paranoia to prevent missing case values */
62 exit_cleanup(RERR_UNSUPPORTED);
63 }
64
65 if (do_compression_level == off_level) {
66 do_compression = CPRES_NONE;
67 return;
68 }
69
70 /* We don't bother with any errors or warnings -- just make sure that the values are valid. */
71 if (do_compression_level == CLVL_NOT_SPECIFIED)
72 do_compression_level = def_level;
73 else if (do_compression_level < min_level)
74 do_compression_level = min_level;
75 else if (do_compression_level > max_level)
76 do_compression_level = max_level;
77}
78
Wayne Davison6e058b42007-07-14 19:24:35 +000079static void add_suffix(struct suffix_tree **prior, char ltr, const char *str)
Andrew Tridgell83fff1a1998-11-20 22:26:29 +000080{
Wayne Davison6e058b42007-07-14 19:24:35 +000081 struct suffix_tree *node, *newnode;
82
83 if (ltr == '[') {
84 const char *after = strchr(str, ']');
Wayne Davison2f1fb732009-09-05 10:25:42 -070085 /* Treat "[foo" and "[]" as having a literal '['. */
86 if (after && after++ != str+1) {
87 while ((ltr = *str++) != ']')
88 add_suffix(prior, ltr, after);
Wayne Davison6e058b42007-07-14 19:24:35 +000089 return;
Wayne Davison2f1fb732009-09-05 10:25:42 -070090 }
Wayne Davison6e058b42007-07-14 19:24:35 +000091 }
92
93 for (node = *prior; node; prior = &node->sibling, node = node->sibling) {
94 if (node->letter == ltr) {
95 if (*str)
96 add_suffix(&node->child, *str, str+1);
97 else
98 node->word_end = 1;
99 return;
100 }
101 if (node->letter > ltr)
102 break;
103 }
104 if (!(newnode = new(struct suffix_tree)))
105 out_of_memory("add_suffix");
106 newnode->sibling = node;
107 newnode->child = NULL;
108 newnode->letter = ltr;
109 *prior = newnode;
110 if (*str) {
111 add_suffix(&newnode->child, *str, str+1);
112 newnode->word_end = 0;
113 } else
114 newnode->word_end = 1;
115}
116
117static void add_nocompress_suffixes(const char *str)
118{
119 char *buf, *t;
120 const char *f = str;
121
122 if (!(buf = new_array(char, strlen(f) + 1)))
123 out_of_memory("add_nocompress_suffixes");
124
125 while (*f) {
126 if (*f == '/') {
127 f++;
128 continue;
129 }
130
131 t = buf;
132 do {
133 if (isUpper(f))
134 *t++ = toLower(f);
135 else
136 *t++ = *f;
137 } while (*++f != '/' && *f);
138 *t++ = '\0';
139
Wayne Davison6e058b42007-07-14 19:24:35 +0000140 add_suffix(&suftree, *buf, buf+1);
141 }
142
143 free(buf);
144}
145
146static void init_set_compression(void)
147{
148 const char *f;
149 char *t, *start;
150
151 if (skip_compress)
152 add_nocompress_suffixes(skip_compress);
153
154 /* A non-daemon transfer skips the default suffix list if the
155 * user specified --skip-compress. */
156 if (skip_compress && module_id < 0)
157 f = "";
158 else
159 f = lp_dont_compress(module_id);
160
161 if (!(match_list = t = new_array(char, strlen(f) + 2)))
162 out_of_memory("set_compression");
163
Wayne Davisond999efe2020-05-25 10:47:57 -0700164 per_file_default_level = do_compression_level;
Wayne Davison6e058b42007-07-14 19:24:35 +0000165
166 while (*f) {
167 if (*f == ' ') {
168 f++;
169 continue;
170 }
171
172 start = t;
173 do {
174 if (isUpper(f))
175 *t++ = toLower(f);
176 else
177 *t++ = *f;
178 } while (*++f != ' ' && *f);
179 *t++ = '\0';
180
181 if (t - start == 1+1 && *start == '*') {
182 /* Optimize a match-string of "*". */
183 *match_list = '\0';
184 suftree = NULL;
185 per_file_default_level = 0;
186 break;
187 }
188
189 /* Move *.foo items into the stuffix tree. */
190 if (*start == '*' && start[1] == '.' && start[2]
Wayne Davison0d585182007-07-14 19:36:52 +0000191 && !strpbrk(start+2, ".?*")) {
Wayne Davison6e058b42007-07-14 19:24:35 +0000192 add_suffix(&suftree, start[2], start+3);
193 t = start;
194 }
195 }
196 *t++ = '\0';
197}
198
199/* determine the compression level based on a wildcard filename list */
200void set_compression(const char *fname)
201{
202 const struct suffix_tree *node;
203 const char *s;
204 char ltr;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000205
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000206 if (!do_compression)
207 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000208
Wayne Davison6e058b42007-07-14 19:24:35 +0000209 if (!match_list)
210 init_set_compression();
David Dykstra63f07741998-11-25 15:37:50 +0000211
Wayne Davisonec497df2005-12-30 07:19:16 +0000212 compression_level = per_file_default_level;
Wayne Davison0fe987e2005-12-30 06:03:40 +0000213
Wayne Davison6e058b42007-07-14 19:24:35 +0000214 if (!*match_list && !suftree)
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000215 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000216
Wayne Davisonec497df2005-12-30 07:19:16 +0000217 if ((s = strrchr(fname, '/')) != NULL)
218 fname = s + 1;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000219
Wayne Davisonec497df2005-12-30 07:19:16 +0000220 for (s = match_list; *s; s += strlen(s) + 1) {
221 if (iwildmatch(s, fname)) {
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000222 compression_level = 0;
Wayne Davison6e058b42007-07-14 19:24:35 +0000223 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000224 }
225 }
Wayne Davison6e058b42007-07-14 19:24:35 +0000226
227 if (!(node = suftree) || !(s = strrchr(fname, '.'))
228 || s == fname || !(ltr = *++s))
229 return;
230
231 while (1) {
Wayne Davison3be1d9b2010-06-19 09:47:00 -0700232 if (isUpper(&ltr))
233 ltr = toLower(&ltr);
Wayne Davison6e058b42007-07-14 19:24:35 +0000234 while (node->letter != ltr) {
235 if (node->letter > ltr)
236 return;
237 if (!(node = node->sibling))
238 return;
239 }
240 if ((ltr = *++s) == '\0') {
241 if (node->word_end)
242 compression_level = 0;
243 return;
244 }
245 if (!(node = node->child))
246 return;
247 }
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000248}
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000249
250/* non-compressing recv token */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000251static int32 simple_recv_token(int f, char **data)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000252{
Wayne Davisonacc461c2005-02-14 08:19:32 +0000253 static int32 residue;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000254 static char *buf;
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000255 int32 n;
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000256
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000257 if (!buf) {
Wayne Davison58cadc82003-12-06 21:07:27 +0000258 buf = new_array(char, CHUNK_SIZE);
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000259 if (!buf)
260 out_of_memory("simple_recv_token");
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000261 }
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000262
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000263 if (residue == 0) {
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000264 int32 i = read_int(f);
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000265 if (i <= 0)
266 return i;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000267 residue = i;
268 }
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000269
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000270 *data = buf;
271 n = MIN(CHUNK_SIZE,residue);
272 residue -= n;
273 read_buf(f,buf,n);
274 return n;
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000275}
276
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000277/* non-compressing send token */
Wayne Davisonacc461c2005-02-14 08:19:32 +0000278static void simple_send_token(int f, int32 token, struct map_struct *buf,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000279 OFF_T offset, int32 n)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000280{
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000281 if (n > 0) {
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000282 int32 len = 0;
283 while (len < n) {
284 int32 n1 = MIN(CHUNK_SIZE, n-len);
285 write_int(f, n1);
286 write_buf(f, map_ptr(buf, offset+len, n1), n1);
287 len += n1;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000288 }
289 }
Andrew Tridgell45f133b1998-05-22 01:53:02 +0000290 /* a -2 token means to send data only and no token */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000291 if (token != -2)
292 write_int(f, -(token+1));
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000293}
294
Paul Mackerras861c20b1996-07-03 04:05:46 +0000295/* Flag bytes in compressed stream are encoded as follows: */
296#define END_FLAG 0 /* that's all folks */
297#define TOKEN_LONG 0x20 /* followed by 32-bit token number */
298#define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
299#define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
300#define TOKEN_REL 0x80 /* + 6-bit relative token number */
301#define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
302
303#define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
304
Wayne Davison24733912003-07-04 07:47:09 +0000305/* zlib.h says that if we want to be able to compress something in a single
306 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
307 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
308 * to ensure that this is a compile-time value). */
309#define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
310
Paul Mackerras861c20b1996-07-03 04:05:46 +0000311/* For coding runs of tokens */
Wayne Davisonacc461c2005-02-14 08:19:32 +0000312static int32 last_token = -1;
313static int32 run_start;
314static int32 last_run_end;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000315
316/* Deflation state */
317static z_stream tx_strm;
318
319/* Output buffer */
Andrew Tridgell3a6a3661998-03-23 12:52:57 +0000320static char *obuf;
Wayne Davison24733912003-07-04 07:47:09 +0000321
322/* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
323 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
324#if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
325#define OBUF_SIZE (MAX_DATA_COUNT+2)
326#else
327#define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
328#endif
Paul Mackerras861c20b1996-07-03 04:05:46 +0000329
330/* Send a deflated token */
331static void
Wayne Davisonacc461c2005-02-14 08:19:32 +0000332send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000333 int32 nb, int32 toklen)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000334{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000335 static int init_done, flush_pending;
Wayne Davison4496e0e2020-05-24 18:08:09 -0700336 int32 n, r;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000337
Paul Mackerras5914bf11998-05-22 06:58:52 +0000338 if (last_token == -1) {
339 /* initialization */
340 if (!init_done) {
341 tx_strm.next_in = NULL;
342 tx_strm.zalloc = NULL;
343 tx_strm.zfree = NULL;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000344 if (deflateInit2(&tx_strm, compression_level,
Paul Mackerras5914bf11998-05-22 06:58:52 +0000345 Z_DEFLATED, -15, 8,
346 Z_DEFAULT_STRATEGY) != Z_OK) {
347 rprintf(FERROR, "compression init failed\n");
Wayne Davisone4c598c2009-11-16 12:35:17 -0800348 exit_cleanup(RERR_PROTOCOL);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000349 }
Wayne Davison58cadc82003-12-06 21:07:27 +0000350 if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
Paul Mackerras5914bf11998-05-22 06:58:52 +0000351 out_of_memory("send_deflated_token");
352 init_done = 1;
353 } else
354 deflateReset(&tx_strm);
355 last_run_end = 0;
356 run_start = token;
357 flush_pending = 0;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000358 } else if (last_token == -2) {
359 run_start = token;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000360 } else if (nb != 0 || token != last_token + 1
361 || token >= run_start + 65536) {
362 /* output previous run */
363 r = run_start - last_run_end;
364 n = last_token - run_start;
365 if (r >= 0 && r <= 63) {
366 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
367 } else {
368 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
369 write_int(f, run_start);
Paul Mackerras861c20b1996-07-03 04:05:46 +0000370 }
Paul Mackerras5914bf11998-05-22 06:58:52 +0000371 if (n != 0) {
372 write_byte(f, n);
373 write_byte(f, n >> 8);
374 }
375 last_run_end = last_token;
376 run_start = token;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000377 }
378
Paul Mackerras5914bf11998-05-22 06:58:52 +0000379 last_token = token;
380
381 if (nb != 0 || flush_pending) {
382 /* deflate the data starting at offset */
383 int flush = Z_NO_FLUSH;
384 tx_strm.avail_in = 0;
385 tx_strm.avail_out = 0;
386 do {
387 if (tx_strm.avail_in == 0 && nb != 0) {
388 /* give it some more input */
389 n = MIN(nb, CHUNK_SIZE);
390 tx_strm.next_in = (Bytef *)
391 map_ptr(buf, offset, n);
392 tx_strm.avail_in = n;
393 nb -= n;
394 offset += n;
395 }
396 if (tx_strm.avail_out == 0) {
397 tx_strm.next_out = (Bytef *)(obuf + 2);
398 tx_strm.avail_out = MAX_DATA_COUNT;
399 if (flush != Z_NO_FLUSH) {
400 /*
401 * We left the last 4 bytes in the
402 * buffer, in case they are the
403 * last 4. Move them to the front.
404 */
405 memcpy(tx_strm.next_out,
406 obuf+MAX_DATA_COUNT-2, 4);
407 tx_strm.next_out += 4;
408 tx_strm.avail_out -= 4;
409 }
410 }
411 if (nb == 0 && token != -2)
412 flush = Z_SYNC_FLUSH;
413 r = deflate(&tx_strm, flush);
414 if (r != Z_OK) {
415 rprintf(FERROR, "deflate returned %d\n", r);
Andrew Tridgell65417571998-11-03 07:08:27 +0000416 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000417 }
418 if (nb == 0 || tx_strm.avail_out == 0) {
419 n = MAX_DATA_COUNT - tx_strm.avail_out;
420 if (flush != Z_NO_FLUSH) {
421 /*
422 * We have to trim off the last 4
423 * bytes of output when flushing
424 * (they are just 0, 0, ff, ff).
425 */
426 n -= 4;
427 }
428 if (n > 0) {
429 obuf[0] = DEFLATED_DATA + (n >> 8);
430 obuf[1] = n;
431 write_buf(f, obuf, n+2);
432 }
433 }
434 } while (nb != 0 || tx_strm.avail_out == 0);
435 flush_pending = token == -2;
436 }
437
438 if (token == -1) {
439 /* end of file - clean up */
440 write_byte(f, END_FLAG);
Wayne Davison4496e0e2020-05-24 18:08:09 -0700441 } else if (token != -2 && do_compression == CPRES_ZLIB) {
Wayne Davisonacc461c2005-02-14 08:19:32 +0000442 /* Add the data in the current block to the compressor's
443 * history and hash table. */
Wayne Davison01f439e2005-02-14 08:28:00 +0000444 do {
445 /* Break up long sections in the same way that
446 * see_deflate_token() does. */
447 int32 n1 = toklen > 0xffff ? 0xffff : toklen;
448 toklen -= n1;
449 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n1);
450 tx_strm.avail_in = n1;
Wayne Davisoncbdff742011-11-21 09:13:11 -0800451 if (protocol_version >= 31) /* Newer protocols avoid a data-duplicating bug */
452 offset += n1;
Wayne Davison22a3ac02014-04-19 12:11:11 -0700453 tx_strm.next_out = (Bytef *) obuf;
454 tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
455 r = deflate(&tx_strm, Z_INSERT_ONLY);
456 if (r != Z_OK || tx_strm.avail_in != 0) {
457 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
458 r, tx_strm.avail_in);
459 exit_cleanup(RERR_STREAMIO);
460 }
Wayne Davison01f439e2005-02-14 08:28:00 +0000461 } while (toklen > 0);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000462 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000463}
464
Paul Mackerras861c20b1996-07-03 04:05:46 +0000465/* tells us what the receiver is in the middle of doing */
466static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
467
468/* for inflating stuff */
469static z_stream rx_strm;
470static char *cbuf;
471static char *dbuf;
472
473/* for decoding runs of tokens */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000474static int32 rx_token;
475static int32 rx_run;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000476
477/* Receive a deflated token and inflate it */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000478static int32 recv_deflated_token(int f, char **data)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000479{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000480 static int init_done;
Wayne Davisonacc461c2005-02-14 08:19:32 +0000481 static int32 saved_flag;
482 int32 n, flag;
483 int r;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000484
Paul Mackerras5914bf11998-05-22 06:58:52 +0000485 for (;;) {
486 switch (recv_state) {
487 case r_init:
488 if (!init_done) {
489 rx_strm.next_out = NULL;
490 rx_strm.zalloc = NULL;
491 rx_strm.zfree = NULL;
492 if (inflateInit2(&rx_strm, -15) != Z_OK) {
493 rprintf(FERROR, "inflate init failed\n");
Wayne Davisone4c598c2009-11-16 12:35:17 -0800494 exit_cleanup(RERR_PROTOCOL);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000495 }
Wayne Davison58cadc82003-12-06 21:07:27 +0000496 if (!(cbuf = new_array(char, MAX_DATA_COUNT))
497 || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
Paul Mackerras5914bf11998-05-22 06:58:52 +0000498 out_of_memory("recv_deflated_token");
499 init_done = 1;
500 } else {
501 inflateReset(&rx_strm);
502 }
Paul Mackerrasb8d45241996-11-06 04:49:53 +0000503 recv_state = r_idle;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000504 rx_token = 0;
505 break;
506
507 case r_idle:
508 case r_inflated:
509 if (saved_flag) {
510 flag = saved_flag & 0xff;
511 saved_flag = 0;
512 } else
513 flag = read_byte(f);
514 if ((flag & 0xC0) == DEFLATED_DATA) {
515 n = ((flag & 0x3f) << 8) + read_byte(f);
516 read_buf(f, cbuf, n);
517 rx_strm.next_in = (Bytef *)cbuf;
518 rx_strm.avail_in = n;
519 recv_state = r_inflating;
520 break;
521 }
522 if (recv_state == r_inflated) {
523 /* check previous inflated stuff ended correctly */
524 rx_strm.avail_in = 0;
525 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000526 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000527 r = inflate(&rx_strm, Z_SYNC_FLUSH);
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000528 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000529 /*
530 * Z_BUF_ERROR just means no progress was
531 * made, i.e. the decompressor didn't have
532 * any pending output for us.
533 */
534 if (r != Z_OK && r != Z_BUF_ERROR) {
535 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
536 r, n);
Andrew Tridgell65417571998-11-03 07:08:27 +0000537 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000538 }
539 if (n != 0 && r != Z_BUF_ERROR) {
540 /* have to return some more data and
541 save the flag for later. */
542 saved_flag = flag + 0x10000;
543 *data = dbuf;
544 return n;
545 }
546 /*
547 * At this point the decompressor should
548 * be expecting to see the 0, 0, ff, ff bytes.
549 */
550 if (!inflateSyncPoint(&rx_strm)) {
551 rprintf(FERROR, "decompressor lost sync!\n");
Andrew Tridgell65417571998-11-03 07:08:27 +0000552 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000553 }
554 rx_strm.avail_in = 4;
555 rx_strm.next_in = (Bytef *)cbuf;
556 cbuf[0] = cbuf[1] = 0;
557 cbuf[2] = cbuf[3] = 0xff;
558 inflate(&rx_strm, Z_SYNC_FLUSH);
559 recv_state = r_idle;
560 }
561 if (flag == END_FLAG) {
562 /* that's all folks */
563 recv_state = r_init;
564 return 0;
565 }
566
567 /* here we have a token of some kind */
568 if (flag & TOKEN_REL) {
569 rx_token += flag & 0x3f;
570 flag >>= 6;
571 } else
572 rx_token = read_int(f);
573 if (flag & 1) {
574 rx_run = read_byte(f);
575 rx_run += read_byte(f) << 8;
576 recv_state = r_running;
577 }
578 return -1 - rx_token;
579
580 case r_inflating:
581 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000582 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000583 r = inflate(&rx_strm, Z_NO_FLUSH);
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000584 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000585 if (r != Z_OK) {
586 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
Andrew Tridgell65417571998-11-03 07:08:27 +0000587 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000588 }
589 if (rx_strm.avail_in == 0)
590 recv_state = r_inflated;
591 if (n != 0) {
592 *data = dbuf;
593 return n;
594 }
595 break;
596
597 case r_running:
598 ++rx_token;
599 if (--rx_run == 0)
600 recv_state = r_idle;
601 return -1 - rx_token;
Paul Mackerrasb8d45241996-11-06 04:49:53 +0000602 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000603 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000604}
605
606/*
607 * put the data corresponding to a token that we've just returned
608 * from recv_deflated_token into the decompressor's history buffer.
609 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000610static void see_deflate_token(char *buf, int32 len)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000611{
Wayne Davisonacc461c2005-02-14 08:19:32 +0000612 int r;
613 int32 blklen;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000614 unsigned char hdr[5];
Paul Mackerras861c20b1996-07-03 04:05:46 +0000615
Paul Mackerras5914bf11998-05-22 06:58:52 +0000616 rx_strm.avail_in = 0;
617 blklen = 0;
618 hdr[0] = 0;
619 do {
620 if (rx_strm.avail_in == 0 && len != 0) {
621 if (blklen == 0) {
622 /* Give it a fake stored-block header. */
623 rx_strm.next_in = (Bytef *)hdr;
624 rx_strm.avail_in = 5;
625 blklen = len;
626 if (blklen > 0xffff)
627 blklen = 0xffff;
628 hdr[1] = blklen;
629 hdr[2] = blklen >> 8;
630 hdr[3] = ~hdr[1];
631 hdr[4] = ~hdr[2];
632 } else {
633 rx_strm.next_in = (Bytef *)buf;
634 rx_strm.avail_in = blklen;
Wayne Davisoncbdff742011-11-21 09:13:11 -0800635 if (protocol_version >= 31) /* Newer protocols avoid a data-duplicating bug */
636 buf += blklen;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000637 len -= blklen;
638 blklen = 0;
639 }
640 }
641 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000642 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000643 r = inflate(&rx_strm, Z_SYNC_FLUSH);
Wayne Davison4286ea62009-12-21 10:13:52 -0800644 if (r != Z_OK && r != Z_BUF_ERROR) {
Paul Mackerras5914bf11998-05-22 06:58:52 +0000645 rprintf(FERROR, "inflate (token) returned %d\n", r);
Andrew Tridgell65417571998-11-03 07:08:27 +0000646 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000647 }
648 } while (len || rx_strm.avail_out == 0);
Paul Mackerras861c20b1996-07-03 04:05:46 +0000649}
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000650
Martin Pool79f671c2002-04-08 08:35:30 +0000651/**
652 * Transmit a verbatim buffer of length @p n followed by a token.
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000653 * If token == -1 then we have reached EOF
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000654 * If n == 0 then don't send a buffer
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000655 */
Wayne Davisonacc461c2005-02-14 08:19:32 +0000656void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000657 int32 n, int32 toklen)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000658{
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000659 if (!do_compression)
660 simple_send_token(f, token, buf, offset, n);
661 else
Paul Mackerras5914bf11998-05-22 06:58:52 +0000662 send_deflated_token(f, token, buf, offset, n, toklen);
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000663}
664
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000665/*
Wayne Davisond2970212020-04-15 17:42:23 -0700666 * receive a token or buffer from the other end. If the return value is >0 then
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000667 * it is a data buffer of that length, and *data will point at the data.
668 * if the return value is -i then it represents token i-1
669 * if the return value is 0 then the end has been reached
670 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000671int32 recv_token(int f, char **data)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000672{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000673 int tok;
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000674
Wayne Davison4496e0e2020-05-24 18:08:09 -0700675 if (!do_compression)
Paul Mackerras5914bf11998-05-22 06:58:52 +0000676 tok = simple_recv_token(f,data);
Wayne Davison4496e0e2020-05-24 18:08:09 -0700677 else /* CPRES_ZLIB & CPRES_ZLIBX */
Paul Mackerras5914bf11998-05-22 06:58:52 +0000678 tok = recv_deflated_token(f, data);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000679 return tok;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000680}
681
682/*
683 * look at the data corresponding to a token, if necessary
684 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000685void see_token(char *data, int32 toklen)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000686{
Wayne Davison4496e0e2020-05-24 18:08:09 -0700687 if (do_compression == CPRES_ZLIB)
Paul Mackerras5914bf11998-05-22 06:58:52 +0000688 see_deflate_token(data, toklen);
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000689}