2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "zlib/zlib.h"
23 extern int do_compression;
26 static int compression_level = Z_DEFAULT_COMPRESSION;
28 /* determine the compression level based on a wildcard filename list */
29 void set_compression(char *fname)
37 compression_level = Z_DEFAULT_COMPRESSION;
38 dont = lp_dont_compress(module_id);
43 if (dont[0] == '*' && !dont[1]) {
44 /* an optimization to skip the rest of this routine */
45 compression_level = 0;
50 fname = strdup(fname);
57 for (tok = strtok(dont, " "); tok; tok = strtok(NULL, " ")) {
58 if (wildmatch(tok, fname)) {
59 compression_level = 0;
67 /* non-compressing recv token */
68 static int32 simple_recv_token(int f, char **data)
75 buf = new_array(char, CHUNK_SIZE);
77 out_of_memory("simple_recv_token");
81 int32 i = read_int(f);
88 n = MIN(CHUNK_SIZE,residue);
95 /* non-compressing send token */
96 static void simple_send_token(int f, int token, struct map_struct *buf,
97 OFF_T offset, int32 n)
102 int32 n1 = MIN(CHUNK_SIZE, n-len);
104 write_buf(f, map_ptr(buf, offset+len, n1), n1);
108 /* a -2 token means to send data only and no token */
110 write_int(f, -(token+1));
114 /* Flag bytes in compressed stream are encoded as follows: */
115 #define END_FLAG 0 /* that's all folks */
116 #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
117 #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
118 #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
119 #define TOKEN_REL 0x80 /* + 6-bit relative token number */
120 #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
122 #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
124 /* zlib.h says that if we want to be able to compress something in a single
125 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
126 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
127 * to ensure that this is a compile-time value). */
128 #define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
130 /* For coding runs of tokens */
131 static int last_token = -1;
132 static int run_start;
133 static int last_run_end;
135 /* Deflation state */
136 static z_stream tx_strm;
141 /* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
142 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
143 #if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
144 #define OBUF_SIZE (MAX_DATA_COUNT+2)
146 #define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
149 /* Send a deflated token */
151 send_deflated_token(int f, int token, struct map_struct *buf, OFF_T offset,
152 int32 nb, int32 toklen)
155 static int init_done, flush_pending;
157 if (last_token == -1) {
160 tx_strm.next_in = NULL;
161 tx_strm.zalloc = NULL;
162 tx_strm.zfree = NULL;
163 if (deflateInit2(&tx_strm, compression_level,
165 Z_DEFAULT_STRATEGY) != Z_OK) {
166 rprintf(FERROR, "compression init failed\n");
167 exit_cleanup(RERR_STREAMIO);
169 if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
170 out_of_memory("send_deflated_token");
173 deflateReset(&tx_strm);
178 } else if (last_token == -2) {
181 } else if (nb != 0 || token != last_token + 1
182 || token >= run_start + 65536) {
183 /* output previous run */
184 r = run_start - last_run_end;
185 n = last_token - run_start;
186 if (r >= 0 && r <= 63) {
187 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
189 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
190 write_int(f, run_start);
194 write_byte(f, n >> 8);
196 last_run_end = last_token;
202 if (nb != 0 || flush_pending) {
203 /* deflate the data starting at offset */
204 int flush = Z_NO_FLUSH;
205 tx_strm.avail_in = 0;
206 tx_strm.avail_out = 0;
208 if (tx_strm.avail_in == 0 && nb != 0) {
209 /* give it some more input */
210 n = MIN(nb, CHUNK_SIZE);
211 tx_strm.next_in = (Bytef *)
212 map_ptr(buf, offset, n);
213 tx_strm.avail_in = n;
217 if (tx_strm.avail_out == 0) {
218 tx_strm.next_out = (Bytef *)(obuf + 2);
219 tx_strm.avail_out = MAX_DATA_COUNT;
220 if (flush != Z_NO_FLUSH) {
222 * We left the last 4 bytes in the
223 * buffer, in case they are the
224 * last 4. Move them to the front.
226 memcpy(tx_strm.next_out,
227 obuf+MAX_DATA_COUNT-2, 4);
228 tx_strm.next_out += 4;
229 tx_strm.avail_out -= 4;
232 if (nb == 0 && token != -2)
233 flush = Z_SYNC_FLUSH;
234 r = deflate(&tx_strm, flush);
236 rprintf(FERROR, "deflate returned %d\n", r);
237 exit_cleanup(RERR_STREAMIO);
239 if (nb == 0 || tx_strm.avail_out == 0) {
240 n = MAX_DATA_COUNT - tx_strm.avail_out;
241 if (flush != Z_NO_FLUSH) {
243 * We have to trim off the last 4
244 * bytes of output when flushing
245 * (they are just 0, 0, ff, ff).
250 obuf[0] = DEFLATED_DATA + (n >> 8);
252 write_buf(f, obuf, n+2);
255 } while (nb != 0 || tx_strm.avail_out == 0);
256 flush_pending = token == -2;
260 /* end of file - clean up */
261 write_byte(f, END_FLAG);
262 } else if (token != -2) {
263 /* add the data in the current block to the compressor's
264 history and hash table */
265 tx_strm.next_in = (Bytef *) map_ptr(buf, offset, toklen);
266 tx_strm.avail_in = toklen;
267 tx_strm.next_out = (Bytef *) obuf;
268 tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
269 r = deflate(&tx_strm, Z_INSERT_ONLY);
270 if (r != Z_OK || tx_strm.avail_in != 0) {
271 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
272 r, tx_strm.avail_in);
273 exit_cleanup(RERR_STREAMIO);
279 /* tells us what the receiver is in the middle of doing */
280 static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
282 /* for inflating stuff */
283 static z_stream rx_strm;
287 /* for decoding runs of tokens */
288 static int32 rx_token;
291 /* Receive a deflated token and inflate it */
292 static int32 recv_deflated_token(int f, char **data)
294 static int init_done;
295 static int saved_flag;
300 switch (recv_state) {
303 rx_strm.next_out = NULL;
304 rx_strm.zalloc = NULL;
305 rx_strm.zfree = NULL;
306 if (inflateInit2(&rx_strm, -15) != Z_OK) {
307 rprintf(FERROR, "inflate init failed\n");
308 exit_cleanup(RERR_STREAMIO);
310 if (!(cbuf = new_array(char, MAX_DATA_COUNT))
311 || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
312 out_of_memory("recv_deflated_token");
315 inflateReset(&rx_strm);
324 flag = saved_flag & 0xff;
328 if ((flag & 0xC0) == DEFLATED_DATA) {
329 n = ((flag & 0x3f) << 8) + read_byte(f);
330 read_buf(f, cbuf, n);
331 rx_strm.next_in = (Bytef *)cbuf;
332 rx_strm.avail_in = n;
333 recv_state = r_inflating;
336 if (recv_state == r_inflated) {
337 /* check previous inflated stuff ended correctly */
338 rx_strm.avail_in = 0;
339 rx_strm.next_out = (Bytef *)dbuf;
340 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
341 r = inflate(&rx_strm, Z_SYNC_FLUSH);
342 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
344 * Z_BUF_ERROR just means no progress was
345 * made, i.e. the decompressor didn't have
346 * any pending output for us.
348 if (r != Z_OK && r != Z_BUF_ERROR) {
349 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
351 exit_cleanup(RERR_STREAMIO);
353 if (n != 0 && r != Z_BUF_ERROR) {
354 /* have to return some more data and
355 save the flag for later. */
356 saved_flag = flag + 0x10000;
361 * At this point the decompressor should
362 * be expecting to see the 0, 0, ff, ff bytes.
364 if (!inflateSyncPoint(&rx_strm)) {
365 rprintf(FERROR, "decompressor lost sync!\n");
366 exit_cleanup(RERR_STREAMIO);
368 rx_strm.avail_in = 4;
369 rx_strm.next_in = (Bytef *)cbuf;
370 cbuf[0] = cbuf[1] = 0;
371 cbuf[2] = cbuf[3] = 0xff;
372 inflate(&rx_strm, Z_SYNC_FLUSH);
375 if (flag == END_FLAG) {
376 /* that's all folks */
381 /* here we have a token of some kind */
382 if (flag & TOKEN_REL) {
383 rx_token += flag & 0x3f;
386 rx_token = read_int(f);
388 rx_run = read_byte(f);
389 rx_run += read_byte(f) << 8;
390 recv_state = r_running;
392 return -1 - rx_token;
395 rx_strm.next_out = (Bytef *)dbuf;
396 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
397 r = inflate(&rx_strm, Z_NO_FLUSH);
398 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
400 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
401 exit_cleanup(RERR_STREAMIO);
403 if (rx_strm.avail_in == 0)
404 recv_state = r_inflated;
415 return -1 - rx_token;
421 * put the data corresponding to a token that we've just returned
422 * from recv_deflated_token into the decompressor's history buffer.
424 static void see_deflate_token(char *buf, int32 len)
427 unsigned char hdr[5];
429 rx_strm.avail_in = 0;
433 if (rx_strm.avail_in == 0 && len != 0) {
435 /* Give it a fake stored-block header. */
436 rx_strm.next_in = (Bytef *)hdr;
437 rx_strm.avail_in = 5;
442 hdr[2] = blklen >> 8;
446 rx_strm.next_in = (Bytef *)buf;
447 rx_strm.avail_in = blklen;
452 rx_strm.next_out = (Bytef *)dbuf;
453 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
454 r = inflate(&rx_strm, Z_SYNC_FLUSH);
456 rprintf(FERROR, "inflate (token) returned %d\n", r);
457 exit_cleanup(RERR_STREAMIO);
459 } while (len || rx_strm.avail_out == 0);
463 * Transmit a verbatim buffer of length @p n followed by a token.
464 * If token == -1 then we have reached EOF
465 * If n == 0 then don't send a buffer
467 void send_token(int f, int token, struct map_struct *buf, OFF_T offset,
468 int32 n, int32 toklen)
471 simple_send_token(f, token, buf, offset, n);
473 send_deflated_token(f, token, buf, offset, n, toklen);
478 * receive a token or buffer from the other end. If the reurn value is >0 then
479 * it is a data buffer of that length, and *data will point at the data.
480 * if the return value is -i then it represents token i-1
481 * if the return value is 0 then the end has been reached
483 int32 recv_token(int f, char **data)
487 if (!do_compression) {
488 tok = simple_recv_token(f,data);
490 tok = recv_deflated_token(f, data);
496 * look at the data corresponding to a token, if necessary
498 void see_token(char *data, int32 toklen)
501 see_deflate_token(data, toklen);