f408d8
Index: gzip-1.5/deflate.c
f408d8
===================================================================
f408d8
--- gzip-1.5.orig/deflate.c
f408d8
+++ gzip-1.5/deflate.c
f408d8
@@ -131,6 +131,14 @@
f408d8
 #endif
f408d8
 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
f408d8
 
f408d8
+#ifndef RSYNC_WIN
f408d8
+#  define RSYNC_WIN 4096
f408d8
+#endif
f408d8
+/* Size of rsync window, must be < MAX_DIST */
f408d8
+
f408d8
+#define RSYNC_SUM_MATCH(sum) ((sum) % RSYNC_WIN == 0)
f408d8
+/* Whether window sum matches magic value */
f408d8
+
f408d8
 /* ===========================================================================
f408d8
  * Local data used by the "longest match" routines.
f408d8
  */
f408d8
@@ -212,6 +220,8 @@ local int compr_level;
f408d8
 unsigned good_match;
f408d8
 /* Use a faster search when the previous match is longer than this */
f408d8
 
f408d8
+local ulg rsync_sum;  /* rolling sum of rsync window */
f408d8
+local ulg rsync_chunk_end; /* next rsync sequence point */
f408d8
 
f408d8
 /* Values for max_lazy_match, good_match and max_chain_length, depending on
f408d8
  * the desired pack level (0..9). The values given below have been tuned to
f408d8
@@ -314,6 +324,10 @@ void lm_init (pack_level, flags)
f408d8
 #endif
f408d8
     /* prev will be initialized on the fly */
f408d8
 
f408d8
+    /* rsync params */
f408d8
+    rsync_chunk_end = 0xFFFFFFFFUL;
f408d8
+    rsync_sum = 0;
f408d8
+
f408d8
     /* Set the default configuration parameters:
f408d8
      */
f408d8
     max_lazy_match   = configuration_table[pack_level].max_lazy;
f408d8
@@ -550,6 +564,8 @@ local void fill_window()
f408d8
         memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
f408d8
         match_start -= WSIZE;
f408d8
         strstart    -= WSIZE; /* we now have strstart >= MAX_DIST: */
f408d8
+        if (rsync_chunk_end != 0xFFFFFFFFUL)
f408d8
+            rsync_chunk_end -= WSIZE;
f408d8
 
f408d8
         block_start -= (long) WSIZE;
f408d8
 
f408d8
@@ -579,13 +595,46 @@ local void fill_window()
f408d8
     }
f408d8
 }
f408d8
 
f408d8
+local void rsync_roll(start, num)
f408d8
+    unsigned start;
f408d8
+    unsigned num;
f408d8
+{
f408d8
+    unsigned i;
f408d8
+
f408d8
+    if (start < RSYNC_WIN) {
f408d8
+       /* before window fills. */
f408d8
+       for (i = start; i < RSYNC_WIN; i++) {
f408d8
+           if (i == start + num) return;
f408d8
+           rsync_sum += (ulg)window[i];
f408d8
+       }
f408d8
+       num -= (RSYNC_WIN - start);
f408d8
+       start = RSYNC_WIN;
f408d8
+    }
f408d8
+
f408d8
+    /* buffer after window full */
f408d8
+    for (i = start; i < start+num; i++) {
f408d8
+       /* New character in */
f408d8
+       rsync_sum += (ulg)window[i];
f408d8
+       /* Old character out */
f408d8
+       rsync_sum -= (ulg)window[i - RSYNC_WIN];
f408d8
+       if (rsync_chunk_end == 0xFFFFFFFFUL && RSYNC_SUM_MATCH(rsync_sum))
f408d8
+           rsync_chunk_end = i;
f408d8
+    }
f408d8
+}
f408d8
+
f408d8
+/* ===========================================================================
f408d8
+ * Set rsync_chunk_end if window sum matches magic value.
f408d8
+ */
f408d8
+#define RSYNC_ROLL(s, n) \
f408d8
+   do { if (rsync) rsync_roll((s), (n)); } while(0)
f408d8
+
f408d8
 /* ===========================================================================
f408d8
  * Flush the current block, with given end-of-file flag.
f408d8
  * IN assertion: strstart is set to the end of the current match.
f408d8
  */
f408d8
 #define FLUSH_BLOCK(eof) \
f408d8
    flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \
f408d8
-                (char*)NULL, (long)strstart - block_start, (eof))
f408d8
+                (char*)NULL, (long)strstart - block_start, flush-1, (eof)) 
f408d8
 
f408d8
 /* ===========================================================================
f408d8
  * Processes a new input file and return its compressed length. This
f408d8
@@ -596,7 +645,7 @@ local void fill_window()
f408d8
 local off_t deflate_fast()
f408d8
 {
f408d8
     IPos hash_head; /* head of the hash chain */
f408d8
-    int flush;      /* set if current block must be flushed */
f408d8
+    int flush;      /* set if current block must be flushed, 2=>and padded  */ 
f408d8
     unsigned match_length = 0;  /* length of best match */
f408d8
 
f408d8
     prev_length = MIN_MATCH-1;
f408d8
@@ -625,7 +674,8 @@ local off_t deflate_fast()
f408d8
             flush = ct_tally(strstart-match_start, match_length - MIN_MATCH);
f408d8
 
f408d8
             lookahead -= match_length;
f408d8
-
f408d8
+            
f408d8
+            RSYNC_ROLL(strstart, match_length);
f408d8
             /* Insert new strings in the hash table only if the match length
f408d8
              * is not too large. This saves time but degrades compression.
f408d8
              */
f408d8
@@ -654,9 +704,14 @@ local off_t deflate_fast()
f408d8
             /* No match, output a literal byte */
f408d8
             Tracevv((stderr,"%c",window[strstart]));
f408d8
             flush = ct_tally (0, window[strstart]);
f408d8
+            RSYNC_ROLL(strstart, 1);
f408d8
             lookahead--;
f408d8
             strstart++;
f408d8
         }
f408d8
+        if (rsync && strstart > rsync_chunk_end) {                                                        
f408d8
+            rsync_chunk_end = 0xFFFFFFFFUL;                                                               
f408d8
+            flush = 2;                                                                                    
f408d8
+       }  
f408d8
         if (flush) FLUSH_BLOCK(0), block_start = strstart;
f408d8
 
f408d8
         /* Make sure that we always have enough lookahead, except
f408d8
@@ -730,6 +785,7 @@ off_t deflate()
f408d8
              */
f408d8
             lookahead -= prev_length-1;
f408d8
             prev_length -= 2;
f408d8
+            RSYNC_ROLL(strstart, prev_length+1);
f408d8
             do {
f408d8
                 strstart++;
f408d8
                 INSERT_STRING(strstart, hash_head);
f408d8
@@ -742,24 +798,40 @@ off_t deflate()
f408d8
             match_available = 0;
f408d8
             match_length = MIN_MATCH-1;
f408d8
             strstart++;
f408d8
-            if (flush) FLUSH_BLOCK(0), block_start = strstart;
f408d8
 
f408d8
+            if (rsync && strstart > rsync_chunk_end) {
f408d8
+                rsync_chunk_end = 0xFFFFFFFFUL;
f408d8
+                flush = 2;
f408d8
+            }
f408d8
+            if (flush) FLUSH_BLOCK(0), block_start = strstart;
f408d8
         } else if (match_available) {
f408d8
             /* If there was no match at the previous position, output a
f408d8
              * single literal. If there was a match but the current match
f408d8
              * is longer, truncate the previous match to a single literal.
f408d8
              */
f408d8
             Tracevv((stderr,"%c",window[strstart-1]));
f408d8
-            if (ct_tally (0, window[strstart-1])) {
f408d8
-                FLUSH_BLOCK(0), block_start = strstart;
f408d8
+            flush = ct_tally (0, window[strstart-1]);
f408d8
+            if (rsync && strstart > rsync_chunk_end) {
f408d8
+                rsync_chunk_end = 0xFFFFFFFFUL;
f408d8
+                flush = 2;
f408d8
             }
f408d8
+            if (flush) FLUSH_BLOCK(0), block_start = strstart;
f408d8
+            RSYNC_ROLL(strstart, 1);
f408d8
             strstart++;
f408d8
             lookahead--;
f408d8
         } else {
f408d8
             /* There is no previous match to compare with, wait for
f408d8
              * the next step to decide.
f408d8
              */
f408d8
+            if (rsync && strstart > rsync_chunk_end) {
f408d8
+                /* Reset huffman tree */
f408d8
+                rsync_chunk_end = 0xFFFFFFFFUL;
f408d8
+                flush = 2;
f408d8
+                FLUSH_BLOCK(0), block_start = strstart;
f408d8
+            }             
f408d8
+             
f408d8
             match_available = 1;
f408d8
+            RSYNC_ROLL(strstart, 1);  
f408d8
             strstart++;
f408d8
             lookahead--;
f408d8
         }
f408d8
Index: gzip-1.5/doc/gzip.texi
f408d8
===================================================================
f408d8
--- gzip-1.5.orig/doc/gzip.texi
f408d8
+++ gzip-1.5/doc/gzip.texi
f408d8
@@ -353,6 +353,14 @@ specified on the command line are direct
f408d8
 into the directory and compress all the files it finds there (or
f408d8
 decompress them in the case of @command{gunzip}).
f408d8
 
f408d8
+@item --rsyncable
f408d8
+While compressing, synchronize the output occasionally based on the
f408d8
+input.  This reduces compression by about 1 percent most cases, but
f408d8
+means that the @code{rsync} program can take advantage of similarities
f408d8
+in the uncompressed input when syncronizing two files compressed with
f408d8
+this flag.  @code{gunzip} cannot tell the difference between a
f408d8
+compressed file created with this option, and one created without it.
f408d8
+
f408d8
 @item --suffix @var{suf}
f408d8
 @itemx -S @var{suf}
f408d8
 Use suffix @var{suf} instead of @samp{.gz}.  Any suffix can be
f408d8
Index: gzip-1.5/gzip.c
f408d8
===================================================================
f408d8
--- gzip-1.5.orig/gzip.c
f408d8
+++ gzip-1.5/gzip.c
f408d8
@@ -213,6 +213,7 @@ int  ofd;                  /* output fil
f408d8
 unsigned insize;           /* valid bytes in inbuf */
f408d8
 unsigned inptr;            /* index of next byte to be processed in inbuf */
f408d8
 unsigned outcnt;           /* bytes in output buffer */
f408d8
+int rsync = 0;             /* make ryncable chunks */
f408d8
 
f408d8
 static int handled_sig[] =
f408d8
   {
f408d8
@@ -270,7 +271,7 @@ static const struct option longopts[] =
f408d8
     {"best",       0, 0, '9'}, /* compress better */
f408d8
     {"lzw",        0, 0, 'Z'}, /* make output compatible with old compress */
f408d8
     {"bits",       1, 0, 'b'}, /* max number of bits per code (implies -Z) */
f408d8
-
f408d8
+    {"rsyncable",  0, 0, 'R'}, /* make rsync-friendly archive */ 
f408d8
     { 0, 0, 0, 0 }
f408d8
 };
f408d8
 
f408d8
@@ -353,6 +354,7 @@ local void help()
f408d8
  "  -Z, --lzw         produce output compatible with old compress",
f408d8
  "  -b, --bits=BITS   max number of bits per code (implies -Z)",
f408d8
 #endif
f408d8
+ "    --rsyncable   Make rsync-friendly archive",  
f408d8
  "",
f408d8
  "With no FILE, or when FILE is -, read standard input.",
f408d8
  "",
f408d8
@@ -482,6 +484,9 @@ int main (int argc, char **argv)
f408d8
             recursive = 1;
f408d8
 #endif
f408d8
             break;
f408d8
+            
f408d8
+        case 'R':
f408d8
+            rsync = 1; break;
f408d8
         case 'S':
f408d8
 #ifdef NO_MULTIPLE_DOTS
f408d8
             if (*optarg == '.') optarg++;
f408d8
Index: gzip-1.5/gzip.h
f408d8
===================================================================
f408d8
--- gzip-1.5.orig/gzip.h
f408d8
+++ gzip-1.5/gzip.h
f408d8
@@ -140,6 +140,7 @@ EXTERN(uch, window);         /* Sliding
f408d8
 extern unsigned insize; /* valid bytes in inbuf */
f408d8
 extern unsigned inptr;  /* index of next byte to be processed in inbuf */
f408d8
 extern unsigned outcnt; /* bytes in output buffer */
f408d8
+extern int rsync;  /* deflate into rsyncable chunks */ 
f408d8
 
f408d8
 extern off_t bytes_in;   /* number of input bytes */
f408d8
 extern off_t bytes_out;  /* number of output bytes */
f408d8
@@ -287,7 +288,7 @@ extern off_t deflate (void);
f408d8
         /* in trees.c */
f408d8
 extern void ct_init     (ush *attr, int *method);
f408d8
 extern int  ct_tally    (int dist, int lc);
f408d8
-extern off_t flush_block (char *buf, ulg stored_len, int eof);
f408d8
+extern off_t flush_block (char *buf, ulg stored_len, int pad, int eof);
f408d8
 
f408d8
         /* in bits.c */
f408d8
 extern void     bi_init    (file_t zipfile);
f408d8
Index: gzip-1.5/trees.c
f408d8
===================================================================
f408d8
--- gzip-1.5.orig/trees.c
f408d8
+++ gzip-1.5/trees.c
f408d8
@@ -856,9 +856,10 @@ local void send_all_trees(lcodes, dcodes
f408d8
  * trees or store, and output the encoded block to the zip file. This function
f408d8
  * returns the total compressed length for the file so far.
f408d8
  */
f408d8
-off_t flush_block(buf, stored_len, eof)
f408d8
+off_t flush_block(buf, stored_len, pad, eof)  
f408d8
     char *buf;        /* input block, or NULL if too old */
f408d8
     ulg stored_len;   /* length of input block */
f408d8
+    int pad;          /* pad output to byte boundary */ 
f408d8
     int eof;          /* true if this is the last block for a file */
f408d8
 {
f408d8
     ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
f408d8
@@ -951,6 +952,10 @@ off_t flush_block(buf, stored_len, eof)
f408d8
         Assert (input_len == bytes_in, "bad input size");
f408d8
         bi_windup();
f408d8
         compressed_len += 7;  /* align on byte boundary */
f408d8
+    } else if (pad && (compressed_len % 8) != 0) {
f408d8
+        send_bits((STORED_BLOCK<<1)+eof, 3);  /* send block type */
f408d8
+        compressed_len = (compressed_len + 3 + 7) & ~7L;
f408d8
+        copy_block(buf, 0, 1); /* with header */
f408d8
     }
f408d8
 
f408d8
     return compressed_len >> 3;