diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-01-05 23:56:53 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-01-05 23:56:53 +0000 |
commit | 57053ee5e498b7afe91cb67f6731ad0c51a7c7a6 (patch) | |
tree | 10731cda21479451f3ca050c9f030b682a1d99aa | |
parent | a4639334d8e29e50174f6f2d329878c443d322d1 (diff) | |
download | busybox-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.c | 73 | ||||
-rw-r--r-- | archival/unzip.c | 10 | ||||
-rw-r--r-- | include/unarchive.h | 15 |
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 */ | 48 | static off_t gunzip_bytes_out; /* number of output bytes */ |
49 | off_t gunzip_bytes_out; /* number of output bytes */ | 49 | static uint32_t gunzip_crc; |
50 | uint32_t gunzip_crc; | ||
51 | 50 | ||
52 | static int gunzip_src_fd; | 51 | static int gunzip_src_fd; |
53 | static unsigned gunzip_outbuf_count; /* bytes in output buffer */ | 52 | static 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 | */ |
168 | static | 167 | static int huft_build(unsigned *b, const unsigned n, |
169 | int 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 */ | ||
859 | void 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 */ | ||
868 | void 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 */ |
874 | USE_DESKTOP(long long) int | 856 | static USE_DESKTOP(long long) int |
875 | inflate_unzip(int in, int out) | 857 | inflate_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 | |||
908 | USE_DESKTOP(long long) int | ||
909 | inflate_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 | |||
925 | USE_DESKTOP(long long) int | 926 | USE_DESKTOP(long long) int |
926 | inflate_gunzip(int in, int out) | 927 | inflate_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 | ||
68 | extern uint32_t gunzip_crc; | ||
69 | extern off_t gunzip_bytes_out; | ||
70 | |||
71 | |||
72 | extern archive_handle_t *init_handle(void); | 68 | extern archive_handle_t *init_handle(void); |
73 | 69 | ||
74 | extern char filter_accept_all(archive_handle_t *archive_handle); | 70 | extern 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) | |||
106 | extern const llist_t *find_list_entry2(const llist_t *list, const char *filename); | 102 | extern const llist_t *find_list_entry2(const llist_t *list, const char *filename); |
107 | 103 | ||
108 | extern USE_DESKTOP(long long) int uncompressStream(int src_fd, int dst_fd); | 104 | extern USE_DESKTOP(long long) int uncompressStream(int src_fd, int dst_fd); |
109 | extern void inflate_init(unsigned int bufsize); | 105 | |
110 | extern void inflate_cleanup(void); | 106 | typedef struct inflate_unzip_result { |
111 | extern 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 | |||
111 | extern USE_DESKTOP(long long) int inflate_unzip(inflate_unzip_result *res, unsigned bufsize, int in, int out); | ||
112 | extern USE_DESKTOP(long long) int inflate_gunzip(int in, int out); | 112 | extern USE_DESKTOP(long long) int inflate_gunzip(int in, int out); |
113 | extern USE_DESKTOP(long long) int unlzma(int src_fd, int dst_fd); | 113 | extern USE_DESKTOP(long long) int unlzma(int src_fd, int dst_fd); |
114 | 114 | ||
115 | extern int open_transformer(int src_fd, | 115 | extern 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 |