attempt to regularize atoi mess.
[oweals/busybox.git] / archival / libunarchive / get_header_tar.c
index 3bbe15d36a32e83ea26b4855a60c18535f59966d..d3cd96d0cc0c96eb4d0f557d03712b8cd3e15dee 100644 (file)
@@ -1,34 +1,29 @@
-/*
- *  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.
+/* vi: set sw=4 ts=4: */
+/* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  *
- *  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.
+ *  FIXME:
+ *    In privileged mode if uname and gname map to a uid and gid then use the
+ *    mapped value instead of the uid/gid values in tar header
  *
- *  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.
+ *  References:
+ *    GNU tar and star man pages,
+ *    Opengroup's ustar interchange format,
+ *     http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include "unarchive.h"
 #include "libbb.h"
+#include "unarchive.h"
 
 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
 static char *longname = NULL;
 static char *linkname = NULL;
 #endif
 
-extern char get_header_tar(archive_handle_t *archive_handle)
+char get_header_tar(archive_handle_t *archive_handle)
 {
        file_header_t *file_header = archive_handle->file_header;
        union {
+               /* ustar header, Posix 1003.1 */
                unsigned char raw[512];
                struct {
                        char name[100]; /*   0-99 */
@@ -48,46 +43,51 @@ extern char get_header_tar(archive_handle_t *archive_handle)
                        char devminor[8];       /* 337-344 */
                        char prefix[155];       /* 345-499 */
                        char padding[12];       /* 500-512 */
-               } formated;
+               } formatted;
        } tar;
        long sum = 0;
        long i;
-       char *tmp;
+       static int end = 0;
 
        /* Align header */
        data_align(archive_handle, 512);
 
-       if (archive_xread(archive_handle, tar.raw, 512) != 512) {
-               /* Assume end of file */
-               return(EXIT_FAILURE);
-       }
+       xread(archive_handle->src_fd, tar.raw, 512);
        archive_handle->offset += 512;
 
        /* If there is no filename its an empty header */
-       if (tar.formated.name[0] == 0) {
-               return(EXIT_SUCCESS);
+       if (tar.formatted.name[0] == 0) {
+               if (end) {
+                       /* This is the second consecutive empty header! End of archive!
+                        * Read until the end to empty the pipe from gz or bz2
+                        */
+                       while (full_read(archive_handle->src_fd, tar.raw, 512) == 512);
+                       return EXIT_FAILURE;
+               }
+               end = 1;
+               return EXIT_SUCCESS;
        }
+       end = 0;
 
        /* Check header has valid magic, "ustar" is for the proper tar
         * 0's are for the old tar format
         */
-       if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
-#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
-               if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
+       if (strncmp(tar.formatted.magic, "ustar", 5) != 0) {
+#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
+               if (strncmp(tar.formatted.magic, "\0\0\0\0\0", 5) != 0)
 #endif
-                       bb_error_msg_and_die("Invalid tar magic");
+                       bb_error_msg_and_die("invalid tar magic");
        }
        /* Do checksum on headers */
        for (i =  0; i < 148 ; i++) {
                sum += tar.raw[i];
        }
        sum += ' ' * 8;
-       for (i =  156; i < 512 ; i++) {
+       for (i = 156; i < 512 ; i++) {
                sum += tar.raw[i];
        }
-       if (sum != strtol(tar.formated.chksum, NULL, 8)) {
-               bb_error_msg("Invalid tar header checksum");
-               return(EXIT_FAILURE);
+       if (sum != xstrtoul(tar.formatted.chksum, 8)) {
+               bb_error_msg_and_die("invalid tar header checksum");
        }
 
 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
@@ -100,40 +100,45 @@ extern char get_header_tar(archive_handle_t *archive_handle)
                linkname = NULL;
        } else
 #endif
-       if (tar.formated.prefix[0] == 0) {
-               file_header->name = strdup(tar.formated.name);
-       } else {
-               file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
-       }
-       tmp = last_char_is(archive_handle->file_header->name, '/');
-       if (tmp) {
-               *tmp = '\0';
+       {
+               file_header->name = xstrndup(tar.formatted.name, 100);
+               if (tar.formatted.prefix[0]) {
+                       char *temp = file_header->name;
+                       file_header->name = concat_path_file(tar.formatted.prefix, temp);
+                       free(temp);
+               }
        }
 
-       file_header->mode = strtol(tar.formated.mode, NULL, 8);
-       file_header->uid = strtol(tar.formated.uid, NULL, 8);
-       file_header->gid = strtol(tar.formated.gid, NULL, 8);
-       file_header->size = strtol(tar.formated.size, NULL, 8);
-       file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
-       file_header->link_name = (tar.formated.linkname[0] != '\0') ? 
-           bb_xstrdup(tar.formated.linkname) : NULL;
-       file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
-                                strtol(tar.formated.devminor, NULL, 8));
-
-#if defined CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY || defined CONFIG_FEATURE_TAR_GNU_EXTENSIONS
-       /* Fix mode, used by the old format */
-       switch (tar.formated.typeflag) {
-# ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
-       case 0:
-       case '0':
-               file_header->mode |= S_IFREG;
-               break;
-#if 0
-       /* hard links are detected as entries with 0 size, a link name, 
-        * and not being a symlink, hence we have nothing to do here */
+       file_header->uid = xstrtoul(tar.formatted.uid, 8);
+       file_header->gid = xstrtoul(tar.formatted.gid, 8);
+       // TODO: LFS support
+       file_header->size = xstrtoul(tar.formatted.size, 8);
+       file_header->mtime = xstrtoul(tar.formatted.mtime, 8);
+       file_header->link_name = tar.formatted.linkname[0] ?
+                                xstrdup(tar.formatted.linkname) : NULL;
+       file_header->device = makedev(xstrtoul(tar.formatted.devmajor, 8),
+                                     xstrtoul(tar.formatted.devminor, 8));
+
+       /* Set bits 0-11 of the files mode */
+       file_header->mode = 07777 & xstrtoul(tar.formatted.mode, 8);
+
+       /* Set bits 12-15 of the files mode */
+       switch (tar.formatted.typeflag) {
+       /* busybox identifies hard links as being regular files with 0 size and a link name */
        case '1':
+               file_header->mode |= S_IFREG;
                break;
+       case '7':
+               /* Reserved for high performance files, treat as normal file */
+       case 0:
+       case '0':
+#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
+               if (last_char_is(file_header->name, '/')) {
+                       file_header->mode |= S_IFDIR;
+               } else
 #endif
+                       file_header->mode |= S_IFREG;
+               break;
        case '2':
                file_header->mode |= S_IFLNK;
                break;
@@ -149,44 +154,54 @@ extern char get_header_tar(archive_handle_t *archive_handle)
        case '6':
                file_header->mode |= S_IFIFO;
                break;
-# endif
-# ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
+#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
        case 'L': {
-                       longname = xmalloc(file_header->size + 1);
-                       archive_xread_all(archive_handle, longname, file_header->size);
-                       longname[file_header->size] = '\0';
+                       longname = xzalloc(file_header->size + 1);
+                       xread(archive_handle->src_fd, longname, file_header->size);
                        archive_handle->offset += file_header->size;
 
-                       return(get_header_tar(archive_handle));
+                       return get_header_tar(archive_handle);
                }
        case 'K': {
-                       linkname = xmalloc(file_header->size + 1);
-                       archive_xread_all(archive_handle, linkname, file_header->size);
-                       linkname[file_header->size] = '\0';
+                       linkname = xzalloc(file_header->size + 1);
+                       xread(archive_handle->src_fd, linkname, file_header->size);
                        archive_handle->offset += file_header->size;
 
                        file_header->name = linkname;
-                       return(get_header_tar(archive_handle));
+                       return get_header_tar(archive_handle);
                }
-       case 'D':
-       case 'M':
-       case 'N':
-       case 'S':
-       case 'V':
-               bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
-# endif
-       }
+       case 'D':       /* GNU dump dir */
+       case 'M':       /* Continuation of multi volume archive*/
+       case 'N':       /* Old GNU for names > 100 characters */
+       case 'S':       /* Sparse file */
+       case 'V':       /* Volume header */
 #endif
+       case 'g':       /* pax global header */
+       case 'x':       /* pax extended header */
+               bb_error_msg("ignoring extension type %c", tar.formatted.typeflag);
+               break;
+       default:
+               bb_error_msg("unknown typeflag: 0x%x", tar.formatted.typeflag);
+       }
+       {       /* Strip trailing '/' in directories */
+               /* Must be done after mode is set as '/' is used to check if its a directory */
+               char *tmp = last_char_is(file_header->name, '/');
+               if (tmp) {
+                       *tmp = '\0';
+               }
+       }
 
        if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
                archive_handle->action_header(archive_handle->file_header);
                archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
                archive_handle->action_data(archive_handle);
-               archive_handle->passed = llist_add_to(archive_handle->passed, archive_handle->file_header->name);
+               llist_add_to(&(archive_handle->passed), file_header->name);
        } else {
-               data_skip(archive_handle);                      
+               data_skip(archive_handle);
        }
        archive_handle->offset += file_header->size;
 
-       return(EXIT_SUCCESS);
+       free(file_header->link_name);
+
+       return EXIT_SUCCESS;
 }