2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, 2010 Joerg Sonnenberger <joerg@NetBSD.org>
5 * Copyright (c) 2007-2008 Dag-Erling Smørgrav
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * This file would be much shorter if we didn't care about command-line
33 * compatibility with Info-ZIP's UnZip, which requires us to duplicate
34 * parts of libarchive in order to gain more detailed control of its
35 * behaviour for the purpose of implementing the -n, -o, -L and -a
39 #include <sys/queue.h>
53 #include <archive_entry.h>
55 /* command-line options */
56 static int a_opt; /* convert EOL */
57 static int C_opt; /* match case-insensitively */
58 static int c_opt; /* extract to stdout */
59 static const char *d_arg; /* directory */
60 static int f_opt; /* update existing files only */
61 static int j_opt; /* junk directories */
62 static int L_opt; /* lowercase names */
63 static int n_opt; /* never overwrite */
64 static int o_opt; /* always overwrite */
65 static int p_opt; /* extract to stdout, quiet */
66 static int q_opt; /* quiet */
67 static int t_opt; /* test */
68 static int u_opt; /* update */
69 static int v_opt; /* verbose/list */
70 static const char *y_str = ""; /* 4 digit year */
71 static int Z1_opt; /* zipinfo mode list files only */
74 static int unzip_debug;
77 static int zipinfo_mode;
82 /* convenience macro */
83 /* XXX should differentiate between ARCHIVE_{WARN,FAIL,RETRY} */
87 if (acret != ARCHIVE_OK) \
88 errorx("%s", archive_error_string(a)); \
92 * Indicates that last info() did not end with EOL. This helps error() et
93 * al. avoid printing an error message on the same line as an incomplete
94 * informational message.
98 /* fatal error message + errno */
100 error(const char *fmt, ...)
105 fprintf(stdout, "\n");
107 fprintf(stderr, "unzip: ");
109 vfprintf(stderr, fmt, ap);
111 fprintf(stderr, ": %s\n", strerror(errno));
115 /* fatal error message, no errno */
117 errorx(const char *fmt, ...)
122 fprintf(stdout, "\n");
124 fprintf(stderr, "unzip: ");
126 vfprintf(stderr, fmt, ap);
128 fprintf(stderr, "\n");
132 /* non-fatal error message + errno */
134 warning(const char *fmt, ...)
139 fprintf(stdout, "\n");
141 fprintf(stderr, "unzip: ");
143 vfprintf(stderr, fmt, ap);
145 fprintf(stderr, ": %s\n", strerror(errno));
148 /* non-fatal error message, no errno */
150 warningx(const char *fmt, ...)
155 fprintf(stdout, "\n");
157 fprintf(stderr, "unzip: ");
159 vfprintf(stderr, fmt, ap);
161 fprintf(stderr, "\n");
164 /* informational message (if not -q) */
166 info(const char *fmt, ...)
170 if (q_opt && !unzip_debug)
173 vfprintf(stdout, fmt, ap);
180 noeol = fmt[strlen(fmt) - 1] != '\n';
183 /* debug message (if unzip_debug) */
185 debug(const char *fmt, ...)
192 vfprintf(stderr, fmt, ap);
199 noeol = fmt[strlen(fmt) - 1] != '\n';
202 /* duplicate a path name, possibly converting to lower case */
204 pathdup(const char *path)
210 while (len && path[len - 1] == '/')
212 if ((str = malloc(len + 1)) == NULL) {
217 for (i = 0; i < len; ++i)
218 str[i] = tolower((unsigned char)path[i]);
220 memcpy(str, path, len);
227 /* concatenate two path names */
229 pathcat(const char *prefix, const char *path)
234 prelen = prefix ? strlen(prefix) + 1 : 0;
235 len = strlen(path) + 1;
236 if ((str = malloc(prelen + len)) == NULL) {
241 memcpy(str, prefix, prelen); /* includes zero */
242 str[prelen - 1] = '/'; /* splat zero */
244 memcpy(str + prelen, path, len); /* includes zero */
250 * Pattern lists for include / exclude processing
253 STAILQ_ENTRY(pattern) link;
257 STAILQ_HEAD(pattern_list, pattern);
258 static struct pattern_list include = STAILQ_HEAD_INITIALIZER(include);
259 static struct pattern_list exclude = STAILQ_HEAD_INITIALIZER(exclude);
262 * Add an entry to a pattern list
265 add_pattern(struct pattern_list *list, const char *pattern)
267 struct pattern *entry;
270 debug("adding pattern '%s'\n", pattern);
271 len = strlen(pattern);
272 if ((entry = malloc(sizeof *entry + len + 1)) == NULL) {
276 memcpy(entry->pattern, pattern, len + 1);
277 STAILQ_INSERT_TAIL(list, entry, link);
281 * Match a string against a list of patterns
284 match_pattern(struct pattern_list *list, const char *str)
286 struct pattern *entry;
288 STAILQ_FOREACH(entry, list, link) {
289 if (fnmatch(entry->pattern, str, C_opt ? FNM_CASEFOLD : 0) == 0)
296 * Verify that a given pathname is in the include list and not in the
300 accept_pathname(const char *pathname)
303 if (!STAILQ_EMPTY(&include) && !match_pattern(&include, pathname))
305 if (!STAILQ_EMPTY(&exclude) && match_pattern(&exclude, pathname))
311 * Create the specified directory with the specified mode, taking certain
312 * precautions on they way.
315 make_dir(const char *path, int mode)
319 if (lstat(path, &sb) == 0) {
320 if (S_ISDIR(sb.st_mode))
323 * Normally, we should either ask the user about removing
324 * the non-directory of the same name as a directory we
325 * wish to create, or respect the -n or -o command-line
326 * options. However, this may lead to a later failure or
327 * even compromise (if this non-directory happens to be a
328 * symlink to somewhere unsafe), so we don't.
332 * Don't check unlink() result; failure will cause mkdir()
333 * to fail later, which we will catch.
337 if (mkdir(path, mode) != 0 && errno != EEXIST)
338 error("mkdir('%s')", path);
342 * Ensure that all directories leading up to (but not including) the
343 * specified path exist.
345 * XXX inefficient + modifies the file in-place
348 make_parent(char *path)
353 sep = strrchr(path, '/');
354 if (sep == NULL || sep == path)
357 if (lstat(path, &sb) == 0) {
358 if (S_ISDIR(sb.st_mode)) {
369 for (sep = path; (sep = strchr(sep, '/')) != NULL; sep++) {
370 /* root in case of absolute d_arg */
374 make_dir(path, 0755);
381 * Extract a directory.
384 extract_dir(struct archive *a, struct archive_entry *e, const char *path)
389 * Dropbox likes to create '/' directory entries, just ignore
395 mode = archive_entry_mode(e) & 0777;
400 * Some zipfiles contain directories with weird permissions such
401 * as 0644 or 0444. This can cause strange issues such as being
402 * unable to extract files into the directory we just created, or
403 * the user being unable to remove the directory later without
404 * first manually changing its permissions. Therefore, we whack
405 * the permissions into shape, assuming that the user wants full
406 * access and that anyone who gets read access also gets execute
415 info(" creating: %s/\n", path);
416 make_dir(path, mode);
417 ac(archive_read_data_skip(a));
420 static unsigned char buffer[8192];
421 static char spinner[] = { '|', '/', '-', '\\' };
424 handle_existing_file(char **path)
432 "replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ",
434 if (fgets(buf, sizeof(buf), stdin) == NULL) {
436 printf("NULL\n(EOF or read error, "
437 "treating as \"[N]one\"...)\n");
456 printf("New name: ");
461 len = getline(path, &alen, stdin);
462 if ((*path)[len - 1] == '\n')
463 (*path)[len - 1] = '\0';
472 * Detect binary files by a combination of character white list and
473 * black list. NUL bytes and other control codes without use in text files
474 * result directly in switching the file to binary mode. Otherwise, at least
475 * one white-listed byte has to be found.
477 * Black-listed: 0..6, 14..25, 28..31
478 * 0xf3ffc07f = 11110011111111111100000001111111b
479 * White-listed: 9..10, 13, >= 32
480 * 0x00002600 = 00000000000000000010011000000000b
482 * See the proginfo/txtvsbin.txt in the zip sources for a detailed discussion.
484 #define BYTE_IS_BINARY(x) ((x) < 32 && (0xf3ffc07fU & (1U << (x))))
485 #define BYTE_IS_TEXT(x) ((x) >= 32 || (0x00002600U & (1U << (x))))
488 check_binary(const unsigned char *buf, size_t len)
491 for (rv = 1; len--; ++buf) {
492 if (BYTE_IS_BINARY(*buf))
494 if (BYTE_IS_TEXT(*buf))
502 * Extract to a file descriptor
505 extract2fd(struct archive *a, char *pathname, int fd)
509 unsigned char *p, *q, *end;
515 /* loop over file contents and write to fd */
516 for (int n = 0; ; n++) {
517 if (fd != STDOUT_FILENO)
518 if (tty && (n % 4) == 0)
519 info(" %c\b\b", spinner[(n / 4) % sizeof spinner]);
521 len = archive_read_data(a, buffer, sizeof buffer);
526 /* left over CR from previous buffer */
528 if (len == 0 || buffer[0] != '\n')
529 if (write(fd, "\r", 1) != 1)
530 error("write('%s')", pathname);
540 * Detect whether this is a text file. The correct way to
541 * do this is to check the least significant bit of the
542 * "internal file attributes" field of the corresponding
543 * file header in the central directory, but libarchive
544 * does not provide access to this field, so we have to
545 * guess by looking for non-ASCII characters in the
546 * buffer. Hopefully we won't guess wrong. If we do
547 * guess wrong, we print a warning message later.
549 if (a_opt && n == 0) {
550 if (check_binary(buffer, len))
555 if (!a_opt || !text) {
556 if (write(fd, buffer, len) != len)
557 error("write('%s')", pathname);
561 /* hard case: convert \r\n to \n (sigh...) */
562 for (p = buffer; p < end; p = q + 1) {
563 for (q = p; q < end; q++) {
564 if (!warn && BYTE_IS_BINARY(*q)) {
565 warningx("%s may be corrupted due"
566 " to weak text file detection"
567 " heuristic", pathname);
579 if (write(fd, p, q - p) != q - p)
580 error("write('%s')", pathname);
588 * Extract a regular file.
591 extract_file(struct archive *a, struct archive_entry *e, char **path)
594 struct timespec mtime;
596 struct timespec ts[2];
598 const char *linkname;
600 mode = archive_entry_mode(e) & 0777;
603 mtime.tv_sec = archive_entry_mtime(e);
604 mtime.tv_nsec = archive_entry_mtime_nsec(e);
606 /* look for existing file of same name */
608 if (lstat(*path, &sb) == 0) {
609 if (u_opt || f_opt) {
610 /* check if up-to-date */
611 if (S_ISREG(sb.st_mode) &&
612 (sb.st_mtim.tv_sec > mtime.tv_sec ||
613 (sb.st_mtim.tv_sec == mtime.tv_sec &&
614 sb.st_mtim.tv_nsec >= mtime.tv_nsec)))
621 /* do not overwrite */
624 check = handle_existing_file(path);
628 return; /* do not overwrite */
636 ts[0].tv_nsec = UTIME_NOW;
639 /* process symlinks */
640 linkname = archive_entry_symlink(e);
641 if (linkname != NULL) {
642 if (symlink(linkname, *path) != 0)
643 error("symlink('%s')", *path);
644 info(" extracting: %s -> %s\n", *path, linkname);
645 if (lchmod(*path, mode) != 0)
646 warning("Cannot set mode for '%s'", *path);
647 /* set access and modification time */
648 if (utimensat(AT_FDCWD, *path, ts, AT_SYMLINK_NOFOLLOW) != 0)
649 warning("utimensat('%s')", *path);
653 if ((fd = open(*path, O_RDWR|O_CREAT|O_TRUNC, mode)) < 0)
654 error("open('%s')", *path);
656 info(" extracting: %s", *path);
658 text = extract2fd(a, *path, fd);
666 /* set access and modification time */
667 if (futimens(fd, ts) != 0)
668 error("futimens('%s')", *path);
670 error("close('%s')", *path);
674 * Extract a zipfile entry: first perform some sanity checks to ensure
675 * that it is either a directory or a regular file and that the path is
676 * not absolute and does not try to break out of the current directory;
677 * then call either extract_dir() or extract_file() as appropriate.
679 * This is complicated a bit by the various ways in which we need to
680 * manipulate the path name. Case conversion (if requested by the -L
681 * option) happens first, but the include / exclude patterns are applied
682 * to the full converted path name, before the directory part of the path
683 * is removed in accordance with the -j option. Sanity checks are
684 * intentionally done earlier than they need to be, so the user will get a
685 * warning about insecure paths even for files or directories which
686 * wouldn't be extracted anyway.
689 extract(struct archive *a, struct archive_entry *e)
691 char *pathname, *realpathname;
695 pathname = pathdup(archive_entry_pathname(e));
696 filetype = archive_entry_filetype(e);
699 if (pathname[0] == '/' ||
700 strncmp(pathname, "../", 3) == 0 ||
701 strstr(pathname, "/../") != NULL) {
702 warningx("skipping insecure entry '%s'", pathname);
703 ac(archive_read_data_skip(a));
708 /* I don't think this can happen in a zipfile.. */
709 if (!S_ISDIR(filetype) && !S_ISREG(filetype) && !S_ISLNK(filetype)) {
710 warningx("skipping non-regular entry '%s'", pathname);
711 ac(archive_read_data_skip(a));
716 /* skip directories in -j case */
717 if (S_ISDIR(filetype) && j_opt) {
718 ac(archive_read_data_skip(a));
723 /* apply include / exclude patterns */
724 if (!accept_pathname(pathname)) {
725 ac(archive_read_data_skip(a));
730 /* apply -j and -d */
732 for (p = q = pathname; *p; ++p)
735 realpathname = pathcat(d_arg, q);
737 realpathname = pathcat(d_arg, pathname);
740 /* ensure that parent directory exists */
741 make_parent(realpathname);
743 if (S_ISDIR(filetype))
744 extract_dir(a, e, realpathname);
746 extract_file(a, e, &realpathname);
753 extract_stdout(struct archive *a, struct archive_entry *e)
758 pathname = pathdup(archive_entry_pathname(e));
759 filetype = archive_entry_filetype(e);
761 /* I don't think this can happen in a zipfile.. */
762 if (!S_ISDIR(filetype) && !S_ISREG(filetype) && !S_ISLNK(filetype)) {
763 warningx("skipping non-regular entry '%s'", pathname);
764 ac(archive_read_data_skip(a));
769 /* skip directories in -j case */
770 if (S_ISDIR(filetype)) {
771 ac(archive_read_data_skip(a));
776 /* apply include / exclude patterns */
777 if (!accept_pathname(pathname)) {
778 ac(archive_read_data_skip(a));
784 info("x %s\n", pathname);
786 (void)extract2fd(a, pathname, STDOUT_FILENO);
792 * Print the name of an entry to stdout.
795 list(struct archive *a, struct archive_entry *e)
801 mtime = archive_entry_mtime(e);
802 tm = localtime(&mtime);
804 strftime(buf, sizeof(buf), "%m-%d-%G %R", tm);
806 strftime(buf, sizeof(buf), "%m-%d-%g %R", tm);
810 printf(" %8ju %s %s\n",
811 (uintmax_t)archive_entry_size(e),
812 buf, archive_entry_pathname(e));
813 } else if (v_opt == 2) {
814 printf("%8ju Stored %7ju 0%% %s %08x %s\n",
815 (uintmax_t)archive_entry_size(e),
816 (uintmax_t)archive_entry_size(e),
819 archive_entry_pathname(e));
823 printf("%s\n",archive_entry_pathname(e));
825 ac(archive_read_data_skip(a));
829 * Extract to memory to check CRC
832 test(struct archive *a, struct archive_entry *e)
838 if (S_ISDIR(archive_entry_filetype(e)))
841 info(" testing: %s\t", archive_entry_pathname(e));
842 while ((len = archive_read_data(a, buffer, sizeof buffer)) > 0)
845 info(" %s\n", archive_error_string(a));
851 /* shouldn't be necessary, but it doesn't hurt */
852 ac(archive_read_data_skip(a));
858 * Main loop: open the zipfile, iterate over its contents and decide what
859 * to do with each entry.
862 unzip(const char *fn)
865 struct archive_entry *e;
867 uintmax_t total_size, file_count, error_count;
869 if ((a = archive_read_new()) == NULL)
870 error("archive_read_new failed");
872 ac(archive_read_support_format_zip(a));
873 ac(archive_read_open_filename(a, fn, 8192));
876 if (!p_opt && !q_opt)
877 printf("Archive: %s\n", fn);
879 printf(" Length %sDate Time Name\n", y_str);
880 printf(" -------- %s---- ---- ----\n", y_str);
881 } else if (v_opt == 2) {
882 printf(" Length Method Size Ratio %sDate Time CRC-32 Name\n", y_str);
883 printf("-------- ------ ------- ----- %s---- ---- ------ ----\n", y_str);
891 ret = archive_read_next_header(a, &e);
892 if (ret == ARCHIVE_EOF)
897 error_count += test(a, e);
900 else if (p_opt || c_opt)
901 extract_stdout(a, e);
909 total_size += archive_entry_size(e);
915 printf(" -------- %s-------\n", y_str);
916 printf(" %8ju %s%ju file%s\n",
917 total_size, y_str, file_count, file_count != 1 ? "s" : "");
918 } else if (v_opt == 2) {
919 printf("-------- ------- --- %s-------\n", y_str);
920 printf("%8ju %7ju 0%% %s%ju file%s\n",
921 total_size, total_size, y_str, file_count,
922 file_count != 1 ? "s" : "");
926 ac(archive_read_free(a));
929 if (error_count > 0) {
930 errorx("%ju checksum error(s) found.", error_count);
933 printf("No errors detected in compressed data of %s.\n",
943 fprintf(stderr, "Usage: unzip [-aCcfjLlnopqtuvyZ1] [-d dir] [-x pattern] "
949 getopts(int argc, char *argv[])
953 optreset = optind = 1;
954 while ((opt = getopt(argc, argv, "aCcd:fjLlnopqtuvx:yZ1")) != -1)
1007 add_pattern(&exclude, optarg);
1023 main(int argc, char *argv[])
1025 const char *zipfile;
1028 if (isatty(STDOUT_FILENO))
1031 if (getenv("UNZIP_DEBUG") != NULL)
1033 for (int i = 0; i < argc; ++i)
1034 debug("%s%c", argv[i], (i < argc - 1) ? ' ' : '\n');
1037 * Info-ZIP's unzip(1) expects certain options to come before the
1038 * zipfile name, and others to come after - though it does not
1039 * enforce this. For simplicity, we accept *all* options both
1040 * before and after the zipfile name.
1042 nopts = getopts(argc, argv);
1045 * When more of the zipinfo mode options are implemented, this
1046 * will need to change.
1048 if (zipinfo_mode && !Z1_opt) {
1049 printf("Zipinfo mode needs additional options\n");
1055 zipfile = argv[nopts++];
1057 if (strcmp(zipfile, "-") == 0)
1058 zipfile = NULL; /* STDIN */
1060 while (nopts < argc && *argv[nopts] != '-')
1061 add_pattern(&include, argv[nopts++]);
1063 nopts--; /* fake argv[0] */
1064 nopts += getopts(argc - nopts, argv + nopts);
1066 if (n_opt + o_opt + u_opt > 1)
1067 errorx("-n, -o and -u are contradictory");