Blame SOURCES/gzip-1.3.13-rsync.patch

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