summaryrefslogtreecommitdiff
path: root/release/src/router/busybox/archival/libunarchive
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2015-01-03 12:04:58 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2015-01-03 12:04:58 +0100
commit008d0be72b2f160382c6e880765e96b64a050c65 (patch)
tree36f48a98a3815a408e2ce1693dd182af90f80305 /release/src/router/busybox/archival/libunarchive
parent611becfb8726c60cb060368541ad98191d4532f5 (diff)
downloadtomato-008d0be72b2f160382c6e880765e96b64a050c65.tar.gz
tomato-008d0be72b2f160382c6e880765e96b64a050c65.tar.bz2
imported original firmware WRT54GL_v4.30.11_11_US
Diffstat (limited to 'release/src/router/busybox/archival/libunarchive')
-rw-r--r--release/src/router/busybox/archival/libunarchive/Makefile30
-rwxr-xr-xrelease/src/router/busybox/archival/libunarchive/Makefile.in79
-rw-r--r--release/src/router/busybox/archival/libunarchive/archive_copy_file.c44
-rw-r--r--release/src/router/busybox/archival/libunarchive/archive_xread.c33
-rw-r--r--release/src/router/busybox/archival/libunarchive/archive_xread_all.c32
-rw-r--r--release/src/router/busybox/archival/libunarchive/archive_xread_all_eof.c32
-rw-r--r--release/src/router/busybox/archival/libunarchive/archive_xread_char.c30
-rw-r--r--release/src/router/busybox/archival/libunarchive/check_header_gzip.c57
-rw-r--r--release/src/router/busybox/archival/libunarchive/data_align.c34
-rw-r--r--release/src/router/busybox/archival/libunarchive/data_extract_all.c100
-rw-r--r--release/src/router/busybox/archival/libunarchive/data_extract_to_buffer.c28
-rw-r--r--release/src/router/busybox/archival/libunarchive/data_extract_to_stdout.c22
-rw-r--r--release/src/router/busybox/archival/libunarchive/data_skip.c27
-rw-r--r--release/src/router/busybox/archival/libunarchive/decompress_bunzip2.c1658
-rw-r--r--release/src/router/busybox/archival/libunarchive/filter_accept_all.c32
-rw-r--r--release/src/router/busybox/archival/libunarchive/filter_accept_list.c34
-rw-r--r--release/src/router/busybox/archival/libunarchive/filter_accept_list_reassign.c58
-rw-r--r--release/src/router/busybox/archival/libunarchive/filter_accept_reject_list.c45
-rw-r--r--release/src/router/busybox/archival/libunarchive/find_list_entry.c30
-rw-r--r--release/src/router/busybox/archival/libunarchive/get_header_ar.c122
-rw-r--r--release/src/router/busybox/archival/libunarchive/get_header_cpio.c189
-rw-r--r--release/src/router/busybox/archival/libunarchive/get_header_tar.c188
-rw-r--r--release/src/router/busybox/archival/libunarchive/get_header_tar_bz2.c42
-rw-r--r--release/src/router/busybox/archival/libunarchive/get_header_tar_gz.c49
-rw-r--r--release/src/router/busybox/archival/libunarchive/header_list.c7
-rw-r--r--release/src/router/busybox/archival/libunarchive/header_skip.c7
-rw-r--r--release/src/router/busybox/archival/libunarchive/header_verbose_list.c29
-rw-r--r--release/src/router/busybox/archival/libunarchive/init_handle.c37
-rw-r--r--release/src/router/busybox/archival/libunarchive/seek_by_char.c25
-rw-r--r--release/src/router/busybox/archival/libunarchive/seek_by_jump.c35
-rw-r--r--release/src/router/busybox/archival/libunarchive/uncompress.c298
-rw-r--r--release/src/router/busybox/archival/libunarchive/unpack_ar_archive.c34
-rw-r--r--release/src/router/busybox/archival/libunarchive/unzip.c1040
33 files changed, 4507 insertions, 0 deletions
diff --git a/release/src/router/busybox/archival/libunarchive/Makefile b/release/src/router/busybox/archival/libunarchive/Makefile
new file mode 100644
index 00000000..272ca243
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/Makefile
@@ -0,0 +1,30 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+TOPDIR:= ../../
+LIBUNARCHIVE_DIR:=./
+include $(TOPDIR).config
+include $(TOPDIR)Rules.mak
+include Makefile.in
+all: $(libraries-y)
+-include $(TOPDIR).depend
+
+clean:
+ rm -f *.o *.a $(AR_TARGET)
+
diff --git a/release/src/router/busybox/archival/libunarchive/Makefile.in b/release/src/router/busybox/archival/libunarchive/Makefile.in
new file mode 100755
index 00000000..2b32aaa5
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/Makefile.in
@@ -0,0 +1,79 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+LIBUNARCHIVE_AR:=libunarchive.a
+ifndef $(LIBUNARCHIVE_DIR)
+LIBUNARCHIVE_DIR:=$(TOPDIR)archival/libunarchive/
+endif
+
+LIBUNARCHIVE-y:= \
+\
+ data_skip.o \
+ data_extract_all.o \
+ data_extract_to_stdout.o \
+ data_extract_to_buffer.o \
+\
+ filter_accept_all.o \
+ filter_accept_list.o \
+ filter_accept_list_reassign.o \
+ filter_accept_reject_list.o \
+\
+ get_header_ar.o \
+ get_header_cpio.o \
+ get_header_tar.o \
+ get_header_tar_bz2.o \
+ get_header_tar_gz.o \
+\
+ header_skip.o \
+ header_list.o \
+ header_verbose_list.o \
+\
+ archive_xread.o \
+ archive_xread_all.o \
+ archive_xread_all_eof.o \
+ archive_xread_char.o \
+\
+ seek_by_char.o \
+ seek_by_jump.o \
+\
+ archive_copy_file.o \
+\
+ check_header_gzip.o \
+ data_align.o \
+ decompress_bunzip2.o \
+ find_list_entry.o \
+ init_handle.o \
+ uncompress.o \
+ unpack_ar_archive.o \
+ unzip.o
+
+LIBUNARCHIVE-$(CONFIG_DPKG) +=
+LIBUNARCHIVE-$(CONFIG_DPKG_DEB) +=
+LIBUNARCHIVE-$(CONFIG_AR) +=
+LIBUNARCHIVE-$(CONFIG_CPIO) +=
+LIBUNARCHIVE-$(CONFIG_GUNZIP) +=
+LIBUNARCHIVE-$(CONFIG_RPM2CPIO) +=
+LIBUNARCHIVE-$(CONFIG_TAR) +=
+LIBUNARCHIVE-$(CONFIG_UNZIP) +=
+
+libraries-y+=$(LIBUNARCHIVE_DIR)$(LIBUNARCHIVE_AR)
+
+$(LIBUNARCHIVE_DIR)$(LIBUNARCHIVE_AR): $(patsubst %,$(LIBUNARCHIVE_DIR)%, $(LIBUNARCHIVE-y))
+ $(AR) -ro $@ $(patsubst %,$(LIBUNARCHIVE_DIR)%, $(LIBUNARCHIVE-y))
+
diff --git a/release/src/router/busybox/archival/libunarchive/archive_copy_file.c b/release/src/router/busybox/archival/libunarchive/archive_copy_file.c
new file mode 100644
index 00000000..675bc6ff
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/archive_copy_file.c
@@ -0,0 +1,44 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <unistd.h>
+
+#include "libbb.h"
+#include "unarchive.h"
+
+extern void archive_copy_file(const archive_handle_t *archive_handle, const int dst_fd)
+{
+ char buffer[512];
+ off_t chunksize = archive_handle->file_header->size;
+
+ while (chunksize != 0) {
+ size_t size;
+ if (chunksize > 512) {
+ size = 512;
+ } else {
+ size = chunksize;
+ }
+// archive_xread_all(archive_handle, buffer, size);
+ size = archive_xread(archive_handle, buffer, size);
+
+ if (write(dst_fd, buffer, size) != size) {
+ bb_error_msg_and_die ("Short write");
+ }
+ chunksize -= size;
+ }
+
+ return;
+}
diff --git a/release/src/router/busybox/archival/libunarchive/archive_xread.c b/release/src/router/busybox/archival/libunarchive/archive_xread.c
new file mode 100644
index 00000000..d63d0d5a
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/archive_xread.c
@@ -0,0 +1,33 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "unarchive.h"
+#include "libbb.h"
+
+extern ssize_t archive_xread(const archive_handle_t *archive_handle, unsigned char *buf, const size_t count)
+{
+ ssize_t size;
+
+ size = archive_handle->read(archive_handle->src_fd, buf, count);
+ if (size < 0) {
+ bb_perror_msg_and_die("Read error");
+ }
+
+ return(size);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/archive_xread_all.c b/release/src/router/busybox/archival/libunarchive/archive_xread_all.c
new file mode 100644
index 00000000..cfe046b2
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/archive_xread_all.c
@@ -0,0 +1,32 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "unarchive.h"
+#include "libbb.h"
+
+extern void archive_xread_all(const archive_handle_t *archive_handle, void *buf, const size_t count)
+{
+ ssize_t size;
+
+ size = archive_xread(archive_handle, buf, count);
+ if (size != count) {
+ bb_error_msg_and_die("Short read");
+ }
+ return;
+}
diff --git a/release/src/router/busybox/archival/libunarchive/archive_xread_all_eof.c b/release/src/router/busybox/archival/libunarchive/archive_xread_all_eof.c
new file mode 100644
index 00000000..23719cd7
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/archive_xread_all_eof.c
@@ -0,0 +1,32 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "unarchive.h"
+#include "libbb.h"
+
+extern ssize_t archive_xread_all_eof(archive_handle_t *archive_handle, unsigned char *buf, size_t count)
+{
+ ssize_t size;
+
+ size = archive_xread(archive_handle, buf, count);
+ if ((size != 0) && (size != count)) {
+ bb_perror_msg_and_die("Short read, read %d of %d", size, count);
+ }
+ return(size);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/archive_xread_char.c b/release/src/router/busybox/archival/libunarchive/archive_xread_char.c
new file mode 100644
index 00000000..4c665e15
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/archive_xread_char.c
@@ -0,0 +1,30 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "unarchive.h"
+#include "libbb.h"
+
+extern unsigned char archive_xread_char(const archive_handle_t *archive_handle)
+{
+ unsigned char tmp;
+
+ archive_xread(archive_handle, &tmp, 1);
+
+ return(tmp);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/check_header_gzip.c b/release/src/router/busybox/archival/libunarchive/check_header_gzip.c
new file mode 100644
index 00000000..13832c24
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/check_header_gzip.c
@@ -0,0 +1,57 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include "libbb.h"
+
+extern void check_header_gzip(int src_fd)
+{
+ union {
+ unsigned char raw[8];
+ struct {
+ unsigned char method;
+ unsigned char flags;
+ unsigned int mtime;
+ unsigned char xtra_flags;
+ unsigned char os_flags;
+ } formated;
+ } header;
+
+ bb_xread_all(src_fd, header.raw, 8);
+
+ /* Check the compression method */
+ if (header.formated.method != 8) {
+ bb_error_msg_and_die("Unknown compression method %d",
+ header.formated.method);
+ }
+
+ if (header.formated.flags & 0x04) {
+ /* bit 2 set: extra field present */
+ unsigned char extra_short;
+
+ extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8);
+ while (extra_short > 0) {
+ /* Ignore extra field */
+ bb_xread_char(src_fd);
+ extra_short--;
+ }
+ }
+
+ /* Discard original name if any */
+ if (header.formated.flags & 0x08) {
+ /* bit 3 set: original file name present */
+ while(bb_xread_char(src_fd) != 0);
+ }
+
+ /* Discard file comment if any */
+ if (header.formated.flags & 0x10) {
+ /* bit 4 set: file comment present */
+ while(bb_xread_char(src_fd) != 0);
+ }
+
+ /* Read the header checksum */
+ if (header.formated.flags & 0x02) {
+ bb_xread_char(src_fd);
+ bb_xread_char(src_fd);
+ }
+
+ return;
+}
diff --git a/release/src/router/busybox/archival/libunarchive/data_align.c b/release/src/router/busybox/archival/libunarchive/data_align.c
new file mode 100644
index 00000000..037242f2
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/data_align.c
@@ -0,0 +1,34 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <unistd.h>
+
+#include "libbb.h"
+#include "unarchive.h"
+
+extern void data_align(archive_handle_t *archive_handle, const unsigned short boundary)
+{
+ const unsigned short skip_amount = (boundary - (archive_handle->offset % boundary)) % boundary;
+
+ archive_handle->seek(archive_handle, skip_amount);
+
+ archive_handle->offset += skip_amount;
+
+ return;
+}
diff --git a/release/src/router/busybox/archival/libunarchive/data_extract_all.c b/release/src/router/busybox/archival/libunarchive/data_extract_all.c
new file mode 100644
index 00000000..bd264dd7
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/data_extract_all.c
@@ -0,0 +1,100 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <utime.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "libbb.h"
+#include "unarchive.h"
+
+extern void data_extract_all(archive_handle_t *archive_handle)
+{
+ file_header_t *file_header = archive_handle->file_header;
+ int dst_fd;
+ int res;
+
+ if (archive_handle->flags & ARCHIVE_CREATE_LEADING_DIRS) {
+ char *name = bb_xstrdup(file_header->name);
+ bb_make_directory (dirname(name), 0777, FILEUTILS_RECUR);
+ free(name);
+ }
+
+ /* Create the filesystem entry */
+ switch(file_header->mode & S_IFMT) {
+ case S_IFREG: {
+#ifdef CONFIG_CPIO
+ if (file_header->link_name && file_header->size == 0) {
+ /* hard link */
+ res = link(file_header->link_name, file_header->name);
+ if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
+ bb_perror_msg("Couldnt create hard link");
+ }
+ } else
+#endif
+ {
+ /* Regular file */
+ unlink(file_header->name);
+ dst_fd = bb_xopen(file_header->name, O_WRONLY | O_CREAT | O_EXCL);
+ archive_copy_file(archive_handle, dst_fd);
+ close(dst_fd);
+ }
+ break;
+ }
+ case S_IFDIR:
+ unlink(file_header->name);
+ res = mkdir(file_header->name, file_header->mode);
+ if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
+ bb_perror_msg("extract_archive: %s", file_header->name);
+ }
+ break;
+ case S_IFLNK:
+ /* Symlink */
+ unlink(file_header->name);
+ res = symlink(file_header->link_name, file_header->name);
+ if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
+ bb_perror_msg("Cannot create symlink from %s to '%s'", file_header->name, file_header->link_name);
+ }
+ break;
+ case S_IFSOCK:
+ case S_IFBLK:
+ case S_IFCHR:
+ case S_IFIFO:
+ unlink(file_header->name);
+ res = mknod(file_header->name, file_header->mode, file_header->device);
+ if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
+ bb_perror_msg("Cannot create node %s", file_header->name);
+ }
+ break;
+ default:
+ bb_error_msg_and_die("Unrecognised file type");
+ }
+
+ chmod(file_header->name, file_header->mode);
+ chown(file_header->name, file_header->uid, file_header->gid);
+
+ if (archive_handle->flags & ARCHIVE_PRESERVE_DATE) {
+ struct utimbuf t;
+ t.actime = t.modtime = file_header->mtime;
+ utime(file_header->name, &t);
+ }
+}
diff --git a/release/src/router/busybox/archival/libunarchive/data_extract_to_buffer.c b/release/src/router/busybox/archival/libunarchive/data_extract_to_buffer.c
new file mode 100644
index 00000000..db5521bc
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/data_extract_to_buffer.c
@@ -0,0 +1,28 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "libbb.h"
+#include "unarchive.h"
+
+extern void data_extract_to_buffer(archive_handle_t *archive_handle)
+{
+ const unsigned int size = archive_handle->file_header->size;
+
+ archive_handle->buffer = xmalloc(size + 1);
+
+ archive_xread_all(archive_handle, archive_handle->buffer, size);
+ archive_handle->buffer[size] = '\0';
+}
diff --git a/release/src/router/busybox/archival/libunarchive/data_extract_to_stdout.c b/release/src/router/busybox/archival/libunarchive/data_extract_to_stdout.c
new file mode 100644
index 00000000..8be2fa2e
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/data_extract_to_stdout.c
@@ -0,0 +1,22 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "unarchive.h"
+
+extern void data_extract_to_stdout(archive_handle_t *archive_handle)
+{
+ archive_copy_file(archive_handle, fileno(stdout));
+}
diff --git a/release/src/router/busybox/archival/libunarchive/data_skip.c b/release/src/router/busybox/archival/libunarchive/data_skip.c
new file mode 100644
index 00000000..b82c9065
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/data_skip.c
@@ -0,0 +1,27 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <sys/types.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include "unarchive.h"
+#include "libbb.h"
+
+extern void data_skip(archive_handle_t *archive_handle)
+{
+ archive_handle->seek(archive_handle, archive_handle->file_header->size);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/decompress_bunzip2.c b/release/src/router/busybox/archival/libunarchive/decompress_bunzip2.c
new file mode 100644
index 00000000..0164b77e
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/decompress_bunzip2.c
@@ -0,0 +1,1658 @@
+/*--
+ This file is a part of bzip2 and/or libbzip2, a program and
+ library for lossless, block-sorting data compression.
+
+ Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ 2. The origin of this software must not be misrepresented; you must
+ not claim that you wrote the original software. If you use this
+ software in a product, an acknowledgment in the product
+ documentation would be appreciated but is not required.
+
+ 3. Altered source versions must be plainly marked as such, and must
+ not be misrepresented as being the original software.
+
+ 4. The name of the author may not be used to endorse or promote
+ products derived from this software without specific prior written
+ permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ Julian Seward, Cambridge, UK.
+ jseward@acm.org
+ bzip2/libbzip2 version 1.0 of 21 March 2000
+
+ This program is based on (at least) the work of:
+ Mike Burrows
+ David Wheeler
+ Peter Fenwick
+ Alistair Moffat
+ Radford Neal
+ Ian H. Witten
+ Robert Sedgewick
+ Jon L. Bentley
+
+ For more information on these sources, see the manual.
+--*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+#include <unistd.h>
+
+#include "busybox.h"
+
+#define MTFA_SIZE 4096
+#define MTFL_SIZE 16
+#define BZ_N_GROUPS 6
+#define BZ_G_SIZE 50
+#define BZ_MAX_ALPHA_SIZE 258
+
+#define BZ_OK 0
+#define BZ_STREAM_END 4
+#define BZ_SEQUENCE_ERROR (-1)
+#define BZ_DATA_ERROR (-4)
+#define BZ_DATA_ERROR_MAGIC (-5)
+#define BZ_IO_ERROR (-6)
+#define BZ_UNEXPECTED_EOF (-7)
+
+#define BZ_RUNA 0
+#define BZ_RUNB 1
+
+#define BZ_MAX_UNUSED 5000
+#define FILE_NAME_LEN 1034
+/*-- states for decompression. --*/
+
+#define BZ_X_IDLE 1
+#define BZ_X_OUTPUT 2
+
+#define BZ_X_MAGIC_1 10
+#define BZ_X_MAGIC_2 11
+#define BZ_X_MAGIC_3 12
+#define BZ_X_MAGIC_4 13
+#define BZ_X_BLKHDR_1 14
+#define BZ_X_BLKHDR_2 15
+#define BZ_X_BLKHDR_3 16
+#define BZ_X_BLKHDR_4 17
+#define BZ_X_BLKHDR_5 18
+#define BZ_X_BLKHDR_6 19
+#define BZ_X_BCRC_1 20
+#define BZ_X_BCRC_2 21
+#define BZ_X_BCRC_3 22
+#define BZ_X_BCRC_4 23
+#define BZ_X_RANDBIT 24
+#define BZ_X_ORIGPTR_1 25
+#define BZ_X_ORIGPTR_2 26
+#define BZ_X_ORIGPTR_3 27
+#define BZ_X_MAPPING_1 28
+#define BZ_X_MAPPING_2 29
+#define BZ_X_SELECTOR_1 30
+#define BZ_X_SELECTOR_2 31
+#define BZ_X_SELECTOR_3 32
+#define BZ_X_CODING_1 33
+#define BZ_X_CODING_2 34
+#define BZ_X_CODING_3 35
+#define BZ_X_MTF_1 36
+#define BZ_X_MTF_2 37
+#define BZ_X_MTF_3 38
+#define BZ_X_MTF_4 39
+#define BZ_X_MTF_5 40
+#define BZ_X_MTF_6 41
+#define BZ_X_ENDHDR_2 42
+#define BZ_X_ENDHDR_3 43
+#define BZ_X_ENDHDR_4 44
+#define BZ_X_ENDHDR_5 45
+#define BZ_X_ENDHDR_6 46
+#define BZ_X_CCRC_1 47
+#define BZ_X_CCRC_2 48
+#define BZ_X_CCRC_3 49
+#define BZ_X_CCRC_4 50
+
+#define BZ_MAX_CODE_LEN 23
+#define OM_TEST 3
+
+typedef struct {
+ char *next_in;
+ unsigned int avail_in;
+
+ char *next_out;
+ unsigned int avail_out;
+
+ void *state;
+
+} bz_stream;
+
+#define BZ_MAX_UNUSED 5000
+typedef struct {
+ bz_stream strm;
+ int fd;
+ unsigned char initialisedOk;
+ char buf[BZ_MAX_UNUSED];
+ int lastErr;
+ int bufN;
+} bzFile;
+
+/*-- Structure holding all the decompression-side stuff. --*/
+typedef struct {
+ /* pointer back to the struct bz_stream */
+ bz_stream* strm;
+
+ /* state indicator for this stream */
+ int state;
+
+ /* for doing the final run-length decoding */
+ unsigned char state_out_ch;
+ int state_out_len;
+ unsigned char blockRandomised;
+ int rNToGo;
+ int rTPos;
+
+ /* the buffer for bit stream reading */
+ unsigned int bsBuff;
+ int bsLive;
+
+ /* misc administratium */
+ int blockSize100k;
+ int currBlockNo;
+
+ /* for undoing the Burrows-Wheeler transform */
+ int origPtr;
+ unsigned int tPos;
+ int k0;
+ int unzftab[256];
+ int nblock_used;
+ int cftab[257];
+ int cftabCopy[257];
+
+ /* for undoing the Burrows-Wheeler transform (FAST) */
+ unsigned int *tt;
+
+ /* stored and calculated CRCs */
+ unsigned int storedBlockCRC;
+ unsigned int storedCombinedCRC;
+ unsigned int calculatedBlockCRC;
+ unsigned int calculatedCombinedCRC;
+
+ /* map of bytes used in block */
+ int nInUse;
+ unsigned char inUse[256];
+ unsigned char inUse16[16];
+ unsigned char seqToUnseq[256];
+
+ /* for decoding the MTF values */
+ unsigned char mtfa [MTFA_SIZE];
+ unsigned char selector [2 + (900000 / BZ_G_SIZE)];
+ unsigned char selectorMtf[2 + (900000 / BZ_G_SIZE)];
+ unsigned char len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
+ int mtfbase[256 / MTFL_SIZE];
+
+ int limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
+ int base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
+ int perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
+ int minLens[BZ_N_GROUPS];
+
+ /* save area for scalars in the main decompress code */
+ int save_i;
+ int save_j;
+ int save_t;
+ int save_alphaSize;
+ int save_nGroups;
+ int save_nSelectors;
+ int save_EOB;
+ int save_groupNo;
+ int save_groupPos;
+ int save_nextSym;
+ int save_nblockMAX;
+ int save_nblock;
+ int save_es;
+ int save_N;
+ int save_curr;
+ int save_zt;
+ int save_zn;
+ int save_zvec;
+ int save_zj;
+ int save_gSel;
+ int save_gMinlen;
+ int *save_gLimit;
+ int *save_gBase;
+ int *save_gPerm;
+} DState;
+
+static int BZ2_rNums[512];
+static bzFile *bzf;
+static int bzerr = BZ_OK;
+
+static const unsigned int BZ2_crc32Table[256] = {
+
+ /*-- Ugly, innit? --*/
+
+ 0x00000000L, 0x04c11db7L, 0x09823b6eL, 0x0d4326d9L,
+ 0x130476dcL, 0x17c56b6bL, 0x1a864db2L, 0x1e475005L,
+ 0x2608edb8L, 0x22c9f00fL, 0x2f8ad6d6L, 0x2b4bcb61L,
+ 0x350c9b64L, 0x31cd86d3L, 0x3c8ea00aL, 0x384fbdbdL,
+ 0x4c11db70L, 0x48d0c6c7L, 0x4593e01eL, 0x4152fda9L,
+ 0x5f15adacL, 0x5bd4b01bL, 0x569796c2L, 0x52568b75L,
+ 0x6a1936c8L, 0x6ed82b7fL, 0x639b0da6L, 0x675a1011L,
+ 0x791d4014L, 0x7ddc5da3L, 0x709f7b7aL, 0x745e66cdL,
+ 0x9823b6e0L, 0x9ce2ab57L, 0x91a18d8eL, 0x95609039L,
+ 0x8b27c03cL, 0x8fe6dd8bL, 0x82a5fb52L, 0x8664e6e5L,
+ 0xbe2b5b58L, 0xbaea46efL, 0xb7a96036L, 0xb3687d81L,
+ 0xad2f2d84L, 0xa9ee3033L, 0xa4ad16eaL, 0xa06c0b5dL,
+ 0xd4326d90L, 0xd0f37027L, 0xddb056feL, 0xd9714b49L,
+ 0xc7361b4cL, 0xc3f706fbL, 0xceb42022L, 0xca753d95L,
+ 0xf23a8028L, 0xf6fb9d9fL, 0xfbb8bb46L, 0xff79a6f1L,
+ 0xe13ef6f4L, 0xe5ffeb43L, 0xe8bccd9aL, 0xec7dd02dL,
+ 0x34867077L, 0x30476dc0L, 0x3d044b19L, 0x39c556aeL,
+ 0x278206abL, 0x23431b1cL, 0x2e003dc5L, 0x2ac12072L,
+ 0x128e9dcfL, 0x164f8078L, 0x1b0ca6a1L, 0x1fcdbb16L,
+ 0x018aeb13L, 0x054bf6a4L, 0x0808d07dL, 0x0cc9cdcaL,
+ 0x7897ab07L, 0x7c56b6b0L, 0x71159069L, 0x75d48ddeL,
+ 0x6b93dddbL, 0x6f52c06cL, 0x6211e6b5L, 0x66d0fb02L,
+ 0x5e9f46bfL, 0x5a5e5b08L, 0x571d7dd1L, 0x53dc6066L,
+ 0x4d9b3063L, 0x495a2dd4L, 0x44190b0dL, 0x40d816baL,
+ 0xaca5c697L, 0xa864db20L, 0xa527fdf9L, 0xa1e6e04eL,
+ 0xbfa1b04bL, 0xbb60adfcL, 0xb6238b25L, 0xb2e29692L,
+ 0x8aad2b2fL, 0x8e6c3698L, 0x832f1041L, 0x87ee0df6L,
+ 0x99a95df3L, 0x9d684044L, 0x902b669dL, 0x94ea7b2aL,
+ 0xe0b41de7L, 0xe4750050L, 0xe9362689L, 0xedf73b3eL,
+ 0xf3b06b3bL, 0xf771768cL, 0xfa325055L, 0xfef34de2L,
+ 0xc6bcf05fL, 0xc27dede8L, 0xcf3ecb31L, 0xcbffd686L,
+ 0xd5b88683L, 0xd1799b34L, 0xdc3abdedL, 0xd8fba05aL,
+ 0x690ce0eeL, 0x6dcdfd59L, 0x608edb80L, 0x644fc637L,
+ 0x7a089632L, 0x7ec98b85L, 0x738aad5cL, 0x774bb0ebL,
+ 0x4f040d56L, 0x4bc510e1L, 0x46863638L, 0x42472b8fL,
+ 0x5c007b8aL, 0x58c1663dL, 0x558240e4L, 0x51435d53L,
+ 0x251d3b9eL, 0x21dc2629L, 0x2c9f00f0L, 0x285e1d47L,
+ 0x36194d42L, 0x32d850f5L, 0x3f9b762cL, 0x3b5a6b9bL,
+ 0x0315d626L, 0x07d4cb91L, 0x0a97ed48L, 0x0e56f0ffL,
+ 0x1011a0faL, 0x14d0bd4dL, 0x19939b94L, 0x1d528623L,
+ 0xf12f560eL, 0xf5ee4bb9L, 0xf8ad6d60L, 0xfc6c70d7L,
+ 0xe22b20d2L, 0xe6ea3d65L, 0xeba91bbcL, 0xef68060bL,
+ 0xd727bbb6L, 0xd3e6a601L, 0xdea580d8L, 0xda649d6fL,
+ 0xc423cd6aL, 0xc0e2d0ddL, 0xcda1f604L, 0xc960ebb3L,
+ 0xbd3e8d7eL, 0xb9ff90c9L, 0xb4bcb610L, 0xb07daba7L,
+ 0xae3afba2L, 0xaafbe615L, 0xa7b8c0ccL, 0xa379dd7bL,
+ 0x9b3660c6L, 0x9ff77d71L, 0x92b45ba8L, 0x9675461fL,
+ 0x8832161aL, 0x8cf30badL, 0x81b02d74L, 0x857130c3L,
+ 0x5d8a9099L, 0x594b8d2eL, 0x5408abf7L, 0x50c9b640L,
+ 0x4e8ee645L, 0x4a4ffbf2L, 0x470cdd2bL, 0x43cdc09cL,
+ 0x7b827d21L, 0x7f436096L, 0x7200464fL, 0x76c15bf8L,
+ 0x68860bfdL, 0x6c47164aL, 0x61043093L, 0x65c52d24L,
+ 0x119b4be9L, 0x155a565eL, 0x18197087L, 0x1cd86d30L,
+ 0x029f3d35L, 0x065e2082L, 0x0b1d065bL, 0x0fdc1becL,
+ 0x3793a651L, 0x3352bbe6L, 0x3e119d3fL, 0x3ad08088L,
+ 0x2497d08dL, 0x2056cd3aL, 0x2d15ebe3L, 0x29d4f654L,
+ 0xc5a92679L, 0xc1683bceL, 0xcc2b1d17L, 0xc8ea00a0L,
+ 0xd6ad50a5L, 0xd26c4d12L, 0xdf2f6bcbL, 0xdbee767cL,
+ 0xe3a1cbc1L, 0xe760d676L, 0xea23f0afL, 0xeee2ed18L,
+ 0xf0a5bd1dL, 0xf464a0aaL, 0xf9278673L, 0xfde69bc4L,
+ 0x89b8fd09L, 0x8d79e0beL, 0x803ac667L, 0x84fbdbd0L,
+ 0x9abc8bd5L, 0x9e7d9662L, 0x933eb0bbL, 0x97ffad0cL,
+ 0xafb010b1L, 0xab710d06L, 0xa6322bdfL, 0xa2f33668L,
+ 0xbcb4666dL, 0xb8757bdaL, 0xb5365d03L, 0xb1f740b4L
+};
+
+static void bz_rand_udp_mask(DState *s)
+{
+ if (s->rNToGo == 0) {
+ s->rNToGo = BZ2_rNums[s->rTPos];
+ s->rTPos++;
+ if (s->rTPos == 512) {
+ s->rTPos = 0;
+ }
+ }
+ s->rNToGo--;
+}
+
+static void BZ2_hbCreateDecodeTables(int *limit, int *base, int *perm, unsigned char *length, int minLen, int maxLen, int alphaSize )
+{
+ int pp, i, j, vec;
+
+ pp = 0;
+ for (i = minLen; i <= maxLen; i++) {
+ for (j = 0; j < alphaSize; j++) {
+ if (length[j] == i) {
+ perm[pp] = j;
+ pp++;
+ }
+ }
+ }
+
+ for (i = 0; i < BZ_MAX_CODE_LEN; i++) {
+ base[i] = 0;
+ }
+
+ for (i = 0; i < alphaSize; i++) {
+ base[length[i]+1]++;
+ }
+
+ for (i = 1; i < BZ_MAX_CODE_LEN; i++) {
+ base[i] += base[i-1];
+ }
+
+ for (i = 0; i < BZ_MAX_CODE_LEN; i++) {
+ limit[i] = 0;
+ }
+ vec = 0;
+
+ for (i = minLen; i <= maxLen; i++) {
+ vec += (base[i+1] - base[i]);
+ limit[i] = vec-1;
+ vec <<= 1;
+ }
+ for (i = minLen + 1; i <= maxLen; i++) {
+ base[i] = ((limit[i-1] + 1) << 1) - base[i];
+ }
+}
+
+
+static int get_bits(DState *s, int *vvv, char nnn)
+{
+ while (1) {
+ if (s->bsLive >= nnn) {
+ *vvv = (s->bsBuff >> (s->bsLive-nnn)) & ((1 << nnn)-1);
+ s->bsLive -= nnn;
+ break;
+ }
+ if (s->strm->avail_in == 0) {
+ return(FALSE);
+ }
+ s->bsBuff = (s->bsBuff << 8) | ((unsigned int) (*((unsigned char*)(s->strm->next_in))));
+ s->bsLive += 8;
+ s->strm->next_in++;
+ s->strm->avail_in--;
+ }
+ return(TRUE);
+}
+
+static int bz_get_fast(DState *s)
+{
+ int cccc;
+ s->tPos = s->tt[s->tPos];
+ cccc = (unsigned char)(s->tPos & 0xff);
+ s->tPos >>= 8;
+ return(cccc);
+}
+
+/*---------------------------------------------------*/
+static inline int BZ2_decompress(DState *s)
+{
+ int uc = 0;
+ int retVal;
+ int minLen, maxLen;
+
+ /* stuff that needs to be saved/restored */
+ int i;
+ int j;
+ int t;
+ int alphaSize;
+ int nGroups;
+ int nSelectors;
+ int EOB;
+ int groupNo;
+ int groupPos;
+ int nextSym;
+ int nblockMAX;
+ int nblock;
+ int es;
+ int N;
+ int curr;
+ int zt;
+ int zn;
+ int zvec;
+ int zj;
+ int gSel;
+ int gMinlen;
+ int *gLimit;
+ int *gBase;
+ int *gPerm;
+ int switch_val;
+
+ int get_mtf_val_init(void)
+ {
+ if (groupPos == 0) {
+ groupNo++;
+ if (groupNo >= nSelectors) {
+ retVal = BZ_DATA_ERROR;
+ return(FALSE);
+ }
+ groupPos = BZ_G_SIZE;
+ gSel = s->selector[groupNo];
+ gMinlen = s->minLens[gSel];
+ gLimit = &(s->limit[gSel][0]);
+ gPerm = &(s->perm[gSel][0]);
+ gBase = &(s->base[gSel][0]);
+ }
+ groupPos--;
+ zn = gMinlen;
+ return(TRUE);
+ }
+
+ if (s->state == BZ_X_MAGIC_1) {
+ /*initialise the save area*/
+ s->save_i = 0;
+ s->save_j = 0;
+ s->save_t = 0;
+ s->save_alphaSize = 0;
+ s->save_nGroups = 0;
+ s->save_nSelectors = 0;
+ s->save_EOB = 0;
+ s->save_groupNo = 0;
+ s->save_groupPos = 0;
+ s->save_nextSym = 0;
+ s->save_nblockMAX = 0;
+ s->save_nblock = 0;
+ s->save_es = 0;
+ s->save_N = 0;
+ s->save_curr = 0;
+ s->save_zt = 0;
+ s->save_zn = 0;
+ s->save_zvec = 0;
+ s->save_zj = 0;
+ s->save_gSel = 0;
+ s->save_gMinlen = 0;
+ s->save_gLimit = NULL;
+ s->save_gBase = NULL;
+ s->save_gPerm = NULL;
+ }
+
+ /*restore from the save area*/
+ i = s->save_i;
+ j = s->save_j;
+ t = s->save_t;
+ alphaSize = s->save_alphaSize;
+ nGroups = s->save_nGroups;
+ nSelectors = s->save_nSelectors;
+ EOB = s->save_EOB;
+ groupNo = s->save_groupNo;
+ groupPos = s->save_groupPos;
+ nextSym = s->save_nextSym;
+ nblockMAX = s->save_nblockMAX;
+ nblock = s->save_nblock;
+ es = s->save_es;
+ N = s->save_N;
+ curr = s->save_curr;
+ zt = s->save_zt;
+ zn = s->save_zn;
+ zvec = s->save_zvec;
+ zj = s->save_zj;
+ gSel = s->save_gSel;
+ gMinlen = s->save_gMinlen;
+ gLimit = s->save_gLimit;
+ gBase = s->save_gBase;
+ gPerm = s->save_gPerm;
+
+ retVal = BZ_OK;
+ switch_val = s->state;
+ switch (switch_val) {
+ case BZ_X_MAGIC_1:
+ s->state = BZ_X_MAGIC_1;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 'B') {
+ retVal = BZ_DATA_ERROR_MAGIC;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_MAGIC_2:
+ s->state = BZ_X_MAGIC_2;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 'Z') {
+ retVal = BZ_DATA_ERROR_MAGIC;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_MAGIC_3:
+ s->state = BZ_X_MAGIC_3;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 'h') {
+ retVal = BZ_DATA_ERROR_MAGIC;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_MAGIC_4:
+ s->state = BZ_X_MAGIC_4;
+ if (! get_bits(s, &s->blockSize100k, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if ((s->blockSize100k < '1') || (s->blockSize100k > '9')) {
+ retVal = BZ_DATA_ERROR_MAGIC;
+ goto save_state_and_return;
+ }
+ s->blockSize100k -= '0';
+
+ s->tt = xmalloc(s->blockSize100k * 100000 * sizeof(int));
+
+ case BZ_X_BLKHDR_1:
+ s->state = BZ_X_BLKHDR_1;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+
+ if (uc == 0x17) {
+ goto endhdr_2;
+ }
+ if (uc != 0x31) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_BLKHDR_2:
+ s->state = BZ_X_BLKHDR_2;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x41) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_BLKHDR_3:
+ s->state = BZ_X_BLKHDR_3;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x59) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_BLKHDR_4:
+ s->state = BZ_X_BLKHDR_4;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x26) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_BLKHDR_5:
+ s->state = BZ_X_BLKHDR_5;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x53) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_BLKHDR_6:
+ s->state = BZ_X_BLKHDR_6;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x59) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ s->currBlockNo++;
+ s->storedBlockCRC = 0;
+
+ case BZ_X_BCRC_1:
+ s->state = BZ_X_BCRC_1;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
+
+ case BZ_X_BCRC_2:
+ s->state = BZ_X_BCRC_2;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
+
+ case BZ_X_BCRC_3:
+ s->state = BZ_X_BCRC_3;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
+
+ case BZ_X_BCRC_4:
+ s->state = BZ_X_BCRC_4;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
+
+ case BZ_X_RANDBIT:
+ s->state = BZ_X_RANDBIT;
+ {
+ int tmp = s->blockRandomised;
+ const int ret = get_bits(s, &tmp, 1);
+ s->blockRandomised = tmp;
+ if (! ret) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ }
+
+ s->origPtr = 0;
+
+ case BZ_X_ORIGPTR_1:
+ s->state = BZ_X_ORIGPTR_1;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->origPtr = (s->origPtr << 8) | ((int)uc);
+
+ case BZ_X_ORIGPTR_2:
+ s->state = BZ_X_ORIGPTR_2;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->origPtr = (s->origPtr << 8) | ((int)uc);
+
+ case BZ_X_ORIGPTR_3:
+ s->state = BZ_X_ORIGPTR_3;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->origPtr = (s->origPtr << 8) | ((int)uc);
+
+ if (s->origPtr < 0) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ if (s->origPtr > 10 + 100000*s->blockSize100k) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ /*--- Receive the mapping table ---*/
+ case BZ_X_MAPPING_1:
+ for (i = 0; i < 16; i++) {
+ s->state = BZ_X_MAPPING_1;
+ if (! get_bits(s, &uc, 1)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc == 1) {
+ s->inUse16[i] = TRUE;
+ } else {
+ s->inUse16[i] = FALSE;
+ }
+ }
+
+ for (i = 0; i < 256; i++) {
+ s->inUse[i] = FALSE;
+ }
+
+ for (i = 0; i < 16; i++) {
+ if (s->inUse16[i]) {
+ for (j = 0; j < 16; j++) {
+ case BZ_X_MAPPING_2:
+ s->state = BZ_X_MAPPING_2;
+ if (! get_bits(s, &uc, 1)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc == 1) {
+ s->inUse[i * 16 + j] = TRUE;
+ }
+ }
+ }
+ }
+
+ s->nInUse = 0;
+ for (i = 0; i < 256; i++) {
+ if (s->inUse[i]) {
+ s->seqToUnseq[s->nInUse] = i;
+ s->nInUse++;
+ }
+ }
+ if (s->nInUse == 0) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ alphaSize = s->nInUse+2;
+
+ /*--- Now the selectors ---*/
+ case BZ_X_SELECTOR_1:
+ s->state = BZ_X_SELECTOR_1;
+ if (! get_bits(s, &nGroups, 3)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (nGroups < 2 || nGroups > 6) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_SELECTOR_2:
+ s->state = BZ_X_SELECTOR_2;
+ if (! get_bits(s, &nSelectors, 15)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (nSelectors < 1) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+
+
+ for (i = 0; i < nSelectors; i++) {
+ j = 0;
+ while (1) {
+ case BZ_X_SELECTOR_3:
+ s->state = BZ_X_SELECTOR_3;
+ if (! get_bits(s, &uc, 1)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc == 0) {
+ break;
+ }
+ j++;
+ if (j >= nGroups) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ }
+ s->selectorMtf[i] = j;
+ }
+
+ /*--- Undo the MTF values for the selectors. ---*/
+ {
+ unsigned char pos[BZ_N_GROUPS], tmp, v;
+ for (v = 0; v < nGroups; v++) {
+ pos[v] = v;
+ }
+ for (i = 0; i < nSelectors; i++) {
+ v = s->selectorMtf[i];
+ tmp = pos[v];
+ while (v > 0) {
+ pos[v] = pos[v-1];
+ v--;
+ }
+ pos[0] = tmp;
+ s->selector[i] = tmp;
+ }
+ }
+
+ /*--- Now the coding tables ---*/
+ for (t = 0; t < nGroups; t++) {
+ case BZ_X_CODING_1:
+ s->state = BZ_X_CODING_1;
+ if (! get_bits(s, &curr, 5)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ for (i = 0; i < alphaSize; i++) {
+ while (TRUE) {
+ if (curr < 1 || curr > 20) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_CODING_2:
+ s->state = BZ_X_CODING_2;
+ if (! get_bits(s, &uc, 1)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc == 0) {
+ break;
+ }
+
+ case BZ_X_CODING_3:
+ s->state = BZ_X_CODING_3;
+ if (! get_bits(s, &uc, 1)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc == 0) {
+ curr++;
+ } else {
+ curr--;
+ }
+ }
+ s->len[t][i] = curr;
+ }
+ }
+
+ /*--- Create the Huffman decoding tables ---*/
+ for (t = 0; t < nGroups; t++) {
+ minLen = 32;
+ maxLen = 0;
+ for (i = 0; i < alphaSize; i++) {
+ if (s->len[t][i] > maxLen) {
+ maxLen = s->len[t][i];
+ }
+ if (s->len[t][i] < minLen) {
+ minLen = s->len[t][i];
+ }
+ }
+
+ BZ2_hbCreateDecodeTables (
+ &(s->limit[t][0]),
+ &(s->base[t][0]),
+ &(s->perm[t][0]),
+ &(s->len[t][0]),
+ minLen, maxLen, alphaSize
+ );
+
+
+ s->minLens[t] = minLen;
+ }
+
+ /*--- Now the MTF values ---*/
+
+ EOB = s->nInUse+1;
+ nblockMAX = 100000 * s->blockSize100k;
+ groupNo = -1;
+ groupPos = 0;
+
+ for (i = 0; i <= 255; i++) {
+ s->unzftab[i] = 0;
+ }
+ /*-- MTF init --*/
+ {
+ int ii, jj, kk;
+ kk = MTFA_SIZE-1;
+ for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
+ for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
+ s->mtfa[kk] = (unsigned char)(ii * MTFL_SIZE + jj);
+ kk--;
+ }
+ s->mtfbase[ii] = kk + 1;
+ }
+ }
+ /*-- end MTF init --*/
+
+ nblock = 0;
+
+ if (! get_mtf_val_init()) {
+ goto save_state_and_return;
+ }
+ case BZ_X_MTF_1:
+ s->state = BZ_X_MTF_1;
+ if (! get_bits(s, &zvec, zn)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ while (1) {
+ if (zn > 20 /* the longest code */) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ if (zvec <= gLimit[zn]) {
+ break;
+ }
+ zn++;
+
+ case BZ_X_MTF_2:
+ s->state = BZ_X_MTF_2;
+ if (! get_bits(s, &zj, 1)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ zvec = (zvec << 1) | zj;
+ }
+ if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ nextSym = gPerm[zvec - gBase[zn]];
+
+ while (1) {
+ if (nextSym == EOB) {
+ break;
+ }
+
+ if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
+ es = -1;
+ N = 1;
+ do {
+ if (nextSym == BZ_RUNA) {
+ es = es + (0+1) * N;
+ } else {
+ if (nextSym == BZ_RUNB) {
+ es = es + (1+1) * N;
+ }
+ }
+ N = N * 2;
+ if (! get_mtf_val_init()) {
+ goto save_state_and_return;
+ }
+ case BZ_X_MTF_3:
+ s->state = BZ_X_MTF_3;
+ if (! get_bits(s, &zvec, zn)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ while (1) {
+ if (zn > 20 /* the longest code */) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ if (zvec <= gLimit[zn]) {
+ break;
+ }
+ zn++;
+
+ case BZ_X_MTF_4:
+ s->state = BZ_X_MTF_4;
+ if (! get_bits(s, &zj, 1)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ zvec = (zvec << 1) | zj;
+ }
+ if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+
+ }
+ nextSym = gPerm[zvec - gBase[zn]];
+ }
+ while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
+
+ es++;
+ uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
+ s->unzftab[uc] += es;
+
+ while (es > 0) {
+ if (nblock >= nblockMAX) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ s->tt[nblock] = (unsigned int)uc;
+ nblock++;
+ es--;
+ }
+ continue;
+ } else {
+ if (nblock >= nblockMAX) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ /*-- uc = MTF ( nextSym-1 ) --*/
+ {
+ int ii, jj, kk, pp, lno, off;
+ unsigned int nn;
+ nn = (unsigned int)(nextSym - 1);
+
+ if (nn < MTFL_SIZE) {
+ /* avoid general-case expense */
+ pp = s->mtfbase[0];
+ uc = s->mtfa[pp+nn];
+ while (nn > 3) {
+ int z = pp+nn;
+ s->mtfa[(z) ] = s->mtfa[(z)-1];
+ s->mtfa[(z)-1] = s->mtfa[(z)-2];
+ s->mtfa[(z)-2] = s->mtfa[(z)-3];
+ s->mtfa[(z)-3] = s->mtfa[(z)-4];
+ nn -= 4;
+ }
+ while (nn > 0) {
+ s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
+ }
+ s->mtfa[pp] = uc;
+ } else {
+ /* general case */
+ lno = nn / MTFL_SIZE;
+ off = nn % MTFL_SIZE;
+ pp = s->mtfbase[lno] + off;
+ uc = s->mtfa[pp];
+ while (pp > s->mtfbase[lno]) {
+ s->mtfa[pp] = s->mtfa[pp-1];
+ pp--;
+ }
+ s->mtfbase[lno]++;
+ while (lno > 0) {
+ s->mtfbase[lno]--;
+ s->mtfa[s->mtfbase[lno]] = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
+ lno--;
+ }
+ s->mtfbase[0]--;
+ s->mtfa[s->mtfbase[0]] = uc;
+ if (s->mtfbase[0] == 0) {
+ kk = MTFA_SIZE-1;
+ for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
+ for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
+ s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
+ kk--;
+ }
+ s->mtfbase[ii] = kk + 1;
+ }
+ }
+ }
+ }
+ /*-- end uc = MTF ( nextSym-1 ) --*/
+
+ s->unzftab[s->seqToUnseq[uc]]++;
+ s->tt[nblock] = (unsigned int)(s->seqToUnseq[uc]);
+ nblock++;
+
+ if (! get_mtf_val_init()) {
+ goto save_state_and_return;
+ }
+ case BZ_X_MTF_5:
+ s->state = BZ_X_MTF_5;
+ if (! get_bits(s, &zvec, zn)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ while (1) {
+ if (zn > 20 /* the longest code */) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ if (zvec <= gLimit[zn]) {
+ break;
+ }
+ zn++;
+
+ case BZ_X_MTF_6:
+ s->state = BZ_X_MTF_6;
+ if (! get_bits(s, &zj, 1)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ zvec = (zvec << 1) | zj;
+ }
+ if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ nextSym = gPerm[zvec - gBase[zn]];
+ continue;
+ }
+ }
+
+ /* Now we know what nblock is, we can do a better sanity
+ check on s->origPtr.
+ */
+ if (s->origPtr < 0 || s->origPtr >= nblock) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ s->state_out_len = 0;
+ s->state_out_ch = 0;
+ s->calculatedBlockCRC = 0xffffffffL;
+ s->state = BZ_X_OUTPUT;
+
+ /*-- Set up cftab to facilitate generation of T^(-1) --*/
+ s->cftab[0] = 0;
+ for (i = 1; i <= 256; i++) {
+ s->cftab[i] = s->unzftab[i-1];
+ }
+ for (i = 1; i <= 256; i++) {
+ s->cftab[i] += s->cftab[i-1];
+ }
+
+ /*-- compute the T^(-1) vector --*/
+ for (i = 0; i < nblock; i++) {
+ uc = (unsigned char)(s->tt[i] & 0xff);
+ s->tt[s->cftab[uc]] |= (i << 8);
+ s->cftab[uc]++;
+ }
+
+ s->tPos = s->tt[s->origPtr] >> 8;
+ s->nblock_used = 0;
+ if (s->blockRandomised) {
+ s->rNToGo = 0;
+ s->rTPos = 0;
+ s->k0 = bz_get_fast(s);
+
+ s->nblock_used++;
+ bz_rand_udp_mask(s);
+ s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
+ } else {
+ s->k0 = bz_get_fast(s);
+ s->nblock_used++;
+ }
+
+ retVal = BZ_OK;
+ goto save_state_and_return;
+
+endhdr_2:
+ case BZ_X_ENDHDR_2:
+ s->state = BZ_X_ENDHDR_2;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x72) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_ENDHDR_3:
+ s->state = BZ_X_ENDHDR_3;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x45) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_ENDHDR_4:
+ s->state = BZ_X_ENDHDR_4;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x38) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_ENDHDR_5:
+ s->state = BZ_X_ENDHDR_5;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x50) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+
+ case BZ_X_ENDHDR_6:
+ s->state = BZ_X_ENDHDR_6;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ if (uc != 0x90) {
+ retVal = BZ_DATA_ERROR;
+ goto save_state_and_return;
+ }
+ s->storedCombinedCRC = 0;
+
+ case BZ_X_CCRC_1:
+ s->state = BZ_X_CCRC_1;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
+ case BZ_X_CCRC_2:
+ s->state = BZ_X_CCRC_2;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
+
+ case BZ_X_CCRC_3:
+ s->state = BZ_X_CCRC_3;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
+
+ case BZ_X_CCRC_4:
+ s->state = BZ_X_CCRC_4;
+ if (! get_bits(s, &uc, 8)) {
+ retVal = BZ_OK;
+ goto save_state_and_return;
+ }
+ s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
+
+ s->state = BZ_X_IDLE;
+ retVal = BZ_STREAM_END;
+ goto save_state_and_return;
+
+ }
+
+save_state_and_return:
+ s->save_i = i;
+ s->save_j = j;
+ s->save_t = t;
+ s->save_alphaSize = alphaSize;
+ s->save_nGroups = nGroups;
+ s->save_nSelectors = nSelectors;
+ s->save_EOB = EOB;
+ s->save_groupNo = groupNo;
+ s->save_groupPos = groupPos;
+ s->save_nextSym = nextSym;
+ s->save_nblockMAX = nblockMAX;
+ s->save_nblock = nblock;
+ s->save_es = es;
+ s->save_N = N;
+ s->save_curr = curr;
+ s->save_zt = zt;
+ s->save_zn = zn;
+ s->save_zvec = zvec;
+ s->save_zj = zj;
+ s->save_gSel = gSel;
+ s->save_gMinlen = gMinlen;
+ s->save_gLimit = gLimit;
+ s->save_gBase = gBase;
+ s->save_gPerm = gPerm;
+
+ return retVal;
+}
+
+extern void BZ2_bzReadClose(void)
+{
+ if (bzf->initialisedOk) {
+ bz_stream *strm = &(bzf->strm);
+ DState *s;
+ if (strm == NULL) {
+ return;
+ }
+ s = strm->state;
+ if ((s == NULL) || (s->strm != strm)) {
+ return;
+ }
+ free(s->tt);
+ free(strm->state);
+ strm->state = NULL;
+ return;
+ }
+ free(bzf);
+}
+
+static void unRLE_obuf_to_output_FAST(DState *s)
+{
+ unsigned char k1;
+
+ if (s->blockRandomised) {
+ while (1) {
+ /* try to finish existing run */
+ while (1) {
+ if (s->strm->avail_out == 0) {
+ return;
+ }
+ if (s->state_out_len == 0) {
+ break;
+ }
+ *((unsigned char *)(s->strm->next_out)) = s->state_out_ch;
+ s->calculatedBlockCRC = (s->calculatedBlockCRC << 8) ^
+ BZ2_crc32Table[(s->calculatedBlockCRC >> 24) ^
+ ((unsigned char)s->state_out_ch)];
+ s->state_out_len--;
+ s->strm->next_out++;
+ s->strm->avail_out--;
+ }
+
+ /* can a new run be started? */
+ if (s->nblock_used == s->save_nblock+1) {
+ return;
+ }
+ s->state_out_len = 1;
+ s->state_out_ch = s->k0;
+ k1 = bz_get_fast(s);
+ bz_rand_udp_mask(s);
+ k1 ^= ((s->rNToGo == 1) ? 1 : 0);
+ s->nblock_used++;
+ if (s->nblock_used == s->save_nblock+1) {
+ continue;
+ }
+ if (k1 != s->k0) {
+ s->k0 = k1;
+ continue;
+ }
+
+ s->state_out_len = 2;
+ k1 = bz_get_fast(s);
+ bz_rand_udp_mask(s);
+ k1 ^= ((s->rNToGo == 1) ? 1 : 0);
+ s->nblock_used++;
+ if (s->nblock_used == s->save_nblock+1) {
+ continue;
+ }
+ if (k1 != s->k0) {
+ s->k0 = k1;
+ continue;
+ }
+ s->state_out_len = 3;
+ k1 = bz_get_fast(s);
+ bz_rand_udp_mask(s);
+ k1 ^= ((s->rNToGo == 1) ? 1 : 0);
+ s->nblock_used++;
+ if (s->nblock_used == s->save_nblock+1) {
+ continue;
+ }
+ if (k1 != s->k0) {
+ s->k0 = k1;
+ continue;
+ }
+
+ k1 = bz_get_fast(s);
+ bz_rand_udp_mask(s);
+ k1 ^= ((s->rNToGo == 1) ? 1 : 0);
+ s->nblock_used++;
+ s->state_out_len = ((int)k1) + 4;
+ s->k0 = bz_get_fast(s);
+ bz_rand_udp_mask(s);
+ s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
+ s->nblock_used++;
+ }
+ } else {
+ /* restore */
+ unsigned int c_calculatedBlockCRC = s->calculatedBlockCRC;
+ unsigned char c_state_out_ch = s->state_out_ch;
+ int c_state_out_len = s->state_out_len;
+ int c_nblock_used = s->nblock_used;
+ int c_k0 = s->k0;
+ unsigned int *c_tt = s->tt;
+ unsigned int c_tPos = s->tPos;
+ char *cs_next_out = s->strm->next_out;
+ unsigned int cs_avail_out = s->strm->avail_out;
+ /* end restore */
+
+ int s_save_nblockPP = s->save_nblock+1;
+
+ while (1) {
+ /* try to finish existing run */
+ if (c_state_out_len > 0) {
+ while (TRUE) {
+ if (cs_avail_out == 0) {
+ goto return_notr;
+ }
+ if (c_state_out_len == 1) {
+ break;
+ }
+ *((unsigned char *)(cs_next_out)) = c_state_out_ch;
+ c_calculatedBlockCRC = (c_calculatedBlockCRC << 8) ^
+ BZ2_crc32Table[(c_calculatedBlockCRC >> 24) ^
+ ((unsigned char)c_state_out_ch)];
+ c_state_out_len--;
+ cs_next_out++;
+ cs_avail_out--;
+ }
+s_state_out_len_eq_one:
+ {
+ if (cs_avail_out == 0) {
+ c_state_out_len = 1;
+ goto return_notr;
+ }
+ *((unsigned char *)(cs_next_out)) = c_state_out_ch;
+ c_calculatedBlockCRC = (c_calculatedBlockCRC << 8) ^
+ BZ2_crc32Table[(c_calculatedBlockCRC >> 24) ^
+ ((unsigned char)c_state_out_ch)];
+ cs_next_out++;
+ cs_avail_out--;
+ }
+ }
+ /* can a new run be started? */
+ if (c_nblock_used == s_save_nblockPP) {
+ c_state_out_len = 0; goto return_notr;
+ }
+ c_state_out_ch = c_k0;
+ c_tPos = c_tt[c_tPos];
+ k1 = (unsigned char)(c_tPos & 0xff);
+ c_tPos >>= 8;
+
+ c_nblock_used++;
+
+ if (k1 != c_k0) {
+ c_k0 = k1;
+ goto s_state_out_len_eq_one;
+ }
+
+ if (c_nblock_used == s_save_nblockPP) {
+ goto s_state_out_len_eq_one;
+ }
+
+ c_state_out_len = 2;
+ c_tPos = c_tt[c_tPos];
+ k1 = (unsigned char)(c_tPos & 0xff);
+ c_tPos >>= 8;
+
+ c_nblock_used++;
+ if (c_nblock_used == s_save_nblockPP) {
+ continue;
+ }
+ if (k1 != c_k0) {
+ c_k0 = k1;
+ continue;
+ }
+
+ c_state_out_len = 3;
+ c_tPos = c_tt[c_tPos];
+ k1 = (unsigned char)(c_tPos & 0xff);
+ c_tPos >>= 8;
+
+ c_nblock_used++;
+ if (c_nblock_used == s_save_nblockPP) {
+ continue;
+ }
+ if (k1 != c_k0) {
+ c_k0 = k1;
+ continue;
+ }
+
+ c_tPos = c_tt[c_tPos];
+ k1 = (unsigned char)(c_tPos & 0xff);
+ c_tPos >>= 8;
+
+ c_nblock_used++;
+ c_state_out_len = ((int)k1) + 4;
+
+ c_tPos = c_tt[c_tPos];
+ c_k0 = (unsigned char)(c_tPos & 0xff);
+ c_tPos >>= 8;
+
+ c_nblock_used++;
+ }
+
+return_notr:
+
+ /* save */
+ s->calculatedBlockCRC = c_calculatedBlockCRC;
+ s->state_out_ch = c_state_out_ch;
+ s->state_out_len = c_state_out_len;
+ s->nblock_used = c_nblock_used;
+ s->k0 = c_k0;
+ s->tt = c_tt;
+ s->tPos = c_tPos;
+ s->strm->next_out = cs_next_out;
+ s->strm->avail_out = cs_avail_out;
+ /* end save */
+ }
+}
+static inline
+int BZ2_bzDecompress(bz_stream *strm)
+{
+ DState* s;
+ s = strm->state;
+
+ while (1) {
+ if (s->state == BZ_X_IDLE) {
+ return BZ_SEQUENCE_ERROR;
+ }
+ if (s->state == BZ_X_OUTPUT) {
+ unRLE_obuf_to_output_FAST(s);
+ if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
+ s->calculatedBlockCRC = ~(s->calculatedBlockCRC);
+ if (s->calculatedBlockCRC != s->storedBlockCRC) {
+ return BZ_DATA_ERROR;
+ }
+ s->calculatedCombinedCRC = (s->calculatedCombinedCRC << 1) | (s->calculatedCombinedCRC >> 31);
+ s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
+ s->state = BZ_X_BLKHDR_1;
+ } else {
+ return BZ_OK;
+ }
+ }
+ if (s->state >= BZ_X_MAGIC_1) {
+ int r = BZ2_decompress(s);
+ if (r == BZ_STREAM_END) {
+ if (s->calculatedCombinedCRC != s->storedCombinedCRC) {
+ return BZ_DATA_ERROR;
+ }
+ return r;
+ }
+ if (s->state != BZ_X_OUTPUT) {
+ return r;
+ }
+ }
+ }
+
+ return(0); /*NOTREACHED*/
+}
+
+extern ssize_t read_bz2(int fd, void *buf, size_t count)
+{
+ int n, ret;
+
+ bzerr = BZ_OK;
+ if (count == 0) {
+ return(0);
+ }
+ bzf->strm.avail_out = count;
+ bzf->strm.next_out = buf;
+
+ while (1) {
+ if (bzf->strm.avail_in == 0) {
+ n = bb_xread(bzf->fd, bzf->buf, BZ_MAX_UNUSED);
+ if (n == 0) {
+ break;
+ }
+ bzf->bufN = n;
+ bzf->strm.avail_in = bzf->bufN;
+ bzf->strm.next_in = bzf->buf;
+ }
+
+ ret = BZ2_bzDecompress(&(bzf->strm));
+
+ if ((ret != BZ_OK) && (ret != BZ_STREAM_END)) {
+ bb_error_msg_and_die("Error decompressing");
+ }
+
+ if (ret == BZ_STREAM_END) {
+ bzerr = BZ_STREAM_END;
+ return(count - bzf->strm.avail_out);
+ }
+ if (bzf->strm.avail_out == 0) {
+ bzerr = BZ_OK;
+ return(count);
+ }
+ }
+ return(0);
+}
+
+extern void BZ2_bzReadOpen(int fd, void *unused, int nUnused)
+{
+ DState *s;
+
+ bzf = xmalloc(sizeof(bzFile));
+ bzf->initialisedOk = FALSE;
+ bzf->fd = fd;
+ bzf->bufN = 0;
+
+ s = xmalloc(sizeof(DState));
+ s->strm = &bzf->strm;
+ s->state = BZ_X_MAGIC_1;
+ s->bsLive = 0;
+ s->bsBuff = 0;
+ s->calculatedCombinedCRC = 0;
+ s->tt = NULL;
+ s->currBlockNo = 0;
+ bzf->strm.state = s;
+
+ while (nUnused > 0) {
+ bzf->buf[bzf->bufN] = *((unsigned char *)(unused));
+ bzf->bufN++;
+ unused = ((void *)( 1 + ((unsigned char *)(unused)) ));
+ nUnused--;
+ }
+ bzf->strm.avail_in = bzf->bufN;
+ bzf->strm.next_in = bzf->buf;
+
+ bzf->initialisedOk = TRUE;
+
+ return;
+}
+
+extern unsigned char uncompressStream(int src_fd, int dst_fd)
+{
+ unsigned char unused[BZ_MAX_UNUSED];
+ unsigned char *unusedTmp;
+ unsigned char obuf[5000];
+ int nread;
+ int nUnused;
+ int streamNo;
+ int i;
+
+ nUnused = 0;
+ streamNo = 0;
+
+ while(1) {
+ BZ2_bzReadOpen(src_fd, unused, nUnused);
+ streamNo++;
+
+ while (bzerr == BZ_OK) {
+ nread = read_bz2(src_fd, obuf, 5000);
+ if (bzerr == BZ_DATA_ERROR_MAGIC) {
+ bb_error_msg_and_die("invalid magic");
+ }
+ if (((bzerr == BZ_OK) || (bzerr == BZ_STREAM_END)) && (nread > 0)) {
+ if (write(dst_fd, obuf, nread) != nread) {
+ BZ2_bzReadClose();
+ bb_perror_msg_and_die("Couldnt write to file");
+ }
+ }
+ }
+ nUnused = bzf->strm.avail_in;
+ unusedTmp = bzf->strm.next_in;
+
+ for (i = 0; i < nUnused; i++) {
+ unused[i] = unusedTmp[i];
+ }
+ BZ2_bzReadClose();
+ if (nUnused == 0) {
+ break;
+ }
+ }
+
+ close(src_fd);
+ if (dst_fd != fileno(stdout)) {
+ close(dst_fd);
+ }
+ return TRUE;
+}
+
diff --git a/release/src/router/busybox/archival/libunarchive/filter_accept_all.c b/release/src/router/busybox/archival/libunarchive/filter_accept_all.c
new file mode 100644
index 00000000..baf9e4b7
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/filter_accept_all.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2002 by Glenn McGrath
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <fnmatch.h>
+#include <stdlib.h>
+
+#include "unarchive.h"
+
+/* Accept any non-null name, its not really a filter at all */
+extern char filter_accept_all(archive_handle_t *archive_handle)
+{
+ if (archive_handle->file_header->name) {
+ return(EXIT_SUCCESS);
+ } else {
+ return(EXIT_FAILURE);
+ }
+}
diff --git a/release/src/router/busybox/archival/libunarchive/filter_accept_list.c b/release/src/router/busybox/archival/libunarchive/filter_accept_list.c
new file mode 100644
index 00000000..e1c4827b
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/filter_accept_list.c
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2002 by Glenn McGrath
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <fnmatch.h>
+#include <stdlib.h>
+
+#include "unarchive.h"
+
+/*
+ * Accept names that are in the accept list, ignoring reject list.
+ */
+extern char filter_accept_list(archive_handle_t *archive_handle)
+{
+ if (find_list_entry(archive_handle->accept, archive_handle->file_header->name)) {
+ return(EXIT_SUCCESS);
+ } else {
+ return(EXIT_FAILURE);
+ }
+}
diff --git a/release/src/router/busybox/archival/libunarchive/filter_accept_list_reassign.c b/release/src/router/busybox/archival/libunarchive/filter_accept_list_reassign.c
new file mode 100644
index 00000000..d9bee79d
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/filter_accept_list_reassign.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2002 by Glenn McGrath
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "libbb.h"
+#include "unarchive.h"
+
+/*
+ * Reassign the subarchive metadata parser based on the filename extension
+ * e.g. if its a .tar.gz modify archive_handle->sub_archive to process a .tar.gz
+ * or if its a .tar.bz2 make archive_handle->sub_archive handle that
+ */
+extern char filter_accept_list_reassign(archive_handle_t *archive_handle)
+{
+ /* Check the file entry is in the accept list */
+ if (find_list_entry(archive_handle->accept, archive_handle->file_header->name)) {
+ const char *name_ptr;
+
+ /* Extract the last 2 extensions */
+ name_ptr = strrchr(archive_handle->file_header->name, '.');
+
+ /* Modify the subarchive handler based on the extension */
+#ifdef CONFIG_FEATURE_DEB_TAR_GZ
+ if (strcmp(name_ptr, ".gz") == 0) {
+ archive_handle->sub_archive->read = read;
+ archive_handle->action_data_subarchive = get_header_tar_gz;
+ return(EXIT_SUCCESS);
+ }
+#endif
+#ifdef CONFIG_FEATURE_DEB_TAR_BZ2
+ if (strcmp(name_ptr, ".bz2") == 0) {
+ archive_handle->sub_archive->read = read_bz2;
+ BZ2_bzReadOpen(archive_handle->src_fd, NULL, 0);
+ archive_handle->action_data_subarchive = get_header_tar;
+ return(EXIT_SUCCESS);
+ }
+#endif
+ }
+ return(EXIT_FAILURE);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/filter_accept_reject_list.c b/release/src/router/busybox/archival/libunarchive/filter_accept_reject_list.c
new file mode 100644
index 00000000..657f7a0b
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/filter_accept_reject_list.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2002 by Glenn McGrath
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <fnmatch.h>
+#include <stdlib.h>
+
+#include "unarchive.h"
+
+/*
+ * Accept names that are in the accept list and not in the reject list
+ */
+extern char filter_accept_reject_list(archive_handle_t *archive_handle)
+{
+ const char *key = archive_handle->file_header->name;
+ const llist_t *accept_entry = find_list_entry(archive_handle->accept, key);
+ const llist_t *reject_entry = find_list_entry(archive_handle->reject, key);
+
+ /* If the key is in a reject list fail */
+ if (reject_entry) {
+ return(EXIT_FAILURE);
+ }
+
+ /* Fail if an accept list was specified and the key wasnt in there */
+ if (archive_handle->accept && (accept_entry == NULL)) {
+ return(EXIT_FAILURE);
+ }
+
+ /* Accepted */
+ return(EXIT_SUCCESS);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/find_list_entry.c b/release/src/router/busybox/archival/libunarchive/find_list_entry.c
new file mode 100644
index 00000000..7ed9e332
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/find_list_entry.c
@@ -0,0 +1,30 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <fnmatch.h>
+#include <stdlib.h>
+#include "unarchive.h"
+
+extern const llist_t *find_list_entry(const llist_t *list, const char *filename)
+{
+ while (list) {
+ if (fnmatch(list->data, filename, 0) == 0) {
+ return(list);
+ }
+ list = list->link;
+ }
+ return(NULL);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/get_header_ar.c b/release/src/router/busybox/archival/libunarchive/get_header_ar.c
new file mode 100644
index 00000000..6c576a8d
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/get_header_ar.c
@@ -0,0 +1,122 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "unarchive.h"
+#include "libbb.h"
+
+extern char get_header_ar(archive_handle_t *archive_handle)
+{
+ file_header_t *typed = archive_handle->file_header;
+ union {
+ char raw[60];
+ struct {
+ char name[16];
+ char date[12];
+ char uid[6];
+ char gid[6];
+ char mode[8];
+ char size[10];
+ char magic[2];
+ } formated;
+ } ar;
+#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
+ static char *ar_long_names;
+ static unsigned int ar_long_name_size;
+#endif
+
+ /* dont use bb_xread as we want to handle the error ourself */
+ if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
+ /* End Of File */
+ return(EXIT_FAILURE);
+ }
+
+ /* Some ar entries have a trailing '\n' after the previous data entry */
+ if (ar.raw[0] == '\n') {
+ /* fix up the header, we started reading 1 byte too early */
+ memmove(ar.raw, &ar.raw[1], 59);
+ ar.raw[59] = bb_xread_char(archive_handle->src_fd);
+ archive_handle->offset++;
+ }
+ archive_handle->offset += 60;
+
+ /* align the headers based on the header magic */
+ if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
+ bb_error_msg_and_die("Invalid ar header");
+ }
+
+ typed->mode = strtol(ar.formated.mode, NULL, 8);
+ typed->mtime = atoi(ar.formated.date);
+ typed->uid = atoi(ar.formated.uid);
+ typed->gid = atoi(ar.formated.gid);
+ typed->size = atoi(ar.formated.size);
+
+ /* long filenames have '/' as the first character */
+ if (ar.formated.name[0] == '/') {
+#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
+ if (ar.formated.name[1] == '/') {
+ /* If the second char is a '/' then this entries data section
+ * stores long filename for multiple entries, they are stored
+ * in static variable long_names for use in future entries */
+ ar_long_name_size = typed->size;
+ ar_long_names = xmalloc(ar_long_name_size);
+ bb_xread_all(archive_handle->src_fd, ar_long_names, ar_long_name_size);
+ archive_handle->offset += ar_long_name_size;
+ /* This ar entries data section only contained filenames for other records
+ * they are stored in the static ar_long_names for future reference */
+ return (get_header_ar(archive_handle)); /* Return next header */
+ } else if (ar.formated.name[1] == ' ') {
+ /* This is the index of symbols in the file for compilers */
+ data_skip(archive_handle);
+ return (get_header_ar(archive_handle)); /* Return next header */
+ } else {
+ /* The number after the '/' indicates the offset in the ar data section
+ (saved in variable long_name) that conatains the real filename */
+ const unsigned int long_offset = atoi(&ar.formated.name[1]);
+ if (long_offset >= ar_long_name_size) {
+ bb_error_msg_and_die("Cant resolve long filename");
+ }
+ typed->name = bb_xstrdup(ar_long_names + long_offset);
+ }
+#else
+ bb_error_msg_and_die("long filenames not supported");
+#endif
+ } else {
+ /* short filenames */
+ typed->name = bb_xstrndup(ar.formated.name, 16);
+ }
+
+ typed->name[strcspn(typed->name, " /")] = '\0';
+
+ if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
+ archive_handle->action_header(typed);
+ if (archive_handle->sub_archive) {
+ while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS);
+ } else {
+ archive_handle->action_data(archive_handle);
+ }
+ } else {
+ data_skip(archive_handle);
+ }
+
+ archive_handle->offset += typed->size + 1;
+
+ return(EXIT_SUCCESS);
+}
+
diff --git a/release/src/router/busybox/archival/libunarchive/get_header_cpio.c b/release/src/router/busybox/archival/libunarchive/get_header_cpio.c
new file mode 100644
index 00000000..975e2a4b
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/get_header_cpio.c
@@ -0,0 +1,189 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "unarchive.h"
+#include "libbb.h"
+
+typedef struct hardlinks_s {
+ file_header_t *entry;
+ int inode;
+ struct hardlinks_s *next;
+} hardlinks_t;
+
+extern char get_header_cpio(archive_handle_t *archive_handle)
+{
+ static hardlinks_t *saved_hardlinks = NULL;
+ static unsigned short pending_hardlinks = 0;
+ file_header_t *file_header = archive_handle->file_header;
+ char cpio_header[110];
+ int namesize;
+ char dummy[16];
+ int major, minor, nlink, inode;
+ char extract_flag;
+
+ if (pending_hardlinks) { /* Deal with any pending hardlinks */
+ hardlinks_t *tmp;
+ hardlinks_t *oldtmp;
+
+ tmp = saved_hardlinks;
+ oldtmp = NULL;
+
+ while (tmp) {
+ bb_error_msg_and_die("need to fix this\n");
+ if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
+ file_header = tmp->entry;
+ if (oldtmp) {
+ oldtmp->next = tmp->next; /* Remove item from linked list */
+ } else {
+ saved_hardlinks = tmp->next;
+ }
+ free(tmp);
+ continue;
+ }
+ oldtmp = tmp;
+ tmp = tmp->next;
+ }
+ pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
+ }
+
+ /* There can be padding before archive header */
+ data_align(archive_handle, 4);
+
+ if (archive_xread_all_eof(archive_handle, cpio_header, 110) == 0) {
+ return(EXIT_FAILURE);
+ }
+ archive_handle->offset += 110;
+
+ if (strncmp(&cpio_header[0], "07070", 5) != 0) {
+ printf("cpio header is %x-%x-%x-%x-%x\n",
+ cpio_header[0],
+ cpio_header[1],
+ cpio_header[2],
+ cpio_header[3],
+ cpio_header[4]);
+ bb_error_msg_and_die("Unsupported cpio format");
+ }
+
+ if ((cpio_header[5] != '1') && (cpio_header[5] != '2')) {
+ bb_error_msg_and_die("Unsupported cpio format, use newc or crc");
+ }
+
+ {
+ unsigned long tmpsize;
+ sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
+ dummy, &inode, (unsigned int*)&file_header->mode,
+ (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid,
+ &nlink, &file_header->mtime, &tmpsize,
+ dummy, &major, &minor, &namesize, dummy);
+ file_header->size = tmpsize;
+ }
+
+ file_header->name = (char *) xmalloc(namesize + 1);
+ archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */
+ file_header->name[namesize] = '\0';
+ archive_handle->offset += namesize;
+
+ /* Update offset amount and skip padding before file contents */
+ data_align(archive_handle, 4);
+
+ if (strcmp(file_header->name, "TRAILER!!!") == 0) {
+ printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */
+ if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
+ hardlinks_t *tmp = saved_hardlinks;
+ hardlinks_t *oldtmp = NULL;
+ while (tmp) {
+ bb_error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
+ oldtmp = tmp;
+ tmp = tmp->next;
+ free (oldtmp->entry->name);
+ free (oldtmp->entry);
+ free (oldtmp);
+ }
+ saved_hardlinks = NULL;
+ pending_hardlinks = 0;
+ }
+ return(EXIT_FAILURE);
+ }
+
+ if (S_ISLNK(file_header->mode)) {
+ file_header->link_name = (char *) xmalloc(file_header->size + 1);
+ archive_xread_all(archive_handle, file_header->link_name, file_header->size);
+ file_header->link_name[file_header->size] = '\0';
+ archive_handle->offset += file_header->size;
+ file_header->size = 0; /* Stop possible seeks in future */
+ }
+ if (nlink > 1 && !S_ISDIR(file_header->mode)) {
+ if (file_header->size == 0) { /* Put file on a linked list for later */
+ hardlinks_t *new = xmalloc(sizeof(hardlinks_t));
+ new->next = saved_hardlinks;
+ new->inode = inode;
+ new->entry = file_header;
+ saved_hardlinks = new;
+ return(EXIT_SUCCESS); // Skip this one
+ } else { /* Found the file with data in */
+ hardlinks_t *tmp = saved_hardlinks;
+ pending_hardlinks = 1;
+ while (tmp) {
+ if (tmp->inode == inode) {
+ tmp->entry->link_name = bb_xstrdup(file_header->name);
+ nlink--;
+ }
+ tmp = tmp->next;
+ }
+ if (nlink > 1) {
+ bb_error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
+ }
+ }
+ }
+ file_header->device = (major << 8) | minor;
+
+ extract_flag = FALSE;
+ if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
+ struct stat statbuf;
+
+ extract_flag = TRUE;
+
+ /* Check if the file already exists */
+ if (lstat (file_header->name, &statbuf) == 0) {
+ if ((archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) || (statbuf.st_mtime < file_header->mtime)) {
+ /* Remove file if flag set or its older than the file to be extracted */
+ if (unlink(file_header->name) == -1) {
+ bb_perror_msg_and_die("Couldnt remove old file");
+ }
+ } else {
+ if (! archive_handle->flags & ARCHIVE_EXTRACT_QUIET) {
+ bb_error_msg("%s not created: newer or same age file exists", file_header->name);
+ }
+ extract_flag = FALSE;
+ }
+ }
+ archive_handle->action_header(file_header);
+ }
+
+ archive_handle->action_header(file_header);
+ if (extract_flag) {
+ archive_handle->action_data(archive_handle);
+ } else {
+ data_skip(archive_handle);
+ }
+ archive_handle->offset += file_header->size;
+ return (EXIT_SUCCESS);
+}
+
diff --git a/release/src/router/busybox/archival/libunarchive/get_header_tar.c b/release/src/router/busybox/archival/libunarchive/get_header_tar.c
new file mode 100644
index 00000000..33cb75df
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/get_header_tar.c
@@ -0,0 +1,188 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "unarchive.h"
+#include "libbb.h"
+
+#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
+static char *longname = NULL;
+static char *linkname = NULL;
+#endif
+
+extern char get_header_tar(archive_handle_t *archive_handle)
+{
+ file_header_t *file_header = archive_handle->file_header;
+ union {
+ unsigned char raw[512];
+ struct {
+ char name[100]; /* 0-99 */
+ char mode[8]; /* 100-107 */
+ char uid[8]; /* 108-115 */
+ char gid[8]; /* 116-123 */
+ char size[12]; /* 124-135 */
+ char mtime[12]; /* 136-147 */
+ char chksum[8]; /* 148-155 */
+ char typeflag; /* 156-156 */
+ char linkname[100]; /* 157-256 */
+ char magic[6]; /* 257-262 */
+ char version[2]; /* 263-264 */
+ char uname[32]; /* 265-296 */
+ char gname[32]; /* 297-328 */
+ char devmajor[8]; /* 329-336 */
+ char devminor[8]; /* 337-344 */
+ char prefix[155]; /* 345-499 */
+ char padding[12]; /* 500-512 */
+ } formated;
+ } tar;
+ long sum = 0;
+ long i;
+ char *tmp;
+
+ /* Align header */
+ data_align(archive_handle, 512);
+
+ if (archive_xread(archive_handle, tar.raw, 512) != 512) {
+ /* Assume end of file */
+ return(EXIT_FAILURE);
+ }
+ archive_handle->offset += 512;
+
+ /* If there is no filename its an empty header */
+ if (tar.formated.name[0] == 0) {
+ return(EXIT_SUCCESS);
+ }
+
+ /* Check header has valid magic, "ustar" is for the proper tar
+ * 0's are for the old tar format
+ */
+ if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
+#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
+ if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
+#endif
+ bb_error_msg_and_die("Invalid tar magic");
+ }
+ /* Do checksum on headers */
+ for (i = 0; i < 148 ; i++) {
+ sum += tar.raw[i];
+ }
+ sum += ' ' * 8;
+ for (i = 156; i < 512 ; i++) {
+ sum += tar.raw[i];
+ }
+ if (sum != strtol(tar.formated.chksum, NULL, 8)) {
+ bb_error_msg("Invalid tar header checksum");
+ return(EXIT_FAILURE);
+ }
+
+#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
+ if (longname) {
+ file_header->name = longname;
+ longname = NULL;
+ }
+ else if (linkname) {
+ file_header->name = linkname;
+ linkname = NULL;
+ } else
+#endif
+ if (tar.formated.prefix[0] == 0) {
+ file_header->name = strdup(tar.formated.name);
+ } else {
+ file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
+ }
+ tmp = last_char_is(archive_handle->file_header->name, '/');
+ if (tmp) {
+ *tmp = '\0';
+ }
+
+ file_header->mode = strtol(tar.formated.mode, NULL, 8);
+ file_header->uid = strtol(tar.formated.uid, NULL, 8);
+ file_header->gid = strtol(tar.formated.gid, NULL, 8);
+ file_header->size = strtol(tar.formated.size, NULL, 8);
+ file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
+ file_header->link_name = (tar.formated.linkname[0] != '\0') ?
+ bb_xstrdup(tar.formated.linkname) : NULL;
+ file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
+ strtol(tar.formated.devminor, NULL, 8));
+
+#if defined CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY || defined CONFIG_FEATURE_TAR_GNU_EXTENSIONS
+ /* Fix mode, used by the old format */
+ switch (tar.formated.typeflag) {
+# ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
+ case 0:
+ case '0':
+ file_header->mode |= S_IFREG;
+ break;
+ case '1':
+ bb_error_msg("WARNING: Converting hard link to symlink");
+ case '2':
+ file_header->mode |= S_IFLNK;
+ break;
+ case '3':
+ file_header->mode |= S_IFCHR;
+ break;
+ case '4':
+ file_header->mode |= S_IFBLK;
+ break;
+ case '5':
+ file_header->mode |= S_IFDIR;
+ break;
+ case '6':
+ file_header->mode |= S_IFIFO;
+ break;
+# endif
+# ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
+ case 'L': {
+ longname = xmalloc(file_header->size + 1);
+ archive_xread_all(archive_handle, longname, file_header->size);
+ longname[file_header->size] = '\0';
+ archive_handle->offset += file_header->size;
+
+ return(get_header_tar(archive_handle));
+ }
+ case 'K': {
+ linkname = xmalloc(file_header->size + 1);
+ archive_xread_all(archive_handle, linkname, file_header->size);
+ linkname[file_header->size] = '\0';
+ archive_handle->offset += file_header->size;
+
+ file_header->name = linkname;
+ return(get_header_tar(archive_handle));
+ }
+ case 'D':
+ case 'M':
+ case 'N':
+ case 'S':
+ case 'V':
+ bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
+# endif
+ }
+#endif
+ if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
+ archive_handle->action_header(archive_handle->file_header);
+ archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
+ archive_handle->action_data(archive_handle);
+ archive_handle->passed = llist_add_to(archive_handle->passed, archive_handle->file_header->name);
+ } else {
+ data_skip(archive_handle);
+ }
+ archive_handle->offset += file_header->size;
+
+ return(EXIT_SUCCESS);
+}
+
diff --git a/release/src/router/busybox/archival/libunarchive/get_header_tar_bz2.c b/release/src/router/busybox/archival/libunarchive/get_header_tar_bz2.c
new file mode 100644
index 00000000..b49ccae1
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/get_header_tar_bz2.c
@@ -0,0 +1,42 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <sys/types.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "libbb.h"
+#include "unarchive.h"
+
+extern char get_header_tar_bz2(archive_handle_t *archive_handle)
+{
+ BZ2_bzReadOpen(archive_handle->src_fd, NULL, 0);
+
+ archive_handle->read = read_bz2;
+ archive_handle->seek = seek_by_char;
+
+ archive_handle->offset = 0;
+ while (get_header_tar(archive_handle) == EXIT_SUCCESS);
+
+ /* Cleanup */
+ BZ2_bzReadClose();
+
+ /* Can only do one tar.bz2 per archive */
+ return(EXIT_FAILURE);
+}
+
diff --git a/release/src/router/busybox/archival/libunarchive/get_header_tar_gz.c b/release/src/router/busybox/archival/libunarchive/get_header_tar_gz.c
new file mode 100644
index 00000000..7792432a
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/get_header_tar_gz.c
@@ -0,0 +1,49 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdlib.h>
+
+#include "libbb.h"
+#include "unarchive.h"
+
+extern char get_header_tar_gz(archive_handle_t *archive_handle)
+{
+ unsigned char magic[2];
+
+ archive_xread_all(archive_handle, &magic, 2);
+ if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
+ bb_error_msg_and_die("Invalid gzip magic");
+ }
+
+ check_header_gzip(archive_handle->src_fd);
+
+ GZ_gzReadOpen(archive_handle->src_fd, 0, 0);
+
+ archive_handle->read = read_gz;
+ archive_handle->seek = seek_by_char;
+
+ archive_handle->offset = 0;
+ while (get_header_tar(archive_handle) == EXIT_SUCCESS);
+
+ /* Cleanup */
+ GZ_gzReadClose();
+
+ check_trailer_gzip(archive_handle->src_fd);
+
+ /* Can only do one tar.bz2 per archive */
+ return(EXIT_FAILURE);
+}
+
diff --git a/release/src/router/busybox/archival/libunarchive/header_list.c b/release/src/router/busybox/archival/libunarchive/header_list.c
new file mode 100644
index 00000000..5849a762
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/header_list.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+#include "unarchive.h"
+
+extern void header_list(const file_header_t *file_header)
+{
+ puts(file_header->name);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/header_skip.c b/release/src/router/busybox/archival/libunarchive/header_skip.c
new file mode 100644
index 00000000..4430178f
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/header_skip.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+#include "unarchive.h"
+
+extern void header_skip(const file_header_t *file_header)
+{
+ return;
+}
diff --git a/release/src/router/busybox/archival/libunarchive/header_verbose_list.c b/release/src/router/busybox/archival/libunarchive/header_verbose_list.c
new file mode 100644
index 00000000..6739dd39
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/header_verbose_list.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include "libbb.h"
+#include "unarchive.h"
+
+extern void header_verbose_list(const file_header_t *file_header)
+{
+ struct tm *mtime = localtime(&file_header->mtime);
+
+ printf("%s %d/%d%10u %4u-%02u-%02u %02u:%02u:%02u %s",
+ bb_mode_string(file_header->mode),
+ file_header->uid,
+ file_header->gid,
+ (unsigned int) file_header->size,
+ 1900 + mtime->tm_year,
+ 1 + mtime->tm_mon,
+ mtime->tm_mday,
+ mtime->tm_hour,
+ mtime->tm_min,
+ mtime->tm_sec,
+ file_header->name);
+
+ if (file_header->link_name) {
+ printf(" -> %s", file_header->link_name);
+ }
+ /* putchar isnt used anywhere else i dont think */
+ puts("");
+}
diff --git a/release/src/router/busybox/archival/libunarchive/init_handle.c b/release/src/router/busybox/archival/libunarchive/init_handle.c
new file mode 100644
index 00000000..2659aa3e
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/init_handle.c
@@ -0,0 +1,37 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <unistd.h>
+#include <string.h>
+#include "libbb.h"
+#include "unarchive.h"
+
+archive_handle_t *init_handle(void)
+{
+ archive_handle_t *archive_handle;
+
+ /* Initialise default values */
+ archive_handle = xmalloc(sizeof(archive_handle_t));
+ memset(archive_handle, 0, sizeof(archive_handle_t));
+ archive_handle->file_header = xmalloc(sizeof(file_header_t));
+ archive_handle->action_header = header_skip;
+ archive_handle->action_data = data_skip;
+ archive_handle->filter = filter_accept_all;
+ archive_handle->read = bb_full_read;
+ archive_handle->seek = seek_by_jump;
+
+ return(archive_handle);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/seek_by_char.c b/release/src/router/busybox/archival/libunarchive/seek_by_char.c
new file mode 100644
index 00000000..f33935cb
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/seek_by_char.c
@@ -0,0 +1,25 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "unarchive.h"
+
+extern void seek_by_char(const archive_handle_t *archive_handle, const unsigned int amount)
+{
+ unsigned int i;
+ for (i = 0; i < amount; i++) {
+ archive_xread_char(archive_handle);
+ }
+}
diff --git a/release/src/router/busybox/archival/libunarchive/seek_by_jump.c b/release/src/router/busybox/archival/libunarchive/seek_by_jump.c
new file mode 100644
index 00000000..578870d9
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/seek_by_jump.c
@@ -0,0 +1,35 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <sys/types.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "libbb.h"
+#include "unarchive.h"
+
+extern void seek_by_jump(const archive_handle_t *archive_handle, const unsigned int amount)
+{
+ if (lseek(archive_handle->src_fd, (off_t) amount, SEEK_CUR) == (off_t) -1) {
+#ifdef CONFIG_FEATURE_UNARCHIVE_TAPE
+ if (errno == ESPIPE) {
+ seek_by_char(archive_handle, amount);
+ } else
+#endif
+ bb_perror_msg_and_die("Seek failure");
+ }
+}
diff --git a/release/src/router/busybox/archival/libunarchive/uncompress.c b/release/src/router/busybox/archival/libunarchive/uncompress.c
new file mode 100644
index 00000000..9851ca39
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/uncompress.c
@@ -0,0 +1,298 @@
+#include "config.h"
+#include "libbb.h"
+
+#if defined CONFIG_UNCOMPRESS || defined CONFIG_FEATURE_GUNZIP_UNCOMPRESS
+
+/* uncompress for busybox -- (c) 2002 Robert Griebl
+ *
+ * based on the original compress42.c source
+ * (see disclaimer below)
+ */
+
+
+/* (N)compress42.c - File compression ala IEEE Computer, Mar 1992.
+ *
+ * Authors:
+ * Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
+ * Jim McKie (decvax!mcvax!jim)
+ * Steve Davies (decvax!vax135!petsd!peora!srd)
+ * Ken Turkowski (decvax!decwrl!turtlevax!ken)
+ * James A. Woods (decvax!ihnp4!ames!jaw)
+ * Joe Orost (decvax!vax135!petsd!joe)
+ * Dave Mack (csu@alembic.acs.com)
+ * Peter Jannesen, Network Communication Systems
+ * (peter@ncs.nl)
+ *
+ * marc@suse.de : a small security fix for a buffer overflow
+ *
+ * [... History snipped ...]
+ *
+ */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+/* Defailt input buffer size */
+#define IBUFSIZ 2048
+
+/* Default output buffer size */
+#define OBUFSIZ 2048
+
+/* Defines for third byte of header */
+#define MAGIC_1 (char_type)'\037' /* First byte of compressed file */
+#define MAGIC_2 (char_type)'\235' /* Second byte of compressed file */
+#define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */
+ /* Masks 0x20 and 0x40 are free. */
+ /* I think 0x20 should mean that there is */
+ /* a fourth header byte (for expansion). */
+#define BLOCK_MODE 0x80 /* Block compresssion if table is full and */
+ /* compression rate is dropping flush tables */
+ /* the next two codes should not be changed lightly, as they must not */
+ /* lie within the contiguous general code space. */
+#define FIRST 257 /* first free entry */
+#define CLEAR 256 /* table clear output code */
+
+#define INIT_BITS 9 /* initial number of bits/code */
+
+
+/* machine variants which require cc -Dmachine: pdp11, z8000, DOS */
+#define FAST
+
+#define HBITS 17 /* 50% occupancy */
+#define HSIZE (1<<HBITS)
+#define HMASK (HSIZE-1)
+#define HPRIME 9941
+#define BITS 16
+#undef MAXSEG_64K
+#define MAXCODE(n) (1L << (n))
+
+/* Block compress mode -C compatible with 2.0 */
+int block_mode = BLOCK_MODE;
+
+/* user settable max # bits/code */
+int maxbits = BITS;
+
+/* Exitcode of compress (-1 no file compressed) */
+int exit_code = -1;
+
+/* Input buffer */
+unsigned char inbuf[IBUFSIZ + 64];
+
+/* Output buffer */
+unsigned char outbuf[OBUFSIZ + 2048];
+
+
+long int htab[HSIZE];
+unsigned short codetab[HSIZE];
+
+#define htabof(i) htab[i]
+#define codetabof(i) codetab[i]
+#define tab_prefixof(i) codetabof(i)
+#define tab_suffixof(i) ((unsigned char *)(htab))[i]
+#define de_stack ((unsigned char *)&(htab[HSIZE-1]))
+#define clear_htab() memset(htab, -1, sizeof(htab))
+#define clear_tab_prefixof() memset(codetab, 0, 256);
+
+
+/*
+ * Decompress stdin to stdout. This routine adapts to the codes in the
+ * file building the "string" table on-the-fly; requiring no table to
+ * be stored in the compressed file. The tables used herein are shared
+ * with those of the compress() routine. See the definitions above.
+ */
+
+extern int uncompress(int fd_in, int fd_out)
+{
+ unsigned char *stackp;
+ long int code;
+ int finchar;
+ long int oldcode;
+ long int incode;
+ int inbits;
+ int posbits;
+ int outpos;
+ int insize;
+ int bitmask;
+ long int free_ent;
+ long int maxcode;
+ long int maxmaxcode;
+ int n_bits;
+ int rsize = 0;
+
+ insize = 0;
+
+ inbuf[0] = bb_xread_char(fd_in);
+
+ maxbits = inbuf[0] & BIT_MASK;
+ block_mode = inbuf[0] & BLOCK_MODE;
+ maxmaxcode = MAXCODE(maxbits);
+
+ if (maxbits > BITS) {
+ bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits,
+ BITS);
+ return -1;
+ }
+
+ maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
+ bitmask = (1 << n_bits) - 1;
+ oldcode = -1;
+ finchar = 0;
+ outpos = 0;
+ posbits = 0 << 3;
+
+ free_ent = ((block_mode) ? FIRST : 256);
+
+ /* As above, initialize the first 256 entries in the table. */
+ clear_tab_prefixof();
+
+ for (code = 255; code >= 0; --code) {
+ tab_suffixof(code) = (unsigned char) code;
+ }
+
+ do {
+ resetbuf:;
+ {
+ int i;
+ int e;
+ int o;
+
+ e = insize - (o = (posbits >> 3));
+
+ for (i = 0; i < e; ++i)
+ inbuf[i] = inbuf[i + o];
+
+ insize = e;
+ posbits = 0;
+ }
+
+ if (insize < (int) sizeof(inbuf) - IBUFSIZ) {
+ rsize = read(fd_in, inbuf + insize, IBUFSIZ);
+ insize += rsize;
+ }
+
+ inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
+ (insize << 3) - (n_bits - 1));
+
+ while (inbits > posbits) {
+ if (free_ent > maxcode) {
+ posbits =
+ ((posbits - 1) +
+ ((n_bits << 3) -
+ (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
+ ++n_bits;
+ if (n_bits == maxbits) {
+ maxcode = maxmaxcode;
+ } else {
+ maxcode = MAXCODE(n_bits) - 1;
+ }
+ bitmask = (1 << n_bits) - 1;
+ goto resetbuf;
+ }
+ {
+ unsigned char *p = &inbuf[posbits >> 3];
+
+ code =
+ ((((long) (p[0])) | ((long) (p[1]) << 8) |
+ ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
+ }
+ posbits += n_bits;
+
+
+ if (oldcode == -1) {
+ outbuf[outpos++] = (unsigned char) (finchar =
+ (int) (oldcode = code));
+ continue;
+ }
+
+ if (code == CLEAR && block_mode) {
+ clear_tab_prefixof();
+ free_ent = FIRST - 1;
+ posbits =
+ ((posbits - 1) +
+ ((n_bits << 3) -
+ (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
+ maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
+ bitmask = (1 << n_bits) - 1;
+ goto resetbuf;
+ }
+
+ incode = code;
+ stackp = de_stack;
+
+ /* Special case for KwKwK string. */
+ if (code >= free_ent) {
+ if (code > free_ent) {
+ unsigned char *p;
+
+ posbits -= n_bits;
+ p = &inbuf[posbits >> 3];
+
+ bb_error_msg
+ ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
+ insize, posbits, p[-1], p[0], p[1], p[2], p[3],
+ (posbits & 07));
+ bb_error_msg("uncompress: corrupt input");
+ return -1;
+ }
+
+ *--stackp = (unsigned char) finchar;
+ code = oldcode;
+ }
+
+ /* Generate output characters in reverse order */
+ while ((long int) code >= (long int) 256) {
+ *--stackp = tab_suffixof(code);
+ code = tab_prefixof(code);
+ }
+
+ *--stackp = (unsigned char) (finchar = tab_suffixof(code));
+
+ /* And put them out in forward order */
+ {
+ int i;
+
+ if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) {
+ do {
+ if (i > OBUFSIZ - outpos) {
+ i = OBUFSIZ - outpos;
+ }
+
+ if (i > 0) {
+ memcpy(outbuf + outpos, stackp, i);
+ outpos += i;
+ }
+
+ if (outpos >= OBUFSIZ) {
+ write(fd_out, outbuf, outpos);
+ outpos = 0;
+ }
+ stackp += i;
+ } while ((i = (de_stack - stackp)) > 0);
+ } else {
+ memcpy(outbuf + outpos, stackp, i);
+ outpos += i;
+ }
+ }
+
+ /* Generate the new entry. */
+ if ((code = free_ent) < maxmaxcode) {
+ tab_prefixof(code) = (unsigned short) oldcode;
+ tab_suffixof(code) = (unsigned char) finchar;
+ free_ent = code + 1;
+ }
+
+ /* Remember previous code. */
+ oldcode = incode;
+ }
+
+ } while (rsize > 0);
+
+ if (outpos > 0) {
+ write(fd_out, outbuf, outpos);
+ }
+
+ return 0;
+}
+
+
+#endif
diff --git a/release/src/router/busybox/archival/libunarchive/unpack_ar_archive.c b/release/src/router/busybox/archival/libunarchive/unpack_ar_archive.c
new file mode 100644
index 00000000..e8f113bc
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/unpack_ar_archive.c
@@ -0,0 +1,34 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+#include "unarchive.h"
+#include "busybox.h"
+
+extern void unpack_ar_archive(archive_handle_t *ar_archive)
+{
+ char magic[7];
+
+ archive_xread_all(ar_archive, magic, 7);
+ if (strncmp(magic, "!<arch>", 7) != 0) {
+ bb_error_msg_and_die("Invalid ar magic");
+ }
+ ar_archive->offset += 7;
+
+ while (get_header_ar(ar_archive) == EXIT_SUCCESS);
+}
diff --git a/release/src/router/busybox/archival/libunarchive/unzip.c b/release/src/router/busybox/archival/libunarchive/unzip.c
new file mode 100644
index 00000000..2b16db3c
--- /dev/null
+++ b/release/src/router/busybox/archival/libunarchive/unzip.c
@@ -0,0 +1,1040 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * gunzip implementation for busybox
+ *
+ * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
+ *
+ * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
+ * based on gzip sources
+ *
+ * Adjusted further by Erik Andersen <andersen@codepoet.org> to support
+ * files as well as stdin/stdout, and to generally behave itself wrt
+ * command line handling.
+ *
+ * General cleanup to better adhere to the style guide and make use of standard
+ * busybox functions by Glenn McGrath <bug1@optushome.com.au>
+ *
+ * read_gz interface + associated hacking by Laurence Anderson
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
+ * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
+ * Copyright (C) 1992-1993 Jean-loup Gailly
+ * The unzip code was written and put in the public domain by Mark Adler.
+ * Portions of the lzw code are derived from the public domain 'compress'
+ * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
+ * Ken Turkowski, Dave Mack and Peter Jannesen.
+ *
+ * See the license_msg below and the file COPYING for the software license.
+ * See the file algorithm.doc for the compression algorithms and file formats.
+ */
+
+#if 0
+static char *license_msg[] = {
+ " Copyright (C) 1992-1993 Jean-loup Gailly",
+ " This program is free software; you can redistribute it and/or modify",
+ " it under the terms of the GNU General Public License as published by",
+ " the Free Software Foundation; either version 2, or (at your option)",
+ " any later version.",
+ "",
+ " This program is distributed in the hope that it will be useful,",
+ " but WITHOUT ANY WARRANTY; without even the implied warranty of",
+ " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
+ " GNU General Public License for more details.",
+ "",
+ " You should have received a copy of the GNU General Public License",
+ " along with this program; if not, write to the Free Software",
+ " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
+ 0
+};
+#endif
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "config.h"
+#include "busybox.h"
+#include "unarchive.h"
+
+typedef struct huft_s {
+ unsigned char e; /* number of extra bits or operation */
+ unsigned char b; /* number of bits in this code or subcode */
+ union {
+ unsigned short n; /* literal, length base, or distance base */
+ struct huft_s *t; /* pointer to next level of table */
+ } v;
+} huft_t;
+
+static int gunzip_src_fd;
+unsigned int gunzip_bytes_out; /* number of output bytes */
+static unsigned int gunzip_outbuf_count; /* bytes in output buffer */
+
+/* gunzip_window size--must be a power of two, and
+ * at least 32K for zip's deflate method */
+static const int gunzip_wsize = 0x8000;
+static unsigned char *gunzip_window;
+
+static unsigned int *gunzip_crc_table;
+unsigned int gunzip_crc;
+
+/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
+#define BMAX 16 /* maximum bit length of any code (16 for explode) */
+#define N_MAX 288 /* maximum number of codes in any set */
+
+/* bitbuffer */
+static unsigned int gunzip_bb; /* bit buffer */
+static unsigned char gunzip_bk; /* bits in bit buffer */
+
+/* These control the size of the bytebuffer */
+#define BYTEBUFFER_MAX 0x8000
+static unsigned char *bytebuffer = NULL;
+static unsigned int bytebuffer_offset = 0;
+static unsigned int bytebuffer_size = 0;
+
+static const unsigned short mask_bits[] = {
+ 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
+ 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
+};
+
+/* Copy lengths for literal codes 257..285 */
+static const unsigned short cplens[] = {
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
+ 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
+};
+
+/* note: see note #13 above about the 258 in this list. */
+/* Extra bits for literal codes 257..285 */
+static const unsigned char cplext[] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5,
+ 5, 5, 5, 0, 99, 99
+}; /* 99==invalid */
+
+/* Copy offsets for distance codes 0..29 */
+static const unsigned short cpdist[] = {
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
+ 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
+};
+
+/* Extra bits for distance codes */
+static const unsigned char cpdext[] = {
+ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
+ 11, 11, 12, 12, 13, 13
+};
+
+/* Tables for deflate from PKZIP's appnote.txt. */
+/* Order of the bit length code lengths */
+static const unsigned char border[] = {
+ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
+};
+
+static void fill_bytebuffer(void)
+{
+ if (bytebuffer_offset >= bytebuffer_size) {
+ /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
+ * to the front of the bytebuffer, leave 4 bytes free at end of tail
+ * so we can easily top up buffer in check_trailer_gzip() */
+ bytebuffer_size = 4 + bb_xread(gunzip_src_fd, &bytebuffer[4], BYTEBUFFER_MAX - 8);
+ bytebuffer_offset = 4;
+ }
+}
+
+static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required)
+{
+ while (*current < required) {
+ fill_bytebuffer();
+ bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current;
+ bytebuffer_offset++;
+ *current += 8;
+ }
+ return(bitbuffer);
+}
+
+static void make_gunzip_crc_table(void)
+{
+ const unsigned int poly = 0xedb88320; /* polynomial exclusive-or pattern */
+ unsigned short i; /* counter for all possible eight bit values */
+
+ /* initial shift register value */
+ gunzip_crc = 0xffffffffL;
+ gunzip_crc_table = (unsigned int *) malloc(256 * sizeof(unsigned int));
+
+ /* Compute and print table of CRC's, five per line */
+ for (i = 0; i < 256; i++) {
+ unsigned int table_entry; /* crc shift register */
+ unsigned char k; /* byte being shifted into crc apparatus */
+
+ table_entry = i;
+ /* The idea to initialize the register with the byte instead of
+ * zero was stolen from Haruhiko Okumura's ar002
+ */
+ for (k = 8; k; k--) {
+ if (table_entry & 1) {
+ table_entry = (table_entry >> 1) ^ poly;
+ } else {
+ table_entry >>= 1;
+ }
+ }
+ gunzip_crc_table[i] = table_entry;
+ }
+}
+
+/*
+ * Free the malloc'ed tables built by huft_build(), which makes a linked
+ * list of the tables it made, with the links in a dummy first entry of
+ * each table.
+ * t: table to free
+ */
+static int huft_free(huft_t * t)
+{
+ huft_t *p;
+ huft_t *q;
+
+ /* Go through linked list, freeing from the malloced (t[-1]) address. */
+ p = t;
+ while (p != (huft_t *) NULL) {
+ q = (--p)->v.t;
+ free((char *) p);
+ p = q;
+ }
+ return 0;
+}
+
+/* Given a list of code lengths and a maximum table size, make a set of
+ * tables to decode that set of codes. Return zero on success, one if
+ * the given code set is incomplete (the tables are still built in this
+ * case), two if the input is invalid (all zero length codes or an
+ * oversubscribed set of lengths), and three if not enough memory.
+ *
+ * b: code lengths in bits (all assumed <= BMAX)
+ * n: number of codes (assumed <= N_MAX)
+ * s: number of simple-valued codes (0..s-1)
+ * d: list of base values for non-simple codes
+ * e: list of extra bits for non-simple codes
+ * t: result: starting table
+ * m: maximum lookup bits, returns actual
+ */
+static int huft_build(unsigned int *b, const unsigned int n,
+ const unsigned int s, const unsigned short *d,
+ const unsigned char *e, huft_t ** t, int *m)
+{
+ unsigned a; /* counter for codes of length k */
+ unsigned c[BMAX + 1]; /* bit length count table */
+ unsigned f; /* i repeats in table every f entries */
+ int g; /* maximum code length */
+ int h; /* table level */
+ register unsigned i; /* counter, current code */
+ register unsigned j; /* counter */
+ register int k; /* number of bits in current code */
+ int l; /* bits per table (returned in m) */
+ register unsigned *p; /* pointer into c[], b[], or v[] */
+ register huft_t *q; /* points to current table */
+ huft_t r; /* table entry for structure assignment */
+ huft_t *u[BMAX]; /* table stack */
+ unsigned v[N_MAX]; /* values in order of bit length */
+ register int w; /* bits before this table == (l * h) */
+ unsigned x[BMAX + 1]; /* bit offsets, then code stack */
+ unsigned *xp; /* pointer into x */
+ int y; /* number of dummy codes added */
+ unsigned z; /* number of entries in current table */
+
+ /* Generate counts for each bit length */
+ memset((void *) (c), 0, sizeof(c));
+ p = b;
+ i = n;
+ do {
+ c[*p]++; /* assume all entries <= BMAX */
+ p++; /* Can't combine with above line (Solaris bug) */
+ } while (--i);
+ if (c[0] == n) { /* null input--all zero length codes */
+ *t = (huft_t *) NULL;
+ *m = 0;
+ return 0;
+ }
+
+ /* Find minimum and maximum length, bound *m by those */
+ l = *m;
+ for (j = 1; j <= BMAX; j++) {
+ if (c[j]) {
+ break;
+ }
+ }
+ k = j; /* minimum code length */
+ if ((unsigned) l < j) {
+ l = j;
+ }
+ for (i = BMAX; i; i--) {
+ if (c[i]) {
+ break;
+ }
+ }
+ g = i; /* maximum code length */
+ if ((unsigned) l > i) {
+ l = i;
+ }
+ *m = l;
+
+ /* Adjust last length count to fill out codes, if needed */
+ for (y = 1 << j; j < i; j++, y <<= 1) {
+ if ((y -= c[j]) < 0) {
+ return 2; /* bad input: more codes than bits */
+ }
+ }
+ if ((y -= c[i]) < 0) {
+ return 2;
+ }
+ c[i] += y;
+
+ /* Generate starting offsets into the value table for each length */
+ x[1] = j = 0;
+ p = c + 1;
+ xp = x + 2;
+ while (--i) { /* note that i == g from above */
+ *xp++ = (j += *p++);
+ }
+
+ /* Make a table of values in order of bit lengths */
+ p = b;
+ i = 0;
+ do {
+ if ((j = *p++) != 0) {
+ v[x[j]++] = i;
+ }
+ } while (++i < n);
+
+ /* Generate the Huffman codes and for each, make the table entries */
+ x[0] = i = 0; /* first Huffman code is zero */
+ p = v; /* grab values in bit order */
+ h = -1; /* no tables yet--level -1 */
+ w = -l; /* bits decoded == (l * h) */
+ u[0] = (huft_t *) NULL; /* just to keep compilers happy */
+ q = (huft_t *) NULL; /* ditto */
+ z = 0; /* ditto */
+
+ /* go through the bit lengths (k already is bits in shortest code) */
+ for (; k <= g; k++) {
+ a = c[k];
+ while (a--) {
+ /* here i is the Huffman code of length k bits for value *p */
+ /* make tables up to required level */
+ while (k > w + l) {
+ h++;
+ w += l; /* previous table always l bits */
+
+ /* compute minimum size table less than or equal to l bits */
+ z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
+ if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */
+ f -= a + 1; /* deduct codes from patterns left */
+ xp = c + k;
+ while (++j < z) { /* try smaller tables up to z bits */
+ if ((f <<= 1) <= *++xp) {
+ break; /* enough codes to use up j bits */
+ }
+ f -= *xp; /* else deduct codes from patterns */
+ }
+ }
+ z = 1 << j; /* table entries for j-bit table */
+
+ /* allocate and link in new table */
+ q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
+
+ *t = q + 1; /* link to list for huft_free() */
+ *(t = &(q->v.t)) = NULL;
+ u[h] = ++q; /* table starts after link */
+
+ /* connect to last table, if there is one */
+ if (h) {
+ x[h] = i; /* save pattern for backing up */
+ r.b = (unsigned char) l; /* bits to dump before this table */
+ r.e = (unsigned char) (16 + j); /* bits in this table */
+ r.v.t = q; /* pointer to this table */
+ j = i >> (w - l); /* (get around Turbo C bug) */
+ u[h - 1][j] = r; /* connect to last table */
+ }
+ }
+
+ /* set up table entry in r */
+ r.b = (unsigned char) (k - w);
+ if (p >= v + n) {
+ r.e = 99; /* out of values--invalid code */
+ } else if (*p < s) {
+ r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
+ r.v.n = (unsigned short) (*p); /* simple code is just the value */
+ p++; /* one compiler does not like *p++ */
+ } else {
+ r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
+ r.v.n = d[*p++ - s];
+ }
+
+ /* fill code-like entries with r */
+ f = 1 << (k - w);
+ for (j = i >> w; j < z; j += f) {
+ q[j] = r;
+ }
+
+ /* backwards increment the k-bit code i */
+ for (j = 1 << (k - 1); i & j; j >>= 1) {
+ i ^= j;
+ }
+ i ^= j;
+
+ /* backup over finished tables */
+ while ((i & ((1 << w) - 1)) != x[h]) {
+ h--; /* don't need to update q */
+ w -= l;
+ }
+ }
+ }
+ /* Return true (1) if we were given an incomplete table */
+ return y != 0 && g != 1;
+}
+
+/*
+ * inflate (decompress) the codes in a deflated (compressed) block.
+ * Return an error code or zero if it all goes ok.
+ *
+ * tl, td: literal/length and distance decoder tables
+ * bl, bd: number of bits decoded by tl[] and td[]
+ */
+static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_bl, const unsigned int my_bd, int setup)
+{
+ static unsigned int e; /* table entry flag/number of extra bits */
+ static unsigned int n, d; /* length and index for copy */
+ static unsigned int w; /* current gunzip_window position */
+ static huft_t *t; /* pointer to table entry */
+ static unsigned int ml, md; /* masks for bl and bd bits */
+ static unsigned int b; /* bit buffer */
+ static unsigned int k; /* number of bits in bit buffer */
+ static huft_t *tl, *td;
+ static unsigned int bl, bd;
+ static int resumeCopy = 0;
+
+ if (setup) { // 1st time we are called, copy in variables
+ tl = my_tl;
+ td = my_td;
+ bl = my_bl;
+ bd = my_bd;
+ /* make local copies of globals */
+ b = gunzip_bb; /* initialize bit buffer */
+ k = gunzip_bk;
+ w = gunzip_outbuf_count; /* initialize gunzip_window position */
+
+ /* inflate the coded data */
+ ml = mask_bits[bl]; /* precompute masks for speed */
+ md = mask_bits[bd];
+ return 0; // Don't actually do anything the first time
+ }
+
+ if (resumeCopy) goto do_copy;
+
+ while (1) { /* do until end of block */
+ b = fill_bitbuffer(b, &k, bl);
+ if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
+ do {
+ if (e == 99) {
+ bb_error_msg_and_die("inflate_codes error 1");;
+ }
+ b >>= t->b;
+ k -= t->b;
+ e -= 16;
+ b = fill_bitbuffer(b, &k, e);
+ } while ((e =
+ (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
+ b >>= t->b;
+ k -= t->b;
+ if (e == 16) { /* then it's a literal */
+ gunzip_window[w++] = (unsigned char) t->v.n;
+ if (w == gunzip_wsize) {
+ gunzip_outbuf_count = (w);
+ //flush_gunzip_window();
+ w = 0;
+ return 1; // We have a block to read
+ }
+ } else { /* it's an EOB or a length */
+
+ /* exit if end of block */
+ if (e == 15) {
+ break;
+ }
+
+ /* get length of block to copy */
+ b = fill_bitbuffer(b, &k, e);
+ n = t->v.n + ((unsigned) b & mask_bits[e]);
+ b >>= e;
+ k -= e;
+
+ /* decode distance of block to copy */
+ b = fill_bitbuffer(b, &k, bd);
+ if ((e = (t = td + ((unsigned) b & md))->e) > 16)
+ do {
+ if (e == 99)
+ bb_error_msg_and_die("inflate_codes error 2");;
+ b >>= t->b;
+ k -= t->b;
+ e -= 16;
+ b = fill_bitbuffer(b, &k, e);
+ } while ((e =
+ (t =
+ t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
+ b >>= t->b;
+ k -= t->b;
+ b = fill_bitbuffer(b, &k, e);
+ d = w - t->v.n - ((unsigned) b & mask_bits[e]);
+ b >>= e;
+ k -= e;
+
+ /* do the copy */
+do_copy: do {
+ n -= (e =
+ (e =
+ gunzip_wsize - ((d &= gunzip_wsize - 1) > w ? d : w)) > n ? n : e);
+ /* copy to new buffer to prevent possible overwrite */
+ if (w - d >= e) { /* (this test assumes unsigned comparison) */
+ memcpy(gunzip_window + w, gunzip_window + d, e);
+ w += e;
+ d += e;
+ } else {
+ /* do it slow to avoid memcpy() overlap */
+ /* !NOMEMCPY */
+ do {
+ gunzip_window[w++] = gunzip_window[d++];
+ } while (--e);
+ }
+ if (w == gunzip_wsize) {
+ gunzip_outbuf_count = (w);
+ if (n) resumeCopy = 1;
+ else resumeCopy = 0;
+ //flush_gunzip_window();
+ w = 0;
+ return 1;
+ }
+ } while (n);
+ resumeCopy = 0;
+ }
+ }
+
+ /* restore the globals from the locals */
+ gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
+ gunzip_bb = b; /* restore global bit buffer */
+ gunzip_bk = k;
+
+ /* normally just after call to inflate_codes, but save code by putting it here */
+ /* free the decoding tables, return */
+ huft_free(tl);
+ huft_free(td);
+
+ /* done */
+ return 0;
+}
+
+static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
+{
+ static int n, b_stored, k_stored, w;
+ if (setup) {
+ n = my_n;
+ b_stored = my_b_stored;
+ k_stored = my_k_stored;
+ w = gunzip_outbuf_count; /* initialize gunzip_window position */
+ return 0; // Don't do anything first time
+ }
+
+ /* read and output the compressed data */
+ while (n--) {
+ b_stored = fill_bitbuffer(b_stored, &k_stored, 8);
+ gunzip_window[w++] = (unsigned char) b_stored;
+ if (w == (unsigned int) gunzip_wsize) {
+ gunzip_outbuf_count = (w);
+ //flush_gunzip_window();
+ w = 0;
+ b_stored >>= 8;
+ k_stored -= 8;
+ return 1; // We have a block
+ }
+ b_stored >>= 8;
+ k_stored -= 8;
+ }
+
+ /* restore the globals from the locals */
+ gunzip_outbuf_count = w; /* restore global gunzip_window pointer */
+ gunzip_bb = b_stored; /* restore global bit buffer */
+ gunzip_bk = k_stored;
+ return 0; // Finished
+}
+
+/*
+ * decompress an inflated block
+ * e: last block flag
+ *
+ * GLOBAL VARIABLES: bb, kk,
+ */
+ // Return values: -1 = inflate_stored, -2 = inflate_codes
+static int inflate_block(int *e)
+{
+ unsigned t; /* block type */
+ register unsigned int b; /* bit buffer */
+ unsigned int k; /* number of bits in bit buffer */
+
+ /* make local bit buffer */
+
+ b = gunzip_bb;
+ k = gunzip_bk;
+
+ /* read in last block bit */
+ b = fill_bitbuffer(b, &k, 1);
+ *e = (int) b & 1;
+ b >>= 1;
+ k -= 1;
+
+ /* read in block type */
+ b = fill_bitbuffer(b, &k, 2);
+ t = (unsigned) b & 3;
+ b >>= 2;
+ k -= 2;
+
+ /* restore the global bit buffer */
+ gunzip_bb = b;
+ gunzip_bk = k;
+
+ /* inflate that block type */
+ switch (t) {
+ case 0: /* Inflate stored */
+ {
+ unsigned int n; /* number of bytes in block */
+ unsigned int b_stored; /* bit buffer */
+ unsigned int k_stored; /* number of bits in bit buffer */
+
+ /* make local copies of globals */
+ b_stored = gunzip_bb; /* initialize bit buffer */
+ k_stored = gunzip_bk;
+
+ /* go to byte boundary */
+ n = k_stored & 7;
+ b_stored >>= n;
+ k_stored -= n;
+
+ /* get the length and its complement */
+ b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
+ n = ((unsigned) b_stored & 0xffff);
+ b_stored >>= 16;
+ k_stored -= 16;
+
+ b_stored = fill_bitbuffer(b_stored, &k_stored, 16);
+ if (n != (unsigned) ((~b_stored) & 0xffff)) {
+ return 1; /* error in compressed data */
+ }
+ b_stored >>= 16;
+ k_stored -= 16;
+
+ inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored
+ return -1;
+ }
+ case 1: /* Inflate fixed
+ * decompress an inflated type 1 (fixed Huffman codes) block. We should
+ * either replace this with a custom decoder, or at least precompute the
+ * Huffman tables.
+ */
+ {
+ int i; /* temporary variable */
+ huft_t *tl; /* literal/length code table */
+ huft_t *td; /* distance code table */
+ unsigned int bl; /* lookup bits for tl */
+ unsigned int bd; /* lookup bits for td */
+ unsigned int l[288]; /* length list for huft_build */
+
+ /* set up literal table */
+ for (i = 0; i < 144; i++) {
+ l[i] = 8;
+ }
+ for (; i < 256; i++) {
+ l[i] = 9;
+ }
+ for (; i < 280; i++) {
+ l[i] = 7;
+ }
+ for (; i < 288; i++) { /* make a complete, but wrong code set */
+ l[i] = 8;
+ }
+ bl = 7;
+ if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
+ return i;
+ }
+
+ /* set up distance table */
+ for (i = 0; i < 30; i++) { /* make an incomplete code set */
+ l[i] = 5;
+ }
+ bd = 5;
+ if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
+ huft_free(tl);
+ return i;
+ }
+
+ /* decompress until an end-of-block code */
+ inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
+
+ /* huft_free code moved into inflate_codes */
+
+ return -2;
+ }
+ case 2: /* Inflate dynamic */
+ {
+ const int dbits = 6; /* bits in base distance lookup table */
+ const int lbits = 9; /* bits in base literal/length lookup table */
+
+ huft_t *tl; /* literal/length code table */
+ huft_t *td; /* distance code table */
+ unsigned int i; /* temporary variables */
+ unsigned int j;
+ unsigned int l; /* last length */
+ unsigned int m; /* mask for bit lengths table */
+ unsigned int n; /* number of lengths to get */
+ unsigned int bl; /* lookup bits for tl */
+ unsigned int bd; /* lookup bits for td */
+ unsigned int nb; /* number of bit length codes */
+ unsigned int nl; /* number of literal/length codes */
+ unsigned int nd; /* number of distance codes */
+
+ unsigned int ll[286 + 30]; /* literal/length and distance code lengths */
+ unsigned int b_dynamic; /* bit buffer */
+ unsigned int k_dynamic; /* number of bits in bit buffer */
+
+ /* make local bit buffer */
+ b_dynamic = gunzip_bb;
+ k_dynamic = gunzip_bk;
+
+ /* read in table lengths */
+ b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
+ nl = 257 + ((unsigned int) b_dynamic & 0x1f); /* number of literal/length codes */
+
+ b_dynamic >>= 5;
+ k_dynamic -= 5;
+ b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 5);
+ nd = 1 + ((unsigned int) b_dynamic & 0x1f); /* number of distance codes */
+
+ b_dynamic >>= 5;
+ k_dynamic -= 5;
+ b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 4);
+ nb = 4 + ((unsigned int) b_dynamic & 0xf); /* number of bit length codes */
+
+ b_dynamic >>= 4;
+ k_dynamic -= 4;
+ if (nl > 286 || nd > 30) {
+ return 1; /* bad lengths */
+ }
+
+ /* read in bit-length-code lengths */
+ for (j = 0; j < nb; j++) {
+ b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
+ ll[border[j]] = (unsigned int) b_dynamic & 7;
+ b_dynamic >>= 3;
+ k_dynamic -= 3;
+ }
+ for (; j < 19; j++) {
+ ll[border[j]] = 0;
+ }
+
+ /* build decoding table for trees--single level, 7 bit lookup */
+ bl = 7;
+ i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl);
+ if (i != 0) {
+ if (i == 1) {
+ huft_free(tl);
+ }
+ return i; /* incomplete code set */
+ }
+
+ /* read in literal and distance code lengths */
+ n = nl + nd;
+ m = mask_bits[bl];
+ i = l = 0;
+ while ((unsigned int) i < n) {
+ b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, (unsigned int)bl);
+ j = (td = tl + ((unsigned int) b_dynamic & m))->b;
+ b_dynamic >>= j;
+ k_dynamic -= j;
+ j = td->v.n;
+ if (j < 16) { /* length of code in bits (0..15) */
+ ll[i++] = l = j; /* save last length in l */
+ } else if (j == 16) { /* repeat last length 3 to 6 times */
+ b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 2);
+ j = 3 + ((unsigned int) b_dynamic & 3);
+ b_dynamic >>= 2;
+ k_dynamic -= 2;
+ if ((unsigned int) i + j > n) {
+ return 1;
+ }
+ while (j--) {
+ ll[i++] = l;
+ }
+ } else if (j == 17) { /* 3 to 10 zero length codes */
+ b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 3);
+ j = 3 + ((unsigned int) b_dynamic & 7);
+ b_dynamic >>= 3;
+ k_dynamic -= 3;
+ if ((unsigned int) i + j > n) {
+ return 1;
+ }
+ while (j--) {
+ ll[i++] = 0;
+ }
+ l = 0;
+ } else { /* j == 18: 11 to 138 zero length codes */
+ b_dynamic = fill_bitbuffer(b_dynamic, &k_dynamic, 7);
+ j = 11 + ((unsigned int) b_dynamic & 0x7f);
+ b_dynamic >>= 7;
+ k_dynamic -= 7;
+ if ((unsigned int) i + j > n) {
+ return 1;
+ }
+ while (j--) {
+ ll[i++] = 0;
+ }
+ l = 0;
+ }
+ }
+
+ /* free decoding table for trees */
+ huft_free(tl);
+
+ /* restore the global bit buffer */
+ gunzip_bb = b_dynamic;
+ gunzip_bk = k_dynamic;
+
+ /* build the decoding tables for literal/length and distance codes */
+ bl = lbits;
+
+ if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
+ if (i == 1) {
+ bb_error_msg_and_die("Incomplete literal tree");
+ huft_free(tl);
+ }
+ return i; /* incomplete code set */
+ }
+
+ bd = dbits;
+ if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
+ if (i == 1) {
+ bb_error_msg_and_die("incomplete distance tree");
+ huft_free(td);
+ }
+ huft_free(tl);
+ return i; /* incomplete code set */
+ }
+
+ /* decompress until an end-of-block code */
+ inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes
+
+ /* huft_free code moved into inflate_codes */
+
+ return -2;
+ }
+ default:
+ /* bad block type */
+ bb_error_msg_and_die("bad block type %d\n", t);
+ }
+}
+
+static void calculate_gunzip_crc(void)
+{
+ int n;
+ for (n = 0; n < gunzip_outbuf_count; n++) {
+ gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
+ }
+ gunzip_bytes_out += gunzip_outbuf_count;
+}
+
+static int inflate_get_next_window(void)
+{
+ static int needAnotherBlock = 1;
+ static int method = -1; // Method == -1 for stored, -2 for codes
+ static int e = 0;
+
+ gunzip_outbuf_count = 0;
+
+ while(1) {
+ int ret;
+
+ if (needAnotherBlock) {
+ if(e) {
+ calculate_gunzip_crc();
+ return 0;
+ } // Last block
+ method = inflate_block(&e);
+ needAnotherBlock = 0;
+ }
+
+ switch (method) {
+ case -1: ret = inflate_stored(0,0,0,0);
+ break;
+ case -2: ret = inflate_codes(0,0,0,0,0);
+ break;
+ default: bb_error_msg_and_die("inflate error %d", method);
+ }
+
+ if (ret == 1) {
+ calculate_gunzip_crc();
+ return 1; // More data left
+ } else needAnotherBlock = 1; // End of that block
+ }
+ /* Doesnt get here */
+}
+
+/*
+ * User functions
+ *
+ * read_gz, GZ_gzReadOpen, GZ_gzReadClose, inflate
+ */
+
+extern ssize_t read_gz(int fd, void *buf, size_t count)
+{
+ static int morebytes = 0, finished = 0;
+
+ if (morebytes) {
+ int bytesRead = morebytes > count ? count : morebytes;
+ memcpy(buf, gunzip_window + (gunzip_outbuf_count - morebytes), bytesRead);
+ morebytes -= bytesRead;
+ return bytesRead;
+ } else if (finished) {
+ return 0;
+ } else if (count >= 0x8000) { // We can decompress direcly to the buffer, 32k at a time
+ // Could decompress to larger buffer, but it must be a power of 2, and calculating that is probably more expensive than the benefit
+ unsigned char *old_gunzip_window = gunzip_window; // Save old window
+ gunzip_window = buf;
+ if (inflate_get_next_window() == 0) finished = 1;
+ gunzip_window = old_gunzip_window; // Restore old window
+ return gunzip_outbuf_count;
+ } else { // Oh well, need to split up the gunzip_window
+ int bytesRead;
+ if (inflate_get_next_window() == 0) finished = 1;
+ morebytes = gunzip_outbuf_count;
+ bytesRead = morebytes > count ? count : morebytes;
+ memcpy(buf, gunzip_window, bytesRead);
+ morebytes -= bytesRead;
+ return bytesRead;
+ }
+
+}
+
+extern void GZ_gzReadOpen(int fd, void *unused, int nUnused)
+{
+ typedef void (*sig_type) (int);
+
+ /* Allocate all global buffers (for DYN_ALLOC option) */
+ gunzip_window = xmalloc(gunzip_wsize);
+ gunzip_outbuf_count = 0;
+ gunzip_bytes_out = 0;
+ gunzip_src_fd = fd;
+
+ /* Input buffer */
+ bytebuffer = xmalloc(BYTEBUFFER_MAX);
+
+ /* initialize gunzip_window, bit buffer */
+ gunzip_bk = 0;
+ gunzip_bb = 0;
+
+ /* Create the crc table */
+ make_gunzip_crc_table();
+}
+
+extern void GZ_gzReadClose(void)
+{
+ /* Cleanup */
+ free(gunzip_window);
+ free(gunzip_crc_table);
+
+ /* Store unused bytes in a global buffer so calling applets can access it */
+ if (gunzip_bk >= 8) {
+ /* Undo too much lookahead. The next read will be byte aligned
+ * so we can discard unused bits in the last meaningful byte. */
+ bytebuffer_offset--;
+ bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
+ gunzip_bb >>= 8;
+ gunzip_bk -= 8;
+ }
+}
+
+/*extern int inflate(int in, int out) // Useful for testing read_gz
+{
+ char buf[8192];
+ ssize_t nread, nwrote;
+
+ GZ_gzReadOpen(in, 0, 0);
+ while(1) { // Robbed from bb_copyfd.c
+ nread = read_gz(in, buf, sizeof(buf));
+ if (nread == 0) break; // no data to write
+ else if (nread == -1) {
+ bb_perror_msg("read");
+ return -1;
+ }
+ nwrote = bb_full_write(out, buf, nread);
+ if (nwrote == -1) {
+ bb_perror_msg("write");
+ return -1;
+ }
+ }
+ GZ_gzReadClose();
+ return 0;
+}*/
+
+extern int inflate(int in, int out)
+{
+ ssize_t nwrote;
+ GZ_gzReadOpen(in, 0, 0);
+ while(1) {
+ int ret = inflate_get_next_window();
+ nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count);
+ if (nwrote == -1) {
+ bb_perror_msg("write");
+ return -1;
+ }
+ if (ret == 0) break;
+ }
+ GZ_gzReadClose();
+ return 0;
+}
+
+extern void check_trailer_gzip(int src_fd)
+{
+ unsigned int stored_crc = 0;
+ unsigned char count;
+
+ /* top up the input buffer with the rest of the trailer */
+ count = bytebuffer_size - bytebuffer_offset;
+ if (count < 8) {
+ bb_xread_all(src_fd, &bytebuffer[bytebuffer_size], 8 - count);
+ bytebuffer_size += 8 - count;
+ }
+ for (count = 0; count != 4; count++) {
+ stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8));
+ bytebuffer_offset++;
+ }
+
+ /* Validate decompression - crc */
+ if (stored_crc != (gunzip_crc ^ 0xffffffffL)) {
+ bb_error_msg_and_die("crc error");
+ }
+
+ /* Validate decompression - size */
+ if (gunzip_bytes_out !=
+ (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
+ (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
+ bb_error_msg_and_die("Incorrect length, but crc is correct");
+ }
+
+}