build: update gnulib submodule to latest
[debian/gzip] / trees.c
diff --git a/trees.c b/trees.c
index 27396a0d027484fd970d2a7425c8e62fc10384b5..f40f8f41a8ae3f6e1a867ff99867fce33c58c9e5 100644 (file)
--- a/trees.c
+++ b/trees.c
@@ -1,8 +1,21 @@
 /* trees.c -- output deflated data using Huffman coding
- * Copyright (C) 1992-1993 Jean-loup Gailly
- * This is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License, see the file COPYING.
- */
+
+   Copyright (C) 1997-1999, 2009-2017 Free Software Foundation, Inc.
+   Copyright (C) 1992-1993 Jean-loup Gailly
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /*
  *  PURPOSE
  *      void ct_tally (int dist, int lc);
  *          Save the match info and tally the frequency counts.
  *
- *      long flush_block (char *buf, ulg stored_len, int eof)
+ *      off_t flush_block (char *buf, ulg stored_len, int eof)
  *          Determine the best encoding for the current block: dynamic trees,
  *          static trees or store, and output the encoded block to the zip
  *          file. Returns the total compressed length for the file so far.
  *
  */
 
+#include <config.h>
 #include <ctype.h>
 
 #include "tailor.h"
 #include "gzip.h"
 
-#ifdef RCSID
-static char rcsid[] = "$Id$";
-#endif
-
 /* ===========================================================================
  * Constants
  */
@@ -271,17 +281,16 @@ local uch flag_bit;         /* current bit used in flags */
 local ulg opt_len;        /* bit length of current block with optimal trees */
 local ulg static_len;     /* bit length of current block with static trees */
 
-local ulg compressed_len; /* total bit length of compressed file */
+local off_t compressed_len; /* total bit length of compressed file */
 
-local ulg input_len;      /* total byte length of input file */
+local off_t input_len;      /* total byte length of input file */
 /* input_len is for debugging only since we can get it by other means. */
 
-ush *file_type;        /* pointer to UNKNOWN, BINARY or ASCII */
-int *file_method;      /* pointer to DEFLATE or STORE */
+static ush *file_type;        /* pointer to UNKNOWN, BINARY or ASCII */
+static int *file_method;      /* pointer to DEFLATE or STORE */
 
 #ifdef DEBUG
-extern ulg bits_sent;  /* bit length of the compressed data */
-extern long isize;     /* byte length of input file */
+extern off_t bits_sent;  /* bit length of the compressed data */
 #endif
 
 extern long block_start;       /* window offset of current block */
@@ -291,17 +300,17 @@ extern unsigned near strstart; /* window offset of current string */
  * Local (static) routines in this file.
  */
 
-local void init_block     OF((void));
-local void pqdownheap     OF((ct_data near *tree, int k));
-local void gen_bitlen     OF((tree_desc near *desc));
-local void gen_codes      OF((ct_data near *tree, int max_code));
-local void build_tree     OF((tree_desc near *desc));
-local void scan_tree      OF((ct_data near *tree, int max_code));
-local void send_tree      OF((ct_data near *tree, int max_code));
-local int  build_bl_tree  OF((void));
-local void send_all_trees OF((int lcodes, int dcodes, int blcodes));
-local void compress_block OF((ct_data near *ltree, ct_data near *dtree));
-local void set_file_type  OF((void));
+local void init_block     (void);
+local void pqdownheap     (ct_data near *tree, int k);
+local void gen_bitlen     (tree_desc near *desc);
+local void gen_codes      (ct_data near *tree, int max_code);
+local void build_tree     (tree_desc near *desc);
+local void scan_tree      (ct_data near *tree, int max_code);
+local void send_tree      (ct_data near *tree, int max_code);
+local int  build_bl_tree  (void);
+local void send_all_trees (int lcodes, int dcodes, int blcodes);
+local void compress_block (ct_data near *ltree, ct_data near *dtree);
+local void set_file_type  (void);
 
 
 #ifndef DEBUG
@@ -342,7 +351,7 @@ void ct_init(attr, methodp)
     file_type = attr;
     file_method = methodp;
     compressed_len = input_len = 0L;
-        
+
     if (static_dtree[0].Len != 0) return; /* ct_init already called */
 
     /* Initialize the mapping length (0..255) -> length code (0..28) */
@@ -810,7 +819,7 @@ local int build_bl_tree()
     }
     /* Update opt_len to include the bit length tree and counts */
     opt_len += 3*(max_blindex+1) + 5+5+4;
-    Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", opt_len, static_len));
+    Tracev((stderr, "\ndyn trees: dyn %lu, stat %lu", opt_len, static_len));
 
     return max_blindex;
 }
@@ -836,13 +845,10 @@ local void send_all_trees(lcodes, dcodes, blcodes)
         Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
         send_bits(bl_tree[bl_order[rank]].Len, 3);
     }
-    Tracev((stderr, "\nbl tree: sent %ld", bits_sent));
 
     send_tree((ct_data near *)dyn_ltree, lcodes-1); /* send the literal tree */
-    Tracev((stderr, "\nlit tree: sent %ld", bits_sent));
 
     send_tree((ct_data near *)dyn_dtree, dcodes-1); /* send the distance tree */
-    Tracev((stderr, "\ndist tree: sent %ld", bits_sent));
 }
 
 /* ===========================================================================
@@ -850,9 +856,10 @@ local void send_all_trees(lcodes, dcodes, blcodes)
  * trees or store, and output the encoded block to the zip file. This function
  * returns the total compressed length for the file so far.
  */
-ulg flush_block(buf, stored_len, eof)
+off_t flush_block(buf, stored_len, pad, eof)
     char *buf;        /* input block, or NULL if too old */
     ulg stored_len;   /* length of input block */
+    int pad;          /* pad output to byte boundary */
     int eof;          /* true if this is the last block for a file */
 {
     ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
@@ -865,10 +872,10 @@ ulg flush_block(buf, stored_len, eof)
 
     /* Construct the literal and distance trees */
     build_tree((tree_desc near *)(&l_desc));
-    Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len));
+    Tracev((stderr, "\nlit data: dyn %lu, stat %lu", opt_len, static_len));
 
     build_tree((tree_desc near *)(&d_desc));
-    Tracev((stderr, "\ndist data: dyn %ld, stat %ld", opt_len, static_len));
+    Tracev((stderr, "\ndist data: dyn %lu, stat %lu", opt_len, static_len));
     /* At this point, opt_len and static_len are the total bit lengths of
      * the compressed block data, excluding the tree representations.
      */
@@ -899,7 +906,8 @@ ulg flush_block(buf, stored_len, eof)
     if (stored_len <= opt_lenb && eof && compressed_len == 0L && seekable()) {
 #endif
         /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
-        if (buf == (char*)0) error ("block vanished");
+        if (!buf)
+          gzip_error ("block vanished");
 
         copy_block(buf, (unsigned)stored_len, 0); /* without header */
         compressed_len = stored_len << 3;
@@ -941,12 +949,14 @@ ulg flush_block(buf, stored_len, eof)
     init_block();
 
     if (eof) {
-        Assert (input_len == isize, "bad input size");
+        Assert (input_len == bytes_in, "bad input size");
         bi_windup();
         compressed_len += 7;  /* align on byte boundary */
+    } else if (pad && (compressed_len % 8) != 0) {
+        send_bits((STORED_BLOCK<<1)+eof, 3);  /* send block type */
+        compressed_len = (compressed_len + 3 + 7) & ~7L;
+        copy_block(buf, 0, 1); /* with header */
     }
-    Tracev((stderr,"\ncomprlen %lu(%lu) ", compressed_len>>3,
-           compressed_len-7*eof));
 
     return compressed_len >> 3;
 }
@@ -1070,6 +1080,6 @@ local void set_file_type()
     while (n < LITERALS) bin_freq += dyn_ltree[n++].Freq;
     *file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII;
     if (*file_type == BINARY && translate_eol) {
-        warn("-l used on binary file", "");
+        warning ("-l used on binary file");
     }
 }