2 * Routines used by the file-transfer code.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2013 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.
27 #define Z_INSERT_ONLY Z_SYNC_FLUSH
30 extern int do_compression
;
31 extern int protocol_version
;
33 extern int def_compress_level
;
34 extern char *skip_compress
;
36 static int compression_level
, per_file_default_level
;
39 struct suffix_tree
*sibling
;
40 struct suffix_tree
*child
;
41 char letter
, word_end
;
44 static char *match_list
;
45 static struct suffix_tree
*suftree
;
47 static void add_suffix(struct suffix_tree
**prior
, char ltr
, const char *str
)
49 struct suffix_tree
*node
, *newnode
;
52 const char *after
= strchr(str
, ']');
53 /* Treat "[foo" and "[]" as having a literal '['. */
54 if (after
&& after
++ != str
+1) {
55 while ((ltr
= *str
++) != ']')
56 add_suffix(prior
, ltr
, after
);
61 for (node
= *prior
; node
; prior
= &node
->sibling
, node
= node
->sibling
) {
62 if (node
->letter
== ltr
) {
64 add_suffix(&node
->child
, *str
, str
+1);
69 if (node
->letter
> ltr
)
72 if (!(newnode
= new(struct suffix_tree
)))
73 out_of_memory("add_suffix");
74 newnode
->sibling
= node
;
75 newnode
->child
= NULL
;
76 newnode
->letter
= ltr
;
79 add_suffix(&newnode
->child
, *str
, str
+1);
80 newnode
->word_end
= 0;
82 newnode
->word_end
= 1;
85 static void add_nocompress_suffixes(const char *str
)
90 if (!(buf
= new_array(char, strlen(f
) + 1)))
91 out_of_memory("add_nocompress_suffixes");
105 } while (*++f
!= '/' && *f
);
108 add_suffix(&suftree
, *buf
, buf
+1);
114 static void init_set_compression(void)
120 add_nocompress_suffixes(skip_compress
);
122 /* A non-daemon transfer skips the default suffix list if the
123 * user specified --skip-compress. */
124 if (skip_compress
&& module_id
< 0)
127 f
= lp_dont_compress(module_id
);
129 if (!(match_list
= t
= new_array(char, strlen(f
) + 2)))
130 out_of_memory("set_compression");
132 per_file_default_level
= def_compress_level
;
146 } while (*++f
!= ' ' && *f
);
149 if (t
- start
== 1+1 && *start
== '*') {
150 /* Optimize a match-string of "*". */
153 per_file_default_level
= 0;
157 /* Move *.foo items into the stuffix tree. */
158 if (*start
== '*' && start
[1] == '.' && start
[2]
159 && !strpbrk(start
+2, ".?*")) {
160 add_suffix(&suftree
, start
[2], start
+3);
167 /* determine the compression level based on a wildcard filename list */
168 void set_compression(const char *fname
)
170 const struct suffix_tree
*node
;
178 init_set_compression();
180 compression_level
= per_file_default_level
;
182 if (!*match_list
&& !suftree
)
185 if ((s
= strrchr(fname
, '/')) != NULL
)
188 for (s
= match_list
; *s
; s
+= strlen(s
) + 1) {
189 if (iwildmatch(s
, fname
)) {
190 compression_level
= 0;
195 if (!(node
= suftree
) || !(s
= strrchr(fname
, '.'))
196 || s
== fname
|| !(ltr
= *++s
))
202 while (node
->letter
!= ltr
) {
203 if (node
->letter
> ltr
)
205 if (!(node
= node
->sibling
))
208 if ((ltr
= *++s
) == '\0') {
210 compression_level
= 0;
213 if (!(node
= node
->child
))
218 /* non-compressing recv token */
219 static int32
simple_recv_token(int f
, char **data
)
221 static int32 residue
;
226 buf
= new_array(char, CHUNK_SIZE
);
228 out_of_memory("simple_recv_token");
232 int32 i
= read_int(f
);
239 n
= MIN(CHUNK_SIZE
,residue
);
245 /* non-compressing send token */
246 static void simple_send_token(int f
, int32 token
, struct map_struct
*buf
,
247 OFF_T offset
, int32 n
)
252 int32 n1
= MIN(CHUNK_SIZE
, n
-len
);
254 write_buf(f
, map_ptr(buf
, offset
+len
, n1
), n1
);
258 /* a -2 token means to send data only and no token */
260 write_int(f
, -(token
+1));
263 /* Flag bytes in compressed stream are encoded as follows: */
264 #define END_FLAG 0 /* that's all folks */
265 #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
266 #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
267 #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
268 #define TOKEN_REL 0x80 /* + 6-bit relative token number */
269 #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
271 #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
273 /* zlib.h says that if we want to be able to compress something in a single
274 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
275 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
276 * to ensure that this is a compile-time value). */
277 #define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
279 /* For coding runs of tokens */
280 static int32 last_token
= -1;
281 static int32 run_start
;
282 static int32 last_run_end
;
284 /* Deflation state */
285 static z_stream tx_strm
;
290 /* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
291 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
292 #if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
293 #define OBUF_SIZE (MAX_DATA_COUNT+2)
295 #define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
298 /* Send a deflated token */
300 send_deflated_token(int f
, int32 token
, struct map_struct
*buf
, OFF_T offset
,
301 int32 nb
, int32 toklen
)
304 static int init_done
, flush_pending
;
306 if (last_token
== -1) {
309 tx_strm
.next_in
= NULL
;
310 tx_strm
.zalloc
= NULL
;
311 tx_strm
.zfree
= NULL
;
312 if (deflateInit2(&tx_strm
, compression_level
,
314 Z_DEFAULT_STRATEGY
) != Z_OK
) {
315 rprintf(FERROR
, "compression init failed\n");
316 exit_cleanup(RERR_PROTOCOL
);
318 if ((obuf
= new_array(char, OBUF_SIZE
)) == NULL
)
319 out_of_memory("send_deflated_token");
322 deflateReset(&tx_strm
);
326 } else if (last_token
== -2) {
328 } else if (nb
!= 0 || token
!= last_token
+ 1
329 || token
>= run_start
+ 65536) {
330 /* output previous run */
331 r
= run_start
- last_run_end
;
332 n
= last_token
- run_start
;
333 if (r
>= 0 && r
<= 63) {
334 write_byte(f
, (n
==0? TOKEN_REL
: TOKENRUN_REL
) + r
);
336 write_byte(f
, (n
==0? TOKEN_LONG
: TOKENRUN_LONG
));
337 write_int(f
, run_start
);
341 write_byte(f
, n
>> 8);
343 last_run_end
= last_token
;
349 if (nb
!= 0 || flush_pending
) {
350 /* deflate the data starting at offset */
351 int flush
= Z_NO_FLUSH
;
352 tx_strm
.avail_in
= 0;
353 tx_strm
.avail_out
= 0;
355 if (tx_strm
.avail_in
== 0 && nb
!= 0) {
356 /* give it some more input */
357 n
= MIN(nb
, CHUNK_SIZE
);
358 tx_strm
.next_in
= (Bytef
*)
359 map_ptr(buf
, offset
, n
);
360 tx_strm
.avail_in
= n
;
364 if (tx_strm
.avail_out
== 0) {
365 tx_strm
.next_out
= (Bytef
*)(obuf
+ 2);
366 tx_strm
.avail_out
= MAX_DATA_COUNT
;
367 if (flush
!= Z_NO_FLUSH
) {
369 * We left the last 4 bytes in the
370 * buffer, in case they are the
371 * last 4. Move them to the front.
373 memcpy(tx_strm
.next_out
,
374 obuf
+MAX_DATA_COUNT
-2, 4);
375 tx_strm
.next_out
+= 4;
376 tx_strm
.avail_out
-= 4;
379 if (nb
== 0 && token
!= -2)
380 flush
= Z_SYNC_FLUSH
;
381 r
= deflate(&tx_strm
, flush
);
383 rprintf(FERROR
, "deflate returned %d\n", r
);
384 exit_cleanup(RERR_STREAMIO
);
386 if (nb
== 0 || tx_strm
.avail_out
== 0) {
387 n
= MAX_DATA_COUNT
- tx_strm
.avail_out
;
388 if (flush
!= Z_NO_FLUSH
) {
390 * We have to trim off the last 4
391 * bytes of output when flushing
392 * (they are just 0, 0, ff, ff).
397 obuf
[0] = DEFLATED_DATA
+ (n
>> 8);
399 write_buf(f
, obuf
, n
+2);
402 } while (nb
!= 0 || tx_strm
.avail_out
== 0);
403 flush_pending
= token
== -2;
407 /* end of file - clean up */
408 write_byte(f
, END_FLAG
);
409 } else if (token
!= -2) {
410 /* Add the data in the current block to the compressor's
411 * history and hash table. */
413 /* Break up long sections in the same way that
414 * see_deflate_token() does. */
415 int32 n1
= toklen
> 0xffff ? 0xffff : toklen
;
417 tx_strm
.next_in
= (Bytef
*)map_ptr(buf
, offset
, n1
);
418 tx_strm
.avail_in
= n1
;
419 if (protocol_version
>= 31) /* Newer protocols avoid a data-duplicating bug */
422 tx_strm
.next_out
= (Bytef
*) obuf
;
423 tx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
424 r
= deflate(&tx_strm
, Z_INSERT_ONLY
);
426 rprintf(FERROR
, "deflate on token returned %d (%d bytes left)\n",
427 r
, tx_strm
.avail_in
);
428 exit_cleanup(RERR_STREAMIO
);
430 } while (tx_strm
.avail_in
!= 0);
431 } while (toklen
> 0);
435 /* tells us what the receiver is in the middle of doing */
436 static enum { r_init
, r_idle
, r_running
, r_inflating
, r_inflated
} recv_state
;
438 /* for inflating stuff */
439 static z_stream rx_strm
;
443 /* for decoding runs of tokens */
444 static int32 rx_token
;
447 /* Receive a deflated token and inflate it */
448 static int32
recv_deflated_token(int f
, char **data
)
450 static int init_done
;
451 static int32 saved_flag
;
456 switch (recv_state
) {
459 rx_strm
.next_out
= NULL
;
460 rx_strm
.zalloc
= NULL
;
461 rx_strm
.zfree
= NULL
;
462 if (inflateInit2(&rx_strm
, -15) != Z_OK
) {
463 rprintf(FERROR
, "inflate init failed\n");
464 exit_cleanup(RERR_PROTOCOL
);
466 if (!(cbuf
= new_array(char, MAX_DATA_COUNT
))
467 || !(dbuf
= new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE
))))
468 out_of_memory("recv_deflated_token");
471 inflateReset(&rx_strm
);
480 flag
= saved_flag
& 0xff;
484 if ((flag
& 0xC0) == DEFLATED_DATA
) {
485 n
= ((flag
& 0x3f) << 8) + read_byte(f
);
486 read_buf(f
, cbuf
, n
);
487 rx_strm
.next_in
= (Bytef
*)cbuf
;
488 rx_strm
.avail_in
= n
;
489 recv_state
= r_inflating
;
492 if (recv_state
== r_inflated
) {
493 /* check previous inflated stuff ended correctly */
494 rx_strm
.avail_in
= 0;
495 rx_strm
.next_out
= (Bytef
*)dbuf
;
496 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
497 r
= inflate(&rx_strm
, Z_SYNC_FLUSH
);
498 n
= AVAIL_OUT_SIZE(CHUNK_SIZE
) - rx_strm
.avail_out
;
500 * Z_BUF_ERROR just means no progress was
501 * made, i.e. the decompressor didn't have
502 * any pending output for us.
504 if (r
!= Z_OK
&& r
!= Z_BUF_ERROR
) {
505 rprintf(FERROR
, "inflate flush returned %d (%d bytes)\n",
507 exit_cleanup(RERR_STREAMIO
);
509 if (n
!= 0 && r
!= Z_BUF_ERROR
) {
510 /* have to return some more data and
511 save the flag for later. */
512 saved_flag
= flag
+ 0x10000;
517 * At this point the decompressor should
518 * be expecting to see the 0, 0, ff, ff bytes.
520 if (!inflateSyncPoint(&rx_strm
)) {
521 rprintf(FERROR
, "decompressor lost sync!\n");
522 exit_cleanup(RERR_STREAMIO
);
524 rx_strm
.avail_in
= 4;
525 rx_strm
.next_in
= (Bytef
*)cbuf
;
526 cbuf
[0] = cbuf
[1] = 0;
527 cbuf
[2] = cbuf
[3] = 0xff;
528 inflate(&rx_strm
, Z_SYNC_FLUSH
);
531 if (flag
== END_FLAG
) {
532 /* that's all folks */
537 /* here we have a token of some kind */
538 if (flag
& TOKEN_REL
) {
539 rx_token
+= flag
& 0x3f;
542 rx_token
= read_int(f
);
544 rx_run
= read_byte(f
);
545 rx_run
+= read_byte(f
) << 8;
546 recv_state
= r_running
;
548 return -1 - rx_token
;
551 rx_strm
.next_out
= (Bytef
*)dbuf
;
552 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
553 r
= inflate(&rx_strm
, Z_NO_FLUSH
);
554 n
= AVAIL_OUT_SIZE(CHUNK_SIZE
) - rx_strm
.avail_out
;
556 rprintf(FERROR
, "inflate returned %d (%d bytes)\n", r
, n
);
557 exit_cleanup(RERR_STREAMIO
);
559 if (rx_strm
.avail_in
== 0)
560 recv_state
= r_inflated
;
571 return -1 - rx_token
;
577 * put the data corresponding to a token that we've just returned
578 * from recv_deflated_token into the decompressor's history buffer.
580 static void see_deflate_token(char *buf
, int32 len
)
584 unsigned char hdr
[5];
586 rx_strm
.avail_in
= 0;
590 if (rx_strm
.avail_in
== 0 && len
!= 0) {
592 /* Give it a fake stored-block header. */
593 rx_strm
.next_in
= (Bytef
*)hdr
;
594 rx_strm
.avail_in
= 5;
599 hdr
[2] = blklen
>> 8;
603 rx_strm
.next_in
= (Bytef
*)buf
;
604 rx_strm
.avail_in
= blklen
;
605 if (protocol_version
>= 31) /* Newer protocols avoid a data-duplicating bug */
611 rx_strm
.next_out
= (Bytef
*)dbuf
;
612 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
613 r
= inflate(&rx_strm
, Z_SYNC_FLUSH
);
614 if (r
!= Z_OK
&& r
!= Z_BUF_ERROR
) {
615 rprintf(FERROR
, "inflate (token) returned %d\n", r
);
616 exit_cleanup(RERR_STREAMIO
);
618 } while (len
|| rx_strm
.avail_out
== 0);
622 * Transmit a verbatim buffer of length @p n followed by a token.
623 * If token == -1 then we have reached EOF
624 * If n == 0 then don't send a buffer
626 void send_token(int f
, int32 token
, struct map_struct
*buf
, OFF_T offset
,
627 int32 n
, int32 toklen
)
630 simple_send_token(f
, token
, buf
, offset
, n
);
632 send_deflated_token(f
, token
, buf
, offset
, n
, toklen
);
636 * receive a token or buffer from the other end. If the reurn value is >0 then
637 * it is a data buffer of that length, and *data will point at the data.
638 * if the return value is -i then it represents token i-1
639 * if the return value is 0 then the end has been reached
641 int32
recv_token(int f
, char **data
)
645 if (!do_compression
) {
646 tok
= simple_recv_token(f
,data
);
648 tok
= recv_deflated_token(f
, data
);
654 * look at the data corresponding to a token, if necessary
656 void see_token(char *data
, int32 toklen
)
659 see_deflate_token(data
, toklen
);