aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2001-03-28 05:35:16 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2001-03-28 05:35:16 +0000
commitf58efb57d1c7134cfeaf73e84f210c2458826b80 (patch)
tree9f4a919574128ae05a430381137d9bb22f3c7f48
parenta2e68fc23319d77789cae6eca899f328d15368a5 (diff)
downloadbusybox-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.c129
-rw-r--r--gzip.c129
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
75extern int method; /* compression method */ 76static 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
145extern unsigned insize; /* valid bytes in inbuf */ 146extern unsigned insize; /* valid bytes in inbuf */
146extern unsigned inptr; /* index of next byte to be processed in inbuf */ 147static unsigned inptr; /* index of next byte to be processed in inbuf */
147extern unsigned outcnt; /* bytes in output buffer */ 148extern unsigned outcnt; /* bytes in output buffer */
148 149
149extern long bytes_in; /* number of input bytes */ 150extern 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: */
310extern int copy (int in, int out); 311extern int copy (int in, int out);
311extern ulg updcrc (uch * s, unsigned n); 312//extern ulg updcrc (uch * s, unsigned n);
312extern void clear_bufs (void); 313//extern void clear_bufs (void);
313extern int fill_inbuf (int eof_ok); 314extern int fill_inbuf (int eof_ok);
314extern void flush_outbuf (void); 315extern void flush_outbuf (void);
315extern void flush_window (void); 316extern void flush_window (void);
316extern void write_buf (int fd, void * buf, unsigned cnt); 317//extern void write_buf (int fd, void * buf, unsigned cnt);
317extern char *strlwr (char *s); 318extern char *strlwr (char *s);
318extern char *add_envopt (int *argcp, char ***argvp, char *env); 319extern char *add_envopt (int *argcp, char ***argvp, char *env);
319extern void read_error_msg (void); 320//extern void read_error_msg (void);
320extern void write_error_msg (void); 321//extern void write_error_msg (void);
321extern void display_ratio (long num, long den, FILE * file); 322extern 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
709int crc_table_empty = 1;
710
711/* ========================================================================
712 * Signal and error handler.
713 */
714void abort_gzip()
715{
716 exit(ERROR);
717}
718
719/* ===========================================================================
720 * Clear input and output buffers
721 */
722static void clear_bufs(void)
723{
724 outcnt = 0;
725 insize = inptr = 0;
726 bytes_in = bytes_out = 0L;
727}
728
729static 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 */
740static void write_buf(fd, buf, cnt)
741int fd;
742void * buf;
743unsigned 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 */
759static 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 */
775static ulg updcrc(s, n)
776uch *s; /* pointer to bytes to pump through */
777unsigned 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
diff --git a/gzip.c b/gzip.c
index 6369b894a..2a7604648 100644
--- a/gzip.c
+++ b/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
75extern int method; /* compression method */ 76static 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
145extern unsigned insize; /* valid bytes in inbuf */ 146extern unsigned insize; /* valid bytes in inbuf */
146extern unsigned inptr; /* index of next byte to be processed in inbuf */ 147static unsigned inptr; /* index of next byte to be processed in inbuf */
147extern unsigned outcnt; /* bytes in output buffer */ 148extern unsigned outcnt; /* bytes in output buffer */
148 149
149extern long bytes_in; /* number of input bytes */ 150extern 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: */
310extern int copy (int in, int out); 311extern int copy (int in, int out);
311extern ulg updcrc (uch * s, unsigned n); 312//extern ulg updcrc (uch * s, unsigned n);
312extern void clear_bufs (void); 313//extern void clear_bufs (void);
313extern int fill_inbuf (int eof_ok); 314extern int fill_inbuf (int eof_ok);
314extern void flush_outbuf (void); 315extern void flush_outbuf (void);
315extern void flush_window (void); 316extern void flush_window (void);
316extern void write_buf (int fd, void * buf, unsigned cnt); 317//extern void write_buf (int fd, void * buf, unsigned cnt);
317extern char *strlwr (char *s); 318extern char *strlwr (char *s);
318extern char *add_envopt (int *argcp, char ***argvp, char *env); 319extern char *add_envopt (int *argcp, char ***argvp, char *env);
319extern void read_error_msg (void); 320//extern void read_error_msg (void);
320extern void write_error_msg (void); 321//extern void write_error_msg (void);
321extern void display_ratio (long num, long den, FILE * file); 322extern 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
709int crc_table_empty = 1;
710
711/* ========================================================================
712 * Signal and error handler.
713 */
714void abort_gzip()
715{
716 exit(ERROR);
717}
718
719/* ===========================================================================
720 * Clear input and output buffers
721 */
722static void clear_bufs(void)
723{
724 outcnt = 0;
725 insize = inptr = 0;
726 bytes_in = bytes_out = 0L;
727}
728
729static 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 */
740static void write_buf(fd, buf, cnt)
741int fd;
742void * buf;
743unsigned 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 */
759static 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 */
775static ulg updcrc(s, n)
776uch *s; /* pointer to bytes to pump through */
777unsigned 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