aboutsummaryrefslogtreecommitdiff
path: root/archival/libunarchive
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2002-11-03 14:05:15 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2002-11-03 14:05:15 +0000
commit237ae42fc96ede945d28d9054f045b73e419d089 (patch)
tree3fb6a9c10150303aca3c218b47aaf327a186382a /archival/libunarchive
parent2fc54a9258c3aa5dad2ce9807ba85cf29af2668e (diff)
downloadbusybox-w32-237ae42fc96ede945d28d9054f045b73e419d089.tar.gz
busybox-w32-237ae42fc96ede945d28d9054f045b73e419d089.tar.bz2
busybox-w32-237ae42fc96ede945d28d9054f045b73e419d089.zip
Abstract read and seek in unarchiving code, convert bunzip to file descriptors, support tar -j
Diffstat (limited to 'archival/libunarchive')
-rw-r--r--archival/libunarchive/Makefile.in12
-rw-r--r--archival/libunarchive/archive_copy_file.c48
-rw-r--r--archival/libunarchive/archive_xread.c33
-rw-r--r--archival/libunarchive/archive_xread_all.c (renamed from archival/libunarchive/seek_sub_file.c)18
-rw-r--r--archival/libunarchive/archive_xread_all_eof.c32
-rw-r--r--archival/libunarchive/archive_xread_char.c30
-rw-r--r--archival/libunarchive/copy_file_chunk_fd.c33
-rw-r--r--archival/libunarchive/data_align.c33
-rw-r--r--archival/libunarchive/data_extract_all.c22
-rw-r--r--archival/libunarchive/data_extract_to_buffer.c23
-rw-r--r--archival/libunarchive/data_extract_to_stdout.c20
-rw-r--r--archival/libunarchive/data_skip.c2
-rw-r--r--archival/libunarchive/decompress_bunzip2.c247
-rw-r--r--archival/libunarchive/get_header_tar.c9
-rw-r--r--archival/libunarchive/get_header_tar_gz.c5
-rw-r--r--archival/libunarchive/init_handle.c19
-rw-r--r--archival/libunarchive/seek_by_char.c25
-rw-r--r--archival/libunarchive/seek_by_jump.c35
-rw-r--r--archival/libunarchive/unpack_ar_archive.c2
19 files changed, 392 insertions, 256 deletions
diff --git a/archival/libunarchive/Makefile.in b/archival/libunarchive/Makefile.in
index 432077721..e406f750e 100644
--- a/archival/libunarchive/Makefile.in
+++ b/archival/libunarchive/Makefile.in
@@ -41,15 +41,23 @@ LIBUNARCHIVE-y:= \
41 header_list.o \ 41 header_list.o \
42 header_verbose_list.o \ 42 header_verbose_list.o \
43\ 43\
44 archive_xread.o \
45 archive_xread_all.o \
46 archive_xread_all_eof.o \
47 archive_xread_char.o \
48\
49 seek_by_char.o \
50 seek_by_jump.o \
51\
52 archive_copy_file.o \
53\
44 add_to_list.o \ 54 add_to_list.o \
45 check_header_gzip.o \ 55 check_header_gzip.o \
46 check_trailer_gzip.o \ 56 check_trailer_gzip.o \
47 copy_file_chunk_fd.o \
48 data_align.o \ 57 data_align.o \
49 decompress_bunzip2.o \ 58 decompress_bunzip2.o \
50 find_list_entry.o \ 59 find_list_entry.o \
51 init_handle.o \ 60 init_handle.o \
52 seek_sub_file.o \
53 uncompress.o \ 61 uncompress.o \
54 unpack_ar_archive.o \ 62 unpack_ar_archive.o \
55 unzip.o 63 unzip.o
diff --git a/archival/libunarchive/archive_copy_file.c b/archival/libunarchive/archive_copy_file.c
new file mode 100644
index 000000000..22355ccd5
--- /dev/null
+++ b/archival/libunarchive/archive_copy_file.c
@@ -0,0 +1,48 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <unistd.h>
18
19#include "libbb.h"
20#include "unarchive.h"
21
22/* Copy CHUNKSIZE bytes (or untill EOF if chunksize == -1)
23 * from SRC_FILE to DST_FILE. */
24extern void archive_copy_file(const archive_handle_t *archive_handle, const int dst_fd)
25{
26 size_t size;
27 char buffer[BUFSIZ];
28 off_t chunksize = archive_handle->file_header->size;
29
30 while (chunksize != 0) {
31 if (chunksize > BUFSIZ) {
32 size = BUFSIZ;
33 } else {
34 size = chunksize;
35 }
36 archive_xread_all(archive_handle, buffer, size);
37
38 if (write(dst_fd, buffer, size) != size) {
39 error_msg_and_die ("Short write");
40 }
41
42 if (chunksize != -1) {
43 chunksize -= size;
44 }
45 }
46
47 return;
48}
diff --git a/archival/libunarchive/archive_xread.c b/archival/libunarchive/archive_xread.c
new file mode 100644
index 000000000..7fde4c0b1
--- /dev/null
+++ b/archival/libunarchive/archive_xread.c
@@ -0,0 +1,33 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include "unarchive.h"
21#include "libbb.h"
22
23extern ssize_t archive_xread(const archive_handle_t *archive_handle, unsigned char *buf, const size_t count)
24{
25 ssize_t size;
26
27 size = archive_handle->read(archive_handle->src_fd, buf, count);
28 if (size == -1) {
29 perror_msg_and_die("Read error");
30 }
31
32 return(size);
33}
diff --git a/archival/libunarchive/seek_sub_file.c b/archival/libunarchive/archive_xread_all.c
index 733bb36a9..ef8cc0141 100644
--- a/archival/libunarchive/seek_sub_file.c
+++ b/archival/libunarchive/archive_xread_all.c
@@ -14,19 +14,19 @@
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */ 15 */
16 16
17#include <sys/types.h> 17#include <stdio.h>
18#include <errno.h>
19#include <unistd.h>
20#include <stdlib.h> 18#include <stdlib.h>
19#include <string.h>
21#include "unarchive.h" 20#include "unarchive.h"
22#include "libbb.h" 21#include "libbb.h"
23 22
24extern void seek_sub_file(const int src_fd, const unsigned int amount) 23extern void archive_xread_all(const archive_handle_t *archive_handle, void *buf, const size_t count)
25{ 24{
26 if ((lseek(src_fd, amount, SEEK_CUR) == -1) && (errno == ESPIPE)) { 25 ssize_t size;
27 unsigned int i; 26
28 for (i = 0; i < amount; i++) { 27 size = archive_xread(archive_handle, buf, count);
29 xread_char(src_fd); 28 if (size != count) {
30 } 29 error_msg_and_die("Short read");
31 } 30 }
31 return;
32} 32}
diff --git a/archival/libunarchive/archive_xread_all_eof.c b/archival/libunarchive/archive_xread_all_eof.c
new file mode 100644
index 000000000..3cfbbd8d1
--- /dev/null
+++ b/archival/libunarchive/archive_xread_all_eof.c
@@ -0,0 +1,32 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include "unarchive.h"
21#include "libbb.h"
22
23extern ssize_t archive_xread_all_eof(archive_handle_t *archive_handle, unsigned char *buf, size_t count)
24{
25 ssize_t size;
26
27 size = archive_xread(archive_handle, buf, count);
28 if ((size != 0) && (size != count)) {
29 perror_msg_and_die("Short read, read %d of %d", size, count);
30 }
31 return(size);
32}
diff --git a/archival/libunarchive/archive_xread_char.c b/archival/libunarchive/archive_xread_char.c
new file mode 100644
index 000000000..4c665e159
--- /dev/null
+++ b/archival/libunarchive/archive_xread_char.c
@@ -0,0 +1,30 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include "unarchive.h"
21#include "libbb.h"
22
23extern unsigned char archive_xread_char(const archive_handle_t *archive_handle)
24{
25 unsigned char tmp;
26
27 archive_xread(archive_handle, &tmp, 1);
28
29 return(tmp);
30}
diff --git a/archival/libunarchive/copy_file_chunk_fd.c b/archival/libunarchive/copy_file_chunk_fd.c
deleted file mode 100644
index fb513e6d5..000000000
--- a/archival/libunarchive/copy_file_chunk_fd.c
+++ /dev/null
@@ -1,33 +0,0 @@
1#include <unistd.h>
2#include <sys/types.h>
3#include "libbb.h"
4
5/* Copy CHUNKSIZE bytes (or untill EOF if chunksize == -1)
6 * from SRC_FILE to DST_FILE. */
7extern int copy_file_chunk_fd(int src_fd, int dst_fd, off_t chunksize)
8{
9 size_t nread, size;
10 char buffer[BUFSIZ];
11
12 while (chunksize != 0) {
13 if (chunksize > BUFSIZ) {
14 size = BUFSIZ;
15 } else {
16 size = chunksize;
17 }
18 nread = xread(src_fd, buffer, size);
19 if (nread == 0) {
20 return 1;
21 }
22
23 if (write (dst_fd, buffer, nread) != nread) {
24 error_msg_and_die ("Short write");
25 }
26
27 if (chunksize != -1) {
28 chunksize -= nread;
29 }
30 }
31
32 return 0;
33}
diff --git a/archival/libunarchive/data_align.c b/archival/libunarchive/data_align.c
index d6243bc19..037242f25 100644
--- a/archival/libunarchive/data_align.c
+++ b/archival/libunarchive/data_align.c
@@ -1,13 +1,34 @@
1#include <errno.h> 1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
2#include <sys/types.h> 17#include <sys/types.h>
18
19#include <errno.h>
3#include <unistd.h> 20#include <unistd.h>
4#include "unarchive.h" 21
5#include "libbb.h" 22#include "libbb.h"
23#include "unarchive.h"
6 24
7extern const unsigned short data_align(const int src_fd, const unsigned int offset, const unsigned short align_to) 25extern void data_align(archive_handle_t *archive_handle, const unsigned short boundary)
8{ 26{
9 const unsigned short skip_amount = (align_to - (offset % align_to)) % align_to; 27 const unsigned short skip_amount = (boundary - (archive_handle->offset % boundary)) % boundary;
10 seek_sub_file(src_fd, skip_amount); 28
29 archive_handle->seek(archive_handle, skip_amount);
30
31 archive_handle->offset += skip_amount;
11 32
12 return(skip_amount); 33 return;
13} 34}
diff --git a/archival/libunarchive/data_extract_all.c b/archival/libunarchive/data_extract_all.c
index f839be35e..dda514771 100644
--- a/archival/libunarchive/data_extract_all.c
+++ b/archival/libunarchive/data_extract_all.c
@@ -1,4 +1,21 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
1#include <sys/types.h> 17#include <sys/types.h>
18
2#include <errno.h> 19#include <errno.h>
3#include <fcntl.h> 20#include <fcntl.h>
4#include <stdlib.h> 21#include <stdlib.h>
@@ -6,6 +23,7 @@
6#include <utime.h> 23#include <utime.h>
7#include <unistd.h> 24#include <unistd.h>
8#include <stdlib.h> 25#include <stdlib.h>
26
9#include "libbb.h" 27#include "libbb.h"
10#include "unarchive.h" 28#include "unarchive.h"
11 29
@@ -21,7 +39,7 @@ extern void data_extract_all(archive_handle_t *archive_handle)
21 free(name); 39 free(name);
22 } 40 }
23 41
24 /* Create the file */ 42 /* Create the filesystem entry */
25 switch(file_header->mode & S_IFMT) { 43 switch(file_header->mode & S_IFMT) {
26 case S_IFREG: { 44 case S_IFREG: {
27#ifdef CONFIG_CPIO 45#ifdef CONFIG_CPIO
@@ -36,7 +54,7 @@ extern void data_extract_all(archive_handle_t *archive_handle)
36 { 54 {
37 /* Regular file */ 55 /* Regular file */
38 dst_fd = xopen(file_header->name, O_WRONLY | O_CREAT); 56 dst_fd = xopen(file_header->name, O_WRONLY | O_CREAT);
39 copy_file_chunk_fd(archive_handle->src_fd, dst_fd, file_header->size); 57 archive_copy_file(archive_handle, dst_fd);
40 close(dst_fd); 58 close(dst_fd);
41 } 59 }
42 break; 60 break;
diff --git a/archival/libunarchive/data_extract_to_buffer.c b/archival/libunarchive/data_extract_to_buffer.c
index 02ee4b362..3fcab6d02 100644
--- a/archival/libunarchive/data_extract_to_buffer.c
+++ b/archival/libunarchive/data_extract_to_buffer.c
@@ -1,11 +1,26 @@
1#include <stdlib.h> 1/*
2#include <stdio.h> 2 * This program is free software; you can redistribute it and/or modify
3#include "unarchive.h" 3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
4#include "libbb.h" 17#include "libbb.h"
18#include "unarchive.h"
5 19
6extern void data_extract_to_buffer(archive_handle_t *archive_handle) 20extern void data_extract_to_buffer(archive_handle_t *archive_handle)
7{ 21{
8 archive_handle->buffer = xmalloc(archive_handle->file_header->size + 1); 22 archive_handle->buffer = xmalloc(archive_handle->file_header->size + 1);
9 23
10 xread_all(archive_handle->src_fd, archive_handle->buffer, archive_handle->file_header->size); 24 archive_xread_all(archive_handle, archive_handle->buffer, archive_handle->file_header->size);
25
11} 26}
diff --git a/archival/libunarchive/data_extract_to_stdout.c b/archival/libunarchive/data_extract_to_stdout.c
index 00687b315..8be2fa2e9 100644
--- a/archival/libunarchive/data_extract_to_stdout.c
+++ b/archival/libunarchive/data_extract_to_stdout.c
@@ -1,8 +1,22 @@
1#include <stdlib.h> 1/*
2#include <stdio.h> 2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
3#include "unarchive.h" 17#include "unarchive.h"
4 18
5extern void data_extract_to_stdout(archive_handle_t *archive_handle) 19extern void data_extract_to_stdout(archive_handle_t *archive_handle)
6{ 20{
7 copy_file_chunk_fd(archive_handle->src_fd, fileno(stdout), archive_handle->file_header->size); 21 archive_copy_file(archive_handle, fileno(stdout));
8} 22}
diff --git a/archival/libunarchive/data_skip.c b/archival/libunarchive/data_skip.c
index 4e63d4304..b82c9065b 100644
--- a/archival/libunarchive/data_skip.c
+++ b/archival/libunarchive/data_skip.c
@@ -23,5 +23,5 @@
23 23
24extern void data_skip(archive_handle_t *archive_handle) 24extern void data_skip(archive_handle_t *archive_handle)
25{ 25{
26 seek_sub_file(archive_handle->src_fd, archive_handle->file_header->size); 26 archive_handle->seek(archive_handle, archive_handle->file_header->size);
27} 27}
diff --git a/archival/libunarchive/decompress_bunzip2.c b/archival/libunarchive/decompress_bunzip2.c
index dd15b819f..00ae5a494 100644
--- a/archival/libunarchive/decompress_bunzip2.c
+++ b/archival/libunarchive/decompress_bunzip2.c
@@ -57,10 +57,8 @@
57#include <string.h> 57#include <string.h>
58#include <getopt.h> 58#include <getopt.h>
59#include <unistd.h> 59#include <unistd.h>
60#include <busybox.h>
61 60
62//#define TRUE 1 61#include "busybox.h"
63//#define FALSE 0
64 62
65#define MTFA_SIZE 4096 63#define MTFA_SIZE 4096
66#define MTFL_SIZE 16 64#define MTFL_SIZE 16
@@ -142,9 +140,10 @@ typedef struct {
142 140
143} bz_stream; 141} bz_stream;
144 142
143#define BZ_MAX_UNUSED 5000
145typedef struct { 144typedef struct {
146 bz_stream strm; 145 bz_stream strm;
147 FILE *handle; 146 int fd;
148 unsigned char initialisedOk; 147 unsigned char initialisedOk;
149 char buf[BZ_MAX_UNUSED]; 148 char buf[BZ_MAX_UNUSED];
150 int lastErr; 149 int lastErr;
@@ -237,18 +236,11 @@ typedef struct {
237 int *save_gPerm; 236 int *save_gPerm;
238} DState; 237} DState;
239 238
240int BZ2_rNums[512]; 239static int BZ2_rNums[512];
241char inName[FILE_NAME_LEN]; 240static bzFile *bzf;
242char outName[FILE_NAME_LEN]; 241static int bzerr = BZ_OK;
243int srcMode;
244int opMode;
245unsigned char deleteOutputOnInterrupt;
246FILE *outputHandleJustInCase;
247int numFileNames;
248int numFilesProcessed;
249int exitValue;
250 242
251const unsigned int BZ2_crc32Table[256] = { 243static const unsigned int BZ2_crc32Table[256] = {
252 244
253 /*-- Ugly, innit? --*/ 245 /*-- Ugly, innit? --*/
254 246
@@ -330,16 +322,6 @@ static void bz_rand_udp_mask(DState *s)
330 s->rNToGo--; 322 s->rNToGo--;
331} 323}
332 324
333static unsigned char myfeof(FILE *f)
334{
335 int c = fgetc(f);
336 if (c == EOF) {
337 return(TRUE);
338 }
339 ungetc(c, f);
340 return(FALSE);
341}
342
343static void BZ2_hbCreateDecodeTables(int *limit, int *base, int *perm, unsigned char *length, int minLen, int maxLen, int alphaSize ) 325static void BZ2_hbCreateDecodeTables(int *limit, int *base, int *perm, unsigned char *length, int minLen, int maxLen, int alphaSize )
344{ 326{
345 int pp, i, j, vec; 327 int pp, i, j, vec;
@@ -1292,43 +1274,8 @@ save_state_and_return:
1292 return retVal; 1274 return retVal;
1293} 1275}
1294 1276
1295//int BZ2_bzDecompressInit(bz_stream* strm, int verbosity_level, int small) 1277static void BZ2_bzReadClose(void)
1296static inline int BZ2_bzDecompressInit(bz_stream* strm)
1297{
1298 DState* s;
1299
1300// if (verbosity_level < 0 || verbosity_level > 4) {
1301// return BZ_PARAM_ERROR;
1302// }
1303 s = xmalloc(sizeof(DState));
1304 s->strm = strm;
1305 strm->state = s;
1306 s->state = BZ_X_MAGIC_1;
1307 s->bsLive = 0;
1308 s->bsBuff = 0;
1309 s->calculatedCombinedCRC = 0;
1310 s->tt = NULL;
1311 s->currBlockNo = 0;
1312
1313 return BZ_OK;
1314}
1315
1316static void bz_seterr(int eee, int *bzerror, bzFile **bzf)
1317{ 1278{
1318 if (bzerror != NULL) {
1319 *bzerror = eee;
1320 }
1321 if (*bzf != NULL) {
1322 (*bzf)->lastErr = eee;
1323 }
1324}
1325
1326static void BZ2_bzReadClose(int *bzerror, void *b)
1327{
1328 bzFile* bzf = (bzFile*)b;
1329
1330 bz_seterr(BZ_OK, bzerror, &bzf);
1331
1332 if (bzf->initialisedOk) { 1279 if (bzf->initialisedOk) {
1333 bz_stream *strm = &(bzf->strm); 1280 bz_stream *strm = &(bzf->strm);
1334 DState *s; 1281 DState *s;
@@ -1588,31 +1535,22 @@ int BZ2_bzDecompress(bz_stream *strm)
1588 return(0); /*NOTREACHED*/ 1535 return(0); /*NOTREACHED*/
1589} 1536}
1590 1537
1591static inline int BZ2_bzRead(int *bzerror, void *b, void *buf, int len) 1538extern ssize_t read_bz2(int fd, void *buf, size_t count)
1592{ 1539{
1593 int n, ret; 1540 int n, ret;
1594 bzFile *bzf = (bzFile*)b;
1595
1596 bz_seterr(BZ_OK, bzerror, &bzf);
1597 1541
1598 if (len == 0) { 1542 bzerr = BZ_OK;
1599 bz_seterr(BZ_OK, bzerror, &bzf); 1543 if (count == 0) {
1600 return 0; 1544 return(0);
1601 } 1545 }
1602 1546 bzf->strm.avail_out = count;
1603 bzf->strm.avail_out = len;
1604 bzf->strm.next_out = buf; 1547 bzf->strm.next_out = buf;
1605 1548
1606 while (1) { 1549 while (1) {
1607 if (ferror(bzf->handle)) { 1550 if (bzf->strm.avail_in == 0) {
1608 bz_seterr(BZ_IO_ERROR, bzerror, &bzf); 1551 n = xread(bzf->fd, bzf->buf, BZ_MAX_UNUSED);
1609 return 0; 1552 if (n == 0) {
1610 } 1553 break;
1611 if ((bzf->strm.avail_in == 0) && !myfeof(bzf->handle)) {
1612 n = fread(bzf->buf, sizeof(unsigned char), BZ_MAX_UNUSED, bzf->handle);
1613 if (ferror(bzf->handle)) {
1614 bz_seterr(BZ_IO_ERROR, bzerror, &bzf);
1615 return 0;
1616 } 1554 }
1617 bzf->bufN = n; 1555 bzf->bufN = n;
1618 bzf->strm.avail_in = bzf->bufN; 1556 bzf->strm.avail_in = bzf->bufN;
@@ -1622,48 +1560,43 @@ static inline int BZ2_bzRead(int *bzerror, void *b, void *buf, int len)
1622 ret = BZ2_bzDecompress(&(bzf->strm)); 1560 ret = BZ2_bzDecompress(&(bzf->strm));
1623 1561
1624 if ((ret != BZ_OK) && (ret != BZ_STREAM_END)) { 1562 if ((ret != BZ_OK) && (ret != BZ_STREAM_END)) {
1625 bz_seterr(ret, bzerror, &bzf); 1563 error_msg_and_die("Error decompressing");
1626 return 0;
1627 }
1628
1629 if ((ret == BZ_OK) && myfeof(bzf->handle) &&
1630 (bzf->strm.avail_in == 0) && (bzf->strm.avail_out > 0)) {
1631 bz_seterr(BZ_UNEXPECTED_EOF, bzerror, &bzf);
1632 return(0);
1633 } 1564 }
1634 1565
1635 if (ret == BZ_STREAM_END) { 1566 if (ret == BZ_STREAM_END) {
1636 bz_seterr(BZ_STREAM_END, bzerror, &bzf); 1567 bzerr = BZ_STREAM_END;
1637 return(len - bzf->strm.avail_out); 1568 return(count - bzf->strm.avail_out);
1638 } 1569 }
1639 if (bzf->strm.avail_out == 0) { 1570 if (bzf->strm.avail_out == 0) {
1640 bz_seterr(BZ_OK, bzerror, &bzf); 1571 bzerr = BZ_OK;
1641 return(len); 1572 return(count);
1642 } 1573 }
1643 } 1574 }
1644 return(0); /*not reached*/ 1575 return(0);
1645} 1576}
1646 1577
1647static inline void *BZ2_bzReadOpen(int *bzerror, FILE *f, void *unused, int nUnused) 1578extern void BZ2_bzReadOpen(int fd, void *unused, int nUnused)
1648{ 1579{
1649 bzFile *bzf = xmalloc(sizeof(bzFile)); 1580 DState *s;
1650 int ret;
1651
1652 bz_seterr(BZ_OK, bzerror, &bzf);
1653 1581
1582 bzf = xmalloc(sizeof(bzFile));
1654 bzf->initialisedOk = FALSE; 1583 bzf->initialisedOk = FALSE;
1655 bzf->handle = f; 1584 bzf->fd = fd;
1656 bzf->bufN = 0; 1585 bzf->bufN = 0;
1657 1586
1658 ret = BZ2_bzDecompressInit(&(bzf->strm)); 1587 s = xmalloc(sizeof(DState));
1659 if (ret != BZ_OK) { 1588 s->strm = &bzf->strm;
1660 bz_seterr(ret, bzerror, &bzf); 1589 s->state = BZ_X_MAGIC_1;
1661 free(bzf); 1590 s->bsLive = 0;
1662 return NULL; 1591 s->bsBuff = 0;
1663 } 1592 s->calculatedCombinedCRC = 0;
1593 s->tt = NULL;
1594 s->currBlockNo = 0;
1595 bzf->strm.state = s;
1664 1596
1665 while (nUnused > 0) { 1597 while (nUnused > 0) {
1666 bzf->buf[bzf->bufN] = *((unsigned char *)(unused)); bzf->bufN++; 1598 bzf->buf[bzf->bufN] = *((unsigned char *)(unused));
1599 bzf->bufN++;
1667 unused = ((void *)( 1 + ((unsigned char *)(unused)) )); 1600 unused = ((void *)( 1 + ((unsigned char *)(unused)) ));
1668 nUnused--; 1601 nUnused--;
1669 } 1602 }
@@ -1671,119 +1604,55 @@ static inline void *BZ2_bzReadOpen(int *bzerror, FILE *f, void *unused, int nUnu
1671 bzf->strm.next_in = bzf->buf; 1604 bzf->strm.next_in = bzf->buf;
1672 1605
1673 bzf->initialisedOk = TRUE; 1606 bzf->initialisedOk = TRUE;
1674 return bzf; 1607
1608 return;
1675} 1609}
1676 1610
1677extern unsigned char uncompressStream(FILE *zStream, FILE *stream) 1611extern unsigned char uncompressStream(int src_fd, int dst_fd)
1678{ 1612{
1679 unsigned char unused[BZ_MAX_UNUSED]; 1613 unsigned char unused[BZ_MAX_UNUSED];
1680 unsigned char *unusedTmp; 1614 unsigned char *unusedTmp;
1681 unsigned char obuf[5000]; 1615 unsigned char obuf[5000];
1682 bzFile *bzf = NULL;
1683 int bzerr_dummy;
1684 int bzerr;
1685 int nread; 1616 int nread;
1686 int nUnused; 1617 int nUnused;
1687 int streamNo; 1618 int streamNo;
1688 int ret;
1689 int i; 1619 int i;
1690 1620
1691 nUnused = 0; 1621 nUnused = 0;
1692 streamNo = 0; 1622 streamNo = 0;
1693 1623
1694 if (ferror(stream)) {
1695 goto errhandler_io;
1696 }
1697 if (ferror(zStream)) {
1698 goto errhandler_io;
1699 }
1700
1701 while(1) { 1624 while(1) {
1702 bzf = BZ2_bzReadOpen(&bzerr, zStream, unused, nUnused); 1625 BZ2_bzReadOpen(src_fd, unused, nUnused);
1703 if (bzf == NULL || bzerr != BZ_OK) {
1704 goto errhandler;
1705 }
1706 streamNo++; 1626 streamNo++;
1707 1627
1708 while (bzerr == BZ_OK) { 1628 while (bzerr == BZ_OK) {
1709 nread = BZ2_bzRead(&bzerr, bzf, obuf, 5000); 1629 nread = read_bz2(src_fd, obuf, 5000);
1710 if (bzerr == BZ_DATA_ERROR_MAGIC) { 1630 if (bzerr == BZ_DATA_ERROR_MAGIC) {
1711 goto errhandler; 1631 error_msg_and_die("invalid magic");
1712 }
1713 if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0) {
1714 fwrite(obuf, sizeof(unsigned char), nread, stream);
1715 } 1632 }
1716 if (ferror(stream)) { 1633 if (((bzerr == BZ_OK) || (bzerr == BZ_STREAM_END)) && (nread > 0)) {
1717 goto errhandler_io; 1634 if (write(dst_fd, obuf, nread) != nread) {
1635 BZ2_bzReadClose();
1636 perror_msg_and_die("Couldnt write to file");
1637 }
1718 } 1638 }
1719 } 1639 }
1720 if (bzerr != BZ_STREAM_END) {
1721 goto errhandler;
1722 }
1723 nUnused = bzf->strm.avail_in; 1640 nUnused = bzf->strm.avail_in;
1724 unusedTmp = bzf->strm.next_in; 1641 unusedTmp = bzf->strm.next_in;
1725 bz_seterr(BZ_OK, &bzerr, &bzf); 1642
1726 for (i = 0; i < nUnused; i++) { 1643 for (i = 0; i < nUnused; i++) {
1727 unused[i] = unusedTmp[i]; 1644 unused[i] = unusedTmp[i];
1728 } 1645 }
1729 BZ2_bzReadClose(&bzerr, bzf); 1646 BZ2_bzReadClose();
1730 if ((nUnused == 0) && myfeof(zStream)) { 1647 if (nUnused == 0) {
1731 break; 1648 break;
1732 } 1649 }
1733 } 1650 }
1734 1651
1735 if (ferror(zStream)) { 1652 close(src_fd);
1736 goto errhandler_io; 1653 if (dst_fd != fileno(stdout)) {
1737 } 1654 close(dst_fd);
1738 ret = fclose(zStream);
1739 if (ret == EOF) {
1740 goto errhandler_io;
1741 }
1742 if (ferror(stream)) {
1743 goto errhandler_io;
1744 }
1745 ret = fflush(stream);
1746 if (ret != 0) {
1747 goto errhandler_io;
1748 }
1749 if (stream != stdout) {
1750 ret = fclose(stream);
1751 if (ret == EOF) {
1752 goto errhandler_io;
1753 }
1754 } 1655 }
1755 return TRUE; 1656 return TRUE;
1756
1757errhandler:
1758 BZ2_bzReadClose ( &bzerr_dummy, bzf );
1759 switch (bzerr) {
1760 case BZ_IO_ERROR:
1761errhandler_io:
1762 error_msg("\n%s: I/O or other error, bailing out. "
1763 "Possible reason follows.\n", applet_name);
1764 perror(applet_name);
1765 exit(1);
1766 case BZ_DATA_ERROR:
1767 error_msg("\n%s: Data integrity error when decompressing.\n", applet_name);
1768 exit(2);
1769 case BZ_UNEXPECTED_EOF:
1770 error_msg("\n%s: Compressed file ends unexpectedly;\n\t"
1771 "perhaps it is corrupted? *Possible* reason follows.\n", applet_name);
1772 perror(applet_name);
1773 exit(2);
1774 case BZ_DATA_ERROR_MAGIC:
1775 if (zStream != stdin) {
1776 fclose(zStream);
1777 }
1778 if (stream != stdout) {
1779 fclose(stream);
1780 }
1781 if (streamNo == 1) {
1782 return FALSE;
1783 } else {
1784 return TRUE;
1785 }
1786 }
1787
1788 return(TRUE); /*notreached*/
1789} 1657}
1658
diff --git a/archival/libunarchive/get_header_tar.c b/archival/libunarchive/get_header_tar.c
index e87eb77b8..b2b9e0b03 100644
--- a/archival/libunarchive/get_header_tar.c
+++ b/archival/libunarchive/get_header_tar.c
@@ -50,9 +50,9 @@ extern char get_header_tar(archive_handle_t *archive_handle)
50 char *tmp; 50 char *tmp;
51 51
52 /* Align header */ 52 /* Align header */
53 archive_handle->offset += data_align(archive_handle->src_fd, archive_handle->offset, 512); 53 data_align(archive_handle, 512);
54 54
55 if (xread_all_eof(archive_handle->src_fd, tar.raw, 512) == 0) { 55 if (archive_xread_all_eof(archive_handle, tar.raw, 512) == 0) {
56 /* End of file */ 56 /* End of file */
57 return(EXIT_FAILURE); 57 return(EXIT_FAILURE);
58 } 58 }
@@ -72,7 +72,6 @@ extern char get_header_tar(archive_handle_t *archive_handle)
72#endif 72#endif
73 error_msg_and_die("Invalid tar magic"); 73 error_msg_and_die("Invalid tar magic");
74 } 74 }
75
76 /* Do checksum on headers */ 75 /* Do checksum on headers */
77 for (i = 0; i < 148 ; i++) { 76 for (i = 0; i < 148 ; i++) {
78 sum += tar.raw[i]; 77 sum += tar.raw[i];
@@ -138,7 +137,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
138 char *longname; 137 char *longname;
139 138
140 longname = xmalloc(file_header->size + 1); 139 longname = xmalloc(file_header->size + 1);
141 xread_all(archive_handle->src_fd, longname, file_header->size); 140 archive_xread_all(archive_handle, longname, file_header->size);
142 longname[file_header->size] = '\0'; 141 longname[file_header->size] = '\0';
143 archive_handle->offset += file_header->size; 142 archive_handle->offset += file_header->size;
144 143
@@ -150,7 +149,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
150 char *linkname; 149 char *linkname;
151 150
152 linkname = xmalloc(file_header->size + 1); 151 linkname = xmalloc(file_header->size + 1);
153 xread_all(archive_handle->src_fd, linkname, file_header->size); 152 archive_xread_all(archive_handle, linkname, file_header->size);
154 linkname[file_header->size] = '\0'; 153 linkname[file_header->size] = '\0';
155 archive_handle->offset += file_header->size; 154 archive_handle->offset += file_header->size;
156 155
diff --git a/archival/libunarchive/get_header_tar_gz.c b/archival/libunarchive/get_header_tar_gz.c
index 24d19fbfd..f0d4b1359 100644
--- a/archival/libunarchive/get_header_tar_gz.c
+++ b/archival/libunarchive/get_header_tar_gz.c
@@ -29,7 +29,10 @@ extern char get_header_tar_gz(archive_handle_t *archive_handle)
29 int pid; 29 int pid;
30 unsigned char magic[2]; 30 unsigned char magic[2];
31 31
32 xread_all(archive_handle->src_fd, &magic, 2); 32 /* Cant lseek over pipe's */
33 archive_handle->seek = seek_by_char;
34
35 archive_xread_all(archive_handle, &magic, 2);
33 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { 36 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
34 error_msg_and_die("Invalid gzip magic"); 37 error_msg_and_die("Invalid gzip magic");
35 } 38 }
diff --git a/archival/libunarchive/init_handle.c b/archival/libunarchive/init_handle.c
index 12d9e7183..4b0103491 100644
--- a/archival/libunarchive/init_handle.c
+++ b/archival/libunarchive/init_handle.c
@@ -1,3 +1,20 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <unistd.h>
1#include <string.h> 18#include <string.h>
2#include "libbb.h" 19#include "libbb.h"
3#include "unarchive.h" 20#include "unarchive.h"
@@ -13,6 +30,8 @@ archive_handle_t *init_handle(void)
13 archive_handle->action_header = header_skip; 30 archive_handle->action_header = header_skip;
14 archive_handle->action_data = data_skip; 31 archive_handle->action_data = data_skip;
15 archive_handle->filter = filter_accept_all; 32 archive_handle->filter = filter_accept_all;
33 archive_handle->read = read;
34 archive_handle->seek = seek_by_jump;
16 35
17 return(archive_handle); 36 return(archive_handle);
18} 37}
diff --git a/archival/libunarchive/seek_by_char.c b/archival/libunarchive/seek_by_char.c
new file mode 100644
index 000000000..f33935cb5
--- /dev/null
+++ b/archival/libunarchive/seek_by_char.c
@@ -0,0 +1,25 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include "unarchive.h"
18
19extern void seek_by_char(const archive_handle_t *archive_handle, const unsigned int amount)
20{
21 unsigned int i;
22 for (i = 0; i < amount; i++) {
23 archive_xread_char(archive_handle);
24 }
25}
diff --git a/archival/libunarchive/seek_by_jump.c b/archival/libunarchive/seek_by_jump.c
new file mode 100644
index 000000000..efad97fc4
--- /dev/null
+++ b/archival/libunarchive/seek_by_jump.c
@@ -0,0 +1,35 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <sys/types.h>
18#include <errno.h>
19#include <unistd.h>
20#include <stdlib.h>
21
22#include "libbb.h"
23#include "unarchive.h"
24
25extern void seek_by_jump(const archive_handle_t *archive_handle, const unsigned int amount)
26{
27 if (lseek(archive_handle->src_fd, (off_t) amount, SEEK_CUR) == (off_t) -1) {
28#if CONFIG_FEATURE_UNARCHIVE_TAPE
29 if (errno == ESPIPE) {
30 seek_by_char(archive_handle, amount);
31 } else
32#endif
33 perror_msg_and_die("Seek failure");
34 }
35}
diff --git a/archival/libunarchive/unpack_ar_archive.c b/archival/libunarchive/unpack_ar_archive.c
index 923b8a068..afa3672ad 100644
--- a/archival/libunarchive/unpack_ar_archive.c
+++ b/archival/libunarchive/unpack_ar_archive.c
@@ -24,7 +24,7 @@ extern void unpack_ar_archive(archive_handle_t *ar_archive)
24{ 24{
25 char magic[7]; 25 char magic[7];
26 26
27 xread_all(ar_archive->src_fd, magic, 7); 27 archive_xread_all(ar_archive, magic, 7);
28 if (strncmp(magic, "!<arch>", 7) != 0) { 28 if (strncmp(magic, "!<arch>", 7) != 0) {
29 error_msg_and_die("Invalid ar magic"); 29 error_msg_and_die("Invalid ar magic");
30 } 30 }