2 * Routines used by the file-transfer code.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003, 2004, 2005 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 2 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, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "zlib/zlib.h"
26 extern int do_compression;
28 extern int def_compress_level;
30 static int compression_level, per_file_default_level;
32 /* determine the compression level based on a wildcard filename list */
33 void set_compression(char *fname)
35 static char *match_list;
42 char *t, *f = lp_dont_compress(module_id);
44 if (!(match_list = t = new_array(char, len + 2)))
45 out_of_memory("set_compression");
56 } while (*++f != ' ' && *f);
59 /* Optimize a match-string of "*". */
60 if (t - match_list == 2 && match_list[0] == '*') {
62 per_file_default_level = 0;
64 per_file_default_level = def_compress_level;
68 compression_level = per_file_default_level;
73 if ((s = strrchr(fname, '/')) != NULL)
76 for (s = match_list; *s; s += strlen(s) + 1) {
77 if (iwildmatch(s, fname)) {
78 compression_level = 0;
84 /* non-compressing recv token */
85 static int32 simple_recv_token(int f, char **data)
92 buf = new_array(char, CHUNK_SIZE);
94 out_of_memory("simple_recv_token");
98 int32 i = read_int(f);
105 n = MIN(CHUNK_SIZE,residue);
111 /* non-compressing send token */
112 static void simple_send_token(int f, int32 token, struct map_struct *buf,
113 OFF_T offset, int32 n)
118 int32 n1 = MIN(CHUNK_SIZE, n-len);
120 write_buf(f, map_ptr(buf, offset+len, n1), n1);
124 /* a -2 token means to send data only and no token */
126 write_int(f, -(token+1));
129 /* Flag bytes in compressed stream are encoded as follows: */
130 #define END_FLAG 0 /* that's all folks */
131 #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
132 #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
133 #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
134 #define TOKEN_REL 0x80 /* + 6-bit relative token number */
135 #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
137 #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
139 /* zlib.h says that if we want to be able to compress something in a single
140 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
141 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
142 * to ensure that this is a compile-time value). */
143 #define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
145 /* For coding runs of tokens */
146 static int32 last_token = -1;
147 static int32 run_start;
148 static int32 last_run_end;
150 /* Deflation state */
151 static z_stream tx_strm;
156 /* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
157 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
158 #if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
159 #define OBUF_SIZE (MAX_DATA_COUNT+2)
161 #define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
164 /* Send a deflated token */
166 send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
167 int32 nb, int32 toklen)
170 static int init_done, flush_pending;
172 if (last_token == -1) {
175 tx_strm.next_in = NULL;
176 tx_strm.zalloc = NULL;
177 tx_strm.zfree = NULL;
178 if (deflateInit2(&tx_strm, compression_level,
180 Z_DEFAULT_STRATEGY) != Z_OK) {
181 rprintf(FERROR, "compression init failed\n");
182 exit_cleanup(RERR_STREAMIO);
184 if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
185 out_of_memory("send_deflated_token");
188 deflateReset(&tx_strm);
192 } else if (last_token == -2) {
194 } else if (nb != 0 || token != last_token + 1
195 || token >= run_start + 65536) {
196 /* output previous run */
197 r = run_start - last_run_end;
198 n = last_token - run_start;
199 if (r >= 0 && r <= 63) {
200 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
202 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
203 write_int(f, run_start);
207 write_byte(f, n >> 8);
209 last_run_end = last_token;
215 if (nb != 0 || flush_pending) {
216 /* deflate the data starting at offset */
217 int flush = Z_NO_FLUSH;
218 tx_strm.avail_in = 0;
219 tx_strm.avail_out = 0;
221 if (tx_strm.avail_in == 0 && nb != 0) {
222 /* give it some more input */
223 n = MIN(nb, CHUNK_SIZE);
224 tx_strm.next_in = (Bytef *)
225 map_ptr(buf, offset, n);
226 tx_strm.avail_in = n;
230 if (tx_strm.avail_out == 0) {
231 tx_strm.next_out = (Bytef *)(obuf + 2);
232 tx_strm.avail_out = MAX_DATA_COUNT;
233 if (flush != Z_NO_FLUSH) {
235 * We left the last 4 bytes in the
236 * buffer, in case they are the
237 * last 4. Move them to the front.
239 memcpy(tx_strm.next_out,
240 obuf+MAX_DATA_COUNT-2, 4);
241 tx_strm.next_out += 4;
242 tx_strm.avail_out -= 4;
245 if (nb == 0 && token != -2)
246 flush = Z_SYNC_FLUSH;
247 r = deflate(&tx_strm, flush);
249 rprintf(FERROR, "deflate returned %d\n", r);
250 exit_cleanup(RERR_STREAMIO);
252 if (nb == 0 || tx_strm.avail_out == 0) {
253 n = MAX_DATA_COUNT - tx_strm.avail_out;
254 if (flush != Z_NO_FLUSH) {
256 * We have to trim off the last 4
257 * bytes of output when flushing
258 * (they are just 0, 0, ff, ff).
263 obuf[0] = DEFLATED_DATA + (n >> 8);
265 write_buf(f, obuf, n+2);
268 } while (nb != 0 || tx_strm.avail_out == 0);
269 flush_pending = token == -2;
273 /* end of file - clean up */
274 write_byte(f, END_FLAG);
275 } else if (token != -2) {
276 /* Add the data in the current block to the compressor's
277 * history and hash table. */
279 /* Break up long sections in the same way that
280 * see_deflate_token() does. */
281 int32 n1 = toklen > 0xffff ? 0xffff : toklen;
283 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n1);
284 tx_strm.avail_in = n1;
285 tx_strm.next_out = (Bytef *) obuf;
286 tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
287 r = deflate(&tx_strm, Z_INSERT_ONLY);
288 if (r != Z_OK || tx_strm.avail_in != 0) {
289 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
290 r, tx_strm.avail_in);
291 exit_cleanup(RERR_STREAMIO);
293 } while (toklen > 0);
297 /* tells us what the receiver is in the middle of doing */
298 static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
300 /* for inflating stuff */
301 static z_stream rx_strm;
305 /* for decoding runs of tokens */
306 static int32 rx_token;
309 /* Receive a deflated token and inflate it */
310 static int32 recv_deflated_token(int f, char **data)
312 static int init_done;
313 static int32 saved_flag;
318 switch (recv_state) {
321 rx_strm.next_out = NULL;
322 rx_strm.zalloc = NULL;
323 rx_strm.zfree = NULL;
324 if (inflateInit2(&rx_strm, -15) != Z_OK) {
325 rprintf(FERROR, "inflate init failed\n");
326 exit_cleanup(RERR_STREAMIO);
328 if (!(cbuf = new_array(char, MAX_DATA_COUNT))
329 || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
330 out_of_memory("recv_deflated_token");
333 inflateReset(&rx_strm);
342 flag = saved_flag & 0xff;
346 if ((flag & 0xC0) == DEFLATED_DATA) {
347 n = ((flag & 0x3f) << 8) + read_byte(f);
348 read_buf(f, cbuf, n);
349 rx_strm.next_in = (Bytef *)cbuf;
350 rx_strm.avail_in = n;
351 recv_state = r_inflating;
354 if (recv_state == r_inflated) {
355 /* check previous inflated stuff ended correctly */
356 rx_strm.avail_in = 0;
357 rx_strm.next_out = (Bytef *)dbuf;
358 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
359 r = inflate(&rx_strm, Z_SYNC_FLUSH);
360 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
362 * Z_BUF_ERROR just means no progress was
363 * made, i.e. the decompressor didn't have
364 * any pending output for us.
366 if (r != Z_OK && r != Z_BUF_ERROR) {
367 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
369 exit_cleanup(RERR_STREAMIO);
371 if (n != 0 && r != Z_BUF_ERROR) {
372 /* have to return some more data and
373 save the flag for later. */
374 saved_flag = flag + 0x10000;
379 * At this point the decompressor should
380 * be expecting to see the 0, 0, ff, ff bytes.
382 if (!inflateSyncPoint(&rx_strm)) {
383 rprintf(FERROR, "decompressor lost sync!\n");
384 exit_cleanup(RERR_STREAMIO);
386 rx_strm.avail_in = 4;
387 rx_strm.next_in = (Bytef *)cbuf;
388 cbuf[0] = cbuf[1] = 0;
389 cbuf[2] = cbuf[3] = 0xff;
390 inflate(&rx_strm, Z_SYNC_FLUSH);
393 if (flag == END_FLAG) {
394 /* that's all folks */
399 /* here we have a token of some kind */
400 if (flag & TOKEN_REL) {
401 rx_token += flag & 0x3f;
404 rx_token = read_int(f);
406 rx_run = read_byte(f);
407 rx_run += read_byte(f) << 8;
408 recv_state = r_running;
410 return -1 - rx_token;
413 rx_strm.next_out = (Bytef *)dbuf;
414 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
415 r = inflate(&rx_strm, Z_NO_FLUSH);
416 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
418 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
419 exit_cleanup(RERR_STREAMIO);
421 if (rx_strm.avail_in == 0)
422 recv_state = r_inflated;
433 return -1 - rx_token;
439 * put the data corresponding to a token that we've just returned
440 * from recv_deflated_token into the decompressor's history buffer.
442 static void see_deflate_token(char *buf, int32 len)
446 unsigned char hdr[5];
448 rx_strm.avail_in = 0;
452 if (rx_strm.avail_in == 0 && len != 0) {
454 /* Give it a fake stored-block header. */
455 rx_strm.next_in = (Bytef *)hdr;
456 rx_strm.avail_in = 5;
461 hdr[2] = blklen >> 8;
465 rx_strm.next_in = (Bytef *)buf;
466 rx_strm.avail_in = blklen;
471 rx_strm.next_out = (Bytef *)dbuf;
472 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
473 r = inflate(&rx_strm, Z_SYNC_FLUSH);
475 rprintf(FERROR, "inflate (token) returned %d\n", r);
476 exit_cleanup(RERR_STREAMIO);
478 } while (len || rx_strm.avail_out == 0);
482 * Transmit a verbatim buffer of length @p n followed by a token.
483 * If token == -1 then we have reached EOF
484 * If n == 0 then don't send a buffer
486 void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
487 int32 n, int32 toklen)
490 simple_send_token(f, token, buf, offset, n);
492 send_deflated_token(f, token, buf, offset, n, toklen);
496 * receive a token or buffer from the other end. If the reurn value is >0 then
497 * it is a data buffer of that length, and *data will point at the data.
498 * if the return value is -i then it represents token i-1
499 * if the return value is 0 then the end has been reached
501 int32 recv_token(int f, char **data)
505 if (!do_compression) {
506 tok = simple_recv_token(f,data);
508 tok = recv_deflated_token(f, data);
514 * look at the data corresponding to a token, if necessary
516 void see_token(char *data, int32 toklen)
519 see_deflate_token(data, toklen);