Debian Codegraph @

zgz/gzip/bits.c

raw
1 /* bits.c -- output variable-length bit strings
2
3 Copyright (C) 1999 Free Software Foundation, Inc.
4 Copyright (C) 1992-1993 Jean-loup Gailly
5 Copyright (C) 2008 Josh Triplett
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21
22 /*
23 * PURPOSE
24 *
25 * Output variable-length bit strings. Compression can be done
26 * to a file or to memory. (The latter is not supported in this version.)
27 *
28 * DISCUSSION
29 *
30 * The PKZIP "deflate" file format interprets compressed file data
31 * as a sequence of bits. Multi-bit strings in the file may cross
32 * byte boundaries without restriction.
33 *
34 * The first bit of each byte is the low-order bit.
35 *
36 * The routines in this file allow a variable-length bit value to
37 * be output right-to-left (useful for literal values). For
38 * left-to-right output (useful for code strings from the tree routines),
39 * the bits must have been reversed first with bi_reverse().
40 *
41 * For in-memory compression, the compressed bit stream goes directly
42 * into the requested output buffer. The input data is read in blocks
43 * by the mem_read() function. The buffer is limited to 64K on 16 bit
44 * machines.
45 *
46 * INTERFACE
47 *
48 * void bi_init (FILE *zipfile)
49 * Initialize the bit string routines.
50 *
51 * void send_bits (int value, int length)
52 * Write out a bit string, taking the source bits right to
53 * left.
54 *
55 * int bi_reverse (int value, int length)
56 * Reverse the bits of a bit string, taking the source bits left to
57 * right and emitting them right to left.
58 *
59 * void bi_windup (void)
60 * Write out any remaining bits in an incomplete byte.
61 *
62 * void copy_block(char *buf, unsigned len, int header)
63 * Copy a stored block to the zip file, storing first the length and
64 * its one's complement if requested.
65 *
66 */
67
68 #include "gzip.h"
69
70 /* ===========================================================================
71 * Local data used by the "bit string" routines.
72 */
73
74 static int zfile; /* output gzip file */
75
76 static unsigned short bi_buf;
77 /* Output buffer. bits are inserted starting at the bottom (least significant
78 * bits).
79 */
80
81 #define Buf_size (8 * 2*sizeof(char))
82 /* Number of bits used within bi_buf. (bi_buf might be implemented on
83 * more than 16 bits on some systems.)
84 */
85
86 static int bi_valid;
87 /* Number of valid bits in bi_buf. All bits above the last valid bit
88 * are always zero.
89 */
90
91 int (*read_buf)(char *buf, unsigned size);
92 /* Current input function. Set to mem_read for in-memory compression */
93
94 /* ===========================================================================
95 * Initialize the bit string routines.
96 */
97 void bi_init (int zipfile) /* output zip file, NO_FILE for in-memory compression */
98 {
99 zfile = zipfile;
100 bi_buf = 0;
101 bi_valid = 0;
102
103 /* Set the defaults for file compression. They are set by memcompress
104 * for in-memory compression.
105 */
106 if (zfile != NO_FILE) {
107 read_buf = file_read;
108 }
109 }
110
111 /* ===========================================================================
112 * Send a value on a given number of bits.
113 * IN assertion: length <= 16 and value fits in length bits.
114 */
115 void send_bits(int value, /* value to send */
116 int length) /* number of bits */
117 {
118 /* If not enough room in bi_buf, use (valid) bits from bi_buf and
119 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
120 * unused bits in value.
121 */
122 if (bi_valid > (int)Buf_size - length) {
123 bi_buf |= (value << bi_valid);
124 put_short(bi_buf);
125 bi_buf = (ush)value >> (Buf_size - bi_valid);
126 bi_valid += length - Buf_size;
127 } else {
128 bi_buf |= value << bi_valid;
129 bi_valid += length;
130 }
131 }
132
133 /* ===========================================================================
134 * Reverse the first len bits of a code, using straightforward code (a faster
135 * method would use a table)
136 * IN assertion: 1 <= len <= 15
137 */
138 unsigned bi_reverse(unsigned code, /* the value to invert */
139 int len) /* its bit length */
140 {
141 register unsigned res = 0;
142 do {
143 res |= code & 1;
144 code >>= 1, res <<= 1;
145 } while (--len > 0);
146 return res >> 1;
147 }
148
149 /* ===========================================================================
150 * Write out any remaining bits in an incomplete byte.
151 */
152 void bi_windup(void)
153 {
154 if (bi_valid > 8) {
155 put_short(bi_buf);
156 } else if (bi_valid > 0) {
157 put_byte(bi_buf);
158 }
159 bi_buf = 0;
160 bi_valid = 0;
161 }
162
163 /* ===========================================================================
164 * Copy a stored block to the zip file, storing first the length and its
165 * one's complement if requested.
166 */
167 void copy_block(char *buf, /* the input data */
168 unsigned len, /* its length */
169 int header) /* true if block header must be written */
170 {
171 bi_windup(); /* align on byte boundary */
172
173 if (header) {
174 put_short((ush)len);
175 put_short((ush)~len);
176 }
177 while (len--) {
178 put_byte(*buf++);
179 }
180 }