5 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
8 +# define RSYNC_WIN 8192
10 +/* Size of rsync window, must be < MAX_DIST */
12 +#define RSYNC_SUM_MATCH(sum) (((sum) & (RSYNC_WIN - 1)) == 0)
13 +/* Whether window sum matches magic value */
15 /* ===========================================================================
16 * Local data used by the "longest match" routines.
20 /* Use a faster search when the previous match is longer than this */
22 +local ulg rsync_sum; /* rolling sum of rsync window */
23 +local ulg rsync_chunk_end; /* next rsync sequence point */
25 /* Values for max_lazy_match, good_match and max_chain_length, depending on
26 * the desired pack level (0..9). The values given below have been tuned to
29 /* prev will be initialized on the fly */
32 + rsync_chunk_end = 0xFFFFFFFFUL;
35 /* Set the default configuration parameters:
37 max_lazy_match = configuration_table[pack_level].max_lazy;
39 memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
41 strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
42 + if (rsync_chunk_end != 0xFFFFFFFFUL)
43 + rsync_chunk_end -= WSIZE;
45 block_start -= (long) WSIZE;
51 +local void rsync_roll(unsigned start, unsigned num)
55 + if (start < RSYNC_WIN) {
56 + /* before window fills. */
57 + for (i = start; i < RSYNC_WIN; i++) {
58 + if (i == start + num) return;
59 + rsync_sum += (ulg)window[i];
61 + num -= (RSYNC_WIN - start);
65 + /* buffer after window full */
66 + for (i = start; i < start+num; i++) {
67 + /* New character in */
68 + rsync_sum += (ulg)window[i];
69 + /* Old character out */
70 + rsync_sum -= (ulg)window[i - RSYNC_WIN];
71 + if (rsync_chunk_end == 0xFFFFFFFFUL && RSYNC_SUM_MATCH(rsync_sum))
72 + rsync_chunk_end = i;
76 +/* ===========================================================================
77 + * Set rsync_chunk_end if window sum matches magic value.
79 +#define RSYNC_ROLL(s, n) \
80 + do { if (rsync) rsync_roll((s), (n)); } while(0)
82 /* ===========================================================================
83 * Flush the current block, with given end-of-file flag.
84 * IN assertion: strstart is set to the end of the current match.
86 #define FLUSH_BLOCK(eof) \
87 flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \
88 - (char*)NULL, (long)strstart - block_start, (eof))
89 + (char*)NULL, (long)strstart - block_start, flush-1, (eof))
91 /* ===========================================================================
92 * Processes a new input file and return its compressed length. This
94 local off_t deflate_fast()
96 IPos hash_head; /* head of the hash chain */
97 - int flush; /* set if current block must be flushed */
98 + int flush = 0; /* set if current block must be flushed, 2=>and padded */
99 unsigned match_length = 0; /* length of best match */
101 prev_length = MIN_MATCH-1;
104 lookahead -= match_length;
106 - /* Insert new strings in the hash table only if the match length
107 + RSYNC_ROLL(strstart, match_length);
108 + /* Insert new strings in the hash table only if the match length
109 * is not too large. This saves time but degrades compression.
111 if (match_length <= max_insert_length) {
113 /* No match, output a literal byte */
114 Tracevv((stderr,"%c",window[strstart]));
115 flush = ct_tally (0, window[strstart]);
116 + RSYNC_ROLL(strstart, 1);
120 + if (rsync && strstart > rsync_chunk_end) {
122 + rsync_chunk_end = 0xFFFFFFFFUL;
124 if (flush) FLUSH_BLOCK(0), block_start = strstart;
126 /* Make sure that we always have enough lookahead, except
129 IPos hash_head; /* head of hash chain */
130 IPos prev_match; /* previous match */
131 - int flush; /* set if current block must be flushed */
132 + int flush = 0; /* set if current block must be flushed */
133 int match_available = 0; /* set if previous match exists */
134 register unsigned match_length = MIN_MATCH-1; /* length of best match */
138 lookahead -= prev_length-1;
140 + RSYNC_ROLL(strstart, prev_length+1);
143 INSERT_STRING(strstart, hash_head);
144 @@ -742,24 +796,38 @@
146 match_length = MIN_MATCH-1;
148 - if (flush) FLUSH_BLOCK(0), block_start = strstart;
150 + if (rsync && strstart > rsync_chunk_end) {
151 + rsync_chunk_end = 0xFFFFFFFFUL;
154 + if (flush) FLUSH_BLOCK(0), block_start = strstart;
155 } else if (match_available) {
156 /* If there was no match at the previous position, output a
157 * single literal. If there was a match but the current match
158 * is longer, truncate the previous match to a single literal.
160 Tracevv((stderr,"%c",window[strstart-1]));
161 - if (ct_tally (0, window[strstart-1])) {
162 - FLUSH_BLOCK(0), block_start = strstart;
164 + flush = ct_tally (0, window[strstart-1]);
165 + if (rsync && strstart > rsync_chunk_end) {
166 + rsync_chunk_end = 0xFFFFFFFFUL;
169 + if (flush) FLUSH_BLOCK(0), block_start = strstart;
170 + RSYNC_ROLL(strstart, 1);
174 /* There is no previous match to compare with, wait for
175 * the next step to decide.
177 + if (rsync && strstart > rsync_chunk_end) {
178 + rsync_chunk_end = 0xFFFFFFFFUL;
180 + FLUSH_BLOCK(0), block_start = strstart;
183 + RSYNC_ROLL(strstart, 1);
190 -V, --version display version number
191 -1, --fast compress faster
192 -9, --best compress better
193 + --rsyncable Make rsync-friendly archive
195 With no FILE, or when FILE is -, read standard input.
198 into the directory and compress all the files it finds there (or
199 decompress them in the case of @command{gunzip}).
202 +While compressing, synchronize the output occasionally based on
203 +the input. This increases size by less than 1 percent most
204 +cases, but means that the @command{rsync} program can much more efficiently
205 +synchronize files compressed with this flag. @command{gunzip}
206 +cannot tell the difference between a compressed file created
207 +with this option, and one created without it.
209 @item --suffix @var{suf}
211 Use suffix @var{suf} instead of @samp{.gz}. Any suffix can be
215 unsigned insize; /* valid bytes in inbuf */
216 unsigned inptr; /* index of next byte to be processed in inbuf */
217 unsigned outcnt; /* bytes in output buffer */
218 +int rsync = 0; /* make ryncable chunks */
220 static int handled_sig[] =
223 {"best", 0, 0, '9'}, /* compress better */
224 {"lzw", 0, 0, 'Z'}, /* make output compatible with old compress */
225 {"bits", 1, 0, 'b'}, /* max number of bits per code (implies -Z) */
227 + {"rsyncable", 0, 0, 'R'}, /* make rsync-friendly archive */
232 " -Z, --lzw produce output compatible with old compress",
233 " -b, --bits=BITS max number of bits per code (implies -Z)",
235 + " --rsyncable Make rsync-friendly archive",
237 "With no FILE, or when FILE is -, read standard input.",
250 #ifdef NO_MULTIPLE_DOTS
251 if (*optarg == '.') optarg++;
256 extern unsigned insize; /* valid bytes in inbuf */
257 extern unsigned inptr; /* index of next byte to be processed in inbuf */
258 extern unsigned outcnt; /* bytes in output buffer */
259 +extern int rsync; /* deflate into rsyncable chunks */
261 extern off_t bytes_in; /* number of input bytes */
262 extern off_t bytes_out; /* number of output bytes */
265 extern void ct_init (ush *attr, int *method);
266 extern int ct_tally (int dist, int lc);
267 -extern off_t flush_block (char *buf, ulg stored_len, int eof);
268 +extern off_t flush_block (char *buf, ulg stored_len, int pad, int eof);
271 extern void bi_init (file_t zipfile);
277 .RB [ " \-acdfhklLnNrtvV19 " ]
287 +While compressing, synchronize the output occasionally based on the input.
288 +This increases size by less than 1 percent most cases, but means that the
290 +program can take advantage of similarities in the uncompressed input
291 +when syncronizing two files compressed with this flag.
293 +cannot tell the difference between a compressed file created with this option,
294 +and one created without it.
296 .B \-S .suf --suffix .suf
297 When compressing, use suffix .suf instead of .gz.
298 Any non-empty suffix can be given, but suffixes
302 * trees or store, and output the encoded block to the zip file. This function
303 * returns the total compressed length for the file so far.
305 -off_t flush_block(buf, stored_len, eof)
306 +off_t flush_block(buf, stored_len, pad, eof)
307 char *buf; /* input block, or NULL if too old */
308 ulg stored_len; /* length of input block */
309 + int pad; /* pad output to byte boundary */
310 int eof; /* true if this is the last block for a file */
312 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
314 Assert (input_len == bytes_in, "bad input size");
316 compressed_len += 7; /* align on byte boundary */
317 + } else if (pad && (compressed_len % 8) != 0) {
318 + send_bits((STORED_BLOCK<<1)+eof, 3); /* send block type */
319 + compressed_len = (compressed_len + 3 + 7) & ~7L;
320 + copy_block(buf, 0, 1); /* with header */
323 return compressed_len >> 3;