This is now in CVS.
[rsync/rsync-patches.git] / gzip-rsyncable.diff
CommitLineData
83907074
MP
1In message <20020403094739.GA5434@samba.org> you write:
2> On 3 Apr 2002, Rusty Russell <rusty@rustcorp.com.au> wrote:
3> > In message <20020403020455.GC18851@samba.org> you write:
4> > > Hi,
5> > >
6> > > I think you said the other day that you had a working --rsyncable
7> > > patch for gzip. Could I have it please?
8>
9> Thanks for that. I'll put it in with our contrib patches and probably
10> merge it soon.
11>
12> I was actually after the patch to gzip, rather than the fuzzy patch
13> for rsync. I'd like to try it myself and also distribute it along
14> with rsync.
15
16It's pending for actual gzip inclusion! The maintainer was dug up and
17everything: it'll even be on by default.
18
19But here's the old patch,
20Rusty.
21--
22 Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
23
24diff -ur gzip-1.2.4.orig/deflate.c gzip-1.2.4-rsync/deflate.c
25--- gzip-1.2.4.orig/deflate.c Fri Aug 13 22:35:31 1993
26+++ gzip-1.2.4-rsync/deflate.c Sat Dec 30 15:33:25 2000
27@@ -121,6 +121,14 @@
28 #endif
29 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
30
31+#ifndef RSYNC_WIN
32+# define RSYNC_WIN 4096
33+#endif
34+/* Size of rsync window, must be < MAX_DIST */
35+
36+#define RSYNC_SUM_MATCH(sum) ((sum) % RSYNC_WIN == 0)
37+/* Whether window sum matches magic value */
38+
39 /* ===========================================================================
40 * Local data used by the "longest match" routines.
41 */
42@@ -202,6 +210,8 @@
43 unsigned near good_match;
44 /* Use a faster search when the previous match is longer than this */
45
46+local ulg rsync_sum; /* rolling sum of rsync window */
47+local ulg rsync_chunk_end; /* next rsync sequence point */
48
49 /* Values for max_lazy_match, good_match and max_chain_length, depending on
50 * the desired pack level (0..9). The values given below have been tuned to
51@@ -300,6 +310,10 @@
52 #endif
53 /* prev will be initialized on the fly */
54
55+ /* rsync params */
56+ rsync_chunk_end = 0xFFFFFFFFUL;
57+ rsync_sum = 0;
58+
59 /* Set the default configuration parameters:
60 */
61 max_lazy_match = configuration_table[pack_level].max_lazy;
62@@ -536,6 +550,8 @@
63 memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
64 match_start -= WSIZE;
65 strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
66+ if (rsync_chunk_end != 0xFFFFFFFFUL)
67+ rsync_chunk_end -= WSIZE;
68
69 block_start -= (long) WSIZE;
70
71@@ -563,13 +579,46 @@
72 }
73 }
74
75+local void rsync_roll(start, num)
76+ unsigned start;
77+ unsigned num;
78+{
79+ unsigned i;
80+
81+ if (start < RSYNC_WIN) {
82+ /* before window fills. */
83+ for (i = start; i < RSYNC_WIN; i++) {
84+ if (i == start + num) return;
85+ rsync_sum += (ulg)window[i];
86+ }
87+ num -= (RSYNC_WIN - start);
88+ start = RSYNC_WIN;
89+ }
90+
91+ /* buffer after window full */
92+ for (i = start; i < start+num; i++) {
93+ /* New character in */
94+ rsync_sum += (ulg)window[i];
95+ /* Old character out */
96+ rsync_sum -= (ulg)window[i - RSYNC_WIN];
97+ if (rsync_chunk_end == 0xFFFFFFFFUL && RSYNC_SUM_MATCH(rsync_sum))
98+ rsync_chunk_end = i;
99+ }
100+}
101+
102+/* ===========================================================================
103+ * Set rsync_chunk_end if window sum matches magic value.
104+ */
105+#define RSYNC_ROLL(s, n) \
106+ do { if (rsync) rsync_roll((s), (n)); } while(0)
107+
108 /* ===========================================================================
109 * Flush the current block, with given end-of-file flag.
110 * IN assertion: strstart is set to the end of the current match.
111 */
112 #define FLUSH_BLOCK(eof) \
113 flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \
114- (char*)NULL, (long)strstart - block_start, (eof))
115+ (char*)NULL, (long)strstart - block_start, flush-1, (eof))
116
117 /* ===========================================================================
118 * Processes a new input file and return its compressed length. This
119@@ -580,7 +629,7 @@
120 local ulg deflate_fast()
121 {
122 IPos hash_head; /* head of the hash chain */
123- int flush; /* set if current block must be flushed */
124+ int flush; /* set if current block must be flushed, 2=>and padded */
125 unsigned match_length = 0; /* length of best match */
126
127 prev_length = MIN_MATCH-1;
128@@ -609,6 +658,7 @@
129
130 lookahead -= match_length;
131
132+ RSYNC_ROLL(strstart, match_length);
133 /* Insert new strings in the hash table only if the match length
134 * is not too large. This saves time but degrades compression.
135 */
136@@ -637,9 +687,14 @@
137 /* No match, output a literal byte */
138 Tracevv((stderr,"%c",window[strstart]));
139 flush = ct_tally (0, window[strstart]);
140+ RSYNC_ROLL(strstart, 1);
141 lookahead--;
142 strstart++;
143 }
144+ if (rsync && strstart > rsync_chunk_end) {
145+ rsync_chunk_end = 0xFFFFFFFFUL;
146+ flush = 2;
147+ }
148 if (flush) FLUSH_BLOCK(0), block_start = strstart;
149
150 /* Make sure that we always have enough lookahead, except
151@@ -715,6 +770,7 @@
152 */
153 lookahead -= prev_length-1;
154 prev_length -= 2;
155+ RSYNC_ROLL(strstart, prev_length+1);
156 do {
157 strstart++;
158 INSERT_STRING(strstart, hash_head);
159@@ -727,24 +783,39 @@
160 match_available = 0;
161 match_length = MIN_MATCH-1;
162 strstart++;
163- if (flush) FLUSH_BLOCK(0), block_start = strstart;
164
165+ if (rsync && strstart > rsync_chunk_end) {
166+ rsync_chunk_end = 0xFFFFFFFFUL;
167+ flush = 2;
168+ }
169+ if (flush) FLUSH_BLOCK(0), block_start = strstart;
170 } else if (match_available) {
171 /* If there was no match at the previous position, output a
172 * single literal. If there was a match but the current match
173 * is longer, truncate the previous match to a single literal.
174 */
175 Tracevv((stderr,"%c",window[strstart-1]));
176- if (ct_tally (0, window[strstart-1])) {
177- FLUSH_BLOCK(0), block_start = strstart;
178- }
179+ flush = ct_tally (0, window[strstart-1]);
180+ if (rsync && strstart > rsync_chunk_end) {
181+ rsync_chunk_end = 0xFFFFFFFFUL;
182+ flush = 2;
183+ }
184+ if (flush) FLUSH_BLOCK(0), block_start = strstart;
185+ RSYNC_ROLL(strstart, 1);
186 strstart++;
187 lookahead--;
188 } else {
189 /* There is no previous match to compare with, wait for
190 * the next step to decide.
191 */
192+ if (rsync && strstart > rsync_chunk_end) {
193+ /* Reset huffman tree */
194+ rsync_chunk_end = 0xFFFFFFFFUL;
195+ flush = 2;
196+ FLUSH_BLOCK(0), block_start = strstart;
197+ }
198 match_available = 1;
199+ RSYNC_ROLL(strstart, 1);
200 strstart++;
201 lookahead--;
202 }
203diff -ur gzip-1.2.4.orig/gzip.c gzip-1.2.4-rsync/gzip.c
204--- gzip-1.2.4.orig/gzip.c Thu Aug 19 23:39:43 1993
205+++ gzip-1.2.4-rsync/gzip.c Fri Dec 29 21:20:54 2000
206@@ -239,6 +239,7 @@
207 unsigned insize; /* valid bytes in inbuf */
208 unsigned inptr; /* index of next byte to be processed in inbuf */
209 unsigned outcnt; /* bytes in output buffer */
210+int rsync = 0; /* make ryncable chunks */
211
212 struct option longopts[] =
213 {
214@@ -268,6 +269,7 @@
215 {"best", 0, 0, '9'}, /* compress better */
216 {"lzw", 0, 0, 'Z'}, /* make output compatible with old compress */
217 {"bits", 1, 0, 'b'}, /* max number of bits per code (implies -Z) */
218+ {"rsyncable", 0, 0, 'R'}, /* make rsync-friendly archive */
219 { 0, 0, 0, 0 }
220 };
221
222@@ -357,6 +359,7 @@
223 " -Z --lzw produce output compatible with old compress",
224 " -b --bits maxbits max number of bits per code (implies -Z)",
225 #endif
226+ " --rsyncable Make rsync-friendly archive",
227 " file... files to (de)compress. If none given, use standard input.",
228 0};
229 char **p = help_msg;
230@@ -516,6 +519,9 @@
231 #else
232 recursive = 1; break;
233 #endif
234+ case 'R':
235+ rsync = 1; break;
236+
237 case 'S':
238 #ifdef NO_MULTIPLE_DOTS
239 if (*optarg == '.') optarg++;
240diff -ur gzip-1.2.4.orig/gzip.h gzip-1.2.4-rsync/gzip.h
241--- gzip-1.2.4.orig/gzip.h Fri Aug 13 22:35:33 1993
242+++ gzip-1.2.4-rsync/gzip.h Sat Dec 30 15:26:56 2000
243@@ -131,6 +131,7 @@
244 extern unsigned insize; /* valid bytes in inbuf */
245 extern unsigned inptr; /* index of next byte to be processed in inbuf */
246 extern unsigned outcnt; /* bytes in output buffer */
247+extern int rsync; /* deflate into rsyncable chunks */
248
249 extern long bytes_in; /* number of input bytes */
250 extern long bytes_out; /* number of output bytes */
251@@ -282,7 +283,7 @@
252 /* in trees.c */
253 void ct_init OF((ush *attr, int *method));
254 int ct_tally OF((int dist, int lc));
255-ulg flush_block OF((char *buf, ulg stored_len, int eof));
256+ulg flush_block OF((char *buf, ulg stored_len, int pad, int eof));
257
258 /* in bits.c */
259 void bi_init OF((file_t zipfile));
260diff -ur gzip-1.2.4.orig/gzip.texi gzip-1.2.4-rsync/gzip.texi
261--- gzip-1.2.4.orig/gzip.texi Thu Aug 19 06:42:50 1993
262+++ gzip-1.2.4-rsync/gzip.texi Fri Dec 29 21:20:54 2000
263@@ -316,6 +316,14 @@
264 into the directory and compress all the files it finds there (or
265 decompress them in the case of @code{gunzip}).
266
267+@item --rsyncable
268+While compressing, synchronize the output occasionally based on the
269+input. This reduces compression by about 1 percent most cases, but
270+means that the @code{rsync} program can take advantage of similarities
271+in the uncompressed input when syncronizing two files compressed with
272+this flag. @code{gunzip} cannot tell the difference between a
273+compressed file created with this option, and one created without it.
274+
275 @item --suffix @var{suf}
276 @itemx -S @var{suf}
277 Use suffix @samp{@var{suf}} instead of @samp{.gz}. Any suffix can be
278diff -ur gzip-1.2.4.orig/trees.c gzip-1.2.4-rsync/trees.c
279--- gzip-1.2.4.orig/trees.c Wed Aug 18 03:36:32 1993
280+++ gzip-1.2.4-rsync/trees.c Sat Dec 30 15:37:00 2000
281@@ -850,9 +850,10 @@
282 * trees or store, and output the encoded block to the zip file. This function
283 * returns the total compressed length for the file so far.
284 */
285-ulg flush_block(buf, stored_len, eof)
286+ulg flush_block(buf, stored_len, pad, eof)
287 char *buf; /* input block, or NULL if too old */
288 ulg stored_len; /* length of input block */
289+ int pad; /* pad output to byte boundary */
290 int eof; /* true if this is the last block for a file */
291 {
292 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
293@@ -944,6 +967,10 @@
294 Assert (input_len == isize, "bad input size");
295 bi_windup();
296 compressed_len += 7; /* align on byte boundary */
297+ } else if (pad && (compressed_len % 8) != 0) {
298+ send_bits((STORED_BLOCK<<1)+eof, 3); /* send block type */
299+ compressed_len = (compressed_len + 3 + 7) & ~7L;
300+ copy_block(buf, 0, 1); /* with header */
301 }
302 Tracev((stderr,"\ncomprlen %lu(%lu) ", compressed_len>>3,
303 compressed_len-7*eof));