aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2001-12-05 16:01:02 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2001-12-05 16:01:02 +0000
commit249f39a2650a1f002803b59c4be82ee98fca5652 (patch)
treece7ed2833c09e2d87d3e7a2677a01bf2c6f91639
parentef03dbcd4db03fcab7b6f48a7038226d202775ca (diff)
downloadbusybox-w32-249f39a2650a1f002803b59c4be82ee98fca5652.tar.gz
busybox-w32-249f39a2650a1f002803b59c4be82ee98fca5652.tar.bz2
busybox-w32-249f39a2650a1f002803b59c4be82ee98fca5652.zip
Simplify unzip(), remove unused checks and unneccessary variables
-rw-r--r--archival/libunarchive/decompress_unzip.c78
-rw-r--r--archival/libunarchive/unzip.c78
-rw-r--r--libbb/unzip.c78
3 files changed, 81 insertions, 153 deletions
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c
index c28ca836c..a747baea5 100644
--- a/archival/libunarchive/decompress_unzip.c
+++ b/archival/libunarchive/decompress_unzip.c
@@ -310,12 +310,8 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
310 z = 1 << j; /* table entries for j-bit table */ 310 z = 1 << j; /* table entries for j-bit table */
311 311
312 /* allocate and link in new table */ 312 /* allocate and link in new table */
313 if ((q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t))) == NULL) { 313 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
314 if (h) { 314
315 huft_free(u[0]);
316 }
317 return 3; /* not enough memory */
318 }
319 hufts += z + 1; /* track memory usage */ 315 hufts += z + 1; /* track memory usage */
320 *t = q + 1; /* link to list for huft_free() */ 316 *t = q + 1; /* link to list for huft_free() */
321 *(t = &(q->v.t)) = NULL; 317 *(t = &(q->v.t)) = NULL;
@@ -858,6 +854,9 @@ static int inflate(void)
858 bk = 0; 854 bk = 0;
859 bb = 0; 855 bb = 0;
860 856
857 /* Create the crc table */
858 make_crc_table();
859
861 /* decompress until the last block */ 860 /* decompress until the last block */
862 do { 861 do {
863 hufts = 0; 862 hufts = 0;
@@ -894,16 +893,10 @@ static int inflate(void)
894 */ 893 */
895extern int unzip(FILE *l_in_file, FILE *l_out_file) 894extern int unzip(FILE *l_in_file, FILE *l_out_file)
896{ 895{
897 const int extra_field = 0x04; /* bit 2 set: extra field present */
898 const int orig_name = 0x08; /* bit 3 set: original file name present */
899 const int comment = 0x10; /* bit 4 set: file comment present */
900 unsigned char buf[8]; /* extended local header */ 896 unsigned char buf[8]; /* extended local header */
901 unsigned char flags; /* compression flags */ 897 unsigned char flags; /* compression flags */
902 char magic[2]; /* magic header */
903 int method;
904 typedef void (*sig_type) (int); 898 typedef void (*sig_type) (int);
905 int exit_code=0; /* program exit code */ 899 unsigned short i;
906 int i;
907 900
908 in_file = l_in_file; 901 in_file = l_in_file;
909 out_file = l_out_file; 902 out_file = l_out_file;
@@ -927,67 +920,49 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
927 outcnt = 0; 920 outcnt = 0;
928 bytes_out = 0L; 921 bytes_out = 0L;
929 922
930 magic[0] = fgetc(in_file);
931 magic[1] = fgetc(in_file);
932
933 /* Magic header for gzip files, 1F 8B = \037\213 */ 923 /* Magic header for gzip files, 1F 8B = \037\213 */
934 if (memcmp(magic, "\037\213", 2) != 0) { 924 if ((fgetc(in_file) != 0x1F) || (fgetc(in_file) != 0x8b)) {
935 error_msg("Invalid gzip magic"); 925 error_msg("Invalid gzip magic");
936 return EXIT_FAILURE; 926 return EXIT_FAILURE;
937 } 927 }
938 928
939 method = (int) fgetc(in_file); 929 /* Check the compression method */
940 if (method != 8) { 930 if (fgetc(in_file) != 8) {
941 error_msg("unknown method %d -- get newer version of gzip", method); 931 error_msg("Unknown compression method");
942 exit_code = 1; 932 return(-1);
943 return -1;
944 } 933 }
945 934
946 flags = (unsigned char) fgetc(in_file); 935 flags = (unsigned char) fgetc(in_file);
947 936
948 /* Ignore time stamp(4), extra flags(1), OS type(1) */ 937 /* Ignore time stamp(4), extra flags(1), OS type(1) */
949 for (i = 0; i < 6; i++) 938 for (i = 0; i < 6; i++) {
950 fgetc(in_file); 939 fgetc(in_file);
940 }
951 941
952 if ((flags & extra_field) != 0) { 942 if (flags & 0x04) {
953 size_t extra; 943 /* bit 2 set: extra field present */
954 extra = fgetc(in_file); 944 const unsigned short extra = fgetc(in_file) + (fgetc(in_file) << 8);
955 extra += fgetc(in_file) << 8;
956 945
957 for (i = 0; i < extra; i++) 946 for (i = 0; i < extra; i++) {
958 fgetc(in_file); 947 fgetc(in_file);
948 }
959 } 949 }
960 950
961 /* Discard original name if any */ 951 /* Discard original name if any */
962 if ((flags & orig_name) != 0) { 952 if (flags & 0x08) {
953 /* bit 3 set: original file name present */
963 while (fgetc(in_file) != 0); /* null */ 954 while (fgetc(in_file) != 0); /* null */
964 } 955 }
965 956
966 /* Discard file comment if any */ 957 /* Discard file comment if any */
967 if ((flags & comment) != 0) { 958 if (flags & 0x10) {
959 /* bit 4 set: file comment present */
968 while (fgetc(in_file) != 0); /* null */ 960 while (fgetc(in_file) != 0); /* null */
969 } 961 }
970 962
971 if (method < 0) {
972 printf("it failed\n");
973 return(exit_code); /* error message already emitted */
974 }
975
976 make_crc_table();
977
978 /* Decompress */ 963 /* Decompress */
979 if (method == 8) { 964 if (inflate() != 0) {
980 965 error_msg("invalid compressed data--format violated");
981 int res = inflate();
982
983 if (res == 3) {
984 error_msg(memory_exhausted);
985 } else if (res != 0) {
986 error_msg("invalid compressed data--format violated");
987 }
988
989 } else {
990 error_msg("internal error, invalid method");
991 } 966 }
992 967
993 /* Get the crc and original length 968 /* Get the crc and original length
@@ -1023,6 +998,7 @@ extern void gz_close(int gunzip_pid)
1023 if (waitpid(gunzip_pid, NULL, 0) == -1) { 998 if (waitpid(gunzip_pid, NULL, 0) == -1) {
1024 printf("Couldnt wait ?"); 999 printf("Couldnt wait ?");
1025 } 1000 }
1026 free(window); 1001
1027 free(crc_table); 1002 free(window);
1003 free(crc_table);
1028} 1004}
diff --git a/archival/libunarchive/unzip.c b/archival/libunarchive/unzip.c
index c28ca836c..a747baea5 100644
--- a/archival/libunarchive/unzip.c
+++ b/archival/libunarchive/unzip.c
@@ -310,12 +310,8 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
310 z = 1 << j; /* table entries for j-bit table */ 310 z = 1 << j; /* table entries for j-bit table */
311 311
312 /* allocate and link in new table */ 312 /* allocate and link in new table */
313 if ((q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t))) == NULL) { 313 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
314 if (h) { 314
315 huft_free(u[0]);
316 }
317 return 3; /* not enough memory */
318 }
319 hufts += z + 1; /* track memory usage */ 315 hufts += z + 1; /* track memory usage */
320 *t = q + 1; /* link to list for huft_free() */ 316 *t = q + 1; /* link to list for huft_free() */
321 *(t = &(q->v.t)) = NULL; 317 *(t = &(q->v.t)) = NULL;
@@ -858,6 +854,9 @@ static int inflate(void)
858 bk = 0; 854 bk = 0;
859 bb = 0; 855 bb = 0;
860 856
857 /* Create the crc table */
858 make_crc_table();
859
861 /* decompress until the last block */ 860 /* decompress until the last block */
862 do { 861 do {
863 hufts = 0; 862 hufts = 0;
@@ -894,16 +893,10 @@ static int inflate(void)
894 */ 893 */
895extern int unzip(FILE *l_in_file, FILE *l_out_file) 894extern int unzip(FILE *l_in_file, FILE *l_out_file)
896{ 895{
897 const int extra_field = 0x04; /* bit 2 set: extra field present */
898 const int orig_name = 0x08; /* bit 3 set: original file name present */
899 const int comment = 0x10; /* bit 4 set: file comment present */
900 unsigned char buf[8]; /* extended local header */ 896 unsigned char buf[8]; /* extended local header */
901 unsigned char flags; /* compression flags */ 897 unsigned char flags; /* compression flags */
902 char magic[2]; /* magic header */
903 int method;
904 typedef void (*sig_type) (int); 898 typedef void (*sig_type) (int);
905 int exit_code=0; /* program exit code */ 899 unsigned short i;
906 int i;
907 900
908 in_file = l_in_file; 901 in_file = l_in_file;
909 out_file = l_out_file; 902 out_file = l_out_file;
@@ -927,67 +920,49 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
927 outcnt = 0; 920 outcnt = 0;
928 bytes_out = 0L; 921 bytes_out = 0L;
929 922
930 magic[0] = fgetc(in_file);
931 magic[1] = fgetc(in_file);
932
933 /* Magic header for gzip files, 1F 8B = \037\213 */ 923 /* Magic header for gzip files, 1F 8B = \037\213 */
934 if (memcmp(magic, "\037\213", 2) != 0) { 924 if ((fgetc(in_file) != 0x1F) || (fgetc(in_file) != 0x8b)) {
935 error_msg("Invalid gzip magic"); 925 error_msg("Invalid gzip magic");
936 return EXIT_FAILURE; 926 return EXIT_FAILURE;
937 } 927 }
938 928
939 method = (int) fgetc(in_file); 929 /* Check the compression method */
940 if (method != 8) { 930 if (fgetc(in_file) != 8) {
941 error_msg("unknown method %d -- get newer version of gzip", method); 931 error_msg("Unknown compression method");
942 exit_code = 1; 932 return(-1);
943 return -1;
944 } 933 }
945 934
946 flags = (unsigned char) fgetc(in_file); 935 flags = (unsigned char) fgetc(in_file);
947 936
948 /* Ignore time stamp(4), extra flags(1), OS type(1) */ 937 /* Ignore time stamp(4), extra flags(1), OS type(1) */
949 for (i = 0; i < 6; i++) 938 for (i = 0; i < 6; i++) {
950 fgetc(in_file); 939 fgetc(in_file);
940 }
951 941
952 if ((flags & extra_field) != 0) { 942 if (flags & 0x04) {
953 size_t extra; 943 /* bit 2 set: extra field present */
954 extra = fgetc(in_file); 944 const unsigned short extra = fgetc(in_file) + (fgetc(in_file) << 8);
955 extra += fgetc(in_file) << 8;
956 945
957 for (i = 0; i < extra; i++) 946 for (i = 0; i < extra; i++) {
958 fgetc(in_file); 947 fgetc(in_file);
948 }
959 } 949 }
960 950
961 /* Discard original name if any */ 951 /* Discard original name if any */
962 if ((flags & orig_name) != 0) { 952 if (flags & 0x08) {
953 /* bit 3 set: original file name present */
963 while (fgetc(in_file) != 0); /* null */ 954 while (fgetc(in_file) != 0); /* null */
964 } 955 }
965 956
966 /* Discard file comment if any */ 957 /* Discard file comment if any */
967 if ((flags & comment) != 0) { 958 if (flags & 0x10) {
959 /* bit 4 set: file comment present */
968 while (fgetc(in_file) != 0); /* null */ 960 while (fgetc(in_file) != 0); /* null */
969 } 961 }
970 962
971 if (method < 0) {
972 printf("it failed\n");
973 return(exit_code); /* error message already emitted */
974 }
975
976 make_crc_table();
977
978 /* Decompress */ 963 /* Decompress */
979 if (method == 8) { 964 if (inflate() != 0) {
980 965 error_msg("invalid compressed data--format violated");
981 int res = inflate();
982
983 if (res == 3) {
984 error_msg(memory_exhausted);
985 } else if (res != 0) {
986 error_msg("invalid compressed data--format violated");
987 }
988
989 } else {
990 error_msg("internal error, invalid method");
991 } 966 }
992 967
993 /* Get the crc and original length 968 /* Get the crc and original length
@@ -1023,6 +998,7 @@ extern void gz_close(int gunzip_pid)
1023 if (waitpid(gunzip_pid, NULL, 0) == -1) { 998 if (waitpid(gunzip_pid, NULL, 0) == -1) {
1024 printf("Couldnt wait ?"); 999 printf("Couldnt wait ?");
1025 } 1000 }
1026 free(window); 1001
1027 free(crc_table); 1002 free(window);
1003 free(crc_table);
1028} 1004}
diff --git a/libbb/unzip.c b/libbb/unzip.c
index c28ca836c..a747baea5 100644
--- a/libbb/unzip.c
+++ b/libbb/unzip.c
@@ -310,12 +310,8 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
310 z = 1 << j; /* table entries for j-bit table */ 310 z = 1 << j; /* table entries for j-bit table */
311 311
312 /* allocate and link in new table */ 312 /* allocate and link in new table */
313 if ((q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t))) == NULL) { 313 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
314 if (h) { 314
315 huft_free(u[0]);
316 }
317 return 3; /* not enough memory */
318 }
319 hufts += z + 1; /* track memory usage */ 315 hufts += z + 1; /* track memory usage */
320 *t = q + 1; /* link to list for huft_free() */ 316 *t = q + 1; /* link to list for huft_free() */
321 *(t = &(q->v.t)) = NULL; 317 *(t = &(q->v.t)) = NULL;
@@ -858,6 +854,9 @@ static int inflate(void)
858 bk = 0; 854 bk = 0;
859 bb = 0; 855 bb = 0;
860 856
857 /* Create the crc table */
858 make_crc_table();
859
861 /* decompress until the last block */ 860 /* decompress until the last block */
862 do { 861 do {
863 hufts = 0; 862 hufts = 0;
@@ -894,16 +893,10 @@ static int inflate(void)
894 */ 893 */
895extern int unzip(FILE *l_in_file, FILE *l_out_file) 894extern int unzip(FILE *l_in_file, FILE *l_out_file)
896{ 895{
897 const int extra_field = 0x04; /* bit 2 set: extra field present */
898 const int orig_name = 0x08; /* bit 3 set: original file name present */
899 const int comment = 0x10; /* bit 4 set: file comment present */
900 unsigned char buf[8]; /* extended local header */ 896 unsigned char buf[8]; /* extended local header */
901 unsigned char flags; /* compression flags */ 897 unsigned char flags; /* compression flags */
902 char magic[2]; /* magic header */
903 int method;
904 typedef void (*sig_type) (int); 898 typedef void (*sig_type) (int);
905 int exit_code=0; /* program exit code */ 899 unsigned short i;
906 int i;
907 900
908 in_file = l_in_file; 901 in_file = l_in_file;
909 out_file = l_out_file; 902 out_file = l_out_file;
@@ -927,67 +920,49 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
927 outcnt = 0; 920 outcnt = 0;
928 bytes_out = 0L; 921 bytes_out = 0L;
929 922
930 magic[0] = fgetc(in_file);
931 magic[1] = fgetc(in_file);
932
933 /* Magic header for gzip files, 1F 8B = \037\213 */ 923 /* Magic header for gzip files, 1F 8B = \037\213 */
934 if (memcmp(magic, "\037\213", 2) != 0) { 924 if ((fgetc(in_file) != 0x1F) || (fgetc(in_file) != 0x8b)) {
935 error_msg("Invalid gzip magic"); 925 error_msg("Invalid gzip magic");
936 return EXIT_FAILURE; 926 return EXIT_FAILURE;
937 } 927 }
938 928
939 method = (int) fgetc(in_file); 929 /* Check the compression method */
940 if (method != 8) { 930 if (fgetc(in_file) != 8) {
941 error_msg("unknown method %d -- get newer version of gzip", method); 931 error_msg("Unknown compression method");
942 exit_code = 1; 932 return(-1);
943 return -1;
944 } 933 }
945 934
946 flags = (unsigned char) fgetc(in_file); 935 flags = (unsigned char) fgetc(in_file);
947 936
948 /* Ignore time stamp(4), extra flags(1), OS type(1) */ 937 /* Ignore time stamp(4), extra flags(1), OS type(1) */
949 for (i = 0; i < 6; i++) 938 for (i = 0; i < 6; i++) {
950 fgetc(in_file); 939 fgetc(in_file);
940 }
951 941
952 if ((flags & extra_field) != 0) { 942 if (flags & 0x04) {
953 size_t extra; 943 /* bit 2 set: extra field present */
954 extra = fgetc(in_file); 944 const unsigned short extra = fgetc(in_file) + (fgetc(in_file) << 8);
955 extra += fgetc(in_file) << 8;
956 945
957 for (i = 0; i < extra; i++) 946 for (i = 0; i < extra; i++) {
958 fgetc(in_file); 947 fgetc(in_file);
948 }
959 } 949 }
960 950
961 /* Discard original name if any */ 951 /* Discard original name if any */
962 if ((flags & orig_name) != 0) { 952 if (flags & 0x08) {
953 /* bit 3 set: original file name present */
963 while (fgetc(in_file) != 0); /* null */ 954 while (fgetc(in_file) != 0); /* null */
964 } 955 }
965 956
966 /* Discard file comment if any */ 957 /* Discard file comment if any */
967 if ((flags & comment) != 0) { 958 if (flags & 0x10) {
959 /* bit 4 set: file comment present */
968 while (fgetc(in_file) != 0); /* null */ 960 while (fgetc(in_file) != 0); /* null */
969 } 961 }
970 962
971 if (method < 0) {
972 printf("it failed\n");
973 return(exit_code); /* error message already emitted */
974 }
975
976 make_crc_table();
977
978 /* Decompress */ 963 /* Decompress */
979 if (method == 8) { 964 if (inflate() != 0) {
980 965 error_msg("invalid compressed data--format violated");
981 int res = inflate();
982
983 if (res == 3) {
984 error_msg(memory_exhausted);
985 } else if (res != 0) {
986 error_msg("invalid compressed data--format violated");
987 }
988
989 } else {
990 error_msg("internal error, invalid method");
991 } 966 }
992 967
993 /* Get the crc and original length 968 /* Get the crc and original length
@@ -1023,6 +998,7 @@ extern void gz_close(int gunzip_pid)
1023 if (waitpid(gunzip_pid, NULL, 0) == -1) { 998 if (waitpid(gunzip_pid, NULL, 0) == -1) {
1024 printf("Couldnt wait ?"); 999 printf("Couldnt wait ?");
1025 } 1000 }
1026 free(window); 1001
1027 free(crc_table); 1002 free(window);
1003 free(crc_table);
1028} 1004}