aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-01-02 13:52:26 +0000
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-01-02 13:52:26 +0000
commit207c7487a7afc394d119d2ff13f7586bc126246c (patch)
tree4f406c877f88a013c4e1dc650ac312640a95cb00 /libbb
parentd3d7a5045c61945e9772f3937f6b03b18d9560cd (diff)
downloadbusybox-w32-207c7487a7afc394d119d2ff13f7586bc126246c.tar.gz
busybox-w32-207c7487a7afc394d119d2ff13f7586bc126246c.tar.bz2
busybox-w32-207c7487a7afc394d119d2ff13f7586bc126246c.zip
unzip applet by Laurence Anderson
---------------------------------------------------------------------- git-svn-id: svn://busybox.net/trunk/busybox@3988 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r--libbb/unzip.c48
1 files changed, 23 insertions, 25 deletions
diff --git a/libbb/unzip.c b/libbb/unzip.c
index 6c28d181d..8075fd717 100644
--- a/libbb/unzip.c
+++ b/libbb/unzip.c
@@ -80,7 +80,7 @@ static const int ERROR = 1;
80 80
81/* 81/*
82 * window size--must be a power of two, and 82 * window size--must be a power of two, and
83 * at least 32K for zip's deflate method 83 * at least 32K for zip's deflate method
84 */ 84 */
85static const int WSIZE = 0x8000; 85static const int WSIZE = 0x8000;
86 86
@@ -846,7 +846,7 @@ static int inflate_block(int *e)
846 * 846 *
847 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr 847 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr
848 */ 848 */
849static int inflate(void) 849extern int inflate(FILE *in, FILE *out)
850{ 850{
851 int e; /* last block flag */ 851 int e; /* last block flag */
852 int r; /* result code */ 852 int r; /* result code */
@@ -857,6 +857,13 @@ static int inflate(void)
857 bk = 0; 857 bk = 0;
858 bb = 0; 858 bb = 0;
859 859
860 in_file = in;
861 out_file = out;
862
863 /* Allocate all global buffers (for DYN_ALLOC option) */
864 window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char)));
865 bytes_out = 0L;
866
860 /* Create the crc table */ 867 /* Create the crc table */
861 make_crc_table(); 868 make_crc_table();
862 869
@@ -881,13 +888,15 @@ static int inflate(void)
881 888
882 /* flush out window */ 889 /* flush out window */
883 flush_window(); 890 flush_window();
891 free(window);
892 free(crc_table);
884 893
885 /* return success */ 894 /* return success */
886 return 0; 895 return 0;
887} 896}
888 897
889/* =========================================================================== 898/* ===========================================================================
890 * Unzip in to out. This routine works on both gzip and pkzip files. 899 * Unzip in to out. This routine works on gzip files only.
891 * 900 *
892 * IN assertions: the buffer inbuf contains already the beginning of 901 * IN assertions: the buffer inbuf contains already the beginning of
893 * the compressed data, from offsets inptr to insize-1 included. 902 * the compressed data, from offsets inptr to insize-1 included.
@@ -901,9 +910,6 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
901 typedef void (*sig_type) (int); 910 typedef void (*sig_type) (int);
902 unsigned short i; 911 unsigned short i;
903 912
904 in_file = l_in_file;
905 out_file = l_out_file;
906
907 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 913 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
908 (void) signal(SIGINT, (sig_type) abort_gzip); 914 (void) signal(SIGINT, (sig_type) abort_gzip);
909 } 915 }
@@ -918,53 +924,48 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
918 } 924 }
919#endif 925#endif
920 926
921 /* Allocate all global buffers (for DYN_ALLOC option) */
922 window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char)));
923 outcnt = 0;
924 bytes_out = 0L;
925
926 /* Magic header for gzip files, 1F 8B = \037\213 */ 927 /* Magic header for gzip files, 1F 8B = \037\213 */
927 if ((fgetc(in_file) != 0x1F) || (fgetc(in_file) != 0x8b)) { 928 if ((fgetc(l_in_file) != 0x1F) || (fgetc(l_in_file) != 0x8b)) {
928 error_msg("Invalid gzip magic"); 929 error_msg("Invalid gzip magic");
929 return EXIT_FAILURE; 930 return EXIT_FAILURE;
930 } 931 }
931 932
932 /* Check the compression method */ 933 /* Check the compression method */
933 if (fgetc(in_file) != 8) { 934 if (fgetc(l_in_file) != 8) {
934 error_msg("Unknown compression method"); 935 error_msg("Unknown compression method");
935 return(-1); 936 return(-1);
936 } 937 }
937 938
938 flags = (unsigned char) fgetc(in_file); 939 flags = (unsigned char) fgetc(l_in_file);
939 940
940 /* Ignore time stamp(4), extra flags(1), OS type(1) */ 941 /* Ignore time stamp(4), extra flags(1), OS type(1) */
941 for (i = 0; i < 6; i++) { 942 for (i = 0; i < 6; i++) {
942 fgetc(in_file); 943 fgetc(l_in_file);
943 } 944 }
944 945
945 if (flags & 0x04) { 946 if (flags & 0x04) {
946 /* bit 2 set: extra field present */ 947 /* bit 2 set: extra field present */
947 const unsigned short extra = fgetc(in_file) + (fgetc(in_file) << 8); 948 const unsigned short extra = fgetc(l_in_file) + (fgetc(l_in_file) << 8);
948 949
949 for (i = 0; i < extra; i++) { 950 for (i = 0; i < extra; i++) {
950 fgetc(in_file); 951 fgetc(l_in_file);
951 } 952 }
952 } 953 }
953 954
954 /* Discard original name if any */ 955 /* Discard original name if any */
955 if (flags & 0x08) { 956 if (flags & 0x08) {
956 /* bit 3 set: original file name present */ 957 /* bit 3 set: original file name present */
957 while (fgetc(in_file) != 0); /* null */ 958 while (fgetc(l_in_file) != 0); /* null */
958 } 959 }
959 960
960 /* Discard file comment if any */ 961 /* Discard file comment if any */
961 if (flags & 0x10) { 962 if (flags & 0x10) {
962 /* bit 4 set: file comment present */ 963 /* bit 4 set: file comment present */
963 while (fgetc(in_file) != 0); /* null */ 964 while (fgetc(l_in_file) != 0); /* null */
964 } 965 }
965 966
966 /* Decompress */ 967 /* Decompress */
967 if (inflate() != 0) { 968 if (inflate(l_in_file, l_out_file) != 0) {
968 error_msg("invalid compressed data--format violated"); 969 error_msg("invalid compressed data--format violated");
969 } 970 }
970 971
@@ -972,7 +973,7 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
972 * crc32 (see algorithm.doc) 973 * crc32 (see algorithm.doc)
973 * uncompressed input size modulo 2^32 974 * uncompressed input size modulo 2^32
974 */ 975 */
975 fread(buf, 1, 8, in_file); 976 fread(buf, 1, 8, l_in_file);
976 977
977 /* Validate decompression - crc */ 978 /* Validate decompression - crc */
978 if ((unsigned int)((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) { 979 if ((unsigned int)((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) {
@@ -983,14 +984,11 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
983 error_msg("invalid compressed data--length error"); 984 error_msg("invalid compressed data--length error");
984 } 985 }
985 986
986 free(window);
987 free(crc_table);
988
989 return 0; 987 return 0;
990} 988}
991 989
992/* 990/*
993 * This needs access to global variables wondow and crc_table, so its not in its own file. 991 * This needs access to global variables window and crc_table, so its not in its own file.
994 */ 992 */
995extern void gz_close(int gunzip_pid) 993extern void gz_close(int gunzip_pid)
996{ 994{