Added a variable to t_stub.c so that "make test" works.
[rsync/rsync-patches.git] / gzip-rsyncable.diff
CommitLineData
58bf8889
WD
1From: Rusty Russell <rusty@rustcorp.com.au>
2
3[NOTE: this patch is for _gzip_! --Wayne]
83907074
MP
4
5It's pending for actual gzip inclusion! The maintainer was dug up and
6everything: it'll even be on by default.
7
8But here's the old patch,
9Rusty.
10--
11 Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
12
83907074
MP
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
15@@ -121,6 +121,14 @@
16 #endif
17 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
18
19+#ifndef RSYNC_WIN
20+# define RSYNC_WIN 4096
21+#endif
22+/* Size of rsync window, must be < MAX_DIST */
23+
24+#define RSYNC_SUM_MATCH(sum) ((sum) % RSYNC_WIN == 0)
25+/* Whether window sum matches magic value */
26+
27 /* ===========================================================================
28 * Local data used by the "longest match" routines.
29 */
30@@ -202,6 +210,8 @@
31 unsigned near good_match;
32 /* Use a faster search when the previous match is longer than this */
33
34+local ulg rsync_sum; /* rolling sum of rsync window */
35+local ulg rsync_chunk_end; /* next rsync sequence point */
36
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
39@@ -300,6 +310,10 @@
40 #endif
41 /* prev will be initialized on the fly */
42
43+ /* rsync params */
44+ rsync_chunk_end = 0xFFFFFFFFUL;
45+ rsync_sum = 0;
46+
47 /* Set the default configuration parameters:
48 */
49 max_lazy_match = configuration_table[pack_level].max_lazy;
50@@ -536,6 +550,8 @@
51 memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
52 match_start -= WSIZE;
53 strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
54+ if (rsync_chunk_end != 0xFFFFFFFFUL)
55+ rsync_chunk_end -= WSIZE;
56
57 block_start -= (long) WSIZE;
58
59@@ -563,13 +579,46 @@
60 }
61 }
62
63+local void rsync_roll(start, num)
64+ unsigned start;
65+ unsigned num;
66+{
67+ unsigned i;
68+
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];
74+ }
75+ num -= (RSYNC_WIN - start);
76+ start = RSYNC_WIN;
77+ }
78+
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;
87+ }
88+}
89+
90+/* ===========================================================================
91+ * Set rsync_chunk_end if window sum matches magic value.
92+ */
93+#define RSYNC_ROLL(s, n) \
94+ do { if (rsync) rsync_roll((s), (n)); } while(0)
95+
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.
99 */
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))
104
105 /* ===========================================================================
106 * Processes a new input file and return its compressed length. This
107@@ -580,7 +629,7 @@
108 local ulg deflate_fast()
109 {
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 */
114
115 prev_length = MIN_MATCH-1;
116@@ -609,6 +658,7 @@
117
118 lookahead -= match_length;
119
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.
123 */
124@@ -637,9 +687,14 @@
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);
129 lookahead--;
130 strstart++;
131 }
132+ if (rsync && strstart > rsync_chunk_end) {
133+ rsync_chunk_end = 0xFFFFFFFFUL;
134+ flush = 2;
135+ }
136 if (flush) FLUSH_BLOCK(0), block_start = strstart;
137
138 /* Make sure that we always have enough lookahead, except
139@@ -715,6 +770,7 @@
140 */
141 lookahead -= prev_length-1;
142 prev_length -= 2;
143+ RSYNC_ROLL(strstart, prev_length+1);
144 do {
145 strstart++;
146 INSERT_STRING(strstart, hash_head);
147@@ -727,24 +783,39 @@
148 match_available = 0;
149 match_length = MIN_MATCH-1;
150 strstart++;
151- if (flush) FLUSH_BLOCK(0), block_start = strstart;
152
153+ if (rsync && strstart > rsync_chunk_end) {
154+ rsync_chunk_end = 0xFFFFFFFFUL;
155+ flush = 2;
156+ }
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.
162 */
163 Tracevv((stderr,"%c",window[strstart-1]));
164- if (ct_tally (0, window[strstart-1])) {
165- FLUSH_BLOCK(0), block_start = strstart;
166- }
167+ flush = ct_tally (0, window[strstart-1]);
168+ if (rsync && strstart > rsync_chunk_end) {
169+ rsync_chunk_end = 0xFFFFFFFFUL;
170+ flush = 2;
171+ }
172+ if (flush) FLUSH_BLOCK(0), block_start = strstart;
173+ RSYNC_ROLL(strstart, 1);
174 strstart++;
175 lookahead--;
176 } else {
177 /* There is no previous match to compare with, wait for
178 * the next step to decide.
179 */
180+ if (rsync && strstart > rsync_chunk_end) {
181+ /* Reset huffman tree */
182+ rsync_chunk_end = 0xFFFFFFFFUL;
183+ flush = 2;
184+ FLUSH_BLOCK(0), block_start = strstart;
185+ }
186 match_available = 1;
187+ RSYNC_ROLL(strstart, 1);
188 strstart++;
189 lookahead--;
190 }
83907074
MP
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
193@@ -239,6 +239,7 @@
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 */
198
199 struct option longopts[] =
200 {
201@@ -268,6 +269,7 @@
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 */
206 { 0, 0, 0, 0 }
207 };
208
209@@ -357,6 +359,7 @@
210 " -Z --lzw produce output compatible with old compress",
211 " -b --bits maxbits max number of bits per code (implies -Z)",
212 #endif
213+ " --rsyncable Make rsync-friendly archive",
214 " file... files to (de)compress. If none given, use standard input.",
215 0};
216 char **p = help_msg;
217@@ -516,6 +519,9 @@
218 #else
219 recursive = 1; break;
220 #endif
221+ case 'R':
222+ rsync = 1; break;
223+
224 case 'S':
225 #ifdef NO_MULTIPLE_DOTS
226 if (*optarg == '.') optarg++;
83907074
MP
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
229@@ -131,6 +131,7 @@
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 */
234
235 extern long bytes_in; /* number of input bytes */
236 extern long bytes_out; /* number of output bytes */
237@@ -282,7 +283,7 @@
238 /* in trees.c */
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));
243
244 /* in bits.c */
245 void bi_init OF((file_t zipfile));
83907074
MP
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
248@@ -316,6 +316,14 @@
249 into the directory and compress all the files it finds there (or
250 decompress them in the case of @code{gunzip}).
251
252+@item --rsyncable
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.
259+
260 @item --suffix @var{suf}
261 @itemx -S @var{suf}
262 Use suffix @samp{@var{suf}} instead of @samp{.gz}. Any suffix can be
83907074
MP
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
265@@ -850,9 +850,10 @@
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.
268 */
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 */
275 {
276 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
277@@ -944,6 +967,10 @@
278 Assert (input_len == isize, "bad input size");
279 bi_windup();
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 */
285 }
286 Tracev((stderr,"\ncomprlen %lu(%lu) ", compressed_len>>3,
287 compressed_len-7*eof));