Add SWAP_LE?? and SWAP_BE?? macros, and make things use them. Converts values
[oweals/busybox.git] / archival / unzip.c
index 7e0d107cc799c2c15b284c146acd45414ea64725..0b1e6f997da43e654374d593f9cec92d33c64ff5 100644 (file)
@@ -32,7 +32,7 @@
 #include "unarchive.h"
 #include "busybox.h"
 
-#if (BYTE_ORDER == BIG_ENDIAN)
+#if BB_BIG_ENDIAN
 static inline unsigned short
 __swap16(unsigned short x) {
        return (((uint16_t)(x) & 0xFF) << 8) | (((uint16_t)(x) & 0xFF00) >> 8);
@@ -45,10 +45,10 @@ __swap32(uint32_t x) {
                ((x & 0xFF0000) >> 8) |
                ((x & 0xFF000000) >> 24));
 }
-#else
-#define __swap16(x) (x)
-#define __swap32(x) (x)
-#endif
+#else /* it's little-endian */
+# define __swap16(x) (x)
+# define __swap32(x) (x)
+#endif /* BB_BIG_ENDIAN */
 
 #define ZIP_FILEHEADER_MAGIC           __swap32(0x04034b50)
 #define ZIP_CDS_MAGIC                  __swap32(0x02014b50)
@@ -100,7 +100,7 @@ static void unzip_create_leading_dirs(char *fn)
        free(name);
 }
 
-static void unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
+static int unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
 {
        if (zip_header->formated.method == 0) {
                /* Method 0 - stored (not compressed) */
@@ -117,15 +117,18 @@ static void unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
                /* Validate decompression - crc */
                if (zip_header->formated.crc32 != (gunzip_crc ^ 0xffffffffL)) {
                        bb_error_msg("Invalid compressed data--crc error");
+                       return 1;
                }
                /* Validate decompression - size */
                if (zip_header->formated.ucmpsize != gunzip_bytes_out) {
                        bb_error_msg("Invalid compressed data--length error");
+                       return 1;
                }
        }
+       return 0;
 }
 
-extern int unzip_main(int argc, char **argv)
+int unzip_main(int argc, char **argv)
 {
        zip_header_t zip_header;
        enum {v_silent, v_normal, v_list} verbosity = v_normal;
@@ -137,7 +140,7 @@ extern int unzip_main(int argc, char **argv)
        llist_t *zaccept = NULL;
        llist_t *zreject = NULL;
        char *base_dir = NULL;
-       int i, opt, opt_range = 0, list_header_done = 0;
+       int failed, i, opt, opt_range = 0, list_header_done = 0;
        char key_buf[512];
        struct stat stat_buf;
 
@@ -177,7 +180,7 @@ extern int unzip_main(int argc, char **argv)
 
                case 1: /* Include files */
                        if (opt == 1) {
-                               zaccept = llist_add_to(zaccept, optarg);
+                               llist_add_to(&zaccept, optarg);
 
                        } else if (opt == 'd') {
                                base_dir = optarg;
@@ -193,7 +196,7 @@ extern int unzip_main(int argc, char **argv)
 
                case 2 : /* Exclude files */
                        if (opt == 1) {
-                               zreject = llist_add_to(zreject, optarg);
+                               llist_add_to(&zreject, optarg);
 
                        } else if (opt == 'd') { /* Extract to base directory */
                                base_dir = optarg;
@@ -233,13 +236,14 @@ extern int unzip_main(int argc, char **argv)
        }
 
        /* Change dir if necessary */
-       if (base_dir && chdir(base_dir)) {
-               bb_perror_msg_and_die("Cannot chdir");
-       }
+       if (base_dir)
+               bb_xchdir(base_dir);
 
        if (verbosity != v_silent)
                printf("Archive:  %s\n", src_fn);
 
+       failed = 0;
+
        while (1) {
                unsigned int magic;
 
@@ -253,7 +257,7 @@ extern int unzip_main(int argc, char **argv)
 
                /* Read the file header */
                unzip_read(src_fd, zip_header.raw, 26);
-#if (BYTE_ORDER == BIG_ENDIAN)
+#if BB_BIG_ENDIAN
                zip_header.formated.version = __swap16(zip_header.formated.version);
                zip_header.formated.flags = __swap16(zip_header.formated.flags);
                zip_header.formated.method = __swap16(zip_header.formated.method);
@@ -264,7 +268,7 @@ extern int unzip_main(int argc, char **argv)
                zip_header.formated.ucmpsize = __swap32(zip_header.formated.ucmpsize);
                zip_header.formated.filename_len = __swap16(zip_header.formated.filename_len);
                zip_header.formated.extra_len = __swap16(zip_header.formated.extra_len);
-#endif
+#endif /* BB_BIG_ENDIAN */
                if ((zip_header.formated.method != 0) && (zip_header.formated.method != 8)) {
                        bb_error_msg_and_die("Unsupported compression method %d", zip_header.formated.method);
                }
@@ -367,7 +371,9 @@ extern int unzip_main(int argc, char **argv)
                        if (verbosity == v_normal) {
                                printf("  inflating: %s\n", dst_fn);
                        }
-                       unzip_extract(&zip_header, src_fd, dst_fd);
+                       if (unzip_extract(&zip_header, src_fd, dst_fd)) {
+                           failed = 1;
+                       }
                        if (dst_fd != STDOUT_FILENO) {
                                /* closing STDOUT is potentially bad for future business */
                                close(dst_fd);
@@ -409,14 +415,5 @@ extern int unzip_main(int argc, char **argv)
                       "%9d                   %d files\n", total_size, total_entries);
        }
 
-       return(EXIT_SUCCESS);
+       return failed;
 }
-
-/* END CODE */
-/*
-Local Variables:
-c-file-style: "linux"
-c-basic-offset: 4
-tab-width: 4
-End:
-*/