cnd / rpms / gzip

Forked from rpms/gzip 3 years ago
Clone

Blame SOURCES/gzip-1.3.13-rsync.patch

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