build: avoid warning about deprecated use of automake's ACLOCAL_AMFLAGS
[gzip.git] / bits.c
blob846b2f282f0a70bb1bb985c755c59876f07e0a10
1 /* bits.c -- output variable-length bit strings
3 Copyright (C) 1999, 2009-2013 Free Software Foundation, Inc.
4 Copyright (C) 1992-1993 Jean-loup Gailly
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 * PURPOSE
24 * Output variable-length bit strings. Compression can be done
25 * to a file or to memory. (The latter is not supported in this version.)
27 * DISCUSSION
29 * The PKZIP "deflate" file format interprets compressed file data
30 * as a sequence of bits. Multi-bit strings in the file may cross
31 * byte boundaries without restriction.
33 * The first bit of each byte is the low-order bit.
35 * The routines in this file allow a variable-length bit value to
36 * be output right-to-left (useful for literal values). For
37 * left-to-right output (useful for code strings from the tree routines),
38 * the bits must have been reversed first with bi_reverse().
40 * For in-memory compression, the compressed bit stream goes directly
41 * into the requested output buffer. The input data is read in blocks
42 * by the mem_read() function. The buffer is limited to 64K on 16 bit
43 * machines.
45 * INTERFACE
47 * void bi_init (FILE *zipfile)
48 * Initialize the bit string routines.
50 * void send_bits (int value, int length)
51 * Write out a bit string, taking the source bits right to
52 * left.
54 * int bi_reverse (int value, int length)
55 * Reverse the bits of a bit string, taking the source bits left to
56 * right and emitting them right to left.
58 * void bi_windup (void)
59 * Write out any remaining bits in an incomplete byte.
61 * void copy_block(char *buf, unsigned len, int header)
62 * Copy a stored block to the zip file, storing first the length and
63 * its one's complement if requested.
67 #include <config.h>
68 #include "tailor.h"
69 #include "gzip.h"
71 #ifdef DEBUG
72 # include <stdio.h>
73 #endif
75 /* ===========================================================================
76 * Local data used by the "bit string" routines.
79 local file_t zfile; /* output gzip file */
81 local unsigned short bi_buf;
82 /* Output buffer. bits are inserted starting at the bottom (least significant
83 * bits).
86 #define Buf_size (8 * 2*sizeof(char))
87 /* Number of bits used within bi_buf. (bi_buf might be implemented on
88 * more than 16 bits on some systems.)
91 local int bi_valid;
92 /* Number of valid bits in bi_buf. All bits above the last valid bit
93 * are always zero.
96 int (*read_buf) (char *buf, unsigned size);
97 /* Current input function. Set to mem_read for in-memory compression */
99 #ifdef DEBUG
100 off_t bits_sent; /* bit length of the compressed data */
101 #endif
103 /* ===========================================================================
104 * Initialize the bit string routines.
106 void bi_init (zipfile)
107 file_t zipfile; /* output zip file, NO_FILE for in-memory compression */
109 zfile = zipfile;
110 bi_buf = 0;
111 bi_valid = 0;
112 #ifdef DEBUG
113 bits_sent = 0L;
114 #endif
116 /* Set the defaults for file compression. They are set by memcompress
117 * for in-memory compression.
119 if (zfile != NO_FILE) {
120 read_buf = file_read;
124 /* ===========================================================================
125 * Send a value on a given number of bits.
126 * IN assertion: length <= 16 and value fits in length bits.
128 void send_bits(value, length)
129 int value; /* value to send */
130 int length; /* number of bits */
132 #ifdef DEBUG
133 Tracev((stderr," l %2d v %4x ", length, value));
134 Assert(length > 0 && length <= 15, "invalid length");
135 bits_sent += (off_t)length;
136 #endif
137 /* If not enough room in bi_buf, use (valid) bits from bi_buf and
138 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
139 * unused bits in value.
141 if (bi_valid > (int)Buf_size - length) {
142 bi_buf |= (value << bi_valid);
143 put_short(bi_buf);
144 bi_buf = (ush)value >> (Buf_size - bi_valid);
145 bi_valid += length - Buf_size;
146 } else {
147 bi_buf |= value << bi_valid;
148 bi_valid += length;
152 /* ===========================================================================
153 * Reverse the first len bits of a code, using straightforward code (a faster
154 * method would use a table)
155 * IN assertion: 1 <= len <= 15
157 unsigned bi_reverse(code, len)
158 unsigned code; /* the value to invert */
159 int len; /* its bit length */
161 register unsigned res = 0;
162 do {
163 res |= code & 1;
164 code >>= 1, res <<= 1;
165 } while (--len > 0);
166 return res >> 1;
169 /* ===========================================================================
170 * Write out any remaining bits in an incomplete byte.
172 void bi_windup()
174 if (bi_valid > 8) {
175 put_short(bi_buf);
176 } else if (bi_valid > 0) {
177 put_byte(bi_buf);
179 bi_buf = 0;
180 bi_valid = 0;
181 #ifdef DEBUG
182 bits_sent = (bits_sent+7) & ~7;
183 #endif
186 /* ===========================================================================
187 * Copy a stored block to the zip file, storing first the length and its
188 * one's complement if requested.
190 void copy_block(buf, len, header)
191 char *buf; /* the input data */
192 unsigned len; /* its length */
193 int header; /* true if block header must be written */
195 bi_windup(); /* align on byte boundary */
197 if (header) {
198 put_short((ush)len);
199 put_short((ush)~len);
200 #ifdef DEBUG
201 bits_sent += 2*16;
202 #endif
204 #ifdef DEBUG
205 bits_sent += (off_t)len<<3;
206 #endif
207 while (len--) {
208 #ifdef CRYPT
209 int t;
210 if (key) zencode(*buf, t);
211 #endif
212 put_byte(*buf++);