aboutsummaryrefslogtreecommitdiff
path: root/archival/gzip.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-07 19:38:26 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-07 19:38:26 +0000
commited0f6db35e02ccd0d8c22337639463a05e3efea1 (patch)
tree18ca97d9c2768b73e721d8b54c788fd82344f268 /archival/gzip.c
parent30551fd6da3c13b96cebf3ec96ede8d54cf365f8 (diff)
downloadbusybox-w32-ed0f6db35e02ccd0d8c22337639463a05e3efea1.tar.gz
busybox-w32-ed0f6db35e02ccd0d8c22337639463a05e3efea1.tar.bz2
busybox-w32-ed0f6db35e02ccd0d8c22337639463a05e3efea1.zip
gzip cleanup part #3
Diffstat (limited to 'archival/gzip.c')
-rw-r--r--archival/gzip.c139
1 files changed, 66 insertions, 73 deletions
diff --git a/archival/gzip.c b/archival/gzip.c
index a16e36978..dcbcae0b3 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -61,8 +61,6 @@ aa: 85.1% -- replaced with aa.gz
61# endif 61# endif
62#endif 62#endif
63 63
64#define NO_FILE (-1) /* in memory compression */
65
66#define PACK_MAGIC "\037\036" /* Magic header for packed files */ 64#define PACK_MAGIC "\037\036" /* Magic header for packed files */
67#define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */ 65#define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
68#define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */ 66#define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
@@ -161,28 +159,26 @@ typedef unsigned short ush;
161typedef unsigned long ulg; 159typedef unsigned long ulg;
162 160
163 161
164 /* from zip.c: */ 162/* from zip.c: */
165static int zip(int in, int out); 163static int zip(int in, int out);
166static unsigned file_read(void *buf, unsigned size); 164static unsigned file_read(void *buf, unsigned size);
167 165
168 /* from deflate.c */ 166/* from deflate.c */
169static void lm_init(ush * flags); 167static void lm_init(ush * flags);
170static ulg deflate(void); 168static ulg deflate(void);
171 169
172 /* from trees.c */ 170/* from trees.c */
173static void ct_init(ush * attr, int *methodp); 171static void ct_init(ush * attr, int *methodp);
174static int ct_tally(int dist, int lc); 172static int ct_tally(int dist, int lc);
175static ulg flush_block(char *buf, ulg stored_len, int eof); 173static ulg flush_block(char *buf, ulg stored_len, int eof);
176 174
177 /* from bits.c */ 175/* from bits.c */
178static void bi_init(int zipfile); 176static void bi_init(int zipfile);
179static void send_bits(int value, int length); 177static void send_bits(int value, int length);
180static unsigned bi_reverse(unsigned value, int length); 178static unsigned bi_reverse(unsigned value, int length);
181static void bi_windup(void); 179static void bi_windup(void);
182static void copy_block(char *buf, unsigned len, int header); 180static void copy_block(char *buf, unsigned len, int header);
183 181
184static void flush_outbuf(void);
185
186/* global buffers */ 182/* global buffers */
187 183
188/* To save memory for 16 bit systems, some arrays are overlaid between 184/* To save memory for 16 bit systems, some arrays are overlaid between
@@ -250,6 +246,20 @@ static ulg bits_sent; /* bit length of the compressed data */
250 246
251 247
252/* =========================================================================== 248/* ===========================================================================
249 * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
250 * (used for the compressed data only)
251 */
252static void flush_outbuf(void)
253{
254 if (outcnt == 0)
255 return;
256
257 xwrite(ofd, (char *) outbuf, outcnt);
258 outcnt = 0;
259}
260
261
262/* ===========================================================================
253 */ 263 */
254/* put_8bit is used for the compressed output */ 264/* put_8bit is used for the compressed output */
255#define put_8bit(c) \ 265#define put_8bit(c) \
@@ -284,6 +294,7 @@ static void put_32bit(ulg n)
284 outbuf[outcnt++] = (c); \ 294 outbuf[outcnt++] = (c); \
285} 295}
286 296
297
287/* =========================================================================== 298/* ===========================================================================
288 * Clear input and output buffers 299 * Clear input and output buffers
289 */ 300 */
@@ -296,29 +307,52 @@ static void clear_bufs(void)
296 isize = 0L; 307 isize = 0L;
297} 308}
298 309
310
299/* =========================================================================== 311/* ===========================================================================
300 * Run a set of bytes through the crc shift register. If s is a NULL 312 * Run a set of bytes through the crc shift register. If s is a NULL
301 * pointer, then initialize the crc shift register contents instead. 313 * pointer, then initialize the crc shift register contents instead.
302 * Return the current crc in either case. 314 * Return the current crc in either case.
303 */ 315 */
316static uint32_t crc = ~0; /* shift register contents */
304static uint32_t updcrc(uch * s, unsigned n) 317static uint32_t updcrc(uch * s, unsigned n)
305{ 318{
306 static uint32_t crc = ~0; /* shift register contents */
307 uint32_t c; /* temporary variable */ 319 uint32_t c; /* temporary variable */
308 320
309 if (s == NULL) { 321 if (s == NULL) {
310 c = ~0; 322 c = ~0;
311 } else { 323 } else {
312 c = crc; 324 c = crc;
313 if (n) 325 while (n) {
314 do { 326 c = crc_32_tab[(uch)(c ^ *s++)] ^ (c >> 8);
315 c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8); 327 n--;
316 } while (--n); 328 }
317 } 329 }
318 crc = c; 330 crc = c;
319 return c; 331 return c;
320} 332}
321 333
334
335/* ===========================================================================
336 * Read a new buffer from the current input file, perform end-of-line
337 * translation, and update the crc and input file size.
338 * IN assertion: size >= 2 (for end-of-line translation)
339 */
340static unsigned file_read(void *buf, unsigned size)
341{
342 unsigned len;
343
344 Assert(insize == 0, "inbuf not empty");
345
346 len = safe_read(ifd, buf, size);
347 if (len == (unsigned)(-1) || len == 0)
348 return len;
349
350 updcrc(buf, len);
351 isize += len;
352 return len;
353}
354
355
322/* =========================================================================== 356/* ===========================================================================
323 * Initialize the bit string routines. 357 * Initialize the bit string routines.
324 */ 358 */
@@ -332,6 +366,7 @@ static void bi_init(int zipfile)
332#endif 366#endif
333} 367}
334 368
369
335/* =========================================================================== 370/* ===========================================================================
336 * Send a value on a given number of bits. 371 * Send a value on a given number of bits.
337 * IN assertion: length <= 16 and value fits in length bits. 372 * IN assertion: length <= 16 and value fits in length bits.
@@ -414,16 +449,6 @@ static void copy_block(char *buf, unsigned len, int header)
414 } 449 }
415} 450}
416 451
417/* void lm_init (int pack_level, ush *flags)
418 * Initialize the "longest match" routines for a new file
419 *
420 * ulg deflate (void)
421 * Processes a new input file and return its compressed length. Sets
422 * the compressed length, crc, deflate flags and internal file
423 * attributes.
424 */
425
426
427/* =========================================================================== 452/* ===========================================================================
428 * Configuration parameters 453 * Configuration parameters
429 */ 454 */
@@ -517,7 +542,7 @@ static unsigned ins_h; /* hash index of string to be inserted */
517/* Number of bits by which ins_h and del_h must be shifted at each 542/* Number of bits by which ins_h and del_h must be shifted at each
518 * input step. It must be such that after MIN_MATCH steps, the oldest 543 * input step. It must be such that after MIN_MATCH steps, the oldest
519 * byte no longer takes part in the hash key, that is: 544 * byte no longer takes part in the hash key, that is:
520 * H_SHIFT * MIN_MATCH >= HASH_BITS 545 * H_SHIFT * MIN_MATCH >= HASH_BITS
521 */ 546 */
522 547
523static unsigned int prev_length; 548static unsigned int prev_length;
@@ -664,8 +689,7 @@ static int longest_match(IPos cur_match)
664 uch *match; /* matched string */ 689 uch *match; /* matched string */
665 int len; /* length of current match */ 690 int len; /* length of current match */
666 int best_len = prev_length; /* best match length so far */ 691 int best_len = prev_length; /* best match length so far */
667 IPos limit = 692 IPos limit = strstart > (IPos) MAX_DIST ? strstart - (IPos) MAX_DIST : 0;
668 strstart > (IPos) MAX_DIST ? strstart - (IPos) MAX_DIST : NIL;
669 /* Stop when cur_match becomes <= limit. To simplify the code, 693 /* Stop when cur_match becomes <= limit. To simplify the code,
670 * we prevent matches with the string of window index 0. 694 * we prevent matches with the string of window index 0.
671 */ 695 */
@@ -739,8 +763,7 @@ static int longest_match(IPos cur_match)
739static void check_match(IPos start, IPos match, int length) 763static void check_match(IPos start, IPos match, int length)
740{ 764{
741 /* check that the match is indeed a match */ 765 /* check that the match is indeed a match */
742 if (memcmp((char *) window + match, 766 if (memcmp(window + match, window + start, length) != EQUAL) {
743 (char *) window + start, length) != EQUAL) {
744 bb_error_msg(" start %d, match %d, length %d", start, match, length); 767 bb_error_msg(" start %d, match %d, length %d", start, match, length);
745 bb_error_msg("invalid match"); 768 bb_error_msg("invalid match");
746 } 769 }
@@ -766,14 +789,13 @@ static void check_match(IPos start, IPos match, int length)
766static void fill_window(void) 789static void fill_window(void)
767{ 790{
768 unsigned n, m; 791 unsigned n, m;
769 unsigned more = 792 unsigned more = window_size - lookahead - strstart;
770 (unsigned) (window_size - (ulg) lookahead - (ulg) strstart);
771 /* Amount of free space at the end of the window. */ 793 /* Amount of free space at the end of the window. */
772 794
773 /* If the window is almost full and there is insufficient lookahead, 795 /* If the window is almost full and there is insufficient lookahead,
774 * move the upper half to the lower one to make room in the upper half. 796 * move the upper half to the lower one to make room in the upper half.
775 */ 797 */
776 if (more == (unsigned) EOF) { 798 if (more == (unsigned) -1) {
777 /* Very unlikely, but possible on 16 bit machine if strstart == 0 799 /* Very unlikely, but possible on 16 bit machine if strstart == 0
778 * and lookahead == 1 (input done one byte at time) 800 * and lookahead == 1 (input done one byte at time)
779 */ 801 */
@@ -784,7 +806,7 @@ static void fill_window(void)
784 */ 806 */
785 Assert(window_size == (ulg) 2 * WSIZE, "no sliding with BIG_MEM"); 807 Assert(window_size == (ulg) 2 * WSIZE, "no sliding with BIG_MEM");
786 808
787 memcpy((char *) window, (char *) window + WSIZE, (unsigned) WSIZE); 809 memcpy(window, window + WSIZE, WSIZE);
788 match_start -= WSIZE; 810 match_start -= WSIZE;
789 strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */ 811 strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
790 812
@@ -819,13 +841,19 @@ static void fill_window(void)
819 * IN assertion: strstart is set to the end of the current match. 841 * IN assertion: strstart is set to the end of the current match.
820 */ 842 */
821#define FLUSH_BLOCK(eof) \ 843#define FLUSH_BLOCK(eof) \
822 flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \ 844 flush_block(block_start >= 0L \
823 (char*)NULL, (long)strstart - block_start, (eof)) 845 ? (char*)&window[(unsigned)block_start] \
846 : (char*)NULL, \
847 (long)strstart - block_start, (eof))
824 848
825/* =========================================================================== 849/* ===========================================================================
826 * Same as above, but achieves better compression. We use a lazy 850 * Same as above, but achieves better compression. We use a lazy
827 * evaluation for matches: a match is finally adopted only if there is 851 * evaluation for matches: a match is finally adopted only if there is
828 * no better match at the next window position. 852 * no better match at the next window position.
853 *
854 * Processes a new input file and return its compressed length. Sets
855 * the compressed length, crc, deflate flags and internal file
856 * attributes.
829 */ 857 */
830static ulg deflate(void) 858static ulg deflate(void)
831{ 859{
@@ -930,6 +958,7 @@ static ulg deflate(void)
930 return FLUSH_BLOCK(1); /* eof */ 958 return FLUSH_BLOCK(1); /* eof */
931} 959}
932 960
961
933/* ======================================================================== */ 962/* ======================================================================== */
934static void abort_gzip(int ATTRIBUTE_UNUSED ignored) 963static void abort_gzip(int ATTRIBUTE_UNUSED ignored)
935{ 964{
@@ -2181,8 +2210,6 @@ static void set_file_type(void)
2181 } 2210 }
2182} 2211}
2183 2212
2184static uint32_t crc; /* crc on uncompressed file data */
2185
2186/* =========================================================================== 2213/* ===========================================================================
2187 * Deflate in to out. 2214 * Deflate in to out.
2188 * IN assertions: the input and output buffers are cleared. 2215 * IN assertions: the input and output buffers are cleared.
@@ -2209,7 +2236,7 @@ static int zip(int in, int out)
2209 put_32bit(time_stamp); 2236 put_32bit(time_stamp);
2210 2237
2211 /* Write deflated file to zip file */ 2238 /* Write deflated file to zip file */
2212 crc = 0; 2239 crc = ~0;
2213 2240
2214 bi_init(out); 2241 bi_init(out);
2215 ct_init(&attr, &method); 2242 ct_init(&attr, &method);
@@ -2218,46 +2245,12 @@ static int zip(int in, int out)
2218 put_8bit((uch) deflate_flags); /* extra flags */ 2245 put_8bit((uch) deflate_flags); /* extra flags */
2219 put_8bit(3); /* OS identifier = 3 (Unix) */ 2246 put_8bit(3); /* OS identifier = 3 (Unix) */
2220 2247
2221 (void) deflate(); 2248 deflate();
2222 2249
2223 /* Write the crc and uncompressed size */ 2250 /* Write the crc and uncompressed size */
2224 put_32bit(crc); 2251 put_32bit(~crc);
2225 put_32bit(isize); 2252 put_32bit(isize);
2226 2253
2227 flush_outbuf(); 2254 flush_outbuf();
2228 return 0; 2255 return 0;
2229} 2256}
2230
2231/* ===========================================================================
2232 * Read a new buffer from the current input file, perform end-of-line
2233 * translation, and update the crc and input file size.
2234 * IN assertion: size >= 2 (for end-of-line translation)
2235 */
2236static unsigned file_read(void *buf, unsigned size)
2237{
2238 unsigned len;
2239
2240 Assert(insize == 0, "inbuf not empty");
2241
2242 len = read(ifd, buf, size);
2243 if (len == (unsigned)(-1) || len == 0)
2244 return len;
2245
2246 crc = ~updcrc((uch *) buf, len);
2247 isize += len;
2248 return len;
2249}
2250
2251/* ===========================================================================
2252 * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
2253 * (used for the compressed data only)
2254 */
2255static void flush_outbuf(void)
2256{
2257 if (outcnt == 0)
2258 return;
2259
2260 xwrite(ofd, (char *) outbuf, outcnt);
2261 outcnt = 0;
2262}
2263