diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2001-03-28 05:35:16 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2001-03-28 05:35:16 +0000 |
commit | f58efb57d1c7134cfeaf73e84f210c2458826b80 (patch) | |
tree | 9f4a919574128ae05a430381137d9bb22f3c7f48 | |
parent | a2e68fc23319d77789cae6eca899f328d15368a5 (diff) | |
download | busybox-w32-f58efb57d1c7134cfeaf73e84f210c2458826b80.tar.gz busybox-w32-f58efb57d1c7134cfeaf73e84f210c2458826b80.tar.bz2 busybox-w32-f58efb57d1c7134cfeaf73e84f210c2458826b80.zip |
Add functions that were shared with gunzip.c, gunzip about to change.
-rw-r--r-- | archival/gzip.c | 129 | ||||
-rw-r--r-- | gzip.c | 129 |
2 files changed, 244 insertions, 14 deletions
diff --git a/archival/gzip.c b/archival/gzip.c index 6369b894a..2a7604648 100644 --- a/archival/gzip.c +++ b/archival/gzip.c | |||
@@ -42,6 +42,7 @@ | |||
42 | #include <stdio.h> | 42 | #include <stdio.h> |
43 | #include <string.h> | 43 | #include <string.h> |
44 | #include <unistd.h> | 44 | #include <unistd.h> |
45 | #include <errno.h> | ||
45 | #include "busybox.h" | 46 | #include "busybox.h" |
46 | #define BB_DECLARE_EXTERN | 47 | #define BB_DECLARE_EXTERN |
47 | #define bb_need_memory_exhausted | 48 | #define bb_need_memory_exhausted |
@@ -72,7 +73,7 @@ typedef unsigned long ulg; | |||
72 | /* methods 4 to 7 reserved */ | 73 | /* methods 4 to 7 reserved */ |
73 | #define DEFLATED 8 | 74 | #define DEFLATED 8 |
74 | #define MAX_METHODS 9 | 75 | #define MAX_METHODS 9 |
75 | extern int method; /* compression method */ | 76 | static int method; /* compression method */ |
76 | 77 | ||
77 | /* To save memory for 16 bit systems, some arrays are overlaid between | 78 | /* To save memory for 16 bit systems, some arrays are overlaid between |
78 | * the various modules: | 79 | * the various modules: |
@@ -143,7 +144,7 @@ EXTERN(ush, tab_prefix1); /* prefix for odd codes */ | |||
143 | #endif | 144 | #endif |
144 | 145 | ||
145 | extern unsigned insize; /* valid bytes in inbuf */ | 146 | extern unsigned insize; /* valid bytes in inbuf */ |
146 | extern unsigned inptr; /* index of next byte to be processed in inbuf */ | 147 | static unsigned inptr; /* index of next byte to be processed in inbuf */ |
147 | extern unsigned outcnt; /* bytes in output buffer */ | 148 | extern unsigned outcnt; /* bytes in output buffer */ |
148 | 149 | ||
149 | extern long bytes_in; /* number of input bytes */ | 150 | extern long bytes_in; /* number of input bytes */ |
@@ -308,16 +309,16 @@ extern int (*read_buf) (char *buf, unsigned size); | |||
308 | 309 | ||
309 | /* in util.c: */ | 310 | /* in util.c: */ |
310 | extern int copy (int in, int out); | 311 | extern int copy (int in, int out); |
311 | extern ulg updcrc (uch * s, unsigned n); | 312 | //extern ulg updcrc (uch * s, unsigned n); |
312 | extern void clear_bufs (void); | 313 | //extern void clear_bufs (void); |
313 | extern int fill_inbuf (int eof_ok); | 314 | extern int fill_inbuf (int eof_ok); |
314 | extern void flush_outbuf (void); | 315 | extern void flush_outbuf (void); |
315 | extern void flush_window (void); | 316 | extern void flush_window (void); |
316 | extern void write_buf (int fd, void * buf, unsigned cnt); | 317 | //extern void write_buf (int fd, void * buf, unsigned cnt); |
317 | extern char *strlwr (char *s); | 318 | extern char *strlwr (char *s); |
318 | extern char *add_envopt (int *argcp, char ***argvp, char *env); | 319 | extern char *add_envopt (int *argcp, char ***argvp, char *env); |
319 | extern void read_error_msg (void); | 320 | //extern void read_error_msg (void); |
320 | extern void write_error_msg (void); | 321 | //extern void write_error_msg (void); |
321 | extern void display_ratio (long num, long den, FILE * file); | 322 | extern void display_ratio (long num, long den, FILE * file); |
322 | 323 | ||
323 | /* in inflate.c */ | 324 | /* in inflate.c */ |
@@ -704,6 +705,120 @@ extern void _expand_args(int *argc, char ***argv); | |||
704 | #ifndef put_char | 705 | #ifndef put_char |
705 | # define put_char(c) put_byte(c) | 706 | # define put_char(c) put_byte(c) |
706 | #endif | 707 | #endif |
708 | |||
709 | int crc_table_empty = 1; | ||
710 | |||
711 | /* ======================================================================== | ||
712 | * Signal and error handler. | ||
713 | */ | ||
714 | void abort_gzip() | ||
715 | { | ||
716 | exit(ERROR); | ||
717 | } | ||
718 | |||
719 | /* =========================================================================== | ||
720 | * Clear input and output buffers | ||
721 | */ | ||
722 | static void clear_bufs(void) | ||
723 | { | ||
724 | outcnt = 0; | ||
725 | insize = inptr = 0; | ||
726 | bytes_in = bytes_out = 0L; | ||
727 | } | ||
728 | |||
729 | static void write_error_msg() | ||
730 | { | ||
731 | fprintf(stderr, "\n"); | ||
732 | perror(""); | ||
733 | abort_gzip(); | ||
734 | } | ||
735 | |||
736 | /* =========================================================================== | ||
737 | * Does the same as write(), but also handles partial pipe writes and checks | ||
738 | * for error return. | ||
739 | */ | ||
740 | static void write_buf(fd, buf, cnt) | ||
741 | int fd; | ||
742 | void * buf; | ||
743 | unsigned cnt; | ||
744 | { | ||
745 | unsigned n; | ||
746 | |||
747 | while ((n = write(fd, buf, cnt)) != cnt) { | ||
748 | if (n == (unsigned) (-1)) { | ||
749 | write_error_msg(); | ||
750 | } | ||
751 | cnt -= n; | ||
752 | buf = (void *) ((char *) buf + n); | ||
753 | } | ||
754 | } | ||
755 | |||
756 | /* ======================================================================== | ||
757 | * Error handlers. | ||
758 | */ | ||
759 | static void read_error_msg() | ||
760 | { | ||
761 | fprintf(stderr, "\n"); | ||
762 | if (errno != 0) { | ||
763 | perror(""); | ||
764 | } else { | ||
765 | fprintf(stderr, "unexpected end of file\n"); | ||
766 | } | ||
767 | abort_gzip(); | ||
768 | } | ||
769 | |||
770 | /* =========================================================================== | ||
771 | * Run a set of bytes through the crc shift register. If s is a NULL | ||
772 | * pointer, then initialize the crc shift register contents instead. | ||
773 | * Return the current crc in either case. | ||
774 | */ | ||
775 | static ulg updcrc(s, n) | ||
776 | uch *s; /* pointer to bytes to pump through */ | ||
777 | unsigned n; /* number of bytes in s[] */ | ||
778 | { | ||
779 | static ulg crc = (ulg) 0xffffffffL; /* shift register contents */ | ||
780 | register ulg c; /* temporary variable */ | ||
781 | static unsigned long crc_32_tab[256]; | ||
782 | if (crc_table_empty) { | ||
783 | unsigned long csr; /* crc shift register */ | ||
784 | unsigned long e; /* polynomial exclusive-or pattern */ | ||
785 | int i; /* counter for all possible eight bit values */ | ||
786 | int k; /* byte being shifted into crc apparatus */ | ||
787 | |||
788 | /* terms of polynomial defining this crc (except x^32): */ | ||
789 | static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; | ||
790 | |||
791 | /* Make exclusive-or pattern from polynomial (0xedb88320) */ | ||
792 | e = 0; | ||
793 | for (i = 0; i < sizeof(p)/sizeof(int); i++) | ||
794 | e |= 1L << (31 - p[i]); | ||
795 | |||
796 | /* Compute and print table of CRC's, five per line */ | ||
797 | crc_32_tab[0] = 0x00000000L; | ||
798 | for (i = 1; i < 256; i++) { | ||
799 | csr = i; | ||
800 | /* The idea to initialize the register with the byte instead of | ||
801 | * zero was stolen from Haruhiko Okumura's ar002 | ||
802 | */ | ||
803 | for (k = 8; k; k--) | ||
804 | csr = csr & 1 ? (csr >> 1) ^ e : csr >> 1; | ||
805 | crc_32_tab[i]=csr; | ||
806 | } | ||
807 | } | ||
808 | |||
809 | if (s == NULL) { | ||
810 | c = 0xffffffffL; | ||
811 | } else { | ||
812 | c = crc; | ||
813 | if (n) | ||
814 | do { | ||
815 | c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8); | ||
816 | } while (--n); | ||
817 | } | ||
818 | crc = c; | ||
819 | return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */ | ||
820 | } | ||
821 | |||
707 | /* bits.c -- output variable-length bit strings | 822 | /* bits.c -- output variable-length bit strings |
708 | * Copyright (C) 1992-1993 Jean-loup Gailly | 823 | * Copyright (C) 1992-1993 Jean-loup Gailly |
709 | * This is free software; you can redistribute it and/or modify it under the | 824 | * This is free software; you can redistribute it and/or modify it under the |
@@ -42,6 +42,7 @@ | |||
42 | #include <stdio.h> | 42 | #include <stdio.h> |
43 | #include <string.h> | 43 | #include <string.h> |
44 | #include <unistd.h> | 44 | #include <unistd.h> |
45 | #include <errno.h> | ||
45 | #include "busybox.h" | 46 | #include "busybox.h" |
46 | #define BB_DECLARE_EXTERN | 47 | #define BB_DECLARE_EXTERN |
47 | #define bb_need_memory_exhausted | 48 | #define bb_need_memory_exhausted |
@@ -72,7 +73,7 @@ typedef unsigned long ulg; | |||
72 | /* methods 4 to 7 reserved */ | 73 | /* methods 4 to 7 reserved */ |
73 | #define DEFLATED 8 | 74 | #define DEFLATED 8 |
74 | #define MAX_METHODS 9 | 75 | #define MAX_METHODS 9 |
75 | extern int method; /* compression method */ | 76 | static int method; /* compression method */ |
76 | 77 | ||
77 | /* To save memory for 16 bit systems, some arrays are overlaid between | 78 | /* To save memory for 16 bit systems, some arrays are overlaid between |
78 | * the various modules: | 79 | * the various modules: |
@@ -143,7 +144,7 @@ EXTERN(ush, tab_prefix1); /* prefix for odd codes */ | |||
143 | #endif | 144 | #endif |
144 | 145 | ||
145 | extern unsigned insize; /* valid bytes in inbuf */ | 146 | extern unsigned insize; /* valid bytes in inbuf */ |
146 | extern unsigned inptr; /* index of next byte to be processed in inbuf */ | 147 | static unsigned inptr; /* index of next byte to be processed in inbuf */ |
147 | extern unsigned outcnt; /* bytes in output buffer */ | 148 | extern unsigned outcnt; /* bytes in output buffer */ |
148 | 149 | ||
149 | extern long bytes_in; /* number of input bytes */ | 150 | extern long bytes_in; /* number of input bytes */ |
@@ -308,16 +309,16 @@ extern int (*read_buf) (char *buf, unsigned size); | |||
308 | 309 | ||
309 | /* in util.c: */ | 310 | /* in util.c: */ |
310 | extern int copy (int in, int out); | 311 | extern int copy (int in, int out); |
311 | extern ulg updcrc (uch * s, unsigned n); | 312 | //extern ulg updcrc (uch * s, unsigned n); |
312 | extern void clear_bufs (void); | 313 | //extern void clear_bufs (void); |
313 | extern int fill_inbuf (int eof_ok); | 314 | extern int fill_inbuf (int eof_ok); |
314 | extern void flush_outbuf (void); | 315 | extern void flush_outbuf (void); |
315 | extern void flush_window (void); | 316 | extern void flush_window (void); |
316 | extern void write_buf (int fd, void * buf, unsigned cnt); | 317 | //extern void write_buf (int fd, void * buf, unsigned cnt); |
317 | extern char *strlwr (char *s); | 318 | extern char *strlwr (char *s); |
318 | extern char *add_envopt (int *argcp, char ***argvp, char *env); | 319 | extern char *add_envopt (int *argcp, char ***argvp, char *env); |
319 | extern void read_error_msg (void); | 320 | //extern void read_error_msg (void); |
320 | extern void write_error_msg (void); | 321 | //extern void write_error_msg (void); |
321 | extern void display_ratio (long num, long den, FILE * file); | 322 | extern void display_ratio (long num, long den, FILE * file); |
322 | 323 | ||
323 | /* in inflate.c */ | 324 | /* in inflate.c */ |
@@ -704,6 +705,120 @@ extern void _expand_args(int *argc, char ***argv); | |||
704 | #ifndef put_char | 705 | #ifndef put_char |
705 | # define put_char(c) put_byte(c) | 706 | # define put_char(c) put_byte(c) |
706 | #endif | 707 | #endif |
708 | |||
709 | int crc_table_empty = 1; | ||
710 | |||
711 | /* ======================================================================== | ||
712 | * Signal and error handler. | ||
713 | */ | ||
714 | void abort_gzip() | ||
715 | { | ||
716 | exit(ERROR); | ||
717 | } | ||
718 | |||
719 | /* =========================================================================== | ||
720 | * Clear input and output buffers | ||
721 | */ | ||
722 | static void clear_bufs(void) | ||
723 | { | ||
724 | outcnt = 0; | ||
725 | insize = inptr = 0; | ||
726 | bytes_in = bytes_out = 0L; | ||
727 | } | ||
728 | |||
729 | static void write_error_msg() | ||
730 | { | ||
731 | fprintf(stderr, "\n"); | ||
732 | perror(""); | ||
733 | abort_gzip(); | ||
734 | } | ||
735 | |||
736 | /* =========================================================================== | ||
737 | * Does the same as write(), but also handles partial pipe writes and checks | ||
738 | * for error return. | ||
739 | */ | ||
740 | static void write_buf(fd, buf, cnt) | ||
741 | int fd; | ||
742 | void * buf; | ||
743 | unsigned cnt; | ||
744 | { | ||
745 | unsigned n; | ||
746 | |||
747 | while ((n = write(fd, buf, cnt)) != cnt) { | ||
748 | if (n == (unsigned) (-1)) { | ||
749 | write_error_msg(); | ||
750 | } | ||
751 | cnt -= n; | ||
752 | buf = (void *) ((char *) buf + n); | ||
753 | } | ||
754 | } | ||
755 | |||
756 | /* ======================================================================== | ||
757 | * Error handlers. | ||
758 | */ | ||
759 | static void read_error_msg() | ||
760 | { | ||
761 | fprintf(stderr, "\n"); | ||
762 | if (errno != 0) { | ||
763 | perror(""); | ||
764 | } else { | ||
765 | fprintf(stderr, "unexpected end of file\n"); | ||
766 | } | ||
767 | abort_gzip(); | ||
768 | } | ||
769 | |||
770 | /* =========================================================================== | ||
771 | * Run a set of bytes through the crc shift register. If s is a NULL | ||
772 | * pointer, then initialize the crc shift register contents instead. | ||
773 | * Return the current crc in either case. | ||
774 | */ | ||
775 | static ulg updcrc(s, n) | ||
776 | uch *s; /* pointer to bytes to pump through */ | ||
777 | unsigned n; /* number of bytes in s[] */ | ||
778 | { | ||
779 | static ulg crc = (ulg) 0xffffffffL; /* shift register contents */ | ||
780 | register ulg c; /* temporary variable */ | ||
781 | static unsigned long crc_32_tab[256]; | ||
782 | if (crc_table_empty) { | ||
783 | unsigned long csr; /* crc shift register */ | ||
784 | unsigned long e; /* polynomial exclusive-or pattern */ | ||
785 | int i; /* counter for all possible eight bit values */ | ||
786 | int k; /* byte being shifted into crc apparatus */ | ||
787 | |||
788 | /* terms of polynomial defining this crc (except x^32): */ | ||
789 | static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; | ||
790 | |||
791 | /* Make exclusive-or pattern from polynomial (0xedb88320) */ | ||
792 | e = 0; | ||
793 | for (i = 0; i < sizeof(p)/sizeof(int); i++) | ||
794 | e |= 1L << (31 - p[i]); | ||
795 | |||
796 | /* Compute and print table of CRC's, five per line */ | ||
797 | crc_32_tab[0] = 0x00000000L; | ||
798 | for (i = 1; i < 256; i++) { | ||
799 | csr = i; | ||
800 | /* The idea to initialize the register with the byte instead of | ||
801 | * zero was stolen from Haruhiko Okumura's ar002 | ||
802 | */ | ||
803 | for (k = 8; k; k--) | ||
804 | csr = csr & 1 ? (csr >> 1) ^ e : csr >> 1; | ||
805 | crc_32_tab[i]=csr; | ||
806 | } | ||
807 | } | ||
808 | |||
809 | if (s == NULL) { | ||
810 | c = 0xffffffffL; | ||
811 | } else { | ||
812 | c = crc; | ||
813 | if (n) | ||
814 | do { | ||
815 | c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8); | ||
816 | } while (--n); | ||
817 | } | ||
818 | crc = c; | ||
819 | return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */ | ||
820 | } | ||
821 | |||
707 | /* bits.c -- output variable-length bit strings | 822 | /* bits.c -- output variable-length bit strings |
708 | * Copyright (C) 1992-1993 Jean-loup Gailly | 823 | * Copyright (C) 1992-1993 Jean-loup Gailly |
709 | * This is free software; you can redistribute it and/or modify it under the | 824 | * This is free software; you can redistribute it and/or modify it under the |