updated rsyncable patch
[debian/gzip] / debian / patches / rsyncable.diff
1 --- a/deflate.c
2 +++ b/deflate.c
3 @@ -131,6 +131,14 @@
4  #endif
5  /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
6  
7 +#ifndef RSYNC_WIN
8 +#  define RSYNC_WIN 8192
9 +#endif
10 +/* Size of rsync window, must be < MAX_DIST */
11 +
12 +#define RSYNC_SUM_MATCH(sum) (((sum) & (RSYNC_WIN - 1)) == 0)
13 +/* Whether window sum matches magic value */
14 +
15  /* ===========================================================================
16   * Local data used by the "longest match" routines.
17   */
18 @@ -212,6 +220,8 @@
19  unsigned good_match;
20  /* Use a faster search when the previous match is longer than this */
21  
22 +local ulg rsync_sum;  /* rolling sum of rsync window */
23 +local ulg rsync_chunk_end; /* next rsync sequence point */
24  
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
27 @@ -314,6 +324,10 @@
28  #endif
29      /* prev will be initialized on the fly */
30  
31 +    /* rsync params */
32 +    rsync_chunk_end = 0xFFFFFFFFUL;
33 +    rsync_sum = 0;
34 +
35      /* Set the default configuration parameters:
36       */
37      max_lazy_match   = configuration_table[pack_level].max_lazy;
38 @@ -550,6 +564,8 @@
39          memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
40          match_start -= WSIZE;
41          strstart    -= WSIZE; /* we now have strstart >= MAX_DIST: */
42 +       if (rsync_chunk_end != 0xFFFFFFFFUL)
43 +           rsync_chunk_end -= WSIZE;
44  
45          block_start -= (long) WSIZE;
46  
47 @@ -579,13 +595,44 @@
48      }
49  }
50  
51 +local void rsync_roll(unsigned start, unsigned num)
52 +{
53 +    unsigned i;
54 +
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];
60 +       }
61 +       num -= (RSYNC_WIN - start);
62 +       start = RSYNC_WIN;
63 +    }
64 +
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;
73 +    }
74 +}
75 +
76 +/* ===========================================================================
77 + * Set rsync_chunk_end if window sum matches magic value.
78 + */
79 +#define RSYNC_ROLL(s, n) \
80 +   do { if (rsync) rsync_roll((s), (n)); } while(0)
81 +
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.
85   */
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))
90  
91  /* ===========================================================================
92   * Processes a new input file and return its compressed length. This
93 @@ -596,7 +643,7 @@
94  local off_t deflate_fast()
95  {
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 */
100  
101      prev_length = MIN_MATCH-1;
102 @@ -626,7 +673,8 @@
103  
104              lookahead -= match_length;
105  
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.
110               */
111              if (match_length <= max_insert_length) {
112 @@ -654,9 +702,14 @@
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);
117              lookahead--;
118              strstart++;
119          }
120 +        if (rsync && strstart > rsync_chunk_end) {
121 +            flush = 2;
122 +            rsync_chunk_end = 0xFFFFFFFFUL;
123 +        }
124          if (flush) FLUSH_BLOCK(0), block_start = strstart;
125  
126          /* Make sure that we always have enough lookahead, except
127 @@ -679,7 +732,7 @@
128  {
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 */
135  
136 @@ -730,6 +783,7 @@
137               */
138              lookahead -= prev_length-1;
139              prev_length -= 2;
140 +           RSYNC_ROLL(strstart, prev_length+1);
141              do {
142                  strstart++;
143                  INSERT_STRING(strstart, hash_head);
144 @@ -742,24 +796,38 @@
145              match_available = 0;
146              match_length = MIN_MATCH-1;
147              strstart++;
148 -            if (flush) FLUSH_BLOCK(0), block_start = strstart;
149  
150 +           if (rsync && strstart > rsync_chunk_end) {
151 +               rsync_chunk_end = 0xFFFFFFFFUL;
152 +               flush = 2;
153 +           }
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.
159               */
160              Tracevv((stderr,"%c",window[strstart-1]));
161 -            if (ct_tally (0, window[strstart-1])) {
162 -                FLUSH_BLOCK(0), block_start = strstart;
163 -            }
164 +           flush = ct_tally (0, window[strstart-1]);
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 +           RSYNC_ROLL(strstart, 1);
171              strstart++;
172              lookahead--;
173          } else {
174              /* There is no previous match to compare with, wait for
175               * the next step to decide.
176               */
177 +           if (rsync && strstart > rsync_chunk_end) {
178 +               rsync_chunk_end = 0xFFFFFFFFUL;
179 +               flush = 2;
180 +               FLUSH_BLOCK(0), block_start = strstart;
181 +           }
182              match_available = 1;
183 +           RSYNC_ROLL(strstart, 1);
184              strstart++;
185              lookahead--;
186          }
187 --- a/doc/gzip.texi
188 +++ b/doc/gzip.texi
189 @@ -208,6 +208,7 @@
190    -V, --version     display version number
191    -1, --fast        compress faster
192    -9, --best        compress better
193 +    --rsyncable   Make rsync-friendly archive
194  
195  With no FILE, or when FILE is -, read standard input.
196  
197 @@ -358,6 +359,14 @@
198  into the directory and compress all the files it finds there (or
199  decompress them in the case of @command{gunzip}).
200  
201 +@item --rsyncable
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.
208 +
209  @item --suffix @var{suf}
210  @itemx -S @var{suf}
211  Use suffix @var{suf} instead of @samp{.gz}.  Any suffix can be
212 --- a/gzip.c
213 +++ b/gzip.c
214 @@ -215,6 +215,7 @@
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 */
219  
220  static int handled_sig[] =
221    {
222 @@ -275,7 +276,7 @@
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) */
226 -
227 +    {"rsyncable",  0, 0, 'R'}, /* make rsync-friendly archive */
228      { 0, 0, 0, 0 }
229  };
230  
231 @@ -359,6 +360,7 @@
232   "  -Z, --lzw         produce output compatible with old compress",
233   "  -b, --bits=BITS   max number of bits per code (implies -Z)",
234  #endif
235 + "  --rsyncable       Make rsync-friendly archive",
236   "",
237   "With no FILE, or when FILE is -, read standard input.",
238   "",
239 @@ -489,8 +491,11 @@
240  #else
241              recursive = 1;
242  #endif
243 -            break;
244 -        case 'S':
245 +           break;
246 +        case 'R':
247 +           rsync = 1; break;
248 +
249 +       case 'S':
250  #ifdef NO_MULTIPLE_DOTS
251              if (*optarg == '.') optarg++;
252  #endif
253 --- a/gzip.h
254 +++ b/gzip.h
255 @@ -140,6 +140,7 @@
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 */
260  
261  extern off_t bytes_in;   /* number of input bytes */
262  extern off_t bytes_out;  /* number of output bytes */
263 @@ -287,7 +288,7 @@
264          /* in trees.c */
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);
269  
270          /* in bits.c */
271  extern void     bi_init    (file_t zipfile);
272 --- a/gzip.1
273 +++ b/gzip.1
274 @@ -5,6 +5,7 @@
275  .ll +8
276  .B gzip
277  .RB [ " \-acdfhklLnNrtvV19 " ]
278 +.RB [ --rsyncable ]
279  .RB [ \-S\ suffix ]
280  [
281  .I "name \&..."
282 @@ -287,6 +288,16 @@
283  .I gunzip
284  ).
285  .TP
286 +.B --rsyncable
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
289 +.BR rsync (1)
290 +program can take advantage of similarities in the uncompressed input
291 +when syncronizing two files compressed with this flag.
292 +.I gunzip
293 +cannot tell the difference between a compressed file created with this option,
294 +and one created without it.
295 +.TP
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
299 --- a/trees.c
300 +++ b/trees.c
301 @@ -856,9 +856,10 @@
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.
304   */
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 */
311  {
312      ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
313 @@ -951,6 +952,10 @@
314          Assert (input_len == bytes_in, "bad input size");
315          bi_windup();
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 */
321      }
322  
323      return compressed_len >> 3;