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
;
25 extern int def_compress_level
;
27 static int compression_level
, per_file_default_level
;
29 /* determine the compression level based on a wildcard filename list */
30 void set_compression(char *fname
)
32 static char *match_list
;
39 char *t
, *f
= lp_dont_compress(module_id
);
41 if (!(match_list
= t
= new_array(char, len
+ 2)))
42 out_of_memory("set_compression");
49 if (isupper(*(unsigned char *)f
))
50 *t
++ = tolower(*(unsigned char *)f
);
53 } while (*++f
!= ' ' && *f
);
56 /* Optimize a match-string of "*". */
57 if (t
- match_list
== 2 && match_list
[0] == '*') {
59 per_file_default_level
= 0;
61 per_file_default_level
= def_compress_level
;
65 compression_level
= per_file_default_level
;
70 if ((s
= strrchr(fname
, '/')) != NULL
)
73 for (s
= match_list
; *s
; s
+= strlen(s
) + 1) {
74 if (iwildmatch(s
, fname
)) {
75 compression_level
= 0;
81 /* non-compressing recv token */
82 static int32
simple_recv_token(int f
, char **data
)
89 buf
= new_array(char, CHUNK_SIZE
);
91 out_of_memory("simple_recv_token");
95 int32 i
= read_int(f
);
102 n
= MIN(CHUNK_SIZE
,residue
);
109 /* non-compressing send token */
110 static void simple_send_token(int f
, int32 token
, struct map_struct
*buf
,
111 OFF_T offset
, int32 n
)
116 int32 n1
= MIN(CHUNK_SIZE
, n
-len
);
118 write_buf(f
, map_ptr(buf
, offset
+len
, n1
), n1
);
122 /* a -2 token means to send data only and no token */
124 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
);
192 } else if (last_token
== -2) {
195 } else if (nb
!= 0 || token
!= last_token
+ 1
196 || token
>= run_start
+ 65536) {
197 /* output previous run */
198 r
= run_start
- last_run_end
;
199 n
= last_token
- run_start
;
200 if (r
>= 0 && r
<= 63) {
201 write_byte(f
, (n
==0? TOKEN_REL
: TOKENRUN_REL
) + r
);
203 write_byte(f
, (n
==0? TOKEN_LONG
: TOKENRUN_LONG
));
204 write_int(f
, run_start
);
208 write_byte(f
, n
>> 8);
210 last_run_end
= last_token
;
216 if (nb
!= 0 || flush_pending
) {
217 /* deflate the data starting at offset */
218 int flush
= Z_NO_FLUSH
;
219 tx_strm
.avail_in
= 0;
220 tx_strm
.avail_out
= 0;
222 if (tx_strm
.avail_in
== 0 && nb
!= 0) {
223 /* give it some more input */
224 n
= MIN(nb
, CHUNK_SIZE
);
225 tx_strm
.next_in
= (Bytef
*)
226 map_ptr(buf
, offset
, n
);
227 tx_strm
.avail_in
= n
;
231 if (tx_strm
.avail_out
== 0) {
232 tx_strm
.next_out
= (Bytef
*)(obuf
+ 2);
233 tx_strm
.avail_out
= MAX_DATA_COUNT
;
234 if (flush
!= Z_NO_FLUSH
) {
236 * We left the last 4 bytes in the
237 * buffer, in case they are the
238 * last 4. Move them to the front.
240 memcpy(tx_strm
.next_out
,
241 obuf
+MAX_DATA_COUNT
-2, 4);
242 tx_strm
.next_out
+= 4;
243 tx_strm
.avail_out
-= 4;
246 if (nb
== 0 && token
!= -2)
247 flush
= Z_SYNC_FLUSH
;
248 r
= deflate(&tx_strm
, flush
);
250 rprintf(FERROR
, "deflate returned %d\n", r
);
251 exit_cleanup(RERR_STREAMIO
);
253 if (nb
== 0 || tx_strm
.avail_out
== 0) {
254 n
= MAX_DATA_COUNT
- tx_strm
.avail_out
;
255 if (flush
!= Z_NO_FLUSH
) {
257 * We have to trim off the last 4
258 * bytes of output when flushing
259 * (they are just 0, 0, ff, ff).
264 obuf
[0] = DEFLATED_DATA
+ (n
>> 8);
266 write_buf(f
, obuf
, n
+2);
269 } while (nb
!= 0 || tx_strm
.avail_out
== 0);
270 flush_pending
= token
== -2;
274 /* end of file - clean up */
275 write_byte(f
, END_FLAG
);
276 } else if (token
!= -2) {
277 /* Add the data in the current block to the compressor's
278 * history and hash table. */
280 /* Break up long sections in the same way that
281 * see_deflate_token() does. */
282 int32 n1
= toklen
> 0xffff ? 0xffff : toklen
;
284 tx_strm
.next_in
= (Bytef
*)map_ptr(buf
, offset
, n1
);
285 tx_strm
.avail_in
= n1
;
286 tx_strm
.next_out
= (Bytef
*) obuf
;
287 tx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
288 r
= deflate(&tx_strm
, Z_INSERT_ONLY
);
289 if (r
!= Z_OK
|| tx_strm
.avail_in
!= 0) {
290 rprintf(FERROR
, "deflate on token returned %d (%d bytes left)\n",
291 r
, tx_strm
.avail_in
);
292 exit_cleanup(RERR_STREAMIO
);
294 } while (toklen
> 0);
299 /* tells us what the receiver is in the middle of doing */
300 static enum { r_init
, r_idle
, r_running
, r_inflating
, r_inflated
} recv_state
;
302 /* for inflating stuff */
303 static z_stream rx_strm
;
307 /* for decoding runs of tokens */
308 static int32 rx_token
;
311 /* Receive a deflated token and inflate it */
312 static int32
recv_deflated_token(int f
, char **data
)
314 static int init_done
;
315 static int32 saved_flag
;
320 switch (recv_state
) {
323 rx_strm
.next_out
= NULL
;
324 rx_strm
.zalloc
= NULL
;
325 rx_strm
.zfree
= NULL
;
326 if (inflateInit2(&rx_strm
, -15) != Z_OK
) {
327 rprintf(FERROR
, "inflate init failed\n");
328 exit_cleanup(RERR_STREAMIO
);
330 if (!(cbuf
= new_array(char, MAX_DATA_COUNT
))
331 || !(dbuf
= new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE
))))
332 out_of_memory("recv_deflated_token");
335 inflateReset(&rx_strm
);
344 flag
= saved_flag
& 0xff;
348 if ((flag
& 0xC0) == DEFLATED_DATA
) {
349 n
= ((flag
& 0x3f) << 8) + read_byte(f
);
350 read_buf(f
, cbuf
, n
);
351 rx_strm
.next_in
= (Bytef
*)cbuf
;
352 rx_strm
.avail_in
= n
;
353 recv_state
= r_inflating
;
356 if (recv_state
== r_inflated
) {
357 /* check previous inflated stuff ended correctly */
358 rx_strm
.avail_in
= 0;
359 rx_strm
.next_out
= (Bytef
*)dbuf
;
360 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
361 r
= inflate(&rx_strm
, Z_SYNC_FLUSH
);
362 n
= AVAIL_OUT_SIZE(CHUNK_SIZE
) - rx_strm
.avail_out
;
364 * Z_BUF_ERROR just means no progress was
365 * made, i.e. the decompressor didn't have
366 * any pending output for us.
368 if (r
!= Z_OK
&& r
!= Z_BUF_ERROR
) {
369 rprintf(FERROR
, "inflate flush returned %d (%d bytes)\n",
371 exit_cleanup(RERR_STREAMIO
);
373 if (n
!= 0 && r
!= Z_BUF_ERROR
) {
374 /* have to return some more data and
375 save the flag for later. */
376 saved_flag
= flag
+ 0x10000;
381 * At this point the decompressor should
382 * be expecting to see the 0, 0, ff, ff bytes.
384 if (!inflateSyncPoint(&rx_strm
)) {
385 rprintf(FERROR
, "decompressor lost sync!\n");
386 exit_cleanup(RERR_STREAMIO
);
388 rx_strm
.avail_in
= 4;
389 rx_strm
.next_in
= (Bytef
*)cbuf
;
390 cbuf
[0] = cbuf
[1] = 0;
391 cbuf
[2] = cbuf
[3] = 0xff;
392 inflate(&rx_strm
, Z_SYNC_FLUSH
);
395 if (flag
== END_FLAG
) {
396 /* that's all folks */
401 /* here we have a token of some kind */
402 if (flag
& TOKEN_REL
) {
403 rx_token
+= flag
& 0x3f;
406 rx_token
= read_int(f
);
408 rx_run
= read_byte(f
);
409 rx_run
+= read_byte(f
) << 8;
410 recv_state
= r_running
;
412 return -1 - rx_token
;
415 rx_strm
.next_out
= (Bytef
*)dbuf
;
416 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
417 r
= inflate(&rx_strm
, Z_NO_FLUSH
);
418 n
= AVAIL_OUT_SIZE(CHUNK_SIZE
) - rx_strm
.avail_out
;
420 rprintf(FERROR
, "inflate returned %d (%d bytes)\n", r
, n
);
421 exit_cleanup(RERR_STREAMIO
);
423 if (rx_strm
.avail_in
== 0)
424 recv_state
= r_inflated
;
435 return -1 - rx_token
;
441 * put the data corresponding to a token that we've just returned
442 * from recv_deflated_token into the decompressor's history buffer.
444 static void see_deflate_token(char *buf
, int32 len
)
448 unsigned char hdr
[5];
450 rx_strm
.avail_in
= 0;
454 if (rx_strm
.avail_in
== 0 && len
!= 0) {
456 /* Give it a fake stored-block header. */
457 rx_strm
.next_in
= (Bytef
*)hdr
;
458 rx_strm
.avail_in
= 5;
463 hdr
[2] = blklen
>> 8;
467 rx_strm
.next_in
= (Bytef
*)buf
;
468 rx_strm
.avail_in
= blklen
;
473 rx_strm
.next_out
= (Bytef
*)dbuf
;
474 rx_strm
.avail_out
= AVAIL_OUT_SIZE(CHUNK_SIZE
);
475 r
= inflate(&rx_strm
, Z_SYNC_FLUSH
);
477 rprintf(FERROR
, "inflate (token) returned %d\n", r
);
478 exit_cleanup(RERR_STREAMIO
);
480 } while (len
|| rx_strm
.avail_out
== 0);
484 * Transmit a verbatim buffer of length @p n followed by a token.
485 * If token == -1 then we have reached EOF
486 * If n == 0 then don't send a buffer
488 void send_token(int f
, int32 token
, struct map_struct
*buf
, OFF_T offset
,
489 int32 n
, int32 toklen
)
492 simple_send_token(f
, token
, buf
, offset
, n
);
494 send_deflated_token(f
, token
, buf
, offset
, n
, toklen
);
499 * receive a token or buffer from the other end. If the reurn value is >0 then
500 * it is a data buffer of that length, and *data will point at the data.
501 * if the return value is -i then it represents token i-1
502 * if the return value is 0 then the end has been reached
504 int32
recv_token(int f
, char **data
)
508 if (!do_compression
) {
509 tok
= simple_recv_token(f
,data
);
511 tok
= recv_deflated_token(f
, data
);
517 * look at the data corresponding to a token, if necessary
519 void see_token(char *data
, int32 toklen
)
522 see_deflate_token(data
, toklen
);