forked from Distrotech/gzip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits.c
214 lines (192 loc) · 6.27 KB
/
bits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* bits.c -- output variable-length bit strings
Copyright (C) 1999, 2009-2013 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
*
* Output variable-length bit strings. Compression can be done
* to a file or to memory. (The latter is not supported in this version.)
*
* DISCUSSION
*
* The PKZIP "deflate" file format interprets compressed file data
* as a sequence of bits. Multi-bit strings in the file may cross
* byte boundaries without restriction.
*
* The first bit of each byte is the low-order bit.
*
* The routines in this file allow a variable-length bit value to
* be output right-to-left (useful for literal values). For
* left-to-right output (useful for code strings from the tree routines),
* the bits must have been reversed first with bi_reverse().
*
* For in-memory compression, the compressed bit stream goes directly
* into the requested output buffer. The input data is read in blocks
* by the mem_read() function. The buffer is limited to 64K on 16 bit
* machines.
*
* INTERFACE
*
* void bi_init (FILE *zipfile)
* Initialize the bit string routines.
*
* void send_bits (int value, int length)
* Write out a bit string, taking the source bits right to
* left.
*
* int bi_reverse (int value, int length)
* Reverse the bits of a bit string, taking the source bits left to
* right and emitting them right to left.
*
* void bi_windup (void)
* Write out any remaining bits in an incomplete byte.
*
* void copy_block(char *buf, unsigned len, int header)
* Copy a stored block to the zip file, storing first the length and
* its one's complement if requested.
*
*/
#include <config.h>
#include "tailor.h"
#include "gzip.h"
#ifdef DEBUG
# include <stdio.h>
#endif
/* ===========================================================================
* Local data used by the "bit string" routines.
*/
local file_t zfile; /* output gzip file */
local unsigned short bi_buf;
/* Output buffer. bits are inserted starting at the bottom (least significant
* bits).
*/
#define Buf_size (8 * 2*sizeof(char))
/* Number of bits used within bi_buf. (bi_buf might be implemented on
* more than 16 bits on some systems.)
*/
local int bi_valid;
/* Number of valid bits in bi_buf. All bits above the last valid bit
* are always zero.
*/
int (*read_buf) (char *buf, unsigned size);
/* Current input function. Set to mem_read for in-memory compression */
#ifdef DEBUG
off_t bits_sent; /* bit length of the compressed data */
#endif
/* ===========================================================================
* Initialize the bit string routines.
*/
void bi_init (zipfile)
file_t zipfile; /* output zip file, NO_FILE for in-memory compression */
{
zfile = zipfile;
bi_buf = 0;
bi_valid = 0;
#ifdef DEBUG
bits_sent = 0L;
#endif
/* Set the defaults for file compression. They are set by memcompress
* for in-memory compression.
*/
if (zfile != NO_FILE) {
read_buf = file_read;
}
}
/* ===========================================================================
* Send a value on a given number of bits.
* IN assertion: length <= 16 and value fits in length bits.
*/
void send_bits(value, length)
int value; /* value to send */
int length; /* number of bits */
{
#ifdef DEBUG
Tracev((stderr," l %2d v %4x ", length, value));
Assert(length > 0 && length <= 15, "invalid length");
bits_sent += (off_t)length;
#endif
/* If not enough room in bi_buf, use (valid) bits from bi_buf and
* (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
* unused bits in value.
*/
if (bi_valid > (int)Buf_size - length) {
bi_buf |= (value << bi_valid);
put_short(bi_buf);
bi_buf = (ush)value >> (Buf_size - bi_valid);
bi_valid += length - Buf_size;
} else {
bi_buf |= value << bi_valid;
bi_valid += length;
}
}
/* ===========================================================================
* Reverse the first len bits of a code, using straightforward code (a faster
* method would use a table)
* IN assertion: 1 <= len <= 15
*/
unsigned bi_reverse(code, len)
unsigned code; /* the value to invert */
int len; /* its bit length */
{
register unsigned res = 0;
do {
res |= code & 1;
code >>= 1, res <<= 1;
} while (--len > 0);
return res >> 1;
}
/* ===========================================================================
* Write out any remaining bits in an incomplete byte.
*/
void bi_windup()
{
if (bi_valid > 8) {
put_short(bi_buf);
} else if (bi_valid > 0) {
put_byte(bi_buf);
}
bi_buf = 0;
bi_valid = 0;
#ifdef DEBUG
bits_sent = (bits_sent+7) & ~7;
#endif
}
/* ===========================================================================
* Copy a stored block to the zip file, storing first the length and its
* one's complement if requested.
*/
void copy_block(buf, len, header)
char *buf; /* the input data */
unsigned len; /* its length */
int header; /* true if block header must be written */
{
bi_windup(); /* align on byte boundary */
if (header) {
put_short((ush)len);
put_short((ush)~len);
#ifdef DEBUG
bits_sent += 2*16;
#endif
}
#ifdef DEBUG
bits_sent += (off_t)len<<3;
#endif
while (len--) {
#ifdef CRYPT
int t;
if (key) zencode(*buf, t);
#endif
put_byte(*buf++);
}
}