2 * Routines used by the file-transfer code.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2009 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.
24 #include "zlib/zlib.h"
26 extern int do_compression
;
28 extern int def_compress_level
;
29 extern char *skip_compress
;
31 static int compression_level
, per_file_default_level
;
34 struct suffix_tree
*sibling
;
35 struct suffix_tree
*child
;
36 char letter
, word_end
;
39 static char *match_list
;
40 static struct suffix_tree
*suftree
;
42 static void add_suffix(struct suffix_tree
**prior
, char ltr
, const char *str
)
44 struct suffix_tree
*node
, *newnode
;
47 const char *after
= strchr(str
, ']');
48 /* Treat "[foo" and "[]" as having a literal '['. */
49 if (after
&& after
++ != str
+1) {
50 while ((ltr
= *str
++) != ']')
51 add_suffix(prior
, ltr
, after
);
56 for (node
= *prior
; node
; prior
= &node
->sibling
, node
= node
->sibling
) {
57 if (node
->letter
== ltr
) {
59 add_suffix(&node
->child
, *str
, str
+1);
64 if (node
->letter
> ltr
)
67 if (!(newnode
= new(struct suffix_tree
)))
68 out_of_memory("add_suffix");
69 newnode
->sibling
= node
;
70 newnode
->child
= NULL
;
71 newnode
->letter
= ltr
;
74 add_suffix(&newnode
->child
, *str
, str
+1);
75 newnode
->word_end
= 0;
77 newnode
->word_end
= 1;
80 static void add_nocompress_suffixes(const char *str
)
85 if (!(buf
= new_array(char, strlen(f
) + 1)))
86 out_of_memory("add_nocompress_suffixes");
100 } while (*++f
!= '/' && *f
);
103 add_suffix(&suftree
, *buf
, buf
+1);
109 static void init_set_compression(void)
115 add_nocompress_suffixes(skip_compress
);
117 /* A non-daemon transfer skips the default suffix list if the
118 * user specified --skip-compress. */
119 if (skip_compress
&& module_id
< 0)
122 f
= lp_dont_compress(module_id
);
124 if (!(match_list
= t
= new_array(char, strlen(f
) + 2)))
125 out_of_memory("set_compression");
127 per_file_default_level
= def_compress_level
;
141 } while (*++f
!= ' ' && *f
);
144 if (t
- start
== 1+1 && *start
== '*') {
145 /* Optimize a match-string of "*". */
148 per_file_default_level
= 0;
152 /* Move *.foo items into the stuffix tree. */
153 if (*start
== '*' && start
[1] == '.' && start
[2]
154 && !strpbrk(start
+2, ".?*")) {
155 add_suffix(&suftree
, start
[2], start
+3);
162 /* determine the compression level based on a wildcard filename list */
163 void set_compression(const char *fname
)
165 const struct suffix_tree
*node
;
173 init_set_compression();
175 compression_level
= per_file_default_level
;
177 if (!*match_list
&& !suftree
)
180 if ((s
= strrchr(fname
, '/')) != NULL
)
183 for (s
= match_list
; *s
; s
+= strlen(s
) + 1) {
184 if (iwildmatch(s
, fname
)) {
185 compression_level
= 0;
190 if (!(node
= suftree
) || !(s
= strrchr(fname
, '.'))
191 || s
== fname
|| !(ltr
= *++s
))
195 while (node
->letter
!= ltr
) {
196 if (node
->letter
> ltr
)
198 if (!(node
= node
->sibling
))
201 if ((ltr
= *++s
) == '\0') {
203 compression_level
= 0;
206 if (!(node
= node
->child
))
211 /* non-compressing recv token */
212 static int32
simple_recv_token(int f
, char **data
)
214 static int32 residue
;
219 buf
= new_array(char, CHUNK_SIZE
);
221 out_of_memory("simple_recv_token");
225 int32 i
= read_int(f
);
232 n
= MIN(CHUNK_SIZE
,residue
);
238 /* non-compressing send token */
239 static void simple_send_token(int f
, int32 token
, struct map_struct
*buf
,
240 OFF_T offset
, int32 n
)
245 int32 n1
= MIN(CHUNK_SIZE
, n
-len
);
247 write_buf(f
, map_ptr(buf
, offset
+len
, n1
), n1
);
251 /* a -2 token means to send data only and no token */
253 write_int(f
, -(token
+1));
256 /* Flag bytes in compressed stream are encoded as follows: */
257 #define END_FLAG 0 /* that's all folks */
258 #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
259 #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
260 #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
261 #define TOKEN_REL 0x80 /* + 6-bit relative token number */
262 #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
264 #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
266 /* zlib.h says that if we want to be able to compress something in a single
267 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
268 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
269 * to ensure that this is a compile-time value). */
270 #define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
272 /* For coding runs of tokens */
273 static int32 last_token
= -1;
274 static int32 run_start
;
275 static int32 last_run_end
;
277 /* Deflation state */
278 static z_stream tx_strm
;
283 /* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
284 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
285 #if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
286 #define OBUF_SIZE (MAX_DATA_COUNT+2)
288 #define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
291 /* Send a deflated token */
293 send_deflated_token(int f
, int32 token
, struct map_struct
*buf
, OFF_T offset
,
294 int32 nb
, int32 toklen
)
297 static int init_done
, flush_pending
;
299 if (last_token
== -1) {
302 tx_strm
.next_in
= NULL
;
303 tx_strm
.zalloc
= NULL
;
304 tx_strm
.zfree
= NULL
;
305 if (deflateInit2(&tx_strm
, compression_level
,
307 Z_DEFAULT_STRATEGY
) != Z_OK
) {
308 rprintf(FERROR
, "compression init failed\n");
309 exit_cleanup(RERR_STREAMIO
);
311 if ((obuf
= new_array(char, OBUF_SIZE
)) == NULL
)
312 out_of_memory("send_deflated_token");
315 deflateReset(&tx_strm
);
319 } else if (last_token
== -2) {
321 } else if (nb
!= 0 || token
!= last_token
+ 1
322 || token
>= run_start
+ 65536) {
323 /* output previous run */
324 r
= run_start
- last_run_end
;
325 n
= last_token
- run_start
;
326 if (r
>= 0 && r
<= 63) {
327 write_byte(f
, (n
==0? TOKEN_REL
: TOKENRUN_REL
) + r
);
329 write_byte(f
, (n
==0? TOKEN_LONG
: TOKENRUN_LONG
));
330 write_int(f
, run_start
);
334 write_byte(f
, n
>> 8);
336 last_run_end
= last_token
;
342 if (nb
!= 0 || flush_pending
) {
343 /* deflate the data starting at offset */
344 int flush
= Z_NO_FLUSH
;
345 tx_strm
.avail_in
= 0;
346 tx_strm
.avail_out
= 0;
348 if (tx_strm
.avail_in
== 0 && nb
!= 0) {
349 /* give it some more input */
350 n
= MIN(nb
, CHUNK_SIZE
);
351 tx_strm
.next_in
= (Bytef
*)
352 map_ptr(buf
, offset
, n
);
353 tx_strm
.avail_in
= n
;
357 if (tx_strm
.avail_out
== 0) {
358 tx_strm
.next_out
= (Bytef
*)(obuf
+ 2);
359 tx_strm
.avail_out
= MAX_DATA_COUNT
;
360 if (flush
!= Z_NO_FLUSH
) {
362 * We left the last 4 bytes in the
363 * buffer, in case they are the
364 * last 4. Move them to the front.
366 memcpy(tx_strm
.next_out
,
367 obuf
+MAX_DATA_COUNT
-2, 4);
368 tx_strm
.next_out
+= 4;
369 tx_strm
.avail_out
-= 4;
372 if (nb
== 0 && token
!= -2)
373 flush
= Z_SYNC_FLUSH
;
374 r
= deflate(&tx_strm
, flush
);
376 rprintf(FERROR
, "deflate returned %d\n", r
);
377 exit_cleanup(RERR_STREAMIO
);
379 if (nb
== 0 || tx_strm
.avail_out
== 0) {
380 n
= MAX_DATA_COUNT
- tx_strm
.avail_out
;
381 if (flush
!= Z_NO_FLUSH
) {
383 * We have to trim off the last 4
384 * bytes of output when flushing
385 * (they are just 0, 0, ff, ff).
390 obuf
[0] = DEFLATED_DATA
+ (n
>> 8);
392 write_buf(f
, obuf
, n
+2);
395 } while (nb
!= 0 || tx_strm
.avail_out
== 0);
396 flush_pending
= token
== -2;
400 /* end of file - clean up */
401 write_byte(f
, END_FLAG
);
402 } else if (token
!= -2) {
403 /* Add the data in the current block to the compressor's
404 * history and hash table. */
406 /* Break up long sections in the same way that
407 * see_deflate_token() does. */
408 int32 n1
= toklen
> 0xffff ? 0xffff : toklen
;
410 tx_strm
.next_in
= (Bytef
*)map_ptr(buf
, offset
, n1
);
411 tx_strm
.avail_in
= n1
;
412 tx_strm
.next_out
= (Bytef
*) obuf
;
413 tx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
414 r
= deflate(&tx_strm
, Z_INSERT_ONLY
);
415 if (r
!= Z_OK
|| tx_strm
.avail_in
!= 0) {
416 rprintf(FERROR
, "deflate on token returned %d (%d bytes left)\n",
417 r
, tx_strm
.avail_in
);
418 exit_cleanup(RERR_STREAMIO
);
420 } while (toklen
> 0);
424 /* tells us what the receiver is in the middle of doing */
425 static enum { r_init
, r_idle
, r_running
, r_inflating
, r_inflated
} recv_state
;
427 /* for inflating stuff */
428 static z_stream rx_strm
;
432 /* for decoding runs of tokens */
433 static int32 rx_token
;
436 /* Receive a deflated token and inflate it */
437 static int32
recv_deflated_token(int f
, char **data
)
439 static int init_done
;
440 static int32 saved_flag
;
445 switch (recv_state
) {
448 rx_strm
.next_out
= NULL
;
449 rx_strm
.zalloc
= NULL
;
450 rx_strm
.zfree
= NULL
;
451 if (inflateInit2(&rx_strm
, -15) != Z_OK
) {
452 rprintf(FERROR
, "inflate init failed\n");
453 exit_cleanup(RERR_STREAMIO
);
455 if (!(cbuf
= new_array(char, MAX_DATA_COUNT
))
456 || !(dbuf
= new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE
))))
457 out_of_memory("recv_deflated_token");
460 inflateReset(&rx_strm
);
469 flag
= saved_flag
& 0xff;
473 if ((flag
& 0xC0) == DEFLATED_DATA
) {
474 n
= ((flag
& 0x3f) << 8) + read_byte(f
);
475 read_buf(f
, cbuf
, n
);
476 rx_strm
.next_in
= (Bytef
*)cbuf
;
477 rx_strm
.avail_in
= n
;
478 recv_state
= r_inflating
;
481 if (recv_state
== r_inflated
) {
482 /* check previous inflated stuff ended correctly */
483 rx_strm
.avail_in
= 0;
484 rx_strm
.next_out
= (Bytef
*)dbuf
;
485 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
486 r
= inflate(&rx_strm
, Z_SYNC_FLUSH
);
487 n
= AVAIL_OUT_SIZE(CHUNK_SIZE
) - rx_strm
.avail_out
;
489 * Z_BUF_ERROR just means no progress was
490 * made, i.e. the decompressor didn't have
491 * any pending output for us.
493 if (r
!= Z_OK
&& r
!= Z_BUF_ERROR
) {
494 rprintf(FERROR
, "inflate flush returned %d (%d bytes)\n",
496 exit_cleanup(RERR_STREAMIO
);
498 if (n
!= 0 && r
!= Z_BUF_ERROR
) {
499 /* have to return some more data and
500 save the flag for later. */
501 saved_flag
= flag
+ 0x10000;
506 * At this point the decompressor should
507 * be expecting to see the 0, 0, ff, ff bytes.
509 if (!inflateSyncPoint(&rx_strm
)) {
510 rprintf(FERROR
, "decompressor lost sync!\n");
511 exit_cleanup(RERR_STREAMIO
);
513 rx_strm
.avail_in
= 4;
514 rx_strm
.next_in
= (Bytef
*)cbuf
;
515 cbuf
[0] = cbuf
[1] = 0;
516 cbuf
[2] = cbuf
[3] = 0xff;
517 inflate(&rx_strm
, Z_SYNC_FLUSH
);
520 if (flag
== END_FLAG
) {
521 /* that's all folks */
526 /* here we have a token of some kind */
527 if (flag
& TOKEN_REL
) {
528 rx_token
+= flag
& 0x3f;
531 rx_token
= read_int(f
);
533 rx_run
= read_byte(f
);
534 rx_run
+= read_byte(f
) << 8;
535 recv_state
= r_running
;
537 return -1 - rx_token
;
540 rx_strm
.next_out
= (Bytef
*)dbuf
;
541 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
542 r
= inflate(&rx_strm
, Z_NO_FLUSH
);
543 n
= AVAIL_OUT_SIZE(CHUNK_SIZE
) - rx_strm
.avail_out
;
545 rprintf(FERROR
, "inflate returned %d (%d bytes)\n", r
, n
);
546 exit_cleanup(RERR_STREAMIO
);
548 if (rx_strm
.avail_in
== 0)
549 recv_state
= r_inflated
;
560 return -1 - rx_token
;
566 * put the data corresponding to a token that we've just returned
567 * from recv_deflated_token into the decompressor's history buffer.
569 static void see_deflate_token(char *buf
, int32 len
)
573 unsigned char hdr
[5];
575 rx_strm
.avail_in
= 0;
579 if (rx_strm
.avail_in
== 0 && len
!= 0) {
581 /* Give it a fake stored-block header. */
582 rx_strm
.next_in
= (Bytef
*)hdr
;
583 rx_strm
.avail_in
= 5;
588 hdr
[2] = blklen
>> 8;
592 rx_strm
.next_in
= (Bytef
*)buf
;
593 rx_strm
.avail_in
= blklen
;
598 rx_strm
.next_out
= (Bytef
*)dbuf
;
599 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
600 r
= inflate(&rx_strm
, Z_SYNC_FLUSH
);
601 if (r
!= Z_OK
&& r
!= Z_BUF_ERROR
) {
602 rprintf(FERROR
, "inflate (token) returned %d\n", r
);
603 exit_cleanup(RERR_STREAMIO
);
605 } while (len
|| rx_strm
.avail_out
== 0);
609 * Transmit a verbatim buffer of length @p n followed by a token.
610 * If token == -1 then we have reached EOF
611 * If n == 0 then don't send a buffer
613 void send_token(int f
, int32 token
, struct map_struct
*buf
, OFF_T offset
,
614 int32 n
, int32 toklen
)
617 simple_send_token(f
, token
, buf
, offset
, n
);
619 send_deflated_token(f
, token
, buf
, offset
, n
, toklen
);
623 * receive a token or buffer from the other end. If the reurn value is >0 then
624 * it is a data buffer of that length, and *data will point at the data.
625 * if the return value is -i then it represents token i-1
626 * if the return value is 0 then the end has been reached
628 int32
recv_token(int f
, char **data
)
632 if (!do_compression
) {
633 tok
= simple_recv_token(f
,data
);
635 tok
= recv_deflated_token(f
, data
);
641 * look at the data corresponding to a token, if necessary
643 void see_token(char *data
, int32 toklen
)
646 see_deflate_token(data
, toklen
);