2 * Routines used by the file-transfer code.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2020 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * 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.
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.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
32 extern int do_compression
;
33 extern int protocol_version
;
35 extern int do_compression_level
;
36 extern char *skip_compress
;
39 #define Z_INSERT_ONLY Z_SYNC_FLUSH
42 static int compression_level
; /* The compression level for the current file. */
43 static int skip_compression_level
; /* The least possible compressing for handling skip-compress files. */
44 static int per_file_default_level
; /* The default level that each new file gets prior to checking its suffix. */
47 struct suffix_tree
*sibling
;
48 struct suffix_tree
*child
;
49 char letter
, word_end
;
52 static char *match_list
;
53 static struct suffix_tree
*suftree
;
55 void init_compression_level(void)
57 int min_level
, max_level
, def_level
, off_level
;
59 switch (do_compression
) {
65 max_level
= Z_BEST_COMPRESSION
;
66 def_level
= 6; /* Z_DEFAULT_COMPRESSION is -1, so set it to the real default */
67 off_level
= skip_compression_level
= Z_NO_COMPRESSION
;
68 if (do_compression_level
== Z_DEFAULT_COMPRESSION
)
69 do_compression_level
= def_level
;
73 min_level
= skip_compression_level
= ZSTD_minCLevel();
74 max_level
= ZSTD_maxCLevel();
75 def_level
= ZSTD_CLEVEL_DEFAULT
;
76 off_level
= CLVL_NOT_SPECIFIED
;
77 if (do_compression_level
== 0)
78 do_compression_level
= def_level
;
83 min_level
= skip_compression_level
= 0;
86 off_level
= CLVL_NOT_SPECIFIED
;
89 default: /* paranoia to prevent missing case values */
93 if (do_compression_level
== CLVL_NOT_SPECIFIED
)
94 do_compression_level
= def_level
;
95 else if (do_compression_level
== off_level
) {
96 do_compression
= CPRES_NONE
;
100 /* We don't bother with any errors or warnings -- just make sure that the values are valid. */
101 if (do_compression_level
< min_level
)
102 do_compression_level
= min_level
;
103 else if (do_compression_level
> max_level
)
104 do_compression_level
= max_level
;
107 static void add_suffix(struct suffix_tree
**prior
, char ltr
, const char *str
)
109 struct suffix_tree
*node
, *newnode
;
112 const char *after
= strchr(str
, ']');
113 /* Treat "[foo" and "[]" as having a literal '['. */
114 if (after
&& after
++ != str
+1) {
115 while ((ltr
= *str
++) != ']')
116 add_suffix(prior
, ltr
, after
);
121 for (node
= *prior
; node
; prior
= &node
->sibling
, node
= node
->sibling
) {
122 if (node
->letter
== ltr
) {
124 add_suffix(&node
->child
, *str
, str
+1);
129 if (node
->letter
> ltr
)
132 if (!(newnode
= new(struct suffix_tree
)))
133 out_of_memory("add_suffix");
134 newnode
->sibling
= node
;
135 newnode
->child
= NULL
;
136 newnode
->letter
= ltr
;
139 add_suffix(&newnode
->child
, *str
, str
+1);
140 newnode
->word_end
= 0;
142 newnode
->word_end
= 1;
145 static void add_nocompress_suffixes(const char *str
)
150 if (!(buf
= new_array(char, strlen(f
) + 1)))
151 out_of_memory("add_nocompress_suffixes");
165 } while (*++f
!= '/' && *f
);
168 add_suffix(&suftree
, *buf
, buf
+1);
174 static void init_set_compression(void)
180 add_nocompress_suffixes(skip_compress
);
182 /* A non-daemon transfer skips the default suffix list if the
183 * user specified --skip-compress. */
184 if (skip_compress
&& module_id
< 0)
187 f
= lp_dont_compress(module_id
);
189 if (!(match_list
= t
= new_array(char, strlen(f
) + 2)))
190 out_of_memory("set_compression");
192 per_file_default_level
= do_compression_level
;
206 } while (*++f
!= ' ' && *f
);
209 if (t
- start
== 1+1 && *start
== '*') {
210 /* Optimize a match-string of "*". */
213 per_file_default_level
= skip_compression_level
;
217 /* Move *.foo items into the stuffix tree. */
218 if (*start
== '*' && start
[1] == '.' && start
[2]
219 && !strpbrk(start
+2, ".?*")) {
220 add_suffix(&suftree
, start
[2], start
+3);
227 /* determine the compression level based on a wildcard filename list */
228 void set_compression(const char *fname
)
230 const struct suffix_tree
*node
;
238 init_set_compression();
240 compression_level
= per_file_default_level
;
242 if (!*match_list
&& !suftree
)
245 if ((s
= strrchr(fname
, '/')) != NULL
)
248 for (s
= match_list
; *s
; s
+= strlen(s
) + 1) {
249 if (iwildmatch(s
, fname
)) {
250 compression_level
= skip_compression_level
;
255 if (!(node
= suftree
) || !(s
= strrchr(fname
, '.'))
256 || s
== fname
|| !(ltr
= *++s
))
262 while (node
->letter
!= ltr
) {
263 if (node
->letter
> ltr
)
265 if (!(node
= node
->sibling
))
268 if ((ltr
= *++s
) == '\0') {
270 compression_level
= skip_compression_level
;
273 if (!(node
= node
->child
))
278 /* non-compressing recv token */
279 static int32
simple_recv_token(int f
, char **data
)
281 static int32 residue
;
286 buf
= new_array(char, CHUNK_SIZE
);
288 out_of_memory("simple_recv_token");
292 int32 i
= read_int(f
);
299 n
= MIN(CHUNK_SIZE
,residue
);
305 /* non-compressing send token */
306 static void simple_send_token(int f
, int32 token
, struct map_struct
*buf
,
307 OFF_T offset
, int32 n
)
312 int32 n1
= MIN(CHUNK_SIZE
, n
-len
);
314 write_buf(f
, map_ptr(buf
, offset
+len
, n1
), n1
);
318 /* a -2 token means to send data only and no token */
320 write_int(f
, -(token
+1));
323 /* Flag bytes in compressed stream are encoded as follows: */
324 #define END_FLAG 0 /* that's all folks */
325 #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
326 #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
327 #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
328 #define TOKEN_REL 0x80 /* + 6-bit relative token number */
329 #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
331 #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
333 /* zlib.h says that if we want to be able to compress something in a single
334 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
335 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
336 * to ensure that this is a compile-time value). */
337 #define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
339 /* For coding runs of tokens */
340 static int32 last_token
= -1;
341 static int32 run_start
;
342 static int32 last_run_end
;
344 /* Deflation state */
345 static z_stream tx_strm
;
350 /* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
351 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
352 #if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
353 #define OBUF_SIZE (MAX_DATA_COUNT+2)
355 #define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
358 /* Send a deflated token */
360 send_deflated_token(int f
, int32 token
, struct map_struct
*buf
, OFF_T offset
,
361 int32 nb
, int32 toklen
)
363 static int init_done
, flush_pending
;
366 if (last_token
== -1) {
369 tx_strm
.next_in
= NULL
;
370 tx_strm
.zalloc
= NULL
;
371 tx_strm
.zfree
= NULL
;
372 if (deflateInit2(&tx_strm
, compression_level
,
374 Z_DEFAULT_STRATEGY
) != Z_OK
) {
375 rprintf(FERROR
, "compression init failed\n");
376 exit_cleanup(RERR_PROTOCOL
);
378 if ((obuf
= new_array(char, OBUF_SIZE
)) == NULL
)
379 out_of_memory("send_deflated_token");
382 deflateReset(&tx_strm
);
386 } else if (last_token
== -2) {
388 } else if (nb
!= 0 || token
!= last_token
+ 1
389 || token
>= run_start
+ 65536) {
390 /* output previous run */
391 r
= run_start
- last_run_end
;
392 n
= last_token
- run_start
;
393 if (r
>= 0 && r
<= 63) {
394 write_byte(f
, (n
==0? TOKEN_REL
: TOKENRUN_REL
) + r
);
396 write_byte(f
, (n
==0? TOKEN_LONG
: TOKENRUN_LONG
));
397 write_int(f
, run_start
);
401 write_byte(f
, n
>> 8);
403 last_run_end
= last_token
;
409 if (nb
!= 0 || flush_pending
) {
410 /* deflate the data starting at offset */
411 int flush
= Z_NO_FLUSH
;
412 tx_strm
.avail_in
= 0;
413 tx_strm
.avail_out
= 0;
415 if (tx_strm
.avail_in
== 0 && nb
!= 0) {
416 /* give it some more input */
417 n
= MIN(nb
, CHUNK_SIZE
);
418 tx_strm
.next_in
= (Bytef
*)
419 map_ptr(buf
, offset
, n
);
420 tx_strm
.avail_in
= n
;
424 if (tx_strm
.avail_out
== 0) {
425 tx_strm
.next_out
= (Bytef
*)(obuf
+ 2);
426 tx_strm
.avail_out
= MAX_DATA_COUNT
;
427 if (flush
!= Z_NO_FLUSH
) {
429 * We left the last 4 bytes in the
430 * buffer, in case they are the
431 * last 4. Move them to the front.
433 memcpy(tx_strm
.next_out
,
434 obuf
+MAX_DATA_COUNT
-2, 4);
435 tx_strm
.next_out
+= 4;
436 tx_strm
.avail_out
-= 4;
439 if (nb
== 0 && token
!= -2)
440 flush
= Z_SYNC_FLUSH
;
441 r
= deflate(&tx_strm
, flush
);
443 rprintf(FERROR
, "deflate returned %d\n", r
);
444 exit_cleanup(RERR_STREAMIO
);
446 if (nb
== 0 || tx_strm
.avail_out
== 0) {
447 n
= MAX_DATA_COUNT
- tx_strm
.avail_out
;
448 if (flush
!= Z_NO_FLUSH
) {
450 * We have to trim off the last 4
451 * bytes of output when flushing
452 * (they are just 0, 0, ff, ff).
457 obuf
[0] = DEFLATED_DATA
+ (n
>> 8);
459 write_buf(f
, obuf
, n
+2);
462 } while (nb
!= 0 || tx_strm
.avail_out
== 0);
463 flush_pending
= token
== -2;
467 /* end of file - clean up */
468 write_byte(f
, END_FLAG
);
469 } else if (token
!= -2 && do_compression
== CPRES_ZLIB
) {
470 /* Add the data in the current block to the compressor's
471 * history and hash table. */
473 /* Break up long sections in the same way that
474 * see_deflate_token() does. */
475 int32 n1
= toklen
> 0xffff ? 0xffff : toklen
;
477 tx_strm
.next_in
= (Bytef
*)map_ptr(buf
, offset
, n1
);
478 tx_strm
.avail_in
= n1
;
479 if (protocol_version
>= 31) /* Newer protocols avoid a data-duplicating bug */
481 tx_strm
.next_out
= (Bytef
*) obuf
;
482 tx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
483 r
= deflate(&tx_strm
, Z_INSERT_ONLY
);
484 if (r
!= Z_OK
|| tx_strm
.avail_in
!= 0) {
485 rprintf(FERROR
, "deflate on token returned %d (%d bytes left)\n",
486 r
, tx_strm
.avail_in
);
487 exit_cleanup(RERR_STREAMIO
);
489 } while (toklen
> 0);
493 /* tells us what the receiver is in the middle of doing */
494 static enum { r_init
, r_idle
, r_running
, r_inflating
, r_inflated
} recv_state
;
496 /* for inflating stuff */
497 static z_stream rx_strm
;
501 /* for decoding runs of tokens */
502 static int32 rx_token
;
505 /* Receive a deflated token and inflate it */
506 static int32
recv_deflated_token(int f
, char **data
)
508 static int init_done
;
509 static int32 saved_flag
;
514 switch (recv_state
) {
517 rx_strm
.next_out
= NULL
;
518 rx_strm
.zalloc
= NULL
;
519 rx_strm
.zfree
= NULL
;
520 if (inflateInit2(&rx_strm
, -15) != Z_OK
) {
521 rprintf(FERROR
, "inflate init failed\n");
522 exit_cleanup(RERR_PROTOCOL
);
524 if (!(cbuf
= new_array(char, MAX_DATA_COUNT
))
525 || !(dbuf
= new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE
))))
526 out_of_memory("recv_deflated_token");
529 inflateReset(&rx_strm
);
538 flag
= saved_flag
& 0xff;
542 if ((flag
& 0xC0) == DEFLATED_DATA
) {
543 n
= ((flag
& 0x3f) << 8) + read_byte(f
);
544 read_buf(f
, cbuf
, n
);
545 rx_strm
.next_in
= (Bytef
*)cbuf
;
546 rx_strm
.avail_in
= n
;
547 recv_state
= r_inflating
;
550 if (recv_state
== r_inflated
) {
551 /* check previous inflated stuff ended correctly */
552 rx_strm
.avail_in
= 0;
553 rx_strm
.next_out
= (Bytef
*)dbuf
;
554 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
555 r
= inflate(&rx_strm
, Z_SYNC_FLUSH
);
556 n
= AVAIL_OUT_SIZE(CHUNK_SIZE
) - rx_strm
.avail_out
;
558 * Z_BUF_ERROR just means no progress was
559 * made, i.e. the decompressor didn't have
560 * any pending output for us.
562 if (r
!= Z_OK
&& r
!= Z_BUF_ERROR
) {
563 rprintf(FERROR
, "inflate flush returned %d (%d bytes)\n",
565 exit_cleanup(RERR_STREAMIO
);
567 if (n
!= 0 && r
!= Z_BUF_ERROR
) {
568 /* have to return some more data and
569 save the flag for later. */
570 saved_flag
= flag
+ 0x10000;
575 * At this point the decompressor should
576 * be expecting to see the 0, 0, ff, ff bytes.
578 if (!inflateSyncPoint(&rx_strm
)) {
579 rprintf(FERROR
, "decompressor lost sync!\n");
580 exit_cleanup(RERR_STREAMIO
);
582 rx_strm
.avail_in
= 4;
583 rx_strm
.next_in
= (Bytef
*)cbuf
;
584 cbuf
[0] = cbuf
[1] = 0;
585 cbuf
[2] = cbuf
[3] = 0xff;
586 inflate(&rx_strm
, Z_SYNC_FLUSH
);
589 if (flag
== END_FLAG
) {
590 /* that's all folks */
595 /* here we have a token of some kind */
596 if (flag
& TOKEN_REL
) {
597 rx_token
+= flag
& 0x3f;
600 rx_token
= read_int(f
);
602 rx_run
= read_byte(f
);
603 rx_run
+= read_byte(f
) << 8;
604 recv_state
= r_running
;
606 return -1 - rx_token
;
609 rx_strm
.next_out
= (Bytef
*)dbuf
;
610 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
611 r
= inflate(&rx_strm
, Z_NO_FLUSH
);
612 n
= AVAIL_OUT_SIZE(CHUNK_SIZE
) - rx_strm
.avail_out
;
614 rprintf(FERROR
, "inflate returned %d (%d bytes)\n", r
, n
);
615 exit_cleanup(RERR_STREAMIO
);
617 if (rx_strm
.avail_in
== 0)
618 recv_state
= r_inflated
;
629 return -1 - rx_token
;
635 * put the data corresponding to a token that we've just returned
636 * from recv_deflated_token into the decompressor's history buffer.
638 static void see_deflate_token(char *buf
, int32 len
)
642 unsigned char hdr
[5];
644 rx_strm
.avail_in
= 0;
648 if (rx_strm
.avail_in
== 0 && len
!= 0) {
650 /* Give it a fake stored-block header. */
651 rx_strm
.next_in
= (Bytef
*)hdr
;
652 rx_strm
.avail_in
= 5;
657 hdr
[2] = blklen
>> 8;
661 rx_strm
.next_in
= (Bytef
*)buf
;
662 rx_strm
.avail_in
= blklen
;
663 if (protocol_version
>= 31) /* Newer protocols avoid a data-duplicating bug */
669 rx_strm
.next_out
= (Bytef
*)dbuf
;
670 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
671 r
= inflate(&rx_strm
, Z_SYNC_FLUSH
);
672 if (r
!= Z_OK
&& r
!= Z_BUF_ERROR
) {
673 rprintf(FERROR
, "inflate (token) returned %d\n", r
);
674 exit_cleanup(RERR_STREAMIO
);
676 } while (len
|| rx_strm
.avail_out
== 0);
681 static ZSTD_inBuffer zstd_in_buff
;
682 static ZSTD_outBuffer zstd_out_buff
;
683 static ZSTD_CCtx
*zstd_cctx
;
685 static void send_zstd_token(int f
, int32 token
, struct map_struct
*buf
,
686 OFF_T offset
, int32 nb
)
688 static int comp_init_done
, flush_pending
;
689 ZSTD_EndDirective flush
= ZSTD_e_continue
;
693 if (!comp_init_done
) {
695 zstd_cctx
= ZSTD_createCCtx();
697 rprintf(FERROR
, "compression init failed\n");
698 exit_cleanup(RERR_PROTOCOL
);
701 obuf
= new_array(char, OBUF_SIZE
);
703 out_of_memory("send_deflated_token");
705 ZSTD_CCtx_setParameter(zstd_cctx
, ZSTD_c_compressionLevel
,
706 do_compression_level
);
707 zstd_out_buff
.dst
= obuf
+ 2;
712 if (last_token
== -1) {
716 } else if (last_token
== -2) {
719 } else if (nb
!= 0 || token
!= last_token
+ 1
720 || token
>= run_start
+ 65536) {
722 /* output previous run */
723 r
= run_start
- last_run_end
;
724 n
= last_token
- run_start
;
726 if (r
>= 0 && r
<= 63) {
727 write_byte(f
, (n
==0? TOKEN_REL
: TOKENRUN_REL
) + r
);
729 write_byte(f
, (n
==0? TOKEN_LONG
: TOKENRUN_LONG
));
730 write_int(f
, run_start
);
734 write_byte(f
, n
>> 8);
736 last_run_end
= last_token
;
742 if (nb
|| flush_pending
) {
744 zstd_in_buff
.src
= map_ptr(buf
, offset
, nb
);
745 zstd_in_buff
.size
= nb
;
746 zstd_in_buff
.pos
= 0;
749 if (zstd_out_buff
.size
== 0) {
750 zstd_out_buff
.size
= MAX_DATA_COUNT
;
751 zstd_out_buff
.pos
= 0;
754 /* File ended, flush */
756 flush
= ZSTD_e_flush
;
758 r
= ZSTD_compressStream2(zstd_cctx
, &zstd_out_buff
, &zstd_in_buff
, flush
);
759 if (ZSTD_isError(r
)) {
760 rprintf(FERROR
, "ZSTD_compressStream returned %d\n", r
);
761 exit_cleanup(RERR_STREAMIO
);
765 * Nothing is sent if the buffer isn't full so avoid smaller
766 * transfers. If a file is finished then we flush the internal
767 * state and send a smaller buffer so that the remote side can
770 if (zstd_out_buff
.pos
== zstd_out_buff
.size
|| flush
== ZSTD_e_flush
) {
771 n
= zstd_out_buff
.pos
;
773 obuf
[0] = DEFLATED_DATA
+ (n
>> 8);
775 write_buf(f
, obuf
, n
+2);
777 zstd_out_buff
.size
= 0;
780 * Loop while the input buffer isn't full consumed or the
781 * internal state isn't fully flushed.
783 } while (zstd_in_buff
.pos
< zstd_in_buff
.size
|| r
> 0);
784 flush_pending
= token
== -2;
788 /* end of file - clean up */
789 write_byte(f
, END_FLAG
);
793 static ZSTD_DCtx
*zstd_dctx
;
795 static int32
recv_zstd_token(int f
, char **data
)
797 static int decomp_init_done
;
798 static int out_buffer_size
;
802 if (!decomp_init_done
) {
804 zstd_dctx
= ZSTD_createDCtx();
806 rprintf(FERROR
, "ZSTD_createDStream failed\n");
807 exit_cleanup(RERR_PROTOCOL
);
810 /* Output buffer fits two decompressed blocks */
811 out_buffer_size
= ZSTD_DStreamOutSize() * 2;
812 cbuf
= new_array(char, MAX_DATA_COUNT
);
813 dbuf
= new_array(char, out_buffer_size
);
815 out_of_memory("recv_zstd_token");
817 zstd_in_buff
.src
= cbuf
;
818 zstd_out_buff
.dst
= dbuf
;
820 decomp_init_done
= 1;
824 switch (recv_state
) {
832 if ((flag
& 0xC0) == DEFLATED_DATA
) {
833 n
= ((flag
& 0x3f) << 8) + read_byte(f
);
834 read_buf(f
, cbuf
, n
);
836 zstd_in_buff
.size
= n
;
837 zstd_in_buff
.pos
= 0;
839 recv_state
= r_inflating
;
841 } else if (flag
== END_FLAG
) {
842 /* that's all folks */
847 /* here we have a token of some kind */
848 if (flag
& TOKEN_REL
) {
849 rx_token
+= flag
& 0x3f;
852 rx_token
= read_int(f
);
854 rx_run
= read_byte(f
);
855 rx_run
+= read_byte(f
) << 8;
856 recv_state
= r_running
;
858 return -1 - rx_token
;
863 zstd_out_buff
.size
= out_buffer_size
;
864 zstd_out_buff
.pos
= 0;
866 r
= ZSTD_decompressStream(zstd_dctx
, &zstd_out_buff
, &zstd_in_buff
);
867 n
= zstd_out_buff
.pos
;
868 if (ZSTD_isError(r
)) {
869 rprintf(FERROR
, "ZSTD decomp returned %d (%d bytes)\n", r
, n
);
870 exit_cleanup(RERR_STREAMIO
);
874 * If the input buffer is fully consumed and the output
875 * buffer is not full then next step is to read more
878 if (zstd_in_buff
.size
== zstd_in_buff
.pos
&& n
< out_buffer_size
)
891 return -1 - rx_token
;
899 #endif /* SUPPORT_ZSTD */
903 send_compressed_token(int f
, int32 token
, struct map_struct
*buf
, OFF_T offset
, int32 nb
)
905 static int init_done
, flush_pending
;
906 int size
= MAX(LZ4_compressBound(CHUNK_SIZE
), MAX_DATA_COUNT
+2);
909 if (last_token
== -1) {
911 if ((obuf
= new_array(char, size
)) == NULL
)
912 out_of_memory("send_compressed_token");
918 } else if (last_token
== -2) {
920 } else if (nb
!= 0 || token
!= last_token
+ 1
921 || token
>= run_start
+ 65536) {
922 /* output previous run */
923 r
= run_start
- last_run_end
;
924 n
= last_token
- run_start
;
925 if (r
>= 0 && r
<= 63) {
926 write_byte(f
, (n
==0? TOKEN_REL
: TOKENRUN_REL
) + r
);
928 write_byte(f
, (n
==0? TOKEN_LONG
: TOKENRUN_LONG
));
929 write_int(f
, run_start
);
933 write_byte(f
, n
>> 8);
935 last_run_end
= last_token
;
941 if (nb
!= 0 || flush_pending
) {
942 int available_in
, available_out
= 0;
947 char *next_out
= obuf
+ 2;
949 if (available_out
== 0) {
950 available_in
= MIN(nb
, MAX_DATA_COUNT
);
951 next_in
= map_ptr(buf
, offset
, available_in
);
955 available_out
= LZ4_compress_default(next_in
, next_out
, available_in
, size
- 2);
956 if (!available_out
) {
957 rprintf(FERROR
, "compress returned %d\n", available_out
);
958 exit_cleanup(RERR_STREAMIO
);
960 if (available_out
<= MAX_DATA_COUNT
) {
961 ptr
[0] = DEFLATED_DATA
+ (available_out
>> 8);
962 ptr
[1] = available_out
;
964 write_buf(f
, ptr
, available_out
+ 2);
968 offset
+= available_in
;
971 flush_pending
= token
== -2;
974 /* end of file - clean up */
975 write_byte(f
, END_FLAG
);
978 static int32
recv_compressed_token(int f
, char **data
)
980 static int32 saved_flag
;
981 static int init_done
;
983 int size
= MAX(LZ4_compressBound(CHUNK_SIZE
), MAX_DATA_COUNT
+2);
984 static const char *next_in
;
989 switch (recv_state
) {
992 if (!(cbuf
= new_array(char, MAX_DATA_COUNT
))
993 || !(dbuf
= new_array(char, size
)))
994 out_of_memory("recv_compressed_token");
1003 flag
= saved_flag
& 0xff;
1006 flag
= read_byte(f
);
1007 if ((flag
& 0xC0) == DEFLATED_DATA
) {
1008 n
= ((flag
& 0x3f) << 8) + read_byte(f
);
1009 read_buf(f
, cbuf
, n
);
1010 next_in
= (char *)cbuf
;
1012 recv_state
= r_inflating
;
1016 if (recv_state
== r_inflated
)
1017 recv_state
= r_idle
;
1019 if (flag
== END_FLAG
) {
1020 /* that's all folks */
1021 recv_state
= r_init
;
1025 /* here we have a token of some kind */
1026 if (flag
& TOKEN_REL
) {
1027 rx_token
+= flag
& 0x3f;
1030 rx_token
= read_int(f
);
1032 rx_run
= read_byte(f
);
1033 rx_run
+= read_byte(f
) << 8;
1034 recv_state
= r_running
;
1036 return -1 - rx_token
;
1039 avail_out
= LZ4_decompress_safe(next_in
, dbuf
, avail_in
, size
);
1040 if (avail_out
< 0) {
1041 rprintf(FERROR
, "uncompress failed: %d\n", avail_out
);
1042 exit_cleanup(RERR_STREAMIO
);
1044 recv_state
= r_inflated
;
1051 recv_state
= r_idle
;
1052 return -1 - rx_token
;
1059 static void see_uncompressed_token(char *buf
, int32 len
)
1061 static const char *next_in
;
1062 static int avail_in
;
1072 if (avail_in
== 0 && len
!= 0) {
1074 /* Give it a fake stored-block header. */
1078 if (blklen
> 0xffff)
1081 hdr
[2] = blklen
>> 8;
1085 next_in
= (char *)buf
;
1087 if (protocol_version
>= 31) /* Newer protocols avoid a data-duplicating bug */
1093 avail_out
= LZ4_decompress_safe(next_in
, dbuf
, avail_in
, LZ4_compressBound(CHUNK_SIZE
));
1094 if (avail_out
< 0) {
1095 rprintf(FERROR
, "uncompress failed: %d\n", avail_out
);
1096 exit_cleanup(RERR_STREAMIO
);
1102 #endif /* SUPPORT_LZ4 */
1105 * Transmit a verbatim buffer of length @p n followed by a token.
1106 * If token == -1 then we have reached EOF
1107 * If n == 0 then don't send a buffer
1109 void send_token(int f
, int32 token
, struct map_struct
*buf
, OFF_T offset
,
1110 int32 n
, int32 toklen
)
1112 switch (do_compression
) {
1114 simple_send_token(f
, token
, buf
, offset
, n
);
1118 send_deflated_token(f
, token
, buf
, offset
, n
, toklen
);
1122 send_zstd_token(f
, token
, buf
, offset
, n
);
1127 send_compressed_token(f
, token
, buf
, offset
, n
);
1136 * receive a token or buffer from the other end. If the return value is >0 then
1137 * it is a data buffer of that length, and *data will point at the data.
1138 * if the return value is -i then it represents token i-1
1139 * if the return value is 0 then the end has been reached
1141 int32
recv_token(int f
, char **data
)
1145 switch (do_compression
) {
1147 tok
= simple_recv_token(f
,data
);
1151 tok
= recv_deflated_token(f
, data
);
1155 tok
= recv_zstd_token(f
, data
);
1160 tok
= recv_compressed_token(f
, data
);
1170 * look at the data corresponding to a token, if necessary
1172 void see_token(char *data
, int32 toklen
)
1174 switch (do_compression
) {
1178 see_deflate_token(data
, toklen
);
1184 /*see_uncompressed_token(data, toklen);*/