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 int default_compression_level = Z_DEFAULT_COMPRESSION;
25 static int compression_level = Z_DEFAULT_COMPRESSION;
27 /* determine the compression level based on a wildcard filename list */
28 void set_compression(char *fname)
34 if (!do_compression) return;
36 compression_level = default_compression_level;
37 dont = lp_dont_compress(module_id);
39 if (!dont || !*dont) return;
42 fname = strdup(fname);
43 if (!dont || !fname) return;
48 for (tok=strtok(dont," ");tok;tok=strtok(NULL," ")) {
49 if (fnmatch(tok, fname, 0) == 0) {
50 compression_level = 0;
58 /* non-compressing recv token */
59 static int simple_recv_token(int f,char **data)
66 buf = (char *)malloc(CHUNK_SIZE);
67 if (!buf) out_of_memory("simple_recv_token");
77 n = MIN(CHUNK_SIZE,residue);
84 /* non-compressing send token */
85 static void simple_send_token(int f,int token,
86 struct map_struct *buf,OFF_T offset,int n)
91 int n1 = MIN(CHUNK_SIZE,n-l);
93 write_buf(f,map_ptr(buf,offset+l,n1),n1);
97 /* a -2 token means to send data only and no token */
99 write_int(f,-(token+1));
104 /* Flag bytes in compressed stream are encoded as follows: */
105 #define END_FLAG 0 /* that's all folks */
106 #define TOKEN_LONG 0x20 /* followed by 32-bit token number */
107 #define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
108 #define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
109 #define TOKEN_REL 0x80 /* + 6-bit relative token number */
110 #define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
112 #define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
114 /* For coding runs of tokens */
115 static int last_token = -1;
116 static int run_start;
117 static int last_run_end;
119 /* Deflation state */
120 static z_stream tx_strm;
125 /* Send a deflated token */
127 send_deflated_token(int f, int token,
128 struct map_struct *buf, OFF_T offset, int nb, int toklen)
131 static int init_done, flush_pending;
133 if (last_token == -1) {
136 tx_strm.next_in = NULL;
137 tx_strm.zalloc = NULL;
138 tx_strm.zfree = NULL;
139 if (deflateInit2(&tx_strm, compression_level,
141 Z_DEFAULT_STRATEGY) != Z_OK) {
142 rprintf(FERROR, "compression init failed\n");
143 exit_cleanup(RERR_STREAMIO);
145 if ((obuf = malloc(MAX_DATA_COUNT+2)) == NULL)
146 out_of_memory("send_deflated_token");
149 deflateReset(&tx_strm);
154 } else if (last_token == -2) {
157 } else if (nb != 0 || token != last_token + 1
158 || token >= run_start + 65536) {
159 /* output previous run */
160 r = run_start - last_run_end;
161 n = last_token - run_start;
162 if (r >= 0 && r <= 63) {
163 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
165 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
166 write_int(f, run_start);
170 write_byte(f, n >> 8);
172 last_run_end = last_token;
178 if (nb != 0 || flush_pending) {
179 /* deflate the data starting at offset */
180 int flush = Z_NO_FLUSH;
181 tx_strm.avail_in = 0;
182 tx_strm.avail_out = 0;
184 if (tx_strm.avail_in == 0 && nb != 0) {
185 /* give it some more input */
186 n = MIN(nb, CHUNK_SIZE);
187 tx_strm.next_in = (Bytef *)
188 map_ptr(buf, offset, n);
189 tx_strm.avail_in = n;
193 if (tx_strm.avail_out == 0) {
194 tx_strm.next_out = (Bytef *)(obuf + 2);
195 tx_strm.avail_out = MAX_DATA_COUNT;
196 if (flush != Z_NO_FLUSH) {
198 * We left the last 4 bytes in the
199 * buffer, in case they are the
200 * last 4. Move them to the front.
202 memcpy(tx_strm.next_out,
203 obuf+MAX_DATA_COUNT-2, 4);
204 tx_strm.next_out += 4;
205 tx_strm.avail_out -= 4;
208 if (nb == 0 && token != -2)
209 flush = Z_SYNC_FLUSH;
210 r = deflate(&tx_strm, flush);
212 rprintf(FERROR, "deflate returned %d\n", r);
213 exit_cleanup(RERR_STREAMIO);
215 if (nb == 0 || tx_strm.avail_out == 0) {
216 n = MAX_DATA_COUNT - tx_strm.avail_out;
217 if (flush != Z_NO_FLUSH) {
219 * We have to trim off the last 4
220 * bytes of output when flushing
221 * (they are just 0, 0, ff, ff).
226 obuf[0] = DEFLATED_DATA + (n >> 8);
228 write_buf(f, obuf, n+2);
231 } while (nb != 0 || tx_strm.avail_out == 0);
232 flush_pending = token == -2;
236 /* end of file - clean up */
237 write_byte(f, END_FLAG);
239 } else if (token != -2) {
240 /* add the data in the current block to the compressor's
241 history and hash table */
242 tx_strm.next_in = (Bytef *) map_ptr(buf, offset, toklen);
243 tx_strm.avail_in = toklen;
244 tx_strm.next_out = (Bytef *) obuf;
245 tx_strm.avail_out = MAX_DATA_COUNT;
246 r = deflate(&tx_strm, Z_INSERT_ONLY);
247 if (r != Z_OK || tx_strm.avail_in != 0) {
248 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
249 r, tx_strm.avail_in);
250 exit_cleanup(RERR_STREAMIO);
256 /* tells us what the receiver is in the middle of doing */
257 static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
259 /* for inflating stuff */
260 static z_stream rx_strm;
264 /* for decoding runs of tokens */
268 /* Receive a deflated token and inflate it */
270 recv_deflated_token(int f, char **data)
273 static int init_done;
274 static int saved_flag;
277 switch (recv_state) {
280 rx_strm.next_out = NULL;
281 rx_strm.zalloc = NULL;
282 rx_strm.zfree = NULL;
283 if (inflateInit2(&rx_strm, -15) != Z_OK) {
284 rprintf(FERROR, "inflate init failed\n");
285 exit_cleanup(RERR_STREAMIO);
287 if ((cbuf = malloc(MAX_DATA_COUNT)) == NULL
288 || (dbuf = malloc(CHUNK_SIZE)) == NULL)
289 out_of_memory("recv_deflated_token");
292 inflateReset(&rx_strm);
301 flag = saved_flag & 0xff;
305 if ((flag & 0xC0) == DEFLATED_DATA) {
306 n = ((flag & 0x3f) << 8) + read_byte(f);
307 read_buf(f, cbuf, n);
308 rx_strm.next_in = (Bytef *)cbuf;
309 rx_strm.avail_in = n;
310 recv_state = r_inflating;
313 if (recv_state == r_inflated) {
314 /* check previous inflated stuff ended correctly */
315 rx_strm.avail_in = 0;
316 rx_strm.next_out = (Bytef *)dbuf;
317 rx_strm.avail_out = CHUNK_SIZE;
318 r = inflate(&rx_strm, Z_SYNC_FLUSH);
319 n = CHUNK_SIZE - rx_strm.avail_out;
321 * Z_BUF_ERROR just means no progress was
322 * made, i.e. the decompressor didn't have
323 * any pending output for us.
325 if (r != Z_OK && r != Z_BUF_ERROR) {
326 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
328 exit_cleanup(RERR_STREAMIO);
330 if (n != 0 && r != Z_BUF_ERROR) {
331 /* have to return some more data and
332 save the flag for later. */
333 saved_flag = flag + 0x10000;
338 * At this point the decompressor should
339 * be expecting to see the 0, 0, ff, ff bytes.
341 if (!inflateSyncPoint(&rx_strm)) {
342 rprintf(FERROR, "decompressor lost sync!\n");
343 exit_cleanup(RERR_STREAMIO);
345 rx_strm.avail_in = 4;
346 rx_strm.next_in = (Bytef *)cbuf;
347 cbuf[0] = cbuf[1] = 0;
348 cbuf[2] = cbuf[3] = 0xff;
349 inflate(&rx_strm, Z_SYNC_FLUSH);
352 if (flag == END_FLAG) {
353 /* that's all folks */
358 /* here we have a token of some kind */
359 if (flag & TOKEN_REL) {
360 rx_token += flag & 0x3f;
363 rx_token = read_int(f);
365 rx_run = read_byte(f);
366 rx_run += read_byte(f) << 8;
367 recv_state = r_running;
369 return -1 - rx_token;
372 rx_strm.next_out = (Bytef *)dbuf;
373 rx_strm.avail_out = CHUNK_SIZE;
374 r = inflate(&rx_strm, Z_NO_FLUSH);
375 n = CHUNK_SIZE - rx_strm.avail_out;
377 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
378 exit_cleanup(RERR_STREAMIO);
380 if (rx_strm.avail_in == 0)
381 recv_state = r_inflated;
392 return -1 - rx_token;
398 * put the data corresponding to a token that we've just returned
399 * from recv_deflated_token into the decompressor's history buffer.
401 static void see_deflate_token(char *buf, int len)
404 unsigned char hdr[5];
406 rx_strm.avail_in = 0;
410 if (rx_strm.avail_in == 0 && len != 0) {
412 /* Give it a fake stored-block header. */
413 rx_strm.next_in = (Bytef *)hdr;
414 rx_strm.avail_in = 5;
419 hdr[2] = blklen >> 8;
423 rx_strm.next_in = (Bytef *)buf;
424 rx_strm.avail_in = blklen;
429 rx_strm.next_out = (Bytef *)dbuf;
430 rx_strm.avail_out = CHUNK_SIZE;
431 r = inflate(&rx_strm, Z_SYNC_FLUSH);
433 rprintf(FERROR, "inflate (token) returned %d\n", r);
434 exit_cleanup(RERR_STREAMIO);
436 } while (len || rx_strm.avail_out == 0);
440 * transmit a verbatim buffer of length n followed by a token
441 * If token == -1 then we have reached EOF
442 * If n == 0 then don't send a buffer
444 void send_token(int f,int token,struct map_struct *buf,OFF_T offset,
447 if (!do_compression) {
448 simple_send_token(f,token,buf,offset,n);
450 send_deflated_token(f, token, buf, offset, n, toklen);
456 * receive a token or buffer from the other end. If the reurn value is >0 then
457 * it is a data buffer of that length, and *data will point at the data.
458 * if the return value is -i then it represents token i-1
459 * if the return value is 0 then the end has been reached
461 int recv_token(int f,char **data)
465 if (!do_compression) {
466 tok = simple_recv_token(f,data);
468 tok = recv_deflated_token(f, data);
474 * look at the data corresponding to a token, if necessary
476 void see_token(char *data, int toklen)
479 see_deflate_token(data, toklen);