1 From: Rusty Russell <rusty@rustcorp.com.au>
3 [NOTE: this patch is for _gzip_! --Wayne]
5 It's pending for actual gzip inclusion! The maintainer was dug up and
6 everything: it'll even be on by default.
8 But here's the old patch,
11 Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
13 --- gzip-1.2.4.orig/deflate.c Fri Aug 13 22:35:31 1993
14 +++ gzip-1.2.4-rsync/deflate.c Sat Dec 30 15:33:25 2000
17 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
20 +# define RSYNC_WIN 4096
22 +/* Size of rsync window, must be < MAX_DIST */
24 +#define RSYNC_SUM_MATCH(sum) ((sum) % RSYNC_WIN == 0)
25 +/* Whether window sum matches magic value */
27 /* ===========================================================================
28 * Local data used by the "longest match" routines.
31 unsigned near good_match;
32 /* Use a faster search when the previous match is longer than this */
34 +local ulg rsync_sum; /* rolling sum of rsync window */
35 +local ulg rsync_chunk_end; /* next rsync sequence point */
37 /* Values for max_lazy_match, good_match and max_chain_length, depending on
38 * the desired pack level (0..9). The values given below have been tuned to
41 /* prev will be initialized on the fly */
44 + rsync_chunk_end = 0xFFFFFFFFUL;
47 /* Set the default configuration parameters:
49 max_lazy_match = configuration_table[pack_level].max_lazy;
51 memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
53 strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
54 + if (rsync_chunk_end != 0xFFFFFFFFUL)
55 + rsync_chunk_end -= WSIZE;
57 block_start -= (long) WSIZE;
63 +local void rsync_roll(start, num)
69 + if (start < RSYNC_WIN) {
70 + /* before window fills. */
71 + for (i = start; i < RSYNC_WIN; i++) {
72 + if (i == start + num) return;
73 + rsync_sum += (ulg)window[i];
75 + num -= (RSYNC_WIN - start);
79 + /* buffer after window full */
80 + for (i = start; i < start+num; i++) {
81 + /* New character in */
82 + rsync_sum += (ulg)window[i];
83 + /* Old character out */
84 + rsync_sum -= (ulg)window[i - RSYNC_WIN];
85 + if (rsync_chunk_end == 0xFFFFFFFFUL && RSYNC_SUM_MATCH(rsync_sum))
86 + rsync_chunk_end = i;
90 +/* ===========================================================================
91 + * Set rsync_chunk_end if window sum matches magic value.
93 +#define RSYNC_ROLL(s, n) \
94 + do { if (rsync) rsync_roll((s), (n)); } while(0)
96 /* ===========================================================================
97 * Flush the current block, with given end-of-file flag.
98 * IN assertion: strstart is set to the end of the current match.
100 #define FLUSH_BLOCK(eof) \
101 flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \
102 - (char*)NULL, (long)strstart - block_start, (eof))
103 + (char*)NULL, (long)strstart - block_start, flush-1, (eof))
105 /* ===========================================================================
106 * Processes a new input file and return its compressed length. This
108 local ulg deflate_fast()
110 IPos hash_head; /* head of the hash chain */
111 - int flush; /* set if current block must be flushed */
112 + int flush; /* set if current block must be flushed, 2=>and padded */
113 unsigned match_length = 0; /* length of best match */
115 prev_length = MIN_MATCH-1;
118 lookahead -= match_length;
120 + RSYNC_ROLL(strstart, match_length);
121 /* Insert new strings in the hash table only if the match length
122 * is not too large. This saves time but degrades compression.
125 /* No match, output a literal byte */
126 Tracevv((stderr,"%c",window[strstart]));
127 flush = ct_tally (0, window[strstart]);
128 + RSYNC_ROLL(strstart, 1);
132 + if (rsync && strstart > rsync_chunk_end) {
133 + rsync_chunk_end = 0xFFFFFFFFUL;
136 if (flush) FLUSH_BLOCK(0), block_start = strstart;
138 /* Make sure that we always have enough lookahead, except
141 lookahead -= prev_length-1;
143 + RSYNC_ROLL(strstart, prev_length+1);
146 INSERT_STRING(strstart, hash_head);
147 @@ -727,24 +783,39 @@
149 match_length = MIN_MATCH-1;
151 - if (flush) FLUSH_BLOCK(0), block_start = strstart;
153 + if (rsync && strstart > rsync_chunk_end) {
154 + rsync_chunk_end = 0xFFFFFFFFUL;
157 + if (flush) FLUSH_BLOCK(0), block_start = strstart;
158 } else if (match_available) {
159 /* If there was no match at the previous position, output a
160 * single literal. If there was a match but the current match
161 * is longer, truncate the previous match to a single literal.
163 Tracevv((stderr,"%c",window[strstart-1]));
164 - if (ct_tally (0, window[strstart-1])) {
165 - FLUSH_BLOCK(0), block_start = strstart;
167 + flush = ct_tally (0, window[strstart-1]);
168 + if (rsync && strstart > rsync_chunk_end) {
169 + rsync_chunk_end = 0xFFFFFFFFUL;
172 + if (flush) FLUSH_BLOCK(0), block_start = strstart;
173 + RSYNC_ROLL(strstart, 1);
177 /* There is no previous match to compare with, wait for
178 * the next step to decide.
180 + if (rsync && strstart > rsync_chunk_end) {
181 + /* Reset huffman tree */
182 + rsync_chunk_end = 0xFFFFFFFFUL;
184 + FLUSH_BLOCK(0), block_start = strstart;
187 + RSYNC_ROLL(strstart, 1);
191 --- gzip-1.2.4.orig/gzip.c Thu Aug 19 23:39:43 1993
192 +++ gzip-1.2.4-rsync/gzip.c Fri Dec 29 21:20:54 2000
194 unsigned insize; /* valid bytes in inbuf */
195 unsigned inptr; /* index of next byte to be processed in inbuf */
196 unsigned outcnt; /* bytes in output buffer */
197 +int rsync = 0; /* make ryncable chunks */
199 struct option longopts[] =
202 {"best", 0, 0, '9'}, /* compress better */
203 {"lzw", 0, 0, 'Z'}, /* make output compatible with old compress */
204 {"bits", 1, 0, 'b'}, /* max number of bits per code (implies -Z) */
205 + {"rsyncable", 0, 0, 'R'}, /* make rsync-friendly archive */
210 " -Z --lzw produce output compatible with old compress",
211 " -b --bits maxbits max number of bits per code (implies -Z)",
213 + " --rsyncable Make rsync-friendly archive",
214 " file... files to (de)compress. If none given, use standard input.",
219 recursive = 1; break;
225 #ifdef NO_MULTIPLE_DOTS
226 if (*optarg == '.') optarg++;
227 --- gzip-1.2.4.orig/gzip.h Fri Aug 13 22:35:33 1993
228 +++ gzip-1.2.4-rsync/gzip.h Sat Dec 30 15:26:56 2000
230 extern unsigned insize; /* valid bytes in inbuf */
231 extern unsigned inptr; /* index of next byte to be processed in inbuf */
232 extern unsigned outcnt; /* bytes in output buffer */
233 +extern int rsync; /* deflate into rsyncable chunks */
235 extern long bytes_in; /* number of input bytes */
236 extern long bytes_out; /* number of output bytes */
239 void ct_init OF((ush *attr, int *method));
240 int ct_tally OF((int dist, int lc));
241 -ulg flush_block OF((char *buf, ulg stored_len, int eof));
242 +ulg flush_block OF((char *buf, ulg stored_len, int pad, int eof));
245 void bi_init OF((file_t zipfile));
246 --- gzip-1.2.4.orig/gzip.texi Thu Aug 19 06:42:50 1993
247 +++ gzip-1.2.4-rsync/gzip.texi Fri Dec 29 21:20:54 2000
249 into the directory and compress all the files it finds there (or
250 decompress them in the case of @code{gunzip}).
253 +While compressing, synchronize the output occasionally based on the
254 +input. This reduces compression by about 1 percent most cases, but
255 +means that the @code{rsync} program can take advantage of similarities
256 +in the uncompressed input when syncronizing two files compressed with
257 +this flag. @code{gunzip} cannot tell the difference between a
258 +compressed file created with this option, and one created without it.
260 @item --suffix @var{suf}
262 Use suffix @samp{@var{suf}} instead of @samp{.gz}. Any suffix can be
263 --- gzip-1.2.4.orig/trees.c Wed Aug 18 03:36:32 1993
264 +++ gzip-1.2.4-rsync/trees.c Sat Dec 30 15:37:00 2000
266 * trees or store, and output the encoded block to the zip file. This function
267 * returns the total compressed length for the file so far.
269 -ulg flush_block(buf, stored_len, eof)
270 +ulg flush_block(buf, stored_len, pad, eof)
271 char *buf; /* input block, or NULL if too old */
272 ulg stored_len; /* length of input block */
273 + int pad; /* pad output to byte boundary */
274 int eof; /* true if this is the last block for a file */
276 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
278 Assert (input_len == isize, "bad input size");
280 compressed_len += 7; /* align on byte boundary */
281 + } else if (pad && (compressed_len % 8) != 0) {
282 + send_bits((STORED_BLOCK<<1)+eof, 3); /* send block type */
283 + compressed_len = (compressed_len + 3 + 7) & ~7L;
284 + copy_block(buf, 0, 1); /* with header */
286 Tracev((stderr,"\ncomprlen %lu(%lu) ", compressed_len>>3,
287 compressed_len-7*eof));