2 * Routines used by the file-transfer code.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2007 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 version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "zlib/zlib.h"
25 extern int do_compression;
27 extern int def_compress_level;
29 static int compression_level, per_file_default_level;
31 /* determine the compression level based on a wildcard filename list */
32 void set_compression(char *fname)
34 static char *match_list;
41 char *t, *f = lp_dont_compress(module_id);
43 if (!(match_list = t = new_array(char, len + 2)))
44 out_of_memory("set_compression");
55 } while (*++f != ' ' && *f);
58 /* Optimize a match-string of "*". */
59 if (t - match_list == 2 && match_list[0] == '*') {
61 per_file_default_level = 0;
63 per_file_default_level = def_compress_level;
67 compression_level = per_file_default_level;
72 if ((s = strrchr(fname, '/')) != NULL)
75 for (s = match_list; *s; s += strlen(s) + 1) {
76 if (iwildmatch(s, fname)) {
77 compression_level = 0;
83 /* non-compressing recv token */
84 static int32 simple_recv_token(int f, char **data)
91 buf = new_array(char, CHUNK_SIZE);
93 out_of_memory("simple_recv_token");
97 int32 i = read_int(f);
104 n = MIN(CHUNK_SIZE,residue);
110 /* non-compressing send token */
111 static void simple_send_token(int f, int32 token, struct map_struct *buf,
112 OFF_T offset, int32 n)
117 int32 n1 = MIN(CHUNK_SIZE, n-len);
119 write_buf(f, map_ptr(buf, offset+len, n1), n1);
123 /* a -2 token means to send data only and no token */
125 write_int(f, -(token+1));
128 /* Flag bytes in compressed stream are encoded as follows: */
129 #define END_FLAG 0 /* that's all folks */
130 #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
131 #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
132 #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
133 #define TOKEN_REL 0x80 /* + 6-bit relative token number */
134 #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
136 #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
138 /* zlib.h says that if we want to be able to compress something in a single
139 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
140 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
141 * to ensure that this is a compile-time value). */
142 #define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
144 /* For coding runs of tokens */
145 static int32 last_token = -1;
146 static int32 run_start;
147 static int32 last_run_end;
149 /* Deflation state */
150 static z_stream tx_strm;
155 /* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
156 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
157 #if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
158 #define OBUF_SIZE (MAX_DATA_COUNT+2)
160 #define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
163 /* Send a deflated token */
165 send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
166 int32 nb, int32 toklen)
169 static int init_done, flush_pending;
171 if (last_token == -1) {
174 tx_strm.next_in = NULL;
175 tx_strm.zalloc = NULL;
176 tx_strm.zfree = NULL;
177 if (deflateInit2(&tx_strm, compression_level,
179 Z_DEFAULT_STRATEGY) != Z_OK) {
180 rprintf(FERROR, "compression init failed\n");
181 exit_cleanup(RERR_STREAMIO);
183 if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
184 out_of_memory("send_deflated_token");
187 deflateReset(&tx_strm);
191 } else if (last_token == -2) {
193 } else if (nb != 0 || token != last_token + 1
194 || token >= run_start + 65536) {
195 /* output previous run */
196 r = run_start - last_run_end;
197 n = last_token - run_start;
198 if (r >= 0 && r <= 63) {
199 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
201 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
202 write_int(f, run_start);
206 write_byte(f, n >> 8);
208 last_run_end = last_token;
214 if (nb != 0 || flush_pending) {
215 /* deflate the data starting at offset */
216 int flush = Z_NO_FLUSH;
217 tx_strm.avail_in = 0;
218 tx_strm.avail_out = 0;
220 if (tx_strm.avail_in == 0 && nb != 0) {
221 /* give it some more input */
222 n = MIN(nb, CHUNK_SIZE);
223 tx_strm.next_in = (Bytef *)
224 map_ptr(buf, offset, n);
225 tx_strm.avail_in = n;
229 if (tx_strm.avail_out == 0) {
230 tx_strm.next_out = (Bytef *)(obuf + 2);
231 tx_strm.avail_out = MAX_DATA_COUNT;
232 if (flush != Z_NO_FLUSH) {
234 * We left the last 4 bytes in the
235 * buffer, in case they are the
236 * last 4. Move them to the front.
238 memcpy(tx_strm.next_out,
239 obuf+MAX_DATA_COUNT-2, 4);
240 tx_strm.next_out += 4;
241 tx_strm.avail_out -= 4;
244 if (nb == 0 && token != -2)
245 flush = Z_SYNC_FLUSH;
246 r = deflate(&tx_strm, flush);
248 rprintf(FERROR, "deflate returned %d\n", r);
249 exit_cleanup(RERR_STREAMIO);
251 if (nb == 0 || tx_strm.avail_out == 0) {
252 n = MAX_DATA_COUNT - tx_strm.avail_out;
253 if (flush != Z_NO_FLUSH) {
255 * We have to trim off the last 4
256 * bytes of output when flushing
257 * (they are just 0, 0, ff, ff).
262 obuf[0] = DEFLATED_DATA + (n >> 8);
264 write_buf(f, obuf, n+2);
267 } while (nb != 0 || tx_strm.avail_out == 0);
268 flush_pending = token == -2;
272 /* end of file - clean up */
273 write_byte(f, END_FLAG);
274 } else if (token != -2) {
275 /* Add the data in the current block to the compressor's
276 * history and hash table. */
278 /* Break up long sections in the same way that
279 * see_deflate_token() does. */
280 int32 n1 = toklen > 0xffff ? 0xffff : toklen;
282 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n1);
283 tx_strm.avail_in = n1;
284 tx_strm.next_out = (Bytef *) obuf;
285 tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
286 r = deflate(&tx_strm, Z_INSERT_ONLY);
287 if (r != Z_OK || tx_strm.avail_in != 0) {
288 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
289 r, tx_strm.avail_in);
290 exit_cleanup(RERR_STREAMIO);
292 } while (toklen > 0);
296 /* tells us what the receiver is in the middle of doing */
297 static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
299 /* for inflating stuff */
300 static z_stream rx_strm;
304 /* for decoding runs of tokens */
305 static int32 rx_token;
308 /* Receive a deflated token and inflate it */
309 static int32 recv_deflated_token(int f, char **data)
311 static int init_done;
312 static int32 saved_flag;
317 switch (recv_state) {
320 rx_strm.next_out = NULL;
321 rx_strm.zalloc = NULL;
322 rx_strm.zfree = NULL;
323 if (inflateInit2(&rx_strm, -15) != Z_OK) {
324 rprintf(FERROR, "inflate init failed\n");
325 exit_cleanup(RERR_STREAMIO);
327 if (!(cbuf = new_array(char, MAX_DATA_COUNT))
328 || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
329 out_of_memory("recv_deflated_token");
332 inflateReset(&rx_strm);
341 flag = saved_flag & 0xff;
345 if ((flag & 0xC0) == DEFLATED_DATA) {
346 n = ((flag & 0x3f) << 8) + read_byte(f);
347 read_buf(f, cbuf, n);
348 rx_strm.next_in = (Bytef *)cbuf;
349 rx_strm.avail_in = n;
350 recv_state = r_inflating;
353 if (recv_state == r_inflated) {
354 /* check previous inflated stuff ended correctly */
355 rx_strm.avail_in = 0;
356 rx_strm.next_out = (Bytef *)dbuf;
357 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
358 r = inflate(&rx_strm, Z_SYNC_FLUSH);
359 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
361 * Z_BUF_ERROR just means no progress was
362 * made, i.e. the decompressor didn't have
363 * any pending output for us.
365 if (r != Z_OK && r != Z_BUF_ERROR) {
366 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
368 exit_cleanup(RERR_STREAMIO);
370 if (n != 0 && r != Z_BUF_ERROR) {
371 /* have to return some more data and
372 save the flag for later. */
373 saved_flag = flag + 0x10000;
378 * At this point the decompressor should
379 * be expecting to see the 0, 0, ff, ff bytes.
381 if (!inflateSyncPoint(&rx_strm)) {
382 rprintf(FERROR, "decompressor lost sync!\n");
383 exit_cleanup(RERR_STREAMIO);
385 rx_strm.avail_in = 4;
386 rx_strm.next_in = (Bytef *)cbuf;
387 cbuf[0] = cbuf[1] = 0;
388 cbuf[2] = cbuf[3] = 0xff;
389 inflate(&rx_strm, Z_SYNC_FLUSH);
392 if (flag == END_FLAG) {
393 /* that's all folks */
398 /* here we have a token of some kind */
399 if (flag & TOKEN_REL) {
400 rx_token += flag & 0x3f;
403 rx_token = read_int(f);
405 rx_run = read_byte(f);
406 rx_run += read_byte(f) << 8;
407 recv_state = r_running;
409 return -1 - rx_token;
412 rx_strm.next_out = (Bytef *)dbuf;
413 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
414 r = inflate(&rx_strm, Z_NO_FLUSH);
415 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
417 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
418 exit_cleanup(RERR_STREAMIO);
420 if (rx_strm.avail_in == 0)
421 recv_state = r_inflated;
432 return -1 - rx_token;
438 * put the data corresponding to a token that we've just returned
439 * from recv_deflated_token into the decompressor's history buffer.
441 static void see_deflate_token(char *buf, int32 len)
445 unsigned char hdr[5];
447 rx_strm.avail_in = 0;
451 if (rx_strm.avail_in == 0 && len != 0) {
453 /* Give it a fake stored-block header. */
454 rx_strm.next_in = (Bytef *)hdr;
455 rx_strm.avail_in = 5;
460 hdr[2] = blklen >> 8;
464 rx_strm.next_in = (Bytef *)buf;
465 rx_strm.avail_in = blklen;
470 rx_strm.next_out = (Bytef *)dbuf;
471 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
472 r = inflate(&rx_strm, Z_SYNC_FLUSH);
474 rprintf(FERROR, "inflate (token) returned %d\n", r);
475 exit_cleanup(RERR_STREAMIO);
477 } while (len || rx_strm.avail_out == 0);
481 * Transmit a verbatim buffer of length @p n followed by a token.
482 * If token == -1 then we have reached EOF
483 * If n == 0 then don't send a buffer
485 void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
486 int32 n, int32 toklen)
489 simple_send_token(f, token, buf, offset, n);
491 send_deflated_token(f, token, buf, offset, n, toklen);
495 * receive a token or buffer from the other end. If the reurn value is >0 then
496 * it is a data buffer of that length, and *data will point at the data.
497 * if the return value is -i then it represents token i-1
498 * if the return value is 0 then the end has been reached
500 int32 recv_token(int f, char **data)
504 if (!do_compression) {
505 tok = simple_recv_token(f,data);
507 tok = recv_deflated_token(f, data);
513 * look at the data corresponding to a token, if necessary
515 void see_token(char *data, int32 toklen)
518 see_deflate_token(data, toklen);