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;
24 static int compression_level = Z_DEFAULT_COMPRESSION;
26 /* determine the compression level based on a wildcard filename list */
27 void set_compression(char *fname)
33 if (!do_compression) return;
35 compression_level = Z_DEFAULT_COMPRESSION;
36 dont = lp_dont_compress(module_id);
38 if (!dont || !*dont) return;
41 fname = strdup(fname);
42 if (!dont || !fname) return;
47 for (tok=strtok(dont," ");tok;tok=strtok(NULL," ")) {
48 if (fnmatch(tok, fname, 0) == 0) {
49 compression_level = 0;
57 /* non-compressing recv token */
58 static int simple_recv_token(int f,char **data)
65 buf = (char *)malloc(CHUNK_SIZE);
66 if (!buf) out_of_memory("simple_recv_token");
76 n = MIN(CHUNK_SIZE,residue);
83 /* non-compressing send token */
84 static void simple_send_token(int f,int token,
85 struct map_struct *buf,OFF_T offset,int n)
90 int n1 = MIN(CHUNK_SIZE,n-l);
92 write_buf(f,map_ptr(buf,offset+l,n1),n1);
96 /* a -2 token means to send data only and no token */
98 write_int(f,-(token+1));
103 /* Flag bytes in compressed stream are encoded as follows: */
104 #define END_FLAG 0 /* that's all folks */
105 #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
106 #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
107 #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
108 #define TOKEN_REL 0x80 /* + 6-bit relative token number */
109 #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
111 #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
113 /* For coding runs of tokens */
114 static int last_token = -1;
115 static int run_start;
116 static int last_run_end;
118 /* Deflation state */
119 static z_stream tx_strm;
124 /* Send a deflated token */
126 send_deflated_token(int f, int token,
127 struct map_struct *buf, OFF_T offset, int nb, int toklen)
130 static int init_done, flush_pending;
132 if (last_token == -1) {
135 tx_strm.next_in = NULL;
136 tx_strm.zalloc = NULL;
137 tx_strm.zfree = NULL;
138 if (deflateInit2(&tx_strm, compression_level,
140 Z_DEFAULT_STRATEGY) != Z_OK) {
141 rprintf(FERROR, "compression init failed\n");
142 exit_cleanup(RERR_STREAMIO);
144 if ((obuf = malloc(MAX_DATA_COUNT+2)) == NULL)
145 out_of_memory("send_deflated_token");
148 deflateReset(&tx_strm);
153 } else if (last_token == -2) {
156 } else if (nb != 0 || token != last_token + 1
157 || token >= run_start + 65536) {
158 /* output previous run */
159 r = run_start - last_run_end;
160 n = last_token - run_start;
161 if (r >= 0 && r <= 63) {
162 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
164 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
165 write_int(f, run_start);
169 write_byte(f, n >> 8);
171 last_run_end = last_token;
177 if (nb != 0 || flush_pending) {
178 /* deflate the data starting at offset */
179 int flush = Z_NO_FLUSH;
180 tx_strm.avail_in = 0;
181 tx_strm.avail_out = 0;
183 if (tx_strm.avail_in == 0 && nb != 0) {
184 /* give it some more input */
185 n = MIN(nb, CHUNK_SIZE);
186 tx_strm.next_in = (Bytef *)
187 map_ptr(buf, offset, n);
188 tx_strm.avail_in = n;
192 if (tx_strm.avail_out == 0) {
193 tx_strm.next_out = (Bytef *)(obuf + 2);
194 tx_strm.avail_out = MAX_DATA_COUNT;
195 if (flush != Z_NO_FLUSH) {
197 * We left the last 4 bytes in the
198 * buffer, in case they are the
199 * last 4. Move them to the front.
201 memcpy(tx_strm.next_out,
202 obuf+MAX_DATA_COUNT-2, 4);
203 tx_strm.next_out += 4;
204 tx_strm.avail_out -= 4;
207 if (nb == 0 && token != -2)
208 flush = Z_SYNC_FLUSH;
209 r = deflate(&tx_strm, flush);
211 rprintf(FERROR, "deflate returned %d\n", r);
212 exit_cleanup(RERR_STREAMIO);
214 if (nb == 0 || tx_strm.avail_out == 0) {
215 n = MAX_DATA_COUNT - tx_strm.avail_out;
216 if (flush != Z_NO_FLUSH) {
218 * We have to trim off the last 4
219 * bytes of output when flushing
220 * (they are just 0, 0, ff, ff).
225 obuf[0] = DEFLATED_DATA + (n >> 8);
227 write_buf(f, obuf, n+2);
230 } while (nb != 0 || tx_strm.avail_out == 0);
231 flush_pending = token == -2;
235 /* end of file - clean up */
236 write_byte(f, END_FLAG);
238 } else if (token != -2) {
239 /* add the data in the current block to the compressor's
240 history and hash table */
241 tx_strm.next_in = (Bytef *) map_ptr(buf, offset, toklen);
242 tx_strm.avail_in = toklen;
243 tx_strm.next_out = (Bytef *) obuf;
244 tx_strm.avail_out = MAX_DATA_COUNT;
245 r = deflate(&tx_strm, Z_INSERT_ONLY);
246 if (r != Z_OK || tx_strm.avail_in != 0) {
247 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
248 r, tx_strm.avail_in);
249 exit_cleanup(RERR_STREAMIO);
255 /* tells us what the receiver is in the middle of doing */
256 static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
258 /* for inflating stuff */
259 static z_stream rx_strm;
263 /* for decoding runs of tokens */
267 /* Receive a deflated token and inflate it */
269 recv_deflated_token(int f, char **data)
272 static int init_done;
273 static int saved_flag;
276 switch (recv_state) {
279 rx_strm.next_out = NULL;
280 rx_strm.zalloc = NULL;
281 rx_strm.zfree = NULL;
282 if (inflateInit2(&rx_strm, -15) != Z_OK) {
283 rprintf(FERROR, "inflate init failed\n");
284 exit_cleanup(RERR_STREAMIO);
286 if ((cbuf = malloc(MAX_DATA_COUNT)) == NULL
287 || (dbuf = malloc(CHUNK_SIZE)) == NULL)
288 out_of_memory("recv_deflated_token");
291 inflateReset(&rx_strm);
300 flag = saved_flag & 0xff;
304 if ((flag & 0xC0) == DEFLATED_DATA) {
305 n = ((flag & 0x3f) << 8) + read_byte(f);
306 read_buf(f, cbuf, n);
307 rx_strm.next_in = (Bytef *)cbuf;
308 rx_strm.avail_in = n;
309 recv_state = r_inflating;
312 if (recv_state == r_inflated) {
313 /* check previous inflated stuff ended correctly */
314 rx_strm.avail_in = 0;
315 rx_strm.next_out = (Bytef *)dbuf;
316 rx_strm.avail_out = CHUNK_SIZE;
317 r = inflate(&rx_strm, Z_SYNC_FLUSH);
318 n = CHUNK_SIZE - rx_strm.avail_out;
320 * Z_BUF_ERROR just means no progress was
321 * made, i.e. the decompressor didn't have
322 * any pending output for us.
324 if (r != Z_OK && r != Z_BUF_ERROR) {
325 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
327 exit_cleanup(RERR_STREAMIO);
329 if (n != 0 && r != Z_BUF_ERROR) {
330 /* have to return some more data and
331 save the flag for later. */
332 saved_flag = flag + 0x10000;
337 * At this point the decompressor should
338 * be expecting to see the 0, 0, ff, ff bytes.
340 if (!inflateSyncPoint(&rx_strm)) {
341 rprintf(FERROR, "decompressor lost sync!\n");
342 exit_cleanup(RERR_STREAMIO);
344 rx_strm.avail_in = 4;
345 rx_strm.next_in = (Bytef *)cbuf;
346 cbuf[0] = cbuf[1] = 0;
347 cbuf[2] = cbuf[3] = 0xff;
348 inflate(&rx_strm, Z_SYNC_FLUSH);
351 if (flag == END_FLAG) {
352 /* that's all folks */
357 /* here we have a token of some kind */
358 if (flag & TOKEN_REL) {
359 rx_token += flag & 0x3f;
362 rx_token = read_int(f);
364 rx_run = read_byte(f);
365 rx_run += read_byte(f) << 8;
366 recv_state = r_running;
368 return -1 - rx_token;
371 rx_strm.next_out = (Bytef *)dbuf;
372 rx_strm.avail_out = CHUNK_SIZE;
373 r = inflate(&rx_strm, Z_NO_FLUSH);
374 n = CHUNK_SIZE - rx_strm.avail_out;
376 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
377 exit_cleanup(RERR_STREAMIO);
379 if (rx_strm.avail_in == 0)
380 recv_state = r_inflated;
391 return -1 - rx_token;
397 * put the data corresponding to a token that we've just returned
398 * from recv_deflated_token into the decompressor's history buffer.
400 static void see_deflate_token(char *buf, int len)
403 unsigned char hdr[5];
405 rx_strm.avail_in = 0;
409 if (rx_strm.avail_in == 0 && len != 0) {
411 /* Give it a fake stored-block header. */
412 rx_strm.next_in = (Bytef *)hdr;
413 rx_strm.avail_in = 5;
418 hdr[2] = blklen >> 8;
422 rx_strm.next_in = (Bytef *)buf;
423 rx_strm.avail_in = blklen;
428 rx_strm.next_out = (Bytef *)dbuf;
429 rx_strm.avail_out = CHUNK_SIZE;
430 r = inflate(&rx_strm, Z_SYNC_FLUSH);
432 rprintf(FERROR, "inflate (token) returned %d\n", r);
433 exit_cleanup(RERR_STREAMIO);
435 } while (len || rx_strm.avail_out == 0);
439 * transmit a verbatim buffer of length n followed by a token
440 * If token == -1 then we have reached EOF
441 * If n == 0 then don't send a buffer
443 void send_token(int f,int token,struct map_struct *buf,OFF_T offset,
446 if (!do_compression) {
447 simple_send_token(f,token,buf,offset,n);
449 send_deflated_token(f, token, buf, offset, n, toklen);
455 * receive a token or buffer from the other end. If the reurn value is >0 then
456 * it is a data buffer of that length, and *data will point at the data.
457 * if the return value is -i then it represents token i-1
458 * if the return value is 0 then the end has been reached
460 int recv_token(int f,char **data)
464 if (!do_compression) {
465 tok = simple_recv_token(f,data);
467 tok = recv_deflated_token(f, data);
473 * look at the data corresponding to a token, if necessary
475 void see_token(char *data, int toklen)
478 see_deflate_token(data, toklen);