b66bd322bc98a627aae123aa5745bf78e4d4ddee
[oweals/busybox.git] / archival / libunarchive / get_header_tar.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or
5  *  (at your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *  GNU Library General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "unarchive.h"
21 #include "libbb.h"
22
23 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
24 static char *longname = NULL;
25 static char *linkname = NULL;
26 #endif
27
28 extern char get_header_tar(archive_handle_t *archive_handle)
29 {
30         file_header_t *file_header = archive_handle->file_header;
31         union {
32                 /* ustar header, Posix 1003.1 */
33                 unsigned char raw[512];
34                 struct {
35                         char name[100]; /*   0-99 */
36                         char mode[8];   /* 100-107 */
37                         char uid[8];    /* 108-115 */
38                         char gid[8];    /* 116-123 */
39                         char size[12];  /* 124-135 */
40                         char mtime[12]; /* 136-147 */
41                         char chksum[8]; /* 148-155 */
42                         char typeflag;  /* 156-156 */
43                         char linkname[100];     /* 157-256 */
44                         char magic[6];  /* 257-262 */
45                         char version[2];        /* 263-264 */
46                         char uname[32]; /* 265-296 */
47                         char gname[32]; /* 297-328 */
48                         char devmajor[8];       /* 329-336 */
49                         char devminor[8];       /* 337-344 */
50                         char prefix[155];       /* 345-499 */
51                         char padding[12];       /* 500-512 */
52                 } formated;
53         } tar;
54         long sum = 0;
55         long i;
56         char *tmp;
57
58         /* Align header */
59         data_align(archive_handle, 512);
60
61         if (archive_xread(archive_handle, tar.raw, 512) != 512) {
62                 /* Assume end of file */
63                 return(EXIT_FAILURE);
64         }
65         archive_handle->offset += 512;
66
67         /* If there is no filename its an empty header */
68         if (tar.formated.name[0] == 0) {
69                 return(EXIT_SUCCESS);
70         }
71
72         /* Check header has valid magic, "ustar" is for the proper tar
73          * 0's are for the old tar format
74          */
75         if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
76 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
77                 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
78 #endif
79                         bb_error_msg_and_die("Invalid tar magic");
80         }
81         /* Do checksum on headers */
82         for (i =  0; i < 148 ; i++) {
83                 sum += tar.raw[i];
84         }
85         sum += ' ' * 8;
86         for (i =  156; i < 512 ; i++) {
87                 sum += tar.raw[i];
88         }
89         if (sum != strtol(tar.formated.chksum, NULL, 8)) {
90                 bb_error_msg("Invalid tar header checksum");
91                 return(EXIT_FAILURE);
92         }
93
94 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
95         if (longname) {
96                 file_header->name = longname;
97                 longname = NULL;
98         }
99         else if (linkname) {
100                 file_header->name = linkname;
101                 linkname = NULL;
102         } else
103 #endif
104         if (tar.formated.prefix[0] == 0) {
105                 file_header->name = strdup(tar.formated.name);
106         } else {
107                 file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
108         }
109
110         file_header->mode = strtol(tar.formated.mode, NULL, 8);
111         file_header->uid = strtol(tar.formated.uid, NULL, 8);
112         file_header->gid = strtol(tar.formated.gid, NULL, 8);
113         file_header->size = strtol(tar.formated.size, NULL, 8);
114         file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
115         file_header->link_name = (tar.formated.linkname[0] != '\0') ? 
116             bb_xstrdup(tar.formated.linkname) : NULL;
117         file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
118                                  strtol(tar.formated.devminor, NULL, 8));
119
120 #if defined CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY || defined CONFIG_FEATURE_TAR_GNU_EXTENSIONS
121         /* Fix mode, used by the old format */
122         switch (tar.formated.typeflag) {
123 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
124         case 0:
125         case '0':
126                 if (last_char_is(file_header->name, '/')) {
127                         file_header->mode |= S_IFDIR;
128                 } else {
129                         file_header->mode |= S_IFREG;
130                 }
131                 break;
132         case '2':
133                 file_header->mode |= S_IFLNK;
134                 break;
135         case '3':
136                 file_header->mode |= S_IFCHR;
137                 break;
138         case '4':
139                 file_header->mode |= S_IFBLK;
140                 break;
141         case '5':
142                 file_header->mode |= S_IFDIR;
143                 break;
144         case '6':
145                 file_header->mode |= S_IFIFO;
146                 break;
147 # endif
148         /* hard links are detected as entries with 0 size, a link name, 
149          * and not being a symlink, hence we have nothing to do here */
150         case '1':
151                 file_header->mode |= ~S_IFLNK;
152                 break;
153 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
154         case 'L': {
155                         longname = xmalloc(file_header->size + 1);
156                         archive_xread_all(archive_handle, longname, file_header->size);
157                         longname[file_header->size] = '\0';
158                         archive_handle->offset += file_header->size;
159
160                         return(get_header_tar(archive_handle));
161                 }
162         case 'K': {
163                         linkname = xmalloc(file_header->size + 1);
164                         archive_xread_all(archive_handle, linkname, file_header->size);
165                         linkname[file_header->size] = '\0';
166                         archive_handle->offset += file_header->size;
167
168                         file_header->name = linkname;
169                         return(get_header_tar(archive_handle));
170                 }
171         case 'D':
172         case 'M':
173         case 'N':
174         case 'S':
175         case 'V':
176                 bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
177 # endif
178         }
179 #endif
180
181         if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
182                 archive_handle->action_header(archive_handle->file_header);
183                 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
184                 archive_handle->action_data(archive_handle);
185                 archive_handle->passed = llist_add_to(archive_handle->passed, archive_handle->file_header->name);
186         } else {
187                 data_skip(archive_handle);                      
188         }
189         archive_handle->offset += file_header->size;
190
191         return(EXIT_SUCCESS);
192 }