Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (17 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/archival/unzip.c

    r821 r1765  
    2525 */
    2626
    27 #include <fcntl.h>
    28 #include <stdlib.h>
    29 #include <string.h>
    30 #include <unistd.h>
    31 #include <errno.h>
     27#include "libbb.h"
    3228#include "unarchive.h"
    33 #include "busybox.h"
    34 
    35 #if BB_BIG_ENDIAN
    36 static inline unsigned short
    37 __swap16(unsigned short x) {
    38     return (((uint16_t)(x) & 0xFF) << 8) | (((uint16_t)(x) & 0xFF00) >> 8);
    39 }
    40 
    41 static inline uint32_t
    42 __swap32(uint32_t x) {
    43      return (((x & 0xFF) << 24) |
    44         ((x & 0xFF00) << 8) |
    45         ((x & 0xFF0000) >> 8) |
    46         ((x & 0xFF000000) >> 24));
    47 }
    48 #else /* it's little-endian */
    49 # define __swap16(x) (x)
    50 # define __swap32(x) (x)
    51 #endif /* BB_BIG_ENDIAN */
    52 
    53 #define ZIP_FILEHEADER_MAGIC        __swap32(0x04034b50)
    54 #define ZIP_CDS_MAGIC           __swap32(0x02014b50)
    55 #define ZIP_CDS_END_MAGIC       __swap32(0x06054b50)
    56 #define ZIP_DD_MAGIC            __swap32(0x08074b50)
    57 
    58 extern unsigned int gunzip_crc;
    59 extern unsigned int gunzip_bytes_out;
     29
     30#define ZIP_FILEHEADER_MAGIC        SWAP_LE32(0x04034b50)
     31#define ZIP_CDS_MAGIC           SWAP_LE32(0x02014b50)
     32#define ZIP_CDS_END_MAGIC       SWAP_LE32(0x06054b50)
     33#define ZIP_DD_MAGIC            SWAP_LE32(0x08074b50)
    6034
    6135typedef union {
     
    7246        unsigned short filename_len;    /* 22-23 */
    7347        unsigned short extra_len;       /* 24-25 */
    74     } formated ATTRIBUTE_PACKED;
     48    } formatted ATTRIBUTE_PACKED;
    7549} zip_header_t;
    7650
     
    7852{
    7953    if (lseek(fd, skip, SEEK_CUR) == (off_t)-1) {
    80         if ((errno != ESPIPE) || (bb_copyfd_size(fd, -1, skip) != skip)) {
    81             bb_error_msg_and_die("Seek failure");
    82         }
    83     }
    84 }
    85 
    86 static void unzip_read(int fd, void *buf, size_t count)
    87 {
    88     if (bb_xread(fd, buf, count) != count) {
    89         bb_error_msg_and_die(bb_msg_read_error);
     54        if (errno != ESPIPE)
     55            bb_error_msg_and_die("seek failure");
     56        bb_copyfd_exact_size(fd, -1, skip);
    9057    }
    9158}
     
    9461{
    9562    /* Create all leading directories */
    96     char *name = bb_xstrdup(fn);
     63    char *name = xstrdup(fn);
    9764    if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
    98         bb_error_msg_and_die("Exiting"); /* bb_make_directory is noisy */
     65        bb_error_msg_and_die("exiting"); /* bb_make_directory is noisy */
    9966    }
    10067    free(name);
     
    10370static int unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
    10471{
    105     if (zip_header->formated.method == 0) {
     72    if (zip_header->formatted.method == 0) {
    10673        /* Method 0 - stored (not compressed) */
    107         int size = zip_header->formated.ucmpsize;
    108         if (size && (bb_copyfd_size(src_fd, dst_fd, size) != size)) {
    109             bb_error_msg_and_die("Cannot complete extraction");
    110         }
    111 
     74        off_t size = zip_header->formatted.ucmpsize;
     75        if (size)
     76            bb_copyfd_exact_size(src_fd, dst_fd, size);
    11277    } else {
    11378        /* Method 8 - inflate */
    114         inflate_init(zip_header->formated.cmpsize);
    115         inflate_unzip(src_fd, dst_fd);
    116         inflate_cleanup();
     79        inflate_unzip_result res;
     80        /* err = */ inflate_unzip(&res, zip_header->formatted.cmpsize, src_fd, dst_fd);
     81// we should check for -1 error return
    11782        /* Validate decompression - crc */
    118         if (zip_header->formated.crc32 != (gunzip_crc ^ 0xffffffffL)) {
    119             bb_error_msg("Invalid compressed data--crc error");
     83        if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) {
     84            bb_error_msg("invalid compressed data--%s error", "crc");
    12085            return 1;
    12186        }
    12287        /* Validate decompression - size */
    123         if (zip_header->formated.ucmpsize != gunzip_bytes_out) {
    124             bb_error_msg("Invalid compressed data--length error");
     88        if (zip_header->formatted.ucmpsize != res.bytes_out) {
     89            bb_error_msg("invalid compressed data--%s error", "length");
    12590            return 1;
    12691        }
     
    12994}
    13095
     96int unzip_main(int argc, char **argv);
    13197int unzip_main(int argc, char **argv)
    13298{
     
    145111    struct stat stat_buf;
    146112
    147     while((opt = getopt(argc, argv, "-d:lnopqx")) != -1) {
    148         switch(opt_range) {
     113    while ((opt = getopt(argc, argv, "-d:lnopqx")) != -1) {
     114        switch (opt_range) {
    149115        case 0: /* Options */
    150             switch(opt) {
     116            switch (opt) {
    151117            case 'l': /* List */
    152118                verbosity = v_list;
     
    169135
    170136            case 1 : /* The zip file */
    171                 src_fn = bb_xstrndup(optarg, strlen(optarg)+4);
     137                src_fn = xmalloc(strlen(optarg)+4);
     138                strcpy(src_fn, optarg);
    172139                opt_range++;
    173140                break;
     
    218185
    219186    /* Open input file */
    220     if (strcmp("-", src_fn) == 0) {
     187    if (LONE_DASH(src_fn)) {
    221188        src_fd = STDIN_FILENO;
    222189        /* Cannot use prompt mode since zip data is arriving on STDIN */
    223190        overwrite = (overwrite == o_prompt) ? o_never : overwrite;
    224 
    225191    } else {
    226192        static const char *const extn[] = {"", ".zip", ".ZIP"};
    227193        int orig_src_fn_len = strlen(src_fn);
    228         for(i = 0; (i < 3) && (src_fd == -1); i++) {
     194        for (i = 0; (i < 3) && (src_fd == -1); i++) {
    229195            strcpy(src_fn + orig_src_fn_len, extn[i]);
    230196            src_fd = open(src_fn, O_RDONLY);
    231197        }
    232198        if (src_fd == -1) {
    233             src_fn[orig_src_fn_len] = 0;
    234             bb_error_msg_and_die("Cannot open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
     199            src_fn[orig_src_fn_len] = '\0';
     200            bb_error_msg_and_die("cannot open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
    235201        }
    236202    }
     
    238204    /* Change dir if necessary */
    239205    if (base_dir)
    240         bb_xchdir(base_dir);
     206        xchdir(base_dir);
    241207
    242208    if (verbosity != v_silent)
     
    249215
    250216        /* Check magic number */
    251         unzip_read(src_fd, &magic, 4);
     217        xread(src_fd, &magic, 4);
    252218        if (magic == ZIP_CDS_MAGIC) {
    253219            break;
    254220        } else if (magic != ZIP_FILEHEADER_MAGIC) {
    255             bb_error_msg_and_die("Invalid zip magic %08X", magic);
     221            bb_error_msg_and_die("invalid zip magic %08X", magic);
    256222        }
    257223
    258224        /* Read the file header */
    259         unzip_read(src_fd, zip_header.raw, 26);
    260 #if BB_BIG_ENDIAN
    261         zip_header.formated.version = __swap16(zip_header.formated.version);
    262         zip_header.formated.flags = __swap16(zip_header.formated.flags);
    263         zip_header.formated.method = __swap16(zip_header.formated.method);
    264         zip_header.formated.modtime = __swap16(zip_header.formated.modtime);
    265         zip_header.formated.moddate = __swap16(zip_header.formated.moddate);
    266         zip_header.formated.crc32 = __swap32(zip_header.formated.crc32);
    267         zip_header.formated.cmpsize = __swap32(zip_header.formated.cmpsize);
    268         zip_header.formated.ucmpsize = __swap32(zip_header.formated.ucmpsize);
    269         zip_header.formated.filename_len = __swap16(zip_header.formated.filename_len);
    270         zip_header.formated.extra_len = __swap16(zip_header.formated.extra_len);
    271 #endif /* BB_BIG_ENDIAN */
    272         if ((zip_header.formated.method != 0) && (zip_header.formated.method != 8)) {
    273             bb_error_msg_and_die("Unsupported compression method %d", zip_header.formated.method);
     225        xread(src_fd, zip_header.raw, 26);
     226        zip_header.formatted.version = SWAP_LE32(zip_header.formatted.version);
     227        zip_header.formatted.flags = SWAP_LE32(zip_header.formatted.flags);
     228        zip_header.formatted.method = SWAP_LE32(zip_header.formatted.method);
     229        zip_header.formatted.modtime = SWAP_LE32(zip_header.formatted.modtime);
     230        zip_header.formatted.moddate = SWAP_LE32(zip_header.formatted.moddate);
     231        zip_header.formatted.crc32 = SWAP_LE32(zip_header.formatted.crc32);
     232        zip_header.formatted.cmpsize = SWAP_LE32(zip_header.formatted.cmpsize);
     233        zip_header.formatted.ucmpsize = SWAP_LE32(zip_header.formatted.ucmpsize);
     234        zip_header.formatted.filename_len = SWAP_LE32(zip_header.formatted.filename_len);
     235        zip_header.formatted.extra_len = SWAP_LE32(zip_header.formatted.extra_len);
     236        if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
     237            bb_error_msg_and_die("unsupported compression method %d", zip_header.formatted.method);
    274238        }
    275239
    276240        /* Read filename */
    277241        free(dst_fn);
    278         dst_fn = xzalloc(zip_header.formated.filename_len + 1);
    279         unzip_read(src_fd, dst_fn, zip_header.formated.filename_len);
     242        dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
     243        xread(src_fd, dst_fn, zip_header.formatted.filename_len);
    280244
    281245        /* Skip extra header bytes */
    282         unzip_skip(src_fd, zip_header.formated.extra_len);
     246        unzip_skip(src_fd, zip_header.formatted.extra_len);
    283247
    284248        if ((verbosity == v_list) && !list_header_done){
    285             printf("  Length     Date   Time    Name\n"
    286                    " --------    ----   ----    ----\n");
     249            puts("  Length     Date   Time    Name\n"
     250                 " --------    ----   ----    ----");
    287251            list_header_done = 1;
    288252        }
     
    294258
    295259        } else { /* Extract entry */
    296             total_size += zip_header.formated.ucmpsize;
     260            total_size += zip_header.formatted.ucmpsize;
    297261
    298262            if (verbosity == v_list) { /* List entry */
    299                 unsigned int dostime = zip_header.formated.modtime | (zip_header.formated.moddate << 16);
     263                unsigned int dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
    300264                printf("%9u  %02u-%02u-%02u %02u:%02u   %s\n",
    301                        zip_header.formated.ucmpsize,
     265                       zip_header.formatted.ucmpsize,
    302266                       (dostime & 0x01e00000) >> 21,
    303267                       (dostime & 0x001f0000) >> 16,
     
    308272                total_entries++;
    309273                i = 'n';
    310 
    311274            } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
    312275                i = -1;
    313 
    314276            } else if (last_char_is(dst_fn, '/')) { /* Extract directory */
    315277                if (stat(dst_fn, &stat_buf) == -1) {
    316278                    if (errno != ENOENT) {
    317                         bb_perror_msg_and_die("Cannot stat '%s'",dst_fn);
     279                        bb_perror_msg_and_die("cannot stat '%s'",dst_fn);
    318280                    }
    319281                    if (verbosity == v_normal) {
     
    322284                    unzip_create_leading_dirs(dst_fn);
    323285                    if (bb_make_directory(dst_fn, 0777, 0)) {
    324                         bb_error_msg_and_die("Exiting");
     286                        bb_error_msg_and_die("exiting");
    325287                    }
    326288                } else {
     
    332294
    333295            } else {  /* Extract file */
    334             _check_file:
     296 _check_file:
    335297                if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
    336298                    if (errno != ENOENT) {
    337                         bb_perror_msg_and_die("Cannot stat '%s'",dst_fn);
     299                        bb_perror_msg_and_die("cannot stat '%s'",dst_fn);
    338300                    }
    339301                    i = 'y';
    340 
    341302                } else { /* File already exists */
    342303                    if (overwrite == o_never) {
    343304                        i = 'n';
    344 
    345305                    } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
    346306                        if (overwrite == o_always) {
     
    349309                            printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
    350310                            if (!fgets(key_buf, 512, stdin)) {
    351                                 bb_perror_msg_and_die("Cannot read input");
     311                                bb_perror_msg_and_die("cannot read input");
    352312                            }
    353313                            i = key_buf[0];
    354314                        }
    355 
    356315                    } else { /* File is not regular file */
    357316                        bb_error_msg_and_die("'%s' exists but is not regular file",dst_fn);
     
    366325        case 'y': /* Open file and fall into unzip */
    367326            unzip_create_leading_dirs(dst_fn);
    368             dst_fd = bb_xopen(dst_fn, O_WRONLY | O_CREAT);
     327            dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
    369328        case -1: /* Unzip */
    370329            if (verbosity == v_normal) {
     
    372331            }
    373332            if (unzip_extract(&zip_header, src_fd, dst_fd)) {
    374                 failed = 1;
     333                failed = 1;
    375334            }
    376335            if (dst_fd != STDOUT_FILENO) {
     
    384343        case 'n':
    385344            /* Skip entry data */
    386             unzip_skip(src_fd, zip_header.formated.cmpsize);
     345            unzip_skip(src_fd, zip_header.formatted.cmpsize);
    387346            break;
    388347
     
    391350            printf("new name: ");
    392351            if (!fgets(key_buf, 512, stdin)) {
    393                 bb_perror_msg_and_die("Cannot read input");
     352                bb_perror_msg_and_die("cannot read input");
    394353            }
    395354            free(dst_fn);
    396             dst_fn = bb_xstrdup(key_buf);
     355            dst_fn = xstrdup(key_buf);
    397356            chomp(dst_fn);
    398357            goto _check_file;
     
    404363
    405364        /* Data descriptor section */
    406         if (zip_header.formated.flags & 4) {
     365        if (zip_header.formatted.flags & 4) {
    407366            /* skip over duplicate crc, compressed size and uncompressed size */
    408367            unzip_skip(src_fd, 12);
Note: See TracChangeset for help on using the changeset viewer.