diff options
author | P. J. McDermott <pjm@nac.net> | 2012-02-09 10:56:43 (EST) |
---|---|---|
committer | P. J. McDermott <pjm@nac.net> | 2012-02-09 10:56:43 (EST) |
commit | f431cd1a48a6a5186633bf5f16a2d21cb4399e8c (patch) | |
tree | 337e36fd9d7884fcf76ba769ab7b0039f52d9d31 /src/libbb |
Initial commit.
TODO: Copyright information.
Including source code and a patch to add files generated by GNU Autoconf
is not very pretty... Running 'make dist' in the SVN trunk to generate
a source archive might be better.
Diffstat (limited to 'src/libbb')
38 files changed, 7037 insertions, 0 deletions
diff --git a/src/libbb/.svn/all-wcprops b/src/libbb/.svn/all-wcprops new file mode 100644 index 0000000..8791632 --- /dev/null +++ b/src/libbb/.svn/all-wcprops @@ -0,0 +1,113 @@ +K 25 +svn:wc:ra_dav:version-url +V 29 +/svn/!svn/ver/604/trunk/libbb +END +wfopen.c +K 25 +svn:wc:ra_dav:version-url +V 36 +/svn/!svn/ver/3/trunk/libbb/wfopen.c +END +safe_strncpy.c +K 25 +svn:wc:ra_dav:version-url +V 44 +/svn/!svn/ver/552/trunk/libbb/safe_strncpy.c +END +gz_open.c +K 25 +svn:wc:ra_dav:version-url +V 39 +/svn/!svn/ver/604/trunk/libbb/gz_open.c +END +make_directory.c +K 25 +svn:wc:ra_dav:version-url +V 46 +/svn/!svn/ver/552/trunk/libbb/make_directory.c +END +copy_file_chunk.c +K 25 +svn:wc:ra_dav:version-url +V 45 +/svn/!svn/ver/3/trunk/libbb/copy_file_chunk.c +END +all_read.c +K 25 +svn:wc:ra_dav:version-url +V 38 +/svn/!svn/ver/3/trunk/libbb/all_read.c +END +unzip.c +K 25 +svn:wc:ra_dav:version-url +V 37 +/svn/!svn/ver/577/trunk/libbb/unzip.c +END +xfuncs.c +K 25 +svn:wc:ra_dav:version-url +V 38 +/svn/!svn/ver/552/trunk/libbb/xfuncs.c +END +copy_file.c +K 25 +svn:wc:ra_dav:version-url +V 41 +/svn/!svn/ver/314/trunk/libbb/copy_file.c +END +mode_string.c +K 25 +svn:wc:ra_dav:version-url +V 41 +/svn/!svn/ver/3/trunk/libbb/mode_string.c +END +parse_mode.c +K 25 +svn:wc:ra_dav:version-url +V 42 +/svn/!svn/ver/552/trunk/libbb/parse_mode.c +END +libbb.h +K 25 +svn:wc:ra_dav:version-url +V 37 +/svn/!svn/ver/471/trunk/libbb/libbb.h +END +xreadlink.c +K 25 +svn:wc:ra_dav:version-url +V 41 +/svn/!svn/ver/552/trunk/libbb/xreadlink.c +END +concat_path_file.c +K 25 +svn:wc:ra_dav:version-url +V 46 +/svn/!svn/ver/3/trunk/libbb/concat_path_file.c +END +unarchive.c +K 25 +svn:wc:ra_dav:version-url +V 41 +/svn/!svn/ver/594/trunk/libbb/unarchive.c +END +time_string.c +K 25 +svn:wc:ra_dav:version-url +V 41 +/svn/!svn/ver/3/trunk/libbb/time_string.c +END +Makefile.am +K 25 +svn:wc:ra_dav:version-url +V 41 +/svn/!svn/ver/471/trunk/libbb/Makefile.am +END +last_char_is.c +K 25 +svn:wc:ra_dav:version-url +V 44 +/svn/!svn/ver/552/trunk/libbb/last_char_is.c +END diff --git a/src/libbb/.svn/entries b/src/libbb/.svn/entries new file mode 100644 index 0000000..41b68e6 --- /dev/null +++ b/src/libbb/.svn/entries @@ -0,0 +1,640 @@ +10 + +dir +635 +http://opkg.googlecode.com/svn/trunk/libbb +http://opkg.googlecode.com/svn + + + +2011-02-18T00:02:14.770648Z +604 +graham.gower@gmail.com + + + + + + + + + + + + + + +e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 + +wfopen.c +file + + + + +2012-02-03T08:11:57.563042Z +4de9274e149c9a721486612d912f010c +2008-12-15T04:10:56.746937Z +3 +ticktock35 + + + + + + + + + + + + + + + + + + + + + +1156 + +safe_strncpy.c +file + + + + +2012-02-03T08:11:57.563042Z +eceb9fe63523c56418c814398928b131 +2010-08-18T03:39:02.973664Z +552 +graham.gower + + + + + + + + + + + + + + + + + + + + + +1187 + +gz_open.c +file + + + + +2012-02-03T08:11:57.563042Z +25bb2e8fa8654800a1ca67a27e9a3b7c +2011-02-18T00:02:14.770648Z +604 +graham.gower@gmail.com + + + + + + + + + + + + + + + + + + + + + +3463 + +make_directory.c +file + + + + +2012-02-03T08:11:57.563042Z +0ac335573a0abb1fd2923b9c0d0badc7 +2010-08-18T03:39:02.973664Z +552 +graham.gower + + + + + + + + + + + + + + + + + + + + + +2212 + +copy_file_chunk.c +file + + + + +2012-02-03T08:11:57.563042Z +1ba798a3be6f8a446b8bef279c12150a +2008-12-15T04:10:56.746937Z +3 +ticktock35 + + + + + + + + + + + + + + + + + + + + + +1797 + +all_read.c +file + + + + +2012-02-03T08:11:57.563042Z +7e29f8b6acbdbf28acc550e5714283e9 +2008-12-15T04:10:56.746937Z +3 +ticktock35 + + + + + + + + + + + + + + + + + + + + + +2043 + +unzip.c +file + + + + +2012-02-03T08:11:57.563042Z +696d797a25573d1258b184f3443a369c +2010-11-11T00:23:29.781328Z +577 +graham.gower@gmail.com + + + + + + + + + + + + + + + + + + + + + +28342 + +xfuncs.c +file + + + + +2012-02-03T08:11:57.563042Z +05055d1a1df58a0ca92a8c5ce0c2c6d2 +2010-08-18T03:39:02.973664Z +552 +graham.gower + + + + + + + + + + + + + + + + + + + + + +1990 + +copy_file.c +file + + + + +2012-02-03T08:11:57.563042Z +0e4fab1c48fd3c4a9ac3ddced1e3e697 +2009-11-16T00:43:40.210441Z +314 +graham.gower + + + + + + + + + + + + + + + + + + + + + +5801 + +mode_string.c +file + + + + +2012-02-03T08:11:57.563042Z +0e327b4639db752764fc893f325380f3 +2008-12-15T04:10:56.746937Z +3 +ticktock35 + + + + + + + + + + + + + + + + + + + + + +1987 + +parse_mode.c +file + + + + +2012-02-03T08:11:57.563042Z +b52917e30e6adccb57ff822d1dd89e54 +2010-08-18T03:39:02.973664Z +552 +graham.gower + + + + + + + + + + + + + + + + + + + + + +3090 + +libbb.h +file + + + + +2012-02-03T08:11:57.563042Z +3e39fa5e5256c01d96d53c1177adf48c +2009-12-09T01:20:03.840165Z +471 +graham.gower + + + + + + + + + + + + + + + + + + + + + +3492 + +xreadlink.c +file + + + + +2012-02-03T08:11:57.567042Z +565265da2163aabe44fc349dc0b9d6f8 +2010-08-18T03:39:02.973664Z +552 +graham.gower + + + + + + + + + + + + + + + + + + + + + +696 + +concat_path_file.c +file + + + + +2012-02-03T08:11:57.567042Z +db5612f5d02a74764abeae502e1b8eb0 +2008-12-15T04:10:56.746937Z +3 +ticktock35 + + + + + + + + + + + + + + + + + + + + + +1364 + +time_string.c +file + + + + +2012-02-03T08:11:57.567042Z +c9f00cb228ef5a4d60ea217d1c47240e +2008-12-15T04:10:56.746937Z +3 +ticktock35 + + + + + + + + + + + + + + + + + + + + + +1455 + +unarchive.c +file + + + + +2012-02-03T08:11:57.567042Z +8cdd4c13dbfab8cb27aa3157ff1387c0 +2010-12-23T01:38:25.615032Z +594 +graham.gower@gmail.com + + + + + + + + + + + + + + + + + + + + + +21712 + +Makefile.am +file + + + + +2012-02-03T08:11:57.567042Z +48cbb7a82fb7cb54cc39bcf02666fe82 +2009-12-09T01:20:03.840165Z +471 +graham.gower + + + + + + + + + + + + + + + + + + + + + +495 + +last_char_is.c +file + + + + +2012-02-03T08:11:57.567042Z +caea3b90b5b195dfcb1bd10e75a4078c +2010-08-18T03:39:02.973664Z +552 +graham.gower + + + + + + + + + + + + + + + + + + + + + +1260 + diff --git a/src/libbb/.svn/text-base/Makefile.am.svn-base b/src/libbb/.svn/text-base/Makefile.am.svn-base new file mode 100644 index 0000000..1cc82df --- /dev/null +++ b/src/libbb/.svn/text-base/Makefile.am.svn-base @@ -0,0 +1,26 @@ +HOST_CPU=@host_cpu@ +BUILD_CPU=@build_cpu@ +ALL_CFLAGS=-g -O -Wall -DHOST_CPU_STR=\"@host_cpu@\" -DBUILD_CPU=@build_cpu@ + +noinst_LTLIBRARIES = libbb.la + +libbb_la_SOURCES = gz_open.c \ + libbb.h \ + unzip.c \ + wfopen.c \ + unarchive.c \ + copy_file.c \ + copy_file_chunk.c \ + xreadlink.c \ + concat_path_file.c \ + xfuncs.c \ + last_char_is.c \ + make_directory.c \ + safe_strncpy.c \ + parse_mode.c \ + time_string.c \ + all_read.c \ + mode_string.c + +libbb_la_CFLAGS = $(ALL_CFLAGS) +#libbb_la_LDFLAGS = -static diff --git a/src/libbb/.svn/text-base/all_read.c.svn-base b/src/libbb/.svn/text-base/all_read.c.svn-base new file mode 100644 index 0000000..6ec731a --- /dev/null +++ b/src/libbb/.svn/text-base/all_read.c.svn-base @@ -0,0 +1,83 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999-2004 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 + */ + +#include <stdio.h> +#include <unistd.h> +#include <errno.h> +#include "libbb.h" + +extern void archive_xread_all(int fd , char *buf, size_t count) +{ + ssize_t size; + + size = full_read(fd, buf, count); + if (size != count) { + perror_msg_and_die("Short read"); + } + return; +} + +/* + * Read all of the supplied buffer from a file. + * This does multiple reads as necessary. + * Returns the amount read, or -1 on an error. + * A short read is returned on an end of file. + */ +ssize_t full_read(int fd, char *buf, int len) +{ + ssize_t cc; + ssize_t total; + + total = 0; + + while (len > 0) { + cc = safe_read(fd, buf, len); + + if (cc < 0) + return cc; /* read() returns -1 on failure. */ + + if (cc == 0) + break; + + buf = ((char *)buf) + cc; + total += cc; + len -= cc; + } + + return total; +} + + +ssize_t safe_read(int fd, void *buf, size_t count) +{ + ssize_t n; + + do { + n = read(fd, buf, count); + } while (n < 0 && errno == EINTR); + + return n; +} + + + +/* END CODE */ + diff --git a/src/libbb/.svn/text-base/concat_path_file.c.svn-base b/src/libbb/.svn/text-base/concat_path_file.c.svn-base new file mode 100644 index 0000000..e62b99e --- /dev/null +++ b/src/libbb/.svn/text-base/concat_path_file.c.svn-base @@ -0,0 +1,45 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 + */ + +/* concatenate path and file name to new allocation buffer, + * not addition '/' if path name already have '/' +*/ + +#include <string.h> +#include "libbb.h" + +extern char *concat_path_file(const char *path, const char *filename) +{ + char *outbuf; + char *lc; + + if (!path) + path=""; + lc = last_char_is(path, '/'); + while (*filename == '/') + filename++; + outbuf = xmalloc(strlen(path)+strlen(filename)+1+(lc==NULL)); + sprintf(outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename); + + return outbuf; +} diff --git a/src/libbb/.svn/text-base/copy_file.c.svn-base b/src/libbb/.svn/text-base/copy_file.c.svn-base new file mode 100644 index 0000000..fb76669 --- /dev/null +++ b/src/libbb/.svn/text-base/copy_file.c.svn-base @@ -0,0 +1,231 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini copy_file implementation for busybox + * + * + * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu> + * + * 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 <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <utime.h> +#include <errno.h> +#include <dirent.h> +#include <stdlib.h> +#include <string.h> + +#include "libbb.h" + +int copy_file(const char *source, const char *dest, int flags) +{ + struct stat source_stat; + struct stat dest_stat; + int dest_exists = 1; + int status = 0; + + if (((flags & FILEUTILS_PRESERVE_SYMLINKS) && + lstat(source, &source_stat) < 0) || + (!(flags & FILEUTILS_PRESERVE_SYMLINKS) && + stat(source, &source_stat) < 0)) { + perror_msg("%s", source); + return -1; + } + + if (stat(dest, &dest_stat) < 0) { + if (errno != ENOENT) { + perror_msg("unable to stat `%s'", dest); + return -1; + } + dest_exists = 0; + } + + if (dest_exists && source_stat.st_rdev == dest_stat.st_rdev && + source_stat.st_ino == dest_stat.st_ino) { + error_msg("`%s' and `%s' are the same file", source, dest); + return -1; + } + + if (S_ISDIR(source_stat.st_mode)) { + DIR *dp; + struct dirent *d; + mode_t saved_umask = 0; + + if (!(flags & FILEUTILS_RECUR)) { + error_msg("%s: omitting directory", source); + return -1; + } + + /* Create DEST. */ + if (dest_exists) { + if (!S_ISDIR(dest_stat.st_mode)) { + error_msg("`%s' is not a directory", dest); + return -1; + } + } else { + mode_t mode; + saved_umask = umask(0); + + mode = source_stat.st_mode; + if (!(flags & FILEUTILS_PRESERVE_STATUS)) + mode = source_stat.st_mode & ~saved_umask; + mode |= S_IRWXU; + + if (mkdir(dest, mode) < 0) { + umask(saved_umask); + perror_msg("cannot create directory `%s'", dest); + return -1; + } + + umask(saved_umask); + } + + /* Recursively copy files in SOURCE. */ + if ((dp = opendir(source)) == NULL) { + perror_msg("unable to open directory `%s'", source); + status = -1; + goto end; + } + + while ((d = readdir(dp)) != NULL) { + char *new_source, *new_dest; + + if (strcmp(d->d_name, ".") == 0 || + strcmp(d->d_name, "..") == 0) + continue; + + new_source = concat_path_file(source, d->d_name); + new_dest = concat_path_file(dest, d->d_name); + if (copy_file(new_source, new_dest, flags) < 0) + status = -1; + free(new_source); + free(new_dest); + } + + /* ??? What if an error occurs in readdir? */ + + if (closedir(dp) < 0) { + perror_msg("unable to close directory `%s'", source); + status = -1; + } + + if (!dest_exists && + chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { + perror_msg("unable to change permissions of `%s'", dest); + status = -1; + } + } else if (S_ISREG(source_stat.st_mode)) { + FILE *sfp, *dfp; + + if (dest_exists) { + if ((dfp = fopen(dest, "w")) == NULL) { + if (!(flags & FILEUTILS_FORCE)) { + perror_msg("unable to open `%s'", dest); + return -1; + } + + if (unlink(dest) < 0) { + perror_msg("unable to remove `%s'", dest); + return -1; + } + + dest_exists = 0; + } + } + + if (!dest_exists) { + int fd; + + if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 || + (dfp = fdopen(fd, "w")) == NULL) { + if (fd >= 0) + close(fd); + perror_msg("unable to open `%s'", dest); + return -1; + } + } + + if ((sfp = fopen(source, "r")) == NULL) { + fclose(dfp); + perror_msg("unable to open `%s'", source); + status = -1; + goto end; + } + + if (copy_file_chunk(sfp, dfp, -1) < 0) + status = -1; + + if (fclose(dfp) < 0) { + perror_msg("unable to close `%s'", dest); + status = -1; + } + + if (fclose(sfp) < 0) { + perror_msg("unable to close `%s'", source); + status = -1; + } + } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || + S_ISSOCK(source_stat.st_mode)) { + if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { + perror_msg("unable to create `%s'", dest); + return -1; + } + } else if (S_ISFIFO(source_stat.st_mode)) { + if (mkfifo(dest, source_stat.st_mode) < 0) { + perror_msg("cannot create fifo `%s'", dest); + return -1; + } + } else if (S_ISLNK(source_stat.st_mode)) { + char *lpath = xreadlink(source); + if (symlink(lpath, dest) < 0) { + perror_msg("cannot create symlink `%s'", dest); + return -1; + } + free(lpath); + +#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) + if (flags & FILEUTILS_PRESERVE_STATUS) + if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) + perror_msg("unable to preserve ownership of `%s'", dest); +#endif + return 0; + } else { + error_msg("internal error: unrecognized file type"); + return -1; + } + +end: + + if (flags & FILEUTILS_PRESERVE_STATUS) { + struct utimbuf times; + + times.actime = source_stat.st_atime; + times.modtime = source_stat.st_mtime; + if (utime(dest, ×) < 0) + perror_msg("unable to preserve times of `%s'", dest); + if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { + source_stat.st_mode &= ~(S_ISUID | S_ISGID); + perror_msg("unable to preserve ownership of `%s'", dest); + } + if (chmod(dest, source_stat.st_mode) < 0) + perror_msg("unable to preserve permissions of `%s'", dest); + } + + return status; +} diff --git a/src/libbb/.svn/text-base/copy_file_chunk.c.svn-base b/src/libbb/.svn/text-base/copy_file_chunk.c.svn-base new file mode 100644 index 0000000..63d2ab1 --- /dev/null +++ b/src/libbb/.svn/text-base/copy_file_chunk.c.svn-base @@ -0,0 +1,70 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 <stdio.h> +#include <sys/stat.h> +#include "libbb.h" + +/* Copy CHUNKSIZE bytes (or until EOF if CHUNKSIZE equals -1) from SRC_FILE + * to DST_FILE. */ +extern int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize) +{ + size_t nread, nwritten, size; + char buffer[BUFSIZ]; + + while (chunksize != 0) { + if (chunksize > BUFSIZ) + size = BUFSIZ; + else + size = chunksize; + + nread = fread (buffer, 1, size, src_file); + + if (nread != size && ferror (src_file)) { + perror_msg ("read"); + return -1; + } else if (nread == 0) { + if (chunksize != -1) { + error_msg ("Unable to read all data"); + return -1; + } + + return 0; + } + + nwritten = fwrite (buffer, 1, nread, dst_file); + + if (nwritten != nread) { + if (ferror (dst_file)) + perror_msg ("write"); + else + error_msg ("Unable to write all data"); + return -1; + } + + if (chunksize != -1) + chunksize -= nwritten; + } + + return 0; +} diff --git a/src/libbb/.svn/text-base/gz_open.c.svn-base b/src/libbb/.svn/text-base/gz_open.c.svn-base new file mode 100644 index 0000000..bdc7564 --- /dev/null +++ b/src/libbb/.svn/text-base/gz_open.c.svn-base @@ -0,0 +1,149 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 <sys/wait.h> +#include <signal.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include "libbb.h" + +static int gz_use_vfork; + +FILE * +gz_open(FILE *compressed_file, int *pid) +{ + int unzip_pipe[2]; + off_t floc; + int cfile = -1; + + gz_use_vfork = (getenv("OPKG_USE_VFORK") != NULL); + + if (gz_use_vfork) { + /* Create a new file descriptor for the input stream + * (it *must* be associated with a file), and lseek() + * to the same position in that fd as the stream. + */ + cfile = dup(fileno(compressed_file)); + floc = ftello(compressed_file); + lseek(cfile, floc, SEEK_SET); + setenv("GZIP", "--quiet", 0); + } + + if (pipe(unzip_pipe)!=0) { + perror_msg("pipe"); + return(NULL); + } + + /* If we don't flush, we end up with two copies of anything pending, + one from the parent, one from the child */ + fflush(stdout); + fflush(stderr); + + if (gz_use_vfork) { + *pid = vfork(); + } else { + *pid = fork(); + } + + if (*pid<0) { + perror_msg("fork"); + return(NULL); + } + + if (*pid==0) { + /* child process */ + close(unzip_pipe[0]); + if (gz_use_vfork) { + dup2(unzip_pipe[1], 1); + dup2(cfile, 0); + execlp("gunzip","gunzip",NULL); + /* If we get here, we had a failure */ + _exit(EXIT_FAILURE); + } else { + unzip(compressed_file, fdopen(unzip_pipe[1], "w")); + fflush(NULL); + fclose(compressed_file); + close(unzip_pipe[1]); + _exit(EXIT_SUCCESS); + } + } + /* Parent process is executing here */ + if (gz_use_vfork) { + close(cfile); + } + close(unzip_pipe[1]); + return(fdopen(unzip_pipe[0], "r")); +} + +int +gz_close(int gunzip_pid) +{ + int status; + int ret; + + if (gz_use_vfork) { + /* The gunzip process remains running in the background if we + * used the vfork()/exec() technique - so we have to kill it + * forcibly. There might be a better way to do this, but that + * affect a lot of other parts of opkg, and this works fine. + */ + if (kill(gunzip_pid, SIGTERM) == -1) { + perror_msg("gz_close(): unable to kill gunzip pid."); + return -1; + } + } + + + if (waitpid(gunzip_pid, &status, 0) == -1) { + perror_msg("waitpid"); + return -1; + } + + if (gz_use_vfork) { + /* Bail out here if we used the vfork()/exec() technique. */ + return 0; + } + + if (WIFSIGNALED(status)) { + error_msg("Unzip process killed by signal %d.\n", + WTERMSIG(status)); + return -1; + } + + if (!WIFEXITED(status)) { + /* shouldn't happen */ + error_msg("Your system is broken: got status %d from waitpid.\n", + status); + return -1; + } + + if ((ret = WEXITSTATUS(status))) { + error_msg("Unzip process failed with return code %d.\n", + ret); + return -1; + } + + return 0; +} diff --git a/src/libbb/.svn/text-base/last_char_is.c.svn-base b/src/libbb/.svn/text-base/last_char_is.c.svn-base new file mode 100644 index 0000000..26c2423 --- /dev/null +++ b/src/libbb/.svn/text-base/last_char_is.c.svn-base @@ -0,0 +1,40 @@ +/* + * busybox library eXtended function + * + * Copyright (C) 2001 Larry Doolittle, <ldoolitt@recycle.lbl.gov> + * + * 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 <string.h> +#include "libbb.h" + +/* Find out if the last character of a string matches the one given Don't + * underrun the buffer if the string length is 0. Also avoids a possible + * space-hogging inline of strlen() per usage. + */ +char * last_char_is(const char *s, int c) +{ + char *sret; + if (!s) + return NULL; + sret = (char *)s+strlen(s)-1; + if (sret>=s && *sret == c) { + return sret; + } else { + return NULL; + } +} diff --git a/src/libbb/.svn/text-base/libbb.h.svn-base b/src/libbb/.svn/text-base/libbb.h.svn-base new file mode 100644 index 0000000..4e1fafc --- /dev/null +++ b/src/libbb/.svn/text-base/libbb.h.svn-base @@ -0,0 +1,123 @@ +/* vi: set sw=4 ts=4: */ +/* + * Busybox main internal header file + * + * 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 + */ + +#ifndef __LIBBB_H__ +#define __LIBBB_H__ 1 + +#include <stdio.h> +#include <stdarg.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <stdlib.h> +#include <netdb.h> + +#include "../libopkg/opkg_message.h" + +#ifndef FALSE +#define FALSE ((int) 0) +#endif + +#ifndef TRUE +#define TRUE ((int) 1) +#endif + +#define error_msg(fmt, args...) opkg_msg(ERROR, fmt"\n", ##args) +#define perror_msg(fmt, args...) opkg_perror(ERROR, fmt, ##args) +#define error_msg_and_die(fmt, args...) \ + do { \ + error_msg(fmt, ##args); \ + exit(EXIT_FAILURE); \ + } while (0) +#define perror_msg_and_die(fmt, args...) \ + do { \ + perror_msg(fmt, ##args); \ + exit(EXIT_FAILURE); \ + } while (0) + +extern void archive_xread_all(int fd, char *buf, size_t count); + +const char *mode_string(int mode); +const char *time_string(time_t timeVal); + +int copy_file(const char *source, const char *dest, int flags); +int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); +ssize_t safe_read(int fd, void *buf, size_t count); +ssize_t full_read(int fd, char *buf, int len); + +extern int parse_mode( const char* s, mode_t* theMode); + +extern FILE *wfopen(const char *path, const char *mode); +extern FILE *xfopen(const char *path, const char *mode); + +extern void *xmalloc (size_t size); +extern void *xrealloc(void *old, size_t size); +extern void *xcalloc(size_t nmemb, size_t size); +extern char *xstrdup (const char *s); +extern char *xstrndup (const char *s, int n); +extern char *safe_strncpy(char *dst, const char *src, size_t size); + +char *xreadlink(const char *path); +char *concat_path_file(const char *path, const char *filename); +char *last_char_is(const char *s, int c); + +typedef struct file_headers_s { + char *name; + char *link_name; + off_t size; + uid_t uid; + gid_t gid; + mode_t mode; + time_t mtime; + dev_t device; +} file_header_t; + +enum extract_functions_e { + extract_verbose_list = 1, + extract_list = 2, + extract_one_to_buffer = 4, + extract_to_stream = 8, + extract_all_to_fs = 16, + extract_preserve_date = 32, + extract_data_tar_gz = 64, + extract_control_tar_gz = 128, + extract_unzip_only = 256, + extract_unconditional = 512, + extract_create_leading_dirs = 1024, + extract_quiet = 2048, + extract_exclude_list = 4096 +}; + +char *deb_extract(const char *package_filename, FILE *out_stream, + const int extract_function, const char *prefix, + const char *filename, int *err); + +extern int unzip(FILE *l_in_file, FILE *l_out_file); +extern int gz_close(int gunzip_pid); +extern FILE *gz_open(FILE *compressed_file, int *pid); + +int make_directory (const char *path, long mode, int flags); + +enum { + FILEUTILS_PRESERVE_STATUS = 1, + FILEUTILS_PRESERVE_SYMLINKS = 2, + FILEUTILS_RECUR = 4, + FILEUTILS_FORCE = 8, +}; + +#endif /* __LIBBB_H__ */ diff --git a/src/libbb/.svn/text-base/make_directory.c.svn-base b/src/libbb/.svn/text-base/make_directory.c.svn-base new file mode 100644 index 0000000..86ab554 --- /dev/null +++ b/src/libbb/.svn/text-base/make_directory.c.svn-base @@ -0,0 +1,80 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini make_directory implementation for busybox + * + * Copyright (C) 2001 Matt Kraai. + * + * 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 <errno.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> +#include <stdlib.h> +#include <libgen.h> + +#include "libbb.h" + +/* Create the directory PATH with mode MODE, or the default if MODE is -1. + * Also create parent directories as necessary if flags contains + * FILEUTILS_RECUR. */ + +int make_directory (const char *path, long mode, int flags) +{ + if (!(flags & FILEUTILS_RECUR)) { + if (mkdir (path, 0777) < 0) { + perror_msg ("Cannot create directory `%s'", path); + return -1; + } + + if (mode != -1 && chmod (path, mode) < 0) { + perror_msg ("Cannot set permissions of directory `%s'", path); + return -1; + } + } else { + struct stat st; + + if (stat (path, &st) < 0 && errno == ENOENT) { + int status; + char *pathcopy, *parent, *parentcopy; + mode_t mask; + + mask = umask (0); + umask (mask); + + /* dirname is unsafe, it may both modify the + memory of the path argument and may return + a pointer to static memory, which can then + be modified by consequtive calls to dirname */ + + pathcopy = xstrdup (path); + parent = dirname (pathcopy); + parentcopy = xstrdup (parent); + status = make_directory (parentcopy, (0777 & ~mask) + | 0300, FILEUTILS_RECUR); + free (pathcopy); + free (parentcopy); + + + if (status < 0 || make_directory (path, mode, 0) < 0) + return -1; + } + } + + return 0; +} diff --git a/src/libbb/.svn/text-base/mode_string.c.svn-base b/src/libbb/.svn/text-base/mode_string.c.svn-base new file mode 100644 index 0000000..12dc179 --- /dev/null +++ b/src/libbb/.svn/text-base/mode_string.c.svn-base @@ -0,0 +1,78 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 <stdio.h> +#include "libbb.h" + + + +#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) +#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) + +/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ +static const mode_t SBIT[] = { + 0, 0, S_ISUID, + 0, 0, S_ISGID, + 0, 0, S_ISVTX +}; + +/* The 9 mode bits to test */ +static const mode_t MBIT[] = { + S_IRUSR, S_IWUSR, S_IXUSR, + S_IRGRP, S_IWGRP, S_IXGRP, + S_IROTH, S_IWOTH, S_IXOTH +}; + +static const char MODE1[] = "rwxrwxrwx"; +static const char MODE0[] = "---------"; +static const char SMODE1[] = "..s..s..t"; +static const char SMODE0[] = "..S..S..T"; + +/* + * Return the standard ls-like mode string from a file mode. + * This is static and so is overwritten on each call. + */ +const char *mode_string(int mode) +{ + static char buf[12]; + + int i; + + buf[0] = TYPECHAR(mode); + for (i = 0; i < 9; i++) { + if (mode & SBIT[i]) + buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i]; + else + buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i]; + } + return buf; +} + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/.svn/text-base/parse_mode.c.svn-base b/src/libbb/.svn/text-base/parse_mode.c.svn-base new file mode 100644 index 0000000..02668c7 --- /dev/null +++ b/src/libbb/.svn/text-base/parse_mode.c.svn-base @@ -0,0 +1,134 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 <stdio.h> +#include <stdlib.h> +#include "libbb.h" + + +/* This function parses the sort of string you might pass + * to chmod (i.e., [ugoa]{+|-|=}[rwxst] ) and returns the + * correct mode described by the string. */ +extern int parse_mode(const char *s, mode_t * theMode) +{ + static const mode_t group_set[] = { + S_ISUID | S_IRWXU, /* u */ + S_ISGID | S_IRWXG, /* g */ + S_IRWXO, /* o */ + S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */ + }; + + static const mode_t mode_set[] = { + S_IRUSR | S_IRGRP | S_IROTH, /* r */ + S_IWUSR | S_IWGRP | S_IWOTH, /* w */ + S_IXUSR | S_IXGRP | S_IXOTH, /* x */ + S_ISUID | S_ISGID, /* s */ + S_ISVTX /* t */ + }; + + static const char group_chars[] = "ugoa"; + static const char mode_chars[] = "rwxst"; + + const char *p; + + mode_t andMode = + S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; + mode_t orMode = 0; + mode_t mode; + mode_t groups; + char type; + char c; + + if (s==NULL) { + return (FALSE); + } + + do { + mode = 0; + groups = 0; + NEXT_GROUP: + if ((c = *s++) == '\0') { + return -1; + } + for (p=group_chars ; *p ; p++) { + if (*p == c) { + groups |= group_set[(int)(p-group_chars)]; + goto NEXT_GROUP; + } + } + switch (c) { + case '=': + case '+': + case '-': + type = c; + if (groups == 0) { /* The default is "all" */ + groups |= S_ISUID | S_ISGID | S_ISVTX + | S_IRWXU | S_IRWXG | S_IRWXO; + } + break; + default: + if ((c < '0') || (c > '7') || (mode | groups)) { + return (FALSE); + } else { + *theMode = strtol(--s, NULL, 8); + return (TRUE); + } + } + + NEXT_MODE: + if (((c = *s++) != '\0') && (c != ',')) { + for (p=mode_chars ; *p ; p++) { + if (*p == c) { + mode |= mode_set[(int)(p-mode_chars)]; + goto NEXT_MODE; + } + } + break; /* We're done so break out of loop.*/ + } + switch (type) { + case '=': + andMode &= ~(groups); /* Now fall through. */ + case '+': + orMode |= mode & groups; + break; + case '-': + andMode &= ~(mode & groups); + orMode &= ~(mode & groups); + break; + } + } while (c == ','); + + *theMode &= andMode; + *theMode |= orMode; + + return TRUE; +} + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/.svn/text-base/safe_strncpy.c.svn-base b/src/libbb/.svn/text-base/safe_strncpy.c.svn-base new file mode 100644 index 0000000..eb2dbab --- /dev/null +++ b/src/libbb/.svn/text-base/safe_strncpy.c.svn-base @@ -0,0 +1,42 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.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 + */ + +#include <string.h> +#include "libbb.h" + + + +/* Like strncpy but make sure the resulting string is always 0 terminated. */ +extern char * safe_strncpy(char *dst, const char *src, size_t size) +{ + dst[size-1] = '\0'; + return strncpy(dst, src, size-1); +} + + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/.svn/text-base/time_string.c.svn-base b/src/libbb/.svn/text-base/time_string.c.svn-base new file mode 100644 index 0000000..d103a02 --- /dev/null +++ b/src/libbb/.svn/text-base/time_string.c.svn-base @@ -0,0 +1,62 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.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 + */ + +#include <stdio.h> +#include <string.h> +#include <time.h> +#include <utime.h> +#include "libbb.h" + + +/* + * Return the standard ls-like time string from a time_t + * This is static and so is overwritten on each call. + */ +const char *time_string(time_t timeVal) +{ + time_t now; + char *str; + static char buf[26]; + + time(&now); + + str = ctime(&timeVal); + + strcpy(buf, &str[4]); + buf[12] = '\0'; + + if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { + strcpy(&buf[7], &str[20]); + buf[11] = '\0'; + } + + return buf; +} + + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/.svn/text-base/unarchive.c.svn-base b/src/libbb/.svn/text-base/unarchive.c.svn-base new file mode 100644 index 0000000..5d4464f --- /dev/null +++ b/src/libbb/.svn/text-base/unarchive.c.svn-base @@ -0,0 +1,784 @@ +/* + * Copyright (C) 2000 by Glenn McGrath + * Copyright (C) 2001 by Laurence Anderson + * + * Based on previous work by busybox developers and others. + * + * 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 <errno.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <utime.h> +#include <libgen.h> + +#include "libbb.h" + +#define CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY 1 +#define CONFIG_FEATURE_TAR_GNU_EXTENSIONS + +#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS +static char *longname = NULL; +static char *linkname = NULL; +#endif + +off_t archive_offset; + +#define SEEK_BUF 4096 +static ssize_t +seek_by_read(FILE* fd, size_t len) +{ + ssize_t cc, total = 0; + char buf[SEEK_BUF]; + + while (len) { + cc = fread(buf, sizeof(buf[0]), + len > SEEK_BUF ? SEEK_BUF : len, + fd); + + total += cc; + len -= cc; + + if(feof(fd) || ferror(fd)) + break; + } + return total; +} + +static void +seek_sub_file(FILE *fd, const int count) +{ + archive_offset += count; + + /* Do not use fseek() on a pipe. It may fail with ESPIPE, leaving the + * stream at an undefined location. + */ + seek_by_read(fd, count); + + return; +} + + +/* Extract the data postioned at src_stream to either filesystem, stdout or + * buffer depending on the value of 'function' which is defined in libbb.h + * + * prefix doesnt have to be just a directory, it may prefix the filename as well. + * + * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath + * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have + * 'dpkg.' as their prefix + * + * For this reason if prefix does point to a dir then it must end with a + * trailing '/' or else the last dir will be assumed to be the file prefix + */ +static char * +extract_archive(FILE *src_stream, FILE *out_stream, + const file_header_t *file_entry, const int function, + const char *prefix, + int *err) +{ + FILE *dst_stream = NULL; + char *full_name = NULL; + char *full_link_name = NULL; + char *buffer = NULL; + struct utimbuf t; + + *err = 0; + + /* prefix doesnt have to be a proper path it may prepend + * the filename as well */ + if (prefix != NULL) { + /* strip leading '/' in filename to extract as prefix may not be dir */ + /* Cant use concat_path_file here as prefix might not be a directory */ + char *path = file_entry->name; + if (strncmp("./", path, 2) == 0) { + path += 2; + if (strlen(path) == 0) + /* Do nothing, current dir already exists. */ + return NULL; + } + full_name = xmalloc(strlen(prefix) + strlen(path) + 1); + strcpy(full_name, prefix); + strcat(full_name, path); + if ( file_entry->link_name ){ + full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1); + strcpy(full_link_name, prefix); + strcat(full_link_name, file_entry->link_name); + } + } else { + full_name = xstrdup(file_entry->name); + if ( file_entry->link_name ) + full_link_name = xstrdup(file_entry->link_name); + } + + + if (function & extract_to_stream) { + if (S_ISREG(file_entry->mode)) { + *err = copy_file_chunk(src_stream, out_stream, file_entry->size); + archive_offset += file_entry->size; + } + } + else if (function & extract_one_to_buffer) { + if (S_ISREG(file_entry->mode)) { + buffer = (char *) xmalloc(file_entry->size + 1); + fread(buffer, 1, file_entry->size, src_stream); + buffer[file_entry->size] = '\0'; + archive_offset += file_entry->size; + goto cleanup; + } + } + else if (function & extract_all_to_fs) { + struct stat oldfile; + int stat_res; + stat_res = lstat (full_name, &oldfile); + if (stat_res == 0) { /* The file already exists */ + if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) { + if (!S_ISDIR(oldfile.st_mode)) { + unlink(full_name); /* Directories might not be empty etc */ + } + } else { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + error_msg("%s not created: newer or same age file exists", file_entry->name); + } + seek_sub_file(src_stream, file_entry->size); + goto cleanup; + } + } + if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */ + char *buf, *parent; + buf = xstrdup(full_name); + parent = dirname(buf); + if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + error_msg("couldn't create leading directories"); + } + } + free (buf); + } + switch(file_entry->mode & S_IFMT) { + case S_IFREG: + if (file_entry->link_name) { /* Found a cpio hard link */ + if (link(full_link_name, full_name) != 0) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg("Cannot link from %s to '%s'", + file_entry->name, file_entry->link_name); + } + } + } else { + if ((dst_stream = wfopen(full_name, "w")) == NULL) { + *err = -1; + seek_sub_file(src_stream, file_entry->size); + goto cleanup; + } + archive_offset += file_entry->size; + *err = copy_file_chunk(src_stream, dst_stream, file_entry->size); + fclose(dst_stream); + } + break; + case S_IFDIR: + if (stat_res != 0) { + if (mkdir(full_name, file_entry->mode) < 0) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg("Cannot make dir %s", full_name); + } + } + } + break; + case S_IFLNK: + if (symlink(file_entry->link_name, full_name) < 0) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name); + } + goto cleanup; + } + break; + case S_IFSOCK: + case S_IFBLK: + case S_IFCHR: + case S_IFIFO: + if (mknod(full_name, file_entry->mode, file_entry->device) == -1) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg("Cannot create node %s", file_entry->name); + } + goto cleanup; + } + break; + default: + *err = -1; + perror_msg("Don't know how to handle %s", full_name); + + } + + /* Changing a symlink's properties normally changes the properties of the + * file pointed to, so dont try and change the date or mode, lchown does + * does the right thing, but isnt available in older versions of libc */ + if (S_ISLNK(file_entry->mode)) { +#if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1) + lchown(full_name, file_entry->uid, file_entry->gid); +#endif + } else { + if (function & extract_preserve_date) { + t.actime = file_entry->mtime; + t.modtime = file_entry->mtime; + utime(full_name, &t); + } + chown(full_name, file_entry->uid, file_entry->gid); + chmod(full_name, file_entry->mode); + } + } else { + /* If we arent extracting data we have to skip it, + * if data size is 0 then then just do it anyway + * (saves testing for it) */ + seek_sub_file(src_stream, file_entry->size); + } + + /* extract_list and extract_verbose_list can be used in conjunction + * with one of the above four extraction functions, so do this seperately */ + if (function & extract_verbose_list) { + fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode), + file_entry->uid, file_entry->gid, + (int) file_entry->size, time_string(file_entry->mtime)); + } + if ((function & extract_list) || (function & extract_verbose_list)){ + /* fputs doesnt add a trailing \n, so use fprintf */ + fprintf(out_stream, "%s\n", file_entry->name); + } + +cleanup: + free(full_name); + if ( full_link_name ) + free(full_link_name); + + return buffer; +} + +static char * +unarchive(FILE *src_stream, FILE *out_stream, + file_header_t *(*get_headers)(FILE *), + void (*free_headers)(file_header_t *), + const int extract_function, + const char *prefix, + const char **extract_names, + int *err) +{ + file_header_t *file_entry; + int extract_flag; + int i; + char *buffer = NULL; + + *err = 0; + + archive_offset = 0; + while ((file_entry = get_headers(src_stream)) != NULL) { + extract_flag = TRUE; + + if (extract_names != NULL) { + int found_flag = FALSE; + char *p = file_entry->name; + + if (p[0] == '.' && p[1] == '/') + p += 2; + + for(i = 0; extract_names[i] != 0; i++) { + if (strcmp(extract_names[i], p) == 0) { + found_flag = TRUE; + break; + } + } + if (extract_function & extract_exclude_list) { + if (found_flag == TRUE) { + extract_flag = FALSE; + } + } else { + /* If its not found in the include list dont extract it */ + if (found_flag == FALSE) { + extract_flag = FALSE; + } + } + } + + if (extract_flag == TRUE) { + buffer = extract_archive(src_stream, out_stream, + file_entry, extract_function, + prefix, err); + *err = 0; /* XXX: ignore extraction errors */ + if (*err) { + free_headers(file_entry); + break; + } + } else { + /* seek past the data entry */ + seek_sub_file(src_stream, file_entry->size); + } + free_headers(file_entry); + } + + return buffer; +} + +static file_header_t * +get_header_ar(FILE *src_stream) +{ + file_header_t *typed; + 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; + static char *ar_long_names; + + if (fread(ar.raw, 1, 60, src_stream) != 60) { + return(NULL); + } + archive_offset += 60; + /* align the headers based on the header magic */ + if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) { + /* some version of ar, have an extra '\n' after each data entry, + * this puts the next header out by 1 */ + if (ar.formated.magic[1] != '`') { + error_msg("Invalid magic"); + return(NULL); + } + /* read the next char out of what would be the data section, + * if its a '\n' then it is a valid header offset by 1*/ + archive_offset++; + if (fgetc(src_stream) != '\n') { + error_msg("Invalid magic"); + return(NULL); + } + /* fix up the header, we started reading 1 byte too early */ + /* raw_header[60] wont be '\n' as it should, but it doesnt matter */ + memmove(ar.raw, &ar.raw[1], 59); + } + + typed = (file_header_t *) xcalloc(1, sizeof(file_header_t)); + + typed->size = (size_t) atoi(ar.formated.size); + /* long filenames have '/' as the first character */ + if (ar.formated.name[0] == '/') { + 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_names = (char *) xrealloc(ar_long_names, typed->size); + fread(ar_long_names, 1, typed->size, src_stream); + archive_offset += typed->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(src_stream)); /* Return next header */ + } else if (ar.formated.name[1] == ' ') { + /* This is the index of symbols in the file for compilers */ + seek_sub_file(src_stream, typed->size); + return (get_header_ar(src_stream)); /* 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 */ + if (!ar_long_names) { + error_msg("Cannot resolve long file name"); + return (NULL); + } + typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1])); + } + } else { + /* short filenames */ + typed->name = xcalloc(1, 16); + strncpy(typed->name, ar.formated.name, 16); + } + typed->name[strcspn(typed->name, " /")]='\0'; + + /* convert the rest of the now valid char header to its typed struct */ + parse_mode(ar.formated.mode, &typed->mode); + typed->mtime = atoi(ar.formated.date); + typed->uid = atoi(ar.formated.uid); + typed->gid = atoi(ar.formated.gid); + + return(typed); +} + +static void +free_header_ar(file_header_t *ar_entry) +{ + if (ar_entry == NULL) + return; + + free(ar_entry->name); + if (ar_entry->link_name) + free(ar_entry->link_name); + + free(ar_entry); +} + + +static file_header_t * +get_header_tar(FILE *tar_stream) +{ + 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; + file_header_t *tar_entry = NULL; + long i; + long sum = 0; + + if (archive_offset % 512 != 0) { + seek_sub_file(tar_stream, 512 - (archive_offset % 512)); + } + + if (fread(tar.raw, 1, 512, tar_stream) != 512) { + /* Unfortunately its common for tar files to have all sorts of + * trailing garbage, fail silently */ +// error_msg("Couldnt read header"); + return(NULL); + } + archive_offset += 512; + + /* Check header has valid magic, unfortunately some tar files + * have empty (0'ed) tar entries at the end, which will + * cause this to fail, so fail silently for now + */ + 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 + return(NULL); + } + + /* 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)) { + if ( strtol(tar.formated.chksum,NULL,8) != 0 ) + error_msg("Invalid tar header checksum"); + return(NULL); + } + + /* convert to type'ed variables */ + tar_entry = xcalloc(1, sizeof(file_header_t)); + + + + // tar_entry->name = xstrdup(tar.formated.name); + +/* + parse_mode(tar.formated.mode, &tar_entry->mode); +*/ + tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8); + + tar_entry->uid = strtol(tar.formated.uid, NULL, 8); + tar_entry->gid = strtol(tar.formated.gid, NULL, 8); + tar_entry->size = strtol(tar.formated.size, NULL, 8); + tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8); + + tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) + + strtol(tar.formated.devminor, NULL, 8); + + /* Fix mode, used by the old format */ + switch (tar.formated.typeflag) { + /* hard links are detected as regular files with 0 size and a link name */ + case '1': + tar_entry->mode |= S_IFREG ; + break; + case 0: + case '0': + +# ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY + if (last_char_is(tar_entry->name, '/')) { + tar_entry->mode |= S_IFDIR; + } else +# endif + tar_entry->mode |= S_IFREG; + break; + case '2': + tar_entry->mode |= S_IFLNK; + break; + case '3': + tar_entry->mode |= S_IFCHR; + break; + case '4': + tar_entry->mode |= S_IFBLK; + break; + case '5': + tar_entry->mode |= S_IFDIR; + break; + case '6': + tar_entry->mode |= S_IFIFO; + break; +# ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS + case 'L': { + longname = xmalloc(tar_entry->size + 1); + if(fread(longname, tar_entry->size, 1, tar_stream) != 1) + return NULL; + longname[tar_entry->size] = '\0'; + archive_offset += tar_entry->size; + + return(get_header_tar(tar_stream)); + } + case 'K': { + linkname = xmalloc(tar_entry->size + 1); + if(fread(linkname, tar_entry->size, 1, tar_stream) != 1) + return NULL; + linkname[tar_entry->size] = '\0'; + archive_offset += tar_entry->size; + + return(get_header_tar(tar_stream)); + } + case 'D': + case 'M': + case 'N': + case 'S': + case 'V': + perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag); +# endif + default: + perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag); + break; + + } + + +#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS + if (longname) { + tar_entry->name = longname; + longname = NULL; + } else +#endif + { + tar_entry->name = xstrndup(tar.formated.name, 100); + + if (tar.formated.prefix[0]) { + char *temp = tar_entry->name; + char *prefixTemp = xstrndup(tar.formated.prefix, 155); + tar_entry->name = concat_path_file(prefixTemp, temp); + free(temp); + free(prefixTemp); + } + } + + if (linkname) { + tar_entry->link_name = linkname; + linkname = NULL; + } else + { + tar_entry->link_name = *tar.formated.linkname != '\0' ? + xstrndup(tar.formated.linkname, 100) : NULL; + } + + return(tar_entry); +} + +static void +free_header_tar(file_header_t *tar_entry) +{ + if (tar_entry == NULL) + return; + + free(tar_entry->name); + if (tar_entry->link_name) + free(tar_entry->link_name); + + free(tar_entry); +} + +char * +deb_extract(const char *package_filename, FILE *out_stream, + const int extract_function, const char *prefix, + const char *filename, int *err) +{ + FILE *deb_stream = NULL; + file_header_t *ar_header = NULL; + const char **file_list = NULL; + char *output_buffer = NULL; + char *ared_file = NULL; + char ar_magic[8]; + int gz_err; + + *err = 0; + + if (filename != NULL) { + file_list = xmalloc(sizeof(char *) * 2); + file_list[0] = filename; + file_list[1] = NULL; + } + + if (extract_function & extract_control_tar_gz) { + ared_file = "control.tar.gz"; + } + else if (extract_function & extract_data_tar_gz) { + ared_file = "data.tar.gz"; + } else { + opkg_msg(ERROR, "Internal error: extract_function=%x\n", + extract_function); + *err = -1; + goto cleanup; + } + + /* open the debian package to be worked on */ + deb_stream = wfopen(package_filename, "r"); + if (deb_stream == NULL) { + *err = -1; + goto cleanup; + } + /* set the buffer size */ + setvbuf(deb_stream, NULL, _IOFBF, 0x8000); + + /* check ar magic */ + fread(ar_magic, 1, 8, deb_stream); + + if (strncmp(ar_magic,"!<arch>",7) == 0) { + archive_offset = 8; + + while ((ar_header = get_header_ar(deb_stream)) != NULL) { + if (strcmp(ared_file, ar_header->name) == 0) { + int gunzip_pid = 0; + FILE *uncompressed_stream; + /* open a stream of decompressed data */ + uncompressed_stream = gz_open(deb_stream, &gunzip_pid); + if (uncompressed_stream == NULL) { + *err = -1; + goto cleanup; + } + + archive_offset = 0; + output_buffer = unarchive(uncompressed_stream, + out_stream, get_header_tar, + free_header_tar, + extract_function, prefix, + file_list, err); + fclose(uncompressed_stream); + gz_err = gz_close(gunzip_pid); + if (gz_err) + *err = -1; + free_header_ar(ar_header); + break; + } + if (fseek(deb_stream, ar_header->size, SEEK_CUR) == -1) { + opkg_perror(ERROR, "Couldn't fseek into %s", package_filename); + *err = -1; + free_header_ar(ar_header); + goto cleanup; + } + free_header_ar(ar_header); + } + goto cleanup; + } else if (strncmp(ar_magic, "\037\213", 2) == 0) { + /* it's a gz file, let's assume it's an opkg */ + int unzipped_opkg_pid; + FILE *unzipped_opkg_stream; + file_header_t *tar_header; + archive_offset = 0; + if (fseek(deb_stream, 0, SEEK_SET) == -1) { + opkg_perror(ERROR, "Couldn't fseek into %s", package_filename); + *err = -1; + goto cleanup; + } + unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid); + if (unzipped_opkg_stream == NULL) { + *err = -1; + goto cleanup; + } + + /* walk through outer tar file to find ared_file */ + while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) { + int name_offset = 0; + if (strncmp(tar_header->name, "./", 2) == 0) + name_offset = 2; + if (strcmp(ared_file, tar_header->name+name_offset) == 0) { + int gunzip_pid = 0; + FILE *uncompressed_stream; + /* open a stream of decompressed data */ + uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid); + if (uncompressed_stream == NULL) { + *err = -1; + goto cleanup; + } + archive_offset = 0; + + output_buffer = unarchive(uncompressed_stream, + out_stream, + get_header_tar, + free_header_tar, + extract_function, + prefix, + file_list, + err); + + free_header_tar(tar_header); + fclose(uncompressed_stream); + gz_err = gz_close(gunzip_pid); + if (gz_err) + *err = -1; + break; + } + seek_sub_file(unzipped_opkg_stream, tar_header->size); + free_header_tar(tar_header); + } + fclose(unzipped_opkg_stream); + gz_err = gz_close(unzipped_opkg_pid); + if (gz_err) + *err = -1; + + goto cleanup; + } else { + *err = -1; + error_msg("%s: invalid magic", package_filename); + } + +cleanup: + if (deb_stream) + fclose(deb_stream); + if (file_list) + free(file_list); + + return output_buffer; +} diff --git a/src/libbb/.svn/text-base/unzip.c.svn-base b/src/libbb/.svn/text-base/unzip.c.svn-base new file mode 100644 index 0000000..435effb --- /dev/null +++ b/src/libbb/.svn/text-base/unzip.c.svn-base @@ -0,0 +1,1021 @@ +/* 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 <andersee@debian.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> + * + * 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. + */ + +#include <sys/types.h> +#include <sys/wait.h> +#include <signal.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include "libbb.h" + +static FILE *in_file, *out_file; + +static unsigned char *window; +static unsigned long *crc_table = NULL; + +static unsigned long crc; /* shift register contents */ + +/* + * window size--must be a power of two, and + * at least 32K for zip's deflate method + */ +static const int WSIZE = 0x8000; + +/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ +static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */ +static const int N_MAX = 288; /* maximum number of codes in any set */ + +static long bytes_out; /* number of output bytes */ +static unsigned long outcnt; /* bytes in output buffer */ + +static unsigned hufts; /* track memory usage */ +static unsigned long bb; /* bit buffer */ +static unsigned bk; /* bits in bit buffer */ + +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 const unsigned short mask_bits[] = { + 0x0000, + 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, + 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff +}; + +//static int error_number = 0; +/* ======================================================================== + * Signal and error handler. + */ + +static void abort_gzip() +{ + error_msg("gzip aborted\n"); + _exit(-1); +} + +static void make_crc_table() +{ + unsigned long table_entry; /* crc shift register */ + unsigned long poly = 0; /* polynomial exclusive-or pattern */ + int i; /* counter for all possible eight bit values */ + int k; /* byte being shifted into crc apparatus */ + + /* terms of polynomial defining this crc (except x^32): */ + static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; + + /* initial shift register value */ + crc = 0xffffffffL; + crc_table = (unsigned long *) xmalloc(256 * sizeof(unsigned long)); + + /* Make exclusive-or pattern from polynomial (0xedb88320) */ + for (i = 0; i < sizeof(p)/sizeof(int); i++) + poly |= 1L << (31 - p[i]); + + /* Compute and print table of CRC's, five per line */ + for (i = 0; i < 256; i++) { + 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--) { + table_entry = table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >> 1; + } + crc_table[i]=table_entry; + } +} + +/* =========================================================================== + * Write the output window window[0..outcnt-1] and update crc and bytes_out. + * (Used for the decompressed data only.) + */ +static void flush_window(void) +{ + int n; + + if (outcnt == 0) + return; + + for (n = 0; n < outcnt; n++) { + crc = crc_table[((int) crc ^ (window[n])) & 0xff] ^ (crc >> 8); + } + + if (fwrite(window, 1, outcnt, out_file) != outcnt) { + /* + * The Parent process may not be interested in all the data we have, + * in which case it will rudely close its end of the pipe and + * wait for us to exit. + */ + if (errno == EPIPE) + _exit(EXIT_SUCCESS); + + error_msg("Couldnt write"); + _exit(EXIT_FAILURE); + } + bytes_out += (unsigned long) outcnt; + outcnt = 0; +} + +/* + * 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, *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 short *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 */ + unsigned i; /* counter, current code */ + unsigned j; /* counter */ + int k; /* number of bits in current code */ + int l; /* bits per table (returned in m) */ + unsigned *p; /* pointer into c[], b[], or v[] */ + 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 */ + 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 */ + if ((q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t))) == NULL) { + if (h) { + huft_free(u[0]); + } + return 3; /* not enough memory */ + } + hufts += z + 1; /* track memory usage */ + *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 *tl, huft_t *td, int bl, int bd) +{ + unsigned long e; /* table entry flag/number of extra bits */ + unsigned long n, d; /* length and index for copy */ + unsigned long w; /* current window position */ + huft_t *t; /* pointer to table entry */ + unsigned ml, md; /* masks for bl and bd bits */ + unsigned long b; /* bit buffer */ + unsigned k; /* number of bits in bit buffer */ + + /* make local copies of globals */ + b = bb; /* initialize bit buffer */ + k = bk; + w = outcnt; /* initialize window position */ + + /* inflate the coded data */ + ml = mask_bits[bl]; /* precompute masks for speed */ + md = mask_bits[bd]; + for (;;) { /* do until end of block */ + while (k < (unsigned) bl) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) + do { + if (e == 99) { + return 1; + } + b >>= t->b; + k -= t->b; + e -= 16; + while (k < e) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + } 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 */ + window[w++] = (unsigned char) t->v.n; + if (w == WSIZE) { + outcnt=(w), + flush_window(); + w = 0; + } + } else { /* it's an EOB or a length */ + + /* exit if end of block */ + if (e == 15) { + break; + } + + /* get length of block to copy */ + while (k < e) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + n = t->v.n + ((unsigned) b & mask_bits[e]); + b >>= e; + k -= e; + + /* decode distance of block to copy */ + while (k < (unsigned) bd) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + + if ((e = (t = td + ((unsigned) b & md))->e) > 16) + do { + if (e == 99) + return 1; + b >>= t->b; + k -= t->b; + e -= 16; + while (k < e) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); + b >>= t->b; + k -= t->b; + while (k < e) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + d = w - t->v.n - ((unsigned) b & mask_bits[e]); + b >>= e; + k -= e; + + /* do the copy */ + do { + n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e); +#if !defined(NOMEMCPY) && !defined(DEBUG) + if (w - d >= e) { /* (this test assumes unsigned comparison) */ + memcpy(window + w, window + d, e); + w += e; + d += e; + } else /* do it slow to avoid memcpy() overlap */ +#endif /* !NOMEMCPY */ + do { + window[w++] = window[d++]; + } while (--e); + if (w == WSIZE) { + outcnt=(w), + flush_window(); + w = 0; + } + } while (n); + } + } + + /* restore the globals from the locals */ + outcnt = w; /* restore global window pointer */ + bb = b; /* restore global bit buffer */ + bk = k; + + /* done */ + return 0; +} + +/* + * decompress an inflated block + * e: last block flag + * + * GLOBAL VARIABLES: bb, kk, + */ +static int inflate_block(int *e) +{ + unsigned t; /* block type */ + unsigned long b; /* bit buffer */ + unsigned k; /* number of bits in bit buffer */ + static unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */ + 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. */ + static unsigned short cplext[] = { /* Extra bits for literal codes 257..285 */ + 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 */ + static unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */ + 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 + }; + static unsigned short cpdext[] = { /* Extra bits for distance codes */ + 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 + }; + + /* make local bit buffer */ + b = bb; + k = bk; + + /* read in last block bit */ + while (k < 1) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + *e = (int) b & 1; + b >>= 1; + k -= 1; + + /* read in block type */ + while (k < 2) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + t = (unsigned) b & 3; + b >>= 2; + k -= 2; + + /* restore the global bit buffer */ + bb = b; + bk = k; + + /* inflate that block type */ + switch (t) { + case 0: /* Inflate stored */ + { + unsigned long n; /* number of bytes in block */ + unsigned long w; /* current window position */ + unsigned long b_stored; /* bit buffer */ + unsigned long k_stored; /* number of bits in bit buffer */ + + /* make local copies of globals */ + b_stored = bb; /* initialize bit buffer */ + k_stored = bk; + w = outcnt; /* initialize window position */ + + /* go to byte boundary */ + n = k_stored & 7; + b_stored >>= n; + k_stored -= n; + + /* get the length and its complement */ + while (k_stored < 16) { + b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; + k_stored += 8; + } + n = ((unsigned) b_stored & 0xffff); + b_stored >>= 16; + k_stored -= 16; + while (k_stored < 16) { + b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; + k_stored += 8; + } + if (n != (unsigned) ((~b_stored) & 0xffff)) { + return 1; /* error in compressed data */ + } + b_stored >>= 16; + k_stored -= 16; + + /* read and output the compressed data */ + while (n--) { + while (k_stored < 8) { + b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; + k_stored += 8; + } + window[w++] = (unsigned char) b_stored; + if (w == (unsigned long)WSIZE) { + outcnt=(w), + flush_window(); + w = 0; + } + b_stored >>= 8; + k_stored -= 8; + } + + /* restore the globals from the locals */ + outcnt = w; /* restore global window pointer */ + bb = b_stored; /* restore global bit buffer */ + bk = k_stored; + return 0; + } + 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 */ + int bl; /* lookup bits for tl */ + 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 */ + if (inflate_codes(tl, td, bl, bd)) { + huft_free(tl); + huft_free(td); + return 1; + } + + /* free the decoding tables, return */ + huft_free(tl); + huft_free(td); + return 0; + } + case 2: /* Inflate dynamic */ + { + /* Tables for deflate from PKZIP's appnote.txt. */ + static unsigned border[] = { /* Order of the bit length code lengths */ + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 + }; + int dbits = 6; /* bits in base distance lookup table */ + int lbits = 9; /* bits in base literal/length lookup table */ + + int i; /* temporary variables */ + unsigned j; + unsigned l; /* last length */ + unsigned m; /* mask for bit lengths table */ + unsigned n; /* number of lengths to get */ + huft_t *tl; /* literal/length code table */ + huft_t *td; /* distance code table */ + int bl; /* lookup bits for tl */ + int bd; /* lookup bits for td */ + unsigned nb; /* number of bit length codes */ + unsigned nl; /* number of literal/length codes */ + unsigned nd; /* number of distance codes */ + + unsigned ll[286 + 30]; /* literal/length and distance code lengths */ + unsigned long b_dynamic; /* bit buffer */ + unsigned k_dynamic; /* number of bits in bit buffer */ + + /* make local bit buffer */ + b_dynamic = bb; + k_dynamic = bk; + + /* read in table lengths */ + while (k_dynamic < 5) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ + b_dynamic >>= 5; + k_dynamic -= 5; + while (k_dynamic < 5) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ + b_dynamic >>= 5; + k_dynamic -= 5; + while (k_dynamic < 4) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + nb = 4 + ((unsigned) 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++) { + while (k_dynamic < 3) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + ll[border[j]] = (unsigned) 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; + if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 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) i < n) { + while (k_dynamic < (unsigned) bl) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + j = (td = tl + ((unsigned) 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 */ + while (k_dynamic < 2) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + j = 3 + ((unsigned) b_dynamic & 3); + b_dynamic >>= 2; + k_dynamic -= 2; + if ((unsigned) i + j > n) { + return 1; + } + while (j--) { + ll[i++] = l; + } + } else if (j == 17) { /* 3 to 10 zero length codes */ + while (k_dynamic < 3) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + j = 3 + ((unsigned) b_dynamic & 7); + b_dynamic >>= 3; + k_dynamic -= 3; + if ((unsigned) i + j > n) { + return 1; + } + while (j--) { + ll[i++] = 0; + } + l = 0; + } else { /* j == 18: 11 to 138 zero length codes */ + while (k_dynamic < 7) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + j = 11 + ((unsigned) b_dynamic & 0x7f); + b_dynamic >>= 7; + k_dynamic -= 7; + if ((unsigned) 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 */ + bb = b_dynamic; + 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) { + error_msg("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) { + error_msg("incomplete distance tree"); + huft_free(td); + } + huft_free(tl); + return i; /* incomplete code set */ + } + + /* decompress until an end-of-block code */ + if (inflate_codes(tl, td, bl, bd)) { + huft_free(tl); + huft_free(td); + return 1; + } + + /* free the decoding tables, return */ + huft_free(tl); + huft_free(td); + return 0; + } + default: + /* bad block type */ + return 2; + } +} + +/* + * decompress an inflated entry + * + * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr + */ +static int inflate() +{ + int e; /* last block flag */ + int r; /* result code */ + unsigned h = 0; /* maximum struct huft's malloc'ed */ + + /* initialize window, bit buffer */ + outcnt = 0; + bk = 0; + bb = 0; + + /* decompress until the last block */ + do { + hufts = 0; + if ((r = inflate_block(&e)) != 0) { + return r; + } + if (hufts > h) { + h = hufts; + } + } while (!e); + + /* Undo too much lookahead. The next read will be byte aligned so we + * can discard unused bits in the last meaningful byte. */ + while (bk >= 8) { + bk -= 8; + ungetc((bb << bk), in_file); + } + + /* flush out window */ + flush_window(); + + /* return success */ + return 0; +} + +/* =========================================================================== + * Unzip in to out. This routine works on both gzip and pkzip files. + * + * IN assertions: the buffer inbuf contains already the beginning of + * the compressed data, from offsets inptr to insize-1 included. + * The magic header has already been checked. The output buffer is cleared. + * in, out: input and output file descriptors + */ +extern int unzip(FILE *l_in_file, FILE *l_out_file) +{ + const int extra_field = 0x04; /* bit 2 set: extra field present */ + const int orig_name = 0x08; /* bit 3 set: original file name present */ + const int comment = 0x10; /* bit 4 set: file comment present */ + unsigned char buf[8]; /* extended local header */ + unsigned char flags; /* compression flags */ + char magic[2]; /* magic header */ + int method; + typedef void (*sig_type) (int); + int exit_code=0; /* program exit code */ + int i; + + in_file = l_in_file; + out_file = l_out_file; + + if (signal(SIGINT, SIG_IGN) != SIG_IGN) { + (void) signal(SIGINT, (sig_type) abort_gzip); + } +#ifdef SIGTERM +// if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { +// (void) signal(SIGTERM, (sig_type) abort_gzip); +// } +#endif +#ifdef SIGHUP + if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { + (void) signal(SIGHUP, (sig_type) abort_gzip); + } +#endif + + signal(SIGPIPE, SIG_IGN); + + /* Allocate all global buffers (for DYN_ALLOC option) */ + window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char))); + outcnt = 0; + bytes_out = 0L; + + magic[0] = fgetc(in_file); + magic[1] = fgetc(in_file); + + /* Magic header for gzip files, 1F 8B = \037\213 */ + if (memcmp(magic, "\037\213", 2) != 0) { + error_msg("Invalid gzip magic"); + return EXIT_FAILURE; + } + + method = (int) fgetc(in_file); + if (method != 8) { + error_msg("unknown method %d -- get newer version of gzip", method); + exit_code = 1; + return -1; + } + + flags = (unsigned char) fgetc(in_file); + + /* Ignore time stamp(4), extra flags(1), OS type(1) */ + for (i = 0; i < 6; i++) + fgetc(in_file); + + if ((flags & extra_field) != 0) { + size_t extra; + extra = fgetc(in_file); + extra += fgetc(in_file) << 8; + + for (i = 0; i < extra; i++) + fgetc(in_file); + } + + /* Discard original name if any */ + if ((flags & orig_name) != 0) { + while (fgetc(in_file) != 0); /* null */ + } + + /* Discard file comment if any */ + if ((flags & comment) != 0) { + while (fgetc(in_file) != 0); /* null */ + } + + if (method < 0) { + return(exit_code); + } + + make_crc_table(); + + /* Decompress */ + if (method == 8) { + + int res = inflate(); + + if (res == 3) { + perror_msg("inflate"); + exit_code = 1; + } else if (res != 0) { + error_msg("invalid compressed data--format violated"); + exit_code = 1; + } + + } else { + error_msg("internal error, invalid method"); + exit_code = 1; + } + + /* Get the crc and original length + * crc32 (see algorithm.doc) + * uncompressed input size modulo 2^32 + */ + fread(buf, 1, 8, in_file); + + /* Validate decompression - crc */ + if (!exit_code && (unsigned int)((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) { + error_msg("invalid compressed data--crc error"); + exit_code = 1; + } + /* Validate decompression - size */ + if (!exit_code && ((buf[4] | (buf[5] << 8)) |((buf[6] | (buf[7] << 8)) << 16)) != (unsigned long) bytes_out) { + error_msg("invalid compressed data--length error"); + exit_code = 1; + } + + free(window); + free(crc_table); + + window = NULL; + crc_table = NULL; + + return exit_code; +} diff --git a/src/libbb/.svn/text-base/wfopen.c.svn-base b/src/libbb/.svn/text-base/wfopen.c.svn-base new file mode 100644 index 0000000..f58ec90 --- /dev/null +++ b/src/libbb/.svn/text-base/wfopen.c.svn-base @@ -0,0 +1,44 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.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 + */ + +#include <stdio.h> +#include <errno.h> +#include "libbb.h" + +FILE *wfopen(const char *path, const char *mode) +{ + FILE *fp; + if ((fp = fopen(path, mode)) == NULL) { + perror_msg("%s", path); + errno = 0; + } + return fp; +} + + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/.svn/text-base/xfuncs.c.svn-base b/src/libbb/.svn/text-base/xfuncs.c.svn-base new file mode 100644 index 0000000..f577315 --- /dev/null +++ b/src/libbb/.svn/text-base/xfuncs.c.svn-base @@ -0,0 +1,93 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.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 + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include "libbb.h" + + +extern void *xmalloc(size_t size) +{ + void *ptr = malloc(size); + if (ptr == NULL && size != 0) + perror_msg_and_die("malloc"); + return ptr; +} + +extern void *xrealloc(void *ptr, size_t size) +{ + ptr = realloc(ptr, size); + if (ptr == NULL && size != 0) + perror_msg_and_die("realloc"); + return ptr; +} + +extern void *xcalloc(size_t nmemb, size_t size) +{ + void *ptr = calloc(nmemb, size); + if (ptr == NULL && nmemb != 0 && size != 0) + perror_msg_and_die("calloc"); + return ptr; +} + +extern char * xstrdup (const char *s) { + char *t; + + if (s == NULL) + return NULL; + + t = strdup (s); + + if (t == NULL) + perror_msg_and_die("strdup"); + + return t; +} + +extern char * xstrndup (const char *s, int n) { + char *t; + + if (s == NULL) + error_msg_and_die("xstrndup bug"); + + t = xmalloc(++n); + + return safe_strncpy(t,s,n); +} + +FILE *xfopen(const char *path, const char *mode) +{ + FILE *fp; + if ((fp = fopen(path, mode)) == NULL) + perror_msg_and_die("%s", path); + return fp; +} + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/.svn/text-base/xreadlink.c.svn-base b/src/libbb/.svn/text-base/xreadlink.c.svn-base new file mode 100644 index 0000000..7d77a3b --- /dev/null +++ b/src/libbb/.svn/text-base/xreadlink.c.svn-base @@ -0,0 +1,37 @@ +/* + * xreadlink.c - safe implementation of readlink. + * Returns a NULL on failure... + */ + +#include <stdio.h> + +/* + * NOTE: This function returns a malloced char* that you will have to free + * yourself. You have been warned. + */ + +#include <unistd.h> +#include "libbb.h" + +extern char *xreadlink(const char *path) +{ + static const int GROWBY = 80; /* how large we will grow strings by */ + + char *buf = NULL; + int bufsize = 0, readsize = 0; + + do { + buf = xrealloc(buf, bufsize += GROWBY); + readsize = readlink(path, buf, bufsize); /* 1st try */ + if (readsize == -1) { + perror_msg("%s", path); + return NULL; + } + } + while (bufsize < readsize + 1); + + buf[readsize] = '\0'; + + return buf; +} + diff --git a/src/libbb/Makefile.am b/src/libbb/Makefile.am new file mode 100644 index 0000000..1cc82df --- /dev/null +++ b/src/libbb/Makefile.am @@ -0,0 +1,26 @@ +HOST_CPU=@host_cpu@ +BUILD_CPU=@build_cpu@ +ALL_CFLAGS=-g -O -Wall -DHOST_CPU_STR=\"@host_cpu@\" -DBUILD_CPU=@build_cpu@ + +noinst_LTLIBRARIES = libbb.la + +libbb_la_SOURCES = gz_open.c \ + libbb.h \ + unzip.c \ + wfopen.c \ + unarchive.c \ + copy_file.c \ + copy_file_chunk.c \ + xreadlink.c \ + concat_path_file.c \ + xfuncs.c \ + last_char_is.c \ + make_directory.c \ + safe_strncpy.c \ + parse_mode.c \ + time_string.c \ + all_read.c \ + mode_string.c + +libbb_la_CFLAGS = $(ALL_CFLAGS) +#libbb_la_LDFLAGS = -static diff --git a/src/libbb/all_read.c b/src/libbb/all_read.c new file mode 100644 index 0000000..6ec731a --- /dev/null +++ b/src/libbb/all_read.c @@ -0,0 +1,83 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999-2004 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 + */ + +#include <stdio.h> +#include <unistd.h> +#include <errno.h> +#include "libbb.h" + +extern void archive_xread_all(int fd , char *buf, size_t count) +{ + ssize_t size; + + size = full_read(fd, buf, count); + if (size != count) { + perror_msg_and_die("Short read"); + } + return; +} + +/* + * Read all of the supplied buffer from a file. + * This does multiple reads as necessary. + * Returns the amount read, or -1 on an error. + * A short read is returned on an end of file. + */ +ssize_t full_read(int fd, char *buf, int len) +{ + ssize_t cc; + ssize_t total; + + total = 0; + + while (len > 0) { + cc = safe_read(fd, buf, len); + + if (cc < 0) + return cc; /* read() returns -1 on failure. */ + + if (cc == 0) + break; + + buf = ((char *)buf) + cc; + total += cc; + len -= cc; + } + + return total; +} + + +ssize_t safe_read(int fd, void *buf, size_t count) +{ + ssize_t n; + + do { + n = read(fd, buf, count); + } while (n < 0 && errno == EINTR); + + return n; +} + + + +/* END CODE */ + diff --git a/src/libbb/concat_path_file.c b/src/libbb/concat_path_file.c new file mode 100644 index 0000000..e62b99e --- /dev/null +++ b/src/libbb/concat_path_file.c @@ -0,0 +1,45 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 + */ + +/* concatenate path and file name to new allocation buffer, + * not addition '/' if path name already have '/' +*/ + +#include <string.h> +#include "libbb.h" + +extern char *concat_path_file(const char *path, const char *filename) +{ + char *outbuf; + char *lc; + + if (!path) + path=""; + lc = last_char_is(path, '/'); + while (*filename == '/') + filename++; + outbuf = xmalloc(strlen(path)+strlen(filename)+1+(lc==NULL)); + sprintf(outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename); + + return outbuf; +} diff --git a/src/libbb/copy_file.c b/src/libbb/copy_file.c new file mode 100644 index 0000000..fb76669 --- /dev/null +++ b/src/libbb/copy_file.c @@ -0,0 +1,231 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini copy_file implementation for busybox + * + * + * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu> + * + * 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 <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <utime.h> +#include <errno.h> +#include <dirent.h> +#include <stdlib.h> +#include <string.h> + +#include "libbb.h" + +int copy_file(const char *source, const char *dest, int flags) +{ + struct stat source_stat; + struct stat dest_stat; + int dest_exists = 1; + int status = 0; + + if (((flags & FILEUTILS_PRESERVE_SYMLINKS) && + lstat(source, &source_stat) < 0) || + (!(flags & FILEUTILS_PRESERVE_SYMLINKS) && + stat(source, &source_stat) < 0)) { + perror_msg("%s", source); + return -1; + } + + if (stat(dest, &dest_stat) < 0) { + if (errno != ENOENT) { + perror_msg("unable to stat `%s'", dest); + return -1; + } + dest_exists = 0; + } + + if (dest_exists && source_stat.st_rdev == dest_stat.st_rdev && + source_stat.st_ino == dest_stat.st_ino) { + error_msg("`%s' and `%s' are the same file", source, dest); + return -1; + } + + if (S_ISDIR(source_stat.st_mode)) { + DIR *dp; + struct dirent *d; + mode_t saved_umask = 0; + + if (!(flags & FILEUTILS_RECUR)) { + error_msg("%s: omitting directory", source); + return -1; + } + + /* Create DEST. */ + if (dest_exists) { + if (!S_ISDIR(dest_stat.st_mode)) { + error_msg("`%s' is not a directory", dest); + return -1; + } + } else { + mode_t mode; + saved_umask = umask(0); + + mode = source_stat.st_mode; + if (!(flags & FILEUTILS_PRESERVE_STATUS)) + mode = source_stat.st_mode & ~saved_umask; + mode |= S_IRWXU; + + if (mkdir(dest, mode) < 0) { + umask(saved_umask); + perror_msg("cannot create directory `%s'", dest); + return -1; + } + + umask(saved_umask); + } + + /* Recursively copy files in SOURCE. */ + if ((dp = opendir(source)) == NULL) { + perror_msg("unable to open directory `%s'", source); + status = -1; + goto end; + } + + while ((d = readdir(dp)) != NULL) { + char *new_source, *new_dest; + + if (strcmp(d->d_name, ".") == 0 || + strcmp(d->d_name, "..") == 0) + continue; + + new_source = concat_path_file(source, d->d_name); + new_dest = concat_path_file(dest, d->d_name); + if (copy_file(new_source, new_dest, flags) < 0) + status = -1; + free(new_source); + free(new_dest); + } + + /* ??? What if an error occurs in readdir? */ + + if (closedir(dp) < 0) { + perror_msg("unable to close directory `%s'", source); + status = -1; + } + + if (!dest_exists && + chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { + perror_msg("unable to change permissions of `%s'", dest); + status = -1; + } + } else if (S_ISREG(source_stat.st_mode)) { + FILE *sfp, *dfp; + + if (dest_exists) { + if ((dfp = fopen(dest, "w")) == NULL) { + if (!(flags & FILEUTILS_FORCE)) { + perror_msg("unable to open `%s'", dest); + return -1; + } + + if (unlink(dest) < 0) { + perror_msg("unable to remove `%s'", dest); + return -1; + } + + dest_exists = 0; + } + } + + if (!dest_exists) { + int fd; + + if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 || + (dfp = fdopen(fd, "w")) == NULL) { + if (fd >= 0) + close(fd); + perror_msg("unable to open `%s'", dest); + return -1; + } + } + + if ((sfp = fopen(source, "r")) == NULL) { + fclose(dfp); + perror_msg("unable to open `%s'", source); + status = -1; + goto end; + } + + if (copy_file_chunk(sfp, dfp, -1) < 0) + status = -1; + + if (fclose(dfp) < 0) { + perror_msg("unable to close `%s'", dest); + status = -1; + } + + if (fclose(sfp) < 0) { + perror_msg("unable to close `%s'", source); + status = -1; + } + } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || + S_ISSOCK(source_stat.st_mode)) { + if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { + perror_msg("unable to create `%s'", dest); + return -1; + } + } else if (S_ISFIFO(source_stat.st_mode)) { + if (mkfifo(dest, source_stat.st_mode) < 0) { + perror_msg("cannot create fifo `%s'", dest); + return -1; + } + } else if (S_ISLNK(source_stat.st_mode)) { + char *lpath = xreadlink(source); + if (symlink(lpath, dest) < 0) { + perror_msg("cannot create symlink `%s'", dest); + return -1; + } + free(lpath); + +#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) + if (flags & FILEUTILS_PRESERVE_STATUS) + if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) + perror_msg("unable to preserve ownership of `%s'", dest); +#endif + return 0; + } else { + error_msg("internal error: unrecognized file type"); + return -1; + } + +end: + + if (flags & FILEUTILS_PRESERVE_STATUS) { + struct utimbuf times; + + times.actime = source_stat.st_atime; + times.modtime = source_stat.st_mtime; + if (utime(dest, ×) < 0) + perror_msg("unable to preserve times of `%s'", dest); + if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { + source_stat.st_mode &= ~(S_ISUID | S_ISGID); + perror_msg("unable to preserve ownership of `%s'", dest); + } + if (chmod(dest, source_stat.st_mode) < 0) + perror_msg("unable to preserve permissions of `%s'", dest); + } + + return status; +} diff --git a/src/libbb/copy_file_chunk.c b/src/libbb/copy_file_chunk.c new file mode 100644 index 0000000..63d2ab1 --- /dev/null +++ b/src/libbb/copy_file_chunk.c @@ -0,0 +1,70 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 <stdio.h> +#include <sys/stat.h> +#include "libbb.h" + +/* Copy CHUNKSIZE bytes (or until EOF if CHUNKSIZE equals -1) from SRC_FILE + * to DST_FILE. */ +extern int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize) +{ + size_t nread, nwritten, size; + char buffer[BUFSIZ]; + + while (chunksize != 0) { + if (chunksize > BUFSIZ) + size = BUFSIZ; + else + size = chunksize; + + nread = fread (buffer, 1, size, src_file); + + if (nread != size && ferror (src_file)) { + perror_msg ("read"); + return -1; + } else if (nread == 0) { + if (chunksize != -1) { + error_msg ("Unable to read all data"); + return -1; + } + + return 0; + } + + nwritten = fwrite (buffer, 1, nread, dst_file); + + if (nwritten != nread) { + if (ferror (dst_file)) + perror_msg ("write"); + else + error_msg ("Unable to write all data"); + return -1; + } + + if (chunksize != -1) + chunksize -= nwritten; + } + + return 0; +} diff --git a/src/libbb/gz_open.c b/src/libbb/gz_open.c new file mode 100644 index 0000000..bdc7564 --- /dev/null +++ b/src/libbb/gz_open.c @@ -0,0 +1,149 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 <sys/wait.h> +#include <signal.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include "libbb.h" + +static int gz_use_vfork; + +FILE * +gz_open(FILE *compressed_file, int *pid) +{ + int unzip_pipe[2]; + off_t floc; + int cfile = -1; + + gz_use_vfork = (getenv("OPKG_USE_VFORK") != NULL); + + if (gz_use_vfork) { + /* Create a new file descriptor for the input stream + * (it *must* be associated with a file), and lseek() + * to the same position in that fd as the stream. + */ + cfile = dup(fileno(compressed_file)); + floc = ftello(compressed_file); + lseek(cfile, floc, SEEK_SET); + setenv("GZIP", "--quiet", 0); + } + + if (pipe(unzip_pipe)!=0) { + perror_msg("pipe"); + return(NULL); + } + + /* If we don't flush, we end up with two copies of anything pending, + one from the parent, one from the child */ + fflush(stdout); + fflush(stderr); + + if (gz_use_vfork) { + *pid = vfork(); + } else { + *pid = fork(); + } + + if (*pid<0) { + perror_msg("fork"); + return(NULL); + } + + if (*pid==0) { + /* child process */ + close(unzip_pipe[0]); + if (gz_use_vfork) { + dup2(unzip_pipe[1], 1); + dup2(cfile, 0); + execlp("gunzip","gunzip",NULL); + /* If we get here, we had a failure */ + _exit(EXIT_FAILURE); + } else { + unzip(compressed_file, fdopen(unzip_pipe[1], "w")); + fflush(NULL); + fclose(compressed_file); + close(unzip_pipe[1]); + _exit(EXIT_SUCCESS); + } + } + /* Parent process is executing here */ + if (gz_use_vfork) { + close(cfile); + } + close(unzip_pipe[1]); + return(fdopen(unzip_pipe[0], "r")); +} + +int +gz_close(int gunzip_pid) +{ + int status; + int ret; + + if (gz_use_vfork) { + /* The gunzip process remains running in the background if we + * used the vfork()/exec() technique - so we have to kill it + * forcibly. There might be a better way to do this, but that + * affect a lot of other parts of opkg, and this works fine. + */ + if (kill(gunzip_pid, SIGTERM) == -1) { + perror_msg("gz_close(): unable to kill gunzip pid."); + return -1; + } + } + + + if (waitpid(gunzip_pid, &status, 0) == -1) { + perror_msg("waitpid"); + return -1; + } + + if (gz_use_vfork) { + /* Bail out here if we used the vfork()/exec() technique. */ + return 0; + } + + if (WIFSIGNALED(status)) { + error_msg("Unzip process killed by signal %d.\n", + WTERMSIG(status)); + return -1; + } + + if (!WIFEXITED(status)) { + /* shouldn't happen */ + error_msg("Your system is broken: got status %d from waitpid.\n", + status); + return -1; + } + + if ((ret = WEXITSTATUS(status))) { + error_msg("Unzip process failed with return code %d.\n", + ret); + return -1; + } + + return 0; +} diff --git a/src/libbb/last_char_is.c b/src/libbb/last_char_is.c new file mode 100644 index 0000000..26c2423 --- /dev/null +++ b/src/libbb/last_char_is.c @@ -0,0 +1,40 @@ +/* + * busybox library eXtended function + * + * Copyright (C) 2001 Larry Doolittle, <ldoolitt@recycle.lbl.gov> + * + * 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 <string.h> +#include "libbb.h" + +/* Find out if the last character of a string matches the one given Don't + * underrun the buffer if the string length is 0. Also avoids a possible + * space-hogging inline of strlen() per usage. + */ +char * last_char_is(const char *s, int c) +{ + char *sret; + if (!s) + return NULL; + sret = (char *)s+strlen(s)-1; + if (sret>=s && *sret == c) { + return sret; + } else { + return NULL; + } +} diff --git a/src/libbb/libbb.h b/src/libbb/libbb.h new file mode 100644 index 0000000..4e1fafc --- /dev/null +++ b/src/libbb/libbb.h @@ -0,0 +1,123 @@ +/* vi: set sw=4 ts=4: */ +/* + * Busybox main internal header file + * + * 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 + */ + +#ifndef __LIBBB_H__ +#define __LIBBB_H__ 1 + +#include <stdio.h> +#include <stdarg.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <stdlib.h> +#include <netdb.h> + +#include "../libopkg/opkg_message.h" + +#ifndef FALSE +#define FALSE ((int) 0) +#endif + +#ifndef TRUE +#define TRUE ((int) 1) +#endif + +#define error_msg(fmt, args...) opkg_msg(ERROR, fmt"\n", ##args) +#define perror_msg(fmt, args...) opkg_perror(ERROR, fmt, ##args) +#define error_msg_and_die(fmt, args...) \ + do { \ + error_msg(fmt, ##args); \ + exit(EXIT_FAILURE); \ + } while (0) +#define perror_msg_and_die(fmt, args...) \ + do { \ + perror_msg(fmt, ##args); \ + exit(EXIT_FAILURE); \ + } while (0) + +extern void archive_xread_all(int fd, char *buf, size_t count); + +const char *mode_string(int mode); +const char *time_string(time_t timeVal); + +int copy_file(const char *source, const char *dest, int flags); +int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); +ssize_t safe_read(int fd, void *buf, size_t count); +ssize_t full_read(int fd, char *buf, int len); + +extern int parse_mode( const char* s, mode_t* theMode); + +extern FILE *wfopen(const char *path, const char *mode); +extern FILE *xfopen(const char *path, const char *mode); + +extern void *xmalloc (size_t size); +extern void *xrealloc(void *old, size_t size); +extern void *xcalloc(size_t nmemb, size_t size); +extern char *xstrdup (const char *s); +extern char *xstrndup (const char *s, int n); +extern char *safe_strncpy(char *dst, const char *src, size_t size); + +char *xreadlink(const char *path); +char *concat_path_file(const char *path, const char *filename); +char *last_char_is(const char *s, int c); + +typedef struct file_headers_s { + char *name; + char *link_name; + off_t size; + uid_t uid; + gid_t gid; + mode_t mode; + time_t mtime; + dev_t device; +} file_header_t; + +enum extract_functions_e { + extract_verbose_list = 1, + extract_list = 2, + extract_one_to_buffer = 4, + extract_to_stream = 8, + extract_all_to_fs = 16, + extract_preserve_date = 32, + extract_data_tar_gz = 64, + extract_control_tar_gz = 128, + extract_unzip_only = 256, + extract_unconditional = 512, + extract_create_leading_dirs = 1024, + extract_quiet = 2048, + extract_exclude_list = 4096 +}; + +char *deb_extract(const char *package_filename, FILE *out_stream, + const int extract_function, const char *prefix, + const char *filename, int *err); + +extern int unzip(FILE *l_in_file, FILE *l_out_file); +extern int gz_close(int gunzip_pid); +extern FILE *gz_open(FILE *compressed_file, int *pid); + +int make_directory (const char *path, long mode, int flags); + +enum { + FILEUTILS_PRESERVE_STATUS = 1, + FILEUTILS_PRESERVE_SYMLINKS = 2, + FILEUTILS_RECUR = 4, + FILEUTILS_FORCE = 8, +}; + +#endif /* __LIBBB_H__ */ diff --git a/src/libbb/make_directory.c b/src/libbb/make_directory.c new file mode 100644 index 0000000..86ab554 --- /dev/null +++ b/src/libbb/make_directory.c @@ -0,0 +1,80 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini make_directory implementation for busybox + * + * Copyright (C) 2001 Matt Kraai. + * + * 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 <errno.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> +#include <stdlib.h> +#include <libgen.h> + +#include "libbb.h" + +/* Create the directory PATH with mode MODE, or the default if MODE is -1. + * Also create parent directories as necessary if flags contains + * FILEUTILS_RECUR. */ + +int make_directory (const char *path, long mode, int flags) +{ + if (!(flags & FILEUTILS_RECUR)) { + if (mkdir (path, 0777) < 0) { + perror_msg ("Cannot create directory `%s'", path); + return -1; + } + + if (mode != -1 && chmod (path, mode) < 0) { + perror_msg ("Cannot set permissions of directory `%s'", path); + return -1; + } + } else { + struct stat st; + + if (stat (path, &st) < 0 && errno == ENOENT) { + int status; + char *pathcopy, *parent, *parentcopy; + mode_t mask; + + mask = umask (0); + umask (mask); + + /* dirname is unsafe, it may both modify the + memory of the path argument and may return + a pointer to static memory, which can then + be modified by consequtive calls to dirname */ + + pathcopy = xstrdup (path); + parent = dirname (pathcopy); + parentcopy = xstrdup (parent); + status = make_directory (parentcopy, (0777 & ~mask) + | 0300, FILEUTILS_RECUR); + free (pathcopy); + free (parentcopy); + + + if (status < 0 || make_directory (path, mode, 0) < 0) + return -1; + } + } + + return 0; +} diff --git a/src/libbb/mode_string.c b/src/libbb/mode_string.c new file mode 100644 index 0000000..12dc179 --- /dev/null +++ b/src/libbb/mode_string.c @@ -0,0 +1,78 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 <stdio.h> +#include "libbb.h" + + + +#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) +#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) + +/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ +static const mode_t SBIT[] = { + 0, 0, S_ISUID, + 0, 0, S_ISGID, + 0, 0, S_ISVTX +}; + +/* The 9 mode bits to test */ +static const mode_t MBIT[] = { + S_IRUSR, S_IWUSR, S_IXUSR, + S_IRGRP, S_IWGRP, S_IXGRP, + S_IROTH, S_IWOTH, S_IXOTH +}; + +static const char MODE1[] = "rwxrwxrwx"; +static const char MODE0[] = "---------"; +static const char SMODE1[] = "..s..s..t"; +static const char SMODE0[] = "..S..S..T"; + +/* + * Return the standard ls-like mode string from a file mode. + * This is static and so is overwritten on each call. + */ +const char *mode_string(int mode) +{ + static char buf[12]; + + int i; + + buf[0] = TYPECHAR(mode); + for (i = 0; i < 9; i++) { + if (mode & SBIT[i]) + buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i]; + else + buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i]; + } + return buf; +} + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/parse_mode.c b/src/libbb/parse_mode.c new file mode 100644 index 0000000..02668c7 --- /dev/null +++ b/src/libbb/parse_mode.c @@ -0,0 +1,134 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) many different people. If you wrote this, please + * acknowledge your work. + * + * 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 <stdio.h> +#include <stdlib.h> +#include "libbb.h" + + +/* This function parses the sort of string you might pass + * to chmod (i.e., [ugoa]{+|-|=}[rwxst] ) and returns the + * correct mode described by the string. */ +extern int parse_mode(const char *s, mode_t * theMode) +{ + static const mode_t group_set[] = { + S_ISUID | S_IRWXU, /* u */ + S_ISGID | S_IRWXG, /* g */ + S_IRWXO, /* o */ + S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */ + }; + + static const mode_t mode_set[] = { + S_IRUSR | S_IRGRP | S_IROTH, /* r */ + S_IWUSR | S_IWGRP | S_IWOTH, /* w */ + S_IXUSR | S_IXGRP | S_IXOTH, /* x */ + S_ISUID | S_ISGID, /* s */ + S_ISVTX /* t */ + }; + + static const char group_chars[] = "ugoa"; + static const char mode_chars[] = "rwxst"; + + const char *p; + + mode_t andMode = + S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; + mode_t orMode = 0; + mode_t mode; + mode_t groups; + char type; + char c; + + if (s==NULL) { + return (FALSE); + } + + do { + mode = 0; + groups = 0; + NEXT_GROUP: + if ((c = *s++) == '\0') { + return -1; + } + for (p=group_chars ; *p ; p++) { + if (*p == c) { + groups |= group_set[(int)(p-group_chars)]; + goto NEXT_GROUP; + } + } + switch (c) { + case '=': + case '+': + case '-': + type = c; + if (groups == 0) { /* The default is "all" */ + groups |= S_ISUID | S_ISGID | S_ISVTX + | S_IRWXU | S_IRWXG | S_IRWXO; + } + break; + default: + if ((c < '0') || (c > '7') || (mode | groups)) { + return (FALSE); + } else { + *theMode = strtol(--s, NULL, 8); + return (TRUE); + } + } + + NEXT_MODE: + if (((c = *s++) != '\0') && (c != ',')) { + for (p=mode_chars ; *p ; p++) { + if (*p == c) { + mode |= mode_set[(int)(p-mode_chars)]; + goto NEXT_MODE; + } + } + break; /* We're done so break out of loop.*/ + } + switch (type) { + case '=': + andMode &= ~(groups); /* Now fall through. */ + case '+': + orMode |= mode & groups; + break; + case '-': + andMode &= ~(mode & groups); + orMode &= ~(mode & groups); + break; + } + } while (c == ','); + + *theMode &= andMode; + *theMode |= orMode; + + return TRUE; +} + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/safe_strncpy.c b/src/libbb/safe_strncpy.c new file mode 100644 index 0000000..eb2dbab --- /dev/null +++ b/src/libbb/safe_strncpy.c @@ -0,0 +1,42 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.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 + */ + +#include <string.h> +#include "libbb.h" + + + +/* Like strncpy but make sure the resulting string is always 0 terminated. */ +extern char * safe_strncpy(char *dst, const char *src, size_t size) +{ + dst[size-1] = '\0'; + return strncpy(dst, src, size-1); +} + + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/time_string.c b/src/libbb/time_string.c new file mode 100644 index 0000000..d103a02 --- /dev/null +++ b/src/libbb/time_string.c @@ -0,0 +1,62 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.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 + */ + +#include <stdio.h> +#include <string.h> +#include <time.h> +#include <utime.h> +#include "libbb.h" + + +/* + * Return the standard ls-like time string from a time_t + * This is static and so is overwritten on each call. + */ +const char *time_string(time_t timeVal) +{ + time_t now; + char *str; + static char buf[26]; + + time(&now); + + str = ctime(&timeVal); + + strcpy(buf, &str[4]); + buf[12] = '\0'; + + if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { + strcpy(&buf[7], &str[20]); + buf[11] = '\0'; + } + + return buf; +} + + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/unarchive.c b/src/libbb/unarchive.c new file mode 100644 index 0000000..5d4464f --- /dev/null +++ b/src/libbb/unarchive.c @@ -0,0 +1,784 @@ +/* + * Copyright (C) 2000 by Glenn McGrath + * Copyright (C) 2001 by Laurence Anderson + * + * Based on previous work by busybox developers and others. + * + * 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 <errno.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <utime.h> +#include <libgen.h> + +#include "libbb.h" + +#define CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY 1 +#define CONFIG_FEATURE_TAR_GNU_EXTENSIONS + +#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS +static char *longname = NULL; +static char *linkname = NULL; +#endif + +off_t archive_offset; + +#define SEEK_BUF 4096 +static ssize_t +seek_by_read(FILE* fd, size_t len) +{ + ssize_t cc, total = 0; + char buf[SEEK_BUF]; + + while (len) { + cc = fread(buf, sizeof(buf[0]), + len > SEEK_BUF ? SEEK_BUF : len, + fd); + + total += cc; + len -= cc; + + if(feof(fd) || ferror(fd)) + break; + } + return total; +} + +static void +seek_sub_file(FILE *fd, const int count) +{ + archive_offset += count; + + /* Do not use fseek() on a pipe. It may fail with ESPIPE, leaving the + * stream at an undefined location. + */ + seek_by_read(fd, count); + + return; +} + + +/* Extract the data postioned at src_stream to either filesystem, stdout or + * buffer depending on the value of 'function' which is defined in libbb.h + * + * prefix doesnt have to be just a directory, it may prefix the filename as well. + * + * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath + * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have + * 'dpkg.' as their prefix + * + * For this reason if prefix does point to a dir then it must end with a + * trailing '/' or else the last dir will be assumed to be the file prefix + */ +static char * +extract_archive(FILE *src_stream, FILE *out_stream, + const file_header_t *file_entry, const int function, + const char *prefix, + int *err) +{ + FILE *dst_stream = NULL; + char *full_name = NULL; + char *full_link_name = NULL; + char *buffer = NULL; + struct utimbuf t; + + *err = 0; + + /* prefix doesnt have to be a proper path it may prepend + * the filename as well */ + if (prefix != NULL) { + /* strip leading '/' in filename to extract as prefix may not be dir */ + /* Cant use concat_path_file here as prefix might not be a directory */ + char *path = file_entry->name; + if (strncmp("./", path, 2) == 0) { + path += 2; + if (strlen(path) == 0) + /* Do nothing, current dir already exists. */ + return NULL; + } + full_name = xmalloc(strlen(prefix) + strlen(path) + 1); + strcpy(full_name, prefix); + strcat(full_name, path); + if ( file_entry->link_name ){ + full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1); + strcpy(full_link_name, prefix); + strcat(full_link_name, file_entry->link_name); + } + } else { + full_name = xstrdup(file_entry->name); + if ( file_entry->link_name ) + full_link_name = xstrdup(file_entry->link_name); + } + + + if (function & extract_to_stream) { + if (S_ISREG(file_entry->mode)) { + *err = copy_file_chunk(src_stream, out_stream, file_entry->size); + archive_offset += file_entry->size; + } + } + else if (function & extract_one_to_buffer) { + if (S_ISREG(file_entry->mode)) { + buffer = (char *) xmalloc(file_entry->size + 1); + fread(buffer, 1, file_entry->size, src_stream); + buffer[file_entry->size] = '\0'; + archive_offset += file_entry->size; + goto cleanup; + } + } + else if (function & extract_all_to_fs) { + struct stat oldfile; + int stat_res; + stat_res = lstat (full_name, &oldfile); + if (stat_res == 0) { /* The file already exists */ + if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) { + if (!S_ISDIR(oldfile.st_mode)) { + unlink(full_name); /* Directories might not be empty etc */ + } + } else { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + error_msg("%s not created: newer or same age file exists", file_entry->name); + } + seek_sub_file(src_stream, file_entry->size); + goto cleanup; + } + } + if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */ + char *buf, *parent; + buf = xstrdup(full_name); + parent = dirname(buf); + if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + error_msg("couldn't create leading directories"); + } + } + free (buf); + } + switch(file_entry->mode & S_IFMT) { + case S_IFREG: + if (file_entry->link_name) { /* Found a cpio hard link */ + if (link(full_link_name, full_name) != 0) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg("Cannot link from %s to '%s'", + file_entry->name, file_entry->link_name); + } + } + } else { + if ((dst_stream = wfopen(full_name, "w")) == NULL) { + *err = -1; + seek_sub_file(src_stream, file_entry->size); + goto cleanup; + } + archive_offset += file_entry->size; + *err = copy_file_chunk(src_stream, dst_stream, file_entry->size); + fclose(dst_stream); + } + break; + case S_IFDIR: + if (stat_res != 0) { + if (mkdir(full_name, file_entry->mode) < 0) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg("Cannot make dir %s", full_name); + } + } + } + break; + case S_IFLNK: + if (symlink(file_entry->link_name, full_name) < 0) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name); + } + goto cleanup; + } + break; + case S_IFSOCK: + case S_IFBLK: + case S_IFCHR: + case S_IFIFO: + if (mknod(full_name, file_entry->mode, file_entry->device) == -1) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg("Cannot create node %s", file_entry->name); + } + goto cleanup; + } + break; + default: + *err = -1; + perror_msg("Don't know how to handle %s", full_name); + + } + + /* Changing a symlink's properties normally changes the properties of the + * file pointed to, so dont try and change the date or mode, lchown does + * does the right thing, but isnt available in older versions of libc */ + if (S_ISLNK(file_entry->mode)) { +#if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1) + lchown(full_name, file_entry->uid, file_entry->gid); +#endif + } else { + if (function & extract_preserve_date) { + t.actime = file_entry->mtime; + t.modtime = file_entry->mtime; + utime(full_name, &t); + } + chown(full_name, file_entry->uid, file_entry->gid); + chmod(full_name, file_entry->mode); + } + } else { + /* If we arent extracting data we have to skip it, + * if data size is 0 then then just do it anyway + * (saves testing for it) */ + seek_sub_file(src_stream, file_entry->size); + } + + /* extract_list and extract_verbose_list can be used in conjunction + * with one of the above four extraction functions, so do this seperately */ + if (function & extract_verbose_list) { + fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode), + file_entry->uid, file_entry->gid, + (int) file_entry->size, time_string(file_entry->mtime)); + } + if ((function & extract_list) || (function & extract_verbose_list)){ + /* fputs doesnt add a trailing \n, so use fprintf */ + fprintf(out_stream, "%s\n", file_entry->name); + } + +cleanup: + free(full_name); + if ( full_link_name ) + free(full_link_name); + + return buffer; +} + +static char * +unarchive(FILE *src_stream, FILE *out_stream, + file_header_t *(*get_headers)(FILE *), + void (*free_headers)(file_header_t *), + const int extract_function, + const char *prefix, + const char **extract_names, + int *err) +{ + file_header_t *file_entry; + int extract_flag; + int i; + char *buffer = NULL; + + *err = 0; + + archive_offset = 0; + while ((file_entry = get_headers(src_stream)) != NULL) { + extract_flag = TRUE; + + if (extract_names != NULL) { + int found_flag = FALSE; + char *p = file_entry->name; + + if (p[0] == '.' && p[1] == '/') + p += 2; + + for(i = 0; extract_names[i] != 0; i++) { + if (strcmp(extract_names[i], p) == 0) { + found_flag = TRUE; + break; + } + } + if (extract_function & extract_exclude_list) { + if (found_flag == TRUE) { + extract_flag = FALSE; + } + } else { + /* If its not found in the include list dont extract it */ + if (found_flag == FALSE) { + extract_flag = FALSE; + } + } + } + + if (extract_flag == TRUE) { + buffer = extract_archive(src_stream, out_stream, + file_entry, extract_function, + prefix, err); + *err = 0; /* XXX: ignore extraction errors */ + if (*err) { + free_headers(file_entry); + break; + } + } else { + /* seek past the data entry */ + seek_sub_file(src_stream, file_entry->size); + } + free_headers(file_entry); + } + + return buffer; +} + +static file_header_t * +get_header_ar(FILE *src_stream) +{ + file_header_t *typed; + 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; + static char *ar_long_names; + + if (fread(ar.raw, 1, 60, src_stream) != 60) { + return(NULL); + } + archive_offset += 60; + /* align the headers based on the header magic */ + if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) { + /* some version of ar, have an extra '\n' after each data entry, + * this puts the next header out by 1 */ + if (ar.formated.magic[1] != '`') { + error_msg("Invalid magic"); + return(NULL); + } + /* read the next char out of what would be the data section, + * if its a '\n' then it is a valid header offset by 1*/ + archive_offset++; + if (fgetc(src_stream) != '\n') { + error_msg("Invalid magic"); + return(NULL); + } + /* fix up the header, we started reading 1 byte too early */ + /* raw_header[60] wont be '\n' as it should, but it doesnt matter */ + memmove(ar.raw, &ar.raw[1], 59); + } + + typed = (file_header_t *) xcalloc(1, sizeof(file_header_t)); + + typed->size = (size_t) atoi(ar.formated.size); + /* long filenames have '/' as the first character */ + if (ar.formated.name[0] == '/') { + 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_names = (char *) xrealloc(ar_long_names, typed->size); + fread(ar_long_names, 1, typed->size, src_stream); + archive_offset += typed->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(src_stream)); /* Return next header */ + } else if (ar.formated.name[1] == ' ') { + /* This is the index of symbols in the file for compilers */ + seek_sub_file(src_stream, typed->size); + return (get_header_ar(src_stream)); /* 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 */ + if (!ar_long_names) { + error_msg("Cannot resolve long file name"); + return (NULL); + } + typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1])); + } + } else { + /* short filenames */ + typed->name = xcalloc(1, 16); + strncpy(typed->name, ar.formated.name, 16); + } + typed->name[strcspn(typed->name, " /")]='\0'; + + /* convert the rest of the now valid char header to its typed struct */ + parse_mode(ar.formated.mode, &typed->mode); + typed->mtime = atoi(ar.formated.date); + typed->uid = atoi(ar.formated.uid); + typed->gid = atoi(ar.formated.gid); + + return(typed); +} + +static void +free_header_ar(file_header_t *ar_entry) +{ + if (ar_entry == NULL) + return; + + free(ar_entry->name); + if (ar_entry->link_name) + free(ar_entry->link_name); + + free(ar_entry); +} + + +static file_header_t * +get_header_tar(FILE *tar_stream) +{ + 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; + file_header_t *tar_entry = NULL; + long i; + long sum = 0; + + if (archive_offset % 512 != 0) { + seek_sub_file(tar_stream, 512 - (archive_offset % 512)); + } + + if (fread(tar.raw, 1, 512, tar_stream) != 512) { + /* Unfortunately its common for tar files to have all sorts of + * trailing garbage, fail silently */ +// error_msg("Couldnt read header"); + return(NULL); + } + archive_offset += 512; + + /* Check header has valid magic, unfortunately some tar files + * have empty (0'ed) tar entries at the end, which will + * cause this to fail, so fail silently for now + */ + 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 + return(NULL); + } + + /* 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)) { + if ( strtol(tar.formated.chksum,NULL,8) != 0 ) + error_msg("Invalid tar header checksum"); + return(NULL); + } + + /* convert to type'ed variables */ + tar_entry = xcalloc(1, sizeof(file_header_t)); + + + + // tar_entry->name = xstrdup(tar.formated.name); + +/* + parse_mode(tar.formated.mode, &tar_entry->mode); +*/ + tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8); + + tar_entry->uid = strtol(tar.formated.uid, NULL, 8); + tar_entry->gid = strtol(tar.formated.gid, NULL, 8); + tar_entry->size = strtol(tar.formated.size, NULL, 8); + tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8); + + tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) + + strtol(tar.formated.devminor, NULL, 8); + + /* Fix mode, used by the old format */ + switch (tar.formated.typeflag) { + /* hard links are detected as regular files with 0 size and a link name */ + case '1': + tar_entry->mode |= S_IFREG ; + break; + case 0: + case '0': + +# ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY + if (last_char_is(tar_entry->name, '/')) { + tar_entry->mode |= S_IFDIR; + } else +# endif + tar_entry->mode |= S_IFREG; + break; + case '2': + tar_entry->mode |= S_IFLNK; + break; + case '3': + tar_entry->mode |= S_IFCHR; + break; + case '4': + tar_entry->mode |= S_IFBLK; + break; + case '5': + tar_entry->mode |= S_IFDIR; + break; + case '6': + tar_entry->mode |= S_IFIFO; + break; +# ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS + case 'L': { + longname = xmalloc(tar_entry->size + 1); + if(fread(longname, tar_entry->size, 1, tar_stream) != 1) + return NULL; + longname[tar_entry->size] = '\0'; + archive_offset += tar_entry->size; + + return(get_header_tar(tar_stream)); + } + case 'K': { + linkname = xmalloc(tar_entry->size + 1); + if(fread(linkname, tar_entry->size, 1, tar_stream) != 1) + return NULL; + linkname[tar_entry->size] = '\0'; + archive_offset += tar_entry->size; + + return(get_header_tar(tar_stream)); + } + case 'D': + case 'M': + case 'N': + case 'S': + case 'V': + perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag); +# endif + default: + perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag); + break; + + } + + +#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS + if (longname) { + tar_entry->name = longname; + longname = NULL; + } else +#endif + { + tar_entry->name = xstrndup(tar.formated.name, 100); + + if (tar.formated.prefix[0]) { + char *temp = tar_entry->name; + char *prefixTemp = xstrndup(tar.formated.prefix, 155); + tar_entry->name = concat_path_file(prefixTemp, temp); + free(temp); + free(prefixTemp); + } + } + + if (linkname) { + tar_entry->link_name = linkname; + linkname = NULL; + } else + { + tar_entry->link_name = *tar.formated.linkname != '\0' ? + xstrndup(tar.formated.linkname, 100) : NULL; + } + + return(tar_entry); +} + +static void +free_header_tar(file_header_t *tar_entry) +{ + if (tar_entry == NULL) + return; + + free(tar_entry->name); + if (tar_entry->link_name) + free(tar_entry->link_name); + + free(tar_entry); +} + +char * +deb_extract(const char *package_filename, FILE *out_stream, + const int extract_function, const char *prefix, + const char *filename, int *err) +{ + FILE *deb_stream = NULL; + file_header_t *ar_header = NULL; + const char **file_list = NULL; + char *output_buffer = NULL; + char *ared_file = NULL; + char ar_magic[8]; + int gz_err; + + *err = 0; + + if (filename != NULL) { + file_list = xmalloc(sizeof(char *) * 2); + file_list[0] = filename; + file_list[1] = NULL; + } + + if (extract_function & extract_control_tar_gz) { + ared_file = "control.tar.gz"; + } + else if (extract_function & extract_data_tar_gz) { + ared_file = "data.tar.gz"; + } else { + opkg_msg(ERROR, "Internal error: extract_function=%x\n", + extract_function); + *err = -1; + goto cleanup; + } + + /* open the debian package to be worked on */ + deb_stream = wfopen(package_filename, "r"); + if (deb_stream == NULL) { + *err = -1; + goto cleanup; + } + /* set the buffer size */ + setvbuf(deb_stream, NULL, _IOFBF, 0x8000); + + /* check ar magic */ + fread(ar_magic, 1, 8, deb_stream); + + if (strncmp(ar_magic,"!<arch>",7) == 0) { + archive_offset = 8; + + while ((ar_header = get_header_ar(deb_stream)) != NULL) { + if (strcmp(ared_file, ar_header->name) == 0) { + int gunzip_pid = 0; + FILE *uncompressed_stream; + /* open a stream of decompressed data */ + uncompressed_stream = gz_open(deb_stream, &gunzip_pid); + if (uncompressed_stream == NULL) { + *err = -1; + goto cleanup; + } + + archive_offset = 0; + output_buffer = unarchive(uncompressed_stream, + out_stream, get_header_tar, + free_header_tar, + extract_function, prefix, + file_list, err); + fclose(uncompressed_stream); + gz_err = gz_close(gunzip_pid); + if (gz_err) + *err = -1; + free_header_ar(ar_header); + break; + } + if (fseek(deb_stream, ar_header->size, SEEK_CUR) == -1) { + opkg_perror(ERROR, "Couldn't fseek into %s", package_filename); + *err = -1; + free_header_ar(ar_header); + goto cleanup; + } + free_header_ar(ar_header); + } + goto cleanup; + } else if (strncmp(ar_magic, "\037\213", 2) == 0) { + /* it's a gz file, let's assume it's an opkg */ + int unzipped_opkg_pid; + FILE *unzipped_opkg_stream; + file_header_t *tar_header; + archive_offset = 0; + if (fseek(deb_stream, 0, SEEK_SET) == -1) { + opkg_perror(ERROR, "Couldn't fseek into %s", package_filename); + *err = -1; + goto cleanup; + } + unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid); + if (unzipped_opkg_stream == NULL) { + *err = -1; + goto cleanup; + } + + /* walk through outer tar file to find ared_file */ + while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) { + int name_offset = 0; + if (strncmp(tar_header->name, "./", 2) == 0) + name_offset = 2; + if (strcmp(ared_file, tar_header->name+name_offset) == 0) { + int gunzip_pid = 0; + FILE *uncompressed_stream; + /* open a stream of decompressed data */ + uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid); + if (uncompressed_stream == NULL) { + *err = -1; + goto cleanup; + } + archive_offset = 0; + + output_buffer = unarchive(uncompressed_stream, + out_stream, + get_header_tar, + free_header_tar, + extract_function, + prefix, + file_list, + err); + + free_header_tar(tar_header); + fclose(uncompressed_stream); + gz_err = gz_close(gunzip_pid); + if (gz_err) + *err = -1; + break; + } + seek_sub_file(unzipped_opkg_stream, tar_header->size); + free_header_tar(tar_header); + } + fclose(unzipped_opkg_stream); + gz_err = gz_close(unzipped_opkg_pid); + if (gz_err) + *err = -1; + + goto cleanup; + } else { + *err = -1; + error_msg("%s: invalid magic", package_filename); + } + +cleanup: + if (deb_stream) + fclose(deb_stream); + if (file_list) + free(file_list); + + return output_buffer; +} diff --git a/src/libbb/unzip.c b/src/libbb/unzip.c new file mode 100644 index 0000000..435effb --- /dev/null +++ b/src/libbb/unzip.c @@ -0,0 +1,1021 @@ +/* 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 <andersee@debian.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> + * + * 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. + */ + +#include <sys/types.h> +#include <sys/wait.h> +#include <signal.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include "libbb.h" + +static FILE *in_file, *out_file; + +static unsigned char *window; +static unsigned long *crc_table = NULL; + +static unsigned long crc; /* shift register contents */ + +/* + * window size--must be a power of two, and + * at least 32K for zip's deflate method + */ +static const int WSIZE = 0x8000; + +/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ +static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */ +static const int N_MAX = 288; /* maximum number of codes in any set */ + +static long bytes_out; /* number of output bytes */ +static unsigned long outcnt; /* bytes in output buffer */ + +static unsigned hufts; /* track memory usage */ +static unsigned long bb; /* bit buffer */ +static unsigned bk; /* bits in bit buffer */ + +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 const unsigned short mask_bits[] = { + 0x0000, + 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, + 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff +}; + +//static int error_number = 0; +/* ======================================================================== + * Signal and error handler. + */ + +static void abort_gzip() +{ + error_msg("gzip aborted\n"); + _exit(-1); +} + +static void make_crc_table() +{ + unsigned long table_entry; /* crc shift register */ + unsigned long poly = 0; /* polynomial exclusive-or pattern */ + int i; /* counter for all possible eight bit values */ + int k; /* byte being shifted into crc apparatus */ + + /* terms of polynomial defining this crc (except x^32): */ + static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; + + /* initial shift register value */ + crc = 0xffffffffL; + crc_table = (unsigned long *) xmalloc(256 * sizeof(unsigned long)); + + /* Make exclusive-or pattern from polynomial (0xedb88320) */ + for (i = 0; i < sizeof(p)/sizeof(int); i++) + poly |= 1L << (31 - p[i]); + + /* Compute and print table of CRC's, five per line */ + for (i = 0; i < 256; i++) { + 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--) { + table_entry = table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >> 1; + } + crc_table[i]=table_entry; + } +} + +/* =========================================================================== + * Write the output window window[0..outcnt-1] and update crc and bytes_out. + * (Used for the decompressed data only.) + */ +static void flush_window(void) +{ + int n; + + if (outcnt == 0) + return; + + for (n = 0; n < outcnt; n++) { + crc = crc_table[((int) crc ^ (window[n])) & 0xff] ^ (crc >> 8); + } + + if (fwrite(window, 1, outcnt, out_file) != outcnt) { + /* + * The Parent process may not be interested in all the data we have, + * in which case it will rudely close its end of the pipe and + * wait for us to exit. + */ + if (errno == EPIPE) + _exit(EXIT_SUCCESS); + + error_msg("Couldnt write"); + _exit(EXIT_FAILURE); + } + bytes_out += (unsigned long) outcnt; + outcnt = 0; +} + +/* + * 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, *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 short *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 */ + unsigned i; /* counter, current code */ + unsigned j; /* counter */ + int k; /* number of bits in current code */ + int l; /* bits per table (returned in m) */ + unsigned *p; /* pointer into c[], b[], or v[] */ + 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 */ + 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 */ + if ((q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t))) == NULL) { + if (h) { + huft_free(u[0]); + } + return 3; /* not enough memory */ + } + hufts += z + 1; /* track memory usage */ + *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 *tl, huft_t *td, int bl, int bd) +{ + unsigned long e; /* table entry flag/number of extra bits */ + unsigned long n, d; /* length and index for copy */ + unsigned long w; /* current window position */ + huft_t *t; /* pointer to table entry */ + unsigned ml, md; /* masks for bl and bd bits */ + unsigned long b; /* bit buffer */ + unsigned k; /* number of bits in bit buffer */ + + /* make local copies of globals */ + b = bb; /* initialize bit buffer */ + k = bk; + w = outcnt; /* initialize window position */ + + /* inflate the coded data */ + ml = mask_bits[bl]; /* precompute masks for speed */ + md = mask_bits[bd]; + for (;;) { /* do until end of block */ + while (k < (unsigned) bl) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) + do { + if (e == 99) { + return 1; + } + b >>= t->b; + k -= t->b; + e -= 16; + while (k < e) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + } 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 */ + window[w++] = (unsigned char) t->v.n; + if (w == WSIZE) { + outcnt=(w), + flush_window(); + w = 0; + } + } else { /* it's an EOB or a length */ + + /* exit if end of block */ + if (e == 15) { + break; + } + + /* get length of block to copy */ + while (k < e) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + n = t->v.n + ((unsigned) b & mask_bits[e]); + b >>= e; + k -= e; + + /* decode distance of block to copy */ + while (k < (unsigned) bd) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + + if ((e = (t = td + ((unsigned) b & md))->e) > 16) + do { + if (e == 99) + return 1; + b >>= t->b; + k -= t->b; + e -= 16; + while (k < e) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); + b >>= t->b; + k -= t->b; + while (k < e) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + d = w - t->v.n - ((unsigned) b & mask_bits[e]); + b >>= e; + k -= e; + + /* do the copy */ + do { + n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e); +#if !defined(NOMEMCPY) && !defined(DEBUG) + if (w - d >= e) { /* (this test assumes unsigned comparison) */ + memcpy(window + w, window + d, e); + w += e; + d += e; + } else /* do it slow to avoid memcpy() overlap */ +#endif /* !NOMEMCPY */ + do { + window[w++] = window[d++]; + } while (--e); + if (w == WSIZE) { + outcnt=(w), + flush_window(); + w = 0; + } + } while (n); + } + } + + /* restore the globals from the locals */ + outcnt = w; /* restore global window pointer */ + bb = b; /* restore global bit buffer */ + bk = k; + + /* done */ + return 0; +} + +/* + * decompress an inflated block + * e: last block flag + * + * GLOBAL VARIABLES: bb, kk, + */ +static int inflate_block(int *e) +{ + unsigned t; /* block type */ + unsigned long b; /* bit buffer */ + unsigned k; /* number of bits in bit buffer */ + static unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */ + 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. */ + static unsigned short cplext[] = { /* Extra bits for literal codes 257..285 */ + 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 */ + static unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */ + 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 + }; + static unsigned short cpdext[] = { /* Extra bits for distance codes */ + 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 + }; + + /* make local bit buffer */ + b = bb; + k = bk; + + /* read in last block bit */ + while (k < 1) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + *e = (int) b & 1; + b >>= 1; + k -= 1; + + /* read in block type */ + while (k < 2) { + b |= ((unsigned long)fgetc(in_file)) << k; + k += 8; + } + t = (unsigned) b & 3; + b >>= 2; + k -= 2; + + /* restore the global bit buffer */ + bb = b; + bk = k; + + /* inflate that block type */ + switch (t) { + case 0: /* Inflate stored */ + { + unsigned long n; /* number of bytes in block */ + unsigned long w; /* current window position */ + unsigned long b_stored; /* bit buffer */ + unsigned long k_stored; /* number of bits in bit buffer */ + + /* make local copies of globals */ + b_stored = bb; /* initialize bit buffer */ + k_stored = bk; + w = outcnt; /* initialize window position */ + + /* go to byte boundary */ + n = k_stored & 7; + b_stored >>= n; + k_stored -= n; + + /* get the length and its complement */ + while (k_stored < 16) { + b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; + k_stored += 8; + } + n = ((unsigned) b_stored & 0xffff); + b_stored >>= 16; + k_stored -= 16; + while (k_stored < 16) { + b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; + k_stored += 8; + } + if (n != (unsigned) ((~b_stored) & 0xffff)) { + return 1; /* error in compressed data */ + } + b_stored >>= 16; + k_stored -= 16; + + /* read and output the compressed data */ + while (n--) { + while (k_stored < 8) { + b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; + k_stored += 8; + } + window[w++] = (unsigned char) b_stored; + if (w == (unsigned long)WSIZE) { + outcnt=(w), + flush_window(); + w = 0; + } + b_stored >>= 8; + k_stored -= 8; + } + + /* restore the globals from the locals */ + outcnt = w; /* restore global window pointer */ + bb = b_stored; /* restore global bit buffer */ + bk = k_stored; + return 0; + } + 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 */ + int bl; /* lookup bits for tl */ + 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 */ + if (inflate_codes(tl, td, bl, bd)) { + huft_free(tl); + huft_free(td); + return 1; + } + + /* free the decoding tables, return */ + huft_free(tl); + huft_free(td); + return 0; + } + case 2: /* Inflate dynamic */ + { + /* Tables for deflate from PKZIP's appnote.txt. */ + static unsigned border[] = { /* Order of the bit length code lengths */ + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 + }; + int dbits = 6; /* bits in base distance lookup table */ + int lbits = 9; /* bits in base literal/length lookup table */ + + int i; /* temporary variables */ + unsigned j; + unsigned l; /* last length */ + unsigned m; /* mask for bit lengths table */ + unsigned n; /* number of lengths to get */ + huft_t *tl; /* literal/length code table */ + huft_t *td; /* distance code table */ + int bl; /* lookup bits for tl */ + int bd; /* lookup bits for td */ + unsigned nb; /* number of bit length codes */ + unsigned nl; /* number of literal/length codes */ + unsigned nd; /* number of distance codes */ + + unsigned ll[286 + 30]; /* literal/length and distance code lengths */ + unsigned long b_dynamic; /* bit buffer */ + unsigned k_dynamic; /* number of bits in bit buffer */ + + /* make local bit buffer */ + b_dynamic = bb; + k_dynamic = bk; + + /* read in table lengths */ + while (k_dynamic < 5) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ + b_dynamic >>= 5; + k_dynamic -= 5; + while (k_dynamic < 5) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ + b_dynamic >>= 5; + k_dynamic -= 5; + while (k_dynamic < 4) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + nb = 4 + ((unsigned) 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++) { + while (k_dynamic < 3) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + ll[border[j]] = (unsigned) 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; + if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 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) i < n) { + while (k_dynamic < (unsigned) bl) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + j = (td = tl + ((unsigned) 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 */ + while (k_dynamic < 2) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + j = 3 + ((unsigned) b_dynamic & 3); + b_dynamic >>= 2; + k_dynamic -= 2; + if ((unsigned) i + j > n) { + return 1; + } + while (j--) { + ll[i++] = l; + } + } else if (j == 17) { /* 3 to 10 zero length codes */ + while (k_dynamic < 3) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + j = 3 + ((unsigned) b_dynamic & 7); + b_dynamic >>= 3; + k_dynamic -= 3; + if ((unsigned) i + j > n) { + return 1; + } + while (j--) { + ll[i++] = 0; + } + l = 0; + } else { /* j == 18: 11 to 138 zero length codes */ + while (k_dynamic < 7) { + b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + k_dynamic += 8; + } + j = 11 + ((unsigned) b_dynamic & 0x7f); + b_dynamic >>= 7; + k_dynamic -= 7; + if ((unsigned) 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 */ + bb = b_dynamic; + 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) { + error_msg("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) { + error_msg("incomplete distance tree"); + huft_free(td); + } + huft_free(tl); + return i; /* incomplete code set */ + } + + /* decompress until an end-of-block code */ + if (inflate_codes(tl, td, bl, bd)) { + huft_free(tl); + huft_free(td); + return 1; + } + + /* free the decoding tables, return */ + huft_free(tl); + huft_free(td); + return 0; + } + default: + /* bad block type */ + return 2; + } +} + +/* + * decompress an inflated entry + * + * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr + */ +static int inflate() +{ + int e; /* last block flag */ + int r; /* result code */ + unsigned h = 0; /* maximum struct huft's malloc'ed */ + + /* initialize window, bit buffer */ + outcnt = 0; + bk = 0; + bb = 0; + + /* decompress until the last block */ + do { + hufts = 0; + if ((r = inflate_block(&e)) != 0) { + return r; + } + if (hufts > h) { + h = hufts; + } + } while (!e); + + /* Undo too much lookahead. The next read will be byte aligned so we + * can discard unused bits in the last meaningful byte. */ + while (bk >= 8) { + bk -= 8; + ungetc((bb << bk), in_file); + } + + /* flush out window */ + flush_window(); + + /* return success */ + return 0; +} + +/* =========================================================================== + * Unzip in to out. This routine works on both gzip and pkzip files. + * + * IN assertions: the buffer inbuf contains already the beginning of + * the compressed data, from offsets inptr to insize-1 included. + * The magic header has already been checked. The output buffer is cleared. + * in, out: input and output file descriptors + */ +extern int unzip(FILE *l_in_file, FILE *l_out_file) +{ + const int extra_field = 0x04; /* bit 2 set: extra field present */ + const int orig_name = 0x08; /* bit 3 set: original file name present */ + const int comment = 0x10; /* bit 4 set: file comment present */ + unsigned char buf[8]; /* extended local header */ + unsigned char flags; /* compression flags */ + char magic[2]; /* magic header */ + int method; + typedef void (*sig_type) (int); + int exit_code=0; /* program exit code */ + int i; + + in_file = l_in_file; + out_file = l_out_file; + + if (signal(SIGINT, SIG_IGN) != SIG_IGN) { + (void) signal(SIGINT, (sig_type) abort_gzip); + } +#ifdef SIGTERM +// if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { +// (void) signal(SIGTERM, (sig_type) abort_gzip); +// } +#endif +#ifdef SIGHUP + if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { + (void) signal(SIGHUP, (sig_type) abort_gzip); + } +#endif + + signal(SIGPIPE, SIG_IGN); + + /* Allocate all global buffers (for DYN_ALLOC option) */ + window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char))); + outcnt = 0; + bytes_out = 0L; + + magic[0] = fgetc(in_file); + magic[1] = fgetc(in_file); + + /* Magic header for gzip files, 1F 8B = \037\213 */ + if (memcmp(magic, "\037\213", 2) != 0) { + error_msg("Invalid gzip magic"); + return EXIT_FAILURE; + } + + method = (int) fgetc(in_file); + if (method != 8) { + error_msg("unknown method %d -- get newer version of gzip", method); + exit_code = 1; + return -1; + } + + flags = (unsigned char) fgetc(in_file); + + /* Ignore time stamp(4), extra flags(1), OS type(1) */ + for (i = 0; i < 6; i++) + fgetc(in_file); + + if ((flags & extra_field) != 0) { + size_t extra; + extra = fgetc(in_file); + extra += fgetc(in_file) << 8; + + for (i = 0; i < extra; i++) + fgetc(in_file); + } + + /* Discard original name if any */ + if ((flags & orig_name) != 0) { + while (fgetc(in_file) != 0); /* null */ + } + + /* Discard file comment if any */ + if ((flags & comment) != 0) { + while (fgetc(in_file) != 0); /* null */ + } + + if (method < 0) { + return(exit_code); + } + + make_crc_table(); + + /* Decompress */ + if (method == 8) { + + int res = inflate(); + + if (res == 3) { + perror_msg("inflate"); + exit_code = 1; + } else if (res != 0) { + error_msg("invalid compressed data--format violated"); + exit_code = 1; + } + + } else { + error_msg("internal error, invalid method"); + exit_code = 1; + } + + /* Get the crc and original length + * crc32 (see algorithm.doc) + * uncompressed input size modulo 2^32 + */ + fread(buf, 1, 8, in_file); + + /* Validate decompression - crc */ + if (!exit_code && (unsigned int)((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) { + error_msg("invalid compressed data--crc error"); + exit_code = 1; + } + /* Validate decompression - size */ + if (!exit_code && ((buf[4] | (buf[5] << 8)) |((buf[6] | (buf[7] << 8)) << 16)) != (unsigned long) bytes_out) { + error_msg("invalid compressed data--length error"); + exit_code = 1; + } + + free(window); + free(crc_table); + + window = NULL; + crc_table = NULL; + + return exit_code; +} diff --git a/src/libbb/wfopen.c b/src/libbb/wfopen.c new file mode 100644 index 0000000..f58ec90 --- /dev/null +++ b/src/libbb/wfopen.c @@ -0,0 +1,44 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.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 + */ + +#include <stdio.h> +#include <errno.h> +#include "libbb.h" + +FILE *wfopen(const char *path, const char *mode) +{ + FILE *fp; + if ((fp = fopen(path, mode)) == NULL) { + perror_msg("%s", path); + errno = 0; + } + return fp; +} + + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/xfuncs.c b/src/libbb/xfuncs.c new file mode 100644 index 0000000..f577315 --- /dev/null +++ b/src/libbb/xfuncs.c @@ -0,0 +1,93 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.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 + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include "libbb.h" + + +extern void *xmalloc(size_t size) +{ + void *ptr = malloc(size); + if (ptr == NULL && size != 0) + perror_msg_and_die("malloc"); + return ptr; +} + +extern void *xrealloc(void *ptr, size_t size) +{ + ptr = realloc(ptr, size); + if (ptr == NULL && size != 0) + perror_msg_and_die("realloc"); + return ptr; +} + +extern void *xcalloc(size_t nmemb, size_t size) +{ + void *ptr = calloc(nmemb, size); + if (ptr == NULL && nmemb != 0 && size != 0) + perror_msg_and_die("calloc"); + return ptr; +} + +extern char * xstrdup (const char *s) { + char *t; + + if (s == NULL) + return NULL; + + t = strdup (s); + + if (t == NULL) + perror_msg_and_die("strdup"); + + return t; +} + +extern char * xstrndup (const char *s, int n) { + char *t; + + if (s == NULL) + error_msg_and_die("xstrndup bug"); + + t = xmalloc(++n); + + return safe_strncpy(t,s,n); +} + +FILE *xfopen(const char *path, const char *mode) +{ + FILE *fp; + if ((fp = fopen(path, mode)) == NULL) + perror_msg_and_die("%s", path); + return fp; +} + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/src/libbb/xreadlink.c b/src/libbb/xreadlink.c new file mode 100644 index 0000000..7d77a3b --- /dev/null +++ b/src/libbb/xreadlink.c @@ -0,0 +1,37 @@ +/* + * xreadlink.c - safe implementation of readlink. + * Returns a NULL on failure... + */ + +#include <stdio.h> + +/* + * NOTE: This function returns a malloced char* that you will have to free + * yourself. You have been warned. + */ + +#include <unistd.h> +#include "libbb.h" + +extern char *xreadlink(const char *path) +{ + static const int GROWBY = 80; /* how large we will grow strings by */ + + char *buf = NULL; + int bufsize = 0, readsize = 0; + + do { + buf = xrealloc(buf, bufsize += GROWBY); + readsize = readlink(path, buf, bufsize); /* 1st try */ + if (readsize == -1) { + perror_msg("%s", path); + return NULL; + } + } + while (bufsize < readsize + 1); + + buf[readsize] = '\0'; + + return buf; +} + |