blob: c108b3af576e1a8c343b062e3552627ea7aa0373 [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 Davisonc3b553a2022-01-15 17:21:01 -08006 * Copyright (C) 2003-2022 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>
Wayne Davison4aaadc22020-05-25 13:31:30 -070025#ifdef SUPPORT_ZSTD
26#include <zstd.h>
27#endif
Wayne Davisonc394e862020-05-25 13:43:25 -070028#ifdef SUPPORT_LZ4
29#include <lz4.h>
30#endif
Wayne Davison7da17142011-11-21 09:22:14 -080031
Andrew Tridgell70d794d1996-07-01 05:55:05 +000032extern int do_compression;
Wayne Davisoncbdff742011-11-21 09:13:11 -080033extern int protocol_version;
Wayne Davisond67c8bd2004-06-18 16:22:14 +000034extern int module_id;
Wayne Davisond999efe2020-05-25 10:47:57 -070035extern int do_compression_level;
Wayne Davison6e058b42007-07-14 19:24:35 +000036extern char *skip_compress;
Wayne Davisond67c8bd2004-06-18 16:22:14 +000037
Wayne Davison64d5ea32020-05-24 15:45:59 -070038#ifndef Z_INSERT_ONLY
39#define Z_INSERT_ONLY Z_SYNC_FLUSH
40#endif
41
Wayne Davison4c9fdb92020-06-02 17:58:24 -070042static int skip_compression_level; /* The least possible compressing for handling skip-compress files. */
43static int per_file_default_level; /* The default level that each new file gets prior to checking its suffix. */
Andrew Tridgell70d794d1996-07-01 05:55:05 +000044
Wayne Davison6e058b42007-07-14 19:24:35 +000045struct suffix_tree {
46 struct suffix_tree *sibling;
47 struct suffix_tree *child;
48 char letter, word_end;
49};
50
51static char *match_list;
52static struct suffix_tree *suftree;
53
Wayne Davisond999efe2020-05-25 10:47:57 -070054void init_compression_level(void)
55{
56 int min_level, max_level, def_level, off_level;
57
58 switch (do_compression) {
Wayne Davison778f0df2020-06-04 16:17:12 -070059 case CPRES_NONE:
Wayne Davison7dbbde82020-06-07 18:58:30 -070060 return;
Wayne Davisond999efe2020-05-25 10:47:57 -070061 case CPRES_ZLIB:
62 case CPRES_ZLIBX:
63 min_level = 1;
64 max_level = Z_BEST_COMPRESSION;
65 def_level = 6; /* Z_DEFAULT_COMPRESSION is -1, so set it to the real default */
Wayne Davison4c9fdb92020-06-02 17:58:24 -070066 off_level = skip_compression_level = Z_NO_COMPRESSION;
Wayne Davisond999efe2020-05-25 10:47:57 -070067 if (do_compression_level == Z_DEFAULT_COMPRESSION)
68 do_compression_level = def_level;
69 break;
Wayne Davison4aaadc22020-05-25 13:31:30 -070070#ifdef SUPPORT_ZSTD
71 case CPRES_ZSTD:
Wayne Davison4c9fdb92020-06-02 17:58:24 -070072 min_level = skip_compression_level = ZSTD_minCLevel();
Wayne Davison4aaadc22020-05-25 13:31:30 -070073 max_level = ZSTD_maxCLevel();
Wayne Davison7dbbde82020-06-07 18:58:30 -070074 def_level = ZSTD_CLEVEL_DEFAULT;
Wayne Davison4aaadc22020-05-25 13:31:30 -070075 off_level = CLVL_NOT_SPECIFIED;
Wayne Davison7dbbde82020-06-07 18:58:30 -070076 if (do_compression_level == 0)
77 do_compression_level = def_level;
Wayne Davison4aaadc22020-05-25 13:31:30 -070078 break;
79#endif
Wayne Davisonc394e862020-05-25 13:43:25 -070080#ifdef SUPPORT_LZ4
81 case CPRES_LZ4:
Wayne Davison4c9fdb92020-06-02 17:58:24 -070082 min_level = skip_compression_level = 0;
Wayne Davisonc394e862020-05-25 13:43:25 -070083 max_level = 0;
84 def_level = 0;
85 off_level = CLVL_NOT_SPECIFIED;
86 break;
87#endif
Wayne Davisond999efe2020-05-25 10:47:57 -070088 default: /* paranoia to prevent missing case values */
Wayne Davisonab110fc2020-07-08 12:19:16 -070089 NOISY_DEATH("Unknown do_compression value");
Wayne Davisond999efe2020-05-25 10:47:57 -070090 }
91
Wayne Davisonabef92c2020-05-25 13:02:56 -070092 if (do_compression_level == CLVL_NOT_SPECIFIED)
93 do_compression_level = def_level;
94 else if (do_compression_level == off_level) {
Wayne Davisond999efe2020-05-25 10:47:57 -070095 do_compression = CPRES_NONE;
96 return;
97 }
98
99 /* We don't bother with any errors or warnings -- just make sure that the values are valid. */
Wayne Davisonabef92c2020-05-25 13:02:56 -0700100 if (do_compression_level < min_level)
Wayne Davisond999efe2020-05-25 10:47:57 -0700101 do_compression_level = min_level;
102 else if (do_compression_level > max_level)
103 do_compression_level = max_level;
104}
105
Wayne Davison6e058b42007-07-14 19:24:35 +0000106static void add_suffix(struct suffix_tree **prior, char ltr, const char *str)
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000107{
Wayne Davison6e058b42007-07-14 19:24:35 +0000108 struct suffix_tree *node, *newnode;
109
110 if (ltr == '[') {
111 const char *after = strchr(str, ']');
Wayne Davison2f1fb732009-09-05 10:25:42 -0700112 /* Treat "[foo" and "[]" as having a literal '['. */
113 if (after && after++ != str+1) {
114 while ((ltr = *str++) != ']')
115 add_suffix(prior, ltr, after);
Wayne Davison6e058b42007-07-14 19:24:35 +0000116 return;
Wayne Davison2f1fb732009-09-05 10:25:42 -0700117 }
Wayne Davison6e058b42007-07-14 19:24:35 +0000118 }
119
120 for (node = *prior; node; prior = &node->sibling, node = node->sibling) {
121 if (node->letter == ltr) {
122 if (*str)
123 add_suffix(&node->child, *str, str+1);
124 else
125 node->word_end = 1;
126 return;
127 }
128 if (node->letter > ltr)
129 break;
130 }
Wayne Davison11eb67e2020-06-25 19:59:19 -0700131 newnode = new(struct suffix_tree);
Wayne Davison6e058b42007-07-14 19:24:35 +0000132 newnode->sibling = node;
133 newnode->child = NULL;
134 newnode->letter = ltr;
135 *prior = newnode;
136 if (*str) {
137 add_suffix(&newnode->child, *str, str+1);
138 newnode->word_end = 0;
139 } else
140 newnode->word_end = 1;
141}
142
143static void add_nocompress_suffixes(const char *str)
144{
145 char *buf, *t;
146 const char *f = str;
147
Wayne Davison11eb67e2020-06-25 19:59:19 -0700148 buf = new_array(char, strlen(f) + 1);
Wayne Davison6e058b42007-07-14 19:24:35 +0000149
150 while (*f) {
151 if (*f == '/') {
152 f++;
153 continue;
154 }
155
156 t = buf;
157 do {
158 if (isUpper(f))
159 *t++ = toLower(f);
160 else
161 *t++ = *f;
162 } while (*++f != '/' && *f);
163 *t++ = '\0';
164
Wayne Davison6e058b42007-07-14 19:24:35 +0000165 add_suffix(&suftree, *buf, buf+1);
166 }
167
168 free(buf);
169}
170
171static void init_set_compression(void)
172{
173 const char *f;
174 char *t, *start;
175
176 if (skip_compress)
177 add_nocompress_suffixes(skip_compress);
178
179 /* A non-daemon transfer skips the default suffix list if the
180 * user specified --skip-compress. */
181 if (skip_compress && module_id < 0)
182 f = "";
183 else
184 f = lp_dont_compress(module_id);
185
Wayne Davison11eb67e2020-06-25 19:59:19 -0700186 match_list = t = new_array(char, strlen(f) + 2);
Wayne Davison6e058b42007-07-14 19:24:35 +0000187
Wayne Davisond999efe2020-05-25 10:47:57 -0700188 per_file_default_level = do_compression_level;
Wayne Davison6e058b42007-07-14 19:24:35 +0000189
190 while (*f) {
191 if (*f == ' ') {
192 f++;
193 continue;
194 }
195
196 start = t;
197 do {
198 if (isUpper(f))
199 *t++ = toLower(f);
200 else
201 *t++ = *f;
202 } while (*++f != ' ' && *f);
203 *t++ = '\0';
204
205 if (t - start == 1+1 && *start == '*') {
206 /* Optimize a match-string of "*". */
207 *match_list = '\0';
208 suftree = NULL;
Wayne Davison4c9fdb92020-06-02 17:58:24 -0700209 per_file_default_level = skip_compression_level;
Wayne Davison6e058b42007-07-14 19:24:35 +0000210 break;
211 }
212
213 /* Move *.foo items into the stuffix tree. */
214 if (*start == '*' && start[1] == '.' && start[2]
Wayne Davison0d585182007-07-14 19:36:52 +0000215 && !strpbrk(start+2, ".?*")) {
Wayne Davison6e058b42007-07-14 19:24:35 +0000216 add_suffix(&suftree, start[2], start+3);
217 t = start;
218 }
219 }
220 *t++ = '\0';
221}
222
223/* determine the compression level based on a wildcard filename list */
224void set_compression(const char *fname)
225{
Wayne Davisonc11467a2021-12-31 10:58:19 -0800226#if 0 /* No compression algorithms currently allow mid-stream changing of the level. */
Wayne Davison6e058b42007-07-14 19:24:35 +0000227 const struct suffix_tree *node;
228 const char *s;
229 char ltr;
Wayne Davisonc11467a2021-12-31 10:58:19 -0800230#endif
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000231
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000232 if (!do_compression)
233 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000234
Wayne Davison6e058b42007-07-14 19:24:35 +0000235 if (!match_list)
236 init_set_compression();
David Dykstra63f07741998-11-25 15:37:50 +0000237
Wayne Davisonc11467a2021-12-31 10:58:19 -0800238#if 0
Wayne Davisonec497df2005-12-30 07:19:16 +0000239 compression_level = per_file_default_level;
Wayne Davison0fe987e2005-12-30 06:03:40 +0000240
Wayne Davison6e058b42007-07-14 19:24:35 +0000241 if (!*match_list && !suftree)
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000242 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000243
Wayne Davisonec497df2005-12-30 07:19:16 +0000244 if ((s = strrchr(fname, '/')) != NULL)
245 fname = s + 1;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000246
Wayne Davisonec497df2005-12-30 07:19:16 +0000247 for (s = match_list; *s; s += strlen(s) + 1) {
248 if (iwildmatch(s, fname)) {
Wayne Davison4c9fdb92020-06-02 17:58:24 -0700249 compression_level = skip_compression_level;
Wayne Davison6e058b42007-07-14 19:24:35 +0000250 return;
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000251 }
252 }
Wayne Davison6e058b42007-07-14 19:24:35 +0000253
254 if (!(node = suftree) || !(s = strrchr(fname, '.'))
255 || s == fname || !(ltr = *++s))
256 return;
257
258 while (1) {
Wayne Davison3be1d9b2010-06-19 09:47:00 -0700259 if (isUpper(&ltr))
260 ltr = toLower(&ltr);
Wayne Davison6e058b42007-07-14 19:24:35 +0000261 while (node->letter != ltr) {
262 if (node->letter > ltr)
263 return;
264 if (!(node = node->sibling))
265 return;
266 }
267 if ((ltr = *++s) == '\0') {
268 if (node->word_end)
Wayne Davison4c9fdb92020-06-02 17:58:24 -0700269 compression_level = skip_compression_level;
Wayne Davison6e058b42007-07-14 19:24:35 +0000270 return;
271 }
272 if (!(node = node->child))
273 return;
274 }
Wayne Davisonc11467a2021-12-31 10:58:19 -0800275#else
276 (void)fname;
277#endif
Andrew Tridgell83fff1a1998-11-20 22:26:29 +0000278}
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000279
280/* non-compressing recv token */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000281static int32 simple_recv_token(int f, char **data)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000282{
Wayne Davisonacc461c2005-02-14 08:19:32 +0000283 static int32 residue;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000284 static char *buf;
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000285 int32 n;
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000286
Wayne Davison11eb67e2020-06-25 19:59:19 -0700287 if (!buf)
Wayne Davison58cadc82003-12-06 21:07:27 +0000288 buf = new_array(char, CHUNK_SIZE);
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000289
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000290 if (residue == 0) {
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000291 int32 i = read_int(f);
Wayne Davisond67c8bd2004-06-18 16:22:14 +0000292 if (i <= 0)
293 return i;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000294 residue = i;
295 }
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000296
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000297 *data = buf;
298 n = MIN(CHUNK_SIZE,residue);
299 residue -= n;
300 read_buf(f,buf,n);
301 return n;
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000302}
303
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000304/* non-compressing send token */
Wayne Davisone63ff702020-06-13 18:19:12 -0700305static void simple_send_token(int f, int32 token, struct map_struct *buf, OFF_T offset, int32 n)
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000306{
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000307 if (n > 0) {
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000308 int32 len = 0;
309 while (len < n) {
310 int32 n1 = MIN(CHUNK_SIZE, n-len);
311 write_int(f, n1);
312 write_buf(f, map_ptr(buf, offset+len, n1), n1);
313 len += n1;
Andrew Tridgellc5eb3651998-05-21 05:57:15 +0000314 }
315 }
Andrew Tridgell45f133b1998-05-22 01:53:02 +0000316 /* a -2 token means to send data only and no token */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000317 if (token != -2)
318 write_int(f, -(token+1));
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000319}
320
Paul Mackerras861c20b1996-07-03 04:05:46 +0000321/* Flag bytes in compressed stream are encoded as follows: */
322#define END_FLAG 0 /* that's all folks */
323#define TOKEN_LONG 0x20 /* followed by 32-bit token number */
324#define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
325#define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
326#define TOKEN_REL 0x80 /* + 6-bit relative token number */
327#define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
328
329#define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
330
Wayne Davison24733912003-07-04 07:47:09 +0000331/* zlib.h says that if we want to be able to compress something in a single
332 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
333 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
334 * to ensure that this is a compile-time value). */
335#define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
336
Paul Mackerras861c20b1996-07-03 04:05:46 +0000337/* For coding runs of tokens */
Wayne Davisonacc461c2005-02-14 08:19:32 +0000338static int32 last_token = -1;
339static int32 run_start;
340static int32 last_run_end;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000341
342/* Deflation state */
343static z_stream tx_strm;
344
345/* Output buffer */
Andrew Tridgell3a6a3661998-03-23 12:52:57 +0000346static char *obuf;
Wayne Davison24733912003-07-04 07:47:09 +0000347
348/* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
349 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
350#if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
351#define OBUF_SIZE (MAX_DATA_COUNT+2)
352#else
353#define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
354#endif
Paul Mackerras861c20b1996-07-03 04:05:46 +0000355
356/* Send a deflated token */
357static void
Wayne Davisone63ff702020-06-13 18:19:12 -0700358send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset, int32 nb, int32 toklen)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000359{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000360 static int init_done, flush_pending;
Wayne Davison4496e0e2020-05-24 18:08:09 -0700361 int32 n, r;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000362
Paul Mackerras5914bf11998-05-22 06:58:52 +0000363 if (last_token == -1) {
364 /* initialization */
365 if (!init_done) {
366 tx_strm.next_in = NULL;
367 tx_strm.zalloc = NULL;
368 tx_strm.zfree = NULL;
Wayne Davisonc11467a2021-12-31 10:58:19 -0800369 if (deflateInit2(&tx_strm, per_file_default_level,
Paul Mackerras5914bf11998-05-22 06:58:52 +0000370 Z_DEFLATED, -15, 8,
371 Z_DEFAULT_STRATEGY) != Z_OK) {
372 rprintf(FERROR, "compression init failed\n");
Wayne Davisone4c598c2009-11-16 12:35:17 -0800373 exit_cleanup(RERR_PROTOCOL);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000374 }
Wayne Davison11eb67e2020-06-25 19:59:19 -0700375 obuf = new_array(char, OBUF_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000376 init_done = 1;
377 } else
378 deflateReset(&tx_strm);
379 last_run_end = 0;
380 run_start = token;
381 flush_pending = 0;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000382 } else if (last_token == -2) {
383 run_start = token;
Wayne Davison85e62c32020-07-04 15:27:47 -0700384 } else if (nb != 0 || token != last_token + 1 || token >= run_start + 65536) {
Paul Mackerras5914bf11998-05-22 06:58:52 +0000385 /* output previous run */
386 r = run_start - last_run_end;
387 n = last_token - run_start;
388 if (r >= 0 && r <= 63) {
389 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
390 } else {
391 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
392 write_int(f, run_start);
Paul Mackerras861c20b1996-07-03 04:05:46 +0000393 }
Paul Mackerras5914bf11998-05-22 06:58:52 +0000394 if (n != 0) {
395 write_byte(f, n);
396 write_byte(f, n >> 8);
397 }
398 last_run_end = last_token;
399 run_start = token;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000400 }
401
Paul Mackerras5914bf11998-05-22 06:58:52 +0000402 last_token = token;
403
404 if (nb != 0 || flush_pending) {
405 /* deflate the data starting at offset */
406 int flush = Z_NO_FLUSH;
407 tx_strm.avail_in = 0;
408 tx_strm.avail_out = 0;
409 do {
410 if (tx_strm.avail_in == 0 && nb != 0) {
411 /* give it some more input */
412 n = MIN(nb, CHUNK_SIZE);
413 tx_strm.next_in = (Bytef *)
414 map_ptr(buf, offset, n);
415 tx_strm.avail_in = n;
416 nb -= n;
417 offset += n;
418 }
419 if (tx_strm.avail_out == 0) {
420 tx_strm.next_out = (Bytef *)(obuf + 2);
421 tx_strm.avail_out = MAX_DATA_COUNT;
422 if (flush != Z_NO_FLUSH) {
423 /*
424 * We left the last 4 bytes in the
425 * buffer, in case they are the
426 * last 4. Move them to the front.
427 */
Wayne Davisone63ff702020-06-13 18:19:12 -0700428 memcpy(tx_strm.next_out, obuf+MAX_DATA_COUNT-2, 4);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000429 tx_strm.next_out += 4;
430 tx_strm.avail_out -= 4;
431 }
432 }
433 if (nb == 0 && token != -2)
434 flush = Z_SYNC_FLUSH;
435 r = deflate(&tx_strm, flush);
436 if (r != Z_OK) {
437 rprintf(FERROR, "deflate returned %d\n", r);
Andrew Tridgell65417571998-11-03 07:08:27 +0000438 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000439 }
440 if (nb == 0 || tx_strm.avail_out == 0) {
441 n = MAX_DATA_COUNT - tx_strm.avail_out;
442 if (flush != Z_NO_FLUSH) {
443 /*
444 * We have to trim off the last 4
445 * bytes of output when flushing
446 * (they are just 0, 0, ff, ff).
447 */
448 n -= 4;
449 }
450 if (n > 0) {
451 obuf[0] = DEFLATED_DATA + (n >> 8);
452 obuf[1] = n;
453 write_buf(f, obuf, n+2);
454 }
455 }
456 } while (nb != 0 || tx_strm.avail_out == 0);
457 flush_pending = token == -2;
458 }
459
460 if (token == -1) {
461 /* end of file - clean up */
462 write_byte(f, END_FLAG);
Wayne Davison4496e0e2020-05-24 18:08:09 -0700463 } else if (token != -2 && do_compression == CPRES_ZLIB) {
Wayne Davisonacc461c2005-02-14 08:19:32 +0000464 /* Add the data in the current block to the compressor's
465 * history and hash table. */
Wayne Davison01f439e2005-02-14 08:28:00 +0000466 do {
467 /* Break up long sections in the same way that
468 * see_deflate_token() does. */
469 int32 n1 = toklen > 0xffff ? 0xffff : toklen;
470 toklen -= n1;
471 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n1);
472 tx_strm.avail_in = n1;
Wayne Davisoncbdff742011-11-21 09:13:11 -0800473 if (protocol_version >= 31) /* Newer protocols avoid a data-duplicating bug */
474 offset += n1;
Wayne Davison22a3ac02014-04-19 12:11:11 -0700475 tx_strm.next_out = (Bytef *) obuf;
476 tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
477 r = deflate(&tx_strm, Z_INSERT_ONLY);
478 if (r != Z_OK || tx_strm.avail_in != 0) {
479 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
480 r, tx_strm.avail_in);
481 exit_cleanup(RERR_STREAMIO);
482 }
Wayne Davison01f439e2005-02-14 08:28:00 +0000483 } while (toklen > 0);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000484 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000485}
486
Paul Mackerras861c20b1996-07-03 04:05:46 +0000487/* tells us what the receiver is in the middle of doing */
488static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
489
490/* for inflating stuff */
491static z_stream rx_strm;
492static char *cbuf;
493static char *dbuf;
494
495/* for decoding runs of tokens */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000496static int32 rx_token;
497static int32 rx_run;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000498
499/* Receive a deflated token and inflate it */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000500static int32 recv_deflated_token(int f, char **data)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000501{
Paul Mackerras5914bf11998-05-22 06:58:52 +0000502 static int init_done;
Wayne Davisonacc461c2005-02-14 08:19:32 +0000503 static int32 saved_flag;
504 int32 n, flag;
505 int r;
Paul Mackerras861c20b1996-07-03 04:05:46 +0000506
Paul Mackerras5914bf11998-05-22 06:58:52 +0000507 for (;;) {
508 switch (recv_state) {
509 case r_init:
510 if (!init_done) {
511 rx_strm.next_out = NULL;
512 rx_strm.zalloc = NULL;
513 rx_strm.zfree = NULL;
514 if (inflateInit2(&rx_strm, -15) != Z_OK) {
515 rprintf(FERROR, "inflate init failed\n");
Wayne Davisone4c598c2009-11-16 12:35:17 -0800516 exit_cleanup(RERR_PROTOCOL);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000517 }
Wayne Davison11eb67e2020-06-25 19:59:19 -0700518 cbuf = new_array(char, MAX_DATA_COUNT);
519 dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE));
Paul Mackerras5914bf11998-05-22 06:58:52 +0000520 init_done = 1;
521 } else {
522 inflateReset(&rx_strm);
523 }
Paul Mackerrasb8d45241996-11-06 04:49:53 +0000524 recv_state = r_idle;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000525 rx_token = 0;
526 break;
527
528 case r_idle:
529 case r_inflated:
530 if (saved_flag) {
531 flag = saved_flag & 0xff;
532 saved_flag = 0;
533 } else
534 flag = read_byte(f);
535 if ((flag & 0xC0) == DEFLATED_DATA) {
536 n = ((flag & 0x3f) << 8) + read_byte(f);
537 read_buf(f, cbuf, n);
538 rx_strm.next_in = (Bytef *)cbuf;
539 rx_strm.avail_in = n;
540 recv_state = r_inflating;
541 break;
542 }
543 if (recv_state == r_inflated) {
544 /* check previous inflated stuff ended correctly */
545 rx_strm.avail_in = 0;
546 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000547 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000548 r = inflate(&rx_strm, Z_SYNC_FLUSH);
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000549 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000550 /*
551 * Z_BUF_ERROR just means no progress was
552 * made, i.e. the decompressor didn't have
553 * any pending output for us.
554 */
555 if (r != Z_OK && r != Z_BUF_ERROR) {
556 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
557 r, n);
Andrew Tridgell65417571998-11-03 07:08:27 +0000558 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000559 }
560 if (n != 0 && r != Z_BUF_ERROR) {
561 /* have to return some more data and
562 save the flag for later. */
563 saved_flag = flag + 0x10000;
564 *data = dbuf;
565 return n;
566 }
567 /*
568 * At this point the decompressor should
569 * be expecting to see the 0, 0, ff, ff bytes.
570 */
571 if (!inflateSyncPoint(&rx_strm)) {
572 rprintf(FERROR, "decompressor lost sync!\n");
Andrew Tridgell65417571998-11-03 07:08:27 +0000573 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000574 }
575 rx_strm.avail_in = 4;
576 rx_strm.next_in = (Bytef *)cbuf;
577 cbuf[0] = cbuf[1] = 0;
Wayne Davison8f151112020-09-29 13:04:41 -0700578 cbuf[2] = cbuf[3] = (char)0xff;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000579 inflate(&rx_strm, Z_SYNC_FLUSH);
580 recv_state = r_idle;
581 }
582 if (flag == END_FLAG) {
583 /* that's all folks */
584 recv_state = r_init;
585 return 0;
586 }
587
588 /* here we have a token of some kind */
589 if (flag & TOKEN_REL) {
590 rx_token += flag & 0x3f;
591 flag >>= 6;
592 } else
593 rx_token = read_int(f);
594 if (flag & 1) {
595 rx_run = read_byte(f);
596 rx_run += read_byte(f) << 8;
597 recv_state = r_running;
598 }
599 return -1 - rx_token;
600
601 case r_inflating:
602 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000603 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000604 r = inflate(&rx_strm, Z_NO_FLUSH);
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000605 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000606 if (r != Z_OK) {
607 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
Andrew Tridgell65417571998-11-03 07:08:27 +0000608 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000609 }
610 if (rx_strm.avail_in == 0)
611 recv_state = r_inflated;
612 if (n != 0) {
613 *data = dbuf;
614 return n;
615 }
616 break;
617
618 case r_running:
619 ++rx_token;
620 if (--rx_run == 0)
621 recv_state = r_idle;
622 return -1 - rx_token;
Paul Mackerrasb8d45241996-11-06 04:49:53 +0000623 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000624 }
Paul Mackerras861c20b1996-07-03 04:05:46 +0000625}
626
627/*
628 * put the data corresponding to a token that we've just returned
629 * from recv_deflated_token into the decompressor's history buffer.
630 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +0000631static void see_deflate_token(char *buf, int32 len)
Paul Mackerras861c20b1996-07-03 04:05:46 +0000632{
Wayne Davisonacc461c2005-02-14 08:19:32 +0000633 int r;
634 int32 blklen;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000635 unsigned char hdr[5];
Paul Mackerras861c20b1996-07-03 04:05:46 +0000636
Paul Mackerras5914bf11998-05-22 06:58:52 +0000637 rx_strm.avail_in = 0;
638 blklen = 0;
639 hdr[0] = 0;
640 do {
641 if (rx_strm.avail_in == 0 && len != 0) {
642 if (blklen == 0) {
643 /* Give it a fake stored-block header. */
644 rx_strm.next_in = (Bytef *)hdr;
645 rx_strm.avail_in = 5;
646 blklen = len;
647 if (blklen > 0xffff)
648 blklen = 0xffff;
649 hdr[1] = blklen;
650 hdr[2] = blklen >> 8;
651 hdr[3] = ~hdr[1];
652 hdr[4] = ~hdr[2];
653 } else {
654 rx_strm.next_in = (Bytef *)buf;
655 rx_strm.avail_in = blklen;
Wayne Davisoncbdff742011-11-21 09:13:11 -0800656 if (protocol_version >= 31) /* Newer protocols avoid a data-duplicating bug */
657 buf += blklen;
Paul Mackerras5914bf11998-05-22 06:58:52 +0000658 len -= blklen;
659 blklen = 0;
660 }
661 }
662 rx_strm.next_out = (Bytef *)dbuf;
Wayne Davison1dbb94c2003-08-20 10:37:53 +0000663 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000664 r = inflate(&rx_strm, Z_SYNC_FLUSH);
Wayne Davison4286ea62009-12-21 10:13:52 -0800665 if (r != Z_OK && r != Z_BUF_ERROR) {
Paul Mackerras5914bf11998-05-22 06:58:52 +0000666 rprintf(FERROR, "inflate (token) returned %d\n", r);
Andrew Tridgell65417571998-11-03 07:08:27 +0000667 exit_cleanup(RERR_STREAMIO);
Paul Mackerras5914bf11998-05-22 06:58:52 +0000668 }
669 } while (len || rx_strm.avail_out == 0);
Paul Mackerras861c20b1996-07-03 04:05:46 +0000670}
Andrew Tridgell70d794d1996-07-01 05:55:05 +0000671
Wayne Davison4aaadc22020-05-25 13:31:30 -0700672#ifdef SUPPORT_ZSTD
673
674static ZSTD_inBuffer zstd_in_buff;
675static ZSTD_outBuffer zstd_out_buff;
676static ZSTD_CCtx *zstd_cctx;
677
Wayne Davisone63ff702020-06-13 18:19:12 -0700678static void send_zstd_token(int f, int32 token, struct map_struct *buf, OFF_T offset, int32 nb)
Wayne Davison4aaadc22020-05-25 13:31:30 -0700679{
680 static int comp_init_done, flush_pending;
681 ZSTD_EndDirective flush = ZSTD_e_continue;
682 int32 n, r;
683
684 /* initialization */
685 if (!comp_init_done) {
Wayne Davison4aaadc22020-05-25 13:31:30 -0700686 zstd_cctx = ZSTD_createCCtx();
687 if (!zstd_cctx) {
688 rprintf(FERROR, "compression init failed\n");
689 exit_cleanup(RERR_PROTOCOL);
690 }
691
Wayne Davisonc7f10de2020-05-28 11:40:52 -0700692 obuf = new_array(char, OBUF_SIZE);
Wayne Davison4aaadc22020-05-25 13:31:30 -0700693
Wayne Davisone63ff702020-06-13 18:19:12 -0700694 ZSTD_CCtx_setParameter(zstd_cctx, ZSTD_c_compressionLevel, do_compression_level);
Wayne Davison4aaadc22020-05-25 13:31:30 -0700695 zstd_out_buff.dst = obuf + 2;
696
697 comp_init_done = 1;
698 }
699
700 if (last_token == -1) {
701 last_run_end = 0;
702 run_start = token;
703 flush_pending = 0;
704 } else if (last_token == -2) {
705 run_start = token;
Wayne Davison85e62c32020-07-04 15:27:47 -0700706 } else if (nb != 0 || token != last_token + 1 || token >= run_start + 65536) {
Wayne Davison4aaadc22020-05-25 13:31:30 -0700707 /* output previous run */
708 r = run_start - last_run_end;
709 n = last_token - run_start;
710
711 if (r >= 0 && r <= 63) {
712 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
713 } else {
714 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
715 write_int(f, run_start);
716 }
717 if (n != 0) {
718 write_byte(f, n);
719 write_byte(f, n >> 8);
720 }
721 last_run_end = last_token;
722 run_start = token;
723 }
724
725 last_token = token;
726
727 if (nb || flush_pending) {
728
729 zstd_in_buff.src = map_ptr(buf, offset, nb);
730 zstd_in_buff.size = nb;
731 zstd_in_buff.pos = 0;
732
733 do {
734 if (zstd_out_buff.size == 0) {
735 zstd_out_buff.size = MAX_DATA_COUNT;
736 zstd_out_buff.pos = 0;
737 }
738
739 /* File ended, flush */
740 if (token != -2)
741 flush = ZSTD_e_flush;
742
743 r = ZSTD_compressStream2(zstd_cctx, &zstd_out_buff, &zstd_in_buff, flush);
744 if (ZSTD_isError(r)) {
745 rprintf(FERROR, "ZSTD_compressStream returned %d\n", r);
746 exit_cleanup(RERR_STREAMIO);
747 }
748
749 /*
750 * Nothing is sent if the buffer isn't full so avoid smaller
751 * transfers. If a file is finished then we flush the internal
752 * state and send a smaller buffer so that the remote side can
753 * finish the file.
754 */
755 if (zstd_out_buff.pos == zstd_out_buff.size || flush == ZSTD_e_flush) {
756 n = zstd_out_buff.pos;
757
758 obuf[0] = DEFLATED_DATA + (n >> 8);
759 obuf[1] = n;
760 write_buf(f, obuf, n+2);
761
762 zstd_out_buff.size = 0;
763 }
764 /*
765 * Loop while the input buffer isn't full consumed or the
766 * internal state isn't fully flushed.
767 */
768 } while (zstd_in_buff.pos < zstd_in_buff.size || r > 0);
769 flush_pending = token == -2;
770 }
771
772 if (token == -1) {
773 /* end of file - clean up */
774 write_byte(f, END_FLAG);
775 }
776}
777
778static ZSTD_DCtx *zstd_dctx;
779
780static int32 recv_zstd_token(int f, char **data)
781{
782 static int decomp_init_done;
783 static int out_buffer_size;
784 int32 n, flag;
785 int r;
786
787 if (!decomp_init_done) {
Wayne Davison4aaadc22020-05-25 13:31:30 -0700788 zstd_dctx = ZSTD_createDCtx();
789 if (!zstd_dctx) {
790 rprintf(FERROR, "ZSTD_createDStream failed\n");
791 exit_cleanup(RERR_PROTOCOL);
792 }
793
794 /* Output buffer fits two decompressed blocks */
795 out_buffer_size = ZSTD_DStreamOutSize() * 2;
796 cbuf = new_array(char, MAX_DATA_COUNT);
797 dbuf = new_array(char, out_buffer_size);
Wayne Davison4aaadc22020-05-25 13:31:30 -0700798
799 zstd_in_buff.src = cbuf;
800 zstd_out_buff.dst = dbuf;
801
802 decomp_init_done = 1;
803 }
804
Wayne Davison85e62c32020-07-04 15:27:47 -0700805 for (;;) {
806 switch (recv_state) {
807 case r_init:
808 recv_state = r_idle;
809 rx_token = 0;
810 break;
Wayne Davison4aaadc22020-05-25 13:31:30 -0700811
Wayne Davison85e62c32020-07-04 15:27:47 -0700812 case r_idle:
813 flag = read_byte(f);
814 if ((flag & 0xC0) == DEFLATED_DATA) {
815 n = ((flag & 0x3f) << 8) + read_byte(f);
816 read_buf(f, cbuf, n);
Wayne Davison4aaadc22020-05-25 13:31:30 -0700817
Wayne Davison85e62c32020-07-04 15:27:47 -0700818 zstd_in_buff.size = n;
819 zstd_in_buff.pos = 0;
Wayne Davison4aaadc22020-05-25 13:31:30 -0700820
Wayne Davison85e62c32020-07-04 15:27:47 -0700821 recv_state = r_inflating;
822 break;
823 }
Wayne Davison4aaadc22020-05-25 13:31:30 -0700824
Wayne Davison85e62c32020-07-04 15:27:47 -0700825 if (flag == END_FLAG) {
826 /* that's all folks */
827 recv_state = r_init;
828 return 0;
829 }
Wayne Davison4aaadc22020-05-25 13:31:30 -0700830 /* here we have a token of some kind */
831 if (flag & TOKEN_REL) {
832 rx_token += flag & 0x3f;
833 flag >>= 6;
834 } else
835 rx_token = read_int(f);
836 if (flag & 1) {
837 rx_run = read_byte(f);
838 rx_run += read_byte(f) << 8;
839 recv_state = r_running;
840 }
841 return -1 - rx_token;
Wayne Davison85e62c32020-07-04 15:27:47 -0700842
843 case r_inflated: /* zstd doesn't get into this state */
844 break;
845
846 case r_inflating:
847 zstd_out_buff.size = out_buffer_size;
848 zstd_out_buff.pos = 0;
849
850 r = ZSTD_decompressStream(zstd_dctx, &zstd_out_buff, &zstd_in_buff);
851 n = zstd_out_buff.pos;
852 if (ZSTD_isError(r)) {
853 rprintf(FERROR, "ZSTD decomp returned %d (%d bytes)\n", r, n);
854 exit_cleanup(RERR_STREAMIO);
855 }
856
857 /*
858 * If the input buffer is fully consumed and the output
859 * buffer is not full then next step is to read more
860 * data.
861 */
862 if (zstd_in_buff.size == zstd_in_buff.pos && n < out_buffer_size)
863 recv_state = r_idle;
864
865 if (n != 0) {
866 *data = dbuf;
867 return n;
868 }
869 break;
870
871 case r_running:
872 ++rx_token;
873 if (--rx_run == 0)
874 recv_state = r_idle;
875 return -1 - rx_token;
Wayne Davison4aaadc22020-05-25 13:31:30 -0700876 }
Wayne Davison4aaadc22020-05-25 13:31:30 -0700877 }
Wayne Davison4aaadc22020-05-25 13:31:30 -0700878}
Wayne Davisonc394e862020-05-25 13:43:25 -0700879#endif /* SUPPORT_ZSTD */
880
881#ifdef SUPPORT_LZ4
882static void
883send_compressed_token(int f, int32 token, struct map_struct *buf, OFF_T offset, int32 nb)
884{
885 static int init_done, flush_pending;
886 int size = MAX(LZ4_compressBound(CHUNK_SIZE), MAX_DATA_COUNT+2);
887 int32 n, r;
888
889 if (last_token == -1) {
890 if (!init_done) {
Wayne Davison11eb67e2020-06-25 19:59:19 -0700891 obuf = new_array(char, size);
Wayne Davisonc394e862020-05-25 13:43:25 -0700892 init_done = 1;
893 }
894 last_run_end = 0;
895 run_start = token;
896 flush_pending = 0;
897 } else if (last_token == -2) {
898 run_start = token;
Wayne Davison85e62c32020-07-04 15:27:47 -0700899 } else if (nb != 0 || token != last_token + 1 || token >= run_start + 65536) {
Wayne Davisonc394e862020-05-25 13:43:25 -0700900 /* output previous run */
901 r = run_start - last_run_end;
902 n = last_token - run_start;
903 if (r >= 0 && r <= 63) {
904 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
905 } else {
906 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
907 write_int(f, run_start);
908 }
909 if (n != 0) {
910 write_byte(f, n);
911 write_byte(f, n >> 8);
912 }
913 last_run_end = last_token;
914 run_start = token;
915 }
916
917 last_token = token;
918
919 if (nb != 0 || flush_pending) {
920 int available_in, available_out = 0;
921 const char *next_in;
922
923 do {
Wayne Davisonc394e862020-05-25 13:43:25 -0700924 char *next_out = obuf + 2;
925
926 if (available_out == 0) {
927 available_in = MIN(nb, MAX_DATA_COUNT);
928 next_in = map_ptr(buf, offset, available_in);
929 } else
930 available_in /= 2;
931
Wayne Davisonc7f10de2020-05-28 11:40:52 -0700932 available_out = LZ4_compress_default(next_in, next_out, available_in, size - 2);
Wayne Davisonc394e862020-05-25 13:43:25 -0700933 if (!available_out) {
934 rprintf(FERROR, "compress returned %d\n", available_out);
935 exit_cleanup(RERR_STREAMIO);
936 }
937 if (available_out <= MAX_DATA_COUNT) {
Wayne Davison2bee3072020-07-04 15:58:56 -0700938 obuf[0] = DEFLATED_DATA + (available_out >> 8);
939 obuf[1] = available_out;
Wayne Davisonc394e862020-05-25 13:43:25 -0700940
Wayne Davison2bee3072020-07-04 15:58:56 -0700941 write_buf(f, obuf, available_out + 2);
Wayne Davisonc394e862020-05-25 13:43:25 -0700942
943 available_out = 0;
944 nb -= available_in;
945 offset += available_in;
946 }
947 } while (nb != 0);
948 flush_pending = token == -2;
949 }
Wayne Davison2bee3072020-07-04 15:58:56 -0700950 if (token == -1) {
Wayne Davisonc394e862020-05-25 13:43:25 -0700951 /* end of file - clean up */
952 write_byte(f, END_FLAG);
Wayne Davison2bee3072020-07-04 15:58:56 -0700953 }
Wayne Davisonc394e862020-05-25 13:43:25 -0700954}
955
956static int32 recv_compressed_token(int f, char **data)
957{
Wayne Davisonc394e862020-05-25 13:43:25 -0700958 static int init_done;
959 int32 n, flag;
960 int size = MAX(LZ4_compressBound(CHUNK_SIZE), MAX_DATA_COUNT+2);
961 static const char *next_in;
962 static int avail_in;
963 int avail_out;
964
965 for (;;) {
966 switch (recv_state) {
967 case r_init:
968 if (!init_done) {
Wayne Davison11eb67e2020-06-25 19:59:19 -0700969 cbuf = new_array(char, MAX_DATA_COUNT);
970 dbuf = new_array(char, size);
Wayne Davisonc394e862020-05-25 13:43:25 -0700971 init_done = 1;
972 }
973 recv_state = r_idle;
974 rx_token = 0;
975 break;
Wayne Davison2bee3072020-07-04 15:58:56 -0700976
Wayne Davisonc394e862020-05-25 13:43:25 -0700977 case r_idle:
Wayne Davison2bee3072020-07-04 15:58:56 -0700978 flag = read_byte(f);
Wayne Davisonc394e862020-05-25 13:43:25 -0700979 if ((flag & 0xC0) == DEFLATED_DATA) {
980 n = ((flag & 0x3f) << 8) + read_byte(f);
981 read_buf(f, cbuf, n);
982 next_in = (char *)cbuf;
983 avail_in = n;
984 recv_state = r_inflating;
985 break;
986 }
987
Wayne Davisonc394e862020-05-25 13:43:25 -0700988 if (flag == END_FLAG) {
989 /* that's all folks */
990 recv_state = r_init;
991 return 0;
992 }
993
994 /* here we have a token of some kind */
995 if (flag & TOKEN_REL) {
996 rx_token += flag & 0x3f;
997 flag >>= 6;
998 } else
999 rx_token = read_int(f);
1000 if (flag & 1) {
1001 rx_run = read_byte(f);
1002 rx_run += read_byte(f) << 8;
1003 recv_state = r_running;
1004 }
1005 return -1 - rx_token;
1006
1007 case r_inflating:
1008 avail_out = LZ4_decompress_safe(next_in, dbuf, avail_in, size);
1009 if (avail_out < 0) {
1010 rprintf(FERROR, "uncompress failed: %d\n", avail_out);
1011 exit_cleanup(RERR_STREAMIO);
1012 }
Wayne Davison2bee3072020-07-04 15:58:56 -07001013 recv_state = r_idle;
Wayne Davisonc394e862020-05-25 13:43:25 -07001014 *data = dbuf;
1015 return avail_out;
1016
Wayne Davison2bee3072020-07-04 15:58:56 -07001017 case r_inflated: /* lz4 doesn't get into this state */
1018 break;
1019
Wayne Davisonc394e862020-05-25 13:43:25 -07001020 case r_running:
1021 ++rx_token;
1022 if (--rx_run == 0)
1023 recv_state = r_idle;
1024 return -1 - rx_token;
1025 }
1026 }
Wayne Davisonc394e862020-05-25 13:43:25 -07001027}
Wayne Davisonc394e862020-05-25 13:43:25 -07001028#endif /* SUPPORT_LZ4 */
Wayne Davison4aaadc22020-05-25 13:31:30 -07001029
Martin Pool79f671c2002-04-08 08:35:30 +00001030/**
1031 * Transmit a verbatim buffer of length @p n followed by a token.
Wayne Davisond67c8bd2004-06-18 16:22:14 +00001032 * If token == -1 then we have reached EOF
Andrew Tridgell70d794d1996-07-01 05:55:05 +00001033 * If n == 0 then don't send a buffer
Andrew Tridgell70d794d1996-07-01 05:55:05 +00001034 */
Wayne Davisonacc461c2005-02-14 08:19:32 +00001035void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
Wayne Davison7fcbf9e2005-01-01 21:08:20 +00001036 int32 n, int32 toklen)
Andrew Tridgell70d794d1996-07-01 05:55:05 +00001037{
Wayne Davison778f0df2020-06-04 16:17:12 -07001038 switch (do_compression) {
1039 case CPRES_NONE:
Wayne Davison7fcbf9e2005-01-01 21:08:20 +00001040 simple_send_token(f, token, buf, offset, n);
Wayne Davison778f0df2020-06-04 16:17:12 -07001041 break;
1042 case CPRES_ZLIB:
1043 case CPRES_ZLIBX:
1044 send_deflated_token(f, token, buf, offset, n, toklen);
1045 break;
Wayne Davison4aaadc22020-05-25 13:31:30 -07001046#ifdef SUPPORT_ZSTD
Wayne Davison778f0df2020-06-04 16:17:12 -07001047 case CPRES_ZSTD:
Wayne Davison4aaadc22020-05-25 13:31:30 -07001048 send_zstd_token(f, token, buf, offset, n);
Wayne Davison778f0df2020-06-04 16:17:12 -07001049 break;
Wayne Davison4aaadc22020-05-25 13:31:30 -07001050#endif
Wayne Davisonc394e862020-05-25 13:43:25 -07001051#ifdef SUPPORT_LZ4
Wayne Davison778f0df2020-06-04 16:17:12 -07001052 case CPRES_LZ4:
Wayne Davisonc394e862020-05-25 13:43:25 -07001053 send_compressed_token(f, token, buf, offset, n);
Wayne Davison778f0df2020-06-04 16:17:12 -07001054 break;
Wayne Davisonc394e862020-05-25 13:43:25 -07001055#endif
Wayne Davison778f0df2020-06-04 16:17:12 -07001056 default:
Wayne Davisonab110fc2020-07-08 12:19:16 -07001057 NOISY_DEATH("Unknown do_compression value");
Wayne Davison778f0df2020-06-04 16:17:12 -07001058 }
Andrew Tridgell70d794d1996-07-01 05:55:05 +00001059}
1060
Andrew Tridgell70d794d1996-07-01 05:55:05 +00001061/*
Wayne Davisond2970212020-04-15 17:42:23 -07001062 * receive a token or buffer from the other end. If the return value is >0 then
Andrew Tridgell70d794d1996-07-01 05:55:05 +00001063 * it is a data buffer of that length, and *data will point at the data.
1064 * if the return value is -i then it represents token i-1
1065 * if the return value is 0 then the end has been reached
1066 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +00001067int32 recv_token(int f, char **data)
Andrew Tridgell70d794d1996-07-01 05:55:05 +00001068{
Wayne Davison778f0df2020-06-04 16:17:12 -07001069 switch (do_compression) {
1070 case CPRES_NONE:
Wayne Davison73053f22020-06-19 09:55:48 -07001071 return simple_recv_token(f,data);
Wayne Davison778f0df2020-06-04 16:17:12 -07001072 case CPRES_ZLIB:
1073 case CPRES_ZLIBX:
Wayne Davison73053f22020-06-19 09:55:48 -07001074 return recv_deflated_token(f, data);
Wayne Davison4aaadc22020-05-25 13:31:30 -07001075#ifdef SUPPORT_ZSTD
Wayne Davison778f0df2020-06-04 16:17:12 -07001076 case CPRES_ZSTD:
Wayne Davison73053f22020-06-19 09:55:48 -07001077 return recv_zstd_token(f, data);
Wayne Davison4aaadc22020-05-25 13:31:30 -07001078#endif
Wayne Davisonc394e862020-05-25 13:43:25 -07001079#ifdef SUPPORT_LZ4
Wayne Davison778f0df2020-06-04 16:17:12 -07001080 case CPRES_LZ4:
Wayne Davison73053f22020-06-19 09:55:48 -07001081 return recv_compressed_token(f, data);
Wayne Davisonc394e862020-05-25 13:43:25 -07001082#endif
Wayne Davison778f0df2020-06-04 16:17:12 -07001083 default:
Wayne Davisonab110fc2020-07-08 12:19:16 -07001084 NOISY_DEATH("Unknown do_compression value");
Wayne Davison778f0df2020-06-04 16:17:12 -07001085 }
Paul Mackerras861c20b1996-07-03 04:05:46 +00001086}
1087
1088/*
1089 * look at the data corresponding to a token, if necessary
1090 */
Wayne Davison7fcbf9e2005-01-01 21:08:20 +00001091void see_token(char *data, int32 toklen)
Paul Mackerras861c20b1996-07-03 04:05:46 +00001092{
Wayne Davison778f0df2020-06-04 16:17:12 -07001093 switch (do_compression) {
1094 case CPRES_NONE:
1095 break;
1096 case CPRES_ZLIB:
Paul Mackerras5914bf11998-05-22 06:58:52 +00001097 see_deflate_token(data, toklen);
Wayne Davison778f0df2020-06-04 16:17:12 -07001098 break;
1099 case CPRES_ZLIBX:
1100 break;
Wayne Davisonbb1365d2020-07-10 09:47:16 -07001101#ifdef SUPPORT_ZSTD
1102 case CPRES_ZSTD:
Wayne Davison778f0df2020-06-04 16:17:12 -07001103 break;
Wayne Davisonc394e862020-05-25 13:43:25 -07001104#endif
Wayne Davison778f0df2020-06-04 16:17:12 -07001105#ifdef SUPPORT_LZ4
Wayne Davisonbb1365d2020-07-10 09:47:16 -07001106 case CPRES_LZ4:
1107 /*see_uncompressed_token(data, toklen);*/
Wayne Davison778f0df2020-06-04 16:17:12 -07001108 break;
1109#endif
1110 default:
Wayne Davisonab110fc2020-07-08 12:19:16 -07001111 NOISY_DEATH("Unknown do_compression value");
Wayne Davison778f0df2020-06-04 16:17:12 -07001112 }
Andrew Tridgell70d794d1996-07-01 05:55:05 +00001113}