aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-07 19:38:06 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-07 19:38:06 +0000
commit30551fd6da3c13b96cebf3ec96ede8d54cf365f8 (patch)
tree29100216ba020a2c963585bc29acd20c42aed02e
parentad403413c776c36631da807b087f42aec1d4bdc9 (diff)
downloadbusybox-w32-30551fd6da3c13b96cebf3ec96ede8d54cf365f8.tar.gz
busybox-w32-30551fd6da3c13b96cebf3ec96ede8d54cf365f8.tar.bz2
busybox-w32-30551fd6da3c13b96cebf3ec96ede8d54cf365f8.zip
gzip cleanup part #2
-rw-r--r--archival/gzip.c260
1 files changed, 50 insertions, 210 deletions
diff --git a/archival/gzip.c b/archival/gzip.c
index c441942f3..a16e36978 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -141,19 +141,19 @@ aa: 85.1% -- replaced with aa.gz
141 141
142/* Diagnostic functions */ 142/* Diagnostic functions */
143#ifdef DEBUG 143#ifdef DEBUG
144# define Assert(cond,msg) {if(!(cond)) bb_error_msg(msg);} 144# define Assert(cond,msg) {if(!(cond)) bb_error_msg(msg);}
145# define Trace(x) fprintf x 145# define Trace(x) fprintf x
146# define Tracev(x) {if (verbose) fprintf x ;} 146# define Tracev(x) {if (verbose) fprintf x ;}
147# define Tracevv(x) {if (verbose>1) fprintf x ;} 147# define Tracevv(x) {if (verbose>1) fprintf x ;}
148# define Tracec(c,x) {if (verbose && (c)) fprintf x ;} 148# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
149# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;} 149# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
150#else 150#else
151# define Assert(cond,msg) 151# define Assert(cond,msg)
152# define Trace(x) 152# define Trace(x)
153# define Tracev(x) 153# define Tracev(x)
154# define Tracevv(x) 154# define Tracevv(x)
155# define Tracec(c,x) 155# define Tracec(c,x)
156# define Tracecv(c,x) 156# define Tracecv(c,x)
157#endif 157#endif
158 158
159typedef unsigned char uch; 159typedef unsigned char uch;
@@ -163,7 +163,7 @@ typedef unsigned long ulg;
163 163
164 /* from zip.c: */ 164 /* from zip.c: */
165static int zip(int in, int out); 165static int zip(int in, int out);
166static int file_read(char *buf, unsigned size); 166static unsigned file_read(void *buf, unsigned size);
167 167
168 /* from deflate.c */ 168 /* from deflate.c */
169static void lm_init(ush * flags); 169static void lm_init(ush * flags);
@@ -180,7 +180,6 @@ static void send_bits(int value, int length);
180static unsigned bi_reverse(unsigned value, int length); 180static unsigned bi_reverse(unsigned value, int length);
181static void bi_windup(void); 181static void bi_windup(void);
182static void copy_block(char *buf, unsigned len, int header); 182static void copy_block(char *buf, unsigned len, int header);
183static int (*read_buf) (char *buf, unsigned size);
184 183
185static void flush_outbuf(void); 184static void flush_outbuf(void);
186 185
@@ -298,22 +297,6 @@ static void clear_bufs(void)
298} 297}
299 298
300/* =========================================================================== 299/* ===========================================================================
301 * Does the same as write(), but also handles partial pipe writes and checks
302 * for error return.
303 */
304static void write_buf(int fd, void *buf, unsigned cnt)
305{
306 unsigned n;
307
308 while ((n = write(fd, buf, cnt)) != cnt) {
309 if (n == (unsigned) (-1))
310 bb_error_msg_and_die(bb_msg_write_error);
311 cnt -= n;
312 buf = (void *) ((char *) buf + n);
313 }
314}
315
316/* ===========================================================================
317 * Run a set of bytes through the crc shift register. If s is a NULL 300 * Run a set of bytes through the crc shift register. If s is a NULL
318 * pointer, then initialize the crc shift register contents instead. 301 * pointer, then initialize the crc shift register contents instead.
319 * Return the current crc in either case. 302 * Return the current crc in either case.
@@ -333,62 +316,9 @@ static uint32_t updcrc(uch * s, unsigned n)
333 } while (--n); 316 } while (--n);
334 } 317 }
335 crc = c; 318 crc = c;
336 return ~c; 319 return c;
337} 320}
338 321
339/* bits.c -- output variable-length bit strings
340 * Copyright (C) 1992-1993 Jean-loup Gailly
341 * This is free software; you can redistribute it and/or modify it under the
342 * terms of the GNU General Public License, see the file COPYING.
343 */
344
345
346/*
347 * PURPOSE
348 *
349 * Output variable-length bit strings. Compression can be done
350 * to a file or to memory. (The latter is not supported in this version.)
351 *
352 * DISCUSSION
353 *
354 * The PKZIP "deflate" file format interprets compressed file data
355 * as a sequence of bits. Multi-bit strings in the file may cross
356 * byte boundaries without restriction.
357 *
358 * The first bit of each byte is the low-order bit.
359 *
360 * The routines in this file allow a variable-length bit value to
361 * be output right-to-left (useful for literal values). For
362 * left-to-right output (useful for code strings from the tree routines),
363 * the bits must have been reversed first with bi_reverse().
364 *
365 * For in-memory compression, the compressed bit stream goes directly
366 * into the requested output buffer. The input data is read in blocks
367 * by the mem_read() function. The buffer is limited to 64K on 16 bit
368 * machines.
369 *
370 * INTERFACE
371 *
372 * void bi_init (FILE *zipfile)
373 * Initialize the bit string routines.
374 *
375 * void send_bits (int value, int length)
376 * Write out a bit string, taking the source bits right to
377 * left.
378 *
379 * int bi_reverse (int value, int length)
380 * Reverse the bits of a bit string, taking the source bits left to
381 * right and emitting them right to left.
382 *
383 * void bi_windup (void)
384 * Write out any remaining bits in an incomplete byte.
385 *
386 * void copy_block(char *buf, unsigned len, int header)
387 * Copy a stored block to the zip file, storing first the length and
388 * its one's complement if requested.
389 *
390 */
391
392/* =========================================================================== 322/* ===========================================================================
393 * Initialize the bit string routines. 323 * Initialize the bit string routines.
394 */ 324 */
@@ -400,13 +330,6 @@ static void bi_init(int zipfile)
400#ifdef DEBUG 330#ifdef DEBUG
401 bits_sent = 0L; 331 bits_sent = 0L;
402#endif 332#endif
403
404 /* Set the defaults for file compression. They are set by memcompress
405 * for in-memory compression.
406 */
407 if (zfile != NO_FILE) {
408 read_buf = file_read;
409 }
410} 333}
411 334
412/* =========================================================================== 335/* ===========================================================================
@@ -418,7 +341,7 @@ static void send_bits(int value, int length)
418#ifdef DEBUG 341#ifdef DEBUG
419 Tracev((stderr, " l %2d v %4x ", length, value)); 342 Tracev((stderr, " l %2d v %4x ", length, value));
420 Assert(length > 0 && length <= 15, "invalid length"); 343 Assert(length > 0 && length <= 15, "invalid length");
421 bits_sent += (ulg) length; 344 bits_sent += length;
422#endif 345#endif
423 /* If not enough room in bi_buf, use (valid) bits from bi_buf and 346 /* If not enough room in bi_buf, use (valid) bits from bi_buf and
424 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) 347 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
@@ -477,8 +400,8 @@ static void copy_block(char *buf, unsigned len, int header)
477 bi_windup(); /* align on byte boundary */ 400 bi_windup(); /* align on byte boundary */
478 401
479 if (header) { 402 if (header) {
480 put_16bit((ush) len); 403 put_16bit(len);
481 put_16bit((ush) ~ len); 404 put_16bit(~len);
482#ifdef DEBUG 405#ifdef DEBUG
483 bits_sent += 2 * 16; 406 bits_sent += 2 * 16;
484#endif 407#endif
@@ -491,61 +414,7 @@ static void copy_block(char *buf, unsigned len, int header)
491 } 414 }
492} 415}
493 416
494/* deflate.c -- compress data using the deflation algorithm 417/* void lm_init (int pack_level, ush *flags)
495 * Copyright (C) 1992-1993 Jean-loup Gailly
496 * This is free software; you can redistribute it and/or modify it under the
497 * terms of the GNU General Public License, see the file COPYING.
498 */
499
500/*
501 * PURPOSE
502 *
503 * Identify new text as repetitions of old text within a fixed-
504 * length sliding window trailing behind the new text.
505 *
506 * DISCUSSION
507 *
508 * The "deflation" process depends on being able to identify portions
509 * of the input text which are identical to earlier input (within a
510 * sliding window trailing behind the input currently being processed).
511 *
512 * The most straightforward technique turns out to be the fastest for
513 * most input files: try all possible matches and select the longest.
514 * The key feature of this algorithm is that insertions into the string
515 * dictionary are very simple and thus fast, and deletions are avoided
516 * completely. Insertions are performed at each input character, whereas
517 * string matches are performed only when the previous match ends. So it
518 * is preferable to spend more time in matches to allow very fast string
519 * insertions and avoid deletions. The matching algorithm for small
520 * strings is inspired from that of Rabin & Karp. A brute force approach
521 * is used to find longer strings when a small match has been found.
522 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
523 * (by Leonid Broukhis).
524 * A previous version of this file used a more sophisticated algorithm
525 * (by Fiala and Greene) which is guaranteed to run in linear amortized
526 * time, but has a larger average cost, uses more memory and is patented.
527 * However the F&G algorithm may be faster for some highly redundant
528 * files if the parameter max_chain_length (described below) is too large.
529 *
530 * ACKNOWLEDGMENTS
531 *
532 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
533 * I found it in 'freeze' written by Leonid Broukhis.
534 * Thanks to many info-zippers for bug reports and testing.
535 *
536 * REFERENCES
537 *
538 * APPNOTE.TXT documentation file in PKZIP 1.93a distribution.
539 *
540 * A description of the Rabin and Karp algorithm is given in the book
541 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
542 *
543 * Fiala,E.R., and Greene,D.H.
544 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
545 *
546 * INTERFACE
547 *
548 * void lm_init (int pack_level, ush *flags)
549 * Initialize the "longest match" routines for a new file 418 * Initialize the "longest match" routines for a new file
550 * 419 *
551 * ulg deflate (void) 420 * ulg deflate (void)
@@ -752,11 +621,12 @@ static void lm_init(ush * flags)
752 strstart = 0; 621 strstart = 0;
753 block_start = 0L; 622 block_start = 0L;
754 623
755 lookahead = read_buf((char *) window, 624 lookahead = file_read(window,
756 sizeof(int) <= 2 ? (unsigned) WSIZE : 2 * WSIZE); 625 sizeof(int) <= 2 ? (unsigned) WSIZE : 2 * WSIZE);
757 626
758 if (lookahead == 0 || lookahead == (unsigned) EOF) { 627 if (lookahead == 0 || lookahead == (unsigned) -1) {
759 eofile = 1, lookahead = 0; 628 eofile = 1;
629 lookahead = 0;
760 return; 630 return;
761 } 631 }
762 eofile = 0; 632 eofile = 0;
@@ -935,8 +805,8 @@ static void fill_window(void)
935 } 805 }
936 /* At this point, more >= 2 */ 806 /* At this point, more >= 2 */
937 if (!eofile) { 807 if (!eofile) {
938 n = read_buf((char *) window + strstart + lookahead, more); 808 n = file_read(window + strstart + lookahead, more);
939 if (n == 0 || n == (unsigned) EOF) { 809 if (n == 0 || n == (unsigned) -1) {
940 eofile = 1; 810 eofile = 1;
941 } else { 811 } else {
942 lookahead += n; 812 lookahead += n;
@@ -977,8 +847,9 @@ static ulg deflate(void)
977 prev_length = match_length, prev_match = match_start; 847 prev_length = match_length, prev_match = match_start;
978 match_length = MIN_MATCH - 1; 848 match_length = MIN_MATCH - 1;
979 849
980 if (hash_head != NIL && prev_length < max_lazy_match && 850 if (hash_head != 0 && prev_length < max_lazy_match
981 strstart - hash_head <= MAX_DIST) { 851 && strstart - hash_head <= MAX_DIST
852 ) {
982 /* To simplify the code, we prevent matches with the string 853 /* To simplify the code, we prevent matches with the string
983 * of window index 0 (in particular we have to avoid a match 854 * of window index 0 (in particular we have to avoid a match
984 * of the string with itself at the start of the input file). 855 * of the string with itself at the start of the input file).
@@ -1024,7 +895,6 @@ static ulg deflate(void)
1024 strstart++; 895 strstart++;
1025 if (flush) 896 if (flush)
1026 FLUSH_BLOCK(0), block_start = strstart; 897 FLUSH_BLOCK(0), block_start = strstart;
1027
1028 } else if (match_available) { 898 } else if (match_available) {
1029 /* If there was no match at the previous position, output a 899 /* If there was no match at the previous position, output a
1030 * single literal. If there was a match but the current match 900 * single literal. If there was a match but the current match
@@ -1060,30 +930,6 @@ static ulg deflate(void)
1060 return FLUSH_BLOCK(1); /* eof */ 930 return FLUSH_BLOCK(1); /* eof */
1061} 931}
1062 932
1063/* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
1064 * Copyright (C) 1992-1993 Jean-loup Gailly
1065 * The unzip code was written and put in the public domain by Mark Adler.
1066 * Portions of the lzw code are derived from the public domain 'compress'
1067 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
1068 * Ken Turkowski, Dave Mack and Peter Jannesen.
1069 *
1070 * See the license_msg below and the file COPYING for the software license.
1071 * See the file algorithm.doc for the compression algorithms and file formats.
1072 */
1073
1074/* Compress files with zip algorithm and 'compress' interface.
1075 * See usage() and help() functions below for all options.
1076 * Outputs:
1077 * file.gz: compressed file with same mode, owner, and utimes
1078 * or stdout with -c option or if stdin used as input.
1079 * If the output file name had to be truncated, the original name is kept
1080 * in the compressed file.
1081 */
1082
1083/* configuration */
1084
1085typedef struct dirent dir_type;
1086
1087/* ======================================================================== */ 933/* ======================================================================== */
1088static void abort_gzip(int ATTRIBUTE_UNUSED ignored) 934static void abort_gzip(int ATTRIBUTE_UNUSED ignored)
1089{ 935{
@@ -1268,12 +1114,12 @@ int gzip_main(int argc, char **argv)
1268 * 1114 *
1269 * INTERFACE 1115 * INTERFACE
1270 * 1116 *
1271 * void ct_init (ush *attr, int *methodp) 1117 * void ct_init(ush *attr, int *methodp)
1272 * Allocate the match buffer, initialize the various tables and save 1118 * Allocate the match buffer, initialize the various tables and save
1273 * the location of the internal file attribute (ascii/binary) and 1119 * the location of the internal file attribute (ascii/binary) and
1274 * method (DEFLATE/STORE) 1120 * method (DEFLATE/STORE)
1275 * 1121 *
1276 * void ct_tally (int dist, int lc); 1122 * void ct_tally(int dist, int lc);
1277 * Save the match info and tally the frequency counts. 1123 * Save the match info and tally the frequency counts.
1278 * 1124 *
1279 * long flush_block (char *buf, ulg stored_len, int eof) 1125 * long flush_block (char *buf, ulg stored_len, int eof)
@@ -1433,17 +1279,17 @@ typedef struct tree_desc {
1433 int max_code; /* largest code with non zero frequency */ 1279 int max_code; /* largest code with non zero frequency */
1434} tree_desc; 1280} tree_desc;
1435 1281
1436static tree_desc l_desc = 1282static tree_desc l_desc = {
1437 { dyn_ltree, static_ltree, extra_lbits, LITERALS + 1, L_CODES, 1283 dyn_ltree, static_ltree, extra_lbits,
1438 MAX_BITS, 0 1284 LITERALS + 1, L_CODES, MAX_BITS, 0
1439}; 1285};
1440 1286
1441static tree_desc d_desc = 1287static tree_desc d_desc = {
1442 { dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0 }; 1288 dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0
1289};
1443 1290
1444static tree_desc bl_desc = 1291static tree_desc bl_desc = {
1445 { bl_tree, (ct_data *) 0, extra_blbits, 0, BL_CODES, MAX_BL_BITS, 1292 bl_tree, NULL, extra_blbits, 0, BL_CODES, MAX_BL_BITS, 0
1446 0
1447}; 1293};
1448 1294
1449 1295
@@ -1451,8 +1297,9 @@ static ush bl_count[MAX_BITS + 1];
1451 1297
1452/* number of codes at each bit length for an optimal tree */ 1298/* number of codes at each bit length for an optimal tree */
1453 1299
1454static const uch bl_order[BL_CODES] 1300static const uch bl_order[BL_CODES] = {
1455= { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; 1301 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
1302};
1456 1303
1457/* The lengths of the bit length codes are sent in order of decreasing 1304/* The lengths of the bit length codes are sent in order of decreasing
1458 * probability, to avoid transmitting the lengths for unused bit length codes. 1305 * probability, to avoid transmitting the lengths for unused bit length codes.
@@ -2334,13 +2181,6 @@ static void set_file_type(void)
2334 } 2181 }
2335} 2182}
2336 2183
2337/* zip.c -- compress files to the gzip or pkzip format
2338 * Copyright (C) 1992-1993 Jean-loup Gailly
2339 * This is free software; you can redistribute it and/or modify it under the
2340 * terms of the GNU General Public License, see the file COPYING.
2341 */
2342
2343
2344static uint32_t crc; /* crc on uncompressed file data */ 2184static uint32_t crc; /* crc on uncompressed file data */
2345 2185
2346/* =========================================================================== 2186/* ===========================================================================
@@ -2369,7 +2209,7 @@ static int zip(int in, int out)
2369 put_32bit(time_stamp); 2209 put_32bit(time_stamp);
2370 2210
2371 /* Write deflated file to zip file */ 2211 /* Write deflated file to zip file */
2372 crc = updcrc(0, 0); 2212 crc = 0;
2373 2213
2374 bi_init(out); 2214 bi_init(out);
2375 ct_init(&attr, &method); 2215 ct_init(&attr, &method);
@@ -2388,25 +2228,24 @@ static int zip(int in, int out)
2388 return 0; 2228 return 0;
2389} 2229}
2390 2230
2391
2392/* =========================================================================== 2231/* ===========================================================================
2393 * Read a new buffer from the current input file, perform end-of-line 2232 * Read a new buffer from the current input file, perform end-of-line
2394 * translation, and update the crc and input file size. 2233 * translation, and update the crc and input file size.
2395 * IN assertion: size >= 2 (for end-of-line translation) 2234 * IN assertion: size >= 2 (for end-of-line translation)
2396 */ 2235 */
2397static int file_read(char *buf, unsigned size) 2236static unsigned file_read(void *buf, unsigned size)
2398{ 2237{
2399 unsigned len; 2238 unsigned len;
2400 2239
2401 Assert(insize == 0, "inbuf not empty"); 2240 Assert(insize == 0, "inbuf not empty");
2402 2241
2403 len = read(ifd, buf, size); 2242 len = read(ifd, buf, size);
2404 if (len == (unsigned) (-1) || len == 0) 2243 if (len == (unsigned)(-1) || len == 0)
2405 return (int) len; 2244 return len;
2406 2245
2407 crc = updcrc((uch *) buf, len); 2246 crc = ~updcrc((uch *) buf, len);
2408 isize += (ulg) len; 2247 isize += len;
2409 return (int) len; 2248 return len;
2410} 2249}
2411 2250
2412/* =========================================================================== 2251/* ===========================================================================
@@ -2418,6 +2257,7 @@ static void flush_outbuf(void)
2418 if (outcnt == 0) 2257 if (outcnt == 0)
2419 return; 2258 return;
2420 2259
2421 write_buf(ofd, (char *) outbuf, outcnt); 2260 xwrite(ofd, (char *) outbuf, outcnt);
2422 outcnt = 0; 2261 outcnt = 0;
2423} 2262}
2263