aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-01-05 23:56:53 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-01-05 23:56:53 +0000
commit57053ee5e498b7afe91cb67f6731ad0c51a7c7a6 (patch)
tree10731cda21479451f3ca050c9f030b682a1d99aa
parenta4639334d8e29e50174f6f2d329878c443d322d1 (diff)
downloadbusybox-w32-57053ee5e498b7afe91cb67f6731ad0c51a7c7a6.tar.gz
busybox-w32-57053ee5e498b7afe91cb67f6731ad0c51a7c7a6.tar.bz2
busybox-w32-57053ee5e498b7afe91cb67f6731ad0c51a7c7a6.zip
do not expose internal state of [g]zip unpacker.
fix memory leak in inflate_gunzip. git-svn-id: svn://busybox.net/trunk/busybox@17167 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--archival/libunarchive/decompress_unzip.c73
-rw-r--r--archival/unzip.c10
-rw-r--r--include/unarchive.h15
3 files changed, 51 insertions, 47 deletions
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c
index 7001c70f9..af74e6598 100644
--- a/archival/libunarchive/decompress_unzip.c
+++ b/archival/libunarchive/decompress_unzip.c
@@ -45,9 +45,8 @@ typedef struct huft_s {
45 } v; 45 } v;
46} huft_t; 46} huft_t;
47 47
48/* Globally-visible data */ 48static off_t gunzip_bytes_out; /* number of output bytes */
49off_t gunzip_bytes_out; /* number of output bytes */ 49static uint32_t gunzip_crc;
50uint32_t gunzip_crc;
51 50
52static int gunzip_src_fd; 51static int gunzip_src_fd;
53static unsigned gunzip_outbuf_count; /* bytes in output buffer */ 52static unsigned gunzip_outbuf_count; /* bytes in output buffer */
@@ -165,8 +164,7 @@ static int huft_free(huft_t * t)
165 * t: result: starting table 164 * t: result: starting table
166 * m: maximum lookup bits, returns actual 165 * m: maximum lookup bits, returns actual
167 */ 166 */
168static 167static int huft_build(unsigned *b, const unsigned n,
169int huft_build(unsigned *b, const unsigned n,
170 const unsigned s, const unsigned short *d, 168 const unsigned s, const unsigned short *d,
171 const unsigned char *e, huft_t ** t, unsigned *m) 169 const unsigned char *e, huft_t ** t, unsigned *m)
172{ 170{
@@ -408,7 +406,6 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned my_bl, c
408 return 1; // We have a block to read 406 return 1; // We have a block to read
409 } 407 }
410 } else { /* it's an EOB or a length */ 408 } else { /* it's an EOB or a length */
411
412 /* exit if end of block */ 409 /* exit if end of block */
413 if (e == 15) { 410 if (e == 15) {
414 break; 411 break;
@@ -595,11 +592,11 @@ static int inflate_block(int *e)
595 inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored 592 inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored
596 return -1; 593 return -1;
597 } 594 }
598 case 1: /* Inflate fixed 595 case 1:
599 * decompress an inflated type 1 (fixed Huffman codes) block. We should 596 /* Inflate fixed
600 * either replace this with a custom decoder, or at least precompute the 597 * decompress an inflated type 1 (fixed Huffman codes) block. We should
601 * Huffman tables. 598 * either replace this with a custom decoder, or at least precompute the
602 */ 599 * Huffman tables. */
603 { 600 {
604 int i; /* temporary variable */ 601 int i; /* temporary variable */
605 huft_t *tl; /* literal/length code table */ 602 huft_t *tl; /* literal/length code table */
@@ -854,25 +851,10 @@ static int inflate_get_next_window(void)
854 /* Doesnt get here */ 851 /* Doesnt get here */
855} 852}
856 853
857/* Initialize bytebuffer, be careful not to overfill the buffer */
858/* Called from archival/unzip.c */
859void inflate_init(unsigned bufsize)
860{
861 /* Set the bytebuffer size, default is same as gunzip_wsize */
862 bytebuffer_max = bufsize + 8;
863 bytebuffer_offset = 4;
864 bytebuffer_size = 0;
865}
866
867/* Called from archival/unzip.c */
868void inflate_cleanup(void)
869{
870 free(bytebuffer);
871}
872 854
873/* Called from inflate_gunzip() and archival/unzip.c */ 855/* Called from inflate_gunzip() and archival/unzip.c */
874USE_DESKTOP(long long) int 856static USE_DESKTOP(long long) int
875inflate_unzip(int in, int out) 857inflate_unzip_internal(int in, int out)
876{ 858{
877 USE_DESKTOP(long long total = 0;) 859 USE_DESKTOP(long long total = 0;)
878 ssize_t nwrote; 860 ssize_t nwrote;
@@ -922,14 +904,35 @@ inflate_unzip(int in, int out)
922 return USE_DESKTOP(total) + 0; 904 return USE_DESKTOP(total) + 0;
923} 905}
924 906
907
908USE_DESKTOP(long long) int
909inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out)
910{
911 USE_DESKTOP(long long) int n;
912
913 bytebuffer_max = bufsize + 8;
914 bytebuffer_offset = 4;
915 bytebuffer_size = 0;
916
917 n = inflate_unzip_internal(in, out);
918
919 res->crc = gunzip_crc;
920 res->bytes_out = gunzip_bytes_out;
921 free(bytebuffer);
922 return n;
923}
924
925
925USE_DESKTOP(long long) int 926USE_DESKTOP(long long) int
926inflate_gunzip(int in, int out) 927inflate_gunzip(int in, int out)
927{ 928{
928 uint32_t stored_crc = 0; 929 uint32_t stored_crc = 0;
929 unsigned count; 930 unsigned count;
930 USE_DESKTOP(long long total = )inflate_unzip(in, out); 931 USE_DESKTOP(long long) int n;
932
933 n = inflate_unzip_internal(in, out);
931 934
932 USE_DESKTOP(if (total < 0) return total;) 935 if (n < 0) goto ret;
933 936
934 /* top up the input buffer with the rest of the trailer */ 937 /* top up the input buffer with the rest of the trailer */
935 count = bytebuffer_size - bytebuffer_offset; 938 count = bytebuffer_size - bytebuffer_offset;
@@ -946,7 +949,8 @@ inflate_gunzip(int in, int out)
946 /* Validate decompression - crc */ 949 /* Validate decompression - crc */
947 if (stored_crc != (~gunzip_crc)) { 950 if (stored_crc != (~gunzip_crc)) {
948 bb_error_msg("crc error"); 951 bb_error_msg("crc error");
949 return -1; 952 n = -1;
953 goto ret;
950 } 954 }
951 955
952 /* Validate decompression - size */ 956 /* Validate decompression - size */
@@ -955,8 +959,9 @@ inflate_gunzip(int in, int out)
955 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24)) 959 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))
956 ) { 960 ) {
957 bb_error_msg("incorrect length"); 961 bb_error_msg("incorrect length");
958 return -1; 962 n = -1;
959 } 963 }
960 964 ret:
961 return USE_DESKTOP(total) + 0; 965 free(bytebuffer);
966 return n;
962} 967}
diff --git a/archival/unzip.c b/archival/unzip.c
index 34a3a8519..b10132ebd 100644
--- a/archival/unzip.c
+++ b/archival/unzip.c
@@ -76,16 +76,16 @@ static int unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
76 bb_copyfd_exact_size(src_fd, dst_fd, size); 76 bb_copyfd_exact_size(src_fd, dst_fd, size);
77 } else { 77 } else {
78 /* Method 8 - inflate */ 78 /* Method 8 - inflate */
79 inflate_init(zip_header->formatted.cmpsize); 79 inflate_unzip_result res;
80 inflate_unzip(src_fd, dst_fd); 80 /* err = */ inflate_unzip(&res, zip_header->formatted.cmpsize, src_fd, dst_fd);
81 inflate_cleanup(); 81// we should check for -1 error return
82 /* Validate decompression - crc */ 82 /* Validate decompression - crc */
83 if (zip_header->formatted.crc32 != (gunzip_crc ^ 0xffffffffL)) { 83 if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) {
84 bb_error_msg("invalid compressed data--%s error", "crc"); 84 bb_error_msg("invalid compressed data--%s error", "crc");
85 return 1; 85 return 1;
86 } 86 }
87 /* Validate decompression - size */ 87 /* Validate decompression - size */
88 if (zip_header->formatted.ucmpsize != gunzip_bytes_out) { 88 if (zip_header->formatted.ucmpsize != res.bytes_out) {
89 bb_error_msg("invalid compressed data--%s error", "length"); 89 bb_error_msg("invalid compressed data--%s error", "length");
90 return 1; 90 return 1;
91 } 91 }
diff --git a/include/unarchive.h b/include/unarchive.h
index 843f68f73..5e87d088e 100644
--- a/include/unarchive.h
+++ b/include/unarchive.h
@@ -65,10 +65,6 @@ typedef struct archive_handle_s {
65} archive_handle_t; 65} archive_handle_t;
66 66
67 67
68extern uint32_t gunzip_crc;
69extern off_t gunzip_bytes_out;
70
71
72extern archive_handle_t *init_handle(void); 68extern archive_handle_t *init_handle(void);
73 69
74extern char filter_accept_all(archive_handle_t *archive_handle); 70extern char filter_accept_all(archive_handle_t *archive_handle);
@@ -106,14 +102,17 @@ extern const llist_t *find_list_entry(const llist_t *list, const char *filename)
106extern const llist_t *find_list_entry2(const llist_t *list, const char *filename); 102extern const llist_t *find_list_entry2(const llist_t *list, const char *filename);
107 103
108extern USE_DESKTOP(long long) int uncompressStream(int src_fd, int dst_fd); 104extern USE_DESKTOP(long long) int uncompressStream(int src_fd, int dst_fd);
109extern void inflate_init(unsigned int bufsize); 105
110extern void inflate_cleanup(void); 106typedef struct inflate_unzip_result {
111extern USE_DESKTOP(long long) int inflate_unzip(int in, int out); 107 off_t bytes_out;
108 uint32_t crc;
109} inflate_unzip_result;
110
111extern USE_DESKTOP(long long) int inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out);
112extern USE_DESKTOP(long long) int inflate_gunzip(int in, int out); 112extern USE_DESKTOP(long long) int inflate_gunzip(int in, int out);
113extern USE_DESKTOP(long long) int unlzma(int src_fd, int dst_fd); 113extern USE_DESKTOP(long long) int unlzma(int src_fd, int dst_fd);
114 114
115extern int open_transformer(int src_fd, 115extern int open_transformer(int src_fd,
116 USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd)); 116 USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd));
117 117
118
119#endif 118#endif